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