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