blob: 38f48795b40eded00a7a68053dc73eff3b751eb1 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Allan Stephens5eee6a62007-06-10 17:24:55 -07004 * Copyright (c) 2001-2007, Ericsson AB
5 * Copyright (c) 2004-2007, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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 Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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 Lidenb97bf3f2006-01-02 19:04:38 +010034 * 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 Lidenb97bf3f2006-01-02 19:04:38 +010045#include <linux/fcntl.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010046#include <asm/string.h>
47#include <asm/atomic.h>
48#include <net/sock.h>
49
50#include <linux/tipc.h>
Per Lidenea714cc2006-01-11 12:28:47 +010051#include <linux/tipc_config.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010052#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 Stephens3654ea02008-04-13 21:35:11 -070060#define OVERLOAD_LIMIT_BASE 5000
61#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010062
63struct tipc_sock {
64 struct sock sk;
65 struct tipc_port *p;
Per Lidenb97bf3f2006-01-02 19:04:38 +010066};
67
Allan Stephens0c3141e2008-04-15 00:22:02 -070068#define tipc_sk(sk) ((struct tipc_sock *)(sk))
69#define tipc_sk_port(sk) ((struct tipc_port *)(tipc_sk(sk)->p))
Per Lidenb97bf3f2006-01-02 19:04:38 +010070
Allan Stephens0c3141e2008-04-15 00:22:02 -070071static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +010072static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
73static void wakeupdispatch(struct tipc_port *tport);
74
Florian Westphalbca65ea2008-02-07 18:18:01 -080075static const struct proto_ops packet_ops;
76static const struct proto_ops stream_ops;
77static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010078
79static struct proto tipc_proto;
80
81static int sockets_enabled = 0;
82
83static atomic_t tipc_queue_size = ATOMIC_INIT(0);
84
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090085/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070086 * 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 Lidenb97bf3f2006-01-02 19:04:38 +0100128 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129
130/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700131 * advance_rx_queue - discard first buffer in socket receive queue
132 *
133 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100134 */
135
Allan Stephens0c3141e2008-04-15 00:22:02 -0700136static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100137{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700138 buf_discard(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139 atomic_dec(&tipc_queue_size);
140}
141
142/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700143 * discard_rx_queue - discard all buffers in socket receive queue
144 *
145 * Caller must hold socket lock
146 */
147
148static 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
164static 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 Lidenb97bf3f2006-01-02 19:04:38 +0100175 * tipc_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700176 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 * @sock: pre-allocated socket structure
178 * @protocol: protocol indicator (must be 0)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900179 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700180 * This routine creates additional data structures used by the TIPC socket,
181 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182 *
183 * Returns 0 on success, errno otherwise
184 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700185
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700186static int tipc_create(struct net *net, struct socket *sock, int protocol)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100187{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700188 const struct proto_ops *ops;
189 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100190 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700191 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700192 u32 portref;
193
194 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100195
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700196 if (net != &init_net)
197 return -EAFNOSUPPORT;
198
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199 if (unlikely(protocol != 0))
200 return -EPROTONOSUPPORT;
201
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202 switch (sock->type) {
203 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700204 ops = &stream_ops;
205 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206 break;
207 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700208 ops = &packet_ops;
209 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210 break;
211 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700213 ops = &msg_ops;
214 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215 break;
Allan Stephens49978652006-06-25 23:47:18 -0700216 default:
Allan Stephens49978652006-06-25 23:47:18 -0700217 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218 }
219
Allan Stephens0c3141e2008-04-15 00:22:02 -0700220 /* Allocate socket's protocol area */
221
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700222 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700223 if (sk == NULL)
224 return -ENOMEM;
225
226 /* Allocate TIPC port for socket to use */
227
228 portref = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700229 TIPC_LOW_IMPORTANCE, &tp_ptr);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700230 if (unlikely(portref == 0)) {
231 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 return -ENOMEM;
233 }
234
Allan Stephens0c3141e2008-04-15 00:22:02 -0700235 /* Finish initializing socket data structures */
236
237 sock->ops = ops;
238 sock->state = state;
239
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240 sock_init_data(sock, sk);
Allan Stephens3654ea02008-04-13 21:35:11 -0700241 sk->sk_rcvtimeo = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700242 sk->sk_backlog_rcv = backlog_rcv;
243 tipc_sk(sk)->p = tipc_get_port(portref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700245 spin_unlock_bh(tp_ptr->lock);
246
Allan Stephens0c3141e2008-04-15 00:22:02 -0700247 if (sock->state == SS_READY) {
248 tipc_set_portunreturnable(portref, 1);
249 if (sock->type == SOCK_DGRAM)
250 tipc_set_portunreliable(portref, 1);
251 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252
253 atomic_inc(&tipc_user_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254 return 0;
255}
256
257/**
258 * release - destroy a TIPC socket
259 * @sock: socket to destroy
260 *
261 * This routine cleans up any messages that are still queued on the socket.
262 * For DGRAM and RDM socket types, all queued messages are rejected.
263 * For SEQPACKET and STREAM socket types, the first message is rejected
264 * and any others are discarded. (If the first message on a STREAM socket
265 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900266 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100267 * NOTE: Rejected messages are not necessarily returned to the sender! They
268 * are returned or discarded according to the "destination droppable" setting
269 * specified for the message by the sender.
270 *
271 * Returns 0 on success, errno otherwise
272 */
273
274static int release(struct socket *sock)
275{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700277 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700279 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280
Allan Stephens0c3141e2008-04-15 00:22:02 -0700281 /*
282 * Exit if socket isn't fully initialized (occurs when a failed accept()
283 * releases a pre-allocated child socket that was never used)
284 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900285
Allan Stephens0c3141e2008-04-15 00:22:02 -0700286 if (sk == NULL)
287 return 0;
288
289 tport = tipc_sk_port(sk);
290 lock_sock(sk);
291
292 /*
293 * Reject all unreceived messages, except on an active connection
294 * (which disconnects locally & sends a 'FIN+' to peer)
295 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296
297 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700298 buf = __skb_dequeue(&sk->sk_receive_queue);
299 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 break;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700301 atomic_dec(&tipc_queue_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
303 buf_discard(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700304 else {
305 if ((sock->state == SS_CONNECTING) ||
306 (sock->state == SS_CONNECTED)) {
307 sock->state = SS_DISCONNECTING;
308 tipc_disconnect(tport->ref);
309 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700311 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 }
313
Allan Stephens0c3141e2008-04-15 00:22:02 -0700314 /*
315 * Delete TIPC port; this ensures no more messages are queued
316 * (also disconnects an active connection & sends a 'FIN-' to peer)
317 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318
Allan Stephens0c3141e2008-04-15 00:22:02 -0700319 res = tipc_deleteport(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320
Allan Stephens0c3141e2008-04-15 00:22:02 -0700321 /* Discard any remaining (connection-based) messages in receive queue */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322
Allan Stephens0c3141e2008-04-15 00:22:02 -0700323 discard_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324
Allan Stephens0c3141e2008-04-15 00:22:02 -0700325 /* Reject any messages that accumulated in backlog queue */
326
327 sock->state = SS_DISCONNECTING;
328 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329
330 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700331 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900333 atomic_dec(&tipc_user_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334 return res;
335}
336
337/**
338 * bind - associate or disassocate TIPC name(s) with a socket
339 * @sock: socket structure
340 * @uaddr: socket address describing name(s) and desired operation
341 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900342 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 * Name and name sequence binding is indicated using a positive scope value;
344 * a negative scope value unbinds the specified name. Specifying no name
345 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900346 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700348 *
349 * NOTE: This routine doesn't need to take the socket lock since it doesn't
350 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 */
352
353static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
354{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700356 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357
Allan Stephens0c3141e2008-04-15 00:22:02 -0700358 if (unlikely(!uaddr_len))
359 return tipc_withdraw(portref, 0, NULL);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900360
Allan Stephens0c3141e2008-04-15 00:22:02 -0700361 if (uaddr_len < sizeof(struct sockaddr_tipc))
362 return -EINVAL;
363 if (addr->family != AF_TIPC)
364 return -EAFNOSUPPORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 if (addr->addrtype == TIPC_ADDR_NAME)
367 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700368 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
369 return -EAFNOSUPPORT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900370
Allan Stephens0c3141e2008-04-15 00:22:02 -0700371 return (addr->scope > 0) ?
372 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
373 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374}
375
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900376/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377 * get_name - get port ID of socket or peer socket
378 * @sock: socket structure
379 * @uaddr: area for returned socket address
380 * @uaddr_len: area for returned length of socket address
381 * @peer: 0 to obtain socket name, 1 to obtain peer socket name
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900382 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700384 *
385 * NOTE: This routine doesn't need to take the socket lock since it doesn't
386 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387 */
388
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900389static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100390 int *uaddr_len, int peer)
391{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700393 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394 u32 res;
395
Allan Stephens0c3141e2008-04-15 00:22:02 -0700396 if (peer) {
397 res = tipc_peer(portref, &addr->addr.id);
398 if (res)
399 return res;
400 } else {
401 tipc_ownidentity(portref, &addr->addr.id);
402 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403
404 *uaddr_len = sizeof(*addr);
405 addr->addrtype = TIPC_ADDR_ID;
406 addr->family = AF_TIPC;
407 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408 addr->addr.name.domain = 0;
409
Allan Stephens0c3141e2008-04-15 00:22:02 -0700410 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411}
412
413/**
414 * poll - read and possibly block on pollmask
415 * @file: file structure associated with the socket
416 * @sock: socket for which to calculate the poll bits
417 * @wait: ???
418 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700419 * Returns pollmask value
420 *
421 * COMMENTARY:
422 * It appears that the usual socket locking mechanisms are not useful here
423 * since the pollmask info is potentially out-of-date the moment this routine
424 * exits. TCP and other protocols seem to rely on higher level poll routines
425 * to handle any preventable race conditions, so TIPC will do the same ...
426 *
427 * TIPC sets the returned events as follows:
428 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
429 * or if a connection-oriented socket is does not have an active connection
430 * (i.e. a read operation will not block).
431 * b) POLLOUT is set except when a socket's connection has been terminated
432 * (i.e. a write operation will not block).
433 * c) POLLHUP is set when a socket's connection has been terminated.
434 *
435 * IMPORTANT: The fact that a read or write operation will not block does NOT
436 * imply that the operation will succeed!
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437 */
438
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900439static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440 poll_table *wait)
441{
Allan Stephens9b674e82008-03-26 16:48:21 -0700442 struct sock *sk = sock->sk;
443 u32 mask;
444
445 poll_wait(file, sk->sk_sleep, wait);
446
447 if (!skb_queue_empty(&sk->sk_receive_queue) ||
448 (sock->state == SS_UNCONNECTED) ||
449 (sock->state == SS_DISCONNECTING))
450 mask = (POLLRDNORM | POLLIN);
451 else
452 mask = 0;
453
454 if (sock->state == SS_DISCONNECTING)
455 mask |= POLLHUP;
456 else
457 mask |= POLLOUT;
458
459 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460}
461
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900462/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463 * dest_name_check - verify user is permitted to send to specified port name
464 * @dest: destination address
465 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900466 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467 * Prevents restricted configuration commands from being issued by
468 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900469 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470 * Returns 0 if permission is granted, otherwise errno
471 */
472
Sam Ravnborg05790c62006-03-20 22:37:04 -0800473static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474{
475 struct tipc_cfg_msg_hdr hdr;
476
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900477 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
478 return 0;
479 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
480 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900481 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
482 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900484 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700486 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900488
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489 return 0;
490}
491
492/**
493 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700494 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495 * @sock: socket structure
496 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700497 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900498 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900500 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
502 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900503 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 * Returns the number of bytes sent on success, or errno otherwise
505 */
506
507static int send_msg(struct kiocb *iocb, struct socket *sock,
508 struct msghdr *m, size_t total_len)
509{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700510 struct sock *sk = sock->sk;
511 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900512 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 int needs_conn;
514 int res = -EINVAL;
515
516 if (unlikely(!dest))
517 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700518 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
519 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520 return -EINVAL;
521
Allan Stephens0c3141e2008-04-15 00:22:02 -0700522 if (iocb)
523 lock_sock(sk);
524
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 needs_conn = (sock->state != SS_READY);
526 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700527 if (sock->state == SS_LISTENING) {
528 res = -EPIPE;
529 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700530 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700531 if (sock->state != SS_UNCONNECTED) {
532 res = -EISCONN;
533 goto exit;
534 }
535 if ((tport->published) ||
536 ((sock->type == SOCK_STREAM) && (total_len != 0))) {
537 res = -EOPNOTSUPP;
538 goto exit;
539 }
540 if (dest->addrtype == TIPC_ADDR_NAME) {
541 tport->conn_type = dest->addr.name.name.type;
542 tport->conn_instance = dest->addr.name.name.instance;
543 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544
545 /* Abort any pending connection attempts (very unlikely) */
546
Allan Stephens0c3141e2008-04-15 00:22:02 -0700547 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548 }
549
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900550 do {
551 if (dest->addrtype == TIPC_ADDR_NAME) {
552 if ((res = dest_name_check(dest, m)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700553 break;
554 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900555 &dest->addr.name.name,
556 dest->addr.name.domain,
557 m->msg_iovlen,
558 m->msg_iov);
559 }
560 else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700561 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900562 &dest->addr.id,
563 m->msg_iovlen,
564 m->msg_iov);
565 }
566 else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567 if (needs_conn) {
568 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700569 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900571 if ((res = dest_name_check(dest, m)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700572 break;
573 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900574 &dest->addr.nameseq,
575 0,
576 m->msg_iovlen,
577 m->msg_iov);
578 }
579 if (likely(res != -ELINKCONG)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700580 if (needs_conn && (res >= 0)) {
581 sock->state = SS_CONNECTING;
582 }
583 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900584 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 if (m->msg_flags & MSG_DONTWAIT) {
586 res = -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700587 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700589 release_sock(sk);
590 res = wait_event_interruptible(*sk->sk_sleep,
591 !tport->congested);
592 lock_sock(sk);
593 if (res)
594 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900595 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700596
597exit:
598 if (iocb)
599 release_sock(sk);
600 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601}
602
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900603/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700605 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 * @sock: socket structure
607 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700608 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900609 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900611 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 * Returns the number of bytes sent on success, or errno otherwise
613 */
614
615static int send_packet(struct kiocb *iocb, struct socket *sock,
616 struct msghdr *m, size_t total_len)
617{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700618 struct sock *sk = sock->sk;
619 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900620 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621 int res;
622
623 /* Handle implied connection establishment */
624
625 if (unlikely(dest))
626 return send_msg(iocb, sock, m, total_len);
627
Allan Stephens0c3141e2008-04-15 00:22:02 -0700628 if (iocb)
629 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900631 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700632 if (unlikely(sock->state != SS_CONNECTED)) {
633 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900634 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700635 else
636 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700637 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700638 }
639
Allan Stephens0c3141e2008-04-15 00:22:02 -0700640 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900641 if (likely(res != -ELINKCONG)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700642 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900643 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 if (m->msg_flags & MSG_DONTWAIT) {
645 res = -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700646 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700648 release_sock(sk);
649 res = wait_event_interruptible(*sk->sk_sleep,
650 (!tport->congested || !tport->connected));
651 lock_sock(sk);
652 if (res)
653 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900654 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700655
656 if (iocb)
657 release_sock(sk);
658 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659}
660
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900661/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100662 * send_stream - send stream-oriented data
663 * @iocb: (unused)
664 * @sock: socket structure
665 * @m: data to send
666 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900667 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900669 *
670 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700671 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 */
673
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674static int send_stream(struct kiocb *iocb, struct socket *sock,
675 struct msghdr *m, size_t total_len)
676{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700677 struct sock *sk = sock->sk;
678 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679 struct msghdr my_msg;
680 struct iovec my_iov;
681 struct iovec *curr_iov;
682 int curr_iovlen;
683 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700684 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685 int curr_left;
686 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700687 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100688 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900689
Allan Stephens0c3141e2008-04-15 00:22:02 -0700690 lock_sock(sk);
691
Allan Stephens05646c92007-06-10 17:25:24 -0700692 /* Handle special cases where there is no connection */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900694 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700695 if (sock->state == SS_UNCONNECTED) {
696 res = send_packet(NULL, sock, m, total_len);
697 goto exit;
698 } else if (sock->state == SS_DISCONNECTING) {
699 res = -EPIPE;
700 goto exit;
701 } else {
702 res = -ENOTCONN;
703 goto exit;
704 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900705 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706
Allan Stephens0c3141e2008-04-15 00:22:02 -0700707 if (unlikely(m->msg_name)) {
708 res = -EISCONN;
709 goto exit;
710 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700711
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900712 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100713 * Send each iovec entry using one or more messages
714 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900715 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716 * (i.e. one large iovec entry), but could be improved to pass sets
717 * of small iovec entries into send_packet().
718 */
719
Allan Stephens1303e8f2006-06-25 23:46:50 -0700720 curr_iov = m->msg_iov;
721 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100722 my_msg.msg_iov = &my_iov;
723 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700724 my_msg.msg_flags = m->msg_flags;
725 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700726 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100727
Allan Stephens05646c92007-06-10 17:25:24 -0700728 hdr_size = msg_hdr_sz(&tport->phdr);
729
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730 while (curr_iovlen--) {
731 curr_start = curr_iov->iov_base;
732 curr_left = curr_iov->iov_len;
733
734 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700735 bytes_to_send = tport->max_pkt - hdr_size;
736 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
737 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
738 if (curr_left < bytes_to_send)
739 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100740 my_iov.iov_base = curr_start;
741 my_iov.iov_len = bytes_to_send;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700742 if ((res = send_packet(NULL, sock, &my_msg, 0)) < 0) {
743 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700744 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700745 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700746 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747 curr_left -= bytes_to_send;
748 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700749 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750 }
751
752 curr_iov++;
753 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700754 res = bytes_sent;
755exit:
756 release_sock(sk);
757 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100758}
759
760/**
761 * auto_connect - complete connection setup to a remote port
762 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900764 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100765 * Returns 0 on success, errno otherwise
766 */
767
Allan Stephens0c3141e2008-04-15 00:22:02 -0700768static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100769{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700770 struct tipc_port *tport = tipc_sk_port(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771 struct tipc_portid peer;
772
773 if (msg_errcode(msg)) {
774 sock->state = SS_DISCONNECTING;
775 return -ECONNREFUSED;
776 }
777
778 peer.ref = msg_origport(msg);
779 peer.node = msg_orignode(msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700780 tipc_connect2port(tport->ref, &peer);
781 tipc_set_portimportance(tport->ref, msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782 sock->state = SS_CONNECTED;
783 return 0;
784}
785
786/**
787 * set_orig_addr - capture sender's address for received message
788 * @m: descriptor for message info
789 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900790 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791 * Note: Address is not captured if not requested by receiver.
792 */
793
Sam Ravnborg05790c62006-03-20 22:37:04 -0800794static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100795{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900796 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100797
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900798 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799 addr->family = AF_TIPC;
800 addr->addrtype = TIPC_ADDR_ID;
801 addr->addr.id.ref = msg_origport(msg);
802 addr->addr.id.node = msg_orignode(msg);
803 addr->addr.name.domain = 0; /* could leave uninitialized */
804 addr->scope = 0; /* could leave uninitialized */
805 m->msg_namelen = sizeof(struct sockaddr_tipc);
806 }
807}
808
809/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900810 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100811 * @m: descriptor for message info
812 * @msg: received message header
813 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900814 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900816 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100817 * Returns 0 if successful, otherwise errno
818 */
819
Sam Ravnborg05790c62006-03-20 22:37:04 -0800820static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100821 struct tipc_port *tport)
822{
823 u32 anc_data[3];
824 u32 err;
825 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700826 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100827 int res;
828
829 if (likely(m->msg_controllen == 0))
830 return 0;
831
832 /* Optionally capture errored message object(s) */
833
834 err = msg ? msg_errcode(msg) : 0;
835 if (unlikely(err)) {
836 anc_data[0] = err;
837 anc_data[1] = msg_data_sz(msg);
Allan Stephens4b087b22006-06-25 23:47:44 -0700838 if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100839 return res;
840 if (anc_data[1] &&
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900841 (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
Per Lidenb97bf3f2006-01-02 19:04:38 +0100842 msg_data(msg))))
843 return res;
844 }
845
846 /* Optionally capture message destination object */
847
848 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
849 switch (dest_type) {
850 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700851 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100852 anc_data[0] = msg_nametype(msg);
853 anc_data[1] = msg_namelower(msg);
854 anc_data[2] = msg_namelower(msg);
855 break;
856 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700857 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100858 anc_data[0] = msg_nametype(msg);
859 anc_data[1] = msg_namelower(msg);
860 anc_data[2] = msg_nameupper(msg);
861 break;
862 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700863 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100864 anc_data[0] = tport->conn_type;
865 anc_data[1] = tport->conn_instance;
866 anc_data[2] = tport->conn_instance;
867 break;
868 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700869 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870 }
Allan Stephens3546c752006-06-25 23:45:24 -0700871 if (has_name &&
Allan Stephens4b087b22006-06-25 23:47:44 -0700872 (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873 return res;
874
875 return 0;
876}
877
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900878/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879 * recv_msg - receive packet-oriented message
880 * @iocb: (unused)
881 * @m: descriptor for message info
882 * @buf_len: total size of user buffer area
883 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900884 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
886 * If the complete message doesn't fit in user area, truncate it.
887 *
888 * Returns size of returned message data, errno otherwise
889 */
890
891static int recv_msg(struct kiocb *iocb, struct socket *sock,
892 struct msghdr *m, size_t buf_len, int flags)
893{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700894 struct sock *sk = sock->sk;
895 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100896 struct sk_buff *buf;
897 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898 unsigned int sz;
899 u32 err;
900 int res;
901
Allan Stephens0c3141e2008-04-15 00:22:02 -0700902 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903
904 if (m->msg_iovlen != 1)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700905 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906
907 if (unlikely(!buf_len))
908 return -EINVAL;
909
Allan Stephens0c3141e2008-04-15 00:22:02 -0700910 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100911
Allan Stephens0c3141e2008-04-15 00:22:02 -0700912 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913 res = -ENOTCONN;
914 goto exit;
915 }
916
Allan Stephens0c3141e2008-04-15 00:22:02 -0700917restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100918
Allan Stephens0c3141e2008-04-15 00:22:02 -0700919 /* Look for a message in receive queue; wait if necessary */
920
921 while (skb_queue_empty(&sk->sk_receive_queue)) {
922 if (sock->state == SS_DISCONNECTING) {
923 res = -ENOTCONN;
924 goto exit;
925 }
926 if (flags & MSG_DONTWAIT) {
927 res = -EWOULDBLOCK;
928 goto exit;
929 }
930 release_sock(sk);
931 res = wait_event_interruptible(*sk->sk_sleep,
932 (!skb_queue_empty(&sk->sk_receive_queue) ||
933 (sock->state == SS_DISCONNECTING)));
934 lock_sock(sk);
935 if (res)
936 goto exit;
937 }
938
939 /* Look at first message in receive queue */
940
941 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942 msg = buf_msg(buf);
943 sz = msg_data_sz(msg);
944 err = msg_errcode(msg);
945
946 /* Complete connection setup for an implied connect */
947
948 if (unlikely(sock->state == SS_CONNECTING)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700949 res = auto_connect(sock, msg);
950 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951 goto exit;
952 }
953
954 /* Discard an empty non-errored message & try again */
955
956 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700957 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958 goto restart;
959 }
960
961 /* Capture sender's address (optional) */
962
963 set_orig_addr(m, msg);
964
965 /* Capture ancillary data (optional) */
966
Allan Stephens0c3141e2008-04-15 00:22:02 -0700967 res = anc_data_recv(m, msg, tport);
968 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969 goto exit;
970
971 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900972
Per Lidenb97bf3f2006-01-02 19:04:38 +0100973 if (!err) {
974 if (unlikely(buf_len < sz)) {
975 sz = buf_len;
976 m->msg_flags |= MSG_TRUNC;
977 }
978 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
979 sz))) {
980 res = -EFAULT;
981 goto exit;
982 }
983 res = sz;
984 } else {
985 if ((sock->state == SS_READY) ||
986 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
987 res = 0;
988 else
989 res = -ECONNRESET;
990 }
991
992 /* Consume received message (optional) */
993
994 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens990098062008-04-15 00:06:12 -0700995 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -0700996 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
997 tipc_acknowledge(tport->ref, tport->conn_unacked);
998 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900999 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001000exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001001 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 return res;
1003}
1004
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001005/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006 * recv_stream - receive stream-oriented data
1007 * @iocb: (unused)
1008 * @m: descriptor for message info
1009 * @buf_len: total size of user buffer area
1010 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001011 *
1012 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 * will optionally wait for more; never truncates data.
1014 *
1015 * Returns size of returned message data, errno otherwise
1016 */
1017
1018static int recv_stream(struct kiocb *iocb, struct socket *sock,
1019 struct msghdr *m, size_t buf_len, int flags)
1020{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001021 struct sock *sk = sock->sk;
1022 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023 struct sk_buff *buf;
1024 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 unsigned int sz;
1026 int sz_to_copy;
1027 int sz_copied = 0;
1028 int needed;
Al Viro28c4dad2006-10-10 22:45:57 +01001029 char __user *crs = m->msg_iov->iov_base;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030 unsigned char *buf_crs;
1031 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001032 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001033
1034 /* Catch invalid receive attempts */
1035
Allan Stephens0c3141e2008-04-15 00:22:02 -07001036 if (m->msg_iovlen != 1)
1037 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
1038
Per Lidenb97bf3f2006-01-02 19:04:38 +01001039 if (unlikely(!buf_len))
1040 return -EINVAL;
1041
Allan Stephens0c3141e2008-04-15 00:22:02 -07001042 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043
Allan Stephens0c3141e2008-04-15 00:22:02 -07001044 if (unlikely((sock->state == SS_UNCONNECTED) ||
1045 (sock->state == SS_CONNECTING))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046 res = -ENOTCONN;
1047 goto exit;
1048 }
1049
Allan Stephens0c3141e2008-04-15 00:22:02 -07001050restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001051
Allan Stephens0c3141e2008-04-15 00:22:02 -07001052 /* Look for a message in receive queue; wait if necessary */
1053
1054 while (skb_queue_empty(&sk->sk_receive_queue)) {
1055 if (sock->state == SS_DISCONNECTING) {
1056 res = -ENOTCONN;
1057 goto exit;
1058 }
1059 if (flags & MSG_DONTWAIT) {
1060 res = -EWOULDBLOCK;
1061 goto exit;
1062 }
1063 release_sock(sk);
1064 res = wait_event_interruptible(*sk->sk_sleep,
1065 (!skb_queue_empty(&sk->sk_receive_queue) ||
1066 (sock->state == SS_DISCONNECTING)));
1067 lock_sock(sk);
1068 if (res)
1069 goto exit;
1070 }
1071
1072 /* Look at first message in receive queue */
1073
1074 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001075 msg = buf_msg(buf);
1076 sz = msg_data_sz(msg);
1077 err = msg_errcode(msg);
1078
1079 /* Discard an empty non-errored message & try again */
1080
1081 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001082 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083 goto restart;
1084 }
1085
1086 /* Optionally capture sender's address & ancillary data of first msg */
1087
1088 if (sz_copied == 0) {
1089 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001090 res = anc_data_recv(m, msg, tport);
1091 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092 goto exit;
1093 }
1094
1095 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001096
Per Lidenb97bf3f2006-01-02 19:04:38 +01001097 if (!err) {
1098 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
Allan Stephens7a8036c2008-04-15 00:15:15 -07001099 sz = (unsigned char *)msg + msg_size(msg) - buf_crs;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001100
1101 needed = (buf_len - sz_copied);
1102 sz_to_copy = (sz <= needed) ? sz : needed;
1103 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1104 res = -EFAULT;
1105 goto exit;
1106 }
1107 sz_copied += sz_to_copy;
1108
1109 if (sz_to_copy < sz) {
1110 if (!(flags & MSG_PEEK))
1111 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1112 goto exit;
1113 }
1114
1115 crs += sz_to_copy;
1116 } else {
1117 if (sz_copied != 0)
1118 goto exit; /* can't add error msg to valid data */
1119
1120 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1121 res = 0;
1122 else
1123 res = -ECONNRESET;
1124 }
1125
1126 /* Consume received message (optional) */
1127
1128 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001129 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1130 tipc_acknowledge(tport->ref, tport->conn_unacked);
1131 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001132 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133
1134 /* Loop around if more data is required */
1135
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001136 if ((sz_copied < buf_len) /* didn't get all requested data */
Allan Stephensa198d3a2008-04-15 00:07:15 -07001137 && (!skb_queue_empty(&sock->sk->sk_receive_queue) ||
1138 (flags & MSG_WAITALL))
1139 /* ... and more is ready or required */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140 && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
1141 && (!err) /* ... and haven't reached a FIN */
1142 )
1143 goto restart;
1144
1145exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001146 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001147 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148}
1149
1150/**
Allan Stephens1819b832008-04-15 00:15:50 -07001151 * rx_queue_full - determine if receive queue can accept another message
1152 * @msg: message to be added to queue
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153 * @queue_size: current size of queue
1154 * @base: nominal maximum size of queue
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001155 *
Allan Stephens1819b832008-04-15 00:15:50 -07001156 * Returns 1 if queue is unable to accept message, 0 otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157 */
1158
Allan Stephens1819b832008-04-15 00:15:50 -07001159static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160{
1161 u32 threshold;
1162 u32 imp = msg_importance(msg);
1163
1164 if (imp == TIPC_LOW_IMPORTANCE)
1165 threshold = base;
1166 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1167 threshold = base * 2;
1168 else if (imp == TIPC_HIGH_IMPORTANCE)
1169 threshold = base * 100;
1170 else
1171 return 0;
1172
1173 if (msg_connected(msg))
1174 threshold *= 4;
1175
Allan Stephens1819b832008-04-15 00:15:50 -07001176 return (queue_size >= threshold);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177}
1178
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001179/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001180 * filter_rcv - validate incoming message
1181 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001183 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001184 * Enqueues message on receive queue if acceptable; optionally handles
1185 * disconnect indication for a connected socket.
1186 *
1187 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001188 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1190 */
1191
Allan Stephens0c3141e2008-04-15 00:22:02 -07001192static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001193{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001194 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 struct tipc_msg *msg = buf_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 u32 recv_q_len;
1197
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 /* Reject message if it is wrong sort of message for socket */
1199
1200 /*
1201 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1202 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1203 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1204 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001205
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206 if (sock->state == SS_READY) {
1207 if (msg_connected(msg)) {
1208 msg_dbg(msg, "dispatch filter 1\n");
1209 return TIPC_ERR_NO_PORT;
1210 }
1211 } else {
1212 if (msg_mcast(msg)) {
1213 msg_dbg(msg, "dispatch filter 2\n");
1214 return TIPC_ERR_NO_PORT;
1215 }
1216 if (sock->state == SS_CONNECTED) {
1217 if (!msg_connected(msg)) {
1218 msg_dbg(msg, "dispatch filter 3\n");
1219 return TIPC_ERR_NO_PORT;
1220 }
1221 }
1222 else if (sock->state == SS_CONNECTING) {
1223 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1224 msg_dbg(msg, "dispatch filter 4\n");
1225 return TIPC_ERR_NO_PORT;
1226 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001227 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 else if (sock->state == SS_LISTENING) {
1229 if (msg_connected(msg) || msg_errcode(msg)) {
1230 msg_dbg(msg, "dispatch filter 5\n");
1231 return TIPC_ERR_NO_PORT;
1232 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001233 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 else if (sock->state == SS_DISCONNECTING) {
1235 msg_dbg(msg, "dispatch filter 6\n");
1236 return TIPC_ERR_NO_PORT;
1237 }
1238 else /* (sock->state == SS_UNCONNECTED) */ {
1239 if (msg_connected(msg) || msg_errcode(msg)) {
1240 msg_dbg(msg, "dispatch filter 7\n");
1241 return TIPC_ERR_NO_PORT;
1242 }
1243 }
1244 }
1245
1246 /* Reject message if there isn't room to queue it */
1247
Allan Stephens1819b832008-04-15 00:15:50 -07001248 recv_q_len = (u32)atomic_read(&tipc_queue_size);
1249 if (unlikely(recv_q_len >= OVERLOAD_LIMIT_BASE)) {
1250 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001251 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001252 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001253 recv_q_len = skb_queue_len(&sk->sk_receive_queue);
Allan Stephens1819b832008-04-15 00:15:50 -07001254 if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
1255 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001256 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001257 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001258
Allan Stephens0c3141e2008-04-15 00:22:02 -07001259 /* Enqueue message (finally!) */
1260
1261 msg_dbg(msg, "<DISP<: ");
1262 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1263 atomic_inc(&tipc_queue_size);
1264 __skb_queue_tail(&sk->sk_receive_queue, buf);
1265
Per Lidenb97bf3f2006-01-02 19:04:38 +01001266 /* Initiate connection termination for an incoming 'FIN' */
1267
1268 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1269 sock->state = SS_DISCONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001270 tipc_disconnect_port(tipc_sk_port(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001271 }
1272
Allan Stephens0c3141e2008-04-15 00:22:02 -07001273 if (waitqueue_active(sk->sk_sleep))
1274 wake_up_interruptible(sk->sk_sleep);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001275 return TIPC_OK;
1276}
1277
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001278/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001279 * backlog_rcv - handle incoming message from backlog queue
1280 * @sk: socket
1281 * @buf: message
1282 *
1283 * Caller must hold socket lock, but not port lock.
1284 *
1285 * Returns 0
1286 */
1287
1288static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1289{
1290 u32 res;
1291
1292 res = filter_rcv(sk, buf);
1293 if (res)
1294 tipc_reject_msg(buf, res);
1295 return 0;
1296}
1297
1298/**
1299 * dispatch - handle incoming message
1300 * @tport: TIPC port that received message
1301 * @buf: message
1302 *
1303 * Called with port lock already taken.
1304 *
1305 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1306 */
1307
1308static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1309{
1310 struct sock *sk = (struct sock *)tport->usr_handle;
1311 u32 res;
1312
1313 /*
1314 * Process message if socket is unlocked; otherwise add to backlog queue
1315 *
1316 * This code is based on sk_receive_skb(), but must be distinct from it
1317 * since a TIPC-specific filter/reject mechanism is utilized
1318 */
1319
1320 bh_lock_sock(sk);
1321 if (!sock_owned_by_user(sk)) {
1322 res = filter_rcv(sk, buf);
1323 } else {
1324 sk_add_backlog(sk, buf);
1325 res = TIPC_OK;
1326 }
1327 bh_unlock_sock(sk);
1328
1329 return res;
1330}
1331
1332/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333 * wakeupdispatch - wake up port after congestion
1334 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001335 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001336 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337 */
1338
1339static void wakeupdispatch(struct tipc_port *tport)
1340{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001341 struct sock *sk = (struct sock *)tport->usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342
Allan Stephens0c3141e2008-04-15 00:22:02 -07001343 if (waitqueue_active(sk->sk_sleep))
1344 wake_up_interruptible(sk->sk_sleep);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345}
1346
1347/**
1348 * connect - establish a connection to another TIPC port
1349 * @sock: socket structure
1350 * @dest: socket address for destination port
1351 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001352 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353 *
1354 * Returns 0 on success, errno otherwise
1355 */
1356
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001357static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358 int flags)
1359{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001360 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001361 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1362 struct msghdr m = {NULL,};
1363 struct sk_buff *buf;
1364 struct tipc_msg *msg;
1365 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366
Allan Stephens0c3141e2008-04-15 00:22:02 -07001367 lock_sock(sk);
1368
Allan Stephensb89741a2008-04-15 00:20:37 -07001369 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370
Allan Stephens0c3141e2008-04-15 00:22:02 -07001371 if (sock->state == SS_READY) {
1372 res = -EOPNOTSUPP;
1373 goto exit;
1374 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375
Allan Stephensb89741a2008-04-15 00:20:37 -07001376 /* For now, TIPC does not support the non-blocking form of connect() */
Allan Stephens4934c692008-04-15 00:16:19 -07001377
Allan Stephens0c3141e2008-04-15 00:22:02 -07001378 if (flags & O_NONBLOCK) {
1379 res = -EWOULDBLOCK;
1380 goto exit;
1381 }
Allan Stephens4934c692008-04-15 00:16:19 -07001382
Allan Stephensb89741a2008-04-15 00:20:37 -07001383 /* Issue Posix-compliant error code if socket is in the wrong state */
Allan Stephens51f9cc12006-06-25 23:49:06 -07001384
Allan Stephens0c3141e2008-04-15 00:22:02 -07001385 if (sock->state == SS_LISTENING) {
1386 res = -EOPNOTSUPP;
1387 goto exit;
1388 }
1389 if (sock->state == SS_CONNECTING) {
1390 res = -EALREADY;
1391 goto exit;
1392 }
1393 if (sock->state != SS_UNCONNECTED) {
1394 res = -EISCONN;
1395 goto exit;
1396 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001397
Allan Stephensb89741a2008-04-15 00:20:37 -07001398 /*
1399 * Reject connection attempt using multicast address
1400 *
1401 * Note: send_msg() validates the rest of the address fields,
1402 * so there's no need to do it here
1403 */
Allan Stephens51f9cc12006-06-25 23:49:06 -07001404
Allan Stephens0c3141e2008-04-15 00:22:02 -07001405 if (dst->addrtype == TIPC_ADDR_MCAST) {
1406 res = -EINVAL;
1407 goto exit;
1408 }
1409
1410 /* Reject any messages already in receive queue (very unlikely) */
1411
1412 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001413
Allan Stephensb89741a2008-04-15 00:20:37 -07001414 /* Send a 'SYN-' to destination */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001415
Allan Stephensb89741a2008-04-15 00:20:37 -07001416 m.msg_name = dest;
1417 m.msg_namelen = destlen;
1418 res = send_msg(NULL, sock, &m, 0);
1419 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001420 goto exit;
Allan Stephensb89741a2008-04-15 00:20:37 -07001421 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422
Allan Stephens0c3141e2008-04-15 00:22:02 -07001423 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001424
Allan Stephens0c3141e2008-04-15 00:22:02 -07001425 release_sock(sk);
1426 res = wait_event_interruptible_timeout(*sk->sk_sleep,
1427 (!skb_queue_empty(&sk->sk_receive_queue) ||
1428 (sock->state != SS_CONNECTING)),
1429 sk->sk_rcvtimeo);
1430 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001431
Allan Stephensb89741a2008-04-15 00:20:37 -07001432 if (res > 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001433 buf = skb_peek(&sk->sk_receive_queue);
1434 if (buf != NULL) {
1435 msg = buf_msg(buf);
1436 res = auto_connect(sock, msg);
1437 if (!res) {
1438 if (!msg_data_sz(msg))
1439 advance_rx_queue(sk);
1440 }
1441 } else {
1442 if (sock->state == SS_CONNECTED) {
1443 res = -EISCONN;
1444 } else {
1445 res = -ECONNREFUSED;
1446 }
Allan Stephensb89741a2008-04-15 00:20:37 -07001447 }
1448 } else {
1449 if (res == 0)
1450 res = -ETIMEDOUT;
1451 else
1452 ; /* leave "res" unchanged */
1453 sock->state = SS_DISCONNECTING;
1454 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001455
Allan Stephens0c3141e2008-04-15 00:22:02 -07001456exit:
1457 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001458 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001459}
1460
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001461/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001462 * listen - allow socket to listen for incoming connections
1463 * @sock: socket structure
1464 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001465 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001466 * Returns 0 on success, errno otherwise
1467 */
1468
1469static int listen(struct socket *sock, int len)
1470{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001471 struct sock *sk = sock->sk;
1472 int res;
1473
1474 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475
1476 if (sock->state == SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001477 res = -EOPNOTSUPP;
1478 else if (sock->state != SS_UNCONNECTED)
1479 res = -EINVAL;
1480 else {
1481 sock->state = SS_LISTENING;
1482 res = 0;
1483 }
1484
1485 release_sock(sk);
1486 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001487}
1488
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001489/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001490 * accept - wait for connection request
1491 * @sock: listening socket
1492 * @newsock: new socket that is to be connected
1493 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001494 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001495 * Returns 0 on success, errno otherwise
1496 */
1497
Allan Stephens0c3141e2008-04-15 00:22:02 -07001498static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001499{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001500 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001501 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001502 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001503
Allan Stephens0c3141e2008-04-15 00:22:02 -07001504 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001505
Allan Stephens0c3141e2008-04-15 00:22:02 -07001506 if (sock->state == SS_READY) {
1507 res = -EOPNOTSUPP;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001508 goto exit;
1509 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001510 if (sock->state != SS_LISTENING) {
1511 res = -EINVAL;
1512 goto exit;
1513 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001514
Allan Stephens0c3141e2008-04-15 00:22:02 -07001515 while (skb_queue_empty(&sk->sk_receive_queue)) {
1516 if (flags & O_NONBLOCK) {
1517 res = -EWOULDBLOCK;
1518 goto exit;
1519 }
1520 release_sock(sk);
1521 res = wait_event_interruptible(*sk->sk_sleep,
1522 (!skb_queue_empty(&sk->sk_receive_queue)));
1523 lock_sock(sk);
1524 if (res)
1525 goto exit;
1526 }
1527
1528 buf = skb_peek(&sk->sk_receive_queue);
1529
1530 res = tipc_create(sock_net(sock->sk), new_sock, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001531 if (!res) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001532 struct sock *new_sk = new_sock->sk;
1533 struct tipc_port *new_tport = tipc_sk_port(new_sk);
1534 u32 new_ref = new_tport->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001535 struct tipc_portid id;
1536 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001537
1538 lock_sock(new_sk);
1539
1540 /*
1541 * Reject any stray messages received by new socket
1542 * before the socket lock was taken (very, very unlikely)
1543 */
1544
1545 reject_rx_queue(new_sk);
1546
1547 /* Connect new socket to it's peer */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001548
1549 id.ref = msg_origport(msg);
1550 id.node = msg_orignode(msg);
1551 tipc_connect2port(new_ref, &id);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001552 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001553
1554 tipc_set_portimportance(new_ref, msg_importance(msg));
1555 if (msg_named(msg)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001556 new_tport->conn_type = msg_nametype(msg);
1557 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001558 }
1559
Allan Stephens0c3141e2008-04-15 00:22:02 -07001560 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001561 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1562 * Respond to 'SYN+' by queuing it on new socket.
1563 */
1564
1565 msg_dbg(msg,"<ACC<: ");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001566 if (!msg_data_sz(msg)) {
1567 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001568
Allan Stephens0c3141e2008-04-15 00:22:02 -07001569 advance_rx_queue(sk);
1570 send_packet(NULL, new_sock, &m, 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001571 } else {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001572 __skb_dequeue(&sk->sk_receive_queue);
1573 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001575 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001576 }
1577exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001578 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001579 return res;
1580}
1581
1582/**
1583 * shutdown - shutdown socket connection
1584 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001585 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001586 *
1587 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001588 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001589 * Returns 0 on success, errno otherwise
1590 */
1591
1592static int shutdown(struct socket *sock, int how)
1593{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001594 struct sock *sk = sock->sk;
1595 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001596 struct sk_buff *buf;
1597 int res;
1598
Allan Stephense247a8f2008-03-06 15:05:38 -08001599 if (how != SHUT_RDWR)
1600 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001601
Allan Stephens0c3141e2008-04-15 00:22:02 -07001602 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001603
1604 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001605 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606 case SS_CONNECTED:
1607
Allan Stephens0c3141e2008-04-15 00:22:02 -07001608 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001609restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001610 buf = __skb_dequeue(&sk->sk_receive_queue);
1611 if (buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001612 atomic_dec(&tipc_queue_size);
1613 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1614 buf_discard(buf);
1615 goto restart;
1616 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001617 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001618 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001619 } else {
1620 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001621 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001622
1623 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001624
1625 /* fall through */
1626
1627 case SS_DISCONNECTING:
1628
Allan Stephens0c3141e2008-04-15 00:22:02 -07001629 /* Discard any unreceived messages; wake up sleeping tasks */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630
Allan Stephens0c3141e2008-04-15 00:22:02 -07001631 discard_rx_queue(sk);
1632 if (waitqueue_active(sk->sk_sleep))
1633 wake_up_interruptible(sk->sk_sleep);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001634 res = 0;
1635 break;
1636
1637 default:
1638 res = -ENOTCONN;
1639 }
1640
Allan Stephens0c3141e2008-04-15 00:22:02 -07001641 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001642 return res;
1643}
1644
1645/**
1646 * setsockopt - set socket option
1647 * @sock: socket structure
1648 * @lvl: option level
1649 * @opt: option identifier
1650 * @ov: pointer to new option value
1651 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001652 *
1653 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001654 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001655 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656 * Returns 0 on success, errno otherwise
1657 */
1658
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001659static int setsockopt(struct socket *sock,
Allan Stephense9024f0f2006-06-25 23:43:57 -07001660 int lvl, int opt, char __user *ov, int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001662 struct sock *sk = sock->sk;
1663 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001664 u32 value;
1665 int res;
1666
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001667 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1668 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669 if (lvl != SOL_TIPC)
1670 return -ENOPROTOOPT;
1671 if (ol < sizeof(value))
1672 return -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001673 if ((res = get_user(value, (u32 __user *)ov)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674 return res;
1675
Allan Stephens0c3141e2008-04-15 00:22:02 -07001676 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001677
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678 switch (opt) {
1679 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001680 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001681 break;
1682 case TIPC_SRC_DROPPABLE:
1683 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001684 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001685 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001686 res = -ENOPROTOOPT;
1687 break;
1688 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001689 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690 break;
1691 case TIPC_CONN_TIMEOUT:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001692 sk->sk_rcvtimeo = msecs_to_jiffies(value);
1693 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001694 break;
1695 default:
1696 res = -EINVAL;
1697 }
1698
Allan Stephens0c3141e2008-04-15 00:22:02 -07001699 release_sock(sk);
1700
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701 return res;
1702}
1703
1704/**
1705 * getsockopt - get socket option
1706 * @sock: socket structure
1707 * @lvl: option level
1708 * @opt: option identifier
1709 * @ov: receptacle for option value
1710 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001711 *
1712 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001713 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001714 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001715 * Returns 0 on success, errno otherwise
1716 */
1717
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001718static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001719 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001720{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001721 struct sock *sk = sock->sk;
1722 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001723 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001724 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001725 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001727 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1728 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001729 if (lvl != SOL_TIPC)
1730 return -ENOPROTOOPT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001731 if ((res = get_user(len, ol)))
1732 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001733
Allan Stephens0c3141e2008-04-15 00:22:02 -07001734 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001735
1736 switch (opt) {
1737 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001738 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739 break;
1740 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001741 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001742 break;
1743 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001744 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001745 break;
1746 case TIPC_CONN_TIMEOUT:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001747 value = jiffies_to_msecs(sk->sk_rcvtimeo);
1748 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749 break;
1750 default:
1751 res = -EINVAL;
1752 }
1753
Allan Stephens0c3141e2008-04-15 00:22:02 -07001754 release_sock(sk);
1755
Per Lidenb97bf3f2006-01-02 19:04:38 +01001756 if (res) {
1757 /* "get" failed */
1758 }
1759 else if (len < sizeof(value)) {
1760 res = -EINVAL;
1761 }
Pavel Emelyanov653252c2008-04-25 01:49:48 -07001762 else if (copy_to_user(ov, &value, sizeof(value))) {
1763 res = -EFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001764 }
1765 else {
1766 res = put_user(sizeof(value), ol);
1767 }
1768
Per Lidenb97bf3f2006-01-02 19:04:38 +01001769 return res;
1770}
1771
1772/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001773 * Protocol switches for the various types of TIPC sockets
1774 */
1775
Florian Westphalbca65ea2008-02-07 18:18:01 -08001776static const struct proto_ops msg_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001777 .owner = THIS_MODULE,
1778 .family = AF_TIPC,
1779 .release = release,
1780 .bind = bind,
1781 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001782 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001783 .accept = accept,
1784 .getname = get_name,
1785 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001786 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787 .listen = listen,
1788 .shutdown = shutdown,
1789 .setsockopt = setsockopt,
1790 .getsockopt = getsockopt,
1791 .sendmsg = send_msg,
1792 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001793 .mmap = sock_no_mmap,
1794 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001795};
1796
Florian Westphalbca65ea2008-02-07 18:18:01 -08001797static const struct proto_ops packet_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001798 .owner = THIS_MODULE,
1799 .family = AF_TIPC,
1800 .release = release,
1801 .bind = bind,
1802 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001803 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001804 .accept = accept,
1805 .getname = get_name,
1806 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001807 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001808 .listen = listen,
1809 .shutdown = shutdown,
1810 .setsockopt = setsockopt,
1811 .getsockopt = getsockopt,
1812 .sendmsg = send_packet,
1813 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001814 .mmap = sock_no_mmap,
1815 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816};
1817
Florian Westphalbca65ea2008-02-07 18:18:01 -08001818static const struct proto_ops stream_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001819 .owner = THIS_MODULE,
1820 .family = AF_TIPC,
1821 .release = release,
1822 .bind = bind,
1823 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001824 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001825 .accept = accept,
1826 .getname = get_name,
1827 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001828 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829 .listen = listen,
1830 .shutdown = shutdown,
1831 .setsockopt = setsockopt,
1832 .getsockopt = getsockopt,
1833 .sendmsg = send_stream,
1834 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001835 .mmap = sock_no_mmap,
1836 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001837};
1838
Florian Westphalbca65ea2008-02-07 18:18:01 -08001839static const struct net_proto_family tipc_family_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001840 .owner = THIS_MODULE,
1841 .family = AF_TIPC,
1842 .create = tipc_create
1843};
1844
1845static struct proto tipc_proto = {
1846 .name = "TIPC",
1847 .owner = THIS_MODULE,
1848 .obj_size = sizeof(struct tipc_sock)
1849};
1850
1851/**
Per Liden4323add2006-01-18 00:38:21 +01001852 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001853 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001854 * Returns 0 on success, errno otherwise
1855 */
Per Liden4323add2006-01-18 00:38:21 +01001856int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001857{
1858 int res;
1859
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001860 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001861 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001862 err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001863 goto out;
1864 }
1865
1866 res = sock_register(&tipc_family_ops);
1867 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001868 err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001869 proto_unregister(&tipc_proto);
1870 goto out;
1871 }
1872
1873 sockets_enabled = 1;
1874 out:
1875 return res;
1876}
1877
1878/**
Per Liden4323add2006-01-18 00:38:21 +01001879 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001880 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001881
Per Liden4323add2006-01-18 00:38:21 +01001882void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001883{
1884 if (!sockets_enabled)
1885 return;
1886
1887 sockets_enabled = 0;
1888 sock_unregister(tipc_family_ops.family);
1889 proto_unregister(&tipc_proto);
1890}
1891