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 |
| 5 | * Copyright (c) 2004-2007, 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 | |
| 37 | #include <linux/module.h> |
| 38 | #include <linux/types.h> |
| 39 | #include <linux/net.h> |
| 40 | #include <linux/socket.h> |
| 41 | #include <linux/errno.h> |
| 42 | #include <linux/mm.h> |
| 43 | #include <linux/slab.h> |
| 44 | #include <linux/poll.h> |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 45 | #include <linux/fcntl.h> |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 46 | #include <asm/semaphore.h> |
| 47 | #include <asm/string.h> |
| 48 | #include <asm/atomic.h> |
| 49 | #include <net/sock.h> |
| 50 | |
| 51 | #include <linux/tipc.h> |
Per Liden | ea714cc | 2006-01-11 12:28:47 +0100 | [diff] [blame] | 52 | #include <linux/tipc_config.h> |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 53 | #include <net/tipc/tipc_msg.h> |
| 54 | #include <net/tipc/tipc_port.h> |
| 55 | |
| 56 | #include "core.h" |
| 57 | |
| 58 | #define SS_LISTENING -1 /* socket is listening */ |
| 59 | #define SS_READY -2 /* socket is connectionless */ |
| 60 | |
| 61 | #define OVERLOAD_LIMIT_BASE 5000 |
| 62 | |
| 63 | struct tipc_sock { |
| 64 | struct sock sk; |
| 65 | struct tipc_port *p; |
| 66 | struct semaphore sem; |
| 67 | }; |
| 68 | |
| 69 | #define tipc_sk(sk) ((struct tipc_sock*)sk) |
| 70 | |
| 71 | static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf); |
| 72 | static void wakeupdispatch(struct tipc_port *tport); |
| 73 | |
| 74 | static struct proto_ops packet_ops; |
| 75 | static struct proto_ops stream_ops; |
| 76 | static struct proto_ops msg_ops; |
| 77 | |
| 78 | static struct proto tipc_proto; |
| 79 | |
| 80 | static int sockets_enabled = 0; |
| 81 | |
| 82 | static atomic_t tipc_queue_size = ATOMIC_INIT(0); |
| 83 | |
| 84 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 85 | /* |
| 86 | * sock_lock(): Lock a port/socket pair. lock_sock() can |
| 87 | * not be used here, since the same lock must protect ports |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 88 | * with non-socket interfaces. |
| 89 | * See net.c for description of locking policy. |
| 90 | */ |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 91 | static void sock_lock(struct tipc_sock* tsock) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 92 | { |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 93 | spin_lock_bh(tsock->p->lock); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 94 | } |
| 95 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 96 | /* |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 97 | * sock_unlock(): Unlock a port/socket pair |
| 98 | */ |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 99 | static void sock_unlock(struct tipc_sock* tsock) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 100 | { |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 101 | spin_unlock_bh(tsock->p->lock); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
| 105 | * pollmask - determine the current set of poll() events for a socket |
| 106 | * @sock: socket structure |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 107 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 108 | * TIPC sets the returned events as follows: |
| 109 | * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty |
| 110 | * or if a connection-oriented socket is does not have an active connection |
| 111 | * (i.e. a read operation will not block). |
| 112 | * b) POLLOUT is set except when a socket's connection has been terminated |
| 113 | * (i.e. a write operation will not block). |
| 114 | * c) POLLHUP is set when a socket's connection has been terminated. |
| 115 | * |
| 116 | * IMPORTANT: The fact that a read or write operation will not block does NOT |
| 117 | * imply that the operation will succeed! |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 118 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 119 | * Returns pollmask value |
| 120 | */ |
| 121 | |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 122 | static u32 pollmask(struct socket *sock) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 123 | { |
| 124 | u32 mask; |
| 125 | |
| 126 | if ((skb_queue_len(&sock->sk->sk_receive_queue) != 0) || |
| 127 | (sock->state == SS_UNCONNECTED) || |
| 128 | (sock->state == SS_DISCONNECTING)) |
| 129 | mask = (POLLRDNORM | POLLIN); |
| 130 | else |
| 131 | mask = 0; |
| 132 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 133 | if (sock->state == SS_DISCONNECTING) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 134 | mask |= POLLHUP; |
| 135 | else |
| 136 | mask |= POLLOUT; |
| 137 | |
| 138 | return mask; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /** |
| 143 | * advance_queue - discard first buffer in queue |
| 144 | * @tsock: TIPC socket |
| 145 | */ |
| 146 | |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 147 | static void advance_queue(struct tipc_sock *tsock) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 148 | { |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 149 | sock_lock(tsock); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 150 | buf_discard(skb_dequeue(&tsock->sk.sk_receive_queue)); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 151 | sock_unlock(tsock); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 152 | atomic_dec(&tipc_queue_size); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * tipc_create - create a TIPC socket |
| 157 | * @sock: pre-allocated socket structure |
| 158 | * @protocol: protocol indicator (must be 0) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 159 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 160 | * This routine creates and attaches a 'struct sock' to the 'struct socket', |
| 161 | * then create and attaches a TIPC port to the 'struct sock' part. |
| 162 | * |
| 163 | * Returns 0 on success, errno otherwise |
| 164 | */ |
| 165 | static int tipc_create(struct socket *sock, int protocol) |
| 166 | { |
| 167 | struct tipc_sock *tsock; |
| 168 | struct tipc_port *port; |
| 169 | struct sock *sk; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 170 | u32 ref; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 171 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 172 | if (unlikely(protocol != 0)) |
| 173 | return -EPROTONOSUPPORT; |
| 174 | |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 175 | ref = tipc_createport_raw(NULL, &dispatch, &wakeupdispatch, TIPC_LOW_IMPORTANCE); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 176 | if (unlikely(!ref)) |
| 177 | return -ENOMEM; |
| 178 | |
| 179 | sock->state = SS_UNCONNECTED; |
| 180 | |
| 181 | switch (sock->type) { |
| 182 | case SOCK_STREAM: |
| 183 | sock->ops = &stream_ops; |
| 184 | break; |
| 185 | case SOCK_SEQPACKET: |
| 186 | sock->ops = &packet_ops; |
| 187 | break; |
| 188 | case SOCK_DGRAM: |
| 189 | tipc_set_portunreliable(ref, 1); |
| 190 | /* fall through */ |
| 191 | case SOCK_RDM: |
| 192 | tipc_set_portunreturnable(ref, 1); |
| 193 | sock->ops = &msg_ops; |
| 194 | sock->state = SS_READY; |
| 195 | break; |
Allan Stephens | 4997865 | 2006-06-25 23:47:18 -0700 | [diff] [blame] | 196 | default: |
| 197 | tipc_deleteport(ref); |
| 198 | return -EPROTOTYPE; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | sk = sk_alloc(AF_TIPC, GFP_KERNEL, &tipc_proto, 1); |
| 202 | if (!sk) { |
| 203 | tipc_deleteport(ref); |
| 204 | return -ENOMEM; |
| 205 | } |
| 206 | |
| 207 | sock_init_data(sock, sk); |
| 208 | init_waitqueue_head(sk->sk_sleep); |
| 209 | sk->sk_rcvtimeo = 8 * HZ; /* default connect timeout = 8s */ |
| 210 | |
| 211 | tsock = tipc_sk(sk); |
| 212 | port = tipc_get_port(ref); |
| 213 | |
| 214 | tsock->p = port; |
| 215 | port->usr_handle = tsock; |
| 216 | |
| 217 | init_MUTEX(&tsock->sem); |
| 218 | |
| 219 | dbg("sock_create: %x\n",tsock); |
| 220 | |
| 221 | atomic_inc(&tipc_user_count); |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * release - destroy a TIPC socket |
| 228 | * @sock: socket to destroy |
| 229 | * |
| 230 | * This routine cleans up any messages that are still queued on the socket. |
| 231 | * For DGRAM and RDM socket types, all queued messages are rejected. |
| 232 | * For SEQPACKET and STREAM socket types, the first message is rejected |
| 233 | * and any others are discarded. (If the first message on a STREAM socket |
| 234 | * 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] | 235 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 236 | * NOTE: Rejected messages are not necessarily returned to the sender! They |
| 237 | * are returned or discarded according to the "destination droppable" setting |
| 238 | * specified for the message by the sender. |
| 239 | * |
| 240 | * Returns 0 on success, errno otherwise |
| 241 | */ |
| 242 | |
| 243 | static int release(struct socket *sock) |
| 244 | { |
| 245 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
| 246 | struct sock *sk = sock->sk; |
| 247 | int res = TIPC_OK; |
| 248 | struct sk_buff *buf; |
| 249 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 250 | dbg("sock_delete: %x\n",tsock); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 251 | if (!tsock) |
| 252 | return 0; |
| 253 | down_interruptible(&tsock->sem); |
| 254 | if (!sock->sk) { |
| 255 | up(&tsock->sem); |
| 256 | return 0; |
| 257 | } |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 258 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 259 | /* Reject unreceived messages, unless no longer connected */ |
| 260 | |
| 261 | while (sock->state != SS_DISCONNECTING) { |
| 262 | sock_lock(tsock); |
| 263 | buf = skb_dequeue(&sk->sk_receive_queue); |
| 264 | if (!buf) |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 265 | tsock->p->usr_handle = NULL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 266 | sock_unlock(tsock); |
| 267 | if (!buf) |
| 268 | break; |
| 269 | if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) |
| 270 | buf_discard(buf); |
| 271 | else |
| 272 | tipc_reject_msg(buf, TIPC_ERR_NO_PORT); |
| 273 | atomic_dec(&tipc_queue_size); |
| 274 | } |
| 275 | |
| 276 | /* Delete TIPC port */ |
| 277 | |
| 278 | res = tipc_deleteport(tsock->p->ref); |
| 279 | sock->sk = NULL; |
| 280 | |
| 281 | /* Discard any remaining messages */ |
| 282 | |
| 283 | while ((buf = skb_dequeue(&sk->sk_receive_queue))) { |
| 284 | buf_discard(buf); |
| 285 | atomic_dec(&tipc_queue_size); |
| 286 | } |
| 287 | |
| 288 | up(&tsock->sem); |
| 289 | |
| 290 | sock_put(sk); |
| 291 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 292 | atomic_dec(&tipc_user_count); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 293 | return res; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * bind - associate or disassocate TIPC name(s) with a socket |
| 298 | * @sock: socket structure |
| 299 | * @uaddr: socket address describing name(s) and desired operation |
| 300 | * @uaddr_len: size of socket address data structure |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 301 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 302 | * Name and name sequence binding is indicated using a positive scope value; |
| 303 | * a negative scope value unbinds the specified name. Specifying no name |
| 304 | * (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] | 305 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 306 | * Returns 0 on success, errno otherwise |
| 307 | */ |
| 308 | |
| 309 | static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len) |
| 310 | { |
| 311 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
| 312 | struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; |
| 313 | int res; |
| 314 | |
| 315 | if (down_interruptible(&tsock->sem)) |
| 316 | return -ERESTARTSYS; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 317 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 318 | if (unlikely(!uaddr_len)) { |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 319 | res = tipc_withdraw(tsock->p->ref, 0, NULL); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 320 | goto exit; |
| 321 | } |
| 322 | |
| 323 | if (uaddr_len < sizeof(struct sockaddr_tipc)) { |
| 324 | res = -EINVAL; |
| 325 | goto exit; |
| 326 | } |
| 327 | |
| 328 | if (addr->family != AF_TIPC) { |
| 329 | res = -EAFNOSUPPORT; |
| 330 | goto exit; |
| 331 | } |
| 332 | if (addr->addrtype == TIPC_ADDR_NAME) |
| 333 | addr->addr.nameseq.upper = addr->addr.nameseq.lower; |
| 334 | else if (addr->addrtype != TIPC_ADDR_NAMESEQ) { |
| 335 | res = -EAFNOSUPPORT; |
| 336 | goto exit; |
| 337 | } |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 338 | |
| 339 | if (addr->scope > 0) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 340 | res = tipc_publish(tsock->p->ref, addr->scope, |
| 341 | &addr->addr.nameseq); |
| 342 | else |
| 343 | res = tipc_withdraw(tsock->p->ref, -addr->scope, |
| 344 | &addr->addr.nameseq); |
| 345 | exit: |
| 346 | up(&tsock->sem); |
| 347 | return res; |
| 348 | } |
| 349 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 350 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 351 | * get_name - get port ID of socket or peer socket |
| 352 | * @sock: socket structure |
| 353 | * @uaddr: area for returned socket address |
| 354 | * @uaddr_len: area for returned length of socket address |
| 355 | * @peer: 0 to obtain socket name, 1 to obtain peer socket name |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 356 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 357 | * Returns 0 on success, errno otherwise |
| 358 | */ |
| 359 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 360 | static int get_name(struct socket *sock, struct sockaddr *uaddr, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 361 | int *uaddr_len, int peer) |
| 362 | { |
| 363 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
| 364 | struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; |
| 365 | u32 res; |
| 366 | |
| 367 | if (down_interruptible(&tsock->sem)) |
| 368 | return -ERESTARTSYS; |
| 369 | |
| 370 | *uaddr_len = sizeof(*addr); |
| 371 | addr->addrtype = TIPC_ADDR_ID; |
| 372 | addr->family = AF_TIPC; |
| 373 | addr->scope = 0; |
| 374 | if (peer) |
| 375 | res = tipc_peer(tsock->p->ref, &addr->addr.id); |
| 376 | else |
| 377 | res = tipc_ownidentity(tsock->p->ref, &addr->addr.id); |
| 378 | addr->addr.name.domain = 0; |
| 379 | |
| 380 | up(&tsock->sem); |
| 381 | return res; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * poll - read and possibly block on pollmask |
| 386 | * @file: file structure associated with the socket |
| 387 | * @sock: socket for which to calculate the poll bits |
| 388 | * @wait: ??? |
| 389 | * |
| 390 | * Returns the pollmask |
| 391 | */ |
| 392 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 393 | static unsigned int poll(struct file *file, struct socket *sock, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 394 | poll_table *wait) |
| 395 | { |
| 396 | poll_wait(file, sock->sk->sk_sleep, wait); |
| 397 | /* NEED LOCK HERE? */ |
| 398 | return pollmask(sock); |
| 399 | } |
| 400 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 401 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 402 | * dest_name_check - verify user is permitted to send to specified port name |
| 403 | * @dest: destination address |
| 404 | * @m: descriptor for message to be sent |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 405 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 406 | * Prevents restricted configuration commands from being issued by |
| 407 | * unauthorized users. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 408 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 409 | * Returns 0 if permission is granted, otherwise errno |
| 410 | */ |
| 411 | |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 412 | static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 413 | { |
| 414 | struct tipc_cfg_msg_hdr hdr; |
| 415 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 416 | if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES)) |
| 417 | return 0; |
| 418 | if (likely(dest->addr.name.name.type == TIPC_TOP_SRV)) |
| 419 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 420 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 421 | if (likely(dest->addr.name.name.type != TIPC_CFG_SRV)) |
| 422 | return -EACCES; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 423 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 424 | 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] | 425 | return -EFAULT; |
Allan Stephens | 70cb234 | 2006-06-25 23:41:47 -0700 | [diff] [blame] | 426 | if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN))) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 427 | return -EACCES; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 428 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * send_msg - send message in connectionless manner |
| 434 | * @iocb: (unused) |
| 435 | * @sock: socket structure |
| 436 | * @m: message to send |
Allan Stephens | e9024f0f | 2006-06-25 23:43:57 -0700 | [diff] [blame] | 437 | * @total_len: length of message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 438 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 439 | * Message must have an destination specified explicitly. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 440 | * Used for SOCK_RDM and SOCK_DGRAM messages, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 441 | * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections. |
| 442 | * (Note: 'SYN+' is prohibited on SOCK_STREAM.) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 443 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 444 | * Returns the number of bytes sent on success, or errno otherwise |
| 445 | */ |
| 446 | |
| 447 | static int send_msg(struct kiocb *iocb, struct socket *sock, |
| 448 | struct msghdr *m, size_t total_len) |
| 449 | { |
| 450 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 451 | struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 452 | struct sk_buff *buf; |
| 453 | int needs_conn; |
| 454 | int res = -EINVAL; |
| 455 | |
| 456 | if (unlikely(!dest)) |
| 457 | return -EDESTADDRREQ; |
Allan Stephens | 51f9cc1 | 2006-06-25 23:49:06 -0700 | [diff] [blame] | 458 | if (unlikely((m->msg_namelen < sizeof(*dest)) || |
| 459 | (dest->family != AF_TIPC))) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 460 | return -EINVAL; |
| 461 | |
| 462 | needs_conn = (sock->state != SS_READY); |
| 463 | if (unlikely(needs_conn)) { |
| 464 | if (sock->state == SS_LISTENING) |
| 465 | return -EPIPE; |
| 466 | if (sock->state != SS_UNCONNECTED) |
| 467 | return -EISCONN; |
| 468 | if ((tsock->p->published) || |
| 469 | ((sock->type == SOCK_STREAM) && (total_len != 0))) |
| 470 | return -EOPNOTSUPP; |
Allan Stephens | 3388007 | 2006-06-25 23:44:57 -0700 | [diff] [blame] | 471 | if (dest->addrtype == TIPC_ADDR_NAME) { |
| 472 | tsock->p->conn_type = dest->addr.name.name.type; |
| 473 | tsock->p->conn_instance = dest->addr.name.name.instance; |
| 474 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | if (down_interruptible(&tsock->sem)) |
| 478 | return -ERESTARTSYS; |
| 479 | |
| 480 | if (needs_conn) { |
| 481 | |
| 482 | /* Abort any pending connection attempts (very unlikely) */ |
| 483 | |
| 484 | while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) { |
| 485 | tipc_reject_msg(buf, TIPC_ERR_NO_PORT); |
| 486 | atomic_dec(&tipc_queue_size); |
| 487 | } |
| 488 | |
| 489 | sock->state = SS_CONNECTING; |
| 490 | } |
| 491 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 492 | do { |
| 493 | if (dest->addrtype == TIPC_ADDR_NAME) { |
| 494 | if ((res = dest_name_check(dest, m))) |
| 495 | goto exit; |
| 496 | res = tipc_send2name(tsock->p->ref, |
| 497 | &dest->addr.name.name, |
| 498 | dest->addr.name.domain, |
| 499 | m->msg_iovlen, |
| 500 | m->msg_iov); |
| 501 | } |
| 502 | else if (dest->addrtype == TIPC_ADDR_ID) { |
| 503 | res = tipc_send2port(tsock->p->ref, |
| 504 | &dest->addr.id, |
| 505 | m->msg_iovlen, |
| 506 | m->msg_iov); |
| 507 | } |
| 508 | else if (dest->addrtype == TIPC_ADDR_MCAST) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 509 | if (needs_conn) { |
| 510 | res = -EOPNOTSUPP; |
| 511 | goto exit; |
| 512 | } |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 513 | if ((res = dest_name_check(dest, m))) |
| 514 | goto exit; |
| 515 | res = tipc_multicast(tsock->p->ref, |
| 516 | &dest->addr.nameseq, |
| 517 | 0, |
| 518 | m->msg_iovlen, |
| 519 | m->msg_iov); |
| 520 | } |
| 521 | if (likely(res != -ELINKCONG)) { |
| 522 | exit: |
| 523 | up(&tsock->sem); |
| 524 | return res; |
| 525 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 526 | if (m->msg_flags & MSG_DONTWAIT) { |
| 527 | res = -EWOULDBLOCK; |
| 528 | goto exit; |
| 529 | } |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 530 | if (wait_event_interruptible(*sock->sk->sk_sleep, |
| 531 | !tsock->p->congested)) { |
| 532 | res = -ERESTARTSYS; |
| 533 | goto exit; |
| 534 | } |
| 535 | } while (1); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 536 | } |
| 537 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 538 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 539 | * send_packet - send a connection-oriented message |
| 540 | * @iocb: (unused) |
| 541 | * @sock: socket structure |
| 542 | * @m: message to send |
Allan Stephens | e9024f0f | 2006-06-25 23:43:57 -0700 | [diff] [blame] | 543 | * @total_len: length of message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 544 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 545 | * Used for SOCK_SEQPACKET messages and SOCK_STREAM data. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 546 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 547 | * Returns the number of bytes sent on success, or errno otherwise |
| 548 | */ |
| 549 | |
| 550 | static int send_packet(struct kiocb *iocb, struct socket *sock, |
| 551 | struct msghdr *m, size_t total_len) |
| 552 | { |
| 553 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 554 | struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 555 | int res; |
| 556 | |
| 557 | /* Handle implied connection establishment */ |
| 558 | |
| 559 | if (unlikely(dest)) |
| 560 | return send_msg(iocb, sock, m, total_len); |
| 561 | |
| 562 | if (down_interruptible(&tsock->sem)) { |
| 563 | return -ERESTARTSYS; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 564 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 565 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 566 | do { |
Allan Stephens | bdd9478 | 2006-06-25 23:45:53 -0700 | [diff] [blame] | 567 | if (unlikely(sock->state != SS_CONNECTED)) { |
| 568 | if (sock->state == SS_DISCONNECTING) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 569 | res = -EPIPE; |
Allan Stephens | bdd9478 | 2006-06-25 23:45:53 -0700 | [diff] [blame] | 570 | else |
| 571 | res = -ENOTCONN; |
| 572 | goto exit; |
| 573 | } |
| 574 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 575 | res = tipc_send(tsock->p->ref, m->msg_iovlen, m->msg_iov); |
| 576 | if (likely(res != -ELINKCONG)) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 577 | exit: |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 578 | up(&tsock->sem); |
| 579 | return res; |
| 580 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 581 | if (m->msg_flags & MSG_DONTWAIT) { |
| 582 | res = -EWOULDBLOCK; |
| 583 | goto exit; |
| 584 | } |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 585 | if (wait_event_interruptible(*sock->sk->sk_sleep, |
| 586 | !tsock->p->congested)) { |
| 587 | res = -ERESTARTSYS; |
| 588 | goto exit; |
| 589 | } |
| 590 | } while (1); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 591 | } |
| 592 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 593 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 594 | * send_stream - send stream-oriented data |
| 595 | * @iocb: (unused) |
| 596 | * @sock: socket structure |
| 597 | * @m: data to send |
| 598 | * @total_len: total length of data to be sent |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 599 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 600 | * Used for SOCK_STREAM data. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 601 | * |
| 602 | * Returns the number of bytes sent on success (or partial success), |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 603 | * or errno if no data sent |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 604 | */ |
| 605 | |
| 606 | |
| 607 | static int send_stream(struct kiocb *iocb, struct socket *sock, |
| 608 | struct msghdr *m, size_t total_len) |
| 609 | { |
Allan Stephens | 05646c9 | 2007-06-10 17:25:24 -0700 | [diff] [blame] | 610 | struct tipc_port *tport; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 611 | struct msghdr my_msg; |
| 612 | struct iovec my_iov; |
| 613 | struct iovec *curr_iov; |
| 614 | int curr_iovlen; |
| 615 | char __user *curr_start; |
Allan Stephens | 05646c9 | 2007-06-10 17:25:24 -0700 | [diff] [blame] | 616 | u32 hdr_size; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 617 | int curr_left; |
| 618 | int bytes_to_send; |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 619 | int bytes_sent; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 620 | int res; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 621 | |
Allan Stephens | 05646c9 | 2007-06-10 17:25:24 -0700 | [diff] [blame] | 622 | /* Handle special cases where there is no connection */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 623 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 624 | if (unlikely(sock->state != SS_CONNECTED)) { |
Allan Stephens | 05646c9 | 2007-06-10 17:25:24 -0700 | [diff] [blame] | 625 | if (sock->state == SS_UNCONNECTED) |
| 626 | return send_packet(iocb, sock, m, total_len); |
| 627 | else if (sock->state == SS_DISCONNECTING) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 628 | return -EPIPE; |
| 629 | else |
| 630 | return -ENOTCONN; |
| 631 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 632 | |
Allan Stephens | eb5959c | 2006-10-16 21:43:54 -0700 | [diff] [blame] | 633 | if (unlikely(m->msg_name)) |
| 634 | return -EISCONN; |
| 635 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 636 | /* |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 637 | * Send each iovec entry using one or more messages |
| 638 | * |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 639 | * Note: This algorithm is good for the most likely case |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 640 | * (i.e. one large iovec entry), but could be improved to pass sets |
| 641 | * of small iovec entries into send_packet(). |
| 642 | */ |
| 643 | |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 644 | curr_iov = m->msg_iov; |
| 645 | curr_iovlen = m->msg_iovlen; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 646 | my_msg.msg_iov = &my_iov; |
| 647 | my_msg.msg_iovlen = 1; |
Allan Stephens | eb5959c | 2006-10-16 21:43:54 -0700 | [diff] [blame] | 648 | my_msg.msg_flags = m->msg_flags; |
| 649 | my_msg.msg_name = NULL; |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 650 | bytes_sent = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 651 | |
Allan Stephens | 05646c9 | 2007-06-10 17:25:24 -0700 | [diff] [blame] | 652 | tport = tipc_sk(sock->sk)->p; |
| 653 | hdr_size = msg_hdr_sz(&tport->phdr); |
| 654 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 655 | while (curr_iovlen--) { |
| 656 | curr_start = curr_iov->iov_base; |
| 657 | curr_left = curr_iov->iov_len; |
| 658 | |
| 659 | while (curr_left) { |
Allan Stephens | 05646c9 | 2007-06-10 17:25:24 -0700 | [diff] [blame] | 660 | bytes_to_send = tport->max_pkt - hdr_size; |
| 661 | if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE) |
| 662 | bytes_to_send = TIPC_MAX_USER_MSG_SIZE; |
| 663 | if (curr_left < bytes_to_send) |
| 664 | bytes_to_send = curr_left; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 665 | my_iov.iov_base = curr_start; |
| 666 | my_iov.iov_len = bytes_to_send; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 667 | if ((res = send_packet(iocb, sock, &my_msg, 0)) < 0) { |
Allan Stephens | 05646c9 | 2007-06-10 17:25:24 -0700 | [diff] [blame] | 668 | if (bytes_sent != 0) |
| 669 | res = bytes_sent; |
| 670 | return res; |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 671 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 672 | curr_left -= bytes_to_send; |
| 673 | curr_start += bytes_to_send; |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 674 | bytes_sent += bytes_to_send; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | curr_iov++; |
| 678 | } |
| 679 | |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 680 | return bytes_sent; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /** |
| 684 | * auto_connect - complete connection setup to a remote port |
| 685 | * @sock: socket structure |
| 686 | * @tsock: TIPC-specific socket structure |
| 687 | * @msg: peer's response message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 688 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 689 | * Returns 0 on success, errno otherwise |
| 690 | */ |
| 691 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 692 | static int auto_connect(struct socket *sock, struct tipc_sock *tsock, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 693 | struct tipc_msg *msg) |
| 694 | { |
| 695 | struct tipc_portid peer; |
| 696 | |
| 697 | if (msg_errcode(msg)) { |
| 698 | sock->state = SS_DISCONNECTING; |
| 699 | return -ECONNREFUSED; |
| 700 | } |
| 701 | |
| 702 | peer.ref = msg_origport(msg); |
| 703 | peer.node = msg_orignode(msg); |
| 704 | tipc_connect2port(tsock->p->ref, &peer); |
| 705 | tipc_set_portimportance(tsock->p->ref, msg_importance(msg)); |
| 706 | sock->state = SS_CONNECTED; |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * set_orig_addr - capture sender's address for received message |
| 712 | * @m: descriptor for message info |
| 713 | * @msg: received message header |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 714 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 715 | * Note: Address is not captured if not requested by receiver. |
| 716 | */ |
| 717 | |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 718 | static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 719 | { |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 720 | struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 721 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 722 | if (addr) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 723 | addr->family = AF_TIPC; |
| 724 | addr->addrtype = TIPC_ADDR_ID; |
| 725 | addr->addr.id.ref = msg_origport(msg); |
| 726 | addr->addr.id.node = msg_orignode(msg); |
| 727 | addr->addr.name.domain = 0; /* could leave uninitialized */ |
| 728 | addr->scope = 0; /* could leave uninitialized */ |
| 729 | m->msg_namelen = sizeof(struct sockaddr_tipc); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /** |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 734 | * anc_data_recv - optionally capture ancillary data for received message |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 735 | * @m: descriptor for message info |
| 736 | * @msg: received message header |
| 737 | * @tport: TIPC port associated with message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 738 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 739 | * Note: Ancillary data is not captured if not requested by receiver. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 740 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 741 | * Returns 0 if successful, otherwise errno |
| 742 | */ |
| 743 | |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 744 | static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 745 | struct tipc_port *tport) |
| 746 | { |
| 747 | u32 anc_data[3]; |
| 748 | u32 err; |
| 749 | u32 dest_type; |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 750 | int has_name; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 751 | int res; |
| 752 | |
| 753 | if (likely(m->msg_controllen == 0)) |
| 754 | return 0; |
| 755 | |
| 756 | /* Optionally capture errored message object(s) */ |
| 757 | |
| 758 | err = msg ? msg_errcode(msg) : 0; |
| 759 | if (unlikely(err)) { |
| 760 | anc_data[0] = err; |
| 761 | anc_data[1] = msg_data_sz(msg); |
Allan Stephens | 4b087b2 | 2006-06-25 23:47:44 -0700 | [diff] [blame] | 762 | if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data))) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 763 | return res; |
| 764 | if (anc_data[1] && |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 765 | (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1], |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 766 | msg_data(msg)))) |
| 767 | return res; |
| 768 | } |
| 769 | |
| 770 | /* Optionally capture message destination object */ |
| 771 | |
| 772 | dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG; |
| 773 | switch (dest_type) { |
| 774 | case TIPC_NAMED_MSG: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 775 | has_name = 1; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 776 | anc_data[0] = msg_nametype(msg); |
| 777 | anc_data[1] = msg_namelower(msg); |
| 778 | anc_data[2] = msg_namelower(msg); |
| 779 | break; |
| 780 | case TIPC_MCAST_MSG: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 781 | has_name = 1; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 782 | anc_data[0] = msg_nametype(msg); |
| 783 | anc_data[1] = msg_namelower(msg); |
| 784 | anc_data[2] = msg_nameupper(msg); |
| 785 | break; |
| 786 | case TIPC_CONN_MSG: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 787 | has_name = (tport->conn_type != 0); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 788 | anc_data[0] = tport->conn_type; |
| 789 | anc_data[1] = tport->conn_instance; |
| 790 | anc_data[2] = tport->conn_instance; |
| 791 | break; |
| 792 | default: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 793 | has_name = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 794 | } |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 795 | if (has_name && |
Allan Stephens | 4b087b2 | 2006-06-25 23:47:44 -0700 | [diff] [blame] | 796 | (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data))) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 797 | return res; |
| 798 | |
| 799 | return 0; |
| 800 | } |
| 801 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 802 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 803 | * recv_msg - receive packet-oriented message |
| 804 | * @iocb: (unused) |
| 805 | * @m: descriptor for message info |
| 806 | * @buf_len: total size of user buffer area |
| 807 | * @flags: receive flags |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 808 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 809 | * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages. |
| 810 | * If the complete message doesn't fit in user area, truncate it. |
| 811 | * |
| 812 | * Returns size of returned message data, errno otherwise |
| 813 | */ |
| 814 | |
| 815 | static int recv_msg(struct kiocb *iocb, struct socket *sock, |
| 816 | struct msghdr *m, size_t buf_len, int flags) |
| 817 | { |
| 818 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
| 819 | struct sk_buff *buf; |
| 820 | struct tipc_msg *msg; |
| 821 | unsigned int q_len; |
| 822 | unsigned int sz; |
| 823 | u32 err; |
| 824 | int res; |
| 825 | |
| 826 | /* Currently doesn't support receiving into multiple iovec entries */ |
| 827 | |
| 828 | if (m->msg_iovlen != 1) |
| 829 | return -EOPNOTSUPP; |
| 830 | |
| 831 | /* Catch invalid receive attempts */ |
| 832 | |
| 833 | if (unlikely(!buf_len)) |
| 834 | return -EINVAL; |
| 835 | |
| 836 | if (sock->type == SOCK_SEQPACKET) { |
| 837 | if (unlikely(sock->state == SS_UNCONNECTED)) |
| 838 | return -ENOTCONN; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 839 | if (unlikely((sock->state == SS_DISCONNECTING) && |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 840 | (skb_queue_len(&sock->sk->sk_receive_queue) == 0))) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 841 | return -ENOTCONN; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | /* Look for a message in receive queue; wait if necessary */ |
| 845 | |
| 846 | if (unlikely(down_interruptible(&tsock->sem))) |
| 847 | return -ERESTARTSYS; |
| 848 | |
| 849 | restart: |
| 850 | if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) && |
| 851 | (flags & MSG_DONTWAIT))) { |
| 852 | res = -EWOULDBLOCK; |
| 853 | goto exit; |
| 854 | } |
| 855 | |
| 856 | if ((res = wait_event_interruptible( |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 857 | *sock->sk->sk_sleep, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 858 | ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) || |
| 859 | (sock->state == SS_DISCONNECTING))) )) { |
| 860 | goto exit; |
| 861 | } |
| 862 | |
| 863 | /* Catch attempt to receive on an already terminated connection */ |
| 864 | /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */ |
| 865 | |
| 866 | if (!q_len) { |
| 867 | res = -ENOTCONN; |
| 868 | goto exit; |
| 869 | } |
| 870 | |
| 871 | /* Get access to first message in receive queue */ |
| 872 | |
| 873 | buf = skb_peek(&sock->sk->sk_receive_queue); |
| 874 | msg = buf_msg(buf); |
| 875 | sz = msg_data_sz(msg); |
| 876 | err = msg_errcode(msg); |
| 877 | |
| 878 | /* Complete connection setup for an implied connect */ |
| 879 | |
| 880 | if (unlikely(sock->state == SS_CONNECTING)) { |
| 881 | if ((res = auto_connect(sock, tsock, msg))) |
| 882 | goto exit; |
| 883 | } |
| 884 | |
| 885 | /* Discard an empty non-errored message & try again */ |
| 886 | |
| 887 | if ((!sz) && (!err)) { |
| 888 | advance_queue(tsock); |
| 889 | goto restart; |
| 890 | } |
| 891 | |
| 892 | /* Capture sender's address (optional) */ |
| 893 | |
| 894 | set_orig_addr(m, msg); |
| 895 | |
| 896 | /* Capture ancillary data (optional) */ |
| 897 | |
| 898 | if ((res = anc_data_recv(m, msg, tsock->p))) |
| 899 | goto exit; |
| 900 | |
| 901 | /* Capture message data (if valid) & compute return value (always) */ |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 902 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 903 | if (!err) { |
| 904 | if (unlikely(buf_len < sz)) { |
| 905 | sz = buf_len; |
| 906 | m->msg_flags |= MSG_TRUNC; |
| 907 | } |
| 908 | if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg), |
| 909 | sz))) { |
| 910 | res = -EFAULT; |
| 911 | goto exit; |
| 912 | } |
| 913 | res = sz; |
| 914 | } else { |
| 915 | if ((sock->state == SS_READY) || |
| 916 | ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)) |
| 917 | res = 0; |
| 918 | else |
| 919 | res = -ECONNRESET; |
| 920 | } |
| 921 | |
| 922 | /* Consume received message (optional) */ |
| 923 | |
| 924 | if (likely(!(flags & MSG_PEEK))) { |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 925 | if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN)) |
| 926 | tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 927 | advance_queue(tsock); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 928 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 929 | exit: |
| 930 | up(&tsock->sem); |
| 931 | return res; |
| 932 | } |
| 933 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 934 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 935 | * recv_stream - receive stream-oriented data |
| 936 | * @iocb: (unused) |
| 937 | * @m: descriptor for message info |
| 938 | * @buf_len: total size of user buffer area |
| 939 | * @flags: receive flags |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 940 | * |
| 941 | * Used for SOCK_STREAM messages only. If not enough data is available |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 942 | * will optionally wait for more; never truncates data. |
| 943 | * |
| 944 | * Returns size of returned message data, errno otherwise |
| 945 | */ |
| 946 | |
| 947 | static int recv_stream(struct kiocb *iocb, struct socket *sock, |
| 948 | struct msghdr *m, size_t buf_len, int flags) |
| 949 | { |
| 950 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
| 951 | struct sk_buff *buf; |
| 952 | struct tipc_msg *msg; |
| 953 | unsigned int q_len; |
| 954 | unsigned int sz; |
| 955 | int sz_to_copy; |
| 956 | int sz_copied = 0; |
| 957 | int needed; |
Al Viro | 28c4dad | 2006-10-10 22:45:57 +0100 | [diff] [blame] | 958 | char __user *crs = m->msg_iov->iov_base; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 959 | unsigned char *buf_crs; |
| 960 | u32 err; |
| 961 | int res; |
| 962 | |
| 963 | /* Currently doesn't support receiving into multiple iovec entries */ |
| 964 | |
| 965 | if (m->msg_iovlen != 1) |
| 966 | return -EOPNOTSUPP; |
| 967 | |
| 968 | /* Catch invalid receive attempts */ |
| 969 | |
| 970 | if (unlikely(!buf_len)) |
| 971 | return -EINVAL; |
| 972 | |
| 973 | if (unlikely(sock->state == SS_DISCONNECTING)) { |
| 974 | if (skb_queue_len(&sock->sk->sk_receive_queue) == 0) |
| 975 | return -ENOTCONN; |
| 976 | } else if (unlikely(sock->state != SS_CONNECTED)) |
| 977 | return -ENOTCONN; |
| 978 | |
| 979 | /* Look for a message in receive queue; wait if necessary */ |
| 980 | |
| 981 | if (unlikely(down_interruptible(&tsock->sem))) |
| 982 | return -ERESTARTSYS; |
| 983 | |
| 984 | restart: |
| 985 | if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) && |
| 986 | (flags & MSG_DONTWAIT))) { |
Allan Stephens | a3b0a5a | 2006-06-25 23:48:22 -0700 | [diff] [blame] | 987 | res = -EWOULDBLOCK; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 988 | goto exit; |
| 989 | } |
| 990 | |
| 991 | if ((res = wait_event_interruptible( |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 992 | *sock->sk->sk_sleep, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 993 | ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) || |
| 994 | (sock->state == SS_DISCONNECTING))) )) { |
| 995 | goto exit; |
| 996 | } |
| 997 | |
| 998 | /* Catch attempt to receive on an already terminated connection */ |
| 999 | /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */ |
| 1000 | |
| 1001 | if (!q_len) { |
| 1002 | res = -ENOTCONN; |
| 1003 | goto exit; |
| 1004 | } |
| 1005 | |
| 1006 | /* Get access to first message in receive queue */ |
| 1007 | |
| 1008 | buf = skb_peek(&sock->sk->sk_receive_queue); |
| 1009 | msg = buf_msg(buf); |
| 1010 | sz = msg_data_sz(msg); |
| 1011 | err = msg_errcode(msg); |
| 1012 | |
| 1013 | /* Discard an empty non-errored message & try again */ |
| 1014 | |
| 1015 | if ((!sz) && (!err)) { |
| 1016 | advance_queue(tsock); |
| 1017 | goto restart; |
| 1018 | } |
| 1019 | |
| 1020 | /* Optionally capture sender's address & ancillary data of first msg */ |
| 1021 | |
| 1022 | if (sz_copied == 0) { |
| 1023 | set_orig_addr(m, msg); |
| 1024 | if ((res = anc_data_recv(m, msg, tsock->p))) |
| 1025 | goto exit; |
| 1026 | } |
| 1027 | |
| 1028 | /* Capture message data (if valid) & compute return value (always) */ |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1029 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1030 | if (!err) { |
| 1031 | buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle); |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1032 | sz = skb_tail_pointer(buf) - buf_crs; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1033 | |
| 1034 | needed = (buf_len - sz_copied); |
| 1035 | sz_to_copy = (sz <= needed) ? sz : needed; |
| 1036 | if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) { |
| 1037 | res = -EFAULT; |
| 1038 | goto exit; |
| 1039 | } |
| 1040 | sz_copied += sz_to_copy; |
| 1041 | |
| 1042 | if (sz_to_copy < sz) { |
| 1043 | if (!(flags & MSG_PEEK)) |
| 1044 | TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy; |
| 1045 | goto exit; |
| 1046 | } |
| 1047 | |
| 1048 | crs += sz_to_copy; |
| 1049 | } else { |
| 1050 | if (sz_copied != 0) |
| 1051 | goto exit; /* can't add error msg to valid data */ |
| 1052 | |
| 1053 | if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control) |
| 1054 | res = 0; |
| 1055 | else |
| 1056 | res = -ECONNRESET; |
| 1057 | } |
| 1058 | |
| 1059 | /* Consume received message (optional) */ |
| 1060 | |
| 1061 | if (likely(!(flags & MSG_PEEK))) { |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1062 | if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN)) |
| 1063 | tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1064 | advance_queue(tsock); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1065 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1066 | |
| 1067 | /* Loop around if more data is required */ |
| 1068 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1069 | if ((sz_copied < buf_len) /* didn't get all requested data */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1070 | && (flags & MSG_WAITALL) /* ... and need to wait for more */ |
| 1071 | && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */ |
| 1072 | && (!err) /* ... and haven't reached a FIN */ |
| 1073 | ) |
| 1074 | goto restart; |
| 1075 | |
| 1076 | exit: |
| 1077 | up(&tsock->sem); |
Allan Stephens | a3b0a5a | 2006-06-25 23:48:22 -0700 | [diff] [blame] | 1078 | return sz_copied ? sz_copied : res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | /** |
| 1082 | * queue_overloaded - test if queue overload condition exists |
| 1083 | * @queue_size: current size of queue |
| 1084 | * @base: nominal maximum size of queue |
| 1085 | * @msg: message to be added to queue |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1086 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1087 | * Returns 1 if queue is currently overloaded, 0 otherwise |
| 1088 | */ |
| 1089 | |
| 1090 | static int queue_overloaded(u32 queue_size, u32 base, struct tipc_msg *msg) |
| 1091 | { |
| 1092 | u32 threshold; |
| 1093 | u32 imp = msg_importance(msg); |
| 1094 | |
| 1095 | if (imp == TIPC_LOW_IMPORTANCE) |
| 1096 | threshold = base; |
| 1097 | else if (imp == TIPC_MEDIUM_IMPORTANCE) |
| 1098 | threshold = base * 2; |
| 1099 | else if (imp == TIPC_HIGH_IMPORTANCE) |
| 1100 | threshold = base * 100; |
| 1101 | else |
| 1102 | return 0; |
| 1103 | |
| 1104 | if (msg_connected(msg)) |
| 1105 | threshold *= 4; |
| 1106 | |
| 1107 | return (queue_size > threshold); |
| 1108 | } |
| 1109 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1110 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1111 | * async_disconnect - wrapper function used to disconnect port |
| 1112 | * @portref: TIPC port reference (passed as pointer-sized value) |
| 1113 | */ |
| 1114 | |
| 1115 | static void async_disconnect(unsigned long portref) |
| 1116 | { |
| 1117 | tipc_disconnect((u32)portref); |
| 1118 | } |
| 1119 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1120 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1121 | * dispatch - handle arriving message |
| 1122 | * @tport: TIPC port that received message |
| 1123 | * @buf: message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1124 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1125 | * Called with port locked. Must not take socket lock to avoid deadlock risk. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1126 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1127 | * Returns TIPC error status code (TIPC_OK if message is not to be rejected) |
| 1128 | */ |
| 1129 | |
| 1130 | static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf) |
| 1131 | { |
| 1132 | struct tipc_msg *msg = buf_msg(buf); |
| 1133 | struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle; |
| 1134 | struct socket *sock; |
| 1135 | u32 recv_q_len; |
| 1136 | |
| 1137 | /* Reject message if socket is closing */ |
| 1138 | |
| 1139 | if (!tsock) |
| 1140 | return TIPC_ERR_NO_PORT; |
| 1141 | |
| 1142 | /* Reject message if it is wrong sort of message for socket */ |
| 1143 | |
| 1144 | /* |
| 1145 | * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD? |
| 1146 | * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY |
| 1147 | * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC |
| 1148 | */ |
| 1149 | sock = tsock->sk.sk_socket; |
| 1150 | if (sock->state == SS_READY) { |
| 1151 | if (msg_connected(msg)) { |
| 1152 | msg_dbg(msg, "dispatch filter 1\n"); |
| 1153 | return TIPC_ERR_NO_PORT; |
| 1154 | } |
| 1155 | } else { |
| 1156 | if (msg_mcast(msg)) { |
| 1157 | msg_dbg(msg, "dispatch filter 2\n"); |
| 1158 | return TIPC_ERR_NO_PORT; |
| 1159 | } |
| 1160 | if (sock->state == SS_CONNECTED) { |
| 1161 | if (!msg_connected(msg)) { |
| 1162 | msg_dbg(msg, "dispatch filter 3\n"); |
| 1163 | return TIPC_ERR_NO_PORT; |
| 1164 | } |
| 1165 | } |
| 1166 | else if (sock->state == SS_CONNECTING) { |
| 1167 | if (!msg_connected(msg) && (msg_errcode(msg) == 0)) { |
| 1168 | msg_dbg(msg, "dispatch filter 4\n"); |
| 1169 | return TIPC_ERR_NO_PORT; |
| 1170 | } |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1171 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1172 | else if (sock->state == SS_LISTENING) { |
| 1173 | if (msg_connected(msg) || msg_errcode(msg)) { |
| 1174 | msg_dbg(msg, "dispatch filter 5\n"); |
| 1175 | return TIPC_ERR_NO_PORT; |
| 1176 | } |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1177 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1178 | else if (sock->state == SS_DISCONNECTING) { |
| 1179 | msg_dbg(msg, "dispatch filter 6\n"); |
| 1180 | return TIPC_ERR_NO_PORT; |
| 1181 | } |
| 1182 | else /* (sock->state == SS_UNCONNECTED) */ { |
| 1183 | if (msg_connected(msg) || msg_errcode(msg)) { |
| 1184 | msg_dbg(msg, "dispatch filter 7\n"); |
| 1185 | return TIPC_ERR_NO_PORT; |
| 1186 | } |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | /* Reject message if there isn't room to queue it */ |
| 1191 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1192 | if (unlikely((u32)atomic_read(&tipc_queue_size) > |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1193 | OVERLOAD_LIMIT_BASE)) { |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1194 | if (queue_overloaded(atomic_read(&tipc_queue_size), |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1195 | OVERLOAD_LIMIT_BASE, msg)) |
| 1196 | return TIPC_ERR_OVERLOAD; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1197 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1198 | recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue); |
| 1199 | if (unlikely(recv_q_len > (OVERLOAD_LIMIT_BASE / 2))) { |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1200 | if (queue_overloaded(recv_q_len, |
| 1201 | OVERLOAD_LIMIT_BASE / 2, msg)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1202 | return TIPC_ERR_OVERLOAD; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1203 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1204 | |
| 1205 | /* Initiate connection termination for an incoming 'FIN' */ |
| 1206 | |
| 1207 | if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) { |
| 1208 | sock->state = SS_DISCONNECTING; |
| 1209 | /* Note: Use signal since port lock is already taken! */ |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 1210 | tipc_k_signal((Handler)async_disconnect, tport->ref); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | /* Enqueue message (finally!) */ |
| 1214 | |
| 1215 | msg_dbg(msg,"<DISP<: "); |
| 1216 | TIPC_SKB_CB(buf)->handle = msg_data(msg); |
| 1217 | atomic_inc(&tipc_queue_size); |
| 1218 | skb_queue_tail(&sock->sk->sk_receive_queue, buf); |
| 1219 | |
Allan Stephens | cfb0c08 | 2006-10-16 21:47:18 -0700 | [diff] [blame] | 1220 | if (waitqueue_active(sock->sk->sk_sleep)) |
| 1221 | wake_up_interruptible(sock->sk->sk_sleep); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1222 | return TIPC_OK; |
| 1223 | } |
| 1224 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1225 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1226 | * wakeupdispatch - wake up port after congestion |
| 1227 | * @tport: port to wakeup |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1228 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1229 | * Called with port lock on. |
| 1230 | */ |
| 1231 | |
| 1232 | static void wakeupdispatch(struct tipc_port *tport) |
| 1233 | { |
| 1234 | struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle; |
| 1235 | |
Allan Stephens | cfb0c08 | 2006-10-16 21:47:18 -0700 | [diff] [blame] | 1236 | if (waitqueue_active(tsock->sk.sk_sleep)) |
| 1237 | wake_up_interruptible(tsock->sk.sk_sleep); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | /** |
| 1241 | * connect - establish a connection to another TIPC port |
| 1242 | * @sock: socket structure |
| 1243 | * @dest: socket address for destination port |
| 1244 | * @destlen: size of socket address data structure |
| 1245 | * @flags: (unused) |
| 1246 | * |
| 1247 | * Returns 0 on success, errno otherwise |
| 1248 | */ |
| 1249 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1250 | static int connect(struct socket *sock, struct sockaddr *dest, int destlen, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1251 | int flags) |
| 1252 | { |
| 1253 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
| 1254 | struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest; |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 1255 | struct msghdr m = {NULL,}; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1256 | struct sk_buff *buf; |
| 1257 | struct tipc_msg *msg; |
| 1258 | int res; |
| 1259 | |
| 1260 | /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */ |
| 1261 | |
| 1262 | if (sock->state == SS_READY) |
| 1263 | return -EOPNOTSUPP; |
| 1264 | |
Allan Stephens | 51f9cc1 | 2006-06-25 23:49:06 -0700 | [diff] [blame] | 1265 | /* Issue Posix-compliant error code if socket is in the wrong state */ |
| 1266 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1267 | if (sock->state == SS_LISTENING) |
| 1268 | return -EOPNOTSUPP; |
| 1269 | if (sock->state == SS_CONNECTING) |
| 1270 | return -EALREADY; |
| 1271 | if (sock->state != SS_UNCONNECTED) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1272 | return -EISCONN; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1273 | |
Allan Stephens | 51f9cc1 | 2006-06-25 23:49:06 -0700 | [diff] [blame] | 1274 | /* |
| 1275 | * Reject connection attempt using multicast address |
| 1276 | * |
| 1277 | * Note: send_msg() validates the rest of the address fields, |
| 1278 | * so there's no need to do it here |
| 1279 | */ |
| 1280 | |
| 1281 | if (dst->addrtype == TIPC_ADDR_MCAST) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1282 | return -EINVAL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1283 | |
| 1284 | /* Send a 'SYN-' to destination */ |
| 1285 | |
| 1286 | m.msg_name = dest; |
Allan Stephens | 51f9cc1 | 2006-06-25 23:49:06 -0700 | [diff] [blame] | 1287 | m.msg_namelen = destlen; |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 1288 | if ((res = send_msg(NULL, sock, &m, 0)) < 0) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1289 | sock->state = SS_DISCONNECTING; |
| 1290 | return res; |
| 1291 | } |
| 1292 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1293 | if (down_interruptible(&tsock->sem)) |
| 1294 | return -ERESTARTSYS; |
| 1295 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1296 | /* Wait for destination's 'ACK' response */ |
| 1297 | |
| 1298 | res = wait_event_interruptible_timeout(*sock->sk->sk_sleep, |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1299 | skb_queue_len(&sock->sk->sk_receive_queue), |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1300 | sock->sk->sk_rcvtimeo); |
| 1301 | buf = skb_peek(&sock->sk->sk_receive_queue); |
| 1302 | if (res > 0) { |
| 1303 | msg = buf_msg(buf); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1304 | res = auto_connect(sock, tsock, msg); |
| 1305 | if (!res) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1306 | if (!msg_data_sz(msg)) |
| 1307 | advance_queue(tsock); |
| 1308 | } |
| 1309 | } else { |
| 1310 | if (res == 0) { |
| 1311 | res = -ETIMEDOUT; |
| 1312 | } else |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1313 | { /* leave "res" unchanged */ } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1314 | sock->state = SS_DISCONNECTING; |
| 1315 | } |
| 1316 | |
| 1317 | up(&tsock->sem); |
| 1318 | return res; |
| 1319 | } |
| 1320 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1321 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1322 | * listen - allow socket to listen for incoming connections |
| 1323 | * @sock: socket structure |
| 1324 | * @len: (unused) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1325 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1326 | * Returns 0 on success, errno otherwise |
| 1327 | */ |
| 1328 | |
| 1329 | static int listen(struct socket *sock, int len) |
| 1330 | { |
| 1331 | /* REQUIRES SOCKET LOCKING OF SOME SORT? */ |
| 1332 | |
| 1333 | if (sock->state == SS_READY) |
| 1334 | return -EOPNOTSUPP; |
| 1335 | if (sock->state != SS_UNCONNECTED) |
| 1336 | return -EINVAL; |
| 1337 | sock->state = SS_LISTENING; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1338 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1339 | } |
| 1340 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1341 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1342 | * accept - wait for connection request |
| 1343 | * @sock: listening socket |
| 1344 | * @newsock: new socket that is to be connected |
| 1345 | * @flags: file-related flags associated with socket |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1346 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1347 | * Returns 0 on success, errno otherwise |
| 1348 | */ |
| 1349 | |
| 1350 | static int accept(struct socket *sock, struct socket *newsock, int flags) |
| 1351 | { |
| 1352 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
| 1353 | struct sk_buff *buf; |
| 1354 | int res = -EFAULT; |
| 1355 | |
| 1356 | if (sock->state == SS_READY) |
| 1357 | return -EOPNOTSUPP; |
| 1358 | if (sock->state != SS_LISTENING) |
| 1359 | return -EINVAL; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1360 | |
| 1361 | if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) && |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1362 | (flags & O_NONBLOCK))) |
| 1363 | return -EWOULDBLOCK; |
| 1364 | |
| 1365 | if (down_interruptible(&tsock->sem)) |
| 1366 | return -ERESTARTSYS; |
| 1367 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1368 | if (wait_event_interruptible(*sock->sk->sk_sleep, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1369 | skb_queue_len(&sock->sk->sk_receive_queue))) { |
| 1370 | res = -ERESTARTSYS; |
| 1371 | goto exit; |
| 1372 | } |
| 1373 | buf = skb_peek(&sock->sk->sk_receive_queue); |
| 1374 | |
| 1375 | res = tipc_create(newsock, 0); |
| 1376 | if (!res) { |
| 1377 | struct tipc_sock *new_tsock = tipc_sk(newsock->sk); |
| 1378 | struct tipc_portid id; |
| 1379 | struct tipc_msg *msg = buf_msg(buf); |
| 1380 | u32 new_ref = new_tsock->p->ref; |
| 1381 | |
| 1382 | id.ref = msg_origport(msg); |
| 1383 | id.node = msg_orignode(msg); |
| 1384 | tipc_connect2port(new_ref, &id); |
| 1385 | newsock->state = SS_CONNECTED; |
| 1386 | |
| 1387 | tipc_set_portimportance(new_ref, msg_importance(msg)); |
| 1388 | if (msg_named(msg)) { |
| 1389 | new_tsock->p->conn_type = msg_nametype(msg); |
| 1390 | new_tsock->p->conn_instance = msg_nameinst(msg); |
| 1391 | } |
| 1392 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1393 | /* |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1394 | * Respond to 'SYN-' by discarding it & returning 'ACK'-. |
| 1395 | * Respond to 'SYN+' by queuing it on new socket. |
| 1396 | */ |
| 1397 | |
| 1398 | msg_dbg(msg,"<ACC<: "); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1399 | if (!msg_data_sz(msg)) { |
| 1400 | struct msghdr m = {NULL,}; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1401 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1402 | send_packet(NULL, newsock, &m, 0); |
| 1403 | advance_queue(tsock); |
| 1404 | } else { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1405 | sock_lock(tsock); |
| 1406 | skb_dequeue(&sock->sk->sk_receive_queue); |
| 1407 | sock_unlock(tsock); |
| 1408 | skb_queue_head(&newsock->sk->sk_receive_queue, buf); |
| 1409 | } |
| 1410 | } |
| 1411 | exit: |
| 1412 | up(&tsock->sem); |
| 1413 | return res; |
| 1414 | } |
| 1415 | |
| 1416 | /** |
| 1417 | * shutdown - shutdown socket connection |
| 1418 | * @sock: socket structure |
Allan Stephens | e9024f0f | 2006-06-25 23:43:57 -0700 | [diff] [blame] | 1419 | * @how: direction to close (unused; always treated as read + write) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1420 | * |
| 1421 | * Terminates connection (if necessary), then purges socket's receive queue. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1422 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1423 | * Returns 0 on success, errno otherwise |
| 1424 | */ |
| 1425 | |
| 1426 | static int shutdown(struct socket *sock, int how) |
| 1427 | { |
| 1428 | struct tipc_sock* tsock = tipc_sk(sock->sk); |
| 1429 | struct sk_buff *buf; |
| 1430 | int res; |
| 1431 | |
| 1432 | /* Could return -EINVAL for an invalid "how", but why bother? */ |
| 1433 | |
| 1434 | if (down_interruptible(&tsock->sem)) |
| 1435 | return -ERESTARTSYS; |
| 1436 | |
| 1437 | sock_lock(tsock); |
| 1438 | |
| 1439 | switch (sock->state) { |
| 1440 | case SS_CONNECTED: |
| 1441 | |
| 1442 | /* Send 'FIN+' or 'FIN-' message to peer */ |
| 1443 | |
| 1444 | sock_unlock(tsock); |
| 1445 | restart: |
| 1446 | if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) { |
| 1447 | atomic_dec(&tipc_queue_size); |
| 1448 | if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) { |
| 1449 | buf_discard(buf); |
| 1450 | goto restart; |
| 1451 | } |
| 1452 | tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN); |
| 1453 | } |
| 1454 | else { |
| 1455 | tipc_shutdown(tsock->p->ref); |
| 1456 | } |
| 1457 | sock_lock(tsock); |
| 1458 | |
| 1459 | /* fall through */ |
| 1460 | |
| 1461 | case SS_DISCONNECTING: |
| 1462 | |
| 1463 | /* Discard any unreceived messages */ |
| 1464 | |
| 1465 | while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) { |
| 1466 | atomic_dec(&tipc_queue_size); |
| 1467 | buf_discard(buf); |
| 1468 | } |
| 1469 | tsock->p->conn_unacked = 0; |
| 1470 | |
| 1471 | /* fall through */ |
| 1472 | |
| 1473 | case SS_CONNECTING: |
| 1474 | sock->state = SS_DISCONNECTING; |
| 1475 | res = 0; |
| 1476 | break; |
| 1477 | |
| 1478 | default: |
| 1479 | res = -ENOTCONN; |
| 1480 | } |
| 1481 | |
| 1482 | sock_unlock(tsock); |
| 1483 | |
| 1484 | up(&tsock->sem); |
| 1485 | return res; |
| 1486 | } |
| 1487 | |
| 1488 | /** |
| 1489 | * setsockopt - set socket option |
| 1490 | * @sock: socket structure |
| 1491 | * @lvl: option level |
| 1492 | * @opt: option identifier |
| 1493 | * @ov: pointer to new option value |
| 1494 | * @ol: length of option value |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1495 | * |
| 1496 | * For stream sockets only, accepts and ignores all IPPROTO_TCP options |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1497 | * (to ease compatibility). |
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 | * Returns 0 on success, errno otherwise |
| 1500 | */ |
| 1501 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1502 | static int setsockopt(struct socket *sock, |
Allan Stephens | e9024f0f | 2006-06-25 23:43:57 -0700 | [diff] [blame] | 1503 | int lvl, int opt, char __user *ov, int ol) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1504 | { |
| 1505 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
| 1506 | u32 value; |
| 1507 | int res; |
| 1508 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1509 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
| 1510 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1511 | if (lvl != SOL_TIPC) |
| 1512 | return -ENOPROTOOPT; |
| 1513 | if (ol < sizeof(value)) |
| 1514 | return -EINVAL; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1515 | if ((res = get_user(value, (u32 __user *)ov))) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1516 | return res; |
| 1517 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1518 | if (down_interruptible(&tsock->sem)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1519 | return -ERESTARTSYS; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1520 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1521 | switch (opt) { |
| 1522 | case TIPC_IMPORTANCE: |
| 1523 | res = tipc_set_portimportance(tsock->p->ref, value); |
| 1524 | break; |
| 1525 | case TIPC_SRC_DROPPABLE: |
| 1526 | if (sock->type != SOCK_STREAM) |
| 1527 | res = tipc_set_portunreliable(tsock->p->ref, value); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1528 | else |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1529 | res = -ENOPROTOOPT; |
| 1530 | break; |
| 1531 | case TIPC_DEST_DROPPABLE: |
| 1532 | res = tipc_set_portunreturnable(tsock->p->ref, value); |
| 1533 | break; |
| 1534 | case TIPC_CONN_TIMEOUT: |
| 1535 | sock->sk->sk_rcvtimeo = (value * HZ / 1000); |
| 1536 | break; |
| 1537 | default: |
| 1538 | res = -EINVAL; |
| 1539 | } |
| 1540 | |
| 1541 | up(&tsock->sem); |
| 1542 | return res; |
| 1543 | } |
| 1544 | |
| 1545 | /** |
| 1546 | * getsockopt - get socket option |
| 1547 | * @sock: socket structure |
| 1548 | * @lvl: option level |
| 1549 | * @opt: option identifier |
| 1550 | * @ov: receptacle for option value |
| 1551 | * @ol: receptacle for length of option value |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1552 | * |
| 1553 | * 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] | 1554 | * (to ease compatibility). |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1555 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1556 | * Returns 0 on success, errno otherwise |
| 1557 | */ |
| 1558 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1559 | static int getsockopt(struct socket *sock, |
Al Viro | 28c4dad | 2006-10-10 22:45:57 +0100 | [diff] [blame] | 1560 | int lvl, int opt, char __user *ov, int __user *ol) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1561 | { |
| 1562 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1563 | int len; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1564 | u32 value; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1565 | int res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1566 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1567 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
| 1568 | return put_user(0, ol); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1569 | if (lvl != SOL_TIPC) |
| 1570 | return -ENOPROTOOPT; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1571 | if ((res = get_user(len, ol))) |
| 1572 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1573 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1574 | if (down_interruptible(&tsock->sem)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1575 | return -ERESTARTSYS; |
| 1576 | |
| 1577 | switch (opt) { |
| 1578 | case TIPC_IMPORTANCE: |
| 1579 | res = tipc_portimportance(tsock->p->ref, &value); |
| 1580 | break; |
| 1581 | case TIPC_SRC_DROPPABLE: |
| 1582 | res = tipc_portunreliable(tsock->p->ref, &value); |
| 1583 | break; |
| 1584 | case TIPC_DEST_DROPPABLE: |
| 1585 | res = tipc_portunreturnable(tsock->p->ref, &value); |
| 1586 | break; |
| 1587 | case TIPC_CONN_TIMEOUT: |
| 1588 | value = (sock->sk->sk_rcvtimeo * 1000) / HZ; |
| 1589 | break; |
| 1590 | default: |
| 1591 | res = -EINVAL; |
| 1592 | } |
| 1593 | |
| 1594 | if (res) { |
| 1595 | /* "get" failed */ |
| 1596 | } |
| 1597 | else if (len < sizeof(value)) { |
| 1598 | res = -EINVAL; |
| 1599 | } |
| 1600 | else if ((res = copy_to_user(ov, &value, sizeof(value)))) { |
| 1601 | /* couldn't return value */ |
| 1602 | } |
| 1603 | else { |
| 1604 | res = put_user(sizeof(value), ol); |
| 1605 | } |
| 1606 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1607 | up(&tsock->sem); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1608 | return res; |
| 1609 | } |
| 1610 | |
| 1611 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1612 | * Protocol switches for the various types of TIPC sockets |
| 1613 | */ |
| 1614 | |
| 1615 | static struct proto_ops msg_ops = { |
| 1616 | .owner = THIS_MODULE, |
| 1617 | .family = AF_TIPC, |
| 1618 | .release = release, |
| 1619 | .bind = bind, |
| 1620 | .connect = connect, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 1621 | .socketpair = sock_no_socketpair, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1622 | .accept = accept, |
| 1623 | .getname = get_name, |
| 1624 | .poll = poll, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 1625 | .ioctl = sock_no_ioctl, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1626 | .listen = listen, |
| 1627 | .shutdown = shutdown, |
| 1628 | .setsockopt = setsockopt, |
| 1629 | .getsockopt = getsockopt, |
| 1630 | .sendmsg = send_msg, |
| 1631 | .recvmsg = recv_msg, |
YOSHIFUJI Hideaki | 8238745 | 2007-07-19 10:44:56 +0900 | [diff] [blame] | 1632 | .mmap = sock_no_mmap, |
| 1633 | .sendpage = sock_no_sendpage |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1634 | }; |
| 1635 | |
| 1636 | static struct proto_ops packet_ops = { |
| 1637 | .owner = THIS_MODULE, |
| 1638 | .family = AF_TIPC, |
| 1639 | .release = release, |
| 1640 | .bind = bind, |
| 1641 | .connect = connect, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 1642 | .socketpair = sock_no_socketpair, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1643 | .accept = accept, |
| 1644 | .getname = get_name, |
| 1645 | .poll = poll, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 1646 | .ioctl = sock_no_ioctl, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1647 | .listen = listen, |
| 1648 | .shutdown = shutdown, |
| 1649 | .setsockopt = setsockopt, |
| 1650 | .getsockopt = getsockopt, |
| 1651 | .sendmsg = send_packet, |
| 1652 | .recvmsg = recv_msg, |
YOSHIFUJI Hideaki | 8238745 | 2007-07-19 10:44:56 +0900 | [diff] [blame] | 1653 | .mmap = sock_no_mmap, |
| 1654 | .sendpage = sock_no_sendpage |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1655 | }; |
| 1656 | |
| 1657 | static struct proto_ops stream_ops = { |
| 1658 | .owner = THIS_MODULE, |
| 1659 | .family = AF_TIPC, |
| 1660 | .release = release, |
| 1661 | .bind = bind, |
| 1662 | .connect = connect, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 1663 | .socketpair = sock_no_socketpair, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1664 | .accept = accept, |
| 1665 | .getname = get_name, |
| 1666 | .poll = poll, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 1667 | .ioctl = sock_no_ioctl, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1668 | .listen = listen, |
| 1669 | .shutdown = shutdown, |
| 1670 | .setsockopt = setsockopt, |
| 1671 | .getsockopt = getsockopt, |
| 1672 | .sendmsg = send_stream, |
| 1673 | .recvmsg = recv_stream, |
YOSHIFUJI Hideaki | 8238745 | 2007-07-19 10:44:56 +0900 | [diff] [blame] | 1674 | .mmap = sock_no_mmap, |
| 1675 | .sendpage = sock_no_sendpage |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1676 | }; |
| 1677 | |
| 1678 | static struct net_proto_family tipc_family_ops = { |
| 1679 | .owner = THIS_MODULE, |
| 1680 | .family = AF_TIPC, |
| 1681 | .create = tipc_create |
| 1682 | }; |
| 1683 | |
| 1684 | static struct proto tipc_proto = { |
| 1685 | .name = "TIPC", |
| 1686 | .owner = THIS_MODULE, |
| 1687 | .obj_size = sizeof(struct tipc_sock) |
| 1688 | }; |
| 1689 | |
| 1690 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 1691 | * tipc_socket_init - initialize TIPC socket interface |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1692 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1693 | * Returns 0 on success, errno otherwise |
| 1694 | */ |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 1695 | int tipc_socket_init(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1696 | { |
| 1697 | int res; |
| 1698 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1699 | res = proto_register(&tipc_proto, 1); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1700 | if (res) { |
Per Liden | d0a14a9 | 2006-01-11 13:52:51 +0100 | [diff] [blame] | 1701 | err("Failed to register TIPC protocol type\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1702 | goto out; |
| 1703 | } |
| 1704 | |
| 1705 | res = sock_register(&tipc_family_ops); |
| 1706 | if (res) { |
Per Liden | d0a14a9 | 2006-01-11 13:52:51 +0100 | [diff] [blame] | 1707 | err("Failed to register TIPC socket type\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1708 | proto_unregister(&tipc_proto); |
| 1709 | goto out; |
| 1710 | } |
| 1711 | |
| 1712 | sockets_enabled = 1; |
| 1713 | out: |
| 1714 | return res; |
| 1715 | } |
| 1716 | |
| 1717 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 1718 | * tipc_socket_stop - stop TIPC socket interface |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1719 | */ |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 1720 | void tipc_socket_stop(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1721 | { |
| 1722 | if (!sockets_enabled) |
| 1723 | return; |
| 1724 | |
| 1725 | sockets_enabled = 0; |
| 1726 | sock_unregister(tipc_family_ops.family); |
| 1727 | proto_unregister(&tipc_proto); |
| 1728 | } |
| 1729 | |