blob: e741416d1d24d4655cd4096b07f84cd978c90cc7 [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 *
Jon Maloye643df12012-11-27 06:15:29 -05004 * Copyright (c) 2001-2007, 2012 Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000038#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039
Erik Hugne2cf8aa12012-06-29 00:16:37 -040040#include <linux/export.h>
41#include <net/sock.h>
42
Per Lidenb97bf3f2006-01-02 19:04:38 +010043#define SS_LISTENING -1 /* socket is listening */
44#define SS_READY -2 /* socket is connectionless */
45
Allan Stephens3654ea02008-04-13 21:35:11 -070046#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010047
48struct tipc_sock {
49 struct sock sk;
50 struct tipc_port *p;
Allan Stephens2da59912008-07-14 22:43:32 -070051 struct tipc_portid peer_name;
Allan Stephensa0f40f02011-05-26 13:44:34 -040052 unsigned int conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +010053};
54
Allan Stephens0c3141e2008-04-15 00:22:02 -070055#define tipc_sk(sk) ((struct tipc_sock *)(sk))
Joe Perchese3192692012-06-03 17:41:40 +000056#define tipc_sk_port(sk) (tipc_sk(sk)->p)
Per Lidenb97bf3f2006-01-02 19:04:38 +010057
Allan Stephens71092ea2011-02-23 14:52:14 -050058#define tipc_rx_ready(sock) (!skb_queue_empty(&sock->sk->sk_receive_queue) || \
59 (sock->state == SS_DISCONNECTING))
60
Allan Stephens0c3141e2008-04-15 00:22:02 -070061static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +010062static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
63static void wakeupdispatch(struct tipc_port *tport);
Ying Xuef288bef2012-08-21 11:16:57 +080064static void tipc_data_ready(struct sock *sk, int len);
65static void tipc_write_space(struct sock *sk);
Ying Xuec5fa7b32013-06-17 10:54:39 -040066static int release(struct socket *sock);
67static int accept(struct socket *sock, struct socket *new_sock, int flags);
Per Lidenb97bf3f2006-01-02 19:04:38 +010068
Florian Westphalbca65ea2008-02-07 18:18:01 -080069static const struct proto_ops packet_ops;
70static const struct proto_ops stream_ops;
71static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010072
73static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040074static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010075
Allan Stephense3ec9c72010-12-31 18:59:34 +000076static int sockets_enabled;
Per Lidenb97bf3f2006-01-02 19:04:38 +010077
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090078/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070079 * Revised TIPC socket locking policy:
80 *
81 * Most socket operations take the standard socket lock when they start
82 * and hold it until they finish (or until they need to sleep). Acquiring
83 * this lock grants the owner exclusive access to the fields of the socket
84 * data structures, with the exception of the backlog queue. A few socket
85 * operations can be done without taking the socket lock because they only
86 * read socket information that never changes during the life of the socket.
87 *
88 * Socket operations may acquire the lock for the associated TIPC port if they
89 * need to perform an operation on the port. If any routine needs to acquire
90 * both the socket lock and the port lock it must take the socket lock first
91 * to avoid the risk of deadlock.
92 *
93 * The dispatcher handling incoming messages cannot grab the socket lock in
94 * the standard fashion, since invoked it runs at the BH level and cannot block.
95 * Instead, it checks to see if the socket lock is currently owned by someone,
96 * and either handles the message itself or adds it to the socket's backlog
97 * queue; in the latter case the queued message is processed once the process
98 * owning the socket lock releases it.
99 *
100 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
101 * the problem of a blocked socket operation preventing any other operations
102 * from occurring. However, applications must be careful if they have
103 * multiple threads trying to send (or receive) on the same socket, as these
104 * operations might interfere with each other. For example, doing a connect
105 * and a receive at the same time might allow the receive to consume the
106 * ACK message meant for the connect. While additional work could be done
107 * to try and overcome this, it doesn't seem to be worthwhile at the present.
108 *
109 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
110 * that another operation that must be performed in a non-blocking manner is
111 * not delayed for very long because the lock has already been taken.
112 *
113 * NOTE: This code assumes that certain fields of a port/socket pair are
114 * constant over its lifetime; such fields can be examined without taking
115 * the socket lock and/or port lock, and do not need to be re-read even
116 * after resuming processing after waiting. These fields include:
117 * - socket type
118 * - pointer to socket sk structure (aka tipc_sock structure)
119 * - pointer to port structure
120 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122
123/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700124 * advance_rx_queue - discard first buffer in socket receive queue
125 *
126 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700128static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400130 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131}
132
133/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700134 * reject_rx_queue - reject all buffers in socket receive queue
135 *
136 * Caller must hold socket lock
137 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700138static void reject_rx_queue(struct sock *sk)
139{
140 struct sk_buff *buf;
141
Ying Xue9da3d472012-11-27 06:15:27 -0500142 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700143 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700144}
145
146/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400147 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700148 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100149 * @sock: pre-allocated socket structure
150 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800151 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900152 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700153 * This routine creates additional data structures used by the TIPC socket,
154 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155 *
156 * Returns 0 on success, errno otherwise
157 */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400158static int tipc_sk_create(struct net *net, struct socket *sock, int protocol,
159 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700161 const struct proto_ops *ops;
162 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700164 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700165
166 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 if (unlikely(protocol != 0))
168 return -EPROTONOSUPPORT;
169
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 switch (sock->type) {
171 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700172 ops = &stream_ops;
173 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 break;
175 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700176 ops = &packet_ops;
177 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 break;
179 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700181 ops = &msg_ops;
182 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 break;
Allan Stephens49978652006-06-25 23:47:18 -0700184 default:
Allan Stephens49978652006-06-25 23:47:18 -0700185 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100186 }
187
Allan Stephens0c3141e2008-04-15 00:22:02 -0700188 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400189 if (!kern)
190 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
191 else
192 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
193
Allan Stephens0c3141e2008-04-15 00:22:02 -0700194 if (sk == NULL)
195 return -ENOMEM;
196
197 /* Allocate TIPC port for socket to use */
Ying Xue3c5db8e2013-06-17 10:54:44 -0400198 tp_ptr = tipc_createport(sk, &dispatch, &wakeupdispatch,
199 TIPC_LOW_IMPORTANCE);
Allan Stephens0ea52242008-07-14 22:42:19 -0700200 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700201 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202 return -ENOMEM;
203 }
204
Allan Stephens0c3141e2008-04-15 00:22:02 -0700205 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700206 sock->ops = ops;
207 sock->state = state;
208
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700210 sk->sk_backlog_rcv = backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400211 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800212 sk->sk_data_ready = tipc_data_ready;
213 sk->sk_write_space = tipc_write_space;
Allan Stephens0ea52242008-07-14 22:42:19 -0700214 tipc_sk(sk)->p = tp_ptr;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400215 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700217 spin_unlock_bh(tp_ptr->lock);
218
Allan Stephens0c3141e2008-04-15 00:22:02 -0700219 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700220 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700221 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700222 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700223 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224
Per Lidenb97bf3f2006-01-02 19:04:38 +0100225 return 0;
226}
227
228/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400229 * tipc_sock_create_local - create TIPC socket from inside TIPC module
230 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
231 *
232 * We cannot use sock_creat_kern here because it bumps module user count.
233 * Since socket owner and creator is the same module we must make sure
234 * that module count remains zero for module local sockets, otherwise
235 * we cannot do rmmod.
236 *
237 * Returns 0 on success, errno otherwise
238 */
239int tipc_sock_create_local(int type, struct socket **res)
240{
241 int rc;
242 struct sock *sk;
243
244 rc = sock_create_lite(AF_TIPC, type, 0, res);
245 if (rc < 0) {
246 pr_err("Failed to create kernel socket\n");
247 return rc;
248 }
249 tipc_sk_create(&init_net, *res, 0, 1);
250
251 sk = (*res)->sk;
252
253 return 0;
254}
255
256/**
257 * tipc_sock_release_local - release socket created by tipc_sock_create_local
258 * @sock: the socket to be released.
259 *
260 * Module reference count is not incremented when such sockets are created,
261 * so we must keep it from being decremented when they are released.
262 */
263void tipc_sock_release_local(struct socket *sock)
264{
265 release(sock);
266 sock->ops = NULL;
267 sock_release(sock);
268}
269
270/**
271 * tipc_sock_accept_local - accept a connection on a socket created
272 * with tipc_sock_create_local. Use this function to avoid that
273 * module reference count is inadvertently incremented.
274 *
275 * @sock: the accepting socket
276 * @newsock: reference to the new socket to be created
277 * @flags: socket flags
278 */
279
280int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400281 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400282{
283 struct sock *sk = sock->sk;
284 int ret;
285
286 ret = sock_create_lite(sk->sk_family, sk->sk_type,
287 sk->sk_protocol, newsock);
288 if (ret < 0)
289 return ret;
290
291 ret = accept(sock, *newsock, flags);
292 if (ret < 0) {
293 sock_release(*newsock);
294 return ret;
295 }
296 (*newsock)->ops = sock->ops;
297 return ret;
298}
299
300/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301 * release - destroy a TIPC socket
302 * @sock: socket to destroy
303 *
304 * This routine cleans up any messages that are still queued on the socket.
305 * For DGRAM and RDM socket types, all queued messages are rejected.
306 * For SEQPACKET and STREAM socket types, the first message is rejected
307 * and any others are discarded. (If the first message on a STREAM socket
308 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900309 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 * NOTE: Rejected messages are not necessarily returned to the sender! They
311 * are returned or discarded according to the "destination droppable" setting
312 * specified for the message by the sender.
313 *
314 * Returns 0 on success, errno otherwise
315 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316static int release(struct socket *sock)
317{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700319 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700321 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322
Allan Stephens0c3141e2008-04-15 00:22:02 -0700323 /*
324 * Exit if socket isn't fully initialized (occurs when a failed accept()
325 * releases a pre-allocated child socket that was never used)
326 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700327 if (sk == NULL)
328 return 0;
329
330 tport = tipc_sk_port(sk);
331 lock_sock(sk);
332
333 /*
334 * Reject all unreceived messages, except on an active connection
335 * (which disconnects locally & sends a 'FIN+' to peer)
336 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700338 buf = __skb_dequeue(&sk->sk_receive_queue);
339 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340 break;
Ying Xue40682432013-10-18 07:23:16 +0200341 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400342 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700343 else {
344 if ((sock->state == SS_CONNECTING) ||
345 (sock->state == SS_CONNECTED)) {
346 sock->state = SS_DISCONNECTING;
347 tipc_disconnect(tport->ref);
348 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700350 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 }
352
Allan Stephens0c3141e2008-04-15 00:22:02 -0700353 /*
354 * Delete TIPC port; this ensures no more messages are queued
355 * (also disconnects an active connection & sends a 'FIN-' to peer)
356 */
Ying Xue84602762013-12-27 10:18:28 +0800357 res = tipc_deleteport(tport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358
Allan Stephens0c3141e2008-04-15 00:22:02 -0700359 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100360 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361
Allan Stephens0c3141e2008-04-15 00:22:02 -0700362 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700363 sock->state = SS_DISCONNECTING;
364 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365
366 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700367 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369 return res;
370}
371
372/**
373 * bind - associate or disassocate TIPC name(s) with a socket
374 * @sock: socket structure
375 * @uaddr: socket address describing name(s) and desired operation
376 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900377 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378 * Name and name sequence binding is indicated using a positive scope value;
379 * a negative scope value unbinds the specified name. Specifying no name
380 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900381 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700383 *
384 * NOTE: This routine doesn't need to take the socket lock since it doesn't
385 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
388{
Ying Xue84602762013-12-27 10:18:28 +0800389 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100390 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Ying Xue84602762013-12-27 10:18:28 +0800391 struct tipc_port *tport = tipc_sk_port(sock->sk);
392 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393
Ying Xue84602762013-12-27 10:18:28 +0800394 lock_sock(sk);
395 if (unlikely(!uaddr_len)) {
396 res = tipc_withdraw(tport, 0, NULL);
397 goto exit;
398 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900399
Ying Xue84602762013-12-27 10:18:28 +0800400 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
401 res = -EINVAL;
402 goto exit;
403 }
404 if (addr->family != AF_TIPC) {
405 res = -EAFNOSUPPORT;
406 goto exit;
407 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409 if (addr->addrtype == TIPC_ADDR_NAME)
410 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800411 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
412 res = -EAFNOSUPPORT;
413 goto exit;
414 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900415
Ying Xue13a2e892013-06-17 10:54:40 -0400416 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400417 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800418 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
419 res = -EACCES;
420 goto exit;
421 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400422
Ying Xue84602762013-12-27 10:18:28 +0800423 res = (addr->scope > 0) ?
424 tipc_publish(tport, addr->scope, &addr->addr.nameseq) :
425 tipc_withdraw(tport, -addr->scope, &addr->addr.nameseq);
426exit:
427 release_sock(sk);
428 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429}
430
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900431/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432 * get_name - get port ID of socket or peer socket
433 * @sock: socket structure
434 * @uaddr: area for returned socket address
435 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700436 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900437 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700439 *
Allan Stephens2da59912008-07-14 22:43:32 -0700440 * NOTE: This routine doesn't need to take the socket lock since it only
441 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000442 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900444static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445 int *uaddr_len, int peer)
446{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700448 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000450 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700451 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700452 if ((sock->state != SS_CONNECTED) &&
453 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
454 return -ENOTCONN;
455 addr->addr.id.ref = tsock->peer_name.ref;
456 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700457 } else {
Allan Stephensb924dcf2010-11-30 12:01:03 +0000458 addr->addr.id.ref = tsock->p->ref;
459 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700460 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461
462 *uaddr_len = sizeof(*addr);
463 addr->addrtype = TIPC_ADDR_ID;
464 addr->family = AF_TIPC;
465 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466 addr->addr.name.domain = 0;
467
Allan Stephens0c3141e2008-04-15 00:22:02 -0700468 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469}
470
471/**
472 * poll - read and possibly block on pollmask
473 * @file: file structure associated with the socket
474 * @sock: socket for which to calculate the poll bits
475 * @wait: ???
476 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700477 * Returns pollmask value
478 *
479 * COMMENTARY:
480 * It appears that the usual socket locking mechanisms are not useful here
481 * since the pollmask info is potentially out-of-date the moment this routine
482 * exits. TCP and other protocols seem to rely on higher level poll routines
483 * to handle any preventable race conditions, so TIPC will do the same ...
484 *
485 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700486 *
Allan Stephensf662c072010-08-17 11:00:06 +0000487 * socket state flags set
488 * ------------ ---------
489 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200490 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000491 *
492 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
493 * no write flags
494 *
495 * connected POLLIN/POLLRDNORM if data in rx queue
496 * POLLOUT if port is not congested
497 *
498 * disconnecting POLLIN/POLLRDNORM/POLLHUP
499 * no write flags
500 *
501 * listening POLLIN if SYN in rx queue
502 * no write flags
503 *
504 * ready POLLIN/POLLRDNORM if data in rx queue
505 * [connectionless] POLLOUT (since port cannot be congested)
506 *
507 * IMPORTANT: The fact that a read or write operation is indicated does NOT
508 * imply that the operation will succeed, merely that it should be performed
509 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100510 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900511static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512 poll_table *wait)
513{
Allan Stephens9b674e82008-03-26 16:48:21 -0700514 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000515 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700516
Ying Xuef288bef2012-08-21 11:16:57 +0800517 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700518
Allan Stephensf662c072010-08-17 11:00:06 +0000519 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200520 case SS_UNCONNECTED:
521 if (!tipc_sk_port(sk)->congested)
522 mask |= POLLOUT;
523 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000524 case SS_READY:
525 case SS_CONNECTED:
526 if (!tipc_sk_port(sk)->congested)
527 mask |= POLLOUT;
528 /* fall thru' */
529 case SS_CONNECTING:
530 case SS_LISTENING:
531 if (!skb_queue_empty(&sk->sk_receive_queue))
532 mask |= (POLLIN | POLLRDNORM);
533 break;
534 case SS_DISCONNECTING:
535 mask = (POLLIN | POLLRDNORM | POLLHUP);
536 break;
537 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700538
539 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540}
541
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900542/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 * dest_name_check - verify user is permitted to send to specified port name
544 * @dest: destination address
545 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900546 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 * Prevents restricted configuration commands from being issued by
548 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900549 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 * Returns 0 if permission is granted, otherwise errno
551 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800552static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553{
554 struct tipc_cfg_msg_hdr hdr;
555
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900556 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
557 return 0;
558 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
559 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900560 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
561 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100562
Allan Stephens3f8dd942011-01-18 13:09:29 -0500563 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
564 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900565 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700567 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900569
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570 return 0;
571}
572
573/**
574 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700575 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 * @sock: socket structure
577 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700578 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900579 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100580 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900581 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
583 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900584 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 * Returns the number of bytes sent on success, or errno otherwise
586 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587static int send_msg(struct kiocb *iocb, struct socket *sock,
588 struct msghdr *m, size_t total_len)
589{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700590 struct sock *sk = sock->sk;
591 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900592 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 int needs_conn;
Ying Xue1d835872011-07-06 05:53:15 -0400594 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 int res = -EINVAL;
596
597 if (unlikely(!dest))
598 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700599 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
600 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601 return -EINVAL;
Ying Xue97f8b872013-01-31 21:51:47 +0100602 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400603 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604
Allan Stephens0c3141e2008-04-15 00:22:02 -0700605 if (iocb)
606 lock_sock(sk);
607
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 needs_conn = (sock->state != SS_READY);
609 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700610 if (sock->state == SS_LISTENING) {
611 res = -EPIPE;
612 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700613 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700614 if (sock->state != SS_UNCONNECTED) {
615 res = -EISCONN;
616 goto exit;
617 }
Erik Hugne5d21cb72013-06-17 10:54:38 -0400618 if (tport->published) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700619 res = -EOPNOTSUPP;
620 goto exit;
621 }
622 if (dest->addrtype == TIPC_ADDR_NAME) {
623 tport->conn_type = dest->addr.name.name.type;
624 tport->conn_instance = dest->addr.name.name.instance;
625 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626
627 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700628 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 }
630
Ying Xue1d835872011-07-06 05:53:15 -0400631 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
632
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900633 do {
634 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000635 res = dest_name_check(dest, m);
636 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700637 break;
638 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900639 &dest->addr.name.name,
640 dest->addr.name.domain,
Allan Stephens26896902011-04-21 10:42:07 -0500641 m->msg_iov,
642 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000643 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700644 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900645 &dest->addr.id,
Allan Stephens26896902011-04-21 10:42:07 -0500646 m->msg_iov,
647 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000648 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649 if (needs_conn) {
650 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700651 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652 }
Allan Stephens2db99832010-12-31 18:59:33 +0000653 res = dest_name_check(dest, m);
654 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700655 break;
656 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900657 &dest->addr.nameseq,
Allan Stephens26896902011-04-21 10:42:07 -0500658 m->msg_iov,
659 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900660 }
661 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000662 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700663 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700664 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900665 }
Ying Xue1d835872011-07-06 05:53:15 -0400666 if (timeout_val <= 0L) {
667 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700668 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700670 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400671 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
672 !tport->congested, timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700673 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900674 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700675
676exit:
677 if (iocb)
678 release_sock(sk);
679 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680}
681
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900682/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700684 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685 * @sock: socket structure
686 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700687 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900688 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900690 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100691 * Returns the number of bytes sent on success, or errno otherwise
692 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693static int send_packet(struct kiocb *iocb, struct socket *sock,
694 struct msghdr *m, size_t total_len)
695{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700696 struct sock *sk = sock->sk;
697 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900698 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Ying Xue1d835872011-07-06 05:53:15 -0400699 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100700 int res;
701
702 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100703 if (unlikely(dest))
704 return send_msg(iocb, sock, m, total_len);
705
Ying Xue97f8b872013-01-31 21:51:47 +0100706 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400707 return -EMSGSIZE;
708
Allan Stephens0c3141e2008-04-15 00:22:02 -0700709 if (iocb)
710 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711
Ying Xue1d835872011-07-06 05:53:15 -0400712 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
713
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900714 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700715 if (unlikely(sock->state != SS_CONNECTED)) {
716 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900717 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700718 else
719 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700720 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700721 }
722
Ying Xue9446b872013-10-18 07:23:15 +0200723 res = tipc_send(tport->ref, m->msg_iov, total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000724 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700725 break;
Ying Xue1d835872011-07-06 05:53:15 -0400726 if (timeout_val <= 0L) {
727 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700728 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700730 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400731 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
732 (!tport->congested || !tport->connected), timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700733 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900734 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700735
736 if (iocb)
737 release_sock(sk);
738 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100739}
740
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900741/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742 * send_stream - send stream-oriented data
743 * @iocb: (unused)
744 * @sock: socket structure
745 * @m: data to send
746 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900747 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900749 *
750 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700751 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753static int send_stream(struct kiocb *iocb, struct socket *sock,
754 struct msghdr *m, size_t total_len)
755{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700756 struct sock *sk = sock->sk;
757 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100758 struct msghdr my_msg;
759 struct iovec my_iov;
760 struct iovec *curr_iov;
761 int curr_iovlen;
762 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700763 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764 int curr_left;
765 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700766 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100767 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900768
Allan Stephens0c3141e2008-04-15 00:22:02 -0700769 lock_sock(sk);
770
Allan Stephens05646c92007-06-10 17:25:24 -0700771 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900772 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700773 if (sock->state == SS_UNCONNECTED) {
774 res = send_packet(NULL, sock, m, total_len);
775 goto exit;
776 } else if (sock->state == SS_DISCONNECTING) {
777 res = -EPIPE;
778 goto exit;
779 } else {
780 res = -ENOTCONN;
781 goto exit;
782 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900783 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100784
Allan Stephens0c3141e2008-04-15 00:22:02 -0700785 if (unlikely(m->msg_name)) {
786 res = -EISCONN;
787 goto exit;
788 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700789
Ying Xue97f8b872013-01-31 21:51:47 +0100790 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400791 res = -EMSGSIZE;
792 goto exit;
793 }
794
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900795 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 * Send each iovec entry using one or more messages
797 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900798 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799 * (i.e. one large iovec entry), but could be improved to pass sets
800 * of small iovec entries into send_packet().
801 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700802 curr_iov = m->msg_iov;
803 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804 my_msg.msg_iov = &my_iov;
805 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700806 my_msg.msg_flags = m->msg_flags;
807 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700808 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100809
Allan Stephens05646c92007-06-10 17:25:24 -0700810 hdr_size = msg_hdr_sz(&tport->phdr);
811
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812 while (curr_iovlen--) {
813 curr_start = curr_iov->iov_base;
814 curr_left = curr_iov->iov_len;
815
816 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700817 bytes_to_send = tport->max_pkt - hdr_size;
818 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
819 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
820 if (curr_left < bytes_to_send)
821 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822 my_iov.iov_base = curr_start;
823 my_iov.iov_len = bytes_to_send;
Allan Stephens26896902011-04-21 10:42:07 -0500824 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000825 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700826 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700827 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700828 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700829 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830 curr_left -= bytes_to_send;
831 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700832 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100833 }
834
835 curr_iov++;
836 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700837 res = bytes_sent;
838exit:
839 release_sock(sk);
840 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100841}
842
843/**
844 * auto_connect - complete connection setup to a remote port
845 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900847 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100848 * Returns 0 on success, errno otherwise
849 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700850static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851{
Allan Stephens2da59912008-07-14 22:43:32 -0700852 struct tipc_sock *tsock = tipc_sk(sock->sk);
Ying Xue584d24b2012-11-29 18:51:19 -0500853 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854
Allan Stephens2da59912008-07-14 22:43:32 -0700855 tsock->peer_name.ref = msg_origport(msg);
856 tsock->peer_name.node = msg_orignode(msg);
Ying Xue584d24b2012-11-29 18:51:19 -0500857 p_ptr = tipc_port_deref(tsock->p->ref);
858 if (!p_ptr)
859 return -EINVAL;
860
861 __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
862
863 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
864 return -EINVAL;
865 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866 sock->state = SS_CONNECTED;
867 return 0;
868}
869
870/**
871 * set_orig_addr - capture sender's address for received message
872 * @m: descriptor for message info
873 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900874 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100875 * Note: Address is not captured if not requested by receiver.
876 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800877static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900879 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900881 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100882 addr->family = AF_TIPC;
883 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000884 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885 addr->addr.id.ref = msg_origport(msg);
886 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000887 addr->addr.name.domain = 0; /* could leave uninitialized */
888 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889 m->msg_namelen = sizeof(struct sockaddr_tipc);
890 }
891}
892
893/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900894 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100895 * @m: descriptor for message info
896 * @msg: received message header
897 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900898 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900900 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 * Returns 0 if successful, otherwise errno
902 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800903static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400904 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905{
906 u32 anc_data[3];
907 u32 err;
908 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700909 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910 int res;
911
912 if (likely(m->msg_controllen == 0))
913 return 0;
914
915 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916 err = msg ? msg_errcode(msg) : 0;
917 if (unlikely(err)) {
918 anc_data[0] = err;
919 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000920 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
921 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000923 if (anc_data[1]) {
924 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
925 msg_data(msg));
926 if (res)
927 return res;
928 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100929 }
930
931 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100932 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
933 switch (dest_type) {
934 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700935 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936 anc_data[0] = msg_nametype(msg);
937 anc_data[1] = msg_namelower(msg);
938 anc_data[2] = msg_namelower(msg);
939 break;
940 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700941 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942 anc_data[0] = msg_nametype(msg);
943 anc_data[1] = msg_namelower(msg);
944 anc_data[2] = msg_nameupper(msg);
945 break;
946 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700947 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100948 anc_data[0] = tport->conn_type;
949 anc_data[1] = tport->conn_instance;
950 anc_data[2] = tport->conn_instance;
951 break;
952 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700953 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100954 }
Allan Stephens2db99832010-12-31 18:59:33 +0000955 if (has_name) {
956 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
957 if (res)
958 return res;
959 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100960
961 return 0;
962}
963
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900964/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100965 * recv_msg - receive packet-oriented message
966 * @iocb: (unused)
967 * @m: descriptor for message info
968 * @buf_len: total size of user buffer area
969 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900970 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100971 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
972 * If the complete message doesn't fit in user area, truncate it.
973 *
974 * Returns size of returned message data, errno otherwise
975 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976static int recv_msg(struct kiocb *iocb, struct socket *sock,
977 struct msghdr *m, size_t buf_len, int flags)
978{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700979 struct sock *sk = sock->sk;
980 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100981 struct sk_buff *buf;
982 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500983 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984 unsigned int sz;
985 u32 err;
986 int res;
987
Allan Stephens0c3141e2008-04-15 00:22:02 -0700988 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989 if (unlikely(!buf_len))
990 return -EINVAL;
991
Allan Stephens0c3141e2008-04-15 00:22:02 -0700992 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993
Allan Stephens0c3141e2008-04-15 00:22:02 -0700994 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995 res = -ENOTCONN;
996 goto exit;
997 }
998
Allan Stephens71092ea2011-02-23 14:52:14 -0500999 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001000restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001001
Allan Stephens0c3141e2008-04-15 00:22:02 -07001002 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001003 while (skb_queue_empty(&sk->sk_receive_queue)) {
1004 if (sock->state == SS_DISCONNECTING) {
1005 res = -ENOTCONN;
1006 goto exit;
1007 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001008 if (timeout <= 0L) {
1009 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001010 goto exit;
1011 }
1012 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001013 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1014 tipc_rx_ready(sock),
1015 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001016 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001017 }
1018
1019 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001020 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001021 msg = buf_msg(buf);
1022 sz = msg_data_sz(msg);
1023 err = msg_errcode(msg);
1024
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001027 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 goto restart;
1029 }
1030
1031 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032 set_orig_addr(m, msg);
1033
1034 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001035 res = anc_data_recv(m, msg, tport);
1036 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037 goto exit;
1038
1039 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 if (!err) {
1041 if (unlikely(buf_len < sz)) {
1042 sz = buf_len;
1043 m->msg_flags |= MSG_TRUNC;
1044 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001045 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1046 m->msg_iov, sz);
1047 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049 res = sz;
1050 } else {
1051 if ((sock->state == SS_READY) ||
1052 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1053 res = 0;
1054 else
1055 res = -ECONNRESET;
1056 }
1057
1058 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001060 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -07001061 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1062 tipc_acknowledge(tport->ref, tport->conn_unacked);
1063 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001064 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001066 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 return res;
1068}
1069
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001070/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 * recv_stream - receive stream-oriented data
1072 * @iocb: (unused)
1073 * @m: descriptor for message info
1074 * @buf_len: total size of user buffer area
1075 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001076 *
1077 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 * will optionally wait for more; never truncates data.
1079 *
1080 * Returns size of returned message data, errno otherwise
1081 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082static int recv_stream(struct kiocb *iocb, struct socket *sock,
1083 struct msghdr *m, size_t buf_len, int flags)
1084{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001085 struct sock *sk = sock->sk;
1086 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001087 struct sk_buff *buf;
1088 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -05001089 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001091 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001094 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001095
1096 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001097 if (unlikely(!buf_len))
1098 return -EINVAL;
1099
Allan Stephens0c3141e2008-04-15 00:22:02 -07001100 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101
Erik Hugne5d21cb72013-06-17 10:54:38 -04001102 if (unlikely((sock->state == SS_UNCONNECTED))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103 res = -ENOTCONN;
1104 goto exit;
1105 }
1106
Florian Westphal3720d402010-08-17 11:00:04 +00001107 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Allan Stephens71092ea2011-02-23 14:52:14 -05001108 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001109
Allan Stephens0c3141e2008-04-15 00:22:02 -07001110restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001111 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001112 while (skb_queue_empty(&sk->sk_receive_queue)) {
1113 if (sock->state == SS_DISCONNECTING) {
1114 res = -ENOTCONN;
1115 goto exit;
1116 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001117 if (timeout <= 0L) {
1118 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001119 goto exit;
1120 }
1121 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001122 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1123 tipc_rx_ready(sock),
1124 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001125 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001126 }
1127
1128 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001129 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130 msg = buf_msg(buf);
1131 sz = msg_data_sz(msg);
1132 err = msg_errcode(msg);
1133
1134 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001136 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 goto restart;
1138 }
1139
1140 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 if (sz_copied == 0) {
1142 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001143 res = anc_data_recv(m, msg, tport);
1144 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 goto exit;
1146 }
1147
1148 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001149 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001150 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001151
Allan Stephens0232fd02011-02-21 09:45:40 -05001152 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153 needed = (buf_len - sz_copied);
1154 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001155
1156 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1157 m->msg_iov, sz_to_copy);
1158 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001160
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 sz_copied += sz_to_copy;
1162
1163 if (sz_to_copy < sz) {
1164 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001165 TIPC_SKB_CB(buf)->handle =
1166 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 goto exit;
1168 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 } else {
1170 if (sz_copied != 0)
1171 goto exit; /* can't add error msg to valid data */
1172
1173 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1174 res = 0;
1175 else
1176 res = -ECONNRESET;
1177 }
1178
1179 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001181 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1182 tipc_acknowledge(tport->ref, tport->conn_unacked);
1183 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001184 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185
1186 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001187 if ((sz_copied < buf_len) && /* didn't get all requested data */
1188 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001189 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001190 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1191 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192 goto restart;
1193
1194exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001195 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001196 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197}
1198
1199/**
Ying Xuef288bef2012-08-21 11:16:57 +08001200 * tipc_write_space - wake up thread if port congestion is released
1201 * @sk: socket
1202 */
1203static void tipc_write_space(struct sock *sk)
1204{
1205 struct socket_wq *wq;
1206
1207 rcu_read_lock();
1208 wq = rcu_dereference(sk->sk_wq);
1209 if (wq_has_sleeper(wq))
1210 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1211 POLLWRNORM | POLLWRBAND);
1212 rcu_read_unlock();
1213}
1214
1215/**
1216 * tipc_data_ready - wake up threads to indicate messages have been received
1217 * @sk: socket
1218 * @len: the length of messages
1219 */
1220static void tipc_data_ready(struct sock *sk, int len)
1221{
1222 struct socket_wq *wq;
1223
1224 rcu_read_lock();
1225 wq = rcu_dereference(sk->sk_wq);
1226 if (wq_has_sleeper(wq))
1227 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1228 POLLRDNORM | POLLRDBAND);
1229 rcu_read_unlock();
1230}
1231
1232/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001233 * filter_connect - Handle all incoming messages for a connection-based socket
1234 * @tsock: TIPC socket
1235 * @msg: message
1236 *
1237 * Returns TIPC error status code and socket error status code
1238 * once it encounters some errors
1239 */
1240static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
1241{
1242 struct socket *sock = tsock->sk.sk_socket;
1243 struct tipc_msg *msg = buf_msg(*buf);
Ying Xue584d24b2012-11-29 18:51:19 -05001244 struct sock *sk = &tsock->sk;
Ying Xue7e6c1312012-11-29 18:39:14 -05001245 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001246 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001247
1248 if (msg_mcast(msg))
1249 return retval;
1250
1251 switch ((int)sock->state) {
1252 case SS_CONNECTED:
1253 /* Accept only connection-based messages sent by peer */
1254 if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
1255 if (unlikely(msg_errcode(msg))) {
1256 sock->state = SS_DISCONNECTING;
1257 __tipc_disconnect(tsock->p);
1258 }
1259 retval = TIPC_OK;
1260 }
1261 break;
1262 case SS_CONNECTING:
1263 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001264 if (unlikely(msg_errcode(msg))) {
1265 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001266 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001267 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001268 break;
1269 }
1270
1271 if (unlikely(!msg_connected(msg)))
1272 break;
1273
1274 res = auto_connect(sock, msg);
1275 if (res) {
1276 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001277 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001278 retval = TIPC_OK;
1279 break;
1280 }
1281
1282 /* If an incoming message is an 'ACK-', it should be
1283 * discarded here because it doesn't contain useful
1284 * data. In addition, we should try to wake up
1285 * connect() routine if sleeping.
1286 */
1287 if (msg_data_sz(msg) == 0) {
1288 kfree_skb(*buf);
1289 *buf = NULL;
1290 if (waitqueue_active(sk_sleep(sk)))
1291 wake_up_interruptible(sk_sleep(sk));
1292 }
1293 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001294 break;
1295 case SS_LISTENING:
1296 case SS_UNCONNECTED:
1297 /* Accept only SYN message */
1298 if (!msg_connected(msg) && !(msg_errcode(msg)))
1299 retval = TIPC_OK;
1300 break;
1301 case SS_DISCONNECTING:
1302 break;
1303 default:
1304 pr_err("Unknown socket state %u\n", sock->state);
1305 }
1306 return retval;
1307}
1308
1309/**
Ying Xueaba79f32013-01-20 23:30:09 +01001310 * rcvbuf_limit - get proper overload limit of socket receive queue
1311 * @sk: socket
1312 * @buf: message
1313 *
1314 * For all connection oriented messages, irrespective of importance,
1315 * the default overload value (i.e. 67MB) is set as limit.
1316 *
1317 * For all connectionless messages, by default new queue limits are
1318 * as belows:
1319 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001320 * TIPC_LOW_IMPORTANCE (4 MB)
1321 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1322 * TIPC_HIGH_IMPORTANCE (16 MB)
1323 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001324 *
1325 * Returns overload limit according to corresponding message importance
1326 */
1327static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1328{
1329 struct tipc_msg *msg = buf_msg(buf);
1330 unsigned int limit;
1331
1332 if (msg_connected(msg))
Ying Xuecc79dd12013-06-17 10:54:37 -04001333 limit = sysctl_tipc_rmem[2];
Ying Xueaba79f32013-01-20 23:30:09 +01001334 else
Ying Xuecc79dd12013-06-17 10:54:37 -04001335 limit = sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1336 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001337 return limit;
1338}
1339
1340/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001341 * filter_rcv - validate incoming message
1342 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001344 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001345 * Enqueues message on receive queue if acceptable; optionally handles
1346 * disconnect indication for a connected socket.
1347 *
1348 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001349 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001350 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1351 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001352static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001354 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001356 unsigned int limit = rcvbuf_limit(sk, buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001357 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001360 if (msg_type(msg) > TIPC_DIRECT_MSG)
1361 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001362
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001364 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366 } else {
Ying Xue7e6c1312012-11-29 18:39:14 -05001367 res = filter_connect(tipc_sk(sk), &buf);
1368 if (res != TIPC_OK || buf == NULL)
1369 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370 }
1371
1372 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001373 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1374 return TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375
Ying Xueaba79f32013-01-20 23:30:09 +01001376 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001377 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001378 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001379 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001380
Ying Xuef288bef2012-08-21 11:16:57 +08001381 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001382 return TIPC_OK;
1383}
1384
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001385/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001386 * backlog_rcv - handle incoming message from backlog queue
1387 * @sk: socket
1388 * @buf: message
1389 *
1390 * Caller must hold socket lock, but not port lock.
1391 *
1392 * Returns 0
1393 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001394static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1395{
1396 u32 res;
1397
1398 res = filter_rcv(sk, buf);
1399 if (res)
1400 tipc_reject_msg(buf, res);
1401 return 0;
1402}
1403
1404/**
1405 * dispatch - handle incoming message
1406 * @tport: TIPC port that received message
1407 * @buf: message
1408 *
1409 * Called with port lock already taken.
1410 *
1411 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1412 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001413static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1414{
Ying Xuec0fee8a2013-06-17 10:54:46 -04001415 struct sock *sk = tport->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001416 u32 res;
1417
1418 /*
1419 * Process message if socket is unlocked; otherwise add to backlog queue
1420 *
1421 * This code is based on sk_receive_skb(), but must be distinct from it
1422 * since a TIPC-specific filter/reject mechanism is utilized
1423 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001424 bh_lock_sock(sk);
1425 if (!sock_owned_by_user(sk)) {
1426 res = filter_rcv(sk, buf);
1427 } else {
Ying Xueaba79f32013-01-20 23:30:09 +01001428 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
Zhu Yi53eecb12010-03-04 18:01:45 +00001429 res = TIPC_ERR_OVERLOAD;
1430 else
1431 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001432 }
1433 bh_unlock_sock(sk);
1434
1435 return res;
1436}
1437
1438/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001439 * wakeupdispatch - wake up port after congestion
1440 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001441 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001442 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001443 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444static void wakeupdispatch(struct tipc_port *tport)
1445{
Ying Xuec0fee8a2013-06-17 10:54:46 -04001446 struct sock *sk = tport->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001447
Ying Xuef288bef2012-08-21 11:16:57 +08001448 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001449}
1450
1451/**
1452 * connect - establish a connection to another TIPC port
1453 * @sock: socket structure
1454 * @dest: socket address for destination port
1455 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001456 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001457 *
1458 * Returns 0 on success, errno otherwise
1459 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001460static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001461 int flags)
1462{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001463 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001464 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1465 struct msghdr m = {NULL,};
Allan Stephensa0f40f02011-05-26 13:44:34 -04001466 unsigned int timeout;
Allan Stephensb89741a2008-04-15 00:20:37 -07001467 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468
Allan Stephens0c3141e2008-04-15 00:22:02 -07001469 lock_sock(sk);
1470
Allan Stephensb89741a2008-04-15 00:20:37 -07001471 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001472 if (sock->state == SS_READY) {
1473 res = -EOPNOTSUPP;
1474 goto exit;
1475 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001476
Allan Stephensb89741a2008-04-15 00:20:37 -07001477 /*
1478 * Reject connection attempt using multicast address
1479 *
1480 * Note: send_msg() validates the rest of the address fields,
1481 * so there's no need to do it here
1482 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001483 if (dst->addrtype == TIPC_ADDR_MCAST) {
1484 res = -EINVAL;
1485 goto exit;
1486 }
1487
Ying Xue584d24b2012-11-29 18:51:19 -05001488 timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001489
Ying Xue584d24b2012-11-29 18:51:19 -05001490 switch (sock->state) {
1491 case SS_UNCONNECTED:
1492 /* Send a 'SYN-' to destination */
1493 m.msg_name = dest;
1494 m.msg_namelen = destlen;
1495
1496 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1497 * indicate send_msg() is never blocked.
1498 */
1499 if (!timeout)
1500 m.msg_flags = MSG_DONTWAIT;
1501
1502 res = send_msg(NULL, sock, &m, 0);
1503 if ((res < 0) && (res != -EWOULDBLOCK))
1504 goto exit;
1505
1506 /* Just entered SS_CONNECTING state; the only
1507 * difference is that return value in non-blocking
1508 * case is EINPROGRESS, rather than EALREADY.
1509 */
1510 res = -EINPROGRESS;
1511 break;
1512 case SS_CONNECTING:
1513 res = -EALREADY;
1514 break;
1515 case SS_CONNECTED:
1516 res = -EISCONN;
1517 break;
1518 default:
1519 res = -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001520 goto exit;
Allan Stephensb89741a2008-04-15 00:20:37 -07001521 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001522
Ying Xue584d24b2012-11-29 18:51:19 -05001523 if (sock->state == SS_CONNECTING) {
1524 if (!timeout)
1525 goto exit;
1526
1527 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1528 release_sock(sk);
1529 res = wait_event_interruptible_timeout(*sk_sleep(sk),
1530 sock->state != SS_CONNECTING,
1531 timeout ? (long)msecs_to_jiffies(timeout)
1532 : MAX_SCHEDULE_TIMEOUT);
1533 lock_sock(sk);
1534 if (res <= 0) {
1535 if (res == 0)
1536 res = -ETIMEDOUT;
1537 else
1538 ; /* leave "res" unchanged */
1539 goto exit;
1540 }
1541 }
1542
1543 if (unlikely(sock->state == SS_DISCONNECTING))
1544 res = sock_error(sk);
1545 else
1546 res = 0;
1547
Allan Stephens0c3141e2008-04-15 00:22:02 -07001548exit:
1549 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001550 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001551}
1552
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001553/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001554 * listen - allow socket to listen for incoming connections
1555 * @sock: socket structure
1556 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001557 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001558 * Returns 0 on success, errno otherwise
1559 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001560static int listen(struct socket *sock, int len)
1561{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001562 struct sock *sk = sock->sk;
1563 int res;
1564
1565 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001566
Ying Xue245f3d32011-07-06 06:01:13 -04001567 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001568 res = -EINVAL;
1569 else {
1570 sock->state = SS_LISTENING;
1571 res = 0;
1572 }
1573
1574 release_sock(sk);
1575 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001576}
1577
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001578/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001579 * accept - wait for connection request
1580 * @sock: listening socket
1581 * @newsock: new socket that is to be connected
1582 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001583 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001584 * Returns 0 on success, errno otherwise
1585 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001586static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001587{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001588 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001589 struct sk_buff *buf;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001590 struct tipc_sock *new_tsock;
1591 struct tipc_port *new_tport;
1592 struct tipc_msg *msg;
1593 u32 new_ref;
1594
Allan Stephens0c3141e2008-04-15 00:22:02 -07001595 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001596
Allan Stephens0c3141e2008-04-15 00:22:02 -07001597 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001598
Allan Stephens0c3141e2008-04-15 00:22:02 -07001599 if (sock->state != SS_LISTENING) {
1600 res = -EINVAL;
1601 goto exit;
1602 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001603
Allan Stephens0c3141e2008-04-15 00:22:02 -07001604 while (skb_queue_empty(&sk->sk_receive_queue)) {
1605 if (flags & O_NONBLOCK) {
1606 res = -EWOULDBLOCK;
1607 goto exit;
1608 }
1609 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001610 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001611 (!skb_queue_empty(&sk->sk_receive_queue)));
1612 lock_sock(sk);
1613 if (res)
1614 goto exit;
1615 }
1616
1617 buf = skb_peek(&sk->sk_receive_queue);
1618
Ying Xuec5fa7b32013-06-17 10:54:39 -04001619 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001620 if (res)
1621 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001622
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001623 new_sk = new_sock->sk;
1624 new_tsock = tipc_sk(new_sk);
1625 new_tport = new_tsock->p;
1626 new_ref = new_tport->ref;
1627 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001628
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001629 /* we lock on new_sk; but lockdep sees the lock on sk */
1630 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001631
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001632 /*
1633 * Reject any stray messages received by new socket
1634 * before the socket lock was taken (very, very unlikely)
1635 */
1636 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001637
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001638 /* Connect new socket to it's peer */
1639 new_tsock->peer_name.ref = msg_origport(msg);
1640 new_tsock->peer_name.node = msg_orignode(msg);
1641 tipc_connect(new_ref, &new_tsock->peer_name);
1642 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001643
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001644 tipc_set_portimportance(new_ref, msg_importance(msg));
1645 if (msg_named(msg)) {
1646 new_tport->conn_type = msg_nametype(msg);
1647 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001648 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001649
1650 /*
1651 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1652 * Respond to 'SYN+' by queuing it on new socket.
1653 */
1654 if (!msg_data_sz(msg)) {
1655 struct msghdr m = {NULL,};
1656
1657 advance_rx_queue(sk);
1658 send_packet(NULL, new_sock, &m, 0);
1659 } else {
1660 __skb_dequeue(&sk->sk_receive_queue);
1661 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001662 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001663 }
1664 release_sock(new_sk);
1665
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001667 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001668 return res;
1669}
1670
1671/**
1672 * shutdown - shutdown socket connection
1673 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001674 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001675 *
1676 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001677 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678 * Returns 0 on success, errno otherwise
1679 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001680static int shutdown(struct socket *sock, int how)
1681{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001682 struct sock *sk = sock->sk;
1683 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001684 struct sk_buff *buf;
1685 int res;
1686
Allan Stephense247a8f2008-03-06 15:05:38 -08001687 if (how != SHUT_RDWR)
1688 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001689
Allan Stephens0c3141e2008-04-15 00:22:02 -07001690 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001691
1692 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001693 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001694 case SS_CONNECTED:
1695
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001697 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001698 buf = __skb_dequeue(&sk->sk_receive_queue);
1699 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001700 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001701 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702 goto restart;
1703 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001704 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001705 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001706 } else {
1707 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001709
1710 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001711
1712 /* fall through */
1713
1714 case SS_DISCONNECTING:
1715
Ying Xue75031152012-10-29 09:38:15 -04001716 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001717 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001718
1719 /* Wake up anyone sleeping in poll */
1720 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001721 res = 0;
1722 break;
1723
1724 default:
1725 res = -ENOTCONN;
1726 }
1727
Allan Stephens0c3141e2008-04-15 00:22:02 -07001728 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001729 return res;
1730}
1731
1732/**
1733 * setsockopt - set socket option
1734 * @sock: socket structure
1735 * @lvl: option level
1736 * @opt: option identifier
1737 * @ov: pointer to new option value
1738 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001739 *
1740 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001742 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001743 * Returns 0 on success, errno otherwise
1744 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001745static int setsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
1746 unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001747{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001748 struct sock *sk = sock->sk;
1749 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001750 u32 value;
1751 int res;
1752
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001753 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1754 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001755 if (lvl != SOL_TIPC)
1756 return -ENOPROTOOPT;
1757 if (ol < sizeof(value))
1758 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001759 res = get_user(value, (u32 __user *)ov);
1760 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001761 return res;
1762
Allan Stephens0c3141e2008-04-15 00:22:02 -07001763 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001764
Per Lidenb97bf3f2006-01-02 19:04:38 +01001765 switch (opt) {
1766 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001767 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001768 break;
1769 case TIPC_SRC_DROPPABLE:
1770 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001771 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001772 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001773 res = -ENOPROTOOPT;
1774 break;
1775 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001776 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001777 break;
1778 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001779 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001780 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001781 break;
1782 default:
1783 res = -EINVAL;
1784 }
1785
Allan Stephens0c3141e2008-04-15 00:22:02 -07001786 release_sock(sk);
1787
Per Lidenb97bf3f2006-01-02 19:04:38 +01001788 return res;
1789}
1790
1791/**
1792 * getsockopt - get socket option
1793 * @sock: socket structure
1794 * @lvl: option level
1795 * @opt: option identifier
1796 * @ov: receptacle for option value
1797 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001798 *
1799 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001801 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001802 * Returns 0 on success, errno otherwise
1803 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001804static int getsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
1805 int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001806{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001807 struct sock *sk = sock->sk;
1808 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001809 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001810 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001811 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001812
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001813 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1814 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001815 if (lvl != SOL_TIPC)
1816 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001817 res = get_user(len, ol);
1818 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001819 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001820
Allan Stephens0c3141e2008-04-15 00:22:02 -07001821 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001822
1823 switch (opt) {
1824 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001825 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826 break;
1827 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001828 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829 break;
1830 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001831 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001832 break;
1833 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001834 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001835 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001836 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001837 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001838 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001839 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001840 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001841 value = skb_queue_len(&sk->sk_receive_queue);
1842 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001843 default:
1844 res = -EINVAL;
1845 }
1846
Allan Stephens0c3141e2008-04-15 00:22:02 -07001847 release_sock(sk);
1848
Paul Gortmaker25860c32010-12-31 18:59:31 +00001849 if (res)
1850 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001851
Paul Gortmaker25860c32010-12-31 18:59:31 +00001852 if (len < sizeof(value))
1853 return -EINVAL;
1854
1855 if (copy_to_user(ov, &value, sizeof(value)))
1856 return -EFAULT;
1857
1858 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001859}
1860
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001861/* Protocol switches for the various types of TIPC sockets */
1862
Florian Westphalbca65ea2008-02-07 18:18:01 -08001863static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001864 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001865 .family = AF_TIPC,
1866 .release = release,
1867 .bind = bind,
1868 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001869 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001870 .accept = sock_no_accept,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001871 .getname = get_name,
1872 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001873 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001874 .listen = sock_no_listen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001875 .shutdown = shutdown,
1876 .setsockopt = setsockopt,
1877 .getsockopt = getsockopt,
1878 .sendmsg = send_msg,
1879 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001880 .mmap = sock_no_mmap,
1881 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001882};
1883
Florian Westphalbca65ea2008-02-07 18:18:01 -08001884static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001885 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886 .family = AF_TIPC,
1887 .release = release,
1888 .bind = bind,
1889 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001890 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001891 .accept = accept,
1892 .getname = get_name,
1893 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001894 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001895 .listen = listen,
1896 .shutdown = shutdown,
1897 .setsockopt = setsockopt,
1898 .getsockopt = getsockopt,
1899 .sendmsg = send_packet,
1900 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001901 .mmap = sock_no_mmap,
1902 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001903};
1904
Florian Westphalbca65ea2008-02-07 18:18:01 -08001905static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001906 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907 .family = AF_TIPC,
1908 .release = release,
1909 .bind = bind,
1910 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001911 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001912 .accept = accept,
1913 .getname = get_name,
1914 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001915 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001916 .listen = listen,
1917 .shutdown = shutdown,
1918 .setsockopt = setsockopt,
1919 .getsockopt = getsockopt,
1920 .sendmsg = send_stream,
1921 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001922 .mmap = sock_no_mmap,
1923 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001924};
1925
Florian Westphalbca65ea2008-02-07 18:18:01 -08001926static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001927 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001928 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04001929 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01001930};
1931
1932static struct proto tipc_proto = {
1933 .name = "TIPC",
1934 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04001935 .obj_size = sizeof(struct tipc_sock),
1936 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01001937};
1938
Ying Xuec5fa7b32013-06-17 10:54:39 -04001939static struct proto tipc_proto_kern = {
1940 .name = "TIPC",
1941 .obj_size = sizeof(struct tipc_sock),
1942 .sysctl_rmem = sysctl_tipc_rmem
1943};
1944
Per Lidenb97bf3f2006-01-02 19:04:38 +01001945/**
Per Liden4323add2006-01-18 00:38:21 +01001946 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001947 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001948 * Returns 0 on success, errno otherwise
1949 */
Per Liden4323add2006-01-18 00:38:21 +01001950int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001951{
1952 int res;
1953
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001954 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001955 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001956 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001957 goto out;
1958 }
1959
1960 res = sock_register(&tipc_family_ops);
1961 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001962 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001963 proto_unregister(&tipc_proto);
1964 goto out;
1965 }
1966
1967 sockets_enabled = 1;
1968 out:
1969 return res;
1970}
1971
1972/**
Per Liden4323add2006-01-18 00:38:21 +01001973 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001974 */
Per Liden4323add2006-01-18 00:38:21 +01001975void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001976{
1977 if (!sockets_enabled)
1978 return;
1979
1980 sockets_enabled = 0;
1981 sock_unregister(tipc_family_ops.family);
1982 proto_unregister(&tipc_proto);
1983}