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