blob: 34f96eda5fa34b2014f8370e12ed114038afab01 [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
Allan Stephens0ea52242008-07-14 22:42:19 -07005 * Copyright (c) 2004-2008, 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>
Per Lidenb97bf3f2006-01-02 19:04:38 +010043#include <linux/poll.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010044#include <linux/fcntl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/gfp.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010046#include <asm/string.h>
47#include <asm/atomic.h>
48#include <net/sock.h>
49
50#include <linux/tipc.h>
Per Lidenea714cc2006-01-11 12:28:47 +010051#include <linux/tipc_config.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010052
53#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000054#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010055
56#define SS_LISTENING -1 /* socket is listening */
57#define SS_READY -2 /* socket is connectionless */
58
Allan Stephens3654ea02008-04-13 21:35:11 -070059#define OVERLOAD_LIMIT_BASE 5000
60#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010061
62struct tipc_sock {
63 struct sock sk;
64 struct tipc_port *p;
Allan Stephens2da59912008-07-14 22:43:32 -070065 struct tipc_portid peer_name;
Allan Stephens564e83b2010-08-17 11:00:15 +000066 long conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +010067};
68
Allan Stephens0c3141e2008-04-15 00:22:02 -070069#define tipc_sk(sk) ((struct tipc_sock *)(sk))
70#define tipc_sk_port(sk) ((struct tipc_port *)(tipc_sk(sk)->p))
Per Lidenb97bf3f2006-01-02 19:04:38 +010071
Allan Stephens0c3141e2008-04-15 00:22:02 -070072static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +010073static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
74static void wakeupdispatch(struct tipc_port *tport);
75
Florian Westphalbca65ea2008-02-07 18:18:01 -080076static const struct proto_ops packet_ops;
77static const struct proto_ops stream_ops;
78static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010079
80static struct proto tipc_proto;
81
82static int sockets_enabled = 0;
83
84static atomic_t tipc_queue_size = ATOMIC_INIT(0);
85
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090086/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070087 * Revised TIPC socket locking policy:
88 *
89 * Most socket operations take the standard socket lock when they start
90 * and hold it until they finish (or until they need to sleep). Acquiring
91 * this lock grants the owner exclusive access to the fields of the socket
92 * data structures, with the exception of the backlog queue. A few socket
93 * operations can be done without taking the socket lock because they only
94 * read socket information that never changes during the life of the socket.
95 *
96 * Socket operations may acquire the lock for the associated TIPC port if they
97 * need to perform an operation on the port. If any routine needs to acquire
98 * both the socket lock and the port lock it must take the socket lock first
99 * to avoid the risk of deadlock.
100 *
101 * The dispatcher handling incoming messages cannot grab the socket lock in
102 * the standard fashion, since invoked it runs at the BH level and cannot block.
103 * Instead, it checks to see if the socket lock is currently owned by someone,
104 * and either handles the message itself or adds it to the socket's backlog
105 * queue; in the latter case the queued message is processed once the process
106 * owning the socket lock releases it.
107 *
108 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
109 * the problem of a blocked socket operation preventing any other operations
110 * from occurring. However, applications must be careful if they have
111 * multiple threads trying to send (or receive) on the same socket, as these
112 * operations might interfere with each other. For example, doing a connect
113 * and a receive at the same time might allow the receive to consume the
114 * ACK message meant for the connect. While additional work could be done
115 * to try and overcome this, it doesn't seem to be worthwhile at the present.
116 *
117 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
118 * that another operation that must be performed in a non-blocking manner is
119 * not delayed for very long because the lock has already been taken.
120 *
121 * NOTE: This code assumes that certain fields of a port/socket pair are
122 * constant over its lifetime; such fields can be examined without taking
123 * the socket lock and/or port lock, and do not need to be re-read even
124 * after resuming processing after waiting. These fields include:
125 * - socket type
126 * - pointer to socket sk structure (aka tipc_sock structure)
127 * - pointer to port structure
128 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130
131/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700132 * advance_rx_queue - discard first buffer in socket receive queue
133 *
134 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135 */
136
Allan Stephens0c3141e2008-04-15 00:22:02 -0700137static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100138{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700139 buf_discard(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140 atomic_dec(&tipc_queue_size);
141}
142
143/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700144 * discard_rx_queue - discard all buffers in socket receive queue
145 *
146 * Caller must hold socket lock
147 */
148
149static void discard_rx_queue(struct sock *sk)
150{
151 struct sk_buff *buf;
152
153 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
154 atomic_dec(&tipc_queue_size);
155 buf_discard(buf);
156 }
157}
158
159/**
160 * reject_rx_queue - reject all buffers in socket receive queue
161 *
162 * Caller must hold socket lock
163 */
164
165static void reject_rx_queue(struct sock *sk)
166{
167 struct sk_buff *buf;
168
169 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
170 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
171 atomic_dec(&tipc_queue_size);
172 }
173}
174
175/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176 * tipc_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700177 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 * @sock: pre-allocated socket structure
179 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800180 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900181 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700182 * This routine creates additional data structures used by the TIPC socket,
183 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184 *
185 * Returns 0 on success, errno otherwise
186 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700187
Eric Paris3f378b62009-11-05 22:18:14 -0800188static int tipc_create(struct net *net, struct socket *sock, int protocol,
189 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100190{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700191 const struct proto_ops *ops;
192 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100193 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700194 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700195
196 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800198 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700199 return -EAFNOSUPPORT;
200
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 if (unlikely(protocol != 0))
202 return -EPROTONOSUPPORT;
203
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204 switch (sock->type) {
205 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700206 ops = &stream_ops;
207 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100208 break;
209 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700210 ops = &packet_ops;
211 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 break;
213 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700215 ops = &msg_ops;
216 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217 break;
Allan Stephens49978652006-06-25 23:47:18 -0700218 default:
Allan Stephens49978652006-06-25 23:47:18 -0700219 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 }
221
Allan Stephens0c3141e2008-04-15 00:22:02 -0700222 /* Allocate socket's protocol area */
223
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700224 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700225 if (sk == NULL)
226 return -ENOMEM;
227
228 /* Allocate TIPC port for socket to use */
229
Allan Stephens0ea52242008-07-14 22:42:19 -0700230 tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
231 TIPC_LOW_IMPORTANCE);
232 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700233 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234 return -ENOMEM;
235 }
236
Allan Stephens0c3141e2008-04-15 00:22:02 -0700237 /* Finish initializing socket data structures */
238
239 sock->ops = ops;
240 sock->state = state;
241
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700243 sk->sk_backlog_rcv = backlog_rcv;
Allan Stephens0ea52242008-07-14 22:42:19 -0700244 tipc_sk(sk)->p = tp_ptr;
Allan Stephens564e83b2010-08-17 11:00:15 +0000245 tipc_sk(sk)->conn_timeout = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700247 spin_unlock_bh(tp_ptr->lock);
248
Allan Stephens0c3141e2008-04-15 00:22:02 -0700249 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700250 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700251 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700252 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700253 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254
255 atomic_inc(&tipc_user_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256 return 0;
257}
258
259/**
260 * release - destroy a TIPC socket
261 * @sock: socket to destroy
262 *
263 * This routine cleans up any messages that are still queued on the socket.
264 * For DGRAM and RDM socket types, all queued messages are rejected.
265 * For SEQPACKET and STREAM socket types, the first message is rejected
266 * and any others are discarded. (If the first message on a STREAM socket
267 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900268 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269 * NOTE: Rejected messages are not necessarily returned to the sender! They
270 * are returned or discarded according to the "destination droppable" setting
271 * specified for the message by the sender.
272 *
273 * Returns 0 on success, errno otherwise
274 */
275
276static int release(struct socket *sock)
277{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700279 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700281 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282
Allan Stephens0c3141e2008-04-15 00:22:02 -0700283 /*
284 * Exit if socket isn't fully initialized (occurs when a failed accept()
285 * releases a pre-allocated child socket that was never used)
286 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900287
Allan Stephens0c3141e2008-04-15 00:22:02 -0700288 if (sk == NULL)
289 return 0;
290
291 tport = tipc_sk_port(sk);
292 lock_sock(sk);
293
294 /*
295 * Reject all unreceived messages, except on an active connection
296 * (which disconnects locally & sends a 'FIN+' to peer)
297 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298
299 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700300 buf = __skb_dequeue(&sk->sk_receive_queue);
301 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 break;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700303 atomic_dec(&tipc_queue_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
305 buf_discard(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700306 else {
307 if ((sock->state == SS_CONNECTING) ||
308 (sock->state == SS_CONNECTED)) {
309 sock->state = SS_DISCONNECTING;
310 tipc_disconnect(tport->ref);
311 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700313 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314 }
315
Allan Stephens0c3141e2008-04-15 00:22:02 -0700316 /*
317 * Delete TIPC port; this ensures no more messages are queued
318 * (also disconnects an active connection & sends a 'FIN-' to peer)
319 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320
Allan Stephens0c3141e2008-04-15 00:22:02 -0700321 res = tipc_deleteport(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322
Allan Stephens0c3141e2008-04-15 00:22:02 -0700323 /* Discard any remaining (connection-based) messages in receive queue */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324
Allan Stephens0c3141e2008-04-15 00:22:02 -0700325 discard_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326
Allan Stephens0c3141e2008-04-15 00:22:02 -0700327 /* Reject any messages that accumulated in backlog queue */
328
329 sock->state = SS_DISCONNECTING;
330 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331
332 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700333 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900335 atomic_dec(&tipc_user_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336 return res;
337}
338
339/**
340 * bind - associate or disassocate TIPC name(s) with a socket
341 * @sock: socket structure
342 * @uaddr: socket address describing name(s) and desired operation
343 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900344 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 * Name and name sequence binding is indicated using a positive scope value;
346 * a negative scope value unbinds the specified name. Specifying no name
347 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900348 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700350 *
351 * NOTE: This routine doesn't need to take the socket lock since it doesn't
352 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353 */
354
355static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
356{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700358 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359
Allan Stephens0c3141e2008-04-15 00:22:02 -0700360 if (unlikely(!uaddr_len))
361 return tipc_withdraw(portref, 0, NULL);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900362
Allan Stephens0c3141e2008-04-15 00:22:02 -0700363 if (uaddr_len < sizeof(struct sockaddr_tipc))
364 return -EINVAL;
365 if (addr->family != AF_TIPC)
366 return -EAFNOSUPPORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368 if (addr->addrtype == TIPC_ADDR_NAME)
369 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700370 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
371 return -EAFNOSUPPORT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900372
Allan Stephens0c3141e2008-04-15 00:22:02 -0700373 return (addr->scope > 0) ?
374 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
375 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376}
377
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900378/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 * get_name - get port ID of socket or peer socket
380 * @sock: socket structure
381 * @uaddr: area for returned socket address
382 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700383 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900384 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700386 *
Allan Stephens2da59912008-07-14 22:43:32 -0700387 * NOTE: This routine doesn't need to take the socket lock since it only
388 * accesses socket information that is unchanging (or which changes in
389 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100390 */
391
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900392static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 int *uaddr_len, int peer)
394{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700396 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000398 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700399 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700400 if ((sock->state != SS_CONNECTED) &&
401 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
402 return -ENOTCONN;
403 addr->addr.id.ref = tsock->peer_name.ref;
404 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700405 } else {
Allan Stephens2da59912008-07-14 22:43:32 -0700406 tipc_ownidentity(tsock->p->ref, &addr->addr.id);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700407 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408
409 *uaddr_len = sizeof(*addr);
410 addr->addrtype = TIPC_ADDR_ID;
411 addr->family = AF_TIPC;
412 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 addr->addr.name.domain = 0;
414
Allan Stephens0c3141e2008-04-15 00:22:02 -0700415 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416}
417
418/**
419 * poll - read and possibly block on pollmask
420 * @file: file structure associated with the socket
421 * @sock: socket for which to calculate the poll bits
422 * @wait: ???
423 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700424 * Returns pollmask value
425 *
426 * COMMENTARY:
427 * It appears that the usual socket locking mechanisms are not useful here
428 * since the pollmask info is potentially out-of-date the moment this routine
429 * exits. TCP and other protocols seem to rely on higher level poll routines
430 * to handle any preventable race conditions, so TIPC will do the same ...
431 *
432 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700433 *
Allan Stephensf662c072010-08-17 11:00:06 +0000434 * socket state flags set
435 * ------------ ---------
436 * unconnected no read flags
437 * no write flags
438 *
439 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
440 * no write flags
441 *
442 * connected POLLIN/POLLRDNORM if data in rx queue
443 * POLLOUT if port is not congested
444 *
445 * disconnecting POLLIN/POLLRDNORM/POLLHUP
446 * no write flags
447 *
448 * listening POLLIN if SYN in rx queue
449 * no write flags
450 *
451 * ready POLLIN/POLLRDNORM if data in rx queue
452 * [connectionless] POLLOUT (since port cannot be congested)
453 *
454 * IMPORTANT: The fact that a read or write operation is indicated does NOT
455 * imply that the operation will succeed, merely that it should be performed
456 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 */
458
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900459static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 poll_table *wait)
461{
Allan Stephens9b674e82008-03-26 16:48:21 -0700462 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000463 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700464
Eric Dumazetaa395142010-04-20 13:03:51 +0000465 poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700466
Allan Stephensf662c072010-08-17 11:00:06 +0000467 switch ((int)sock->state) {
468 case SS_READY:
469 case SS_CONNECTED:
470 if (!tipc_sk_port(sk)->congested)
471 mask |= POLLOUT;
472 /* fall thru' */
473 case SS_CONNECTING:
474 case SS_LISTENING:
475 if (!skb_queue_empty(&sk->sk_receive_queue))
476 mask |= (POLLIN | POLLRDNORM);
477 break;
478 case SS_DISCONNECTING:
479 mask = (POLLIN | POLLRDNORM | POLLHUP);
480 break;
481 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700482
483 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484}
485
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900486/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 * dest_name_check - verify user is permitted to send to specified port name
488 * @dest: destination address
489 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900490 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 * Prevents restricted configuration commands from being issued by
492 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900493 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494 * Returns 0 if permission is granted, otherwise errno
495 */
496
Sam Ravnborg05790c62006-03-20 22:37:04 -0800497static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498{
499 struct tipc_cfg_msg_hdr hdr;
500
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900501 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
502 return 0;
503 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
504 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900505 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
506 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900508 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700510 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900512
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 return 0;
514}
515
516/**
517 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700518 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100519 * @sock: socket structure
520 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700521 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900522 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900524 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
526 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900527 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528 * Returns the number of bytes sent on success, or errno otherwise
529 */
530
531static int send_msg(struct kiocb *iocb, struct socket *sock,
532 struct msghdr *m, size_t total_len)
533{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700534 struct sock *sk = sock->sk;
535 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900536 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 int needs_conn;
538 int res = -EINVAL;
539
540 if (unlikely(!dest))
541 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700542 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
543 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 return -EINVAL;
545
Allan Stephens0c3141e2008-04-15 00:22:02 -0700546 if (iocb)
547 lock_sock(sk);
548
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549 needs_conn = (sock->state != SS_READY);
550 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700551 if (sock->state == SS_LISTENING) {
552 res = -EPIPE;
553 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700554 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700555 if (sock->state != SS_UNCONNECTED) {
556 res = -EISCONN;
557 goto exit;
558 }
559 if ((tport->published) ||
560 ((sock->type == SOCK_STREAM) && (total_len != 0))) {
561 res = -EOPNOTSUPP;
562 goto exit;
563 }
564 if (dest->addrtype == TIPC_ADDR_NAME) {
565 tport->conn_type = dest->addr.name.name.type;
566 tport->conn_instance = dest->addr.name.name.instance;
567 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568
569 /* Abort any pending connection attempts (very unlikely) */
570
Allan Stephens0c3141e2008-04-15 00:22:02 -0700571 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572 }
573
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900574 do {
575 if (dest->addrtype == TIPC_ADDR_NAME) {
576 if ((res = dest_name_check(dest, m)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700577 break;
578 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900579 &dest->addr.name.name,
580 dest->addr.name.domain,
581 m->msg_iovlen,
582 m->msg_iov);
583 }
584 else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700585 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900586 &dest->addr.id,
587 m->msg_iovlen,
588 m->msg_iov);
589 }
590 else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 if (needs_conn) {
592 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700593 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900595 if ((res = dest_name_check(dest, m)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700596 break;
597 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900598 &dest->addr.nameseq,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900599 m->msg_iovlen,
600 m->msg_iov);
601 }
602 if (likely(res != -ELINKCONG)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700603 if (needs_conn && (res >= 0)) {
604 sock->state = SS_CONNECTING;
605 }
606 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900607 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 if (m->msg_flags & MSG_DONTWAIT) {
609 res = -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700610 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700612 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +0000613 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -0700614 !tport->congested);
615 lock_sock(sk);
616 if (res)
617 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900618 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700619
620exit:
621 if (iocb)
622 release_sock(sk);
623 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624}
625
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900626/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700628 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 * @sock: socket structure
630 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700631 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900632 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900634 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635 * Returns the number of bytes sent on success, or errno otherwise
636 */
637
638static int send_packet(struct kiocb *iocb, struct socket *sock,
639 struct msghdr *m, size_t total_len)
640{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700641 struct sock *sk = sock->sk;
642 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900643 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644 int res;
645
646 /* Handle implied connection establishment */
647
648 if (unlikely(dest))
649 return send_msg(iocb, sock, m, total_len);
650
Allan Stephens0c3141e2008-04-15 00:22:02 -0700651 if (iocb)
652 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900654 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700655 if (unlikely(sock->state != SS_CONNECTED)) {
656 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900657 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700658 else
659 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700660 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700661 }
662
Allan Stephens0c3141e2008-04-15 00:22:02 -0700663 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900664 if (likely(res != -ELINKCONG)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700665 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900666 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667 if (m->msg_flags & MSG_DONTWAIT) {
668 res = -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700669 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700671 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +0000672 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -0700673 (!tport->congested || !tport->connected));
674 lock_sock(sk);
675 if (res)
676 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900677 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700678
679 if (iocb)
680 release_sock(sk);
681 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100682}
683
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900684/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685 * send_stream - send stream-oriented data
686 * @iocb: (unused)
687 * @sock: socket structure
688 * @m: data to send
689 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900690 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100691 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900692 *
693 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700694 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100695 */
696
Per Lidenb97bf3f2006-01-02 19:04:38 +0100697static int send_stream(struct kiocb *iocb, struct socket *sock,
698 struct msghdr *m, size_t total_len)
699{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700700 struct sock *sk = sock->sk;
701 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100702 struct msghdr my_msg;
703 struct iovec my_iov;
704 struct iovec *curr_iov;
705 int curr_iovlen;
706 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700707 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708 int curr_left;
709 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700710 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900712
Allan Stephens0c3141e2008-04-15 00:22:02 -0700713 lock_sock(sk);
714
Allan Stephens05646c92007-06-10 17:25:24 -0700715 /* Handle special cases where there is no connection */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900717 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700718 if (sock->state == SS_UNCONNECTED) {
719 res = send_packet(NULL, sock, m, total_len);
720 goto exit;
721 } else if (sock->state == SS_DISCONNECTING) {
722 res = -EPIPE;
723 goto exit;
724 } else {
725 res = -ENOTCONN;
726 goto exit;
727 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900728 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729
Allan Stephens0c3141e2008-04-15 00:22:02 -0700730 if (unlikely(m->msg_name)) {
731 res = -EISCONN;
732 goto exit;
733 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700734
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900735 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736 * Send each iovec entry using one or more messages
737 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900738 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100739 * (i.e. one large iovec entry), but could be improved to pass sets
740 * of small iovec entries into send_packet().
741 */
742
Allan Stephens1303e8f2006-06-25 23:46:50 -0700743 curr_iov = m->msg_iov;
744 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 my_msg.msg_iov = &my_iov;
746 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700747 my_msg.msg_flags = m->msg_flags;
748 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700749 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750
Allan Stephens05646c92007-06-10 17:25:24 -0700751 hdr_size = msg_hdr_sz(&tport->phdr);
752
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753 while (curr_iovlen--) {
754 curr_start = curr_iov->iov_base;
755 curr_left = curr_iov->iov_len;
756
757 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700758 bytes_to_send = tport->max_pkt - hdr_size;
759 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
760 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
761 if (curr_left < bytes_to_send)
762 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763 my_iov.iov_base = curr_start;
764 my_iov.iov_len = bytes_to_send;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700765 if ((res = send_packet(NULL, sock, &my_msg, 0)) < 0) {
766 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700767 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700768 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700769 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100770 curr_left -= bytes_to_send;
771 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700772 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 }
774
775 curr_iov++;
776 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700777 res = bytes_sent;
778exit:
779 release_sock(sk);
780 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100781}
782
783/**
784 * auto_connect - complete connection setup to a remote port
785 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100786 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900787 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788 * Returns 0 on success, errno otherwise
789 */
790
Allan Stephens0c3141e2008-04-15 00:22:02 -0700791static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792{
Allan Stephens2da59912008-07-14 22:43:32 -0700793 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794
795 if (msg_errcode(msg)) {
796 sock->state = SS_DISCONNECTING;
797 return -ECONNREFUSED;
798 }
799
Allan Stephens2da59912008-07-14 22:43:32 -0700800 tsock->peer_name.ref = msg_origport(msg);
801 tsock->peer_name.node = msg_orignode(msg);
802 tipc_connect2port(tsock->p->ref, &tsock->peer_name);
803 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804 sock->state = SS_CONNECTED;
805 return 0;
806}
807
808/**
809 * set_orig_addr - capture sender's address for received message
810 * @m: descriptor for message info
811 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900812 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813 * Note: Address is not captured if not requested by receiver.
814 */
815
Sam Ravnborg05790c62006-03-20 22:37:04 -0800816static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100817{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900818 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100819
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900820 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100821 addr->family = AF_TIPC;
822 addr->addrtype = TIPC_ADDR_ID;
823 addr->addr.id.ref = msg_origport(msg);
824 addr->addr.id.node = msg_orignode(msg);
825 addr->addr.name.domain = 0; /* could leave uninitialized */
826 addr->scope = 0; /* could leave uninitialized */
827 m->msg_namelen = sizeof(struct sockaddr_tipc);
828 }
829}
830
831/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900832 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100833 * @m: descriptor for message info
834 * @msg: received message header
835 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900836 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100837 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900838 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100839 * Returns 0 if successful, otherwise errno
840 */
841
Sam Ravnborg05790c62006-03-20 22:37:04 -0800842static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843 struct tipc_port *tport)
844{
845 u32 anc_data[3];
846 u32 err;
847 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700848 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100849 int res;
850
851 if (likely(m->msg_controllen == 0))
852 return 0;
853
854 /* Optionally capture errored message object(s) */
855
856 err = msg ? msg_errcode(msg) : 0;
857 if (unlikely(err)) {
858 anc_data[0] = err;
859 anc_data[1] = msg_data_sz(msg);
Allan Stephens4b087b22006-06-25 23:47:44 -0700860 if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861 return res;
862 if (anc_data[1] &&
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900863 (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
Per Lidenb97bf3f2006-01-02 19:04:38 +0100864 msg_data(msg))))
865 return res;
866 }
867
868 /* Optionally capture message destination object */
869
870 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
871 switch (dest_type) {
872 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700873 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100874 anc_data[0] = msg_nametype(msg);
875 anc_data[1] = msg_namelower(msg);
876 anc_data[2] = msg_namelower(msg);
877 break;
878 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700879 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 anc_data[0] = msg_nametype(msg);
881 anc_data[1] = msg_namelower(msg);
882 anc_data[2] = msg_nameupper(msg);
883 break;
884 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700885 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886 anc_data[0] = tport->conn_type;
887 anc_data[1] = tport->conn_instance;
888 anc_data[2] = tport->conn_instance;
889 break;
890 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700891 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892 }
Allan Stephens3546c752006-06-25 23:45:24 -0700893 if (has_name &&
Allan Stephens4b087b22006-06-25 23:47:44 -0700894 (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100895 return res;
896
897 return 0;
898}
899
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900900/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 * recv_msg - receive packet-oriented message
902 * @iocb: (unused)
903 * @m: descriptor for message info
904 * @buf_len: total size of user buffer area
905 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900906 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
908 * If the complete message doesn't fit in user area, truncate it.
909 *
910 * Returns size of returned message data, errno otherwise
911 */
912
913static int recv_msg(struct kiocb *iocb, struct socket *sock,
914 struct msghdr *m, size_t buf_len, int flags)
915{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700916 struct sock *sk = sock->sk;
917 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100918 struct sk_buff *buf;
919 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100920 unsigned int sz;
921 u32 err;
922 int res;
923
Allan Stephens0c3141e2008-04-15 00:22:02 -0700924 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100925
926 if (m->msg_iovlen != 1)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700927 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928
929 if (unlikely(!buf_len))
930 return -EINVAL;
931
Allan Stephens0c3141e2008-04-15 00:22:02 -0700932 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100933
Allan Stephens0c3141e2008-04-15 00:22:02 -0700934 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935 res = -ENOTCONN;
936 goto exit;
937 }
938
Allan Stephens0c3141e2008-04-15 00:22:02 -0700939restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940
Allan Stephens0c3141e2008-04-15 00:22:02 -0700941 /* Look for a message in receive queue; wait if necessary */
942
943 while (skb_queue_empty(&sk->sk_receive_queue)) {
944 if (sock->state == SS_DISCONNECTING) {
945 res = -ENOTCONN;
946 goto exit;
947 }
948 if (flags & MSG_DONTWAIT) {
949 res = -EWOULDBLOCK;
950 goto exit;
951 }
952 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +0000953 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -0700954 (!skb_queue_empty(&sk->sk_receive_queue) ||
955 (sock->state == SS_DISCONNECTING)));
956 lock_sock(sk);
957 if (res)
958 goto exit;
959 }
960
961 /* Look at first message in receive queue */
962
963 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964 msg = buf_msg(buf);
965 sz = msg_data_sz(msg);
966 err = msg_errcode(msg);
967
968 /* Complete connection setup for an implied connect */
969
970 if (unlikely(sock->state == SS_CONNECTING)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700971 res = auto_connect(sock, msg);
972 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100973 goto exit;
974 }
975
976 /* Discard an empty non-errored message & try again */
977
978 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700979 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980 goto restart;
981 }
982
983 /* Capture sender's address (optional) */
984
985 set_orig_addr(m, msg);
986
987 /* Capture ancillary data (optional) */
988
Allan Stephens0c3141e2008-04-15 00:22:02 -0700989 res = anc_data_recv(m, msg, tport);
990 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991 goto exit;
992
993 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900994
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995 if (!err) {
996 if (unlikely(buf_len < sz)) {
997 sz = buf_len;
998 m->msg_flags |= MSG_TRUNC;
999 }
1000 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
1001 sz))) {
1002 res = -EFAULT;
1003 goto exit;
1004 }
1005 res = sz;
1006 } else {
1007 if ((sock->state == SS_READY) ||
1008 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1009 res = 0;
1010 else
1011 res = -ECONNRESET;
1012 }
1013
1014 /* Consume received message (optional) */
1015
1016 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001017 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -07001018 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1019 tipc_acknowledge(tport->ref, tport->conn_unacked);
1020 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001021 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001022exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001023 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 return res;
1025}
1026
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001027/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 * recv_stream - receive stream-oriented data
1029 * @iocb: (unused)
1030 * @m: descriptor for message info
1031 * @buf_len: total size of user buffer area
1032 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001033 *
1034 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035 * will optionally wait for more; never truncates data.
1036 *
1037 * Returns size of returned message data, errno otherwise
1038 */
1039
1040static int recv_stream(struct kiocb *iocb, struct socket *sock,
1041 struct msghdr *m, size_t buf_len, int flags)
1042{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001043 struct sock *sk = sock->sk;
1044 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045 struct sk_buff *buf;
1046 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001047 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001048 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049 int sz_copied = 0;
Al Viro28c4dad2006-10-10 22:45:57 +01001050 char __user *crs = m->msg_iov->iov_base;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001051 unsigned char *buf_crs;
1052 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001053 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054
1055 /* Catch invalid receive attempts */
1056
Allan Stephens0c3141e2008-04-15 00:22:02 -07001057 if (m->msg_iovlen != 1)
1058 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
1059
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 if (unlikely(!buf_len))
1061 return -EINVAL;
1062
Allan Stephens0c3141e2008-04-15 00:22:02 -07001063 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064
Allan Stephens0c3141e2008-04-15 00:22:02 -07001065 if (unlikely((sock->state == SS_UNCONNECTED) ||
1066 (sock->state == SS_CONNECTING))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 res = -ENOTCONN;
1068 goto exit;
1069 }
1070
Florian Westphal3720d402010-08-17 11:00:04 +00001071 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
1072
Allan Stephens0c3141e2008-04-15 00:22:02 -07001073restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074
Allan Stephens0c3141e2008-04-15 00:22:02 -07001075 /* Look for a message in receive queue; wait if necessary */
1076
1077 while (skb_queue_empty(&sk->sk_receive_queue)) {
1078 if (sock->state == SS_DISCONNECTING) {
1079 res = -ENOTCONN;
1080 goto exit;
1081 }
1082 if (flags & MSG_DONTWAIT) {
1083 res = -EWOULDBLOCK;
1084 goto exit;
1085 }
1086 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001087 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001088 (!skb_queue_empty(&sk->sk_receive_queue) ||
1089 (sock->state == SS_DISCONNECTING)));
1090 lock_sock(sk);
1091 if (res)
1092 goto exit;
1093 }
1094
1095 /* Look at first message in receive queue */
1096
1097 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001098 msg = buf_msg(buf);
1099 sz = msg_data_sz(msg);
1100 err = msg_errcode(msg);
1101
1102 /* Discard an empty non-errored message & try again */
1103
1104 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001105 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001106 goto restart;
1107 }
1108
1109 /* Optionally capture sender's address & ancillary data of first msg */
1110
1111 if (sz_copied == 0) {
1112 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001113 res = anc_data_recv(m, msg, tport);
1114 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115 goto exit;
1116 }
1117
1118 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001119
Per Lidenb97bf3f2006-01-02 19:04:38 +01001120 if (!err) {
1121 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
Allan Stephens7a8036c2008-04-15 00:15:15 -07001122 sz = (unsigned char *)msg + msg_size(msg) - buf_crs;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001123
1124 needed = (buf_len - sz_copied);
1125 sz_to_copy = (sz <= needed) ? sz : needed;
1126 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1127 res = -EFAULT;
1128 goto exit;
1129 }
1130 sz_copied += sz_to_copy;
1131
1132 if (sz_to_copy < sz) {
1133 if (!(flags & MSG_PEEK))
1134 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1135 goto exit;
1136 }
1137
1138 crs += sz_to_copy;
1139 } else {
1140 if (sz_copied != 0)
1141 goto exit; /* can't add error msg to valid data */
1142
1143 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1144 res = 0;
1145 else
1146 res = -ECONNRESET;
1147 }
1148
1149 /* Consume received message (optional) */
1150
1151 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001152 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1153 tipc_acknowledge(tport->ref, tport->conn_unacked);
1154 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001155 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156
1157 /* Loop around if more data is required */
1158
Joe Perchesf64f9e72009-11-29 16:55:45 -08001159 if ((sz_copied < buf_len) && /* didn't get all requested data */
1160 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001161 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001162 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1163 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164 goto restart;
1165
1166exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001167 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001168 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169}
1170
1171/**
Allan Stephens1819b832008-04-15 00:15:50 -07001172 * rx_queue_full - determine if receive queue can accept another message
1173 * @msg: message to be added to queue
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 * @queue_size: current size of queue
1175 * @base: nominal maximum size of queue
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001176 *
Allan Stephens1819b832008-04-15 00:15:50 -07001177 * Returns 1 if queue is unable to accept message, 0 otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178 */
1179
Allan Stephens1819b832008-04-15 00:15:50 -07001180static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001181{
1182 u32 threshold;
1183 u32 imp = msg_importance(msg);
1184
1185 if (imp == TIPC_LOW_IMPORTANCE)
1186 threshold = base;
1187 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1188 threshold = base * 2;
1189 else if (imp == TIPC_HIGH_IMPORTANCE)
1190 threshold = base * 100;
1191 else
1192 return 0;
1193
1194 if (msg_connected(msg))
1195 threshold *= 4;
1196
Eric Dumazeta02cec22010-09-22 20:43:57 +00001197 return queue_size >= threshold;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198}
1199
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001200/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001201 * filter_rcv - validate incoming message
1202 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001204 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001205 * Enqueues message on receive queue if acceptable; optionally handles
1206 * disconnect indication for a connected socket.
1207 *
1208 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001209 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001210 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1211 */
1212
Allan Stephens0c3141e2008-04-15 00:22:02 -07001213static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001215 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216 struct tipc_msg *msg = buf_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001217 u32 recv_q_len;
1218
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 /* Reject message if it is wrong sort of message for socket */
1220
1221 /*
1222 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1223 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1224 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1225 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001226
Per Lidenb97bf3f2006-01-02 19:04:38 +01001227 if (sock->state == SS_READY) {
1228 if (msg_connected(msg)) {
1229 msg_dbg(msg, "dispatch filter 1\n");
1230 return TIPC_ERR_NO_PORT;
1231 }
1232 } else {
1233 if (msg_mcast(msg)) {
1234 msg_dbg(msg, "dispatch filter 2\n");
1235 return TIPC_ERR_NO_PORT;
1236 }
1237 if (sock->state == SS_CONNECTED) {
1238 if (!msg_connected(msg)) {
1239 msg_dbg(msg, "dispatch filter 3\n");
1240 return TIPC_ERR_NO_PORT;
1241 }
1242 }
1243 else if (sock->state == SS_CONNECTING) {
1244 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1245 msg_dbg(msg, "dispatch filter 4\n");
1246 return TIPC_ERR_NO_PORT;
1247 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001248 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001249 else if (sock->state == SS_LISTENING) {
1250 if (msg_connected(msg) || msg_errcode(msg)) {
1251 msg_dbg(msg, "dispatch filter 5\n");
1252 return TIPC_ERR_NO_PORT;
1253 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001254 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001255 else if (sock->state == SS_DISCONNECTING) {
1256 msg_dbg(msg, "dispatch filter 6\n");
1257 return TIPC_ERR_NO_PORT;
1258 }
1259 else /* (sock->state == SS_UNCONNECTED) */ {
1260 if (msg_connected(msg) || msg_errcode(msg)) {
1261 msg_dbg(msg, "dispatch filter 7\n");
1262 return TIPC_ERR_NO_PORT;
1263 }
1264 }
1265 }
1266
1267 /* Reject message if there isn't room to queue it */
1268
Allan Stephens1819b832008-04-15 00:15:50 -07001269 recv_q_len = (u32)atomic_read(&tipc_queue_size);
1270 if (unlikely(recv_q_len >= OVERLOAD_LIMIT_BASE)) {
1271 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001273 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001274 recv_q_len = skb_queue_len(&sk->sk_receive_queue);
Allan Stephens1819b832008-04-15 00:15:50 -07001275 if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
1276 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001277 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001278 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279
Allan Stephens0c3141e2008-04-15 00:22:02 -07001280 /* Enqueue message (finally!) */
1281
1282 msg_dbg(msg, "<DISP<: ");
1283 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1284 atomic_inc(&tipc_queue_size);
1285 __skb_queue_tail(&sk->sk_receive_queue, buf);
1286
Per Lidenb97bf3f2006-01-02 19:04:38 +01001287 /* Initiate connection termination for an incoming 'FIN' */
1288
1289 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1290 sock->state = SS_DISCONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001291 tipc_disconnect_port(tipc_sk_port(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 }
1293
Eric Dumazetaa395142010-04-20 13:03:51 +00001294 if (waitqueue_active(sk_sleep(sk)))
1295 wake_up_interruptible(sk_sleep(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001296 return TIPC_OK;
1297}
1298
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001299/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001300 * backlog_rcv - handle incoming message from backlog queue
1301 * @sk: socket
1302 * @buf: message
1303 *
1304 * Caller must hold socket lock, but not port lock.
1305 *
1306 * Returns 0
1307 */
1308
1309static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1310{
1311 u32 res;
1312
1313 res = filter_rcv(sk, buf);
1314 if (res)
1315 tipc_reject_msg(buf, res);
1316 return 0;
1317}
1318
1319/**
1320 * dispatch - handle incoming message
1321 * @tport: TIPC port that received message
1322 * @buf: message
1323 *
1324 * Called with port lock already taken.
1325 *
1326 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1327 */
1328
1329static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1330{
1331 struct sock *sk = (struct sock *)tport->usr_handle;
1332 u32 res;
1333
1334 /*
1335 * Process message if socket is unlocked; otherwise add to backlog queue
1336 *
1337 * This code is based on sk_receive_skb(), but must be distinct from it
1338 * since a TIPC-specific filter/reject mechanism is utilized
1339 */
1340
1341 bh_lock_sock(sk);
1342 if (!sock_owned_by_user(sk)) {
1343 res = filter_rcv(sk, buf);
1344 } else {
Zhu Yia3a858f2010-03-04 18:01:47 +00001345 if (sk_add_backlog(sk, buf))
Zhu Yi53eecb12010-03-04 18:01:45 +00001346 res = TIPC_ERR_OVERLOAD;
1347 else
1348 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001349 }
1350 bh_unlock_sock(sk);
1351
1352 return res;
1353}
1354
1355/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 * wakeupdispatch - wake up port after congestion
1357 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001358 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001359 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001360 */
1361
1362static void wakeupdispatch(struct tipc_port *tport)
1363{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001364 struct sock *sk = (struct sock *)tport->usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365
Eric Dumazetaa395142010-04-20 13:03:51 +00001366 if (waitqueue_active(sk_sleep(sk)))
1367 wake_up_interruptible(sk_sleep(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001368}
1369
1370/**
1371 * connect - establish a connection to another TIPC port
1372 * @sock: socket structure
1373 * @dest: socket address for destination port
1374 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001375 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 *
1377 * Returns 0 on success, errno otherwise
1378 */
1379
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001380static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381 int flags)
1382{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001383 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001384 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1385 struct msghdr m = {NULL,};
1386 struct sk_buff *buf;
1387 struct tipc_msg *msg;
Allan Stephens564e83b2010-08-17 11:00:15 +00001388 long timeout;
Allan Stephensb89741a2008-04-15 00:20:37 -07001389 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390
Allan Stephens0c3141e2008-04-15 00:22:02 -07001391 lock_sock(sk);
1392
Allan Stephensb89741a2008-04-15 00:20:37 -07001393 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394
Allan Stephens0c3141e2008-04-15 00:22:02 -07001395 if (sock->state == SS_READY) {
1396 res = -EOPNOTSUPP;
1397 goto exit;
1398 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001399
Allan Stephensb89741a2008-04-15 00:20:37 -07001400 /* For now, TIPC does not support the non-blocking form of connect() */
Allan Stephens4934c692008-04-15 00:16:19 -07001401
Allan Stephens0c3141e2008-04-15 00:22:02 -07001402 if (flags & O_NONBLOCK) {
Allan Stephens35997e32010-08-17 11:00:05 +00001403 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001404 goto exit;
1405 }
Allan Stephens4934c692008-04-15 00:16:19 -07001406
Allan Stephensb89741a2008-04-15 00:20:37 -07001407 /* Issue Posix-compliant error code if socket is in the wrong state */
Allan Stephens51f9cc12006-06-25 23:49:06 -07001408
Allan Stephens0c3141e2008-04-15 00:22:02 -07001409 if (sock->state == SS_LISTENING) {
1410 res = -EOPNOTSUPP;
1411 goto exit;
1412 }
1413 if (sock->state == SS_CONNECTING) {
1414 res = -EALREADY;
1415 goto exit;
1416 }
1417 if (sock->state != SS_UNCONNECTED) {
1418 res = -EISCONN;
1419 goto exit;
1420 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421
Allan Stephensb89741a2008-04-15 00:20:37 -07001422 /*
1423 * Reject connection attempt using multicast address
1424 *
1425 * Note: send_msg() validates the rest of the address fields,
1426 * so there's no need to do it here
1427 */
Allan Stephens51f9cc12006-06-25 23:49:06 -07001428
Allan Stephens0c3141e2008-04-15 00:22:02 -07001429 if (dst->addrtype == TIPC_ADDR_MCAST) {
1430 res = -EINVAL;
1431 goto exit;
1432 }
1433
1434 /* Reject any messages already in receive queue (very unlikely) */
1435
1436 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001437
Allan Stephensb89741a2008-04-15 00:20:37 -07001438 /* Send a 'SYN-' to destination */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001439
Allan Stephensb89741a2008-04-15 00:20:37 -07001440 m.msg_name = dest;
1441 m.msg_namelen = destlen;
1442 res = send_msg(NULL, sock, &m, 0);
1443 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001444 goto exit;
Allan Stephensb89741a2008-04-15 00:20:37 -07001445 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001446
Allan Stephens0c3141e2008-04-15 00:22:02 -07001447 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001448
Allan Stephens564e83b2010-08-17 11:00:15 +00001449 timeout = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001450 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001451 res = wait_event_interruptible_timeout(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001452 (!skb_queue_empty(&sk->sk_receive_queue) ||
1453 (sock->state != SS_CONNECTING)),
Allan Stephens564e83b2010-08-17 11:00:15 +00001454 timeout ? timeout : MAX_SCHEDULE_TIMEOUT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001455 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001456
Allan Stephensb89741a2008-04-15 00:20:37 -07001457 if (res > 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001458 buf = skb_peek(&sk->sk_receive_queue);
1459 if (buf != NULL) {
1460 msg = buf_msg(buf);
1461 res = auto_connect(sock, msg);
1462 if (!res) {
1463 if (!msg_data_sz(msg))
1464 advance_rx_queue(sk);
1465 }
1466 } else {
1467 if (sock->state == SS_CONNECTED) {
1468 res = -EISCONN;
1469 } else {
1470 res = -ECONNREFUSED;
1471 }
Allan Stephensb89741a2008-04-15 00:20:37 -07001472 }
1473 } else {
1474 if (res == 0)
1475 res = -ETIMEDOUT;
1476 else
1477 ; /* leave "res" unchanged */
1478 sock->state = SS_DISCONNECTING;
1479 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001480
Allan Stephens0c3141e2008-04-15 00:22:02 -07001481exit:
1482 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001483 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484}
1485
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001486/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001487 * listen - allow socket to listen for incoming connections
1488 * @sock: socket structure
1489 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001490 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001491 * Returns 0 on success, errno otherwise
1492 */
1493
1494static int listen(struct socket *sock, int len)
1495{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001496 struct sock *sk = sock->sk;
1497 int res;
1498
1499 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001500
1501 if (sock->state == SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001502 res = -EOPNOTSUPP;
1503 else if (sock->state != SS_UNCONNECTED)
1504 res = -EINVAL;
1505 else {
1506 sock->state = SS_LISTENING;
1507 res = 0;
1508 }
1509
1510 release_sock(sk);
1511 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001512}
1513
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001514/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001515 * accept - wait for connection request
1516 * @sock: listening socket
1517 * @newsock: new socket that is to be connected
1518 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001519 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001520 * Returns 0 on success, errno otherwise
1521 */
1522
Allan Stephens0c3141e2008-04-15 00:22:02 -07001523static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001524{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001525 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001526 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001527 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001528
Allan Stephens0c3141e2008-04-15 00:22:02 -07001529 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001530
Allan Stephens0c3141e2008-04-15 00:22:02 -07001531 if (sock->state == SS_READY) {
1532 res = -EOPNOTSUPP;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001533 goto exit;
1534 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001535 if (sock->state != SS_LISTENING) {
1536 res = -EINVAL;
1537 goto exit;
1538 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001539
Allan Stephens0c3141e2008-04-15 00:22:02 -07001540 while (skb_queue_empty(&sk->sk_receive_queue)) {
1541 if (flags & O_NONBLOCK) {
1542 res = -EWOULDBLOCK;
1543 goto exit;
1544 }
1545 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001546 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001547 (!skb_queue_empty(&sk->sk_receive_queue)));
1548 lock_sock(sk);
1549 if (res)
1550 goto exit;
1551 }
1552
1553 buf = skb_peek(&sk->sk_receive_queue);
1554
Eric Paris3f378b62009-11-05 22:18:14 -08001555 res = tipc_create(sock_net(sock->sk), new_sock, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001556 if (!res) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001557 struct sock *new_sk = new_sock->sk;
Allan Stephens2da59912008-07-14 22:43:32 -07001558 struct tipc_sock *new_tsock = tipc_sk(new_sk);
1559 struct tipc_port *new_tport = new_tsock->p;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001560 u32 new_ref = new_tport->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001561 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001562
1563 lock_sock(new_sk);
1564
1565 /*
1566 * Reject any stray messages received by new socket
1567 * before the socket lock was taken (very, very unlikely)
1568 */
1569
1570 reject_rx_queue(new_sk);
1571
1572 /* Connect new socket to it's peer */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001573
Allan Stephens2da59912008-07-14 22:43:32 -07001574 new_tsock->peer_name.ref = msg_origport(msg);
1575 new_tsock->peer_name.node = msg_orignode(msg);
1576 tipc_connect2port(new_ref, &new_tsock->peer_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001577 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001578
1579 tipc_set_portimportance(new_ref, msg_importance(msg));
1580 if (msg_named(msg)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001581 new_tport->conn_type = msg_nametype(msg);
1582 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001583 }
1584
Allan Stephens0c3141e2008-04-15 00:22:02 -07001585 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001586 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1587 * Respond to 'SYN+' by queuing it on new socket.
1588 */
1589
1590 msg_dbg(msg,"<ACC<: ");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001591 if (!msg_data_sz(msg)) {
1592 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001593
Allan Stephens0c3141e2008-04-15 00:22:02 -07001594 advance_rx_queue(sk);
1595 send_packet(NULL, new_sock, &m, 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001596 } else {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001597 __skb_dequeue(&sk->sk_receive_queue);
1598 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001599 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001600 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001601 }
1602exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001603 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001604 return res;
1605}
1606
1607/**
1608 * shutdown - shutdown socket connection
1609 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001610 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001611 *
1612 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001613 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001614 * Returns 0 on success, errno otherwise
1615 */
1616
1617static int shutdown(struct socket *sock, int how)
1618{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001619 struct sock *sk = sock->sk;
1620 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001621 struct sk_buff *buf;
1622 int res;
1623
Allan Stephense247a8f2008-03-06 15:05:38 -08001624 if (how != SHUT_RDWR)
1625 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001626
Allan Stephens0c3141e2008-04-15 00:22:02 -07001627 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001628
1629 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001630 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001631 case SS_CONNECTED:
1632
Allan Stephens0c3141e2008-04-15 00:22:02 -07001633 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001634restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001635 buf = __skb_dequeue(&sk->sk_receive_queue);
1636 if (buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001637 atomic_dec(&tipc_queue_size);
1638 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1639 buf_discard(buf);
1640 goto restart;
1641 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001642 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001643 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001644 } else {
1645 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001646 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001647
1648 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001649
1650 /* fall through */
1651
1652 case SS_DISCONNECTING:
1653
Allan Stephens0c3141e2008-04-15 00:22:02 -07001654 /* Discard any unreceived messages; wake up sleeping tasks */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001655
Allan Stephens0c3141e2008-04-15 00:22:02 -07001656 discard_rx_queue(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001657 if (waitqueue_active(sk_sleep(sk)))
1658 wake_up_interruptible(sk_sleep(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001659 res = 0;
1660 break;
1661
1662 default:
1663 res = -ENOTCONN;
1664 }
1665
Allan Stephens0c3141e2008-04-15 00:22:02 -07001666 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001667 return res;
1668}
1669
1670/**
1671 * setsockopt - set socket option
1672 * @sock: socket structure
1673 * @lvl: option level
1674 * @opt: option identifier
1675 * @ov: pointer to new option value
1676 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001677 *
1678 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001680 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001681 * Returns 0 on success, errno otherwise
1682 */
1683
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001684static int setsockopt(struct socket *sock,
David S. Millerb7058842009-09-30 16:12:20 -07001685 int lvl, int opt, char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001686{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001687 struct sock *sk = sock->sk;
1688 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001689 u32 value;
1690 int res;
1691
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001692 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1693 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001694 if (lvl != SOL_TIPC)
1695 return -ENOPROTOOPT;
1696 if (ol < sizeof(value))
1697 return -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001698 if ((res = get_user(value, (u32 __user *)ov)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699 return res;
1700
Allan Stephens0c3141e2008-04-15 00:22:02 -07001701 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001702
Per Lidenb97bf3f2006-01-02 19:04:38 +01001703 switch (opt) {
1704 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001705 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001706 break;
1707 case TIPC_SRC_DROPPABLE:
1708 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001709 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001710 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001711 res = -ENOPROTOOPT;
1712 break;
1713 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001714 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001715 break;
1716 case TIPC_CONN_TIMEOUT:
Allan Stephens564e83b2010-08-17 11:00:15 +00001717 tipc_sk(sk)->conn_timeout = msecs_to_jiffies(value);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001718 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719 break;
1720 default:
1721 res = -EINVAL;
1722 }
1723
Allan Stephens0c3141e2008-04-15 00:22:02 -07001724 release_sock(sk);
1725
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726 return res;
1727}
1728
1729/**
1730 * getsockopt - get socket option
1731 * @sock: socket structure
1732 * @lvl: option level
1733 * @opt: option identifier
1734 * @ov: receptacle for option value
1735 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001736 *
1737 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001738 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001739 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001740 * Returns 0 on success, errno otherwise
1741 */
1742
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001743static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001744 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001745{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001746 struct sock *sk = sock->sk;
1747 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001748 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001750 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001751
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001752 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1753 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001754 if (lvl != SOL_TIPC)
1755 return -ENOPROTOOPT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001756 if ((res = get_user(len, ol)))
1757 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001758
Allan Stephens0c3141e2008-04-15 00:22:02 -07001759 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001760
1761 switch (opt) {
1762 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001763 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001764 break;
1765 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001766 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001767 break;
1768 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001769 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001770 break;
1771 case TIPC_CONN_TIMEOUT:
Allan Stephens564e83b2010-08-17 11:00:15 +00001772 value = jiffies_to_msecs(tipc_sk(sk)->conn_timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001773 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774 break;
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001775 case TIPC_NODE_RECVQ_DEPTH:
1776 value = (u32)atomic_read(&tipc_queue_size);
1777 break;
1778 case TIPC_SOCK_RECVQ_DEPTH:
1779 value = skb_queue_len(&sk->sk_receive_queue);
1780 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001781 default:
1782 res = -EINVAL;
1783 }
1784
Allan Stephens0c3141e2008-04-15 00:22:02 -07001785 release_sock(sk);
1786
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787 if (res) {
1788 /* "get" failed */
1789 }
1790 else if (len < sizeof(value)) {
1791 res = -EINVAL;
1792 }
Pavel Emelyanov653252c2008-04-25 01:49:48 -07001793 else if (copy_to_user(ov, &value, sizeof(value))) {
1794 res = -EFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001795 }
1796 else {
1797 res = put_user(sizeof(value), ol);
1798 }
1799
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800 return res;
1801}
1802
1803/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001804 * Protocol switches for the various types of TIPC sockets
1805 */
1806
Florian Westphalbca65ea2008-02-07 18:18:01 -08001807static const struct proto_ops msg_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001808 .owner = THIS_MODULE,
1809 .family = AF_TIPC,
1810 .release = release,
1811 .bind = bind,
1812 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001813 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814 .accept = accept,
1815 .getname = get_name,
1816 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001817 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001818 .listen = listen,
1819 .shutdown = shutdown,
1820 .setsockopt = setsockopt,
1821 .getsockopt = getsockopt,
1822 .sendmsg = send_msg,
1823 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001824 .mmap = sock_no_mmap,
1825 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826};
1827
Florian Westphalbca65ea2008-02-07 18:18:01 -08001828static const struct proto_ops packet_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829 .owner = THIS_MODULE,
1830 .family = AF_TIPC,
1831 .release = release,
1832 .bind = bind,
1833 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001834 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001835 .accept = accept,
1836 .getname = get_name,
1837 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001838 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001839 .listen = listen,
1840 .shutdown = shutdown,
1841 .setsockopt = setsockopt,
1842 .getsockopt = getsockopt,
1843 .sendmsg = send_packet,
1844 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001845 .mmap = sock_no_mmap,
1846 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001847};
1848
Florian Westphalbca65ea2008-02-07 18:18:01 -08001849static const struct proto_ops stream_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001850 .owner = THIS_MODULE,
1851 .family = AF_TIPC,
1852 .release = release,
1853 .bind = bind,
1854 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001855 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001856 .accept = accept,
1857 .getname = get_name,
1858 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001859 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001860 .listen = listen,
1861 .shutdown = shutdown,
1862 .setsockopt = setsockopt,
1863 .getsockopt = getsockopt,
1864 .sendmsg = send_stream,
1865 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001866 .mmap = sock_no_mmap,
1867 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001868};
1869
Florian Westphalbca65ea2008-02-07 18:18:01 -08001870static const struct net_proto_family tipc_family_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001871 .owner = THIS_MODULE,
1872 .family = AF_TIPC,
1873 .create = tipc_create
1874};
1875
1876static struct proto tipc_proto = {
1877 .name = "TIPC",
1878 .owner = THIS_MODULE,
1879 .obj_size = sizeof(struct tipc_sock)
1880};
1881
1882/**
Per Liden4323add2006-01-18 00:38:21 +01001883 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001884 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001885 * Returns 0 on success, errno otherwise
1886 */
Per Liden4323add2006-01-18 00:38:21 +01001887int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001888{
1889 int res;
1890
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001891 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001892 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001893 err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001894 goto out;
1895 }
1896
1897 res = sock_register(&tipc_family_ops);
1898 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001899 err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001900 proto_unregister(&tipc_proto);
1901 goto out;
1902 }
1903
1904 sockets_enabled = 1;
1905 out:
1906 return res;
1907}
1908
1909/**
Per Liden4323add2006-01-18 00:38:21 +01001910 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001911 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001912
Per Liden4323add2006-01-18 00:38:21 +01001913void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001914{
1915 if (!sockets_enabled)
1916 return;
1917
1918 sockets_enabled = 0;
1919 sock_unregister(tipc_family_ops.family);
1920 proto_unregister(&tipc_proto);
1921}
1922