blob: ce8249c768271b9f778df694261d09a6845c5f05 [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;
Allan Stephens0232fd02011-02-21 09:45:40 -0500341 if (TIPC_SKB_CB(buf)->handle != 0)
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 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700357 res = tipc_deleteport(tport->ref);
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{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700390 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391
Allan Stephens0c3141e2008-04-15 00:22:02 -0700392 if (unlikely(!uaddr_len))
393 return tipc_withdraw(portref, 0, NULL);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900394
Allan Stephens0c3141e2008-04-15 00:22:02 -0700395 if (uaddr_len < sizeof(struct sockaddr_tipc))
396 return -EINVAL;
397 if (addr->family != AF_TIPC)
398 return -EAFNOSUPPORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400 if (addr->addrtype == TIPC_ADDR_NAME)
401 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700402 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
403 return -EAFNOSUPPORT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900404
Ying Xue13a2e892013-06-17 10:54:40 -0400405 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400406 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
407 (addr->addr.nameseq.type != TIPC_CFG_SRV))
Allan Stephensc422f1b2011-11-02 15:49:40 -0400408 return -EACCES;
409
Allan Stephens0c3141e2008-04-15 00:22:02 -0700410 return (addr->scope > 0) ?
411 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
412 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413}
414
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900415/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 * get_name - get port ID of socket or peer socket
417 * @sock: socket structure
418 * @uaddr: area for returned socket address
419 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700420 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900421 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700423 *
Allan Stephens2da59912008-07-14 22:43:32 -0700424 * NOTE: This routine doesn't need to take the socket lock since it only
425 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000426 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900428static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429 int *uaddr_len, int peer)
430{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700432 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000434 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700435 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700436 if ((sock->state != SS_CONNECTED) &&
437 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
438 return -ENOTCONN;
439 addr->addr.id.ref = tsock->peer_name.ref;
440 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700441 } else {
Allan Stephensb924dcf2010-11-30 12:01:03 +0000442 addr->addr.id.ref = tsock->p->ref;
443 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700444 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445
446 *uaddr_len = sizeof(*addr);
447 addr->addrtype = TIPC_ADDR_ID;
448 addr->family = AF_TIPC;
449 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 addr->addr.name.domain = 0;
451
Allan Stephens0c3141e2008-04-15 00:22:02 -0700452 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453}
454
455/**
456 * poll - read and possibly block on pollmask
457 * @file: file structure associated with the socket
458 * @sock: socket for which to calculate the poll bits
459 * @wait: ???
460 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700461 * Returns pollmask value
462 *
463 * COMMENTARY:
464 * It appears that the usual socket locking mechanisms are not useful here
465 * since the pollmask info is potentially out-of-date the moment this routine
466 * exits. TCP and other protocols seem to rely on higher level poll routines
467 * to handle any preventable race conditions, so TIPC will do the same ...
468 *
469 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700470 *
Allan Stephensf662c072010-08-17 11:00:06 +0000471 * socket state flags set
472 * ------------ ---------
473 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200474 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000475 *
476 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
477 * no write flags
478 *
479 * connected POLLIN/POLLRDNORM if data in rx queue
480 * POLLOUT if port is not congested
481 *
482 * disconnecting POLLIN/POLLRDNORM/POLLHUP
483 * no write flags
484 *
485 * listening POLLIN if SYN in rx queue
486 * no write flags
487 *
488 * ready POLLIN/POLLRDNORM if data in rx queue
489 * [connectionless] POLLOUT (since port cannot be congested)
490 *
491 * IMPORTANT: The fact that a read or write operation is indicated does NOT
492 * imply that the operation will succeed, merely that it should be performed
493 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900495static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496 poll_table *wait)
497{
Allan Stephens9b674e82008-03-26 16:48:21 -0700498 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000499 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700500
Ying Xuef288bef2012-08-21 11:16:57 +0800501 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700502
Allan Stephensf662c072010-08-17 11:00:06 +0000503 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200504 case SS_UNCONNECTED:
505 if (!tipc_sk_port(sk)->congested)
506 mask |= POLLOUT;
507 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000508 case SS_READY:
509 case SS_CONNECTED:
510 if (!tipc_sk_port(sk)->congested)
511 mask |= POLLOUT;
512 /* fall thru' */
513 case SS_CONNECTING:
514 case SS_LISTENING:
515 if (!skb_queue_empty(&sk->sk_receive_queue))
516 mask |= (POLLIN | POLLRDNORM);
517 break;
518 case SS_DISCONNECTING:
519 mask = (POLLIN | POLLRDNORM | POLLHUP);
520 break;
521 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700522
523 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524}
525
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900526/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527 * dest_name_check - verify user is permitted to send to specified port name
528 * @dest: destination address
529 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900530 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531 * Prevents restricted configuration commands from being issued by
532 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900533 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534 * Returns 0 if permission is granted, otherwise errno
535 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800536static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537{
538 struct tipc_cfg_msg_hdr hdr;
539
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900540 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
541 return 0;
542 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
543 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900544 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
545 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546
Allan Stephens3f8dd942011-01-18 13:09:29 -0500547 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
548 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900549 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700551 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900553
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 return 0;
555}
556
557/**
558 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700559 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560 * @sock: socket structure
561 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700562 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900563 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900565 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
567 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900568 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100569 * Returns the number of bytes sent on success, or errno otherwise
570 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571static int send_msg(struct kiocb *iocb, struct socket *sock,
572 struct msghdr *m, size_t total_len)
573{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700574 struct sock *sk = sock->sk;
575 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900576 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577 int needs_conn;
Ying Xue1d835872011-07-06 05:53:15 -0400578 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579 int res = -EINVAL;
580
581 if (unlikely(!dest))
582 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700583 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
584 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 return -EINVAL;
Ying Xue97f8b872013-01-31 21:51:47 +0100586 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400587 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588
Allan Stephens0c3141e2008-04-15 00:22:02 -0700589 if (iocb)
590 lock_sock(sk);
591
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592 needs_conn = (sock->state != SS_READY);
593 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700594 if (sock->state == SS_LISTENING) {
595 res = -EPIPE;
596 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700597 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700598 if (sock->state != SS_UNCONNECTED) {
599 res = -EISCONN;
600 goto exit;
601 }
Erik Hugne5d21cb72013-06-17 10:54:38 -0400602 if (tport->published) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700603 res = -EOPNOTSUPP;
604 goto exit;
605 }
606 if (dest->addrtype == TIPC_ADDR_NAME) {
607 tport->conn_type = dest->addr.name.name.type;
608 tport->conn_instance = dest->addr.name.name.instance;
609 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610
611 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700612 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 }
614
Ying Xue1d835872011-07-06 05:53:15 -0400615 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
616
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900617 do {
618 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000619 res = dest_name_check(dest, m);
620 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700621 break;
622 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900623 &dest->addr.name.name,
624 dest->addr.name.domain,
625 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500626 m->msg_iov,
627 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000628 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700629 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900630 &dest->addr.id,
631 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500632 m->msg_iov,
633 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000634 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635 if (needs_conn) {
636 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700637 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 }
Allan Stephens2db99832010-12-31 18:59:33 +0000639 res = dest_name_check(dest, m);
640 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700641 break;
642 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900643 &dest->addr.nameseq,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900644 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500645 m->msg_iov,
646 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900647 }
648 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000649 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700650 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700651 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900652 }
Ying Xue1d835872011-07-06 05:53:15 -0400653 if (timeout_val <= 0L) {
654 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700655 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700657 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400658 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
659 !tport->congested, timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700660 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900661 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700662
663exit:
664 if (iocb)
665 release_sock(sk);
666 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667}
668
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900669/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700671 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 * @sock: socket structure
673 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700674 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900675 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900677 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678 * Returns the number of bytes sent on success, or errno otherwise
679 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680static int send_packet(struct kiocb *iocb, struct socket *sock,
681 struct msghdr *m, size_t total_len)
682{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700683 struct sock *sk = sock->sk;
684 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900685 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Ying Xue1d835872011-07-06 05:53:15 -0400686 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100687 int res;
688
689 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690 if (unlikely(dest))
691 return send_msg(iocb, sock, m, total_len);
692
Ying Xue97f8b872013-01-31 21:51:47 +0100693 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400694 return -EMSGSIZE;
695
Allan Stephens0c3141e2008-04-15 00:22:02 -0700696 if (iocb)
697 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698
Ying Xue1d835872011-07-06 05:53:15 -0400699 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
700
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900701 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700702 if (unlikely(sock->state != SS_CONNECTED)) {
703 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900704 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700705 else
706 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700707 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700708 }
709
Allan Stephens26896902011-04-21 10:42:07 -0500710 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov,
711 total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000712 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700713 break;
Ying Xue1d835872011-07-06 05:53:15 -0400714 if (timeout_val <= 0L) {
715 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700716 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100717 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700718 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400719 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
720 (!tport->congested || !tport->connected), timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700721 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900722 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700723
724 if (iocb)
725 release_sock(sk);
726 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100727}
728
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900729/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730 * send_stream - send stream-oriented data
731 * @iocb: (unused)
732 * @sock: socket structure
733 * @m: data to send
734 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900735 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900737 *
738 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700739 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100740 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741static int send_stream(struct kiocb *iocb, struct socket *sock,
742 struct msghdr *m, size_t total_len)
743{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700744 struct sock *sk = sock->sk;
745 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100746 struct msghdr my_msg;
747 struct iovec my_iov;
748 struct iovec *curr_iov;
749 int curr_iovlen;
750 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700751 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752 int curr_left;
753 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700754 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900756
Allan Stephens0c3141e2008-04-15 00:22:02 -0700757 lock_sock(sk);
758
Allan Stephens05646c92007-06-10 17:25:24 -0700759 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900760 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700761 if (sock->state == SS_UNCONNECTED) {
762 res = send_packet(NULL, sock, m, total_len);
763 goto exit;
764 } else if (sock->state == SS_DISCONNECTING) {
765 res = -EPIPE;
766 goto exit;
767 } else {
768 res = -ENOTCONN;
769 goto exit;
770 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900771 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100772
Allan Stephens0c3141e2008-04-15 00:22:02 -0700773 if (unlikely(m->msg_name)) {
774 res = -EISCONN;
775 goto exit;
776 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700777
Ying Xue97f8b872013-01-31 21:51:47 +0100778 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400779 res = -EMSGSIZE;
780 goto exit;
781 }
782
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900783 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100784 * Send each iovec entry using one or more messages
785 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900786 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100787 * (i.e. one large iovec entry), but could be improved to pass sets
788 * of small iovec entries into send_packet().
789 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700790 curr_iov = m->msg_iov;
791 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792 my_msg.msg_iov = &my_iov;
793 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700794 my_msg.msg_flags = m->msg_flags;
795 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700796 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100797
Allan Stephens05646c92007-06-10 17:25:24 -0700798 hdr_size = msg_hdr_sz(&tport->phdr);
799
Per Lidenb97bf3f2006-01-02 19:04:38 +0100800 while (curr_iovlen--) {
801 curr_start = curr_iov->iov_base;
802 curr_left = curr_iov->iov_len;
803
804 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700805 bytes_to_send = tport->max_pkt - hdr_size;
806 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
807 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
808 if (curr_left < bytes_to_send)
809 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810 my_iov.iov_base = curr_start;
811 my_iov.iov_len = bytes_to_send;
Allan Stephens26896902011-04-21 10:42:07 -0500812 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000813 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700814 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700815 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700816 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700817 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100818 curr_left -= bytes_to_send;
819 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700820 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100821 }
822
823 curr_iov++;
824 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700825 res = bytes_sent;
826exit:
827 release_sock(sk);
828 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829}
830
831/**
832 * auto_connect - complete connection setup to a remote port
833 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900835 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836 * Returns 0 on success, errno otherwise
837 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700838static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100839{
Allan Stephens2da59912008-07-14 22:43:32 -0700840 struct tipc_sock *tsock = tipc_sk(sock->sk);
Ying Xue584d24b2012-11-29 18:51:19 -0500841 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100842
Allan Stephens2da59912008-07-14 22:43:32 -0700843 tsock->peer_name.ref = msg_origport(msg);
844 tsock->peer_name.node = msg_orignode(msg);
Ying Xue584d24b2012-11-29 18:51:19 -0500845 p_ptr = tipc_port_deref(tsock->p->ref);
846 if (!p_ptr)
847 return -EINVAL;
848
849 __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
850
851 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
852 return -EINVAL;
853 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854 sock->state = SS_CONNECTED;
855 return 0;
856}
857
858/**
859 * set_orig_addr - capture sender's address for received message
860 * @m: descriptor for message info
861 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900862 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863 * Note: Address is not captured if not requested by receiver.
864 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800865static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900867 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100868
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900869 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870 addr->family = AF_TIPC;
871 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000872 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873 addr->addr.id.ref = msg_origport(msg);
874 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000875 addr->addr.name.domain = 0; /* could leave uninitialized */
876 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100877 m->msg_namelen = sizeof(struct sockaddr_tipc);
878 }
879}
880
881/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900882 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100883 * @m: descriptor for message info
884 * @msg: received message header
885 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900886 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100887 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900888 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889 * Returns 0 if successful, otherwise errno
890 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800891static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400892 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893{
894 u32 anc_data[3];
895 u32 err;
896 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700897 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898 int res;
899
900 if (likely(m->msg_controllen == 0))
901 return 0;
902
903 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904 err = msg ? msg_errcode(msg) : 0;
905 if (unlikely(err)) {
906 anc_data[0] = err;
907 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000908 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
909 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000911 if (anc_data[1]) {
912 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
913 msg_data(msg));
914 if (res)
915 return res;
916 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100917 }
918
919 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100920 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
921 switch (dest_type) {
922 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700923 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924 anc_data[0] = msg_nametype(msg);
925 anc_data[1] = msg_namelower(msg);
926 anc_data[2] = msg_namelower(msg);
927 break;
928 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700929 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100930 anc_data[0] = msg_nametype(msg);
931 anc_data[1] = msg_namelower(msg);
932 anc_data[2] = msg_nameupper(msg);
933 break;
934 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700935 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936 anc_data[0] = tport->conn_type;
937 anc_data[1] = tport->conn_instance;
938 anc_data[2] = tport->conn_instance;
939 break;
940 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700941 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942 }
Allan Stephens2db99832010-12-31 18:59:33 +0000943 if (has_name) {
944 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
945 if (res)
946 return res;
947 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100948
949 return 0;
950}
951
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900952/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100953 * recv_msg - receive packet-oriented message
954 * @iocb: (unused)
955 * @m: descriptor for message info
956 * @buf_len: total size of user buffer area
957 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900958 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
960 * If the complete message doesn't fit in user area, truncate it.
961 *
962 * Returns size of returned message data, errno otherwise
963 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964static int recv_msg(struct kiocb *iocb, struct socket *sock,
965 struct msghdr *m, size_t buf_len, int flags)
966{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700967 struct sock *sk = sock->sk;
968 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969 struct sk_buff *buf;
970 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500971 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100972 unsigned int sz;
973 u32 err;
974 int res;
975
Allan Stephens0c3141e2008-04-15 00:22:02 -0700976 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100977 if (unlikely(!buf_len))
978 return -EINVAL;
979
Allan Stephens0c3141e2008-04-15 00:22:02 -0700980 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100981
Allan Stephens0c3141e2008-04-15 00:22:02 -0700982 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100983 res = -ENOTCONN;
984 goto exit;
985 }
986
Mathias Krause60085c32013-04-07 01:52:00 +0000987 /* will be updated in set_orig_addr() if needed */
988 m->msg_namelen = 0;
989
Allan Stephens71092ea2011-02-23 14:52:14 -0500990 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700991restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100992
Allan Stephens0c3141e2008-04-15 00:22:02 -0700993 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700994 while (skb_queue_empty(&sk->sk_receive_queue)) {
995 if (sock->state == SS_DISCONNECTING) {
996 res = -ENOTCONN;
997 goto exit;
998 }
Allan Stephens71092ea2011-02-23 14:52:14 -0500999 if (timeout <= 0L) {
1000 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001001 goto exit;
1002 }
1003 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001004 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1005 tipc_rx_ready(sock),
1006 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001007 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001008 }
1009
1010 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001011 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001012 msg = buf_msg(buf);
1013 sz = msg_data_sz(msg);
1014 err = msg_errcode(msg);
1015
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001017 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001018 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001019 goto restart;
1020 }
1021
1022 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023 set_orig_addr(m, msg);
1024
1025 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001026 res = anc_data_recv(m, msg, tport);
1027 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 goto exit;
1029
1030 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031 if (!err) {
1032 if (unlikely(buf_len < sz)) {
1033 sz = buf_len;
1034 m->msg_flags |= MSG_TRUNC;
1035 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001036 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1037 m->msg_iov, sz);
1038 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001039 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 res = sz;
1041 } else {
1042 if ((sock->state == SS_READY) ||
1043 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1044 res = 0;
1045 else
1046 res = -ECONNRESET;
1047 }
1048
1049 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001051 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -07001052 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1053 tipc_acknowledge(tport->ref, tport->conn_unacked);
1054 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001055 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001056exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001057 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001058 return res;
1059}
1060
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001061/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 * recv_stream - receive stream-oriented data
1063 * @iocb: (unused)
1064 * @m: descriptor for message info
1065 * @buf_len: total size of user buffer area
1066 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001067 *
1068 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 * will optionally wait for more; never truncates data.
1070 *
1071 * Returns size of returned message data, errno otherwise
1072 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073static int recv_stream(struct kiocb *iocb, struct socket *sock,
1074 struct msghdr *m, size_t buf_len, int flags)
1075{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001076 struct sock *sk = sock->sk;
1077 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 struct sk_buff *buf;
1079 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -05001080 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001081 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001082 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001085 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001086
1087 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001088 if (unlikely(!buf_len))
1089 return -EINVAL;
1090
Allan Stephens0c3141e2008-04-15 00:22:02 -07001091 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092
Erik Hugne5d21cb72013-06-17 10:54:38 -04001093 if (unlikely((sock->state == SS_UNCONNECTED))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001094 res = -ENOTCONN;
1095 goto exit;
1096 }
1097
Mathias Krause60085c32013-04-07 01:52:00 +00001098 /* will be updated in set_orig_addr() if needed */
1099 m->msg_namelen = 0;
1100
Florian Westphal3720d402010-08-17 11:00:04 +00001101 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Allan Stephens71092ea2011-02-23 14:52:14 -05001102 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001103
Allan Stephens0c3141e2008-04-15 00:22:02 -07001104restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001105 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001106 while (skb_queue_empty(&sk->sk_receive_queue)) {
1107 if (sock->state == SS_DISCONNECTING) {
1108 res = -ENOTCONN;
1109 goto exit;
1110 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001111 if (timeout <= 0L) {
1112 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001113 goto exit;
1114 }
1115 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001116 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1117 tipc_rx_ready(sock),
1118 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001119 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001120 }
1121
1122 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001123 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124 msg = buf_msg(buf);
1125 sz = msg_data_sz(msg);
1126 err = msg_errcode(msg);
1127
1128 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001130 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001131 goto restart;
1132 }
1133
1134 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135 if (sz_copied == 0) {
1136 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001137 res = anc_data_recv(m, msg, tport);
1138 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001139 goto exit;
1140 }
1141
1142 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001144 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145
Allan Stephens0232fd02011-02-21 09:45:40 -05001146 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147 needed = (buf_len - sz_copied);
1148 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001149
1150 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1151 m->msg_iov, sz_to_copy);
1152 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001154
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155 sz_copied += sz_to_copy;
1156
1157 if (sz_to_copy < sz) {
1158 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001159 TIPC_SKB_CB(buf)->handle =
1160 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 goto exit;
1162 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 } else {
1164 if (sz_copied != 0)
1165 goto exit; /* can't add error msg to valid data */
1166
1167 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1168 res = 0;
1169 else
1170 res = -ECONNRESET;
1171 }
1172
1173 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001175 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1176 tipc_acknowledge(tport->ref, tport->conn_unacked);
1177 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001178 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179
1180 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001181 if ((sz_copied < buf_len) && /* didn't get all requested data */
1182 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001183 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001184 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1185 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 goto restart;
1187
1188exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001189 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001190 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191}
1192
1193/**
Ying Xuef288bef2012-08-21 11:16:57 +08001194 * tipc_write_space - wake up thread if port congestion is released
1195 * @sk: socket
1196 */
1197static void tipc_write_space(struct sock *sk)
1198{
1199 struct socket_wq *wq;
1200
1201 rcu_read_lock();
1202 wq = rcu_dereference(sk->sk_wq);
1203 if (wq_has_sleeper(wq))
1204 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1205 POLLWRNORM | POLLWRBAND);
1206 rcu_read_unlock();
1207}
1208
1209/**
1210 * tipc_data_ready - wake up threads to indicate messages have been received
1211 * @sk: socket
1212 * @len: the length of messages
1213 */
1214static void tipc_data_ready(struct sock *sk, int len)
1215{
1216 struct socket_wq *wq;
1217
1218 rcu_read_lock();
1219 wq = rcu_dereference(sk->sk_wq);
1220 if (wq_has_sleeper(wq))
1221 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1222 POLLRDNORM | POLLRDBAND);
1223 rcu_read_unlock();
1224}
1225
1226/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001227 * filter_connect - Handle all incoming messages for a connection-based socket
1228 * @tsock: TIPC socket
1229 * @msg: message
1230 *
1231 * Returns TIPC error status code and socket error status code
1232 * once it encounters some errors
1233 */
1234static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
1235{
1236 struct socket *sock = tsock->sk.sk_socket;
1237 struct tipc_msg *msg = buf_msg(*buf);
Ying Xue584d24b2012-11-29 18:51:19 -05001238 struct sock *sk = &tsock->sk;
Ying Xue7e6c1312012-11-29 18:39:14 -05001239 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001240 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001241
1242 if (msg_mcast(msg))
1243 return retval;
1244
1245 switch ((int)sock->state) {
1246 case SS_CONNECTED:
1247 /* Accept only connection-based messages sent by peer */
1248 if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
1249 if (unlikely(msg_errcode(msg))) {
1250 sock->state = SS_DISCONNECTING;
1251 __tipc_disconnect(tsock->p);
1252 }
1253 retval = TIPC_OK;
1254 }
1255 break;
1256 case SS_CONNECTING:
1257 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001258 if (unlikely(msg_errcode(msg))) {
1259 sock->state = SS_DISCONNECTING;
1260 sk->sk_err = -ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001261 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001262 break;
1263 }
1264
1265 if (unlikely(!msg_connected(msg)))
1266 break;
1267
1268 res = auto_connect(sock, msg);
1269 if (res) {
1270 sock->state = SS_DISCONNECTING;
1271 sk->sk_err = res;
1272 retval = TIPC_OK;
1273 break;
1274 }
1275
1276 /* If an incoming message is an 'ACK-', it should be
1277 * discarded here because it doesn't contain useful
1278 * data. In addition, we should try to wake up
1279 * connect() routine if sleeping.
1280 */
1281 if (msg_data_sz(msg) == 0) {
1282 kfree_skb(*buf);
1283 *buf = NULL;
1284 if (waitqueue_active(sk_sleep(sk)))
1285 wake_up_interruptible(sk_sleep(sk));
1286 }
1287 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001288 break;
1289 case SS_LISTENING:
1290 case SS_UNCONNECTED:
1291 /* Accept only SYN message */
1292 if (!msg_connected(msg) && !(msg_errcode(msg)))
1293 retval = TIPC_OK;
1294 break;
1295 case SS_DISCONNECTING:
1296 break;
1297 default:
1298 pr_err("Unknown socket state %u\n", sock->state);
1299 }
1300 return retval;
1301}
1302
1303/**
Ying Xueaba79f32013-01-20 23:30:09 +01001304 * rcvbuf_limit - get proper overload limit of socket receive queue
1305 * @sk: socket
1306 * @buf: message
1307 *
1308 * For all connection oriented messages, irrespective of importance,
1309 * the default overload value (i.e. 67MB) is set as limit.
1310 *
1311 * For all connectionless messages, by default new queue limits are
1312 * as belows:
1313 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001314 * TIPC_LOW_IMPORTANCE (4 MB)
1315 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1316 * TIPC_HIGH_IMPORTANCE (16 MB)
1317 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001318 *
1319 * Returns overload limit according to corresponding message importance
1320 */
1321static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1322{
1323 struct tipc_msg *msg = buf_msg(buf);
1324 unsigned int limit;
1325
1326 if (msg_connected(msg))
Ying Xuecc79dd12013-06-17 10:54:37 -04001327 limit = sysctl_tipc_rmem[2];
Ying Xueaba79f32013-01-20 23:30:09 +01001328 else
Ying Xuecc79dd12013-06-17 10:54:37 -04001329 limit = sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1330 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001331 return limit;
1332}
1333
1334/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001335 * filter_rcv - validate incoming message
1336 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001338 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001339 * Enqueues message on receive queue if acceptable; optionally handles
1340 * disconnect indication for a connected socket.
1341 *
1342 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001343 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1345 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001346static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001348 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001350 unsigned int limit = rcvbuf_limit(sk, buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001351 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001354 if (msg_type(msg) > TIPC_DIRECT_MSG)
1355 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001356
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001358 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001360 } else {
Ying Xue7e6c1312012-11-29 18:39:14 -05001361 res = filter_connect(tipc_sk(sk), &buf);
1362 if (res != TIPC_OK || buf == NULL)
1363 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001364 }
1365
1366 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001367 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1368 return TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001369
Ying Xueaba79f32013-01-20 23:30:09 +01001370 /* Enqueue message */
Allan Stephens0232fd02011-02-21 09:45:40 -05001371 TIPC_SKB_CB(buf)->handle = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001372 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001373 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001374
Ying Xuef288bef2012-08-21 11:16:57 +08001375 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 return TIPC_OK;
1377}
1378
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001379/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001380 * backlog_rcv - handle incoming message from backlog queue
1381 * @sk: socket
1382 * @buf: message
1383 *
1384 * Caller must hold socket lock, but not port lock.
1385 *
1386 * Returns 0
1387 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001388static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1389{
1390 u32 res;
1391
1392 res = filter_rcv(sk, buf);
1393 if (res)
1394 tipc_reject_msg(buf, res);
1395 return 0;
1396}
1397
1398/**
1399 * dispatch - handle incoming message
1400 * @tport: TIPC port that received message
1401 * @buf: message
1402 *
1403 * Called with port lock already taken.
1404 *
1405 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1406 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001407static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1408{
Ying Xuec0fee8a2013-06-17 10:54:46 -04001409 struct sock *sk = tport->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001410 u32 res;
1411
1412 /*
1413 * Process message if socket is unlocked; otherwise add to backlog queue
1414 *
1415 * This code is based on sk_receive_skb(), but must be distinct from it
1416 * since a TIPC-specific filter/reject mechanism is utilized
1417 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001418 bh_lock_sock(sk);
1419 if (!sock_owned_by_user(sk)) {
1420 res = filter_rcv(sk, buf);
1421 } else {
Ying Xueaba79f32013-01-20 23:30:09 +01001422 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
Zhu Yi53eecb12010-03-04 18:01:45 +00001423 res = TIPC_ERR_OVERLOAD;
1424 else
1425 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001426 }
1427 bh_unlock_sock(sk);
1428
1429 return res;
1430}
1431
1432/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 * wakeupdispatch - wake up port after congestion
1434 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001435 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001436 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001437 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001438static void wakeupdispatch(struct tipc_port *tport)
1439{
Ying Xuec0fee8a2013-06-17 10:54:46 -04001440 struct sock *sk = tport->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001441
Ying Xuef288bef2012-08-21 11:16:57 +08001442 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001443}
1444
1445/**
1446 * connect - establish a connection to another TIPC port
1447 * @sock: socket structure
1448 * @dest: socket address for destination port
1449 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001450 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001451 *
1452 * Returns 0 on success, errno otherwise
1453 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001454static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001455 int flags)
1456{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001457 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001458 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1459 struct msghdr m = {NULL,};
Allan Stephensa0f40f02011-05-26 13:44:34 -04001460 unsigned int timeout;
Allan Stephensb89741a2008-04-15 00:20:37 -07001461 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001462
Allan Stephens0c3141e2008-04-15 00:22:02 -07001463 lock_sock(sk);
1464
Allan Stephensb89741a2008-04-15 00:20:37 -07001465 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001466 if (sock->state == SS_READY) {
1467 res = -EOPNOTSUPP;
1468 goto exit;
1469 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470
Allan Stephensb89741a2008-04-15 00:20:37 -07001471 /*
1472 * Reject connection attempt using multicast address
1473 *
1474 * Note: send_msg() validates the rest of the address fields,
1475 * so there's no need to do it here
1476 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001477 if (dst->addrtype == TIPC_ADDR_MCAST) {
1478 res = -EINVAL;
1479 goto exit;
1480 }
1481
Ying Xue584d24b2012-11-29 18:51:19 -05001482 timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001483
Ying Xue584d24b2012-11-29 18:51:19 -05001484 switch (sock->state) {
1485 case SS_UNCONNECTED:
1486 /* Send a 'SYN-' to destination */
1487 m.msg_name = dest;
1488 m.msg_namelen = destlen;
1489
1490 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1491 * indicate send_msg() is never blocked.
1492 */
1493 if (!timeout)
1494 m.msg_flags = MSG_DONTWAIT;
1495
1496 res = send_msg(NULL, sock, &m, 0);
1497 if ((res < 0) && (res != -EWOULDBLOCK))
1498 goto exit;
1499
1500 /* Just entered SS_CONNECTING state; the only
1501 * difference is that return value in non-blocking
1502 * case is EINPROGRESS, rather than EALREADY.
1503 */
1504 res = -EINPROGRESS;
1505 break;
1506 case SS_CONNECTING:
1507 res = -EALREADY;
1508 break;
1509 case SS_CONNECTED:
1510 res = -EISCONN;
1511 break;
1512 default:
1513 res = -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001514 goto exit;
Allan Stephensb89741a2008-04-15 00:20:37 -07001515 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001516
Ying Xue584d24b2012-11-29 18:51:19 -05001517 if (sock->state == SS_CONNECTING) {
1518 if (!timeout)
1519 goto exit;
1520
1521 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1522 release_sock(sk);
1523 res = wait_event_interruptible_timeout(*sk_sleep(sk),
1524 sock->state != SS_CONNECTING,
1525 timeout ? (long)msecs_to_jiffies(timeout)
1526 : MAX_SCHEDULE_TIMEOUT);
1527 lock_sock(sk);
1528 if (res <= 0) {
1529 if (res == 0)
1530 res = -ETIMEDOUT;
1531 else
1532 ; /* leave "res" unchanged */
1533 goto exit;
1534 }
1535 }
1536
1537 if (unlikely(sock->state == SS_DISCONNECTING))
1538 res = sock_error(sk);
1539 else
1540 res = 0;
1541
Allan Stephens0c3141e2008-04-15 00:22:02 -07001542exit:
1543 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001544 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001545}
1546
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001547/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001548 * listen - allow socket to listen for incoming connections
1549 * @sock: socket structure
1550 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001551 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001552 * Returns 0 on success, errno otherwise
1553 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001554static int listen(struct socket *sock, int len)
1555{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001556 struct sock *sk = sock->sk;
1557 int res;
1558
1559 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001560
Ying Xue245f3d32011-07-06 06:01:13 -04001561 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001562 res = -EINVAL;
1563 else {
1564 sock->state = SS_LISTENING;
1565 res = 0;
1566 }
1567
1568 release_sock(sk);
1569 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001570}
1571
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001572/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001573 * accept - wait for connection request
1574 * @sock: listening socket
1575 * @newsock: new socket that is to be connected
1576 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001577 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001578 * Returns 0 on success, errno otherwise
1579 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001580static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001581{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001582 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001583 struct sk_buff *buf;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001584 struct tipc_sock *new_tsock;
1585 struct tipc_port *new_tport;
1586 struct tipc_msg *msg;
1587 u32 new_ref;
1588
Allan Stephens0c3141e2008-04-15 00:22:02 -07001589 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001590
Allan Stephens0c3141e2008-04-15 00:22:02 -07001591 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001592
Allan Stephens0c3141e2008-04-15 00:22:02 -07001593 if (sock->state != SS_LISTENING) {
1594 res = -EINVAL;
1595 goto exit;
1596 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001597
Allan Stephens0c3141e2008-04-15 00:22:02 -07001598 while (skb_queue_empty(&sk->sk_receive_queue)) {
1599 if (flags & O_NONBLOCK) {
1600 res = -EWOULDBLOCK;
1601 goto exit;
1602 }
1603 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001604 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001605 (!skb_queue_empty(&sk->sk_receive_queue)));
1606 lock_sock(sk);
1607 if (res)
1608 goto exit;
1609 }
1610
1611 buf = skb_peek(&sk->sk_receive_queue);
1612
Ying Xuec5fa7b32013-06-17 10:54:39 -04001613 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001614 if (res)
1615 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001616
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001617 new_sk = new_sock->sk;
1618 new_tsock = tipc_sk(new_sk);
1619 new_tport = new_tsock->p;
1620 new_ref = new_tport->ref;
1621 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001622
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001623 /* we lock on new_sk; but lockdep sees the lock on sk */
1624 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001625
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001626 /*
1627 * Reject any stray messages received by new socket
1628 * before the socket lock was taken (very, very unlikely)
1629 */
1630 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001631
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001632 /* Connect new socket to it's peer */
1633 new_tsock->peer_name.ref = msg_origport(msg);
1634 new_tsock->peer_name.node = msg_orignode(msg);
1635 tipc_connect(new_ref, &new_tsock->peer_name);
1636 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001637
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001638 tipc_set_portimportance(new_ref, msg_importance(msg));
1639 if (msg_named(msg)) {
1640 new_tport->conn_type = msg_nametype(msg);
1641 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001642 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001643
1644 /*
1645 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1646 * Respond to 'SYN+' by queuing it on new socket.
1647 */
1648 if (!msg_data_sz(msg)) {
1649 struct msghdr m = {NULL,};
1650
1651 advance_rx_queue(sk);
1652 send_packet(NULL, new_sock, &m, 0);
1653 } else {
1654 __skb_dequeue(&sk->sk_receive_queue);
1655 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001656 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001657 }
1658 release_sock(new_sk);
1659
Per Lidenb97bf3f2006-01-02 19:04:38 +01001660exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001661 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001662 return res;
1663}
1664
1665/**
1666 * shutdown - shutdown socket connection
1667 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001668 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669 *
1670 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001671 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001672 * Returns 0 on success, errno otherwise
1673 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674static int shutdown(struct socket *sock, int how)
1675{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001676 struct sock *sk = sock->sk;
1677 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678 struct sk_buff *buf;
1679 int res;
1680
Allan Stephense247a8f2008-03-06 15:05:38 -08001681 if (how != SHUT_RDWR)
1682 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683
Allan Stephens0c3141e2008-04-15 00:22:02 -07001684 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001685
1686 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001687 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001688 case SS_CONNECTED:
1689
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001691 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001692 buf = __skb_dequeue(&sk->sk_receive_queue);
1693 if (buf) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001694 if (TIPC_SKB_CB(buf)->handle != 0) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001695 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696 goto restart;
1697 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001698 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001700 } else {
1701 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001703
1704 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001705
1706 /* fall through */
1707
1708 case SS_DISCONNECTING:
1709
Ying Xue75031152012-10-29 09:38:15 -04001710 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001711 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001712
1713 /* Wake up anyone sleeping in poll */
1714 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001715 res = 0;
1716 break;
1717
1718 default:
1719 res = -ENOTCONN;
1720 }
1721
Allan Stephens0c3141e2008-04-15 00:22:02 -07001722 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001723 return res;
1724}
1725
1726/**
1727 * setsockopt - set socket option
1728 * @sock: socket structure
1729 * @lvl: option level
1730 * @opt: option identifier
1731 * @ov: pointer to new option value
1732 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001733 *
1734 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001735 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001736 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001737 * Returns 0 on success, errno otherwise
1738 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001739static int setsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
1740 unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001742 struct sock *sk = sock->sk;
1743 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001744 u32 value;
1745 int res;
1746
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001747 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1748 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749 if (lvl != SOL_TIPC)
1750 return -ENOPROTOOPT;
1751 if (ol < sizeof(value))
1752 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001753 res = get_user(value, (u32 __user *)ov);
1754 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001755 return res;
1756
Allan Stephens0c3141e2008-04-15 00:22:02 -07001757 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001758
Per Lidenb97bf3f2006-01-02 19:04:38 +01001759 switch (opt) {
1760 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001761 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001762 break;
1763 case TIPC_SRC_DROPPABLE:
1764 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001765 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001766 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001767 res = -ENOPROTOOPT;
1768 break;
1769 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001770 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001771 break;
1772 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001773 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001774 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001775 break;
1776 default:
1777 res = -EINVAL;
1778 }
1779
Allan Stephens0c3141e2008-04-15 00:22:02 -07001780 release_sock(sk);
1781
Per Lidenb97bf3f2006-01-02 19:04:38 +01001782 return res;
1783}
1784
1785/**
1786 * getsockopt - get socket option
1787 * @sock: socket structure
1788 * @lvl: option level
1789 * @opt: option identifier
1790 * @ov: receptacle for option value
1791 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001792 *
1793 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001794 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001795 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001796 * Returns 0 on success, errno otherwise
1797 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001798static int getsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
1799 int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001801 struct sock *sk = sock->sk;
1802 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001803 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001804 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001805 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001806
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001807 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1808 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001809 if (lvl != SOL_TIPC)
1810 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001811 res = get_user(len, ol);
1812 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001813 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814
Allan Stephens0c3141e2008-04-15 00:22:02 -07001815 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816
1817 switch (opt) {
1818 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001819 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001820 break;
1821 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001822 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001823 break;
1824 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001825 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826 break;
1827 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001828 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001829 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001830 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001831 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001832 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001833 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001834 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001835 value = skb_queue_len(&sk->sk_receive_queue);
1836 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001837 default:
1838 res = -EINVAL;
1839 }
1840
Allan Stephens0c3141e2008-04-15 00:22:02 -07001841 release_sock(sk);
1842
Paul Gortmaker25860c32010-12-31 18:59:31 +00001843 if (res)
1844 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001845
Paul Gortmaker25860c32010-12-31 18:59:31 +00001846 if (len < sizeof(value))
1847 return -EINVAL;
1848
1849 if (copy_to_user(ov, &value, sizeof(value)))
1850 return -EFAULT;
1851
1852 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001853}
1854
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001855/* Protocol switches for the various types of TIPC sockets */
1856
Florian Westphalbca65ea2008-02-07 18:18:01 -08001857static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001858 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001859 .family = AF_TIPC,
1860 .release = release,
1861 .bind = bind,
1862 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001863 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001864 .accept = sock_no_accept,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001865 .getname = get_name,
1866 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001867 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001868 .listen = sock_no_listen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001869 .shutdown = shutdown,
1870 .setsockopt = setsockopt,
1871 .getsockopt = getsockopt,
1872 .sendmsg = send_msg,
1873 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001874 .mmap = sock_no_mmap,
1875 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001876};
1877
Florian Westphalbca65ea2008-02-07 18:18:01 -08001878static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001879 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001880 .family = AF_TIPC,
1881 .release = release,
1882 .bind = bind,
1883 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001884 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001885 .accept = accept,
1886 .getname = get_name,
1887 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001888 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001889 .listen = listen,
1890 .shutdown = shutdown,
1891 .setsockopt = setsockopt,
1892 .getsockopt = getsockopt,
1893 .sendmsg = send_packet,
1894 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001895 .mmap = sock_no_mmap,
1896 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001897};
1898
Florian Westphalbca65ea2008-02-07 18:18:01 -08001899static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001900 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001901 .family = AF_TIPC,
1902 .release = release,
1903 .bind = bind,
1904 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001905 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001906 .accept = accept,
1907 .getname = get_name,
1908 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001909 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001910 .listen = listen,
1911 .shutdown = shutdown,
1912 .setsockopt = setsockopt,
1913 .getsockopt = getsockopt,
1914 .sendmsg = send_stream,
1915 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001916 .mmap = sock_no_mmap,
1917 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001918};
1919
Florian Westphalbca65ea2008-02-07 18:18:01 -08001920static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001921 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001922 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04001923 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01001924};
1925
1926static struct proto tipc_proto = {
1927 .name = "TIPC",
1928 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04001929 .obj_size = sizeof(struct tipc_sock),
1930 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01001931};
1932
Ying Xuec5fa7b32013-06-17 10:54:39 -04001933static struct proto tipc_proto_kern = {
1934 .name = "TIPC",
1935 .obj_size = sizeof(struct tipc_sock),
1936 .sysctl_rmem = sysctl_tipc_rmem
1937};
1938
Per Lidenb97bf3f2006-01-02 19:04:38 +01001939/**
Per Liden4323add2006-01-18 00:38:21 +01001940 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001941 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001942 * Returns 0 on success, errno otherwise
1943 */
Per Liden4323add2006-01-18 00:38:21 +01001944int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001945{
1946 int res;
1947
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001948 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001949 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001950 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001951 goto out;
1952 }
1953
1954 res = sock_register(&tipc_family_ops);
1955 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001956 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001957 proto_unregister(&tipc_proto);
1958 goto out;
1959 }
1960
1961 sockets_enabled = 1;
1962 out:
1963 return res;
1964}
1965
1966/**
Per Liden4323add2006-01-18 00:38:21 +01001967 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001968 */
Per Liden4323add2006-01-18 00:38:21 +01001969void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001970{
1971 if (!sockets_enabled)
1972 return;
1973
1974 sockets_enabled = 0;
1975 sock_unregister(tipc_family_ops.family);
1976 proto_unregister(&tipc_proto);
1977}