blob: 1b5fb6103dede4b9a8c9a16e0ebee9947125b394 [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>
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -080046#include <linux/mutex.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010047#include <asm/string.h>
48#include <asm/atomic.h>
49#include <net/sock.h>
50
51#include <linux/tipc.h>
Per Lidenea714cc2006-01-11 12:28:47 +010052#include <linux/tipc_config.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010053#include <net/tipc/tipc_msg.h>
54#include <net/tipc/tipc_port.h>
55
56#include "core.h"
57
58#define SS_LISTENING -1 /* socket is listening */
59#define SS_READY -2 /* socket is connectionless */
60
61#define OVERLOAD_LIMIT_BASE 5000
62
63struct tipc_sock {
64 struct sock sk;
65 struct tipc_port *p;
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -080066 struct mutex lock;
Per Lidenb97bf3f2006-01-02 19:04:38 +010067};
68
69#define tipc_sk(sk) ((struct tipc_sock*)sk)
70
71static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
72static void wakeupdispatch(struct tipc_port *tport);
73
Florian Westphalbca65ea2008-02-07 18:18:01 -080074static const struct proto_ops packet_ops;
75static const struct proto_ops stream_ops;
76static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010077
78static struct proto tipc_proto;
79
80static int sockets_enabled = 0;
81
82static atomic_t tipc_queue_size = ATOMIC_INIT(0);
83
84
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090085/*
86 * sock_lock(): Lock a port/socket pair. lock_sock() can
87 * not be used here, since the same lock must protect ports
Per Lidenb97bf3f2006-01-02 19:04:38 +010088 * with non-socket interfaces.
89 * See net.c for description of locking policy.
90 */
Sam Ravnborg05790c62006-03-20 22:37:04 -080091static void sock_lock(struct tipc_sock* tsock)
Per Lidenb97bf3f2006-01-02 19:04:38 +010092{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090093 spin_lock_bh(tsock->p->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010094}
95
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090096/*
Per Lidenb97bf3f2006-01-02 19:04:38 +010097 * sock_unlock(): Unlock a port/socket pair
98 */
Sam Ravnborg05790c62006-03-20 22:37:04 -080099static void sock_unlock(struct tipc_sock* tsock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100100{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900101 spin_unlock_bh(tsock->p->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100102}
103
104/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105 * advance_queue - discard first buffer in queue
106 * @tsock: TIPC socket
107 */
108
Sam Ravnborg05790c62006-03-20 22:37:04 -0800109static void advance_queue(struct tipc_sock *tsock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100110{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900111 sock_lock(tsock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112 buf_discard(skb_dequeue(&tsock->sk.sk_receive_queue));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900113 sock_unlock(tsock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114 atomic_dec(&tipc_queue_size);
115}
116
117/**
118 * tipc_create - create a TIPC socket
119 * @sock: pre-allocated socket structure
120 * @protocol: protocol indicator (must be 0)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900121 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122 * This routine creates and attaches a 'struct sock' to the 'struct socket',
123 * then create and attaches a TIPC port to the 'struct sock' part.
124 *
125 * Returns 0 on success, errno otherwise
126 */
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700127static int tipc_create(struct net *net, struct socket *sock, int protocol)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128{
129 struct tipc_sock *tsock;
130 struct tipc_port *port;
131 struct sock *sk;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900132 u32 ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100133
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700134 if (net != &init_net)
135 return -EAFNOSUPPORT;
136
Per Lidenb97bf3f2006-01-02 19:04:38 +0100137 if (unlikely(protocol != 0))
138 return -EPROTONOSUPPORT;
139
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800140 ref = tipc_createport_raw(NULL, &dispatch, &wakeupdispatch, TIPC_LOW_IMPORTANCE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100141 if (unlikely(!ref))
142 return -ENOMEM;
143
144 sock->state = SS_UNCONNECTED;
145
146 switch (sock->type) {
147 case SOCK_STREAM:
148 sock->ops = &stream_ops;
149 break;
150 case SOCK_SEQPACKET:
151 sock->ops = &packet_ops;
152 break;
153 case SOCK_DGRAM:
154 tipc_set_portunreliable(ref, 1);
155 /* fall through */
156 case SOCK_RDM:
157 tipc_set_portunreturnable(ref, 1);
158 sock->ops = &msg_ops;
159 sock->state = SS_READY;
160 break;
Allan Stephens49978652006-06-25 23:47:18 -0700161 default:
162 tipc_deleteport(ref);
163 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 }
165
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700166 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 if (!sk) {
168 tipc_deleteport(ref);
169 return -ENOMEM;
170 }
171
172 sock_init_data(sock, sk);
173 init_waitqueue_head(sk->sk_sleep);
174 sk->sk_rcvtimeo = 8 * HZ; /* default connect timeout = 8s */
175
176 tsock = tipc_sk(sk);
177 port = tipc_get_port(ref);
178
179 tsock->p = port;
180 port->usr_handle = tsock;
181
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800182 mutex_init(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183
184 dbg("sock_create: %x\n",tsock);
185
186 atomic_inc(&tipc_user_count);
187
188 return 0;
189}
190
191/**
192 * release - destroy a TIPC socket
193 * @sock: socket to destroy
194 *
195 * This routine cleans up any messages that are still queued on the socket.
196 * For DGRAM and RDM socket types, all queued messages are rejected.
197 * For SEQPACKET and STREAM socket types, the first message is rejected
198 * and any others are discarded. (If the first message on a STREAM socket
199 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900200 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 * NOTE: Rejected messages are not necessarily returned to the sender! They
202 * are returned or discarded according to the "destination droppable" setting
203 * specified for the message by the sender.
204 *
205 * Returns 0 on success, errno otherwise
206 */
207
208static int release(struct socket *sock)
209{
210 struct tipc_sock *tsock = tipc_sk(sock->sk);
211 struct sock *sk = sock->sk;
212 int res = TIPC_OK;
213 struct sk_buff *buf;
214
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900215 dbg("sock_delete: %x\n",tsock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 if (!tsock)
217 return 0;
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800218 mutex_lock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219 if (!sock->sk) {
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800220 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221 return 0;
222 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900223
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224 /* Reject unreceived messages, unless no longer connected */
225
226 while (sock->state != SS_DISCONNECTING) {
227 sock_lock(tsock);
228 buf = skb_dequeue(&sk->sk_receive_queue);
229 if (!buf)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800230 tsock->p->usr_handle = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231 sock_unlock(tsock);
232 if (!buf)
233 break;
234 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
235 buf_discard(buf);
236 else
237 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
238 atomic_dec(&tipc_queue_size);
239 }
240
241 /* Delete TIPC port */
242
243 res = tipc_deleteport(tsock->p->ref);
244 sock->sk = NULL;
245
246 /* Discard any remaining messages */
247
248 while ((buf = skb_dequeue(&sk->sk_receive_queue))) {
249 buf_discard(buf);
250 atomic_dec(&tipc_queue_size);
251 }
252
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800253 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254
255 sock_put(sk);
256
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900257 atomic_dec(&tipc_user_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 return res;
259}
260
261/**
262 * bind - associate or disassocate TIPC name(s) with a socket
263 * @sock: socket structure
264 * @uaddr: socket address describing name(s) and desired operation
265 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900266 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100267 * Name and name sequence binding is indicated using a positive scope value;
268 * a negative scope value unbinds the specified name. Specifying no name
269 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900270 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271 * Returns 0 on success, errno otherwise
272 */
273
274static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
275{
276 struct tipc_sock *tsock = tipc_sk(sock->sk);
277 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
278 int res;
279
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800280 if (mutex_lock_interruptible(&tsock->lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100281 return -ERESTARTSYS;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900282
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283 if (unlikely(!uaddr_len)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800284 res = tipc_withdraw(tsock->p->ref, 0, NULL);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285 goto exit;
286 }
287
288 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
289 res = -EINVAL;
290 goto exit;
291 }
292
293 if (addr->family != AF_TIPC) {
294 res = -EAFNOSUPPORT;
295 goto exit;
296 }
297 if (addr->addrtype == TIPC_ADDR_NAME)
298 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
299 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
300 res = -EAFNOSUPPORT;
301 goto exit;
302 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900303
304 if (addr->scope > 0)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305 res = tipc_publish(tsock->p->ref, addr->scope,
306 &addr->addr.nameseq);
307 else
308 res = tipc_withdraw(tsock->p->ref, -addr->scope,
309 &addr->addr.nameseq);
310exit:
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800311 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 return res;
313}
314
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900315/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316 * get_name - get port ID of socket or peer socket
317 * @sock: socket structure
318 * @uaddr: area for returned socket address
319 * @uaddr_len: area for returned length of socket address
320 * @peer: 0 to obtain socket name, 1 to obtain peer socket name
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900321 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 * Returns 0 on success, errno otherwise
323 */
324
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900325static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326 int *uaddr_len, int peer)
327{
328 struct tipc_sock *tsock = tipc_sk(sock->sk);
329 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
330 u32 res;
331
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800332 if (mutex_lock_interruptible(&tsock->lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 return -ERESTARTSYS;
334
335 *uaddr_len = sizeof(*addr);
336 addr->addrtype = TIPC_ADDR_ID;
337 addr->family = AF_TIPC;
338 addr->scope = 0;
339 if (peer)
340 res = tipc_peer(tsock->p->ref, &addr->addr.id);
341 else
342 res = tipc_ownidentity(tsock->p->ref, &addr->addr.id);
343 addr->addr.name.domain = 0;
344
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800345 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346 return res;
347}
348
349/**
350 * poll - read and possibly block on pollmask
351 * @file: file structure associated with the socket
352 * @sock: socket for which to calculate the poll bits
353 * @wait: ???
354 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700355 * Returns pollmask value
356 *
357 * COMMENTARY:
358 * It appears that the usual socket locking mechanisms are not useful here
359 * since the pollmask info is potentially out-of-date the moment this routine
360 * exits. TCP and other protocols seem to rely on higher level poll routines
361 * to handle any preventable race conditions, so TIPC will do the same ...
362 *
363 * TIPC sets the returned events as follows:
364 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
365 * or if a connection-oriented socket is does not have an active connection
366 * (i.e. a read operation will not block).
367 * b) POLLOUT is set except when a socket's connection has been terminated
368 * (i.e. a write operation will not block).
369 * c) POLLHUP is set when a socket's connection has been terminated.
370 *
371 * IMPORTANT: The fact that a read or write operation will not block does NOT
372 * imply that the operation will succeed!
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373 */
374
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900375static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376 poll_table *wait)
377{
Allan Stephens9b674e82008-03-26 16:48:21 -0700378 struct sock *sk = sock->sk;
379 u32 mask;
380
381 poll_wait(file, sk->sk_sleep, wait);
382
383 if (!skb_queue_empty(&sk->sk_receive_queue) ||
384 (sock->state == SS_UNCONNECTED) ||
385 (sock->state == SS_DISCONNECTING))
386 mask = (POLLRDNORM | POLLIN);
387 else
388 mask = 0;
389
390 if (sock->state == SS_DISCONNECTING)
391 mask |= POLLHUP;
392 else
393 mask |= POLLOUT;
394
395 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396}
397
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900398/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399 * dest_name_check - verify user is permitted to send to specified port name
400 * @dest: destination address
401 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900402 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 * Prevents restricted configuration commands from being issued by
404 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900405 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406 * Returns 0 if permission is granted, otherwise errno
407 */
408
Sam Ravnborg05790c62006-03-20 22:37:04 -0800409static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410{
411 struct tipc_cfg_msg_hdr hdr;
412
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900413 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
414 return 0;
415 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
416 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900418 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
419 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900421 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700423 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900425
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 return 0;
427}
428
429/**
430 * send_msg - send message in connectionless manner
431 * @iocb: (unused)
432 * @sock: socket structure
433 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700434 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900435 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100436 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900437 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
439 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900440 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 * Returns the number of bytes sent on success, or errno otherwise
442 */
443
444static int send_msg(struct kiocb *iocb, struct socket *sock,
445 struct msghdr *m, size_t total_len)
446{
447 struct tipc_sock *tsock = tipc_sk(sock->sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900448 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449 struct sk_buff *buf;
450 int needs_conn;
451 int res = -EINVAL;
452
453 if (unlikely(!dest))
454 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700455 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
456 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 return -EINVAL;
458
459 needs_conn = (sock->state != SS_READY);
460 if (unlikely(needs_conn)) {
461 if (sock->state == SS_LISTENING)
462 return -EPIPE;
463 if (sock->state != SS_UNCONNECTED)
464 return -EISCONN;
465 if ((tsock->p->published) ||
466 ((sock->type == SOCK_STREAM) && (total_len != 0)))
467 return -EOPNOTSUPP;
Allan Stephens33880072006-06-25 23:44:57 -0700468 if (dest->addrtype == TIPC_ADDR_NAME) {
469 tsock->p->conn_type = dest->addr.name.name.type;
470 tsock->p->conn_instance = dest->addr.name.name.instance;
471 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472 }
473
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800474 if (mutex_lock_interruptible(&tsock->lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475 return -ERESTARTSYS;
476
477 if (needs_conn) {
478
479 /* Abort any pending connection attempts (very unlikely) */
480
481 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
482 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
483 atomic_dec(&tipc_queue_size);
484 }
485
486 sock->state = SS_CONNECTING;
487 }
488
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900489 do {
490 if (dest->addrtype == TIPC_ADDR_NAME) {
491 if ((res = dest_name_check(dest, m)))
492 goto exit;
493 res = tipc_send2name(tsock->p->ref,
494 &dest->addr.name.name,
495 dest->addr.name.domain,
496 m->msg_iovlen,
497 m->msg_iov);
498 }
499 else if (dest->addrtype == TIPC_ADDR_ID) {
500 res = tipc_send2port(tsock->p->ref,
501 &dest->addr.id,
502 m->msg_iovlen,
503 m->msg_iov);
504 }
505 else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 if (needs_conn) {
507 res = -EOPNOTSUPP;
508 goto exit;
509 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900510 if ((res = dest_name_check(dest, m)))
511 goto exit;
512 res = tipc_multicast(tsock->p->ref,
513 &dest->addr.nameseq,
514 0,
515 m->msg_iovlen,
516 m->msg_iov);
517 }
518 if (likely(res != -ELINKCONG)) {
519exit:
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800520 mutex_unlock(&tsock->lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900521 return res;
522 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523 if (m->msg_flags & MSG_DONTWAIT) {
524 res = -EWOULDBLOCK;
525 goto exit;
526 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900527 if (wait_event_interruptible(*sock->sk->sk_sleep,
528 !tsock->p->congested)) {
529 res = -ERESTARTSYS;
530 goto exit;
531 }
532 } while (1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533}
534
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900535/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536 * send_packet - send a connection-oriented message
537 * @iocb: (unused)
538 * @sock: socket structure
539 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700540 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900541 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900543 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 * Returns the number of bytes sent on success, or errno otherwise
545 */
546
547static int send_packet(struct kiocb *iocb, struct socket *sock,
548 struct msghdr *m, size_t total_len)
549{
550 struct tipc_sock *tsock = tipc_sk(sock->sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900551 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 int res;
553
554 /* Handle implied connection establishment */
555
556 if (unlikely(dest))
557 return send_msg(iocb, sock, m, total_len);
558
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800559 if (mutex_lock_interruptible(&tsock->lock)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560 return -ERESTARTSYS;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900561 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100562
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900563 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700564 if (unlikely(sock->state != SS_CONNECTED)) {
565 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900566 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700567 else
568 res = -ENOTCONN;
569 goto exit;
570 }
571
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900572 res = tipc_send(tsock->p->ref, m->msg_iovlen, m->msg_iov);
573 if (likely(res != -ELINKCONG)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574exit:
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800575 mutex_unlock(&tsock->lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900576 return res;
577 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 if (m->msg_flags & MSG_DONTWAIT) {
579 res = -EWOULDBLOCK;
580 goto exit;
581 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900582 if (wait_event_interruptible(*sock->sk->sk_sleep,
583 !tsock->p->congested)) {
584 res = -ERESTARTSYS;
585 goto exit;
586 }
587 } while (1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588}
589
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900590/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 * send_stream - send stream-oriented data
592 * @iocb: (unused)
593 * @sock: socket structure
594 * @m: data to send
595 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900596 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900598 *
599 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700600 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601 */
602
603
604static int send_stream(struct kiocb *iocb, struct socket *sock,
605 struct msghdr *m, size_t total_len)
606{
Allan Stephens05646c92007-06-10 17:25:24 -0700607 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 struct msghdr my_msg;
609 struct iovec my_iov;
610 struct iovec *curr_iov;
611 int curr_iovlen;
612 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700613 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 int curr_left;
615 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700616 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900618
Allan Stephens05646c92007-06-10 17:25:24 -0700619 /* Handle special cases where there is no connection */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900621 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens05646c92007-06-10 17:25:24 -0700622 if (sock->state == SS_UNCONNECTED)
623 return send_packet(iocb, sock, m, total_len);
624 else if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900625 return -EPIPE;
626 else
627 return -ENOTCONN;
628 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629
Allan Stephenseb5959c2006-10-16 21:43:54 -0700630 if (unlikely(m->msg_name))
631 return -EISCONN;
632
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900633 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634 * Send each iovec entry using one or more messages
635 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900636 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 * (i.e. one large iovec entry), but could be improved to pass sets
638 * of small iovec entries into send_packet().
639 */
640
Allan Stephens1303e8f2006-06-25 23:46:50 -0700641 curr_iov = m->msg_iov;
642 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643 my_msg.msg_iov = &my_iov;
644 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700645 my_msg.msg_flags = m->msg_flags;
646 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700647 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648
Allan Stephens05646c92007-06-10 17:25:24 -0700649 tport = tipc_sk(sock->sk)->p;
650 hdr_size = msg_hdr_sz(&tport->phdr);
651
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652 while (curr_iovlen--) {
653 curr_start = curr_iov->iov_base;
654 curr_left = curr_iov->iov_len;
655
656 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700657 bytes_to_send = tport->max_pkt - hdr_size;
658 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
659 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
660 if (curr_left < bytes_to_send)
661 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100662 my_iov.iov_base = curr_start;
663 my_iov.iov_len = bytes_to_send;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900664 if ((res = send_packet(iocb, sock, &my_msg, 0)) < 0) {
Allan Stephens05646c92007-06-10 17:25:24 -0700665 if (bytes_sent != 0)
666 res = bytes_sent;
667 return res;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700668 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 curr_left -= bytes_to_send;
670 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700671 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 }
673
674 curr_iov++;
675 }
676
Allan Stephens1303e8f2006-06-25 23:46:50 -0700677 return bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678}
679
680/**
681 * auto_connect - complete connection setup to a remote port
682 * @sock: socket structure
683 * @tsock: TIPC-specific socket structure
684 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900685 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686 * Returns 0 on success, errno otherwise
687 */
688
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900689static int auto_connect(struct socket *sock, struct tipc_sock *tsock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690 struct tipc_msg *msg)
691{
692 struct tipc_portid peer;
693
694 if (msg_errcode(msg)) {
695 sock->state = SS_DISCONNECTING;
696 return -ECONNREFUSED;
697 }
698
699 peer.ref = msg_origport(msg);
700 peer.node = msg_orignode(msg);
701 tipc_connect2port(tsock->p->ref, &peer);
702 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
703 sock->state = SS_CONNECTED;
704 return 0;
705}
706
707/**
708 * set_orig_addr - capture sender's address for received message
709 * @m: descriptor for message info
710 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900711 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712 * Note: Address is not captured if not requested by receiver.
713 */
714
Sam Ravnborg05790c62006-03-20 22:37:04 -0800715static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900717 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100718
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900719 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100720 addr->family = AF_TIPC;
721 addr->addrtype = TIPC_ADDR_ID;
722 addr->addr.id.ref = msg_origport(msg);
723 addr->addr.id.node = msg_orignode(msg);
724 addr->addr.name.domain = 0; /* could leave uninitialized */
725 addr->scope = 0; /* could leave uninitialized */
726 m->msg_namelen = sizeof(struct sockaddr_tipc);
727 }
728}
729
730/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900731 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732 * @m: descriptor for message info
733 * @msg: received message header
734 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900735 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900737 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100738 * Returns 0 if successful, otherwise errno
739 */
740
Sam Ravnborg05790c62006-03-20 22:37:04 -0800741static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742 struct tipc_port *tport)
743{
744 u32 anc_data[3];
745 u32 err;
746 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700747 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748 int res;
749
750 if (likely(m->msg_controllen == 0))
751 return 0;
752
753 /* Optionally capture errored message object(s) */
754
755 err = msg ? msg_errcode(msg) : 0;
756 if (unlikely(err)) {
757 anc_data[0] = err;
758 anc_data[1] = msg_data_sz(msg);
Allan Stephens4b087b22006-06-25 23:47:44 -0700759 if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 return res;
761 if (anc_data[1] &&
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900762 (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763 msg_data(msg))))
764 return res;
765 }
766
767 /* Optionally capture message destination object */
768
769 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
770 switch (dest_type) {
771 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700772 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 anc_data[0] = msg_nametype(msg);
774 anc_data[1] = msg_namelower(msg);
775 anc_data[2] = msg_namelower(msg);
776 break;
777 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700778 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779 anc_data[0] = msg_nametype(msg);
780 anc_data[1] = msg_namelower(msg);
781 anc_data[2] = msg_nameupper(msg);
782 break;
783 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700784 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 anc_data[0] = tport->conn_type;
786 anc_data[1] = tport->conn_instance;
787 anc_data[2] = tport->conn_instance;
788 break;
789 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700790 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791 }
Allan Stephens3546c752006-06-25 23:45:24 -0700792 if (has_name &&
Allan Stephens4b087b22006-06-25 23:47:44 -0700793 (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794 return res;
795
796 return 0;
797}
798
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900799/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100800 * recv_msg - receive packet-oriented message
801 * @iocb: (unused)
802 * @m: descriptor for message info
803 * @buf_len: total size of user buffer area
804 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900805 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100806 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
807 * If the complete message doesn't fit in user area, truncate it.
808 *
809 * Returns size of returned message data, errno otherwise
810 */
811
812static int recv_msg(struct kiocb *iocb, struct socket *sock,
813 struct msghdr *m, size_t buf_len, int flags)
814{
815 struct tipc_sock *tsock = tipc_sk(sock->sk);
816 struct sk_buff *buf;
817 struct tipc_msg *msg;
818 unsigned int q_len;
819 unsigned int sz;
820 u32 err;
821 int res;
822
823 /* Currently doesn't support receiving into multiple iovec entries */
824
825 if (m->msg_iovlen != 1)
826 return -EOPNOTSUPP;
827
828 /* Catch invalid receive attempts */
829
830 if (unlikely(!buf_len))
831 return -EINVAL;
832
833 if (sock->type == SOCK_SEQPACKET) {
834 if (unlikely(sock->state == SS_UNCONNECTED))
835 return -ENOTCONN;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900836 if (unlikely((sock->state == SS_DISCONNECTING) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100837 (skb_queue_len(&sock->sk->sk_receive_queue) == 0)))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900838 return -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100839 }
840
841 /* Look for a message in receive queue; wait if necessary */
842
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800843 if (unlikely(mutex_lock_interruptible(&tsock->lock)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100844 return -ERESTARTSYS;
845
846restart:
847 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
848 (flags & MSG_DONTWAIT))) {
849 res = -EWOULDBLOCK;
850 goto exit;
851 }
852
853 if ((res = wait_event_interruptible(
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900854 *sock->sk->sk_sleep,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
856 (sock->state == SS_DISCONNECTING))) )) {
857 goto exit;
858 }
859
860 /* Catch attempt to receive on an already terminated connection */
861 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
862
863 if (!q_len) {
864 res = -ENOTCONN;
865 goto exit;
866 }
867
868 /* Get access to first message in receive queue */
869
870 buf = skb_peek(&sock->sk->sk_receive_queue);
871 msg = buf_msg(buf);
872 sz = msg_data_sz(msg);
873 err = msg_errcode(msg);
874
875 /* Complete connection setup for an implied connect */
876
877 if (unlikely(sock->state == SS_CONNECTING)) {
878 if ((res = auto_connect(sock, tsock, msg)))
879 goto exit;
880 }
881
882 /* Discard an empty non-errored message & try again */
883
884 if ((!sz) && (!err)) {
885 advance_queue(tsock);
886 goto restart;
887 }
888
889 /* Capture sender's address (optional) */
890
891 set_orig_addr(m, msg);
892
893 /* Capture ancillary data (optional) */
894
895 if ((res = anc_data_recv(m, msg, tsock->p)))
896 goto exit;
897
898 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900899
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900 if (!err) {
901 if (unlikely(buf_len < sz)) {
902 sz = buf_len;
903 m->msg_flags |= MSG_TRUNC;
904 }
905 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
906 sz))) {
907 res = -EFAULT;
908 goto exit;
909 }
910 res = sz;
911 } else {
912 if ((sock->state == SS_READY) ||
913 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
914 res = 0;
915 else
916 res = -ECONNRESET;
917 }
918
919 /* Consume received message (optional) */
920
921 if (likely(!(flags & MSG_PEEK))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900922 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
923 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924 advance_queue(tsock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900925 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926exit:
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800927 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928 return res;
929}
930
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900931/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100932 * recv_stream - receive stream-oriented data
933 * @iocb: (unused)
934 * @m: descriptor for message info
935 * @buf_len: total size of user buffer area
936 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900937 *
938 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +0100939 * will optionally wait for more; never truncates data.
940 *
941 * Returns size of returned message data, errno otherwise
942 */
943
944static int recv_stream(struct kiocb *iocb, struct socket *sock,
945 struct msghdr *m, size_t buf_len, int flags)
946{
947 struct tipc_sock *tsock = tipc_sk(sock->sk);
948 struct sk_buff *buf;
949 struct tipc_msg *msg;
950 unsigned int q_len;
951 unsigned int sz;
952 int sz_to_copy;
953 int sz_copied = 0;
954 int needed;
Al Viro28c4dad2006-10-10 22:45:57 +0100955 char __user *crs = m->msg_iov->iov_base;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100956 unsigned char *buf_crs;
957 u32 err;
958 int res;
959
960 /* Currently doesn't support receiving into multiple iovec entries */
961
962 if (m->msg_iovlen != 1)
963 return -EOPNOTSUPP;
964
965 /* Catch invalid receive attempts */
966
967 if (unlikely(!buf_len))
968 return -EINVAL;
969
970 if (unlikely(sock->state == SS_DISCONNECTING)) {
971 if (skb_queue_len(&sock->sk->sk_receive_queue) == 0)
972 return -ENOTCONN;
973 } else if (unlikely(sock->state != SS_CONNECTED))
974 return -ENOTCONN;
975
976 /* Look for a message in receive queue; wait if necessary */
977
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800978 if (unlikely(mutex_lock_interruptible(&tsock->lock)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100979 return -ERESTARTSYS;
980
981restart:
982 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
983 (flags & MSG_DONTWAIT))) {
Allan Stephensa3b0a5a2006-06-25 23:48:22 -0700984 res = -EWOULDBLOCK;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985 goto exit;
986 }
987
988 if ((res = wait_event_interruptible(
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900989 *sock->sk->sk_sleep,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
991 (sock->state == SS_DISCONNECTING))) )) {
992 goto exit;
993 }
994
995 /* Catch attempt to receive on an already terminated connection */
996 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
997
998 if (!q_len) {
999 res = -ENOTCONN;
1000 goto exit;
1001 }
1002
1003 /* Get access to first message in receive queue */
1004
1005 buf = skb_peek(&sock->sk->sk_receive_queue);
1006 msg = buf_msg(buf);
1007 sz = msg_data_sz(msg);
1008 err = msg_errcode(msg);
1009
1010 /* Discard an empty non-errored message & try again */
1011
1012 if ((!sz) && (!err)) {
1013 advance_queue(tsock);
1014 goto restart;
1015 }
1016
1017 /* Optionally capture sender's address & ancillary data of first msg */
1018
1019 if (sz_copied == 0) {
1020 set_orig_addr(m, msg);
1021 if ((res = anc_data_recv(m, msg, tsock->p)))
1022 goto exit;
1023 }
1024
1025 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001026
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027 if (!err) {
1028 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001029 sz = skb_tail_pointer(buf) - buf_crs;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030
1031 needed = (buf_len - sz_copied);
1032 sz_to_copy = (sz <= needed) ? sz : needed;
1033 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1034 res = -EFAULT;
1035 goto exit;
1036 }
1037 sz_copied += sz_to_copy;
1038
1039 if (sz_to_copy < sz) {
1040 if (!(flags & MSG_PEEK))
1041 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1042 goto exit;
1043 }
1044
1045 crs += sz_to_copy;
1046 } else {
1047 if (sz_copied != 0)
1048 goto exit; /* can't add error msg to valid data */
1049
1050 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1051 res = 0;
1052 else
1053 res = -ECONNRESET;
1054 }
1055
1056 /* Consume received message (optional) */
1057
1058 if (likely(!(flags & MSG_PEEK))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001059 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1060 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061 advance_queue(tsock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001062 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001063
1064 /* Loop around if more data is required */
1065
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001066 if ((sz_copied < buf_len) /* didn't get all requested data */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 && (flags & MSG_WAITALL) /* ... and need to wait for more */
1068 && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
1069 && (!err) /* ... and haven't reached a FIN */
1070 )
1071 goto restart;
1072
1073exit:
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001074 mutex_unlock(&tsock->lock);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001075 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076}
1077
1078/**
1079 * queue_overloaded - test if queue overload condition exists
1080 * @queue_size: current size of queue
1081 * @base: nominal maximum size of queue
1082 * @msg: message to be added to queue
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001083 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084 * Returns 1 if queue is currently overloaded, 0 otherwise
1085 */
1086
1087static int queue_overloaded(u32 queue_size, u32 base, struct tipc_msg *msg)
1088{
1089 u32 threshold;
1090 u32 imp = msg_importance(msg);
1091
1092 if (imp == TIPC_LOW_IMPORTANCE)
1093 threshold = base;
1094 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1095 threshold = base * 2;
1096 else if (imp == TIPC_HIGH_IMPORTANCE)
1097 threshold = base * 100;
1098 else
1099 return 0;
1100
1101 if (msg_connected(msg))
1102 threshold *= 4;
1103
1104 return (queue_size > threshold);
1105}
1106
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001107/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108 * async_disconnect - wrapper function used to disconnect port
1109 * @portref: TIPC port reference (passed as pointer-sized value)
1110 */
1111
1112static void async_disconnect(unsigned long portref)
1113{
1114 tipc_disconnect((u32)portref);
1115}
1116
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001117/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118 * dispatch - handle arriving message
1119 * @tport: TIPC port that received message
1120 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001121 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122 * Called with port locked. Must not take socket lock to avoid deadlock risk.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001123 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1125 */
1126
1127static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1128{
1129 struct tipc_msg *msg = buf_msg(buf);
1130 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1131 struct socket *sock;
1132 u32 recv_q_len;
1133
1134 /* Reject message if socket is closing */
1135
1136 if (!tsock)
1137 return TIPC_ERR_NO_PORT;
1138
1139 /* Reject message if it is wrong sort of message for socket */
1140
1141 /*
1142 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1143 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1144 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1145 */
1146 sock = tsock->sk.sk_socket;
1147 if (sock->state == SS_READY) {
1148 if (msg_connected(msg)) {
1149 msg_dbg(msg, "dispatch filter 1\n");
1150 return TIPC_ERR_NO_PORT;
1151 }
1152 } else {
1153 if (msg_mcast(msg)) {
1154 msg_dbg(msg, "dispatch filter 2\n");
1155 return TIPC_ERR_NO_PORT;
1156 }
1157 if (sock->state == SS_CONNECTED) {
1158 if (!msg_connected(msg)) {
1159 msg_dbg(msg, "dispatch filter 3\n");
1160 return TIPC_ERR_NO_PORT;
1161 }
1162 }
1163 else if (sock->state == SS_CONNECTING) {
1164 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1165 msg_dbg(msg, "dispatch filter 4\n");
1166 return TIPC_ERR_NO_PORT;
1167 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001168 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 else if (sock->state == SS_LISTENING) {
1170 if (msg_connected(msg) || msg_errcode(msg)) {
1171 msg_dbg(msg, "dispatch filter 5\n");
1172 return TIPC_ERR_NO_PORT;
1173 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001174 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175 else if (sock->state == SS_DISCONNECTING) {
1176 msg_dbg(msg, "dispatch filter 6\n");
1177 return TIPC_ERR_NO_PORT;
1178 }
1179 else /* (sock->state == SS_UNCONNECTED) */ {
1180 if (msg_connected(msg) || msg_errcode(msg)) {
1181 msg_dbg(msg, "dispatch filter 7\n");
1182 return TIPC_ERR_NO_PORT;
1183 }
1184 }
1185 }
1186
1187 /* Reject message if there isn't room to queue it */
1188
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001189 if (unlikely((u32)atomic_read(&tipc_queue_size) >
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190 OVERLOAD_LIMIT_BASE)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001191 if (queue_overloaded(atomic_read(&tipc_queue_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192 OVERLOAD_LIMIT_BASE, msg))
1193 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001194 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue);
1196 if (unlikely(recv_q_len > (OVERLOAD_LIMIT_BASE / 2))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001197 if (queue_overloaded(recv_q_len,
1198 OVERLOAD_LIMIT_BASE / 2, msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001200 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201
1202 /* Initiate connection termination for an incoming 'FIN' */
1203
1204 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1205 sock->state = SS_DISCONNECTING;
1206 /* Note: Use signal since port lock is already taken! */
Per Liden4323add2006-01-18 00:38:21 +01001207 tipc_k_signal((Handler)async_disconnect, tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 }
1209
1210 /* Enqueue message (finally!) */
1211
1212 msg_dbg(msg,"<DISP<: ");
1213 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1214 atomic_inc(&tipc_queue_size);
1215 skb_queue_tail(&sock->sk->sk_receive_queue, buf);
1216
Allan Stephenscfb0c082006-10-16 21:47:18 -07001217 if (waitqueue_active(sock->sk->sk_sleep))
1218 wake_up_interruptible(sock->sk->sk_sleep);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 return TIPC_OK;
1220}
1221
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001222/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223 * wakeupdispatch - wake up port after congestion
1224 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001225 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226 * Called with port lock on.
1227 */
1228
1229static void wakeupdispatch(struct tipc_port *tport)
1230{
1231 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1232
Allan Stephenscfb0c082006-10-16 21:47:18 -07001233 if (waitqueue_active(tsock->sk.sk_sleep))
1234 wake_up_interruptible(tsock->sk.sk_sleep);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235}
1236
1237/**
1238 * connect - establish a connection to another TIPC port
1239 * @sock: socket structure
1240 * @dest: socket address for destination port
1241 * @destlen: size of socket address data structure
1242 * @flags: (unused)
1243 *
1244 * Returns 0 on success, errno otherwise
1245 */
1246
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001247static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001248 int flags)
1249{
1250 struct tipc_sock *tsock = tipc_sk(sock->sk);
1251 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001252 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001253 struct sk_buff *buf;
1254 struct tipc_msg *msg;
1255 int res;
1256
1257 /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */
1258
1259 if (sock->state == SS_READY)
1260 return -EOPNOTSUPP;
1261
Allan Stephens51f9cc12006-06-25 23:49:06 -07001262 /* Issue Posix-compliant error code if socket is in the wrong state */
1263
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264 if (sock->state == SS_LISTENING)
1265 return -EOPNOTSUPP;
1266 if (sock->state == SS_CONNECTING)
1267 return -EALREADY;
1268 if (sock->state != SS_UNCONNECTED)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001269 return -EISCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001270
Allan Stephens51f9cc12006-06-25 23:49:06 -07001271 /*
1272 * Reject connection attempt using multicast address
1273 *
1274 * Note: send_msg() validates the rest of the address fields,
1275 * so there's no need to do it here
1276 */
1277
1278 if (dst->addrtype == TIPC_ADDR_MCAST)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001279 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001280
1281 /* Send a 'SYN-' to destination */
1282
1283 m.msg_name = dest;
Allan Stephens51f9cc12006-06-25 23:49:06 -07001284 m.msg_namelen = destlen;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001285 if ((res = send_msg(NULL, sock, &m, 0)) < 0) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001286 sock->state = SS_DISCONNECTING;
1287 return res;
1288 }
1289
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001290 if (mutex_lock_interruptible(&tsock->lock))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001291 return -ERESTARTSYS;
1292
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293 /* Wait for destination's 'ACK' response */
1294
1295 res = wait_event_interruptible_timeout(*sock->sk->sk_sleep,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001296 skb_queue_len(&sock->sk->sk_receive_queue),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001297 sock->sk->sk_rcvtimeo);
1298 buf = skb_peek(&sock->sk->sk_receive_queue);
1299 if (res > 0) {
1300 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001301 res = auto_connect(sock, tsock, msg);
1302 if (!res) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001303 if (!msg_data_sz(msg))
1304 advance_queue(tsock);
1305 }
1306 } else {
1307 if (res == 0) {
1308 res = -ETIMEDOUT;
1309 } else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001310 { /* leave "res" unchanged */ }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001311 sock->state = SS_DISCONNECTING;
1312 }
1313
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001314 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315 return res;
1316}
1317
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001318/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319 * listen - allow socket to listen for incoming connections
1320 * @sock: socket structure
1321 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001322 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 * Returns 0 on success, errno otherwise
1324 */
1325
1326static int listen(struct socket *sock, int len)
1327{
1328 /* REQUIRES SOCKET LOCKING OF SOME SORT? */
1329
1330 if (sock->state == SS_READY)
1331 return -EOPNOTSUPP;
1332 if (sock->state != SS_UNCONNECTED)
1333 return -EINVAL;
1334 sock->state = SS_LISTENING;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001335 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001336}
1337
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001338/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 * accept - wait for connection request
1340 * @sock: listening socket
1341 * @newsock: new socket that is to be connected
1342 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001343 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 * Returns 0 on success, errno otherwise
1345 */
1346
1347static int accept(struct socket *sock, struct socket *newsock, int flags)
1348{
1349 struct tipc_sock *tsock = tipc_sk(sock->sk);
1350 struct sk_buff *buf;
1351 int res = -EFAULT;
1352
1353 if (sock->state == SS_READY)
1354 return -EOPNOTSUPP;
1355 if (sock->state != SS_LISTENING)
1356 return -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001357
1358 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359 (flags & O_NONBLOCK)))
1360 return -EWOULDBLOCK;
1361
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001362 if (mutex_lock_interruptible(&tsock->lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363 return -ERESTARTSYS;
1364
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001365 if (wait_event_interruptible(*sock->sk->sk_sleep,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366 skb_queue_len(&sock->sk->sk_receive_queue))) {
1367 res = -ERESTARTSYS;
1368 goto exit;
1369 }
1370 buf = skb_peek(&sock->sk->sk_receive_queue);
1371
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001372 res = tipc_create(sock_net(sock->sk), newsock, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001373 if (!res) {
1374 struct tipc_sock *new_tsock = tipc_sk(newsock->sk);
1375 struct tipc_portid id;
1376 struct tipc_msg *msg = buf_msg(buf);
1377 u32 new_ref = new_tsock->p->ref;
1378
1379 id.ref = msg_origport(msg);
1380 id.node = msg_orignode(msg);
1381 tipc_connect2port(new_ref, &id);
1382 newsock->state = SS_CONNECTED;
1383
1384 tipc_set_portimportance(new_ref, msg_importance(msg));
1385 if (msg_named(msg)) {
1386 new_tsock->p->conn_type = msg_nametype(msg);
1387 new_tsock->p->conn_instance = msg_nameinst(msg);
1388 }
1389
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001390 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001391 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1392 * Respond to 'SYN+' by queuing it on new socket.
1393 */
1394
1395 msg_dbg(msg,"<ACC<: ");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001396 if (!msg_data_sz(msg)) {
1397 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001399 send_packet(NULL, newsock, &m, 0);
1400 advance_queue(tsock);
1401 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001402 sock_lock(tsock);
1403 skb_dequeue(&sock->sk->sk_receive_queue);
1404 sock_unlock(tsock);
1405 skb_queue_head(&newsock->sk->sk_receive_queue, buf);
1406 }
1407 }
1408exit:
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001409 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001410 return res;
1411}
1412
1413/**
1414 * shutdown - shutdown socket connection
1415 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001416 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417 *
1418 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001419 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001420 * Returns 0 on success, errno otherwise
1421 */
1422
1423static int shutdown(struct socket *sock, int how)
1424{
1425 struct tipc_sock* tsock = tipc_sk(sock->sk);
1426 struct sk_buff *buf;
1427 int res;
1428
Allan Stephense247a8f2008-03-06 15:05:38 -08001429 if (how != SHUT_RDWR)
1430 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001431
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001432 if (mutex_lock_interruptible(&tsock->lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 return -ERESTARTSYS;
1434
1435 sock_lock(tsock);
1436
1437 switch (sock->state) {
1438 case SS_CONNECTED:
1439
1440 /* Send 'FIN+' or 'FIN-' message to peer */
1441
1442 sock_unlock(tsock);
1443restart:
1444 if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1445 atomic_dec(&tipc_queue_size);
1446 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1447 buf_discard(buf);
1448 goto restart;
1449 }
1450 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
1451 }
1452 else {
1453 tipc_shutdown(tsock->p->ref);
1454 }
1455 sock_lock(tsock);
1456
1457 /* fall through */
1458
1459 case SS_DISCONNECTING:
1460
1461 /* Discard any unreceived messages */
1462
1463 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1464 atomic_dec(&tipc_queue_size);
1465 buf_discard(buf);
1466 }
1467 tsock->p->conn_unacked = 0;
1468
1469 /* fall through */
1470
1471 case SS_CONNECTING:
1472 sock->state = SS_DISCONNECTING;
1473 res = 0;
1474 break;
1475
1476 default:
1477 res = -ENOTCONN;
1478 }
1479
1480 sock_unlock(tsock);
1481
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001482 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001483 return res;
1484}
1485
1486/**
1487 * setsockopt - set socket option
1488 * @sock: socket structure
1489 * @lvl: option level
1490 * @opt: option identifier
1491 * @ov: pointer to new option value
1492 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001493 *
1494 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001495 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001496 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001497 * Returns 0 on success, errno otherwise
1498 */
1499
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001500static int setsockopt(struct socket *sock,
Allan Stephense9024f0f2006-06-25 23:43:57 -07001501 int lvl, int opt, char __user *ov, int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001502{
1503 struct tipc_sock *tsock = tipc_sk(sock->sk);
1504 u32 value;
1505 int res;
1506
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001507 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1508 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001509 if (lvl != SOL_TIPC)
1510 return -ENOPROTOOPT;
1511 if (ol < sizeof(value))
1512 return -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001513 if ((res = get_user(value, (u32 __user *)ov)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001514 return res;
1515
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001516 if (mutex_lock_interruptible(&tsock->lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001517 return -ERESTARTSYS;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001518
Per Lidenb97bf3f2006-01-02 19:04:38 +01001519 switch (opt) {
1520 case TIPC_IMPORTANCE:
1521 res = tipc_set_portimportance(tsock->p->ref, value);
1522 break;
1523 case TIPC_SRC_DROPPABLE:
1524 if (sock->type != SOCK_STREAM)
1525 res = tipc_set_portunreliable(tsock->p->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001526 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001527 res = -ENOPROTOOPT;
1528 break;
1529 case TIPC_DEST_DROPPABLE:
1530 res = tipc_set_portunreturnable(tsock->p->ref, value);
1531 break;
1532 case TIPC_CONN_TIMEOUT:
1533 sock->sk->sk_rcvtimeo = (value * HZ / 1000);
1534 break;
1535 default:
1536 res = -EINVAL;
1537 }
1538
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001539 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001540 return res;
1541}
1542
1543/**
1544 * getsockopt - get socket option
1545 * @sock: socket structure
1546 * @lvl: option level
1547 * @opt: option identifier
1548 * @ov: receptacle for option value
1549 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001550 *
1551 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001552 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001553 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001554 * Returns 0 on success, errno otherwise
1555 */
1556
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001557static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001558 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001559{
1560 struct tipc_sock *tsock = tipc_sk(sock->sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001561 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001562 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001563 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001564
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001565 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1566 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001567 if (lvl != SOL_TIPC)
1568 return -ENOPROTOOPT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001569 if ((res = get_user(len, ol)))
1570 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001571
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001572 if (mutex_lock_interruptible(&tsock->lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001573 return -ERESTARTSYS;
1574
1575 switch (opt) {
1576 case TIPC_IMPORTANCE:
1577 res = tipc_portimportance(tsock->p->ref, &value);
1578 break;
1579 case TIPC_SRC_DROPPABLE:
1580 res = tipc_portunreliable(tsock->p->ref, &value);
1581 break;
1582 case TIPC_DEST_DROPPABLE:
1583 res = tipc_portunreturnable(tsock->p->ref, &value);
1584 break;
1585 case TIPC_CONN_TIMEOUT:
1586 value = (sock->sk->sk_rcvtimeo * 1000) / HZ;
1587 break;
1588 default:
1589 res = -EINVAL;
1590 }
1591
1592 if (res) {
1593 /* "get" failed */
1594 }
1595 else if (len < sizeof(value)) {
1596 res = -EINVAL;
1597 }
1598 else if ((res = copy_to_user(ov, &value, sizeof(value)))) {
1599 /* couldn't return value */
1600 }
1601 else {
1602 res = put_user(sizeof(value), ol);
1603 }
1604
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001605 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606 return res;
1607}
1608
1609/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001610 * Protocol switches for the various types of TIPC sockets
1611 */
1612
Florian Westphalbca65ea2008-02-07 18:18:01 -08001613static const struct proto_ops msg_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001614 .owner = THIS_MODULE,
1615 .family = AF_TIPC,
1616 .release = release,
1617 .bind = bind,
1618 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001619 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001620 .accept = accept,
1621 .getname = get_name,
1622 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001623 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001624 .listen = listen,
1625 .shutdown = shutdown,
1626 .setsockopt = setsockopt,
1627 .getsockopt = getsockopt,
1628 .sendmsg = send_msg,
1629 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001630 .mmap = sock_no_mmap,
1631 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001632};
1633
Florian Westphalbca65ea2008-02-07 18:18:01 -08001634static const struct proto_ops packet_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001635 .owner = THIS_MODULE,
1636 .family = AF_TIPC,
1637 .release = release,
1638 .bind = bind,
1639 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001640 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001641 .accept = accept,
1642 .getname = get_name,
1643 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001644 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001645 .listen = listen,
1646 .shutdown = shutdown,
1647 .setsockopt = setsockopt,
1648 .getsockopt = getsockopt,
1649 .sendmsg = send_packet,
1650 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001651 .mmap = sock_no_mmap,
1652 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001653};
1654
Florian Westphalbca65ea2008-02-07 18:18:01 -08001655static const struct proto_ops stream_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656 .owner = THIS_MODULE,
1657 .family = AF_TIPC,
1658 .release = release,
1659 .bind = bind,
1660 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001661 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001662 .accept = accept,
1663 .getname = get_name,
1664 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001665 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666 .listen = listen,
1667 .shutdown = shutdown,
1668 .setsockopt = setsockopt,
1669 .getsockopt = getsockopt,
1670 .sendmsg = send_stream,
1671 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001672 .mmap = sock_no_mmap,
1673 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674};
1675
Florian Westphalbca65ea2008-02-07 18:18:01 -08001676static const struct net_proto_family tipc_family_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001677 .owner = THIS_MODULE,
1678 .family = AF_TIPC,
1679 .create = tipc_create
1680};
1681
1682static struct proto tipc_proto = {
1683 .name = "TIPC",
1684 .owner = THIS_MODULE,
1685 .obj_size = sizeof(struct tipc_sock)
1686};
1687
1688/**
Per Liden4323add2006-01-18 00:38:21 +01001689 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001690 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001691 * Returns 0 on success, errno otherwise
1692 */
Per Liden4323add2006-01-18 00:38:21 +01001693int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001694{
1695 int res;
1696
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001697 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001698 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001699 err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001700 goto out;
1701 }
1702
1703 res = sock_register(&tipc_family_ops);
1704 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001705 err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001706 proto_unregister(&tipc_proto);
1707 goto out;
1708 }
1709
1710 sockets_enabled = 1;
1711 out:
1712 return res;
1713}
1714
1715/**
Per Liden4323add2006-01-18 00:38:21 +01001716 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001717 */
Per Liden4323add2006-01-18 00:38:21 +01001718void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719{
1720 if (!sockets_enabled)
1721 return;
1722
1723 sockets_enabled = 0;
1724 sock_unregister(tipc_family_ops.family);
1725 proto_unregister(&tipc_proto);
1726}
1727