blob: b2ae25ae30388cea4085b7e8e95da4d1212d64bc [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;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400242
243 rc = sock_create_lite(AF_TIPC, type, 0, res);
244 if (rc < 0) {
245 pr_err("Failed to create kernel socket\n");
246 return rc;
247 }
248 tipc_sk_create(&init_net, *res, 0, 1);
249
Ying Xuec5fa7b32013-06-17 10:54:39 -0400250 return 0;
251}
252
253/**
254 * tipc_sock_release_local - release socket created by tipc_sock_create_local
255 * @sock: the socket to be released.
256 *
257 * Module reference count is not incremented when such sockets are created,
258 * so we must keep it from being decremented when they are released.
259 */
260void tipc_sock_release_local(struct socket *sock)
261{
262 release(sock);
263 sock->ops = NULL;
264 sock_release(sock);
265}
266
267/**
268 * tipc_sock_accept_local - accept a connection on a socket created
269 * with tipc_sock_create_local. Use this function to avoid that
270 * module reference count is inadvertently incremented.
271 *
272 * @sock: the accepting socket
273 * @newsock: reference to the new socket to be created
274 * @flags: socket flags
275 */
276
277int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400278 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400279{
280 struct sock *sk = sock->sk;
281 int ret;
282
283 ret = sock_create_lite(sk->sk_family, sk->sk_type,
284 sk->sk_protocol, newsock);
285 if (ret < 0)
286 return ret;
287
288 ret = accept(sock, *newsock, flags);
289 if (ret < 0) {
290 sock_release(*newsock);
291 return ret;
292 }
293 (*newsock)->ops = sock->ops;
294 return ret;
295}
296
297/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298 * release - destroy a TIPC socket
299 * @sock: socket to destroy
300 *
301 * This routine cleans up any messages that are still queued on the socket.
302 * For DGRAM and RDM socket types, all queued messages are rejected.
303 * For SEQPACKET and STREAM socket types, the first message is rejected
304 * and any others are discarded. (If the first message on a STREAM socket
305 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900306 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 * NOTE: Rejected messages are not necessarily returned to the sender! They
308 * are returned or discarded according to the "destination droppable" setting
309 * specified for the message by the sender.
310 *
311 * Returns 0 on success, errno otherwise
312 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313static int release(struct socket *sock)
314{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700316 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700318 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100319
Allan Stephens0c3141e2008-04-15 00:22:02 -0700320 /*
321 * Exit if socket isn't fully initialized (occurs when a failed accept()
322 * releases a pre-allocated child socket that was never used)
323 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700324 if (sk == NULL)
325 return 0;
326
327 tport = tipc_sk_port(sk);
328 lock_sock(sk);
329
330 /*
331 * Reject all unreceived messages, except on an active connection
332 * (which disconnects locally & sends a 'FIN+' to peer)
333 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700335 buf = __skb_dequeue(&sk->sk_receive_queue);
336 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 break;
Ying Xue40682432013-10-18 07:23:16 +0200338 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400339 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700340 else {
341 if ((sock->state == SS_CONNECTING) ||
342 (sock->state == SS_CONNECTED)) {
343 sock->state = SS_DISCONNECTING;
344 tipc_disconnect(tport->ref);
345 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700347 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348 }
349
Allan Stephens0c3141e2008-04-15 00:22:02 -0700350 /*
351 * Delete TIPC port; this ensures no more messages are queued
352 * (also disconnects an active connection & sends a 'FIN-' to peer)
353 */
Ying Xue84602762013-12-27 10:18:28 +0800354 res = tipc_deleteport(tport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355
Allan Stephens0c3141e2008-04-15 00:22:02 -0700356 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100357 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358
Allan Stephens0c3141e2008-04-15 00:22:02 -0700359 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700360 sock->state = SS_DISCONNECTING;
361 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362
363 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700364 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 return res;
367}
368
369/**
370 * bind - associate or disassocate TIPC name(s) with a socket
371 * @sock: socket structure
372 * @uaddr: socket address describing name(s) and desired operation
373 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900374 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375 * Name and name sequence binding is indicated using a positive scope value;
376 * a negative scope value unbinds the specified name. Specifying no name
377 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900378 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700380 *
381 * NOTE: This routine doesn't need to take the socket lock since it doesn't
382 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
385{
Ying Xue84602762013-12-27 10:18:28 +0800386 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Ying Xue84602762013-12-27 10:18:28 +0800388 struct tipc_port *tport = tipc_sk_port(sock->sk);
389 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100390
Ying Xue84602762013-12-27 10:18:28 +0800391 lock_sock(sk);
392 if (unlikely(!uaddr_len)) {
393 res = tipc_withdraw(tport, 0, NULL);
394 goto exit;
395 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900396
Ying Xue84602762013-12-27 10:18:28 +0800397 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
398 res = -EINVAL;
399 goto exit;
400 }
401 if (addr->family != AF_TIPC) {
402 res = -EAFNOSUPPORT;
403 goto exit;
404 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406 if (addr->addrtype == TIPC_ADDR_NAME)
407 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800408 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
409 res = -EAFNOSUPPORT;
410 goto exit;
411 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900412
Ying Xue13a2e892013-06-17 10:54:40 -0400413 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400414 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800415 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
416 res = -EACCES;
417 goto exit;
418 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400419
Ying Xue84602762013-12-27 10:18:28 +0800420 res = (addr->scope > 0) ?
421 tipc_publish(tport, addr->scope, &addr->addr.nameseq) :
422 tipc_withdraw(tport, -addr->scope, &addr->addr.nameseq);
423exit:
424 release_sock(sk);
425 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426}
427
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900428/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429 * get_name - get port ID of socket or peer socket
430 * @sock: socket structure
431 * @uaddr: area for returned socket address
432 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700433 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900434 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700436 *
Allan Stephens2da59912008-07-14 22:43:32 -0700437 * NOTE: This routine doesn't need to take the socket lock since it only
438 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000439 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900441static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 int *uaddr_len, int peer)
443{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700445 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000447 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700448 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700449 if ((sock->state != SS_CONNECTED) &&
450 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
451 return -ENOTCONN;
452 addr->addr.id.ref = tsock->peer_name.ref;
453 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700454 } else {
Allan Stephensb924dcf2010-11-30 12:01:03 +0000455 addr->addr.id.ref = tsock->p->ref;
456 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700457 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458
459 *uaddr_len = sizeof(*addr);
460 addr->addrtype = TIPC_ADDR_ID;
461 addr->family = AF_TIPC;
462 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463 addr->addr.name.domain = 0;
464
Allan Stephens0c3141e2008-04-15 00:22:02 -0700465 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466}
467
468/**
469 * poll - read and possibly block on pollmask
470 * @file: file structure associated with the socket
471 * @sock: socket for which to calculate the poll bits
472 * @wait: ???
473 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700474 * Returns pollmask value
475 *
476 * COMMENTARY:
477 * It appears that the usual socket locking mechanisms are not useful here
478 * since the pollmask info is potentially out-of-date the moment this routine
479 * exits. TCP and other protocols seem to rely on higher level poll routines
480 * to handle any preventable race conditions, so TIPC will do the same ...
481 *
482 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700483 *
Allan Stephensf662c072010-08-17 11:00:06 +0000484 * socket state flags set
485 * ------------ ---------
486 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200487 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000488 *
489 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
490 * no write flags
491 *
492 * connected POLLIN/POLLRDNORM if data in rx queue
493 * POLLOUT if port is not congested
494 *
495 * disconnecting POLLIN/POLLRDNORM/POLLHUP
496 * no write flags
497 *
498 * listening POLLIN if SYN in rx queue
499 * no write flags
500 *
501 * ready POLLIN/POLLRDNORM if data in rx queue
502 * [connectionless] POLLOUT (since port cannot be congested)
503 *
504 * IMPORTANT: The fact that a read or write operation is indicated does NOT
505 * imply that the operation will succeed, merely that it should be performed
506 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900508static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 poll_table *wait)
510{
Allan Stephens9b674e82008-03-26 16:48:21 -0700511 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000512 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700513
Ying Xuef288bef2012-08-21 11:16:57 +0800514 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700515
Allan Stephensf662c072010-08-17 11:00:06 +0000516 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200517 case SS_UNCONNECTED:
518 if (!tipc_sk_port(sk)->congested)
519 mask |= POLLOUT;
520 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000521 case SS_READY:
522 case SS_CONNECTED:
523 if (!tipc_sk_port(sk)->congested)
524 mask |= POLLOUT;
525 /* fall thru' */
526 case SS_CONNECTING:
527 case SS_LISTENING:
528 if (!skb_queue_empty(&sk->sk_receive_queue))
529 mask |= (POLLIN | POLLRDNORM);
530 break;
531 case SS_DISCONNECTING:
532 mask = (POLLIN | POLLRDNORM | POLLHUP);
533 break;
534 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700535
536 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537}
538
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900539/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 * dest_name_check - verify user is permitted to send to specified port name
541 * @dest: destination address
542 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900543 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 * Prevents restricted configuration commands from being issued by
545 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900546 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 * Returns 0 if permission is granted, otherwise errno
548 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800549static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550{
551 struct tipc_cfg_msg_hdr hdr;
552
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900553 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
554 return 0;
555 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
556 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900557 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
558 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559
Allan Stephens3f8dd942011-01-18 13:09:29 -0500560 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
561 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900562 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700564 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100565 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900566
Per Lidenb97bf3f2006-01-02 19:04:38 +0100567 return 0;
568}
569
570/**
571 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700572 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573 * @sock: socket structure
574 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700575 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900576 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900578 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
580 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900581 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 * Returns the number of bytes sent on success, or errno otherwise
583 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584static int send_msg(struct kiocb *iocb, struct socket *sock,
585 struct msghdr *m, size_t total_len)
586{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700587 struct sock *sk = sock->sk;
588 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900589 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590 int needs_conn;
Ying Xue1d835872011-07-06 05:53:15 -0400591 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592 int res = -EINVAL;
593
594 if (unlikely(!dest))
595 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700596 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
597 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 return -EINVAL;
Ying Xue97f8b872013-01-31 21:51:47 +0100599 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400600 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601
Allan Stephens0c3141e2008-04-15 00:22:02 -0700602 if (iocb)
603 lock_sock(sk);
604
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 needs_conn = (sock->state != SS_READY);
606 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700607 if (sock->state == SS_LISTENING) {
608 res = -EPIPE;
609 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700610 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700611 if (sock->state != SS_UNCONNECTED) {
612 res = -EISCONN;
613 goto exit;
614 }
Erik Hugne5d21cb72013-06-17 10:54:38 -0400615 if (tport->published) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700616 res = -EOPNOTSUPP;
617 goto exit;
618 }
619 if (dest->addrtype == TIPC_ADDR_NAME) {
620 tport->conn_type = dest->addr.name.name.type;
621 tport->conn_instance = dest->addr.name.name.instance;
622 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623
624 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700625 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 }
627
Ying Xue1d835872011-07-06 05:53:15 -0400628 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
629
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900630 do {
631 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000632 res = dest_name_check(dest, m);
633 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700634 break;
635 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900636 &dest->addr.name.name,
637 dest->addr.name.domain,
Allan Stephens26896902011-04-21 10:42:07 -0500638 m->msg_iov,
639 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000640 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700641 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900642 &dest->addr.id,
Allan Stephens26896902011-04-21 10:42:07 -0500643 m->msg_iov,
644 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000645 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 if (needs_conn) {
647 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700648 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649 }
Allan Stephens2db99832010-12-31 18:59:33 +0000650 res = dest_name_check(dest, m);
651 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700652 break;
653 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900654 &dest->addr.nameseq,
Allan Stephens26896902011-04-21 10:42:07 -0500655 m->msg_iov,
656 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900657 }
658 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000659 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700660 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700661 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900662 }
Ying Xue1d835872011-07-06 05:53:15 -0400663 if (timeout_val <= 0L) {
664 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700665 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700667 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400668 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
669 !tport->congested, timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700670 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900671 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700672
673exit:
674 if (iocb)
675 release_sock(sk);
676 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100677}
678
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900679/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700681 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100682 * @sock: socket structure
683 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700684 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900685 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900687 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100688 * Returns the number of bytes sent on success, or errno otherwise
689 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690static int send_packet(struct kiocb *iocb, struct socket *sock,
691 struct msghdr *m, size_t total_len)
692{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700693 struct sock *sk = sock->sk;
694 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900695 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Ying Xue1d835872011-07-06 05:53:15 -0400696 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100697 int res;
698
699 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100700 if (unlikely(dest))
701 return send_msg(iocb, sock, m, total_len);
702
Ying Xue97f8b872013-01-31 21:51:47 +0100703 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400704 return -EMSGSIZE;
705
Allan Stephens0c3141e2008-04-15 00:22:02 -0700706 if (iocb)
707 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708
Ying Xue1d835872011-07-06 05:53:15 -0400709 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
710
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900711 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700712 if (unlikely(sock->state != SS_CONNECTED)) {
713 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900714 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700715 else
716 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700717 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700718 }
719
Ying Xue9446b872013-10-18 07:23:15 +0200720 res = tipc_send(tport->ref, m->msg_iov, total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000721 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700722 break;
Ying Xue1d835872011-07-06 05:53:15 -0400723 if (timeout_val <= 0L) {
724 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700725 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700727 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400728 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
729 (!tport->congested || !tport->connected), timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700730 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900731 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700732
733 if (iocb)
734 release_sock(sk);
735 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736}
737
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900738/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100739 * send_stream - send stream-oriented data
740 * @iocb: (unused)
741 * @sock: socket structure
742 * @m: data to send
743 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900744 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900746 *
747 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700748 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750static int send_stream(struct kiocb *iocb, struct socket *sock,
751 struct msghdr *m, size_t total_len)
752{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700753 struct sock *sk = sock->sk;
754 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 struct msghdr my_msg;
756 struct iovec my_iov;
757 struct iovec *curr_iov;
758 int curr_iovlen;
759 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700760 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761 int curr_left;
762 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700763 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900765
Allan Stephens0c3141e2008-04-15 00:22:02 -0700766 lock_sock(sk);
767
Allan Stephens05646c92007-06-10 17:25:24 -0700768 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900769 if (unlikely(sock->state != SS_CONNECTED)) {
wangweidong3b8401f2013-12-12 09:36:40 +0800770 if (sock->state == SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700771 res = send_packet(NULL, sock, m, total_len);
wangweidongb0555972013-12-27 10:09:39 +0800772 else
773 res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800774 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900775 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776
Allan Stephens0c3141e2008-04-15 00:22:02 -0700777 if (unlikely(m->msg_name)) {
778 res = -EISCONN;
779 goto exit;
780 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700781
Ying Xue97f8b872013-01-31 21:51:47 +0100782 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400783 res = -EMSGSIZE;
784 goto exit;
785 }
786
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900787 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788 * Send each iovec entry using one or more messages
789 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900790 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791 * (i.e. one large iovec entry), but could be improved to pass sets
792 * of small iovec entries into send_packet().
793 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700794 curr_iov = m->msg_iov;
795 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 my_msg.msg_iov = &my_iov;
797 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700798 my_msg.msg_flags = m->msg_flags;
799 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700800 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100801
Allan Stephens05646c92007-06-10 17:25:24 -0700802 hdr_size = msg_hdr_sz(&tport->phdr);
803
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804 while (curr_iovlen--) {
805 curr_start = curr_iov->iov_base;
806 curr_left = curr_iov->iov_len;
807
808 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700809 bytes_to_send = tport->max_pkt - hdr_size;
810 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
811 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
812 if (curr_left < bytes_to_send)
813 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100814 my_iov.iov_base = curr_start;
815 my_iov.iov_len = bytes_to_send;
Allan Stephens26896902011-04-21 10:42:07 -0500816 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000817 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700818 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700819 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700820 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700821 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822 curr_left -= bytes_to_send;
823 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700824 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825 }
826
827 curr_iov++;
828 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700829 res = bytes_sent;
830exit:
831 release_sock(sk);
832 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100833}
834
835/**
836 * auto_connect - complete connection setup to a remote port
837 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100838 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900839 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840 * Returns 0 on success, errno otherwise
841 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700842static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843{
Allan Stephens2da59912008-07-14 22:43:32 -0700844 struct tipc_sock *tsock = tipc_sk(sock->sk);
Ying Xue584d24b2012-11-29 18:51:19 -0500845 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846
Allan Stephens2da59912008-07-14 22:43:32 -0700847 tsock->peer_name.ref = msg_origport(msg);
848 tsock->peer_name.node = msg_orignode(msg);
Ying Xue584d24b2012-11-29 18:51:19 -0500849 p_ptr = tipc_port_deref(tsock->p->ref);
850 if (!p_ptr)
851 return -EINVAL;
852
853 __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
854
855 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
856 return -EINVAL;
857 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100858 sock->state = SS_CONNECTED;
859 return 0;
860}
861
862/**
863 * set_orig_addr - capture sender's address for received message
864 * @m: descriptor for message info
865 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900866 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867 * Note: Address is not captured if not requested by receiver.
868 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800869static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900871 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900873 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100874 addr->family = AF_TIPC;
875 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000876 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100877 addr->addr.id.ref = msg_origport(msg);
878 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000879 addr->addr.name.domain = 0; /* could leave uninitialized */
880 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881 m->msg_namelen = sizeof(struct sockaddr_tipc);
882 }
883}
884
885/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900886 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100887 * @m: descriptor for message info
888 * @msg: received message header
889 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900890 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900892 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893 * Returns 0 if successful, otherwise errno
894 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800895static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400896 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897{
898 u32 anc_data[3];
899 u32 err;
900 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700901 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902 int res;
903
904 if (likely(m->msg_controllen == 0))
905 return 0;
906
907 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908 err = msg ? msg_errcode(msg) : 0;
909 if (unlikely(err)) {
910 anc_data[0] = err;
911 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000912 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
913 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000915 if (anc_data[1]) {
916 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
917 msg_data(msg));
918 if (res)
919 return res;
920 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921 }
922
923 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
925 switch (dest_type) {
926 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700927 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928 anc_data[0] = msg_nametype(msg);
929 anc_data[1] = msg_namelower(msg);
930 anc_data[2] = msg_namelower(msg);
931 break;
932 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700933 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934 anc_data[0] = msg_nametype(msg);
935 anc_data[1] = msg_namelower(msg);
936 anc_data[2] = msg_nameupper(msg);
937 break;
938 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700939 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940 anc_data[0] = tport->conn_type;
941 anc_data[1] = tport->conn_instance;
942 anc_data[2] = tport->conn_instance;
943 break;
944 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700945 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946 }
Allan Stephens2db99832010-12-31 18:59:33 +0000947 if (has_name) {
948 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
949 if (res)
950 return res;
951 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100952
953 return 0;
954}
955
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900956/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100957 * recv_msg - receive packet-oriented message
958 * @iocb: (unused)
959 * @m: descriptor for message info
960 * @buf_len: total size of user buffer area
961 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900962 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100963 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
964 * If the complete message doesn't fit in user area, truncate it.
965 *
966 * Returns size of returned message data, errno otherwise
967 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968static int recv_msg(struct kiocb *iocb, struct socket *sock,
969 struct msghdr *m, size_t buf_len, int flags)
970{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700971 struct sock *sk = sock->sk;
972 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100973 struct sk_buff *buf;
974 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500975 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976 unsigned int sz;
977 u32 err;
978 int res;
979
Allan Stephens0c3141e2008-04-15 00:22:02 -0700980 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100981 if (unlikely(!buf_len))
982 return -EINVAL;
983
Allan Stephens0c3141e2008-04-15 00:22:02 -0700984 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985
Allan Stephens0c3141e2008-04-15 00:22:02 -0700986 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100987 res = -ENOTCONN;
988 goto exit;
989 }
990
Allan Stephens71092ea2011-02-23 14:52:14 -0500991 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700992restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993
Allan Stephens0c3141e2008-04-15 00:22:02 -0700994 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700995 while (skb_queue_empty(&sk->sk_receive_queue)) {
996 if (sock->state == SS_DISCONNECTING) {
997 res = -ENOTCONN;
998 goto exit;
999 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001000 if (timeout <= 0L) {
1001 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001002 goto exit;
1003 }
1004 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001005 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1006 tipc_rx_ready(sock),
1007 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001008 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001009 }
1010
1011 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001012 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 msg = buf_msg(buf);
1014 sz = msg_data_sz(msg);
1015 err = msg_errcode(msg);
1016
Per Lidenb97bf3f2006-01-02 19:04:38 +01001017 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001019 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020 goto restart;
1021 }
1022
1023 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 set_orig_addr(m, msg);
1025
1026 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001027 res = anc_data_recv(m, msg, tport);
1028 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 goto exit;
1030
1031 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032 if (!err) {
1033 if (unlikely(buf_len < sz)) {
1034 sz = buf_len;
1035 m->msg_flags |= MSG_TRUNC;
1036 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001037 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1038 m->msg_iov, sz);
1039 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041 res = sz;
1042 } else {
1043 if ((sock->state == SS_READY) ||
1044 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1045 res = 0;
1046 else
1047 res = -ECONNRESET;
1048 }
1049
1050 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001051 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001052 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -07001053 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1054 tipc_acknowledge(tport->ref, tport->conn_unacked);
1055 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001056 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001057exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001058 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059 return res;
1060}
1061
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001062/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001063 * recv_stream - receive stream-oriented data
1064 * @iocb: (unused)
1065 * @m: descriptor for message info
1066 * @buf_len: total size of user buffer area
1067 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001068 *
1069 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001070 * will optionally wait for more; never truncates data.
1071 *
1072 * Returns size of returned message data, errno otherwise
1073 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074static int recv_stream(struct kiocb *iocb, struct socket *sock,
1075 struct msghdr *m, size_t buf_len, int flags)
1076{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001077 struct sock *sk = sock->sk;
1078 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001079 struct sk_buff *buf;
1080 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -05001081 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001083 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001085 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001086 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001087
1088 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089 if (unlikely(!buf_len))
1090 return -EINVAL;
1091
Allan Stephens0c3141e2008-04-15 00:22:02 -07001092 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093
Erik Hugne5d21cb72013-06-17 10:54:38 -04001094 if (unlikely((sock->state == SS_UNCONNECTED))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001095 res = -ENOTCONN;
1096 goto exit;
1097 }
1098
Florian Westphal3720d402010-08-17 11:00:04 +00001099 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Allan Stephens71092ea2011-02-23 14:52:14 -05001100 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001101
Allan Stephens0c3141e2008-04-15 00:22:02 -07001102restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001103 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001104 while (skb_queue_empty(&sk->sk_receive_queue)) {
1105 if (sock->state == SS_DISCONNECTING) {
1106 res = -ENOTCONN;
1107 goto exit;
1108 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001109 if (timeout <= 0L) {
1110 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001111 goto exit;
1112 }
1113 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001114 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1115 tipc_rx_ready(sock),
1116 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001117 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001118 }
1119
1120 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001121 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122 msg = buf_msg(buf);
1123 sz = msg_data_sz(msg);
1124 err = msg_errcode(msg);
1125
1126 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001128 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129 goto restart;
1130 }
1131
1132 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133 if (sz_copied == 0) {
1134 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001135 res = anc_data_recv(m, msg, tport);
1136 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 goto exit;
1138 }
1139
1140 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001142 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143
Allan Stephens0232fd02011-02-21 09:45:40 -05001144 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 needed = (buf_len - sz_copied);
1146 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001147
1148 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1149 m->msg_iov, sz_to_copy);
1150 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001151 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001152
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153 sz_copied += sz_to_copy;
1154
1155 if (sz_to_copy < sz) {
1156 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001157 TIPC_SKB_CB(buf)->handle =
1158 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 goto exit;
1160 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 } else {
1162 if (sz_copied != 0)
1163 goto exit; /* can't add error msg to valid data */
1164
1165 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1166 res = 0;
1167 else
1168 res = -ECONNRESET;
1169 }
1170
1171 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001173 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1174 tipc_acknowledge(tport->ref, tport->conn_unacked);
1175 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001176 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177
1178 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001179 if ((sz_copied < buf_len) && /* didn't get all requested data */
1180 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001181 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001182 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1183 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184 goto restart;
1185
1186exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001187 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001188 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189}
1190
1191/**
Ying Xuef288bef2012-08-21 11:16:57 +08001192 * tipc_write_space - wake up thread if port congestion is released
1193 * @sk: socket
1194 */
1195static void tipc_write_space(struct sock *sk)
1196{
1197 struct socket_wq *wq;
1198
1199 rcu_read_lock();
1200 wq = rcu_dereference(sk->sk_wq);
1201 if (wq_has_sleeper(wq))
1202 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1203 POLLWRNORM | POLLWRBAND);
1204 rcu_read_unlock();
1205}
1206
1207/**
1208 * tipc_data_ready - wake up threads to indicate messages have been received
1209 * @sk: socket
1210 * @len: the length of messages
1211 */
1212static void tipc_data_ready(struct sock *sk, int len)
1213{
1214 struct socket_wq *wq;
1215
1216 rcu_read_lock();
1217 wq = rcu_dereference(sk->sk_wq);
1218 if (wq_has_sleeper(wq))
1219 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1220 POLLRDNORM | POLLRDBAND);
1221 rcu_read_unlock();
1222}
1223
1224/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001225 * filter_connect - Handle all incoming messages for a connection-based socket
1226 * @tsock: TIPC socket
1227 * @msg: message
1228 *
1229 * Returns TIPC error status code and socket error status code
1230 * once it encounters some errors
1231 */
1232static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
1233{
1234 struct socket *sock = tsock->sk.sk_socket;
1235 struct tipc_msg *msg = buf_msg(*buf);
Ying Xue584d24b2012-11-29 18:51:19 -05001236 struct sock *sk = &tsock->sk;
Ying Xue7e6c1312012-11-29 18:39:14 -05001237 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001238 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001239
1240 if (msg_mcast(msg))
1241 return retval;
1242
1243 switch ((int)sock->state) {
1244 case SS_CONNECTED:
1245 /* Accept only connection-based messages sent by peer */
1246 if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
1247 if (unlikely(msg_errcode(msg))) {
1248 sock->state = SS_DISCONNECTING;
1249 __tipc_disconnect(tsock->p);
1250 }
1251 retval = TIPC_OK;
1252 }
1253 break;
1254 case SS_CONNECTING:
1255 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001256 if (unlikely(msg_errcode(msg))) {
1257 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001258 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001259 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001260 break;
1261 }
1262
1263 if (unlikely(!msg_connected(msg)))
1264 break;
1265
1266 res = auto_connect(sock, msg);
1267 if (res) {
1268 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001269 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001270 retval = TIPC_OK;
1271 break;
1272 }
1273
1274 /* If an incoming message is an 'ACK-', it should be
1275 * discarded here because it doesn't contain useful
1276 * data. In addition, we should try to wake up
1277 * connect() routine if sleeping.
1278 */
1279 if (msg_data_sz(msg) == 0) {
1280 kfree_skb(*buf);
1281 *buf = NULL;
1282 if (waitqueue_active(sk_sleep(sk)))
1283 wake_up_interruptible(sk_sleep(sk));
1284 }
1285 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001286 break;
1287 case SS_LISTENING:
1288 case SS_UNCONNECTED:
1289 /* Accept only SYN message */
1290 if (!msg_connected(msg) && !(msg_errcode(msg)))
1291 retval = TIPC_OK;
1292 break;
1293 case SS_DISCONNECTING:
1294 break;
1295 default:
1296 pr_err("Unknown socket state %u\n", sock->state);
1297 }
1298 return retval;
1299}
1300
1301/**
Ying Xueaba79f32013-01-20 23:30:09 +01001302 * rcvbuf_limit - get proper overload limit of socket receive queue
1303 * @sk: socket
1304 * @buf: message
1305 *
1306 * For all connection oriented messages, irrespective of importance,
1307 * the default overload value (i.e. 67MB) is set as limit.
1308 *
1309 * For all connectionless messages, by default new queue limits are
1310 * as belows:
1311 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001312 * TIPC_LOW_IMPORTANCE (4 MB)
1313 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1314 * TIPC_HIGH_IMPORTANCE (16 MB)
1315 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001316 *
1317 * Returns overload limit according to corresponding message importance
1318 */
1319static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1320{
1321 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001322
1323 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001324 return sysctl_tipc_rmem[2];
1325
1326 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1327 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001328}
1329
1330/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001331 * filter_rcv - validate incoming message
1332 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001334 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001335 * Enqueues message on receive queue if acceptable; optionally handles
1336 * disconnect indication for a connected socket.
1337 *
1338 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001339 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1341 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001342static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001344 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001346 unsigned int limit = rcvbuf_limit(sk, buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001347 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001348
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001350 if (msg_type(msg) > TIPC_DIRECT_MSG)
1351 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001352
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001354 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 } else {
Ying Xue7e6c1312012-11-29 18:39:14 -05001357 res = filter_connect(tipc_sk(sk), &buf);
1358 if (res != TIPC_OK || buf == NULL)
1359 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001360 }
1361
1362 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001363 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1364 return TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365
Ying Xueaba79f32013-01-20 23:30:09 +01001366 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001367 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001368 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001369 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001370
Ying Xuef288bef2012-08-21 11:16:57 +08001371 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372 return TIPC_OK;
1373}
1374
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001375/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001376 * backlog_rcv - handle incoming message from backlog queue
1377 * @sk: socket
1378 * @buf: message
1379 *
1380 * Caller must hold socket lock, but not port lock.
1381 *
1382 * Returns 0
1383 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001384static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1385{
1386 u32 res;
1387
1388 res = filter_rcv(sk, buf);
1389 if (res)
1390 tipc_reject_msg(buf, res);
1391 return 0;
1392}
1393
1394/**
1395 * dispatch - handle incoming message
1396 * @tport: TIPC port that received message
1397 * @buf: message
1398 *
1399 * Called with port lock already taken.
1400 *
1401 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1402 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001403static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1404{
Ying Xuec0fee8a2013-06-17 10:54:46 -04001405 struct sock *sk = tport->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001406 u32 res;
1407
1408 /*
1409 * Process message if socket is unlocked; otherwise add to backlog queue
1410 *
1411 * This code is based on sk_receive_skb(), but must be distinct from it
1412 * since a TIPC-specific filter/reject mechanism is utilized
1413 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001414 bh_lock_sock(sk);
1415 if (!sock_owned_by_user(sk)) {
1416 res = filter_rcv(sk, buf);
1417 } else {
Ying Xueaba79f32013-01-20 23:30:09 +01001418 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
Zhu Yi53eecb12010-03-04 18:01:45 +00001419 res = TIPC_ERR_OVERLOAD;
1420 else
1421 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001422 }
1423 bh_unlock_sock(sk);
1424
1425 return res;
1426}
1427
1428/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001429 * wakeupdispatch - wake up port after congestion
1430 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001431 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001432 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001434static void wakeupdispatch(struct tipc_port *tport)
1435{
Ying Xuec0fee8a2013-06-17 10:54:46 -04001436 struct sock *sk = tport->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001437
Ying Xuef288bef2012-08-21 11:16:57 +08001438 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001439}
1440
Ying Xue78eb3a52014-01-17 09:50:03 +08001441static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1442{
1443 struct sock *sk = sock->sk;
1444 DEFINE_WAIT(wait);
1445 int done;
1446
1447 do {
1448 int err = sock_error(sk);
1449 if (err)
1450 return err;
1451 if (!*timeo_p)
1452 return -ETIMEDOUT;
1453 if (signal_pending(current))
1454 return sock_intr_errno(*timeo_p);
1455
1456 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1457 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1458 finish_wait(sk_sleep(sk), &wait);
1459 } while (!done);
1460 return 0;
1461}
1462
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463/**
1464 * connect - establish a connection to another TIPC port
1465 * @sock: socket structure
1466 * @dest: socket address for destination port
1467 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001468 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001469 *
1470 * Returns 0 on success, errno otherwise
1471 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001472static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473 int flags)
1474{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001475 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001476 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1477 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001478 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1479 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001480 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001481
Allan Stephens0c3141e2008-04-15 00:22:02 -07001482 lock_sock(sk);
1483
Allan Stephensb89741a2008-04-15 00:20:37 -07001484 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001485 if (sock->state == SS_READY) {
1486 res = -EOPNOTSUPP;
1487 goto exit;
1488 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001489
Allan Stephensb89741a2008-04-15 00:20:37 -07001490 /*
1491 * Reject connection attempt using multicast address
1492 *
1493 * Note: send_msg() validates the rest of the address fields,
1494 * so there's no need to do it here
1495 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001496 if (dst->addrtype == TIPC_ADDR_MCAST) {
1497 res = -EINVAL;
1498 goto exit;
1499 }
1500
Ying Xue78eb3a52014-01-17 09:50:03 +08001501 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001502 switch (sock->state) {
1503 case SS_UNCONNECTED:
1504 /* Send a 'SYN-' to destination */
1505 m.msg_name = dest;
1506 m.msg_namelen = destlen;
1507
1508 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1509 * indicate send_msg() is never blocked.
1510 */
1511 if (!timeout)
1512 m.msg_flags = MSG_DONTWAIT;
1513
1514 res = send_msg(NULL, sock, &m, 0);
1515 if ((res < 0) && (res != -EWOULDBLOCK))
1516 goto exit;
1517
1518 /* Just entered SS_CONNECTING state; the only
1519 * difference is that return value in non-blocking
1520 * case is EINPROGRESS, rather than EALREADY.
1521 */
1522 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001523 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001524 if (previous == SS_CONNECTING)
1525 res = -EALREADY;
1526 if (!timeout)
1527 goto exit;
1528 timeout = msecs_to_jiffies(timeout);
1529 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1530 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001531 break;
1532 case SS_CONNECTED:
1533 res = -EISCONN;
1534 break;
1535 default:
1536 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001537 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001538 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001539exit:
1540 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001541 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001542}
1543
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001544/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001545 * listen - allow socket to listen for incoming connections
1546 * @sock: socket structure
1547 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001548 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001549 * Returns 0 on success, errno otherwise
1550 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001551static int listen(struct socket *sock, int len)
1552{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001553 struct sock *sk = sock->sk;
1554 int res;
1555
1556 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001557
Ying Xue245f3d32011-07-06 06:01:13 -04001558 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001559 res = -EINVAL;
1560 else {
1561 sock->state = SS_LISTENING;
1562 res = 0;
1563 }
1564
1565 release_sock(sk);
1566 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001567}
1568
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001569/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001570 * accept - wait for connection request
1571 * @sock: listening socket
1572 * @newsock: new socket that is to be connected
1573 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001574 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001575 * Returns 0 on success, errno otherwise
1576 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001577static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001578{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001579 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001580 struct sk_buff *buf;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001581 struct tipc_sock *new_tsock;
1582 struct tipc_port *new_tport;
1583 struct tipc_msg *msg;
1584 u32 new_ref;
1585
Allan Stephens0c3141e2008-04-15 00:22:02 -07001586 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001587
Allan Stephens0c3141e2008-04-15 00:22:02 -07001588 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001589
Allan Stephens0c3141e2008-04-15 00:22:02 -07001590 if (sock->state != SS_LISTENING) {
1591 res = -EINVAL;
1592 goto exit;
1593 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001594
Allan Stephens0c3141e2008-04-15 00:22:02 -07001595 while (skb_queue_empty(&sk->sk_receive_queue)) {
1596 if (flags & O_NONBLOCK) {
1597 res = -EWOULDBLOCK;
1598 goto exit;
1599 }
1600 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001601 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001602 (!skb_queue_empty(&sk->sk_receive_queue)));
1603 lock_sock(sk);
1604 if (res)
1605 goto exit;
1606 }
1607
1608 buf = skb_peek(&sk->sk_receive_queue);
1609
Ying Xuec5fa7b32013-06-17 10:54:39 -04001610 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001611 if (res)
1612 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001613
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001614 new_sk = new_sock->sk;
1615 new_tsock = tipc_sk(new_sk);
1616 new_tport = new_tsock->p;
1617 new_ref = new_tport->ref;
1618 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001619
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001620 /* we lock on new_sk; but lockdep sees the lock on sk */
1621 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001622
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001623 /*
1624 * Reject any stray messages received by new socket
1625 * before the socket lock was taken (very, very unlikely)
1626 */
1627 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001628
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001629 /* Connect new socket to it's peer */
1630 new_tsock->peer_name.ref = msg_origport(msg);
1631 new_tsock->peer_name.node = msg_orignode(msg);
1632 tipc_connect(new_ref, &new_tsock->peer_name);
1633 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001634
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001635 tipc_set_portimportance(new_ref, msg_importance(msg));
1636 if (msg_named(msg)) {
1637 new_tport->conn_type = msg_nametype(msg);
1638 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001639 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001640
1641 /*
1642 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1643 * Respond to 'SYN+' by queuing it on new socket.
1644 */
1645 if (!msg_data_sz(msg)) {
1646 struct msghdr m = {NULL,};
1647
1648 advance_rx_queue(sk);
1649 send_packet(NULL, new_sock, &m, 0);
1650 } else {
1651 __skb_dequeue(&sk->sk_receive_queue);
1652 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001653 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001654 }
1655 release_sock(new_sk);
1656
Per Lidenb97bf3f2006-01-02 19:04:38 +01001657exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001658 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001659 return res;
1660}
1661
1662/**
1663 * shutdown - shutdown socket connection
1664 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001665 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666 *
1667 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001668 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669 * Returns 0 on success, errno otherwise
1670 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001671static int shutdown(struct socket *sock, int how)
1672{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001673 struct sock *sk = sock->sk;
1674 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001675 struct sk_buff *buf;
1676 int res;
1677
Allan Stephense247a8f2008-03-06 15:05:38 -08001678 if (how != SHUT_RDWR)
1679 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001680
Allan Stephens0c3141e2008-04-15 00:22:02 -07001681 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001682
1683 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001684 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001685 case SS_CONNECTED:
1686
Per Lidenb97bf3f2006-01-02 19:04:38 +01001687restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001688 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001689 buf = __skb_dequeue(&sk->sk_receive_queue);
1690 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001691 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001692 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001693 goto restart;
1694 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001695 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001697 } else {
1698 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001700
1701 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702
1703 /* fall through */
1704
1705 case SS_DISCONNECTING:
1706
Ying Xue75031152012-10-29 09:38:15 -04001707 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001708 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001709
1710 /* Wake up anyone sleeping in poll */
1711 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001712 res = 0;
1713 break;
1714
1715 default:
1716 res = -ENOTCONN;
1717 }
1718
Allan Stephens0c3141e2008-04-15 00:22:02 -07001719 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001720 return res;
1721}
1722
1723/**
1724 * setsockopt - set socket option
1725 * @sock: socket structure
1726 * @lvl: option level
1727 * @opt: option identifier
1728 * @ov: pointer to new option value
1729 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001730 *
1731 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001732 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001733 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001734 * Returns 0 on success, errno otherwise
1735 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001736static int setsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
1737 unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001738{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001739 struct sock *sk = sock->sk;
1740 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741 u32 value;
1742 int res;
1743
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001744 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1745 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746 if (lvl != SOL_TIPC)
1747 return -ENOPROTOOPT;
1748 if (ol < sizeof(value))
1749 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001750 res = get_user(value, (u32 __user *)ov);
1751 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001752 return res;
1753
Allan Stephens0c3141e2008-04-15 00:22:02 -07001754 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001755
Per Lidenb97bf3f2006-01-02 19:04:38 +01001756 switch (opt) {
1757 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001758 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001759 break;
1760 case TIPC_SRC_DROPPABLE:
1761 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001762 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001763 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001764 res = -ENOPROTOOPT;
1765 break;
1766 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001767 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001768 break;
1769 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001770 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001771 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001772 break;
1773 default:
1774 res = -EINVAL;
1775 }
1776
Allan Stephens0c3141e2008-04-15 00:22:02 -07001777 release_sock(sk);
1778
Per Lidenb97bf3f2006-01-02 19:04:38 +01001779 return res;
1780}
1781
1782/**
1783 * getsockopt - get socket option
1784 * @sock: socket structure
1785 * @lvl: option level
1786 * @opt: option identifier
1787 * @ov: receptacle for option value
1788 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001789 *
1790 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001791 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001792 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793 * Returns 0 on success, errno otherwise
1794 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001795static int getsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
1796 int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001797{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001798 struct sock *sk = sock->sk;
1799 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001800 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001801 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001802 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001803
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001804 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1805 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001806 if (lvl != SOL_TIPC)
1807 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001808 res = get_user(len, ol);
1809 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001810 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001811
Allan Stephens0c3141e2008-04-15 00:22:02 -07001812 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001813
1814 switch (opt) {
1815 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001816 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001817 break;
1818 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001819 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001820 break;
1821 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001822 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001823 break;
1824 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001825 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001826 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001827 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001828 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001829 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001830 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001831 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001832 value = skb_queue_len(&sk->sk_receive_queue);
1833 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834 default:
1835 res = -EINVAL;
1836 }
1837
Allan Stephens0c3141e2008-04-15 00:22:02 -07001838 release_sock(sk);
1839
Paul Gortmaker25860c32010-12-31 18:59:31 +00001840 if (res)
1841 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001842
Paul Gortmaker25860c32010-12-31 18:59:31 +00001843 if (len < sizeof(value))
1844 return -EINVAL;
1845
1846 if (copy_to_user(ov, &value, sizeof(value)))
1847 return -EFAULT;
1848
1849 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001850}
1851
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001852/* Protocol switches for the various types of TIPC sockets */
1853
Florian Westphalbca65ea2008-02-07 18:18:01 -08001854static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001855 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001856 .family = AF_TIPC,
1857 .release = release,
1858 .bind = bind,
1859 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001860 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001861 .accept = sock_no_accept,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001862 .getname = get_name,
1863 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001864 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001865 .listen = sock_no_listen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001866 .shutdown = shutdown,
1867 .setsockopt = setsockopt,
1868 .getsockopt = getsockopt,
1869 .sendmsg = send_msg,
1870 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001871 .mmap = sock_no_mmap,
1872 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001873};
1874
Florian Westphalbca65ea2008-02-07 18:18:01 -08001875static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001876 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001877 .family = AF_TIPC,
1878 .release = release,
1879 .bind = bind,
1880 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001881 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001882 .accept = accept,
1883 .getname = get_name,
1884 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001885 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886 .listen = listen,
1887 .shutdown = shutdown,
1888 .setsockopt = setsockopt,
1889 .getsockopt = getsockopt,
1890 .sendmsg = send_packet,
1891 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001892 .mmap = sock_no_mmap,
1893 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001894};
1895
Florian Westphalbca65ea2008-02-07 18:18:01 -08001896static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001897 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001898 .family = AF_TIPC,
1899 .release = release,
1900 .bind = bind,
1901 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001902 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001903 .accept = accept,
1904 .getname = get_name,
1905 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001906 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907 .listen = listen,
1908 .shutdown = shutdown,
1909 .setsockopt = setsockopt,
1910 .getsockopt = getsockopt,
1911 .sendmsg = send_stream,
1912 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001913 .mmap = sock_no_mmap,
1914 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001915};
1916
Florian Westphalbca65ea2008-02-07 18:18:01 -08001917static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001918 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001919 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04001920 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01001921};
1922
1923static struct proto tipc_proto = {
1924 .name = "TIPC",
1925 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04001926 .obj_size = sizeof(struct tipc_sock),
1927 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01001928};
1929
Ying Xuec5fa7b32013-06-17 10:54:39 -04001930static struct proto tipc_proto_kern = {
1931 .name = "TIPC",
1932 .obj_size = sizeof(struct tipc_sock),
1933 .sysctl_rmem = sysctl_tipc_rmem
1934};
1935
Per Lidenb97bf3f2006-01-02 19:04:38 +01001936/**
Per Liden4323add2006-01-18 00:38:21 +01001937 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001938 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001939 * Returns 0 on success, errno otherwise
1940 */
Per Liden4323add2006-01-18 00:38:21 +01001941int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001942{
1943 int res;
1944
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001945 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001946 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001947 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001948 goto out;
1949 }
1950
1951 res = sock_register(&tipc_family_ops);
1952 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001953 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001954 proto_unregister(&tipc_proto);
1955 goto out;
1956 }
1957
1958 sockets_enabled = 1;
1959 out:
1960 return res;
1961}
1962
1963/**
Per Liden4323add2006-01-18 00:38:21 +01001964 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001965 */
Per Liden4323add2006-01-18 00:38:21 +01001966void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001967{
1968 if (!sockets_enabled)
1969 return;
1970
1971 sockets_enabled = 0;
1972 sock_unregister(tipc_family_ops.family);
1973 proto_unregister(&tipc_proto);
1974}