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