Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * net/tipc/socket.c: TIPC socket API |
| 3 | * |
Per Liden | 593a5f2 | 2006-01-11 19:14:19 +0100 | [diff] [blame] | 4 | * Copyright (c) 2001-2006, Ericsson AB |
Allan Stephens | eb5959c | 2006-10-16 21:43:54 -0700 | [diff] [blame] | 5 | * Copyright (c) 2004-2006, 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 | |
| 85 | /* |
| 86 | * sock_lock(): Lock a port/socket pair. lock_sock() can |
| 87 | * not be used here, since the same lock must protect ports |
| 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 | { |
| 93 | spin_lock_bh(tsock->p->lock); |
| 94 | } |
| 95 | |
| 96 | /* |
| 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 | { |
| 101 | spin_unlock_bh(tsock->p->lock); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * pollmask - determine the current set of poll() events for a socket |
| 106 | * @sock: socket structure |
| 107 | * |
| 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! |
| 118 | * |
| 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 | |
| 133 | if (sock->state == SS_DISCONNECTING) |
| 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 | { |
| 149 | sock_lock(tsock); |
| 150 | buf_discard(skb_dequeue(&tsock->sk.sk_receive_queue)); |
| 151 | sock_unlock(tsock); |
| 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) |
| 159 | * |
| 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; |
| 170 | u32 ref; |
| 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.) |
| 235 | * |
| 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 | |
| 250 | dbg("sock_delete: %x\n",tsock); |
| 251 | if (!tsock) |
| 252 | return 0; |
| 253 | down_interruptible(&tsock->sem); |
| 254 | if (!sock->sk) { |
| 255 | up(&tsock->sem); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 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 | |
| 292 | atomic_dec(&tipc_user_count); |
| 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 |
| 301 | * |
| 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. |
| 305 | * |
| 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; |
| 317 | |
| 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 | } |
| 338 | |
| 339 | if (addr->scope > 0) |
| 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 | |
| 350 | /** |
| 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 |
| 356 | * |
| 357 | * Returns 0 on success, errno otherwise |
| 358 | */ |
| 359 | |
| 360 | static int get_name(struct socket *sock, struct sockaddr *uaddr, |
| 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 | |
| 393 | static unsigned int poll(struct file *file, struct socket *sock, |
| 394 | poll_table *wait) |
| 395 | { |
| 396 | poll_wait(file, sock->sk->sk_sleep, wait); |
| 397 | /* NEED LOCK HERE? */ |
| 398 | return pollmask(sock); |
| 399 | } |
| 400 | |
| 401 | /** |
| 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 |
| 405 | * |
| 406 | * Prevents restricted configuration commands from being issued by |
| 407 | * unauthorized users. |
| 408 | * |
| 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 | |
| 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; |
| 420 | |
| 421 | if (likely(dest->addr.name.name.type != TIPC_CFG_SRV)) |
| 422 | return -EACCES; |
| 423 | |
| 424 | if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr))) |
| 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; |
| 428 | |
| 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 |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 438 | * |
| 439 | * Message must have an destination specified explicitly. |
| 440 | * Used for SOCK_RDM and SOCK_DGRAM messages, |
| 441 | * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections. |
| 442 | * (Note: 'SYN+' is prohibited on SOCK_STREAM.) |
| 443 | * |
| 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); |
| 451 | struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name; |
| 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 | |
| 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) { |
| 509 | if (needs_conn) { |
| 510 | res = -EOPNOTSUPP; |
| 511 | goto exit; |
| 512 | } |
| 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 | } |
| 526 | if (m->msg_flags & MSG_DONTWAIT) { |
| 527 | res = -EWOULDBLOCK; |
| 528 | goto exit; |
| 529 | } |
| 530 | if (wait_event_interruptible(*sock->sk->sk_sleep, |
| 531 | !tsock->p->congested)) { |
| 532 | res = -ERESTARTSYS; |
| 533 | goto exit; |
| 534 | } |
| 535 | } while (1); |
| 536 | } |
| 537 | |
| 538 | /** |
| 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 |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 544 | * |
| 545 | * Used for SOCK_SEQPACKET messages and SOCK_STREAM data. |
| 546 | * |
| 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); |
| 554 | struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name; |
| 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; |
| 564 | } |
| 565 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [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) |
| 569 | res = -EPIPE; |
| 570 | else |
| 571 | res = -ENOTCONN; |
| 572 | goto exit; |
| 573 | } |
| 574 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 575 | res = tipc_send(tsock->p->ref, m->msg_iovlen, m->msg_iov); |
| 576 | if (likely(res != -ELINKCONG)) { |
| 577 | exit: |
| 578 | up(&tsock->sem); |
| 579 | return res; |
| 580 | } |
| 581 | if (m->msg_flags & MSG_DONTWAIT) { |
| 582 | res = -EWOULDBLOCK; |
| 583 | goto exit; |
| 584 | } |
| 585 | if (wait_event_interruptible(*sock->sk->sk_sleep, |
| 586 | !tsock->p->congested)) { |
| 587 | res = -ERESTARTSYS; |
| 588 | goto exit; |
| 589 | } |
| 590 | } while (1); |
| 591 | } |
| 592 | |
| 593 | /** |
| 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 |
| 599 | * |
| 600 | * Used for SOCK_STREAM data. |
| 601 | * |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 602 | * Returns the number of bytes sent on success (or partial success), |
| 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 | { |
| 610 | struct msghdr my_msg; |
| 611 | struct iovec my_iov; |
| 612 | struct iovec *curr_iov; |
| 613 | int curr_iovlen; |
| 614 | char __user *curr_start; |
| 615 | int curr_left; |
| 616 | int bytes_to_send; |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 617 | int bytes_sent; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 618 | int res; |
| 619 | |
| 620 | if (likely(total_len <= TIPC_MAX_USER_MSG_SIZE)) |
| 621 | return send_packet(iocb, sock, m, total_len); |
| 622 | |
| 623 | /* Can only send large data streams if already connected */ |
| 624 | |
| 625 | if (unlikely(sock->state != SS_CONNECTED)) { |
| 626 | if (sock->state == SS_DISCONNECTING) |
| 627 | return -EPIPE; |
| 628 | else |
| 629 | return -ENOTCONN; |
| 630 | } |
| 631 | |
Allan Stephens | eb5959c | 2006-10-16 21:43:54 -0700 | [diff] [blame] | 632 | if (unlikely(m->msg_name)) |
| 633 | return -EISCONN; |
| 634 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 635 | /* |
| 636 | * Send each iovec entry using one or more messages |
| 637 | * |
| 638 | * Note: This algorithm is good for the most likely case |
| 639 | * (i.e. one large iovec entry), but could be improved to pass sets |
| 640 | * of small iovec entries into send_packet(). |
| 641 | */ |
| 642 | |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 643 | curr_iov = m->msg_iov; |
| 644 | curr_iovlen = m->msg_iovlen; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 645 | my_msg.msg_iov = &my_iov; |
| 646 | my_msg.msg_iovlen = 1; |
Allan Stephens | eb5959c | 2006-10-16 21:43:54 -0700 | [diff] [blame] | 647 | my_msg.msg_flags = m->msg_flags; |
| 648 | my_msg.msg_name = NULL; |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 649 | bytes_sent = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 650 | |
| 651 | while (curr_iovlen--) { |
| 652 | curr_start = curr_iov->iov_base; |
| 653 | curr_left = curr_iov->iov_len; |
| 654 | |
| 655 | while (curr_left) { |
| 656 | bytes_to_send = (curr_left < TIPC_MAX_USER_MSG_SIZE) |
| 657 | ? curr_left : TIPC_MAX_USER_MSG_SIZE; |
| 658 | my_iov.iov_base = curr_start; |
| 659 | my_iov.iov_len = bytes_to_send; |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 660 | if ((res = send_packet(iocb, sock, &my_msg, 0)) < 0) { |
| 661 | return bytes_sent ? bytes_sent : res; |
| 662 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 663 | curr_left -= bytes_to_send; |
| 664 | curr_start += bytes_to_send; |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 665 | bytes_sent += bytes_to_send; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | curr_iov++; |
| 669 | } |
| 670 | |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 671 | return bytes_sent; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | /** |
| 675 | * auto_connect - complete connection setup to a remote port |
| 676 | * @sock: socket structure |
| 677 | * @tsock: TIPC-specific socket structure |
| 678 | * @msg: peer's response message |
| 679 | * |
| 680 | * Returns 0 on success, errno otherwise |
| 681 | */ |
| 682 | |
| 683 | static int auto_connect(struct socket *sock, struct tipc_sock *tsock, |
| 684 | struct tipc_msg *msg) |
| 685 | { |
| 686 | struct tipc_portid peer; |
| 687 | |
| 688 | if (msg_errcode(msg)) { |
| 689 | sock->state = SS_DISCONNECTING; |
| 690 | return -ECONNREFUSED; |
| 691 | } |
| 692 | |
| 693 | peer.ref = msg_origport(msg); |
| 694 | peer.node = msg_orignode(msg); |
| 695 | tipc_connect2port(tsock->p->ref, &peer); |
| 696 | tipc_set_portimportance(tsock->p->ref, msg_importance(msg)); |
| 697 | sock->state = SS_CONNECTED; |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * set_orig_addr - capture sender's address for received message |
| 703 | * @m: descriptor for message info |
| 704 | * @msg: received message header |
| 705 | * |
| 706 | * Note: Address is not captured if not requested by receiver. |
| 707 | */ |
| 708 | |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 709 | static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 710 | { |
| 711 | struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name; |
| 712 | |
| 713 | if (addr) { |
| 714 | addr->family = AF_TIPC; |
| 715 | addr->addrtype = TIPC_ADDR_ID; |
| 716 | addr->addr.id.ref = msg_origport(msg); |
| 717 | addr->addr.id.node = msg_orignode(msg); |
| 718 | addr->addr.name.domain = 0; /* could leave uninitialized */ |
| 719 | addr->scope = 0; /* could leave uninitialized */ |
| 720 | m->msg_namelen = sizeof(struct sockaddr_tipc); |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | /** |
| 725 | * anc_data_recv - optionally capture ancillary data for received message |
| 726 | * @m: descriptor for message info |
| 727 | * @msg: received message header |
| 728 | * @tport: TIPC port associated with message |
| 729 | * |
| 730 | * Note: Ancillary data is not captured if not requested by receiver. |
| 731 | * |
| 732 | * Returns 0 if successful, otherwise errno |
| 733 | */ |
| 734 | |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 735 | static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 736 | struct tipc_port *tport) |
| 737 | { |
| 738 | u32 anc_data[3]; |
| 739 | u32 err; |
| 740 | u32 dest_type; |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 741 | int has_name; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 742 | int res; |
| 743 | |
| 744 | if (likely(m->msg_controllen == 0)) |
| 745 | return 0; |
| 746 | |
| 747 | /* Optionally capture errored message object(s) */ |
| 748 | |
| 749 | err = msg ? msg_errcode(msg) : 0; |
| 750 | if (unlikely(err)) { |
| 751 | anc_data[0] = err; |
| 752 | anc_data[1] = msg_data_sz(msg); |
Allan Stephens | 4b087b2 | 2006-06-25 23:47:44 -0700 | [diff] [blame] | 753 | if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data))) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 754 | return res; |
| 755 | if (anc_data[1] && |
Allan Stephens | 4b087b2 | 2006-06-25 23:47:44 -0700 | [diff] [blame] | 756 | (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1], |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 757 | msg_data(msg)))) |
| 758 | return res; |
| 759 | } |
| 760 | |
| 761 | /* Optionally capture message destination object */ |
| 762 | |
| 763 | dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG; |
| 764 | switch (dest_type) { |
| 765 | case TIPC_NAMED_MSG: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 766 | has_name = 1; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 767 | anc_data[0] = msg_nametype(msg); |
| 768 | anc_data[1] = msg_namelower(msg); |
| 769 | anc_data[2] = msg_namelower(msg); |
| 770 | break; |
| 771 | case TIPC_MCAST_MSG: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 772 | has_name = 1; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 773 | anc_data[0] = msg_nametype(msg); |
| 774 | anc_data[1] = msg_namelower(msg); |
| 775 | anc_data[2] = msg_nameupper(msg); |
| 776 | break; |
| 777 | case TIPC_CONN_MSG: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 778 | has_name = (tport->conn_type != 0); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 779 | anc_data[0] = tport->conn_type; |
| 780 | anc_data[1] = tport->conn_instance; |
| 781 | anc_data[2] = tport->conn_instance; |
| 782 | break; |
| 783 | default: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 784 | has_name = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 785 | } |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 786 | if (has_name && |
Allan Stephens | 4b087b2 | 2006-06-25 23:47:44 -0700 | [diff] [blame] | 787 | (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data))) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 788 | return res; |
| 789 | |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * recv_msg - receive packet-oriented message |
| 795 | * @iocb: (unused) |
| 796 | * @m: descriptor for message info |
| 797 | * @buf_len: total size of user buffer area |
| 798 | * @flags: receive flags |
| 799 | * |
| 800 | * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages. |
| 801 | * If the complete message doesn't fit in user area, truncate it. |
| 802 | * |
| 803 | * Returns size of returned message data, errno otherwise |
| 804 | */ |
| 805 | |
| 806 | static int recv_msg(struct kiocb *iocb, struct socket *sock, |
| 807 | struct msghdr *m, size_t buf_len, int flags) |
| 808 | { |
| 809 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
| 810 | struct sk_buff *buf; |
| 811 | struct tipc_msg *msg; |
| 812 | unsigned int q_len; |
| 813 | unsigned int sz; |
| 814 | u32 err; |
| 815 | int res; |
| 816 | |
| 817 | /* Currently doesn't support receiving into multiple iovec entries */ |
| 818 | |
| 819 | if (m->msg_iovlen != 1) |
| 820 | return -EOPNOTSUPP; |
| 821 | |
| 822 | /* Catch invalid receive attempts */ |
| 823 | |
| 824 | if (unlikely(!buf_len)) |
| 825 | return -EINVAL; |
| 826 | |
| 827 | if (sock->type == SOCK_SEQPACKET) { |
| 828 | if (unlikely(sock->state == SS_UNCONNECTED)) |
| 829 | return -ENOTCONN; |
| 830 | if (unlikely((sock->state == SS_DISCONNECTING) && |
| 831 | (skb_queue_len(&sock->sk->sk_receive_queue) == 0))) |
| 832 | return -ENOTCONN; |
| 833 | } |
| 834 | |
| 835 | /* Look for a message in receive queue; wait if necessary */ |
| 836 | |
| 837 | if (unlikely(down_interruptible(&tsock->sem))) |
| 838 | return -ERESTARTSYS; |
| 839 | |
| 840 | restart: |
| 841 | if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) && |
| 842 | (flags & MSG_DONTWAIT))) { |
| 843 | res = -EWOULDBLOCK; |
| 844 | goto exit; |
| 845 | } |
| 846 | |
| 847 | if ((res = wait_event_interruptible( |
| 848 | *sock->sk->sk_sleep, |
| 849 | ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) || |
| 850 | (sock->state == SS_DISCONNECTING))) )) { |
| 851 | goto exit; |
| 852 | } |
| 853 | |
| 854 | /* Catch attempt to receive on an already terminated connection */ |
| 855 | /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */ |
| 856 | |
| 857 | if (!q_len) { |
| 858 | res = -ENOTCONN; |
| 859 | goto exit; |
| 860 | } |
| 861 | |
| 862 | /* Get access to first message in receive queue */ |
| 863 | |
| 864 | buf = skb_peek(&sock->sk->sk_receive_queue); |
| 865 | msg = buf_msg(buf); |
| 866 | sz = msg_data_sz(msg); |
| 867 | err = msg_errcode(msg); |
| 868 | |
| 869 | /* Complete connection setup for an implied connect */ |
| 870 | |
| 871 | if (unlikely(sock->state == SS_CONNECTING)) { |
| 872 | if ((res = auto_connect(sock, tsock, msg))) |
| 873 | goto exit; |
| 874 | } |
| 875 | |
| 876 | /* Discard an empty non-errored message & try again */ |
| 877 | |
| 878 | if ((!sz) && (!err)) { |
| 879 | advance_queue(tsock); |
| 880 | goto restart; |
| 881 | } |
| 882 | |
| 883 | /* Capture sender's address (optional) */ |
| 884 | |
| 885 | set_orig_addr(m, msg); |
| 886 | |
| 887 | /* Capture ancillary data (optional) */ |
| 888 | |
| 889 | if ((res = anc_data_recv(m, msg, tsock->p))) |
| 890 | goto exit; |
| 891 | |
| 892 | /* Capture message data (if valid) & compute return value (always) */ |
| 893 | |
| 894 | if (!err) { |
| 895 | if (unlikely(buf_len < sz)) { |
| 896 | sz = buf_len; |
| 897 | m->msg_flags |= MSG_TRUNC; |
| 898 | } |
| 899 | if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg), |
| 900 | sz))) { |
| 901 | res = -EFAULT; |
| 902 | goto exit; |
| 903 | } |
| 904 | res = sz; |
| 905 | } else { |
| 906 | if ((sock->state == SS_READY) || |
| 907 | ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)) |
| 908 | res = 0; |
| 909 | else |
| 910 | res = -ECONNRESET; |
| 911 | } |
| 912 | |
| 913 | /* Consume received message (optional) */ |
| 914 | |
| 915 | if (likely(!(flags & MSG_PEEK))) { |
| 916 | if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN)) |
| 917 | tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked); |
| 918 | advance_queue(tsock); |
| 919 | } |
| 920 | exit: |
| 921 | up(&tsock->sem); |
| 922 | return res; |
| 923 | } |
| 924 | |
| 925 | /** |
| 926 | * recv_stream - receive stream-oriented data |
| 927 | * @iocb: (unused) |
| 928 | * @m: descriptor for message info |
| 929 | * @buf_len: total size of user buffer area |
| 930 | * @flags: receive flags |
| 931 | * |
| 932 | * Used for SOCK_STREAM messages only. If not enough data is available |
| 933 | * will optionally wait for more; never truncates data. |
| 934 | * |
| 935 | * Returns size of returned message data, errno otherwise |
| 936 | */ |
| 937 | |
| 938 | static int recv_stream(struct kiocb *iocb, struct socket *sock, |
| 939 | struct msghdr *m, size_t buf_len, int flags) |
| 940 | { |
| 941 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
| 942 | struct sk_buff *buf; |
| 943 | struct tipc_msg *msg; |
| 944 | unsigned int q_len; |
| 945 | unsigned int sz; |
| 946 | int sz_to_copy; |
| 947 | int sz_copied = 0; |
| 948 | int needed; |
Al Viro | 28c4dad | 2006-10-10 22:45:57 +0100 | [diff] [blame] | 949 | char __user *crs = m->msg_iov->iov_base; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 950 | unsigned char *buf_crs; |
| 951 | u32 err; |
| 952 | int res; |
| 953 | |
| 954 | /* Currently doesn't support receiving into multiple iovec entries */ |
| 955 | |
| 956 | if (m->msg_iovlen != 1) |
| 957 | return -EOPNOTSUPP; |
| 958 | |
| 959 | /* Catch invalid receive attempts */ |
| 960 | |
| 961 | if (unlikely(!buf_len)) |
| 962 | return -EINVAL; |
| 963 | |
| 964 | if (unlikely(sock->state == SS_DISCONNECTING)) { |
| 965 | if (skb_queue_len(&sock->sk->sk_receive_queue) == 0) |
| 966 | return -ENOTCONN; |
| 967 | } else if (unlikely(sock->state != SS_CONNECTED)) |
| 968 | return -ENOTCONN; |
| 969 | |
| 970 | /* Look for a message in receive queue; wait if necessary */ |
| 971 | |
| 972 | if (unlikely(down_interruptible(&tsock->sem))) |
| 973 | return -ERESTARTSYS; |
| 974 | |
| 975 | restart: |
| 976 | if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) && |
| 977 | (flags & MSG_DONTWAIT))) { |
Allan Stephens | a3b0a5a | 2006-06-25 23:48:22 -0700 | [diff] [blame] | 978 | res = -EWOULDBLOCK; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 979 | goto exit; |
| 980 | } |
| 981 | |
| 982 | if ((res = wait_event_interruptible( |
| 983 | *sock->sk->sk_sleep, |
| 984 | ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) || |
| 985 | (sock->state == SS_DISCONNECTING))) )) { |
| 986 | goto exit; |
| 987 | } |
| 988 | |
| 989 | /* Catch attempt to receive on an already terminated connection */ |
| 990 | /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */ |
| 991 | |
| 992 | if (!q_len) { |
| 993 | res = -ENOTCONN; |
| 994 | goto exit; |
| 995 | } |
| 996 | |
| 997 | /* Get access to first message in receive queue */ |
| 998 | |
| 999 | buf = skb_peek(&sock->sk->sk_receive_queue); |
| 1000 | msg = buf_msg(buf); |
| 1001 | sz = msg_data_sz(msg); |
| 1002 | err = msg_errcode(msg); |
| 1003 | |
| 1004 | /* Discard an empty non-errored message & try again */ |
| 1005 | |
| 1006 | if ((!sz) && (!err)) { |
| 1007 | advance_queue(tsock); |
| 1008 | goto restart; |
| 1009 | } |
| 1010 | |
| 1011 | /* Optionally capture sender's address & ancillary data of first msg */ |
| 1012 | |
| 1013 | if (sz_copied == 0) { |
| 1014 | set_orig_addr(m, msg); |
| 1015 | if ((res = anc_data_recv(m, msg, tsock->p))) |
| 1016 | goto exit; |
| 1017 | } |
| 1018 | |
| 1019 | /* Capture message data (if valid) & compute return value (always) */ |
| 1020 | |
| 1021 | if (!err) { |
| 1022 | buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle); |
| 1023 | sz = buf->tail - buf_crs; |
| 1024 | |
| 1025 | needed = (buf_len - sz_copied); |
| 1026 | sz_to_copy = (sz <= needed) ? sz : needed; |
| 1027 | if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) { |
| 1028 | res = -EFAULT; |
| 1029 | goto exit; |
| 1030 | } |
| 1031 | sz_copied += sz_to_copy; |
| 1032 | |
| 1033 | if (sz_to_copy < sz) { |
| 1034 | if (!(flags & MSG_PEEK)) |
| 1035 | TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy; |
| 1036 | goto exit; |
| 1037 | } |
| 1038 | |
| 1039 | crs += sz_to_copy; |
| 1040 | } else { |
| 1041 | if (sz_copied != 0) |
| 1042 | goto exit; /* can't add error msg to valid data */ |
| 1043 | |
| 1044 | if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control) |
| 1045 | res = 0; |
| 1046 | else |
| 1047 | res = -ECONNRESET; |
| 1048 | } |
| 1049 | |
| 1050 | /* Consume received message (optional) */ |
| 1051 | |
| 1052 | if (likely(!(flags & MSG_PEEK))) { |
| 1053 | if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN)) |
| 1054 | tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked); |
| 1055 | advance_queue(tsock); |
| 1056 | } |
| 1057 | |
| 1058 | /* Loop around if more data is required */ |
| 1059 | |
| 1060 | if ((sz_copied < buf_len) /* didn't get all requested data */ |
| 1061 | && (flags & MSG_WAITALL) /* ... and need to wait for more */ |
| 1062 | && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */ |
| 1063 | && (!err) /* ... and haven't reached a FIN */ |
| 1064 | ) |
| 1065 | goto restart; |
| 1066 | |
| 1067 | exit: |
| 1068 | up(&tsock->sem); |
Allan Stephens | a3b0a5a | 2006-06-25 23:48:22 -0700 | [diff] [blame] | 1069 | return sz_copied ? sz_copied : res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | /** |
| 1073 | * queue_overloaded - test if queue overload condition exists |
| 1074 | * @queue_size: current size of queue |
| 1075 | * @base: nominal maximum size of queue |
| 1076 | * @msg: message to be added to queue |
| 1077 | * |
| 1078 | * Returns 1 if queue is currently overloaded, 0 otherwise |
| 1079 | */ |
| 1080 | |
| 1081 | static int queue_overloaded(u32 queue_size, u32 base, struct tipc_msg *msg) |
| 1082 | { |
| 1083 | u32 threshold; |
| 1084 | u32 imp = msg_importance(msg); |
| 1085 | |
| 1086 | if (imp == TIPC_LOW_IMPORTANCE) |
| 1087 | threshold = base; |
| 1088 | else if (imp == TIPC_MEDIUM_IMPORTANCE) |
| 1089 | threshold = base * 2; |
| 1090 | else if (imp == TIPC_HIGH_IMPORTANCE) |
| 1091 | threshold = base * 100; |
| 1092 | else |
| 1093 | return 0; |
| 1094 | |
| 1095 | if (msg_connected(msg)) |
| 1096 | threshold *= 4; |
| 1097 | |
| 1098 | return (queue_size > threshold); |
| 1099 | } |
| 1100 | |
| 1101 | /** |
| 1102 | * async_disconnect - wrapper function used to disconnect port |
| 1103 | * @portref: TIPC port reference (passed as pointer-sized value) |
| 1104 | */ |
| 1105 | |
| 1106 | static void async_disconnect(unsigned long portref) |
| 1107 | { |
| 1108 | tipc_disconnect((u32)portref); |
| 1109 | } |
| 1110 | |
| 1111 | /** |
| 1112 | * dispatch - handle arriving message |
| 1113 | * @tport: TIPC port that received message |
| 1114 | * @buf: message |
| 1115 | * |
| 1116 | * Called with port locked. Must not take socket lock to avoid deadlock risk. |
| 1117 | * |
| 1118 | * Returns TIPC error status code (TIPC_OK if message is not to be rejected) |
| 1119 | */ |
| 1120 | |
| 1121 | static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf) |
| 1122 | { |
| 1123 | struct tipc_msg *msg = buf_msg(buf); |
| 1124 | struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle; |
| 1125 | struct socket *sock; |
| 1126 | u32 recv_q_len; |
| 1127 | |
| 1128 | /* Reject message if socket is closing */ |
| 1129 | |
| 1130 | if (!tsock) |
| 1131 | return TIPC_ERR_NO_PORT; |
| 1132 | |
| 1133 | /* Reject message if it is wrong sort of message for socket */ |
| 1134 | |
| 1135 | /* |
| 1136 | * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD? |
| 1137 | * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY |
| 1138 | * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC |
| 1139 | */ |
| 1140 | sock = tsock->sk.sk_socket; |
| 1141 | if (sock->state == SS_READY) { |
| 1142 | if (msg_connected(msg)) { |
| 1143 | msg_dbg(msg, "dispatch filter 1\n"); |
| 1144 | return TIPC_ERR_NO_PORT; |
| 1145 | } |
| 1146 | } else { |
| 1147 | if (msg_mcast(msg)) { |
| 1148 | msg_dbg(msg, "dispatch filter 2\n"); |
| 1149 | return TIPC_ERR_NO_PORT; |
| 1150 | } |
| 1151 | if (sock->state == SS_CONNECTED) { |
| 1152 | if (!msg_connected(msg)) { |
| 1153 | msg_dbg(msg, "dispatch filter 3\n"); |
| 1154 | return TIPC_ERR_NO_PORT; |
| 1155 | } |
| 1156 | } |
| 1157 | else if (sock->state == SS_CONNECTING) { |
| 1158 | if (!msg_connected(msg) && (msg_errcode(msg) == 0)) { |
| 1159 | msg_dbg(msg, "dispatch filter 4\n"); |
| 1160 | return TIPC_ERR_NO_PORT; |
| 1161 | } |
| 1162 | } |
| 1163 | else if (sock->state == SS_LISTENING) { |
| 1164 | if (msg_connected(msg) || msg_errcode(msg)) { |
| 1165 | msg_dbg(msg, "dispatch filter 5\n"); |
| 1166 | return TIPC_ERR_NO_PORT; |
| 1167 | } |
| 1168 | } |
| 1169 | else if (sock->state == SS_DISCONNECTING) { |
| 1170 | msg_dbg(msg, "dispatch filter 6\n"); |
| 1171 | return TIPC_ERR_NO_PORT; |
| 1172 | } |
| 1173 | else /* (sock->state == SS_UNCONNECTED) */ { |
| 1174 | if (msg_connected(msg) || msg_errcode(msg)) { |
| 1175 | msg_dbg(msg, "dispatch filter 7\n"); |
| 1176 | return TIPC_ERR_NO_PORT; |
| 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | /* Reject message if there isn't room to queue it */ |
| 1182 | |
| 1183 | if (unlikely((u32)atomic_read(&tipc_queue_size) > |
| 1184 | OVERLOAD_LIMIT_BASE)) { |
| 1185 | if (queue_overloaded(atomic_read(&tipc_queue_size), |
| 1186 | OVERLOAD_LIMIT_BASE, msg)) |
| 1187 | return TIPC_ERR_OVERLOAD; |
| 1188 | } |
| 1189 | recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue); |
| 1190 | if (unlikely(recv_q_len > (OVERLOAD_LIMIT_BASE / 2))) { |
| 1191 | if (queue_overloaded(recv_q_len, |
| 1192 | OVERLOAD_LIMIT_BASE / 2, msg)) |
| 1193 | return TIPC_ERR_OVERLOAD; |
| 1194 | } |
| 1195 | |
| 1196 | /* Initiate connection termination for an incoming 'FIN' */ |
| 1197 | |
| 1198 | if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) { |
| 1199 | sock->state = SS_DISCONNECTING; |
| 1200 | /* Note: Use signal since port lock is already taken! */ |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 1201 | tipc_k_signal((Handler)async_disconnect, tport->ref); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | /* Enqueue message (finally!) */ |
| 1205 | |
| 1206 | msg_dbg(msg,"<DISP<: "); |
| 1207 | TIPC_SKB_CB(buf)->handle = msg_data(msg); |
| 1208 | atomic_inc(&tipc_queue_size); |
| 1209 | skb_queue_tail(&sock->sk->sk_receive_queue, buf); |
| 1210 | |
Allan Stephens | cfb0c08 | 2006-10-16 21:47:18 -0700 | [diff] [blame] | 1211 | if (waitqueue_active(sock->sk->sk_sleep)) |
| 1212 | wake_up_interruptible(sock->sk->sk_sleep); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1213 | return TIPC_OK; |
| 1214 | } |
| 1215 | |
| 1216 | /** |
| 1217 | * wakeupdispatch - wake up port after congestion |
| 1218 | * @tport: port to wakeup |
| 1219 | * |
| 1220 | * Called with port lock on. |
| 1221 | */ |
| 1222 | |
| 1223 | static void wakeupdispatch(struct tipc_port *tport) |
| 1224 | { |
| 1225 | struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle; |
| 1226 | |
Allan Stephens | cfb0c08 | 2006-10-16 21:47:18 -0700 | [diff] [blame] | 1227 | if (waitqueue_active(tsock->sk.sk_sleep)) |
| 1228 | wake_up_interruptible(tsock->sk.sk_sleep); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | /** |
| 1232 | * connect - establish a connection to another TIPC port |
| 1233 | * @sock: socket structure |
| 1234 | * @dest: socket address for destination port |
| 1235 | * @destlen: size of socket address data structure |
| 1236 | * @flags: (unused) |
| 1237 | * |
| 1238 | * Returns 0 on success, errno otherwise |
| 1239 | */ |
| 1240 | |
| 1241 | static int connect(struct socket *sock, struct sockaddr *dest, int destlen, |
| 1242 | int flags) |
| 1243 | { |
| 1244 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
| 1245 | struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest; |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 1246 | struct msghdr m = {NULL,}; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1247 | struct sk_buff *buf; |
| 1248 | struct tipc_msg *msg; |
| 1249 | int res; |
| 1250 | |
| 1251 | /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */ |
| 1252 | |
| 1253 | if (sock->state == SS_READY) |
| 1254 | return -EOPNOTSUPP; |
| 1255 | |
Allan Stephens | 51f9cc1 | 2006-06-25 23:49:06 -0700 | [diff] [blame] | 1256 | /* Issue Posix-compliant error code if socket is in the wrong state */ |
| 1257 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1258 | if (sock->state == SS_LISTENING) |
| 1259 | return -EOPNOTSUPP; |
| 1260 | if (sock->state == SS_CONNECTING) |
| 1261 | return -EALREADY; |
| 1262 | if (sock->state != SS_UNCONNECTED) |
| 1263 | return -EISCONN; |
| 1264 | |
Allan Stephens | 51f9cc1 | 2006-06-25 23:49:06 -0700 | [diff] [blame] | 1265 | /* |
| 1266 | * Reject connection attempt using multicast address |
| 1267 | * |
| 1268 | * Note: send_msg() validates the rest of the address fields, |
| 1269 | * so there's no need to do it here |
| 1270 | */ |
| 1271 | |
| 1272 | if (dst->addrtype == TIPC_ADDR_MCAST) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1273 | return -EINVAL; |
| 1274 | |
| 1275 | /* Send a 'SYN-' to destination */ |
| 1276 | |
| 1277 | m.msg_name = dest; |
Allan Stephens | 51f9cc1 | 2006-06-25 23:49:06 -0700 | [diff] [blame] | 1278 | m.msg_namelen = destlen; |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 1279 | if ((res = send_msg(NULL, sock, &m, 0)) < 0) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1280 | sock->state = SS_DISCONNECTING; |
| 1281 | return res; |
| 1282 | } |
| 1283 | |
| 1284 | if (down_interruptible(&tsock->sem)) |
| 1285 | return -ERESTARTSYS; |
| 1286 | |
| 1287 | /* Wait for destination's 'ACK' response */ |
| 1288 | |
| 1289 | res = wait_event_interruptible_timeout(*sock->sk->sk_sleep, |
| 1290 | skb_queue_len(&sock->sk->sk_receive_queue), |
| 1291 | sock->sk->sk_rcvtimeo); |
| 1292 | buf = skb_peek(&sock->sk->sk_receive_queue); |
| 1293 | if (res > 0) { |
| 1294 | msg = buf_msg(buf); |
| 1295 | res = auto_connect(sock, tsock, msg); |
| 1296 | if (!res) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1297 | if (!msg_data_sz(msg)) |
| 1298 | advance_queue(tsock); |
| 1299 | } |
| 1300 | } else { |
| 1301 | if (res == 0) { |
| 1302 | res = -ETIMEDOUT; |
| 1303 | } else |
| 1304 | { /* leave "res" unchanged */ } |
| 1305 | sock->state = SS_DISCONNECTING; |
| 1306 | } |
| 1307 | |
| 1308 | up(&tsock->sem); |
| 1309 | return res; |
| 1310 | } |
| 1311 | |
| 1312 | /** |
| 1313 | * listen - allow socket to listen for incoming connections |
| 1314 | * @sock: socket structure |
| 1315 | * @len: (unused) |
| 1316 | * |
| 1317 | * Returns 0 on success, errno otherwise |
| 1318 | */ |
| 1319 | |
| 1320 | static int listen(struct socket *sock, int len) |
| 1321 | { |
| 1322 | /* REQUIRES SOCKET LOCKING OF SOME SORT? */ |
| 1323 | |
| 1324 | if (sock->state == SS_READY) |
| 1325 | return -EOPNOTSUPP; |
| 1326 | if (sock->state != SS_UNCONNECTED) |
| 1327 | return -EINVAL; |
| 1328 | sock->state = SS_LISTENING; |
| 1329 | return 0; |
| 1330 | } |
| 1331 | |
| 1332 | /** |
| 1333 | * accept - wait for connection request |
| 1334 | * @sock: listening socket |
| 1335 | * @newsock: new socket that is to be connected |
| 1336 | * @flags: file-related flags associated with socket |
| 1337 | * |
| 1338 | * Returns 0 on success, errno otherwise |
| 1339 | */ |
| 1340 | |
| 1341 | static int accept(struct socket *sock, struct socket *newsock, int flags) |
| 1342 | { |
| 1343 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
| 1344 | struct sk_buff *buf; |
| 1345 | int res = -EFAULT; |
| 1346 | |
| 1347 | if (sock->state == SS_READY) |
| 1348 | return -EOPNOTSUPP; |
| 1349 | if (sock->state != SS_LISTENING) |
| 1350 | return -EINVAL; |
| 1351 | |
| 1352 | if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) && |
| 1353 | (flags & O_NONBLOCK))) |
| 1354 | return -EWOULDBLOCK; |
| 1355 | |
| 1356 | if (down_interruptible(&tsock->sem)) |
| 1357 | return -ERESTARTSYS; |
| 1358 | |
| 1359 | if (wait_event_interruptible(*sock->sk->sk_sleep, |
| 1360 | skb_queue_len(&sock->sk->sk_receive_queue))) { |
| 1361 | res = -ERESTARTSYS; |
| 1362 | goto exit; |
| 1363 | } |
| 1364 | buf = skb_peek(&sock->sk->sk_receive_queue); |
| 1365 | |
| 1366 | res = tipc_create(newsock, 0); |
| 1367 | if (!res) { |
| 1368 | struct tipc_sock *new_tsock = tipc_sk(newsock->sk); |
| 1369 | struct tipc_portid id; |
| 1370 | struct tipc_msg *msg = buf_msg(buf); |
| 1371 | u32 new_ref = new_tsock->p->ref; |
| 1372 | |
| 1373 | id.ref = msg_origport(msg); |
| 1374 | id.node = msg_orignode(msg); |
| 1375 | tipc_connect2port(new_ref, &id); |
| 1376 | newsock->state = SS_CONNECTED; |
| 1377 | |
| 1378 | tipc_set_portimportance(new_ref, msg_importance(msg)); |
| 1379 | if (msg_named(msg)) { |
| 1380 | new_tsock->p->conn_type = msg_nametype(msg); |
| 1381 | new_tsock->p->conn_instance = msg_nameinst(msg); |
| 1382 | } |
| 1383 | |
| 1384 | /* |
| 1385 | * Respond to 'SYN-' by discarding it & returning 'ACK'-. |
| 1386 | * Respond to 'SYN+' by queuing it on new socket. |
| 1387 | */ |
| 1388 | |
| 1389 | msg_dbg(msg,"<ACC<: "); |
| 1390 | if (!msg_data_sz(msg)) { |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 1391 | struct msghdr m = {NULL,}; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1392 | |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 1393 | send_packet(NULL, newsock, &m, 0); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1394 | advance_queue(tsock); |
| 1395 | } else { |
| 1396 | sock_lock(tsock); |
| 1397 | skb_dequeue(&sock->sk->sk_receive_queue); |
| 1398 | sock_unlock(tsock); |
| 1399 | skb_queue_head(&newsock->sk->sk_receive_queue, buf); |
| 1400 | } |
| 1401 | } |
| 1402 | exit: |
| 1403 | up(&tsock->sem); |
| 1404 | return res; |
| 1405 | } |
| 1406 | |
| 1407 | /** |
| 1408 | * shutdown - shutdown socket connection |
| 1409 | * @sock: socket structure |
Allan Stephens | e9024f0f | 2006-06-25 23:43:57 -0700 | [diff] [blame] | 1410 | * @how: direction to close (unused; always treated as read + write) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1411 | * |
| 1412 | * Terminates connection (if necessary), then purges socket's receive queue. |
| 1413 | * |
| 1414 | * Returns 0 on success, errno otherwise |
| 1415 | */ |
| 1416 | |
| 1417 | static int shutdown(struct socket *sock, int how) |
| 1418 | { |
| 1419 | struct tipc_sock* tsock = tipc_sk(sock->sk); |
| 1420 | struct sk_buff *buf; |
| 1421 | int res; |
| 1422 | |
| 1423 | /* Could return -EINVAL for an invalid "how", but why bother? */ |
| 1424 | |
| 1425 | if (down_interruptible(&tsock->sem)) |
| 1426 | return -ERESTARTSYS; |
| 1427 | |
| 1428 | sock_lock(tsock); |
| 1429 | |
| 1430 | switch (sock->state) { |
| 1431 | case SS_CONNECTED: |
| 1432 | |
| 1433 | /* Send 'FIN+' or 'FIN-' message to peer */ |
| 1434 | |
| 1435 | sock_unlock(tsock); |
| 1436 | restart: |
| 1437 | if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) { |
| 1438 | atomic_dec(&tipc_queue_size); |
| 1439 | if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) { |
| 1440 | buf_discard(buf); |
| 1441 | goto restart; |
| 1442 | } |
| 1443 | tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN); |
| 1444 | } |
| 1445 | else { |
| 1446 | tipc_shutdown(tsock->p->ref); |
| 1447 | } |
| 1448 | sock_lock(tsock); |
| 1449 | |
| 1450 | /* fall through */ |
| 1451 | |
| 1452 | case SS_DISCONNECTING: |
| 1453 | |
| 1454 | /* Discard any unreceived messages */ |
| 1455 | |
| 1456 | while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) { |
| 1457 | atomic_dec(&tipc_queue_size); |
| 1458 | buf_discard(buf); |
| 1459 | } |
| 1460 | tsock->p->conn_unacked = 0; |
| 1461 | |
| 1462 | /* fall through */ |
| 1463 | |
| 1464 | case SS_CONNECTING: |
| 1465 | sock->state = SS_DISCONNECTING; |
| 1466 | res = 0; |
| 1467 | break; |
| 1468 | |
| 1469 | default: |
| 1470 | res = -ENOTCONN; |
| 1471 | } |
| 1472 | |
| 1473 | sock_unlock(tsock); |
| 1474 | |
| 1475 | up(&tsock->sem); |
| 1476 | return res; |
| 1477 | } |
| 1478 | |
| 1479 | /** |
| 1480 | * setsockopt - set socket option |
| 1481 | * @sock: socket structure |
| 1482 | * @lvl: option level |
| 1483 | * @opt: option identifier |
| 1484 | * @ov: pointer to new option value |
| 1485 | * @ol: length of option value |
| 1486 | * |
| 1487 | * For stream sockets only, accepts and ignores all IPPROTO_TCP options |
| 1488 | * (to ease compatibility). |
| 1489 | * |
| 1490 | * Returns 0 on success, errno otherwise |
| 1491 | */ |
| 1492 | |
Allan Stephens | e9024f0f | 2006-06-25 23:43:57 -0700 | [diff] [blame] | 1493 | static int setsockopt(struct socket *sock, |
| 1494 | int lvl, int opt, char __user *ov, int ol) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1495 | { |
| 1496 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
| 1497 | u32 value; |
| 1498 | int res; |
| 1499 | |
| 1500 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
| 1501 | return 0; |
| 1502 | if (lvl != SOL_TIPC) |
| 1503 | return -ENOPROTOOPT; |
| 1504 | if (ol < sizeof(value)) |
| 1505 | return -EINVAL; |
Al Viro | 28c4dad | 2006-10-10 22:45:57 +0100 | [diff] [blame] | 1506 | if ((res = get_user(value, (u32 __user *)ov))) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1507 | return res; |
| 1508 | |
| 1509 | if (down_interruptible(&tsock->sem)) |
| 1510 | return -ERESTARTSYS; |
| 1511 | |
| 1512 | switch (opt) { |
| 1513 | case TIPC_IMPORTANCE: |
| 1514 | res = tipc_set_portimportance(tsock->p->ref, value); |
| 1515 | break; |
| 1516 | case TIPC_SRC_DROPPABLE: |
| 1517 | if (sock->type != SOCK_STREAM) |
| 1518 | res = tipc_set_portunreliable(tsock->p->ref, value); |
| 1519 | else |
| 1520 | res = -ENOPROTOOPT; |
| 1521 | break; |
| 1522 | case TIPC_DEST_DROPPABLE: |
| 1523 | res = tipc_set_portunreturnable(tsock->p->ref, value); |
| 1524 | break; |
| 1525 | case TIPC_CONN_TIMEOUT: |
| 1526 | sock->sk->sk_rcvtimeo = (value * HZ / 1000); |
| 1527 | break; |
| 1528 | default: |
| 1529 | res = -EINVAL; |
| 1530 | } |
| 1531 | |
| 1532 | up(&tsock->sem); |
| 1533 | return res; |
| 1534 | } |
| 1535 | |
| 1536 | /** |
| 1537 | * getsockopt - get socket option |
| 1538 | * @sock: socket structure |
| 1539 | * @lvl: option level |
| 1540 | * @opt: option identifier |
| 1541 | * @ov: receptacle for option value |
| 1542 | * @ol: receptacle for length of option value |
| 1543 | * |
| 1544 | * For stream sockets only, returns 0 length result for all IPPROTO_TCP options |
| 1545 | * (to ease compatibility). |
| 1546 | * |
| 1547 | * Returns 0 on success, errno otherwise |
| 1548 | */ |
| 1549 | |
Allan Stephens | e9024f0f | 2006-06-25 23:43:57 -0700 | [diff] [blame] | 1550 | static int getsockopt(struct socket *sock, |
Al Viro | 28c4dad | 2006-10-10 22:45:57 +0100 | [diff] [blame] | 1551 | int lvl, int opt, char __user *ov, int __user *ol) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1552 | { |
| 1553 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
| 1554 | int len; |
| 1555 | u32 value; |
| 1556 | int res; |
| 1557 | |
| 1558 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
| 1559 | return put_user(0, ol); |
| 1560 | if (lvl != SOL_TIPC) |
| 1561 | return -ENOPROTOOPT; |
| 1562 | if ((res = get_user(len, ol))) |
| 1563 | return res; |
| 1564 | |
| 1565 | if (down_interruptible(&tsock->sem)) |
| 1566 | return -ERESTARTSYS; |
| 1567 | |
| 1568 | switch (opt) { |
| 1569 | case TIPC_IMPORTANCE: |
| 1570 | res = tipc_portimportance(tsock->p->ref, &value); |
| 1571 | break; |
| 1572 | case TIPC_SRC_DROPPABLE: |
| 1573 | res = tipc_portunreliable(tsock->p->ref, &value); |
| 1574 | break; |
| 1575 | case TIPC_DEST_DROPPABLE: |
| 1576 | res = tipc_portunreturnable(tsock->p->ref, &value); |
| 1577 | break; |
| 1578 | case TIPC_CONN_TIMEOUT: |
| 1579 | value = (sock->sk->sk_rcvtimeo * 1000) / HZ; |
| 1580 | break; |
| 1581 | default: |
| 1582 | res = -EINVAL; |
| 1583 | } |
| 1584 | |
| 1585 | if (res) { |
| 1586 | /* "get" failed */ |
| 1587 | } |
| 1588 | else if (len < sizeof(value)) { |
| 1589 | res = -EINVAL; |
| 1590 | } |
| 1591 | else if ((res = copy_to_user(ov, &value, sizeof(value)))) { |
| 1592 | /* couldn't return value */ |
| 1593 | } |
| 1594 | else { |
| 1595 | res = put_user(sizeof(value), ol); |
| 1596 | } |
| 1597 | |
| 1598 | up(&tsock->sem); |
| 1599 | return res; |
| 1600 | } |
| 1601 | |
| 1602 | /** |
| 1603 | * Placeholders for non-implemented functionality |
| 1604 | * |
| 1605 | * Returns error code (POSIX-compliant where defined) |
| 1606 | */ |
| 1607 | |
| 1608 | static int ioctl(struct socket *s, u32 cmd, unsigned long arg) |
| 1609 | { |
| 1610 | return -EINVAL; |
| 1611 | } |
| 1612 | |
| 1613 | static int no_mmap(struct file *file, struct socket *sock, |
| 1614 | struct vm_area_struct *vma) |
| 1615 | { |
| 1616 | return -EINVAL; |
| 1617 | } |
| 1618 | static ssize_t no_sendpage(struct socket *sock, struct page *page, |
| 1619 | int offset, size_t size, int flags) |
| 1620 | { |
| 1621 | return -EINVAL; |
| 1622 | } |
| 1623 | |
| 1624 | static int no_skpair(struct socket *s1, struct socket *s2) |
| 1625 | { |
| 1626 | return -EOPNOTSUPP; |
| 1627 | } |
| 1628 | |
| 1629 | /** |
| 1630 | * Protocol switches for the various types of TIPC sockets |
| 1631 | */ |
| 1632 | |
| 1633 | static struct proto_ops msg_ops = { |
| 1634 | .owner = THIS_MODULE, |
| 1635 | .family = AF_TIPC, |
| 1636 | .release = release, |
| 1637 | .bind = bind, |
| 1638 | .connect = connect, |
| 1639 | .socketpair = no_skpair, |
| 1640 | .accept = accept, |
| 1641 | .getname = get_name, |
| 1642 | .poll = poll, |
| 1643 | .ioctl = ioctl, |
| 1644 | .listen = listen, |
| 1645 | .shutdown = shutdown, |
| 1646 | .setsockopt = setsockopt, |
| 1647 | .getsockopt = getsockopt, |
| 1648 | .sendmsg = send_msg, |
| 1649 | .recvmsg = recv_msg, |
| 1650 | .mmap = no_mmap, |
| 1651 | .sendpage = no_sendpage |
| 1652 | }; |
| 1653 | |
| 1654 | static struct proto_ops packet_ops = { |
| 1655 | .owner = THIS_MODULE, |
| 1656 | .family = AF_TIPC, |
| 1657 | .release = release, |
| 1658 | .bind = bind, |
| 1659 | .connect = connect, |
| 1660 | .socketpair = no_skpair, |
| 1661 | .accept = accept, |
| 1662 | .getname = get_name, |
| 1663 | .poll = poll, |
| 1664 | .ioctl = ioctl, |
| 1665 | .listen = listen, |
| 1666 | .shutdown = shutdown, |
| 1667 | .setsockopt = setsockopt, |
| 1668 | .getsockopt = getsockopt, |
| 1669 | .sendmsg = send_packet, |
| 1670 | .recvmsg = recv_msg, |
| 1671 | .mmap = no_mmap, |
| 1672 | .sendpage = no_sendpage |
| 1673 | }; |
| 1674 | |
| 1675 | static struct proto_ops stream_ops = { |
| 1676 | .owner = THIS_MODULE, |
| 1677 | .family = AF_TIPC, |
| 1678 | .release = release, |
| 1679 | .bind = bind, |
| 1680 | .connect = connect, |
| 1681 | .socketpair = no_skpair, |
| 1682 | .accept = accept, |
| 1683 | .getname = get_name, |
| 1684 | .poll = poll, |
| 1685 | .ioctl = ioctl, |
| 1686 | .listen = listen, |
| 1687 | .shutdown = shutdown, |
| 1688 | .setsockopt = setsockopt, |
| 1689 | .getsockopt = getsockopt, |
| 1690 | .sendmsg = send_stream, |
| 1691 | .recvmsg = recv_stream, |
| 1692 | .mmap = no_mmap, |
| 1693 | .sendpage = no_sendpage |
| 1694 | }; |
| 1695 | |
| 1696 | static struct net_proto_family tipc_family_ops = { |
| 1697 | .owner = THIS_MODULE, |
| 1698 | .family = AF_TIPC, |
| 1699 | .create = tipc_create |
| 1700 | }; |
| 1701 | |
| 1702 | static struct proto tipc_proto = { |
| 1703 | .name = "TIPC", |
| 1704 | .owner = THIS_MODULE, |
| 1705 | .obj_size = sizeof(struct tipc_sock) |
| 1706 | }; |
| 1707 | |
| 1708 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 1709 | * tipc_socket_init - initialize TIPC socket interface |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1710 | * |
| 1711 | * Returns 0 on success, errno otherwise |
| 1712 | */ |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 1713 | int tipc_socket_init(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1714 | { |
| 1715 | int res; |
| 1716 | |
| 1717 | res = proto_register(&tipc_proto, 1); |
| 1718 | if (res) { |
Per Liden | d0a14a9 | 2006-01-11 13:52:51 +0100 | [diff] [blame] | 1719 | err("Failed to register TIPC protocol type\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1720 | goto out; |
| 1721 | } |
| 1722 | |
| 1723 | res = sock_register(&tipc_family_ops); |
| 1724 | if (res) { |
Per Liden | d0a14a9 | 2006-01-11 13:52:51 +0100 | [diff] [blame] | 1725 | err("Failed to register TIPC socket type\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1726 | proto_unregister(&tipc_proto); |
| 1727 | goto out; |
| 1728 | } |
| 1729 | |
| 1730 | sockets_enabled = 1; |
| 1731 | out: |
| 1732 | return res; |
| 1733 | } |
| 1734 | |
| 1735 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 1736 | * tipc_socket_stop - stop TIPC socket interface |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1737 | */ |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 1738 | void tipc_socket_stop(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1739 | { |
| 1740 | if (!sockets_enabled) |
| 1741 | return; |
| 1742 | |
| 1743 | sockets_enabled = 0; |
| 1744 | sock_unregister(tipc_family_ops.family); |
| 1745 | proto_unregister(&tipc_proto); |
| 1746 | } |
| 1747 | |