blob: d3f9c2d87b0dd7b4535e9c3c8d917ea1907af5aa [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);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 sk->sk_rcvtimeo = 8 * HZ; /* default connect timeout = 8s */
174
175 tsock = tipc_sk(sk);
176 port = tipc_get_port(ref);
177
178 tsock->p = port;
179 port->usr_handle = tsock;
180
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800181 mutex_init(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182
183 dbg("sock_create: %x\n",tsock);
184
185 atomic_inc(&tipc_user_count);
186
187 return 0;
188}
189
190/**
191 * release - destroy a TIPC socket
192 * @sock: socket to destroy
193 *
194 * This routine cleans up any messages that are still queued on the socket.
195 * For DGRAM and RDM socket types, all queued messages are rejected.
196 * For SEQPACKET and STREAM socket types, the first message is rejected
197 * and any others are discarded. (If the first message on a STREAM socket
198 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900199 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100200 * NOTE: Rejected messages are not necessarily returned to the sender! They
201 * are returned or discarded according to the "destination droppable" setting
202 * specified for the message by the sender.
203 *
204 * Returns 0 on success, errno otherwise
205 */
206
207static int release(struct socket *sock)
208{
209 struct tipc_sock *tsock = tipc_sk(sock->sk);
210 struct sock *sk = sock->sk;
211 int res = TIPC_OK;
212 struct sk_buff *buf;
213
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900214 dbg("sock_delete: %x\n",tsock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215 if (!tsock)
216 return 0;
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800217 mutex_lock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218 if (!sock->sk) {
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800219 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 return 0;
221 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900222
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223 /* Reject unreceived messages, unless no longer connected */
224
225 while (sock->state != SS_DISCONNECTING) {
226 sock_lock(tsock);
227 buf = skb_dequeue(&sk->sk_receive_queue);
228 if (!buf)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800229 tsock->p->usr_handle = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230 sock_unlock(tsock);
231 if (!buf)
232 break;
233 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
234 buf_discard(buf);
235 else
236 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
237 atomic_dec(&tipc_queue_size);
238 }
239
240 /* Delete TIPC port */
241
242 res = tipc_deleteport(tsock->p->ref);
243 sock->sk = NULL;
244
245 /* Discard any remaining messages */
246
247 while ((buf = skb_dequeue(&sk->sk_receive_queue))) {
248 buf_discard(buf);
249 atomic_dec(&tipc_queue_size);
250 }
251
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800252 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253
254 sock_put(sk);
255
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900256 atomic_dec(&tipc_user_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257 return res;
258}
259
260/**
261 * bind - associate or disassocate TIPC name(s) with a socket
262 * @sock: socket structure
263 * @uaddr: socket address describing name(s) and desired operation
264 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900265 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 * Name and name sequence binding is indicated using a positive scope value;
267 * a negative scope value unbinds the specified name. Specifying no name
268 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900269 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270 * Returns 0 on success, errno otherwise
271 */
272
273static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
274{
275 struct tipc_sock *tsock = tipc_sk(sock->sk);
276 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
277 int res;
278
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800279 if (mutex_lock_interruptible(&tsock->lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280 return -ERESTARTSYS;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900281
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282 if (unlikely(!uaddr_len)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800283 res = tipc_withdraw(tsock->p->ref, 0, NULL);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284 goto exit;
285 }
286
287 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
288 res = -EINVAL;
289 goto exit;
290 }
291
292 if (addr->family != AF_TIPC) {
293 res = -EAFNOSUPPORT;
294 goto exit;
295 }
296 if (addr->addrtype == TIPC_ADDR_NAME)
297 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
298 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
299 res = -EAFNOSUPPORT;
300 goto exit;
301 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900302
303 if (addr->scope > 0)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 res = tipc_publish(tsock->p->ref, addr->scope,
305 &addr->addr.nameseq);
306 else
307 res = tipc_withdraw(tsock->p->ref, -addr->scope,
308 &addr->addr.nameseq);
309exit:
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800310 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311 return res;
312}
313
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900314/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315 * get_name - get port ID of socket or peer socket
316 * @sock: socket structure
317 * @uaddr: area for returned socket address
318 * @uaddr_len: area for returned length of socket address
319 * @peer: 0 to obtain socket name, 1 to obtain peer socket name
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900320 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 * Returns 0 on success, errno otherwise
322 */
323
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900324static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 int *uaddr_len, int peer)
326{
327 struct tipc_sock *tsock = tipc_sk(sock->sk);
328 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
329 u32 res;
330
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800331 if (mutex_lock_interruptible(&tsock->lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332 return -ERESTARTSYS;
333
334 *uaddr_len = sizeof(*addr);
335 addr->addrtype = TIPC_ADDR_ID;
336 addr->family = AF_TIPC;
337 addr->scope = 0;
338 if (peer)
339 res = tipc_peer(tsock->p->ref, &addr->addr.id);
340 else
341 res = tipc_ownidentity(tsock->p->ref, &addr->addr.id);
342 addr->addr.name.domain = 0;
343
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800344 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 return res;
346}
347
348/**
349 * poll - read and possibly block on pollmask
350 * @file: file structure associated with the socket
351 * @sock: socket for which to calculate the poll bits
352 * @wait: ???
353 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700354 * Returns pollmask value
355 *
356 * COMMENTARY:
357 * It appears that the usual socket locking mechanisms are not useful here
358 * since the pollmask info is potentially out-of-date the moment this routine
359 * exits. TCP and other protocols seem to rely on higher level poll routines
360 * to handle any preventable race conditions, so TIPC will do the same ...
361 *
362 * TIPC sets the returned events as follows:
363 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
364 * or if a connection-oriented socket is does not have an active connection
365 * (i.e. a read operation will not block).
366 * b) POLLOUT is set except when a socket's connection has been terminated
367 * (i.e. a write operation will not block).
368 * c) POLLHUP is set when a socket's connection has been terminated.
369 *
370 * IMPORTANT: The fact that a read or write operation will not block does NOT
371 * imply that the operation will succeed!
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372 */
373
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900374static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375 poll_table *wait)
376{
Allan Stephens9b674e82008-03-26 16:48:21 -0700377 struct sock *sk = sock->sk;
378 u32 mask;
379
380 poll_wait(file, sk->sk_sleep, wait);
381
382 if (!skb_queue_empty(&sk->sk_receive_queue) ||
383 (sock->state == SS_UNCONNECTED) ||
384 (sock->state == SS_DISCONNECTING))
385 mask = (POLLRDNORM | POLLIN);
386 else
387 mask = 0;
388
389 if (sock->state == SS_DISCONNECTING)
390 mask |= POLLHUP;
391 else
392 mask |= POLLOUT;
393
394 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395}
396
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900397/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398 * dest_name_check - verify user is permitted to send to specified port name
399 * @dest: destination address
400 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900401 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402 * Prevents restricted configuration commands from being issued by
403 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900404 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405 * Returns 0 if permission is granted, otherwise errno
406 */
407
Sam Ravnborg05790c62006-03-20 22:37:04 -0800408static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409{
410 struct tipc_cfg_msg_hdr hdr;
411
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900412 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
413 return 0;
414 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
415 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900417 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
418 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900420 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700422 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900424
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425 return 0;
426}
427
428/**
429 * send_msg - send message in connectionless manner
430 * @iocb: (unused)
431 * @sock: socket structure
432 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700433 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900434 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900436 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
438 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900439 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440 * Returns the number of bytes sent on success, or errno otherwise
441 */
442
443static int send_msg(struct kiocb *iocb, struct socket *sock,
444 struct msghdr *m, size_t total_len)
445{
446 struct tipc_sock *tsock = tipc_sk(sock->sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900447 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448 struct sk_buff *buf;
449 int needs_conn;
450 int res = -EINVAL;
451
452 if (unlikely(!dest))
453 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700454 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
455 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456 return -EINVAL;
457
458 needs_conn = (sock->state != SS_READY);
459 if (unlikely(needs_conn)) {
460 if (sock->state == SS_LISTENING)
461 return -EPIPE;
462 if (sock->state != SS_UNCONNECTED)
463 return -EISCONN;
464 if ((tsock->p->published) ||
465 ((sock->type == SOCK_STREAM) && (total_len != 0)))
466 return -EOPNOTSUPP;
Allan Stephens33880072006-06-25 23:44:57 -0700467 if (dest->addrtype == TIPC_ADDR_NAME) {
468 tsock->p->conn_type = dest->addr.name.name.type;
469 tsock->p->conn_instance = dest->addr.name.name.instance;
470 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471 }
472
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800473 if (mutex_lock_interruptible(&tsock->lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474 return -ERESTARTSYS;
475
476 if (needs_conn) {
477
478 /* Abort any pending connection attempts (very unlikely) */
479
480 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
481 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
482 atomic_dec(&tipc_queue_size);
483 }
484
485 sock->state = SS_CONNECTING;
486 }
487
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900488 do {
489 if (dest->addrtype == TIPC_ADDR_NAME) {
490 if ((res = dest_name_check(dest, m)))
491 goto exit;
492 res = tipc_send2name(tsock->p->ref,
493 &dest->addr.name.name,
494 dest->addr.name.domain,
495 m->msg_iovlen,
496 m->msg_iov);
497 }
498 else if (dest->addrtype == TIPC_ADDR_ID) {
499 res = tipc_send2port(tsock->p->ref,
500 &dest->addr.id,
501 m->msg_iovlen,
502 m->msg_iov);
503 }
504 else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 if (needs_conn) {
506 res = -EOPNOTSUPP;
507 goto exit;
508 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900509 if ((res = dest_name_check(dest, m)))
510 goto exit;
511 res = tipc_multicast(tsock->p->ref,
512 &dest->addr.nameseq,
513 0,
514 m->msg_iovlen,
515 m->msg_iov);
516 }
517 if (likely(res != -ELINKCONG)) {
518exit:
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800519 mutex_unlock(&tsock->lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900520 return res;
521 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522 if (m->msg_flags & MSG_DONTWAIT) {
523 res = -EWOULDBLOCK;
524 goto exit;
525 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900526 if (wait_event_interruptible(*sock->sk->sk_sleep,
527 !tsock->p->congested)) {
528 res = -ERESTARTSYS;
529 goto exit;
530 }
531 } while (1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100532}
533
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900534/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 * send_packet - send a connection-oriented message
536 * @iocb: (unused)
537 * @sock: socket structure
538 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700539 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900540 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900542 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 * Returns the number of bytes sent on success, or errno otherwise
544 */
545
546static int send_packet(struct kiocb *iocb, struct socket *sock,
547 struct msghdr *m, size_t total_len)
548{
549 struct tipc_sock *tsock = tipc_sk(sock->sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900550 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 int res;
552
553 /* Handle implied connection establishment */
554
555 if (unlikely(dest))
556 return send_msg(iocb, sock, m, total_len);
557
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800558 if (mutex_lock_interruptible(&tsock->lock)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 return -ERESTARTSYS;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900560 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900562 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700563 if (unlikely(sock->state != SS_CONNECTED)) {
564 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900565 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700566 else
567 res = -ENOTCONN;
568 goto exit;
569 }
570
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900571 res = tipc_send(tsock->p->ref, m->msg_iovlen, m->msg_iov);
572 if (likely(res != -ELINKCONG)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573exit:
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800574 mutex_unlock(&tsock->lock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900575 return res;
576 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577 if (m->msg_flags & MSG_DONTWAIT) {
578 res = -EWOULDBLOCK;
579 goto exit;
580 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900581 if (wait_event_interruptible(*sock->sk->sk_sleep,
582 !tsock->p->congested)) {
583 res = -ERESTARTSYS;
584 goto exit;
585 }
586 } while (1);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587}
588
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900589/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590 * send_stream - send stream-oriented data
591 * @iocb: (unused)
592 * @sock: socket structure
593 * @m: data to send
594 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900595 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900597 *
598 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700599 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 */
601
602
603static int send_stream(struct kiocb *iocb, struct socket *sock,
604 struct msghdr *m, size_t total_len)
605{
Allan Stephens05646c92007-06-10 17:25:24 -0700606 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 struct msghdr my_msg;
608 struct iovec my_iov;
609 struct iovec *curr_iov;
610 int curr_iovlen;
611 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700612 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 int curr_left;
614 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700615 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900617
Allan Stephens05646c92007-06-10 17:25:24 -0700618 /* Handle special cases where there is no connection */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900620 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens05646c92007-06-10 17:25:24 -0700621 if (sock->state == SS_UNCONNECTED)
622 return send_packet(iocb, sock, m, total_len);
623 else if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900624 return -EPIPE;
625 else
626 return -ENOTCONN;
627 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628
Allan Stephenseb5959c2006-10-16 21:43:54 -0700629 if (unlikely(m->msg_name))
630 return -EISCONN;
631
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900632 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 * Send each iovec entry using one or more messages
634 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900635 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 * (i.e. one large iovec entry), but could be improved to pass sets
637 * of small iovec entries into send_packet().
638 */
639
Allan Stephens1303e8f2006-06-25 23:46:50 -0700640 curr_iov = m->msg_iov;
641 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 my_msg.msg_iov = &my_iov;
643 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700644 my_msg.msg_flags = m->msg_flags;
645 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700646 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647
Allan Stephens05646c92007-06-10 17:25:24 -0700648 tport = tipc_sk(sock->sk)->p;
649 hdr_size = msg_hdr_sz(&tport->phdr);
650
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 while (curr_iovlen--) {
652 curr_start = curr_iov->iov_base;
653 curr_left = curr_iov->iov_len;
654
655 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700656 bytes_to_send = tport->max_pkt - hdr_size;
657 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
658 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
659 if (curr_left < bytes_to_send)
660 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661 my_iov.iov_base = curr_start;
662 my_iov.iov_len = bytes_to_send;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900663 if ((res = send_packet(iocb, sock, &my_msg, 0)) < 0) {
Allan Stephens05646c92007-06-10 17:25:24 -0700664 if (bytes_sent != 0)
665 res = bytes_sent;
666 return res;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700667 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668 curr_left -= bytes_to_send;
669 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700670 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671 }
672
673 curr_iov++;
674 }
675
Allan Stephens1303e8f2006-06-25 23:46:50 -0700676 return bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100677}
678
679/**
680 * auto_connect - complete connection setup to a remote port
681 * @sock: socket structure
682 * @tsock: TIPC-specific socket structure
683 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900684 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685 * Returns 0 on success, errno otherwise
686 */
687
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900688static int auto_connect(struct socket *sock, struct tipc_sock *tsock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689 struct tipc_msg *msg)
690{
691 struct tipc_portid peer;
692
693 if (msg_errcode(msg)) {
694 sock->state = SS_DISCONNECTING;
695 return -ECONNREFUSED;
696 }
697
698 peer.ref = msg_origport(msg);
699 peer.node = msg_orignode(msg);
700 tipc_connect2port(tsock->p->ref, &peer);
701 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
702 sock->state = SS_CONNECTED;
703 return 0;
704}
705
706/**
707 * set_orig_addr - capture sender's address for received message
708 * @m: descriptor for message info
709 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900710 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711 * Note: Address is not captured if not requested by receiver.
712 */
713
Sam Ravnborg05790c62006-03-20 22:37:04 -0800714static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100715{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900716 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100717
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900718 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719 addr->family = AF_TIPC;
720 addr->addrtype = TIPC_ADDR_ID;
721 addr->addr.id.ref = msg_origport(msg);
722 addr->addr.id.node = msg_orignode(msg);
723 addr->addr.name.domain = 0; /* could leave uninitialized */
724 addr->scope = 0; /* could leave uninitialized */
725 m->msg_namelen = sizeof(struct sockaddr_tipc);
726 }
727}
728
729/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900730 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731 * @m: descriptor for message info
732 * @msg: received message header
733 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900734 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900736 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737 * Returns 0 if successful, otherwise errno
738 */
739
Sam Ravnborg05790c62006-03-20 22:37:04 -0800740static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741 struct tipc_port *tport)
742{
743 u32 anc_data[3];
744 u32 err;
745 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700746 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747 int res;
748
749 if (likely(m->msg_controllen == 0))
750 return 0;
751
752 /* Optionally capture errored message object(s) */
753
754 err = msg ? msg_errcode(msg) : 0;
755 if (unlikely(err)) {
756 anc_data[0] = err;
757 anc_data[1] = msg_data_sz(msg);
Allan Stephens4b087b22006-06-25 23:47:44 -0700758 if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100759 return res;
760 if (anc_data[1] &&
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900761 (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
Per Lidenb97bf3f2006-01-02 19:04:38 +0100762 msg_data(msg))))
763 return res;
764 }
765
766 /* Optionally capture message destination object */
767
768 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
769 switch (dest_type) {
770 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700771 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100772 anc_data[0] = msg_nametype(msg);
773 anc_data[1] = msg_namelower(msg);
774 anc_data[2] = msg_namelower(msg);
775 break;
776 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700777 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 anc_data[0] = msg_nametype(msg);
779 anc_data[1] = msg_namelower(msg);
780 anc_data[2] = msg_nameupper(msg);
781 break;
782 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700783 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100784 anc_data[0] = tport->conn_type;
785 anc_data[1] = tport->conn_instance;
786 anc_data[2] = tport->conn_instance;
787 break;
788 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700789 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100790 }
Allan Stephens3546c752006-06-25 23:45:24 -0700791 if (has_name &&
Allan Stephens4b087b22006-06-25 23:47:44 -0700792 (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793 return res;
794
795 return 0;
796}
797
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900798/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799 * recv_msg - receive packet-oriented message
800 * @iocb: (unused)
801 * @m: descriptor for message info
802 * @buf_len: total size of user buffer area
803 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900804 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
806 * If the complete message doesn't fit in user area, truncate it.
807 *
808 * Returns size of returned message data, errno otherwise
809 */
810
811static int recv_msg(struct kiocb *iocb, struct socket *sock,
812 struct msghdr *m, size_t buf_len, int flags)
813{
814 struct tipc_sock *tsock = tipc_sk(sock->sk);
815 struct sk_buff *buf;
816 struct tipc_msg *msg;
817 unsigned int q_len;
818 unsigned int sz;
819 u32 err;
820 int res;
821
822 /* Currently doesn't support receiving into multiple iovec entries */
823
824 if (m->msg_iovlen != 1)
825 return -EOPNOTSUPP;
826
827 /* Catch invalid receive attempts */
828
829 if (unlikely(!buf_len))
830 return -EINVAL;
831
832 if (sock->type == SOCK_SEQPACKET) {
833 if (unlikely(sock->state == SS_UNCONNECTED))
834 return -ENOTCONN;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900835 if (unlikely((sock->state == SS_DISCONNECTING) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836 (skb_queue_len(&sock->sk->sk_receive_queue) == 0)))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900837 return -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100838 }
839
840 /* Look for a message in receive queue; wait if necessary */
841
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800842 if (unlikely(mutex_lock_interruptible(&tsock->lock)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843 return -ERESTARTSYS;
844
845restart:
846 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
847 (flags & MSG_DONTWAIT))) {
848 res = -EWOULDBLOCK;
849 goto exit;
850 }
851
852 if ((res = wait_event_interruptible(
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900853 *sock->sk->sk_sleep,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
855 (sock->state == SS_DISCONNECTING))) )) {
856 goto exit;
857 }
858
859 /* Catch attempt to receive on an already terminated connection */
860 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
861
862 if (!q_len) {
863 res = -ENOTCONN;
864 goto exit;
865 }
866
867 /* Get access to first message in receive queue */
868
869 buf = skb_peek(&sock->sk->sk_receive_queue);
870 msg = buf_msg(buf);
871 sz = msg_data_sz(msg);
872 err = msg_errcode(msg);
873
874 /* Complete connection setup for an implied connect */
875
876 if (unlikely(sock->state == SS_CONNECTING)) {
877 if ((res = auto_connect(sock, tsock, msg)))
878 goto exit;
879 }
880
881 /* Discard an empty non-errored message & try again */
882
883 if ((!sz) && (!err)) {
884 advance_queue(tsock);
885 goto restart;
886 }
887
888 /* Capture sender's address (optional) */
889
890 set_orig_addr(m, msg);
891
892 /* Capture ancillary data (optional) */
893
894 if ((res = anc_data_recv(m, msg, tsock->p)))
895 goto exit;
896
897 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900898
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899 if (!err) {
900 if (unlikely(buf_len < sz)) {
901 sz = buf_len;
902 m->msg_flags |= MSG_TRUNC;
903 }
904 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
905 sz))) {
906 res = -EFAULT;
907 goto exit;
908 }
909 res = sz;
910 } else {
911 if ((sock->state == SS_READY) ||
912 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
913 res = 0;
914 else
915 res = -ECONNRESET;
916 }
917
918 /* Consume received message (optional) */
919
920 if (likely(!(flags & MSG_PEEK))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900921 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
922 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100923 advance_queue(tsock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900924 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100925exit:
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800926 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927 return res;
928}
929
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900930/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931 * recv_stream - receive stream-oriented data
932 * @iocb: (unused)
933 * @m: descriptor for message info
934 * @buf_len: total size of user buffer area
935 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900936 *
937 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +0100938 * will optionally wait for more; never truncates data.
939 *
940 * Returns size of returned message data, errno otherwise
941 */
942
943static int recv_stream(struct kiocb *iocb, struct socket *sock,
944 struct msghdr *m, size_t buf_len, int flags)
945{
946 struct tipc_sock *tsock = tipc_sk(sock->sk);
947 struct sk_buff *buf;
948 struct tipc_msg *msg;
949 unsigned int q_len;
950 unsigned int sz;
951 int sz_to_copy;
952 int sz_copied = 0;
953 int needed;
Al Viro28c4dad2006-10-10 22:45:57 +0100954 char __user *crs = m->msg_iov->iov_base;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955 unsigned char *buf_crs;
956 u32 err;
957 int res;
958
959 /* Currently doesn't support receiving into multiple iovec entries */
960
961 if (m->msg_iovlen != 1)
962 return -EOPNOTSUPP;
963
964 /* Catch invalid receive attempts */
965
966 if (unlikely(!buf_len))
967 return -EINVAL;
968
969 if (unlikely(sock->state == SS_DISCONNECTING)) {
970 if (skb_queue_len(&sock->sk->sk_receive_queue) == 0)
971 return -ENOTCONN;
972 } else if (unlikely(sock->state != SS_CONNECTED))
973 return -ENOTCONN;
974
975 /* Look for a message in receive queue; wait if necessary */
976
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -0800977 if (unlikely(mutex_lock_interruptible(&tsock->lock)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100978 return -ERESTARTSYS;
979
980restart:
981 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
982 (flags & MSG_DONTWAIT))) {
Allan Stephensa3b0a5a2006-06-25 23:48:22 -0700983 res = -EWOULDBLOCK;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984 goto exit;
985 }
986
987 if ((res = wait_event_interruptible(
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900988 *sock->sk->sk_sleep,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
990 (sock->state == SS_DISCONNECTING))) )) {
991 goto exit;
992 }
993
994 /* Catch attempt to receive on an already terminated connection */
995 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
996
997 if (!q_len) {
998 res = -ENOTCONN;
999 goto exit;
1000 }
1001
1002 /* Get access to first message in receive queue */
1003
1004 buf = skb_peek(&sock->sk->sk_receive_queue);
1005 msg = buf_msg(buf);
1006 sz = msg_data_sz(msg);
1007 err = msg_errcode(msg);
1008
1009 /* Discard an empty non-errored message & try again */
1010
1011 if ((!sz) && (!err)) {
1012 advance_queue(tsock);
1013 goto restart;
1014 }
1015
1016 /* Optionally capture sender's address & ancillary data of first msg */
1017
1018 if (sz_copied == 0) {
1019 set_orig_addr(m, msg);
1020 if ((res = anc_data_recv(m, msg, tsock->p)))
1021 goto exit;
1022 }
1023
1024 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001025
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 if (!err) {
1027 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001028 sz = skb_tail_pointer(buf) - buf_crs;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029
1030 needed = (buf_len - sz_copied);
1031 sz_to_copy = (sz <= needed) ? sz : needed;
1032 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1033 res = -EFAULT;
1034 goto exit;
1035 }
1036 sz_copied += sz_to_copy;
1037
1038 if (sz_to_copy < sz) {
1039 if (!(flags & MSG_PEEK))
1040 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1041 goto exit;
1042 }
1043
1044 crs += sz_to_copy;
1045 } else {
1046 if (sz_copied != 0)
1047 goto exit; /* can't add error msg to valid data */
1048
1049 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1050 res = 0;
1051 else
1052 res = -ECONNRESET;
1053 }
1054
1055 /* Consume received message (optional) */
1056
1057 if (likely(!(flags & MSG_PEEK))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001058 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1059 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 advance_queue(tsock);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001061 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062
1063 /* Loop around if more data is required */
1064
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001065 if ((sz_copied < buf_len) /* didn't get all requested data */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 && (flags & MSG_WAITALL) /* ... and need to wait for more */
1067 && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
1068 && (!err) /* ... and haven't reached a FIN */
1069 )
1070 goto restart;
1071
1072exit:
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001073 mutex_unlock(&tsock->lock);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001074 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001075}
1076
1077/**
1078 * queue_overloaded - test if queue overload condition exists
1079 * @queue_size: current size of queue
1080 * @base: nominal maximum size of queue
1081 * @msg: message to be added to queue
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001082 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083 * Returns 1 if queue is currently overloaded, 0 otherwise
1084 */
1085
1086static int queue_overloaded(u32 queue_size, u32 base, struct tipc_msg *msg)
1087{
1088 u32 threshold;
1089 u32 imp = msg_importance(msg);
1090
1091 if (imp == TIPC_LOW_IMPORTANCE)
1092 threshold = base;
1093 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1094 threshold = base * 2;
1095 else if (imp == TIPC_HIGH_IMPORTANCE)
1096 threshold = base * 100;
1097 else
1098 return 0;
1099
1100 if (msg_connected(msg))
1101 threshold *= 4;
1102
1103 return (queue_size > threshold);
1104}
1105
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001106/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001107 * async_disconnect - wrapper function used to disconnect port
1108 * @portref: TIPC port reference (passed as pointer-sized value)
1109 */
1110
1111static void async_disconnect(unsigned long portref)
1112{
1113 tipc_disconnect((u32)portref);
1114}
1115
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001116/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117 * dispatch - handle arriving message
1118 * @tport: TIPC port that received message
1119 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001120 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121 * Called with port locked. Must not take socket lock to avoid deadlock risk.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001122 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001123 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1124 */
1125
1126static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1127{
1128 struct tipc_msg *msg = buf_msg(buf);
1129 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1130 struct socket *sock;
1131 u32 recv_q_len;
1132
1133 /* Reject message if socket is closing */
1134
1135 if (!tsock)
1136 return TIPC_ERR_NO_PORT;
1137
1138 /* Reject message if it is wrong sort of message for socket */
1139
1140 /*
1141 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1142 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1143 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1144 */
1145 sock = tsock->sk.sk_socket;
1146 if (sock->state == SS_READY) {
1147 if (msg_connected(msg)) {
1148 msg_dbg(msg, "dispatch filter 1\n");
1149 return TIPC_ERR_NO_PORT;
1150 }
1151 } else {
1152 if (msg_mcast(msg)) {
1153 msg_dbg(msg, "dispatch filter 2\n");
1154 return TIPC_ERR_NO_PORT;
1155 }
1156 if (sock->state == SS_CONNECTED) {
1157 if (!msg_connected(msg)) {
1158 msg_dbg(msg, "dispatch filter 3\n");
1159 return TIPC_ERR_NO_PORT;
1160 }
1161 }
1162 else if (sock->state == SS_CONNECTING) {
1163 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1164 msg_dbg(msg, "dispatch filter 4\n");
1165 return TIPC_ERR_NO_PORT;
1166 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001167 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 else if (sock->state == SS_LISTENING) {
1169 if (msg_connected(msg) || msg_errcode(msg)) {
1170 msg_dbg(msg, "dispatch filter 5\n");
1171 return TIPC_ERR_NO_PORT;
1172 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001173 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 else if (sock->state == SS_DISCONNECTING) {
1175 msg_dbg(msg, "dispatch filter 6\n");
1176 return TIPC_ERR_NO_PORT;
1177 }
1178 else /* (sock->state == SS_UNCONNECTED) */ {
1179 if (msg_connected(msg) || msg_errcode(msg)) {
1180 msg_dbg(msg, "dispatch filter 7\n");
1181 return TIPC_ERR_NO_PORT;
1182 }
1183 }
1184 }
1185
1186 /* Reject message if there isn't room to queue it */
1187
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001188 if (unlikely((u32)atomic_read(&tipc_queue_size) >
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 OVERLOAD_LIMIT_BASE)) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001190 if (queue_overloaded(atomic_read(&tipc_queue_size),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191 OVERLOAD_LIMIT_BASE, msg))
1192 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001193 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue);
1195 if (unlikely(recv_q_len > (OVERLOAD_LIMIT_BASE / 2))) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001196 if (queue_overloaded(recv_q_len,
1197 OVERLOAD_LIMIT_BASE / 2, msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001199 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200
1201 /* Initiate connection termination for an incoming 'FIN' */
1202
1203 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1204 sock->state = SS_DISCONNECTING;
1205 /* Note: Use signal since port lock is already taken! */
Per Liden4323add2006-01-18 00:38:21 +01001206 tipc_k_signal((Handler)async_disconnect, tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 }
1208
1209 /* Enqueue message (finally!) */
1210
1211 msg_dbg(msg,"<DISP<: ");
1212 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1213 atomic_inc(&tipc_queue_size);
1214 skb_queue_tail(&sock->sk->sk_receive_queue, buf);
1215
Allan Stephenscfb0c082006-10-16 21:47:18 -07001216 if (waitqueue_active(sock->sk->sk_sleep))
1217 wake_up_interruptible(sock->sk->sk_sleep);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218 return TIPC_OK;
1219}
1220
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001221/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222 * wakeupdispatch - wake up port after congestion
1223 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001224 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225 * Called with port lock on.
1226 */
1227
1228static void wakeupdispatch(struct tipc_port *tport)
1229{
1230 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1231
Allan Stephenscfb0c082006-10-16 21:47:18 -07001232 if (waitqueue_active(tsock->sk.sk_sleep))
1233 wake_up_interruptible(tsock->sk.sk_sleep);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234}
1235
1236/**
1237 * connect - establish a connection to another TIPC port
1238 * @sock: socket structure
1239 * @dest: socket address for destination port
1240 * @destlen: size of socket address data structure
1241 * @flags: (unused)
1242 *
1243 * Returns 0 on success, errno otherwise
1244 */
1245
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001246static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001247 int flags)
1248{
1249 struct tipc_sock *tsock = tipc_sk(sock->sk);
1250 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001251 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 struct sk_buff *buf;
1253 struct tipc_msg *msg;
1254 int res;
1255
1256 /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */
1257
1258 if (sock->state == SS_READY)
1259 return -EOPNOTSUPP;
1260
Allan Stephens51f9cc12006-06-25 23:49:06 -07001261 /* Issue Posix-compliant error code if socket is in the wrong state */
1262
Per Lidenb97bf3f2006-01-02 19:04:38 +01001263 if (sock->state == SS_LISTENING)
1264 return -EOPNOTSUPP;
1265 if (sock->state == SS_CONNECTING)
1266 return -EALREADY;
1267 if (sock->state != SS_UNCONNECTED)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001268 return -EISCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001269
Allan Stephens51f9cc12006-06-25 23:49:06 -07001270 /*
1271 * Reject connection attempt using multicast address
1272 *
1273 * Note: send_msg() validates the rest of the address fields,
1274 * so there's no need to do it here
1275 */
1276
1277 if (dst->addrtype == TIPC_ADDR_MCAST)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001278 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279
1280 /* Send a 'SYN-' to destination */
1281
1282 m.msg_name = dest;
Allan Stephens51f9cc12006-06-25 23:49:06 -07001283 m.msg_namelen = destlen;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001284 if ((res = send_msg(NULL, sock, &m, 0)) < 0) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285 sock->state = SS_DISCONNECTING;
1286 return res;
1287 }
1288
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001289 if (mutex_lock_interruptible(&tsock->lock))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001290 return -ERESTARTSYS;
1291
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 /* Wait for destination's 'ACK' response */
1293
1294 res = wait_event_interruptible_timeout(*sock->sk->sk_sleep,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001295 skb_queue_len(&sock->sk->sk_receive_queue),
Per Lidenb97bf3f2006-01-02 19:04:38 +01001296 sock->sk->sk_rcvtimeo);
1297 buf = skb_peek(&sock->sk->sk_receive_queue);
1298 if (res > 0) {
1299 msg = buf_msg(buf);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001300 res = auto_connect(sock, tsock, msg);
1301 if (!res) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001302 if (!msg_data_sz(msg))
1303 advance_queue(tsock);
1304 }
1305 } else {
1306 if (res == 0) {
1307 res = -ETIMEDOUT;
1308 } else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001309 { /* leave "res" unchanged */ }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310 sock->state = SS_DISCONNECTING;
1311 }
1312
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001313 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 return res;
1315}
1316
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001317/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001318 * listen - allow socket to listen for incoming connections
1319 * @sock: socket structure
1320 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001321 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001322 * Returns 0 on success, errno otherwise
1323 */
1324
1325static int listen(struct socket *sock, int len)
1326{
1327 /* REQUIRES SOCKET LOCKING OF SOME SORT? */
1328
1329 if (sock->state == SS_READY)
1330 return -EOPNOTSUPP;
1331 if (sock->state != SS_UNCONNECTED)
1332 return -EINVAL;
1333 sock->state = SS_LISTENING;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001334 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335}
1336
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001337/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 * accept - wait for connection request
1339 * @sock: listening socket
1340 * @newsock: new socket that is to be connected
1341 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001342 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343 * Returns 0 on success, errno otherwise
1344 */
1345
1346static int accept(struct socket *sock, struct socket *newsock, int flags)
1347{
1348 struct tipc_sock *tsock = tipc_sk(sock->sk);
1349 struct sk_buff *buf;
1350 int res = -EFAULT;
1351
1352 if (sock->state == SS_READY)
1353 return -EOPNOTSUPP;
1354 if (sock->state != SS_LISTENING)
1355 return -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001356
1357 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358 (flags & O_NONBLOCK)))
1359 return -EWOULDBLOCK;
1360
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001361 if (mutex_lock_interruptible(&tsock->lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362 return -ERESTARTSYS;
1363
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001364 if (wait_event_interruptible(*sock->sk->sk_sleep,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365 skb_queue_len(&sock->sk->sk_receive_queue))) {
1366 res = -ERESTARTSYS;
1367 goto exit;
1368 }
1369 buf = skb_peek(&sock->sk->sk_receive_queue);
1370
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001371 res = tipc_create(sock_net(sock->sk), newsock, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372 if (!res) {
1373 struct tipc_sock *new_tsock = tipc_sk(newsock->sk);
1374 struct tipc_portid id;
1375 struct tipc_msg *msg = buf_msg(buf);
1376 u32 new_ref = new_tsock->p->ref;
1377
1378 id.ref = msg_origport(msg);
1379 id.node = msg_orignode(msg);
1380 tipc_connect2port(new_ref, &id);
1381 newsock->state = SS_CONNECTED;
1382
1383 tipc_set_portimportance(new_ref, msg_importance(msg));
1384 if (msg_named(msg)) {
1385 new_tsock->p->conn_type = msg_nametype(msg);
1386 new_tsock->p->conn_instance = msg_nameinst(msg);
1387 }
1388
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001389 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1391 * Respond to 'SYN+' by queuing it on new socket.
1392 */
1393
1394 msg_dbg(msg,"<ACC<: ");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001395 if (!msg_data_sz(msg)) {
1396 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001397
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001398 send_packet(NULL, newsock, &m, 0);
1399 advance_queue(tsock);
1400 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001401 sock_lock(tsock);
1402 skb_dequeue(&sock->sk->sk_receive_queue);
1403 sock_unlock(tsock);
1404 skb_queue_head(&newsock->sk->sk_receive_queue, buf);
1405 }
1406 }
1407exit:
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001408 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001409 return res;
1410}
1411
1412/**
1413 * shutdown - shutdown socket connection
1414 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001415 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001416 *
1417 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001418 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001419 * Returns 0 on success, errno otherwise
1420 */
1421
1422static int shutdown(struct socket *sock, int how)
1423{
1424 struct tipc_sock* tsock = tipc_sk(sock->sk);
1425 struct sk_buff *buf;
1426 int res;
1427
Allan Stephense247a8f2008-03-06 15:05:38 -08001428 if (how != SHUT_RDWR)
1429 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001430
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001431 if (mutex_lock_interruptible(&tsock->lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001432 return -ERESTARTSYS;
1433
1434 sock_lock(tsock);
1435
1436 switch (sock->state) {
1437 case SS_CONNECTED:
1438
1439 /* Send 'FIN+' or 'FIN-' message to peer */
1440
1441 sock_unlock(tsock);
1442restart:
1443 if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1444 atomic_dec(&tipc_queue_size);
1445 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1446 buf_discard(buf);
1447 goto restart;
1448 }
1449 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
1450 }
1451 else {
1452 tipc_shutdown(tsock->p->ref);
1453 }
1454 sock_lock(tsock);
1455
1456 /* fall through */
1457
1458 case SS_DISCONNECTING:
1459
1460 /* Discard any unreceived messages */
1461
1462 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1463 atomic_dec(&tipc_queue_size);
1464 buf_discard(buf);
1465 }
1466 tsock->p->conn_unacked = 0;
1467
1468 /* fall through */
1469
1470 case SS_CONNECTING:
1471 sock->state = SS_DISCONNECTING;
1472 res = 0;
1473 break;
1474
1475 default:
1476 res = -ENOTCONN;
1477 }
1478
1479 sock_unlock(tsock);
1480
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001481 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001482 return res;
1483}
1484
1485/**
1486 * setsockopt - set socket option
1487 * @sock: socket structure
1488 * @lvl: option level
1489 * @opt: option identifier
1490 * @ov: pointer to new option value
1491 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001492 *
1493 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001494 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001495 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001496 * Returns 0 on success, errno otherwise
1497 */
1498
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001499static int setsockopt(struct socket *sock,
Allan Stephense9024f0f2006-06-25 23:43:57 -07001500 int lvl, int opt, char __user *ov, int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001501{
1502 struct tipc_sock *tsock = tipc_sk(sock->sk);
1503 u32 value;
1504 int res;
1505
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001506 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1507 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001508 if (lvl != SOL_TIPC)
1509 return -ENOPROTOOPT;
1510 if (ol < sizeof(value))
1511 return -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001512 if ((res = get_user(value, (u32 __user *)ov)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001513 return res;
1514
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001515 if (mutex_lock_interruptible(&tsock->lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001516 return -ERESTARTSYS;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001517
Per Lidenb97bf3f2006-01-02 19:04:38 +01001518 switch (opt) {
1519 case TIPC_IMPORTANCE:
1520 res = tipc_set_portimportance(tsock->p->ref, value);
1521 break;
1522 case TIPC_SRC_DROPPABLE:
1523 if (sock->type != SOCK_STREAM)
1524 res = tipc_set_portunreliable(tsock->p->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001525 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001526 res = -ENOPROTOOPT;
1527 break;
1528 case TIPC_DEST_DROPPABLE:
1529 res = tipc_set_portunreturnable(tsock->p->ref, value);
1530 break;
1531 case TIPC_CONN_TIMEOUT:
1532 sock->sk->sk_rcvtimeo = (value * HZ / 1000);
1533 break;
1534 default:
1535 res = -EINVAL;
1536 }
1537
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001538 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001539 return res;
1540}
1541
1542/**
1543 * getsockopt - get socket option
1544 * @sock: socket structure
1545 * @lvl: option level
1546 * @opt: option identifier
1547 * @ov: receptacle for option value
1548 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001549 *
1550 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001551 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001552 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001553 * Returns 0 on success, errno otherwise
1554 */
1555
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001556static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001557 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001558{
1559 struct tipc_sock *tsock = tipc_sk(sock->sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001560 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001561 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001562 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001563
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001564 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1565 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001566 if (lvl != SOL_TIPC)
1567 return -ENOPROTOOPT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001568 if ((res = get_user(len, ol)))
1569 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001570
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001571 if (mutex_lock_interruptible(&tsock->lock))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001572 return -ERESTARTSYS;
1573
1574 switch (opt) {
1575 case TIPC_IMPORTANCE:
1576 res = tipc_portimportance(tsock->p->ref, &value);
1577 break;
1578 case TIPC_SRC_DROPPABLE:
1579 res = tipc_portunreliable(tsock->p->ref, &value);
1580 break;
1581 case TIPC_DEST_DROPPABLE:
1582 res = tipc_portunreturnable(tsock->p->ref, &value);
1583 break;
1584 case TIPC_CONN_TIMEOUT:
1585 value = (sock->sk->sk_rcvtimeo * 1000) / HZ;
1586 break;
1587 default:
1588 res = -EINVAL;
1589 }
1590
1591 if (res) {
1592 /* "get" failed */
1593 }
1594 else if (len < sizeof(value)) {
1595 res = -EINVAL;
1596 }
1597 else if ((res = copy_to_user(ov, &value, sizeof(value)))) {
1598 /* couldn't return value */
1599 }
1600 else {
1601 res = put_user(sizeof(value), ol);
1602 }
1603
Matthias Kaehlcke26bad2c2008-03-03 23:35:53 -08001604 mutex_unlock(&tsock->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001605 return res;
1606}
1607
1608/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001609 * Protocol switches for the various types of TIPC sockets
1610 */
1611
Florian Westphalbca65ea2008-02-07 18:18:01 -08001612static const struct proto_ops msg_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001613 .owner = THIS_MODULE,
1614 .family = AF_TIPC,
1615 .release = release,
1616 .bind = bind,
1617 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001618 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001619 .accept = accept,
1620 .getname = get_name,
1621 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001622 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001623 .listen = listen,
1624 .shutdown = shutdown,
1625 .setsockopt = setsockopt,
1626 .getsockopt = getsockopt,
1627 .sendmsg = send_msg,
1628 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001629 .mmap = sock_no_mmap,
1630 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001631};
1632
Florian Westphalbca65ea2008-02-07 18:18:01 -08001633static const struct proto_ops packet_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001634 .owner = THIS_MODULE,
1635 .family = AF_TIPC,
1636 .release = release,
1637 .bind = bind,
1638 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001639 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001640 .accept = accept,
1641 .getname = get_name,
1642 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001643 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001644 .listen = listen,
1645 .shutdown = shutdown,
1646 .setsockopt = setsockopt,
1647 .getsockopt = getsockopt,
1648 .sendmsg = send_packet,
1649 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001650 .mmap = sock_no_mmap,
1651 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652};
1653
Florian Westphalbca65ea2008-02-07 18:18:01 -08001654static const struct proto_ops stream_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001655 .owner = THIS_MODULE,
1656 .family = AF_TIPC,
1657 .release = release,
1658 .bind = bind,
1659 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001660 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661 .accept = accept,
1662 .getname = get_name,
1663 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001664 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665 .listen = listen,
1666 .shutdown = shutdown,
1667 .setsockopt = setsockopt,
1668 .getsockopt = getsockopt,
1669 .sendmsg = send_stream,
1670 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001671 .mmap = sock_no_mmap,
1672 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001673};
1674
Florian Westphalbca65ea2008-02-07 18:18:01 -08001675static const struct net_proto_family tipc_family_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001676 .owner = THIS_MODULE,
1677 .family = AF_TIPC,
1678 .create = tipc_create
1679};
1680
1681static struct proto tipc_proto = {
1682 .name = "TIPC",
1683 .owner = THIS_MODULE,
1684 .obj_size = sizeof(struct tipc_sock)
1685};
1686
1687/**
Per Liden4323add2006-01-18 00:38:21 +01001688 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001689 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690 * Returns 0 on success, errno otherwise
1691 */
Per Liden4323add2006-01-18 00:38:21 +01001692int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001693{
1694 int res;
1695
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001696 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001697 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001698 err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699 goto out;
1700 }
1701
1702 res = sock_register(&tipc_family_ops);
1703 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001704 err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001705 proto_unregister(&tipc_proto);
1706 goto out;
1707 }
1708
1709 sockets_enabled = 1;
1710 out:
1711 return res;
1712}
1713
1714/**
Per Liden4323add2006-01-18 00:38:21 +01001715 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001716 */
Per Liden4323add2006-01-18 00:38:21 +01001717void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001718{
1719 if (!sockets_enabled)
1720 return;
1721
1722 sockets_enabled = 0;
1723 sock_unregister(tipc_family_ops.family);
1724 proto_unregister(&tipc_proto);
1725}
1726