blob: 05853159536a03b984708c400808dcf6e26fcb77 [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 Stephens0c3141e2008-04-15 00:22:02 -0700191 u32 portref;
192
193 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700195 if (net != &init_net)
196 return -EAFNOSUPPORT;
197
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198 if (unlikely(protocol != 0))
199 return -EPROTONOSUPPORT;
200
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 switch (sock->type) {
202 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700203 ops = &stream_ops;
204 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100205 break;
206 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700207 ops = &packet_ops;
208 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209 break;
210 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700212 ops = &msg_ops;
213 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 break;
Allan Stephens49978652006-06-25 23:47:18 -0700215 default:
Allan Stephens49978652006-06-25 23:47:18 -0700216 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217 }
218
Allan Stephens0c3141e2008-04-15 00:22:02 -0700219 /* Allocate socket's protocol area */
220
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700221 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700222 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 Lidenb97bf3f2006-01-02 19:04:38 +0100231 return -ENOMEM;
232 }
233
Allan Stephens0c3141e2008-04-15 00:22:02 -0700234 /* Finish initializing socket data structures */
235
236 sock->ops = ops;
237 sock->state = state;
238
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 sock_init_data(sock, sk);
Allan Stephens3654ea02008-04-13 21:35:11 -0700240 sk->sk_rcvtimeo = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700241 sk->sk_backlog_rcv = backlog_rcv;
242 tipc_sk(sk)->p = tipc_get_port(portref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243
Allan Stephens0c3141e2008-04-15 00:22:02 -0700244 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 Lidenb97bf3f2006-01-02 19:04:38 +0100249
250 atomic_inc(&tipc_user_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251 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 Hideakic4307282007-02-09 23:25:21 +0900263 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264 * 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
271static int release(struct socket *sock)
272{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700274 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700276 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100277
Allan Stephens0c3141e2008-04-15 00:22:02 -0700278 /*
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 Hideakic4307282007-02-09 23:25:21 +0900282
Allan Stephens0c3141e2008-04-15 00:22:02 -0700283 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 Lidenb97bf3f2006-01-02 19:04:38 +0100293
294 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700295 buf = __skb_dequeue(&sk->sk_receive_queue);
296 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297 break;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700298 atomic_dec(&tipc_queue_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
300 buf_discard(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700301 else {
302 if ((sock->state == SS_CONNECTING) ||
303 (sock->state == SS_CONNECTED)) {
304 sock->state = SS_DISCONNECTING;
305 tipc_disconnect(tport->ref);
306 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700308 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309 }
310
Allan Stephens0c3141e2008-04-15 00:22:02 -0700311 /*
312 * Delete TIPC port; this ensures no more messages are queued
313 * (also disconnects an active connection & sends a 'FIN-' to peer)
314 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315
Allan Stephens0c3141e2008-04-15 00:22:02 -0700316 res = tipc_deleteport(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317
Allan Stephens0c3141e2008-04-15 00:22:02 -0700318 /* Discard any remaining (connection-based) messages in receive queue */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100319
Allan Stephens0c3141e2008-04-15 00:22:02 -0700320 discard_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321
Allan Stephens0c3141e2008-04-15 00:22:02 -0700322 /* Reject any messages that accumulated in backlog queue */
323
324 sock->state = SS_DISCONNECTING;
325 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326
327 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700328 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900330 atomic_dec(&tipc_user_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331 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 Hideakic4307282007-02-09 23:25:21 +0900339 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340 * 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 Hideakic4307282007-02-09 23:25:21 +0900343 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700345 *
346 * NOTE: This routine doesn't need to take the socket lock since it doesn't
347 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348 */
349
350static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
351{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700353 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354
Allan Stephens0c3141e2008-04-15 00:22:02 -0700355 if (unlikely(!uaddr_len))
356 return tipc_withdraw(portref, 0, NULL);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900357
Allan Stephens0c3141e2008-04-15 00:22:02 -0700358 if (uaddr_len < sizeof(struct sockaddr_tipc))
359 return -EINVAL;
360 if (addr->family != AF_TIPC)
361 return -EAFNOSUPPORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363 if (addr->addrtype == TIPC_ADDR_NAME)
364 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700365 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
366 return -EAFNOSUPPORT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900367
Allan Stephens0c3141e2008-04-15 00:22:02 -0700368 return (addr->scope > 0) ?
369 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
370 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100371}
372
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900373/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374 * 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 Hideakic4307282007-02-09 23:25:21 +0900379 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700381 *
382 * NOTE: This routine doesn't need to take the socket lock since it doesn't
383 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384 */
385
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900386static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387 int *uaddr_len, int peer)
388{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700390 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 u32 res;
392
Allan Stephens0c3141e2008-04-15 00:22:02 -0700393 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 Lidenb97bf3f2006-01-02 19:04:38 +0100400
401 *uaddr_len = sizeof(*addr);
402 addr->addrtype = TIPC_ADDR_ID;
403 addr->family = AF_TIPC;
404 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405 addr->addr.name.domain = 0;
406
Allan Stephens0c3141e2008-04-15 00:22:02 -0700407 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408}
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 Stephens9b674e82008-03-26 16:48:21 -0700416 * 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 Lidenb97bf3f2006-01-02 19:04:38 +0100434 */
435
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900436static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437 poll_table *wait)
438{
Allan Stephens9b674e82008-03-26 16:48:21 -0700439 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 Lidenb97bf3f2006-01-02 19:04:38 +0100457}
458
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900459/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 * 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 Hideakic4307282007-02-09 23:25:21 +0900463 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 * Prevents restricted configuration commands from being issued by
465 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900466 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467 * Returns 0 if permission is granted, otherwise errno
468 */
469
Sam Ravnborg05790c62006-03-20 22:37:04 -0800470static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471{
472 struct tipc_cfg_msg_hdr hdr;
473
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900474 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 Hideakic4307282007-02-09 23:25:21 +0900478 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
479 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900481 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700483 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900485
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486 return 0;
487}
488
489/**
490 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700491 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492 * @sock: socket structure
493 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700494 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900495 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900497 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
499 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900500 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 * Returns the number of bytes sent on success, or errno otherwise
502 */
503
504static int send_msg(struct kiocb *iocb, struct socket *sock,
505 struct msghdr *m, size_t total_len)
506{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700507 struct sock *sk = sock->sk;
508 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900509 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100510 int needs_conn;
511 int res = -EINVAL;
512
513 if (unlikely(!dest))
514 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700515 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
516 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517 return -EINVAL;
518
Allan Stephens0c3141e2008-04-15 00:22:02 -0700519 if (iocb)
520 lock_sock(sk);
521
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522 needs_conn = (sock->state != SS_READY);
523 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700524 if (sock->state == SS_LISTENING) {
525 res = -EPIPE;
526 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700527 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700528 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 Lidenb97bf3f2006-01-02 19:04:38 +0100541
542 /* Abort any pending connection attempts (very unlikely) */
543
Allan Stephens0c3141e2008-04-15 00:22:02 -0700544 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 }
546
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900547 do {
548 if (dest->addrtype == TIPC_ADDR_NAME) {
549 if ((res = dest_name_check(dest, m)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700550 break;
551 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900552 &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 Stephens0c3141e2008-04-15 00:22:02 -0700558 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900559 &dest->addr.id,
560 m->msg_iovlen,
561 m->msg_iov);
562 }
563 else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564 if (needs_conn) {
565 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700566 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900568 if ((res = dest_name_check(dest, m)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700569 break;
570 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900571 &dest->addr.nameseq,
572 0,
573 m->msg_iovlen,
574 m->msg_iov);
575 }
576 if (likely(res != -ELINKCONG)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700577 if (needs_conn && (res >= 0)) {
578 sock->state = SS_CONNECTING;
579 }
580 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900581 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 if (m->msg_flags & MSG_DONTWAIT) {
583 res = -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700584 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700586 release_sock(sk);
587 res = wait_event_interruptible(*sk->sk_sleep,
588 !tport->congested);
589 lock_sock(sk);
590 if (res)
591 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900592 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700593
594exit:
595 if (iocb)
596 release_sock(sk);
597 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598}
599
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900600/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700602 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603 * @sock: socket structure
604 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700605 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900606 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900608 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 * Returns the number of bytes sent on success, or errno otherwise
610 */
611
612static int send_packet(struct kiocb *iocb, struct socket *sock,
613 struct msghdr *m, size_t total_len)
614{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700615 struct sock *sk = sock->sk;
616 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900617 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618 int res;
619
620 /* Handle implied connection establishment */
621
622 if (unlikely(dest))
623 return send_msg(iocb, sock, m, total_len);
624
Allan Stephens0c3141e2008-04-15 00:22:02 -0700625 if (iocb)
626 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900628 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700629 if (unlikely(sock->state != SS_CONNECTED)) {
630 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900631 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700632 else
633 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700634 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700635 }
636
Allan Stephens0c3141e2008-04-15 00:22:02 -0700637 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900638 if (likely(res != -ELINKCONG)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700639 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900640 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 if (m->msg_flags & MSG_DONTWAIT) {
642 res = -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700643 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700645 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 Hideakic4307282007-02-09 23:25:21 +0900651 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700652
653 if (iocb)
654 release_sock(sk);
655 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656}
657
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900658/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659 * 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 Hideakic4307282007-02-09 23:25:21 +0900664 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900666 *
667 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700668 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 */
670
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671static int send_stream(struct kiocb *iocb, struct socket *sock,
672 struct msghdr *m, size_t total_len)
673{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700674 struct sock *sk = sock->sk;
675 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 struct msghdr my_msg;
677 struct iovec my_iov;
678 struct iovec *curr_iov;
679 int curr_iovlen;
680 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700681 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100682 int curr_left;
683 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700684 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900686
Allan Stephens0c3141e2008-04-15 00:22:02 -0700687 lock_sock(sk);
688
Allan Stephens05646c92007-06-10 17:25:24 -0700689 /* Handle special cases where there is no connection */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900691 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700692 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 Hideakic4307282007-02-09 23:25:21 +0900702 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100703
Allan Stephens0c3141e2008-04-15 00:22:02 -0700704 if (unlikely(m->msg_name)) {
705 res = -EISCONN;
706 goto exit;
707 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700708
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900709 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710 * Send each iovec entry using one or more messages
711 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900712 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100713 * (i.e. one large iovec entry), but could be improved to pass sets
714 * of small iovec entries into send_packet().
715 */
716
Allan Stephens1303e8f2006-06-25 23:46:50 -0700717 curr_iov = m->msg_iov;
718 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719 my_msg.msg_iov = &my_iov;
720 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700721 my_msg.msg_flags = m->msg_flags;
722 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700723 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724
Allan Stephens05646c92007-06-10 17:25:24 -0700725 hdr_size = msg_hdr_sz(&tport->phdr);
726
Per Lidenb97bf3f2006-01-02 19:04:38 +0100727 while (curr_iovlen--) {
728 curr_start = curr_iov->iov_base;
729 curr_left = curr_iov->iov_len;
730
731 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700732 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 Lidenb97bf3f2006-01-02 19:04:38 +0100737 my_iov.iov_base = curr_start;
738 my_iov.iov_len = bytes_to_send;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700739 if ((res = send_packet(NULL, sock, &my_msg, 0)) < 0) {
740 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700741 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700742 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700743 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100744 curr_left -= bytes_to_send;
745 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700746 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747 }
748
749 curr_iov++;
750 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700751 res = bytes_sent;
752exit:
753 release_sock(sk);
754 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755}
756
757/**
758 * auto_connect - complete connection setup to a remote port
759 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900761 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100762 * Returns 0 on success, errno otherwise
763 */
764
Allan Stephens0c3141e2008-04-15 00:22:02 -0700765static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700767 struct tipc_port *tport = tipc_sk_port(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100768 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 Stephens0c3141e2008-04-15 00:22:02 -0700777 tipc_connect2port(tport->ref, &peer);
778 tipc_set_portimportance(tport->ref, msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779 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 Hideakic4307282007-02-09 23:25:21 +0900787 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788 * Note: Address is not captured if not requested by receiver.
789 */
790
Sam Ravnborg05790c62006-03-20 22:37:04 -0800791static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900793 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900795 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 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 Hideakic4307282007-02-09 23:25:21 +0900807 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808 * @m: descriptor for message info
809 * @msg: received message header
810 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900811 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900813 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100814 * Returns 0 if successful, otherwise errno
815 */
816
Sam Ravnborg05790c62006-03-20 22:37:04 -0800817static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100818 struct tipc_port *tport)
819{
820 u32 anc_data[3];
821 u32 err;
822 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700823 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824 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 Stephens4b087b22006-06-25 23:47:44 -0700835 if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836 return res;
837 if (anc_data[1] &&
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900838 (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
Per Lidenb97bf3f2006-01-02 19:04:38 +0100839 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 Stephens3546c752006-06-25 23:45:24 -0700848 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100849 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 Stephens3546c752006-06-25 23:45:24 -0700854 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855 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 Stephens3546c752006-06-25 23:45:24 -0700860 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861 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 Stephens3546c752006-06-25 23:45:24 -0700866 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867 }
Allan Stephens3546c752006-06-25 23:45:24 -0700868 if (has_name &&
Allan Stephens4b087b22006-06-25 23:47:44 -0700869 (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870 return res;
871
872 return 0;
873}
874
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900875/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100876 * 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 Hideakic4307282007-02-09 23:25:21 +0900881 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100882 * 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
888static int recv_msg(struct kiocb *iocb, struct socket *sock,
889 struct msghdr *m, size_t buf_len, int flags)
890{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700891 struct sock *sk = sock->sk;
892 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893 struct sk_buff *buf;
894 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100895 unsigned int sz;
896 u32 err;
897 int res;
898
Allan Stephens0c3141e2008-04-15 00:22:02 -0700899 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900
901 if (m->msg_iovlen != 1)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700902 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903
904 if (unlikely(!buf_len))
905 return -EINVAL;
906
Allan Stephens0c3141e2008-04-15 00:22:02 -0700907 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908
Allan Stephens0c3141e2008-04-15 00:22:02 -0700909 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910 res = -ENOTCONN;
911 goto exit;
912 }
913
Allan Stephens0c3141e2008-04-15 00:22:02 -0700914restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100915
Allan Stephens0c3141e2008-04-15 00:22:02 -0700916 /* 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 Lidenb97bf3f2006-01-02 19:04:38 +0100939 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 Stephens0c3141e2008-04-15 00:22:02 -0700946 res = auto_connect(sock, msg);
947 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100948 goto exit;
949 }
950
951 /* Discard an empty non-errored message & try again */
952
953 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700954 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955 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 Stephens0c3141e2008-04-15 00:22:02 -0700964 res = anc_data_recv(m, msg, tport);
965 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100966 goto exit;
967
968 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900969
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970 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 Stephens99009802008-04-15 00:06:12 -0700992 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -0700993 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
994 tipc_acknowledge(tport->ref, tport->conn_unacked);
995 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900996 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100997exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700998 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100999 return res;
1000}
1001
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001002/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001003 * 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 Hideakic4307282007-02-09 23:25:21 +09001008 *
1009 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010 * will optionally wait for more; never truncates data.
1011 *
1012 * Returns size of returned message data, errno otherwise
1013 */
1014
1015static int recv_stream(struct kiocb *iocb, struct socket *sock,
1016 struct msghdr *m, size_t buf_len, int flags)
1017{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001018 struct sock *sk = sock->sk;
1019 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020 struct sk_buff *buf;
1021 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001022 unsigned int sz;
1023 int sz_to_copy;
1024 int sz_copied = 0;
1025 int needed;
Al Viro28c4dad2006-10-10 22:45:57 +01001026 char __user *crs = m->msg_iov->iov_base;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027 unsigned char *buf_crs;
1028 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001029 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030
1031 /* Catch invalid receive attempts */
1032
Allan Stephens0c3141e2008-04-15 00:22:02 -07001033 if (m->msg_iovlen != 1)
1034 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
1035
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036 if (unlikely(!buf_len))
1037 return -EINVAL;
1038
Allan Stephens0c3141e2008-04-15 00:22:02 -07001039 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040
Allan Stephens0c3141e2008-04-15 00:22:02 -07001041 if (unlikely((sock->state == SS_UNCONNECTED) ||
1042 (sock->state == SS_CONNECTING))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043 res = -ENOTCONN;
1044 goto exit;
1045 }
1046
Allan Stephens0c3141e2008-04-15 00:22:02 -07001047restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048
Allan Stephens0c3141e2008-04-15 00:22:02 -07001049 /* 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 Lidenb97bf3f2006-01-02 19:04:38 +01001072 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 Stephens0c3141e2008-04-15 00:22:02 -07001079 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001080 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 Stephens0c3141e2008-04-15 00:22:02 -07001087 res = anc_data_recv(m, msg, tport);
1088 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089 goto exit;
1090 }
1091
1092 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001093
Per Lidenb97bf3f2006-01-02 19:04:38 +01001094 if (!err) {
1095 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
Allan Stephens7a8036c2008-04-15 00:15:15 -07001096 sz = (unsigned char *)msg + msg_size(msg) - buf_crs;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001097
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 Stephens0c3141e2008-04-15 00:22:02 -07001126 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1127 tipc_acknowledge(tport->ref, tport->conn_unacked);
1128 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001129 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130
1131 /* Loop around if more data is required */
1132
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001133 if ((sz_copied < buf_len) /* didn't get all requested data */
Allan Stephensa198d3a2008-04-15 00:07:15 -07001134 && (!skb_queue_empty(&sock->sk->sk_receive_queue) ||
1135 (flags & MSG_WAITALL))
1136 /* ... and more is ready or required */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
1138 && (!err) /* ... and haven't reached a FIN */
1139 )
1140 goto restart;
1141
1142exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001143 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001144 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145}
1146
1147/**
Allan Stephens1819b832008-04-15 00:15:50 -07001148 * rx_queue_full - determine if receive queue can accept another message
1149 * @msg: message to be added to queue
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150 * @queue_size: current size of queue
1151 * @base: nominal maximum size of queue
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001152 *
Allan Stephens1819b832008-04-15 00:15:50 -07001153 * Returns 1 if queue is unable to accept message, 0 otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154 */
1155
Allan Stephens1819b832008-04-15 00:15:50 -07001156static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157{
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 Stephens1819b832008-04-15 00:15:50 -07001173 return (queue_size >= threshold);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174}
1175
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001176/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001177 * filter_rcv - validate incoming message
1178 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001180 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001181 * 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 Hideakic4307282007-02-09 23:25:21 +09001185 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1187 */
1188
Allan Stephens0c3141e2008-04-15 00:22:02 -07001189static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001191 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192 struct tipc_msg *msg = buf_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001193 u32 recv_q_len;
1194
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 /* 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 Stephens0c3141e2008-04-15 00:22:02 -07001202
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 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 Hideakic4307282007-02-09 23:25:21 +09001224 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225 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 Hideakic4307282007-02-09 23:25:21 +09001230 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 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 Stephens1819b832008-04-15 00:15:50 -07001245 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 Lidenb97bf3f2006-01-02 19:04:38 +01001248 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001249 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001250 recv_q_len = skb_queue_len(&sk->sk_receive_queue);
Allan Stephens1819b832008-04-15 00:15:50 -07001251 if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
1252 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001253 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001254 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001255
Allan Stephens0c3141e2008-04-15 00:22:02 -07001256 /* 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 Lidenb97bf3f2006-01-02 19:04:38 +01001263 /* Initiate connection termination for an incoming 'FIN' */
1264
1265 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1266 sock->state = SS_DISCONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001267 tipc_disconnect_port(tipc_sk_port(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268 }
1269
Allan Stephens0c3141e2008-04-15 00:22:02 -07001270 if (waitqueue_active(sk->sk_sleep))
1271 wake_up_interruptible(sk->sk_sleep);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 return TIPC_OK;
1273}
1274
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001275/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001276 * 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
1285static 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
1305static 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 Lidenb97bf3f2006-01-02 19:04:38 +01001330 * wakeupdispatch - wake up port after congestion
1331 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001332 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001333 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334 */
1335
1336static void wakeupdispatch(struct tipc_port *tport)
1337{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001338 struct sock *sk = (struct sock *)tport->usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339
Allan Stephens0c3141e2008-04-15 00:22:02 -07001340 if (waitqueue_active(sk->sk_sleep))
1341 wake_up_interruptible(sk->sk_sleep);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342}
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 Stephens0c3141e2008-04-15 00:22:02 -07001349 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001350 *
1351 * Returns 0 on success, errno otherwise
1352 */
1353
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001354static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355 int flags)
1356{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001357 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001358 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 Lidenb97bf3f2006-01-02 19:04:38 +01001363
Allan Stephens0c3141e2008-04-15 00:22:02 -07001364 lock_sock(sk);
1365
Allan Stephensb89741a2008-04-15 00:20:37 -07001366 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367
Allan Stephens0c3141e2008-04-15 00:22:02 -07001368 if (sock->state == SS_READY) {
1369 res = -EOPNOTSUPP;
1370 goto exit;
1371 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372
Allan Stephensb89741a2008-04-15 00:20:37 -07001373 /* For now, TIPC does not support the non-blocking form of connect() */
Allan Stephens4934c692008-04-15 00:16:19 -07001374
Allan Stephens0c3141e2008-04-15 00:22:02 -07001375 if (flags & O_NONBLOCK) {
1376 res = -EWOULDBLOCK;
1377 goto exit;
1378 }
Allan Stephens4934c692008-04-15 00:16:19 -07001379
Allan Stephensb89741a2008-04-15 00:20:37 -07001380 /* Issue Posix-compliant error code if socket is in the wrong state */
Allan Stephens51f9cc12006-06-25 23:49:06 -07001381
Allan Stephens0c3141e2008-04-15 00:22:02 -07001382 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 Lidenb97bf3f2006-01-02 19:04:38 +01001394
Allan Stephensb89741a2008-04-15 00:20:37 -07001395 /*
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 Stephens51f9cc12006-06-25 23:49:06 -07001401
Allan Stephens0c3141e2008-04-15 00:22:02 -07001402 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 Lidenb97bf3f2006-01-02 19:04:38 +01001410
Allan Stephensb89741a2008-04-15 00:20:37 -07001411 /* Send a 'SYN-' to destination */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001412
Allan Stephensb89741a2008-04-15 00:20:37 -07001413 m.msg_name = dest;
1414 m.msg_namelen = destlen;
1415 res = send_msg(NULL, sock, &m, 0);
1416 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001417 goto exit;
Allan Stephensb89741a2008-04-15 00:20:37 -07001418 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001419
Allan Stephens0c3141e2008-04-15 00:22:02 -07001420 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001421
Allan Stephens0c3141e2008-04-15 00:22:02 -07001422 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 Lidenb97bf3f2006-01-02 19:04:38 +01001428
Allan Stephensb89741a2008-04-15 00:20:37 -07001429 if (res > 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001430 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 Stephensb89741a2008-04-15 00:20:37 -07001444 }
1445 } else {
1446 if (res == 0)
1447 res = -ETIMEDOUT;
1448 else
1449 ; /* leave "res" unchanged */
1450 sock->state = SS_DISCONNECTING;
1451 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001452
Allan Stephens0c3141e2008-04-15 00:22:02 -07001453exit:
1454 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001455 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001456}
1457
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001458/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001459 * listen - allow socket to listen for incoming connections
1460 * @sock: socket structure
1461 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001462 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463 * Returns 0 on success, errno otherwise
1464 */
1465
1466static int listen(struct socket *sock, int len)
1467{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001468 struct sock *sk = sock->sk;
1469 int res;
1470
1471 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001472
1473 if (sock->state == SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001474 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 Lidenb97bf3f2006-01-02 19:04:38 +01001484}
1485
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001486/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001487 * 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 Hideakic4307282007-02-09 23:25:21 +09001491 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001492 * Returns 0 on success, errno otherwise
1493 */
1494
Allan Stephens0c3141e2008-04-15 00:22:02 -07001495static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001496{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001497 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001498 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001499 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001500
Allan Stephens0c3141e2008-04-15 00:22:02 -07001501 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001502
Allan Stephens0c3141e2008-04-15 00:22:02 -07001503 if (sock->state == SS_READY) {
1504 res = -EOPNOTSUPP;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001505 goto exit;
1506 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001507 if (sock->state != SS_LISTENING) {
1508 res = -EINVAL;
1509 goto exit;
1510 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511
Allan Stephens0c3141e2008-04-15 00:22:02 -07001512 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 Lidenb97bf3f2006-01-02 19:04:38 +01001528 if (!res) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001529 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 Lidenb97bf3f2006-01-02 19:04:38 +01001532 struct tipc_portid id;
1533 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001534
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 Lidenb97bf3f2006-01-02 19:04:38 +01001545
1546 id.ref = msg_origport(msg);
1547 id.node = msg_orignode(msg);
1548 tipc_connect2port(new_ref, &id);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001549 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001550
1551 tipc_set_portimportance(new_ref, msg_importance(msg));
1552 if (msg_named(msg)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001553 new_tport->conn_type = msg_nametype(msg);
1554 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001555 }
1556
Allan Stephens0c3141e2008-04-15 00:22:02 -07001557 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001558 * 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 Hideakic4307282007-02-09 23:25:21 +09001563 if (!msg_data_sz(msg)) {
1564 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001565
Allan Stephens0c3141e2008-04-15 00:22:02 -07001566 advance_rx_queue(sk);
1567 send_packet(NULL, new_sock, &m, 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001568 } else {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001569 __skb_dequeue(&sk->sk_receive_queue);
1570 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001571 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001572 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001573 }
1574exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001575 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001576 return res;
1577}
1578
1579/**
1580 * shutdown - shutdown socket connection
1581 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001582 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001583 *
1584 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001585 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001586 * Returns 0 on success, errno otherwise
1587 */
1588
1589static int shutdown(struct socket *sock, int how)
1590{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001591 struct sock *sk = sock->sk;
1592 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001593 struct sk_buff *buf;
1594 int res;
1595
Allan Stephense247a8f2008-03-06 15:05:38 -08001596 if (how != SHUT_RDWR)
1597 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001598
Allan Stephens0c3141e2008-04-15 00:22:02 -07001599 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600
1601 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001602 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001603 case SS_CONNECTED:
1604
Allan Stephens0c3141e2008-04-15 00:22:02 -07001605 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001607 buf = __skb_dequeue(&sk->sk_receive_queue);
1608 if (buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001609 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 Stephens0c3141e2008-04-15 00:22:02 -07001614 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001615 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001616 } else {
1617 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001618 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001619
1620 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001621
1622 /* fall through */
1623
1624 case SS_DISCONNECTING:
1625
Allan Stephens0c3141e2008-04-15 00:22:02 -07001626 /* Discard any unreceived messages; wake up sleeping tasks */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001627
Allan Stephens0c3141e2008-04-15 00:22:02 -07001628 discard_rx_queue(sk);
1629 if (waitqueue_active(sk->sk_sleep))
1630 wake_up_interruptible(sk->sk_sleep);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001631 res = 0;
1632 break;
1633
1634 default:
1635 res = -ENOTCONN;
1636 }
1637
Allan Stephens0c3141e2008-04-15 00:22:02 -07001638 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001639 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 Hideakic4307282007-02-09 23:25:21 +09001649 *
1650 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001651 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001652 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001653 * Returns 0 on success, errno otherwise
1654 */
1655
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001656static int setsockopt(struct socket *sock,
Allan Stephense9024f0f2006-06-25 23:43:57 -07001657 int lvl, int opt, char __user *ov, int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001658{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001659 struct sock *sk = sock->sk;
1660 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661 u32 value;
1662 int res;
1663
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001664 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1665 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666 if (lvl != SOL_TIPC)
1667 return -ENOPROTOOPT;
1668 if (ol < sizeof(value))
1669 return -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001670 if ((res = get_user(value, (u32 __user *)ov)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001671 return res;
1672
Allan Stephens0c3141e2008-04-15 00:22:02 -07001673 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001674
Per Lidenb97bf3f2006-01-02 19:04:38 +01001675 switch (opt) {
1676 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001677 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678 break;
1679 case TIPC_SRC_DROPPABLE:
1680 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001681 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001682 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683 res = -ENOPROTOOPT;
1684 break;
1685 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001686 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001687 break;
1688 case TIPC_CONN_TIMEOUT:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001689 sk->sk_rcvtimeo = msecs_to_jiffies(value);
1690 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001691 break;
1692 default:
1693 res = -EINVAL;
1694 }
1695
Allan Stephens0c3141e2008-04-15 00:22:02 -07001696 release_sock(sk);
1697
Per Lidenb97bf3f2006-01-02 19:04:38 +01001698 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 Hideakic4307282007-02-09 23:25:21 +09001708 *
1709 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001710 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001711 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001712 * Returns 0 on success, errno otherwise
1713 */
1714
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001715static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001716 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001717{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001718 struct sock *sk = sock->sk;
1719 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001720 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001721 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001722 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001723
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001724 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1725 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726 if (lvl != SOL_TIPC)
1727 return -ENOPROTOOPT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001728 if ((res = get_user(len, ol)))
1729 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001730
Allan Stephens0c3141e2008-04-15 00:22:02 -07001731 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001732
1733 switch (opt) {
1734 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001735 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001736 break;
1737 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001738 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739 break;
1740 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001741 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001742 break;
1743 case TIPC_CONN_TIMEOUT:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001744 value = jiffies_to_msecs(sk->sk_rcvtimeo);
1745 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746 break;
1747 default:
1748 res = -EINVAL;
1749 }
1750
Allan Stephens0c3141e2008-04-15 00:22:02 -07001751 release_sock(sk);
1752
Per Lidenb97bf3f2006-01-02 19:04:38 +01001753 if (res) {
1754 /* "get" failed */
1755 }
1756 else if (len < sizeof(value)) {
1757 res = -EINVAL;
1758 }
1759 else if ((res = copy_to_user(ov, &value, sizeof(value)))) {
1760 /* couldn't return value */
1761 }
1762 else {
1763 res = put_user(sizeof(value), ol);
1764 }
1765
Per Lidenb97bf3f2006-01-02 19:04:38 +01001766 return res;
1767}
1768
1769/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001770 * Protocol switches for the various types of TIPC sockets
1771 */
1772
Florian Westphalbca65ea2008-02-07 18:18:01 -08001773static const struct proto_ops msg_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774 .owner = THIS_MODULE,
1775 .family = AF_TIPC,
1776 .release = release,
1777 .bind = bind,
1778 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001779 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001780 .accept = accept,
1781 .getname = get_name,
1782 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001783 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001784 .listen = listen,
1785 .shutdown = shutdown,
1786 .setsockopt = setsockopt,
1787 .getsockopt = getsockopt,
1788 .sendmsg = send_msg,
1789 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001790 .mmap = sock_no_mmap,
1791 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001792};
1793
Florian Westphalbca65ea2008-02-07 18:18:01 -08001794static const struct proto_ops packet_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001795 .owner = THIS_MODULE,
1796 .family = AF_TIPC,
1797 .release = release,
1798 .bind = bind,
1799 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001800 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001801 .accept = accept,
1802 .getname = get_name,
1803 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001804 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001805 .listen = listen,
1806 .shutdown = shutdown,
1807 .setsockopt = setsockopt,
1808 .getsockopt = getsockopt,
1809 .sendmsg = send_packet,
1810 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001811 .mmap = sock_no_mmap,
1812 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001813};
1814
Florian Westphalbca65ea2008-02-07 18:18:01 -08001815static const struct proto_ops stream_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816 .owner = THIS_MODULE,
1817 .family = AF_TIPC,
1818 .release = release,
1819 .bind = bind,
1820 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001821 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001822 .accept = accept,
1823 .getname = get_name,
1824 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001825 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826 .listen = listen,
1827 .shutdown = shutdown,
1828 .setsockopt = setsockopt,
1829 .getsockopt = getsockopt,
1830 .sendmsg = send_stream,
1831 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001832 .mmap = sock_no_mmap,
1833 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834};
1835
Florian Westphalbca65ea2008-02-07 18:18:01 -08001836static const struct net_proto_family tipc_family_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001837 .owner = THIS_MODULE,
1838 .family = AF_TIPC,
1839 .create = tipc_create
1840};
1841
1842static struct proto tipc_proto = {
1843 .name = "TIPC",
1844 .owner = THIS_MODULE,
1845 .obj_size = sizeof(struct tipc_sock)
1846};
1847
1848/**
Per Liden4323add2006-01-18 00:38:21 +01001849 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001850 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001851 * Returns 0 on success, errno otherwise
1852 */
Per Liden4323add2006-01-18 00:38:21 +01001853int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001854{
1855 int res;
1856
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001857 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001858 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001859 err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001860 goto out;
1861 }
1862
1863 res = sock_register(&tipc_family_ops);
1864 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001865 err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001866 proto_unregister(&tipc_proto);
1867 goto out;
1868 }
1869
1870 sockets_enabled = 1;
1871 out:
1872 return res;
1873}
1874
1875/**
Per Liden4323add2006-01-18 00:38:21 +01001876 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001877 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001878
Per Liden4323add2006-01-18 00:38:21 +01001879void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001880{
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