blob: 32037c57937d8cfdc85a0606a8fbc8d57bf2d8cc [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 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700354 res = tipc_deleteport(tport->ref);
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{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700387 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388
Allan Stephens0c3141e2008-04-15 00:22:02 -0700389 if (unlikely(!uaddr_len))
390 return tipc_withdraw(portref, 0, NULL);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900391
Allan Stephens0c3141e2008-04-15 00:22:02 -0700392 if (uaddr_len < sizeof(struct sockaddr_tipc))
393 return -EINVAL;
394 if (addr->family != AF_TIPC)
395 return -EAFNOSUPPORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397 if (addr->addrtype == TIPC_ADDR_NAME)
398 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700399 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
400 return -EAFNOSUPPORT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900401
Ying Xue13a2e892013-06-17 10:54:40 -0400402 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400403 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
404 (addr->addr.nameseq.type != TIPC_CFG_SRV))
Allan Stephensc422f1b2011-11-02 15:49:40 -0400405 return -EACCES;
406
Allan Stephens0c3141e2008-04-15 00:22:02 -0700407 return (addr->scope > 0) ?
408 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
409 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410}
411
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900412/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 * get_name - get port ID of socket or peer socket
414 * @sock: socket structure
415 * @uaddr: area for returned socket address
416 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700417 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900418 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700420 *
Allan Stephens2da59912008-07-14 22:43:32 -0700421 * NOTE: This routine doesn't need to take the socket lock since it only
422 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000423 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900425static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 int *uaddr_len, int peer)
427{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700429 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000431 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700432 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700433 if ((sock->state != SS_CONNECTED) &&
434 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
435 return -ENOTCONN;
436 addr->addr.id.ref = tsock->peer_name.ref;
437 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700438 } else {
Allan Stephensb924dcf2010-11-30 12:01:03 +0000439 addr->addr.id.ref = tsock->p->ref;
440 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700441 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442
443 *uaddr_len = sizeof(*addr);
444 addr->addrtype = TIPC_ADDR_ID;
445 addr->family = AF_TIPC;
446 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447 addr->addr.name.domain = 0;
448
Allan Stephens0c3141e2008-04-15 00:22:02 -0700449 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450}
451
452/**
453 * poll - read and possibly block on pollmask
454 * @file: file structure associated with the socket
455 * @sock: socket for which to calculate the poll bits
456 * @wait: ???
457 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700458 * Returns pollmask value
459 *
460 * COMMENTARY:
461 * It appears that the usual socket locking mechanisms are not useful here
462 * since the pollmask info is potentially out-of-date the moment this routine
463 * exits. TCP and other protocols seem to rely on higher level poll routines
464 * to handle any preventable race conditions, so TIPC will do the same ...
465 *
466 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700467 *
Allan Stephensf662c072010-08-17 11:00:06 +0000468 * socket state flags set
469 * ------------ ---------
470 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200471 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000472 *
473 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
474 * no write flags
475 *
476 * connected POLLIN/POLLRDNORM if data in rx queue
477 * POLLOUT if port is not congested
478 *
479 * disconnecting POLLIN/POLLRDNORM/POLLHUP
480 * no write flags
481 *
482 * listening POLLIN if SYN in rx queue
483 * no write flags
484 *
485 * ready POLLIN/POLLRDNORM if data in rx queue
486 * [connectionless] POLLOUT (since port cannot be congested)
487 *
488 * IMPORTANT: The fact that a read or write operation is indicated does NOT
489 * imply that the operation will succeed, merely that it should be performed
490 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900492static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493 poll_table *wait)
494{
Allan Stephens9b674e82008-03-26 16:48:21 -0700495 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000496 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700497
Ying Xuef288bef2012-08-21 11:16:57 +0800498 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700499
Allan Stephensf662c072010-08-17 11:00:06 +0000500 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200501 case SS_UNCONNECTED:
502 if (!tipc_sk_port(sk)->congested)
503 mask |= POLLOUT;
504 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000505 case SS_READY:
506 case SS_CONNECTED:
507 if (!tipc_sk_port(sk)->congested)
508 mask |= POLLOUT;
509 /* fall thru' */
510 case SS_CONNECTING:
511 case SS_LISTENING:
512 if (!skb_queue_empty(&sk->sk_receive_queue))
513 mask |= (POLLIN | POLLRDNORM);
514 break;
515 case SS_DISCONNECTING:
516 mask = (POLLIN | POLLRDNORM | POLLHUP);
517 break;
518 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700519
520 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521}
522
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900523/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524 * dest_name_check - verify user is permitted to send to specified port name
525 * @dest: destination address
526 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900527 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528 * Prevents restricted configuration commands from being issued by
529 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900530 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531 * Returns 0 if permission is granted, otherwise errno
532 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800533static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534{
535 struct tipc_cfg_msg_hdr hdr;
536
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900537 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
538 return 0;
539 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
540 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900541 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
542 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543
Allan Stephens3f8dd942011-01-18 13:09:29 -0500544 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
545 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900546 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700548 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900550
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 return 0;
552}
553
554/**
555 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700556 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 * @sock: socket structure
558 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700559 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900560 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900562 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
564 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900565 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 * Returns the number of bytes sent on success, or errno otherwise
567 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568static int send_msg(struct kiocb *iocb, struct socket *sock,
569 struct msghdr *m, size_t total_len)
570{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700571 struct sock *sk = sock->sk;
572 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900573 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574 int needs_conn;
Ying Xue1d835872011-07-06 05:53:15 -0400575 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 int res = -EINVAL;
577
578 if (unlikely(!dest))
579 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700580 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
581 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 return -EINVAL;
Ying Xue97f8b872013-01-31 21:51:47 +0100583 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400584 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585
Allan Stephens0c3141e2008-04-15 00:22:02 -0700586 if (iocb)
587 lock_sock(sk);
588
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589 needs_conn = (sock->state != SS_READY);
590 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700591 if (sock->state == SS_LISTENING) {
592 res = -EPIPE;
593 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700594 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700595 if (sock->state != SS_UNCONNECTED) {
596 res = -EISCONN;
597 goto exit;
598 }
Erik Hugne5d21cb72013-06-17 10:54:38 -0400599 if (tport->published) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700600 res = -EOPNOTSUPP;
601 goto exit;
602 }
603 if (dest->addrtype == TIPC_ADDR_NAME) {
604 tport->conn_type = dest->addr.name.name.type;
605 tport->conn_instance = dest->addr.name.name.instance;
606 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607
608 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700609 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 }
611
Ying Xue1d835872011-07-06 05:53:15 -0400612 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
613
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900614 do {
615 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000616 res = dest_name_check(dest, m);
617 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700618 break;
619 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900620 &dest->addr.name.name,
621 dest->addr.name.domain,
Allan Stephens26896902011-04-21 10:42:07 -0500622 m->msg_iov,
623 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000624 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700625 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900626 &dest->addr.id,
Allan Stephens26896902011-04-21 10:42:07 -0500627 m->msg_iov,
628 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000629 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 if (needs_conn) {
631 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700632 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 }
Allan Stephens2db99832010-12-31 18:59:33 +0000634 res = dest_name_check(dest, m);
635 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700636 break;
637 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900638 &dest->addr.nameseq,
Allan Stephens26896902011-04-21 10:42:07 -0500639 m->msg_iov,
640 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900641 }
642 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000643 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700644 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700645 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900646 }
Ying Xue1d835872011-07-06 05:53:15 -0400647 if (timeout_val <= 0L) {
648 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700649 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700651 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400652 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
653 !tport->congested, timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700654 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900655 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700656
657exit:
658 if (iocb)
659 release_sock(sk);
660 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661}
662
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900663/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700665 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 * @sock: socket structure
667 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700668 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900669 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900671 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 * Returns the number of bytes sent on success, or errno otherwise
673 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674static int send_packet(struct kiocb *iocb, struct socket *sock,
675 struct msghdr *m, size_t total_len)
676{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700677 struct sock *sk = sock->sk;
678 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900679 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Ying Xue1d835872011-07-06 05:53:15 -0400680 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681 int res;
682
683 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100684 if (unlikely(dest))
685 return send_msg(iocb, sock, m, total_len);
686
Ying Xue97f8b872013-01-31 21:51:47 +0100687 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400688 return -EMSGSIZE;
689
Allan Stephens0c3141e2008-04-15 00:22:02 -0700690 if (iocb)
691 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100692
Ying Xue1d835872011-07-06 05:53:15 -0400693 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
694
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900695 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700696 if (unlikely(sock->state != SS_CONNECTED)) {
697 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900698 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700699 else
700 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700701 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700702 }
703
Ying Xue9446b872013-10-18 07:23:15 +0200704 res = tipc_send(tport->ref, m->msg_iov, total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000705 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700706 break;
Ying Xue1d835872011-07-06 05:53:15 -0400707 if (timeout_val <= 0L) {
708 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700709 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700711 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400712 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
713 (!tport->congested || !tport->connected), timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700714 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900715 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700716
717 if (iocb)
718 release_sock(sk);
719 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100720}
721
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900722/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100723 * send_stream - send stream-oriented data
724 * @iocb: (unused)
725 * @sock: socket structure
726 * @m: data to send
727 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900728 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900730 *
731 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700732 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734static int send_stream(struct kiocb *iocb, struct socket *sock,
735 struct msghdr *m, size_t total_len)
736{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700737 struct sock *sk = sock->sk;
738 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100739 struct msghdr my_msg;
740 struct iovec my_iov;
741 struct iovec *curr_iov;
742 int curr_iovlen;
743 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700744 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 int curr_left;
746 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700747 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900749
Allan Stephens0c3141e2008-04-15 00:22:02 -0700750 lock_sock(sk);
751
Allan Stephens05646c92007-06-10 17:25:24 -0700752 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900753 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700754 if (sock->state == SS_UNCONNECTED) {
755 res = send_packet(NULL, sock, m, total_len);
756 goto exit;
757 } else if (sock->state == SS_DISCONNECTING) {
758 res = -EPIPE;
759 goto exit;
760 } else {
761 res = -ENOTCONN;
762 goto exit;
763 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900764 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100765
Allan Stephens0c3141e2008-04-15 00:22:02 -0700766 if (unlikely(m->msg_name)) {
767 res = -EISCONN;
768 goto exit;
769 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700770
Ying Xue97f8b872013-01-31 21:51:47 +0100771 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400772 res = -EMSGSIZE;
773 goto exit;
774 }
775
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900776 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100777 * Send each iovec entry using one or more messages
778 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900779 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780 * (i.e. one large iovec entry), but could be improved to pass sets
781 * of small iovec entries into send_packet().
782 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700783 curr_iov = m->msg_iov;
784 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 my_msg.msg_iov = &my_iov;
786 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700787 my_msg.msg_flags = m->msg_flags;
788 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700789 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100790
Allan Stephens05646c92007-06-10 17:25:24 -0700791 hdr_size = msg_hdr_sz(&tport->phdr);
792
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793 while (curr_iovlen--) {
794 curr_start = curr_iov->iov_base;
795 curr_left = curr_iov->iov_len;
796
797 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700798 bytes_to_send = tport->max_pkt - hdr_size;
799 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
800 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
801 if (curr_left < bytes_to_send)
802 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100803 my_iov.iov_base = curr_start;
804 my_iov.iov_len = bytes_to_send;
Allan Stephens26896902011-04-21 10:42:07 -0500805 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000806 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700807 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700808 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700809 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700810 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100811 curr_left -= bytes_to_send;
812 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700813 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100814 }
815
816 curr_iov++;
817 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700818 res = bytes_sent;
819exit:
820 release_sock(sk);
821 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822}
823
824/**
825 * auto_connect - complete connection setup to a remote port
826 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100827 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900828 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829 * Returns 0 on success, errno otherwise
830 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700831static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100832{
Allan Stephens2da59912008-07-14 22:43:32 -0700833 struct tipc_sock *tsock = tipc_sk(sock->sk);
Ying Xue584d24b2012-11-29 18:51:19 -0500834 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100835
Allan Stephens2da59912008-07-14 22:43:32 -0700836 tsock->peer_name.ref = msg_origport(msg);
837 tsock->peer_name.node = msg_orignode(msg);
Ying Xue584d24b2012-11-29 18:51:19 -0500838 p_ptr = tipc_port_deref(tsock->p->ref);
839 if (!p_ptr)
840 return -EINVAL;
841
842 __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
843
844 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
845 return -EINVAL;
846 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 sock->state = SS_CONNECTED;
848 return 0;
849}
850
851/**
852 * set_orig_addr - capture sender's address for received message
853 * @m: descriptor for message info
854 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900855 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100856 * Note: Address is not captured if not requested by receiver.
857 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800858static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900860 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900862 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863 addr->family = AF_TIPC;
864 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000865 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866 addr->addr.id.ref = msg_origport(msg);
867 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000868 addr->addr.name.domain = 0; /* could leave uninitialized */
869 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870 m->msg_namelen = sizeof(struct sockaddr_tipc);
871 }
872}
873
874/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900875 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100876 * @m: descriptor for message info
877 * @msg: received message header
878 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900879 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900881 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100882 * Returns 0 if successful, otherwise errno
883 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800884static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400885 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886{
887 u32 anc_data[3];
888 u32 err;
889 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700890 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 int res;
892
893 if (likely(m->msg_controllen == 0))
894 return 0;
895
896 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897 err = msg ? msg_errcode(msg) : 0;
898 if (unlikely(err)) {
899 anc_data[0] = err;
900 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000901 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
902 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000904 if (anc_data[1]) {
905 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
906 msg_data(msg));
907 if (res)
908 return res;
909 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910 }
911
912 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
914 switch (dest_type) {
915 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700916 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100917 anc_data[0] = msg_nametype(msg);
918 anc_data[1] = msg_namelower(msg);
919 anc_data[2] = msg_namelower(msg);
920 break;
921 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700922 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100923 anc_data[0] = msg_nametype(msg);
924 anc_data[1] = msg_namelower(msg);
925 anc_data[2] = msg_nameupper(msg);
926 break;
927 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700928 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100929 anc_data[0] = tport->conn_type;
930 anc_data[1] = tport->conn_instance;
931 anc_data[2] = tport->conn_instance;
932 break;
933 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700934 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935 }
Allan Stephens2db99832010-12-31 18:59:33 +0000936 if (has_name) {
937 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
938 if (res)
939 return res;
940 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941
942 return 0;
943}
944
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900945/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946 * recv_msg - receive packet-oriented message
947 * @iocb: (unused)
948 * @m: descriptor for message info
949 * @buf_len: total size of user buffer area
950 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900951 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100952 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
953 * If the complete message doesn't fit in user area, truncate it.
954 *
955 * Returns size of returned message data, errno otherwise
956 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100957static int recv_msg(struct kiocb *iocb, struct socket *sock,
958 struct msghdr *m, size_t buf_len, int flags)
959{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700960 struct sock *sk = sock->sk;
961 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100962 struct sk_buff *buf;
963 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500964 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100965 unsigned int sz;
966 u32 err;
967 int res;
968
Allan Stephens0c3141e2008-04-15 00:22:02 -0700969 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970 if (unlikely(!buf_len))
971 return -EINVAL;
972
Allan Stephens0c3141e2008-04-15 00:22:02 -0700973 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974
Allan Stephens0c3141e2008-04-15 00:22:02 -0700975 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976 res = -ENOTCONN;
977 goto exit;
978 }
979
Allan Stephens71092ea2011-02-23 14:52:14 -0500980 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700981restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100982
Allan Stephens0c3141e2008-04-15 00:22:02 -0700983 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700984 while (skb_queue_empty(&sk->sk_receive_queue)) {
985 if (sock->state == SS_DISCONNECTING) {
986 res = -ENOTCONN;
987 goto exit;
988 }
Allan Stephens71092ea2011-02-23 14:52:14 -0500989 if (timeout <= 0L) {
990 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700991 goto exit;
992 }
993 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -0500994 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
995 tipc_rx_ready(sock),
996 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700997 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700998 }
999
1000 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001001 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 msg = buf_msg(buf);
1003 sz = msg_data_sz(msg);
1004 err = msg_errcode(msg);
1005
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001008 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001009 goto restart;
1010 }
1011
1012 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 set_orig_addr(m, msg);
1014
1015 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001016 res = anc_data_recv(m, msg, tport);
1017 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 goto exit;
1019
1020 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001021 if (!err) {
1022 if (unlikely(buf_len < sz)) {
1023 sz = buf_len;
1024 m->msg_flags |= MSG_TRUNC;
1025 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001026 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1027 m->msg_iov, sz);
1028 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030 res = sz;
1031 } else {
1032 if ((sock->state == SS_READY) ||
1033 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1034 res = 0;
1035 else
1036 res = -ECONNRESET;
1037 }
1038
1039 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens990098062008-04-15 00:06:12 -07001041 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -07001042 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1043 tipc_acknowledge(tport->ref, tport->conn_unacked);
1044 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001045 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001047 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 return res;
1049}
1050
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001051/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052 * recv_stream - receive stream-oriented data
1053 * @iocb: (unused)
1054 * @m: descriptor for message info
1055 * @buf_len: total size of user buffer area
1056 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001057 *
1058 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059 * will optionally wait for more; never truncates data.
1060 *
1061 * Returns size of returned message data, errno otherwise
1062 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001063static int recv_stream(struct kiocb *iocb, struct socket *sock,
1064 struct msghdr *m, size_t buf_len, int flags)
1065{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001066 struct sock *sk = sock->sk;
1067 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068 struct sk_buff *buf;
1069 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -05001070 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001072 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001075 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076
1077 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 if (unlikely(!buf_len))
1079 return -EINVAL;
1080
Allan Stephens0c3141e2008-04-15 00:22:02 -07001081 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082
Erik Hugne5d21cb72013-06-17 10:54:38 -04001083 if (unlikely((sock->state == SS_UNCONNECTED))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084 res = -ENOTCONN;
1085 goto exit;
1086 }
1087
Florian Westphal3720d402010-08-17 11:00:04 +00001088 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Allan Stephens71092ea2011-02-23 14:52:14 -05001089 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001090
Allan Stephens0c3141e2008-04-15 00:22:02 -07001091restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001092 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001093 while (skb_queue_empty(&sk->sk_receive_queue)) {
1094 if (sock->state == SS_DISCONNECTING) {
1095 res = -ENOTCONN;
1096 goto exit;
1097 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001098 if (timeout <= 0L) {
1099 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001100 goto exit;
1101 }
1102 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001103 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1104 tipc_rx_ready(sock),
1105 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001106 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001107 }
1108
1109 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001110 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001111 msg = buf_msg(buf);
1112 sz = msg_data_sz(msg);
1113 err = msg_errcode(msg);
1114
1115 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001117 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118 goto restart;
1119 }
1120
1121 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122 if (sz_copied == 0) {
1123 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001124 res = anc_data_recv(m, msg, tport);
1125 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126 goto exit;
1127 }
1128
1129 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001131 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132
Allan Stephens0232fd02011-02-21 09:45:40 -05001133 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134 needed = (buf_len - sz_copied);
1135 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001136
1137 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1138 m->msg_iov, sz_to_copy);
1139 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001141
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142 sz_copied += sz_to_copy;
1143
1144 if (sz_to_copy < sz) {
1145 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001146 TIPC_SKB_CB(buf)->handle =
1147 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 goto exit;
1149 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150 } else {
1151 if (sz_copied != 0)
1152 goto exit; /* can't add error msg to valid data */
1153
1154 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1155 res = 0;
1156 else
1157 res = -ECONNRESET;
1158 }
1159
1160 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001162 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1163 tipc_acknowledge(tport->ref, tport->conn_unacked);
1164 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001165 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166
1167 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001168 if ((sz_copied < buf_len) && /* didn't get all requested data */
1169 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001170 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001171 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1172 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 goto restart;
1174
1175exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001176 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001177 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178}
1179
1180/**
Ying Xuef288bef2012-08-21 11:16:57 +08001181 * tipc_write_space - wake up thread if port congestion is released
1182 * @sk: socket
1183 */
1184static void tipc_write_space(struct sock *sk)
1185{
1186 struct socket_wq *wq;
1187
1188 rcu_read_lock();
1189 wq = rcu_dereference(sk->sk_wq);
1190 if (wq_has_sleeper(wq))
1191 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1192 POLLWRNORM | POLLWRBAND);
1193 rcu_read_unlock();
1194}
1195
1196/**
1197 * tipc_data_ready - wake up threads to indicate messages have been received
1198 * @sk: socket
1199 * @len: the length of messages
1200 */
1201static void tipc_data_ready(struct sock *sk, int len)
1202{
1203 struct socket_wq *wq;
1204
1205 rcu_read_lock();
1206 wq = rcu_dereference(sk->sk_wq);
1207 if (wq_has_sleeper(wq))
1208 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1209 POLLRDNORM | POLLRDBAND);
1210 rcu_read_unlock();
1211}
1212
1213/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001214 * filter_connect - Handle all incoming messages for a connection-based socket
1215 * @tsock: TIPC socket
1216 * @msg: message
1217 *
1218 * Returns TIPC error status code and socket error status code
1219 * once it encounters some errors
1220 */
1221static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
1222{
1223 struct socket *sock = tsock->sk.sk_socket;
1224 struct tipc_msg *msg = buf_msg(*buf);
Ying Xue584d24b2012-11-29 18:51:19 -05001225 struct sock *sk = &tsock->sk;
Ying Xue7e6c1312012-11-29 18:39:14 -05001226 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001227 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001228
1229 if (msg_mcast(msg))
1230 return retval;
1231
1232 switch ((int)sock->state) {
1233 case SS_CONNECTED:
1234 /* Accept only connection-based messages sent by peer */
1235 if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
1236 if (unlikely(msg_errcode(msg))) {
1237 sock->state = SS_DISCONNECTING;
1238 __tipc_disconnect(tsock->p);
1239 }
1240 retval = TIPC_OK;
1241 }
1242 break;
1243 case SS_CONNECTING:
1244 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001245 if (unlikely(msg_errcode(msg))) {
1246 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001247 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001248 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001249 break;
1250 }
1251
1252 if (unlikely(!msg_connected(msg)))
1253 break;
1254
1255 res = auto_connect(sock, msg);
1256 if (res) {
1257 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001258 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001259 retval = TIPC_OK;
1260 break;
1261 }
1262
1263 /* If an incoming message is an 'ACK-', it should be
1264 * discarded here because it doesn't contain useful
1265 * data. In addition, we should try to wake up
1266 * connect() routine if sleeping.
1267 */
1268 if (msg_data_sz(msg) == 0) {
1269 kfree_skb(*buf);
1270 *buf = NULL;
1271 if (waitqueue_active(sk_sleep(sk)))
1272 wake_up_interruptible(sk_sleep(sk));
1273 }
1274 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001275 break;
1276 case SS_LISTENING:
1277 case SS_UNCONNECTED:
1278 /* Accept only SYN message */
1279 if (!msg_connected(msg) && !(msg_errcode(msg)))
1280 retval = TIPC_OK;
1281 break;
1282 case SS_DISCONNECTING:
1283 break;
1284 default:
1285 pr_err("Unknown socket state %u\n", sock->state);
1286 }
1287 return retval;
1288}
1289
1290/**
Ying Xueaba79f32013-01-20 23:30:09 +01001291 * rcvbuf_limit - get proper overload limit of socket receive queue
1292 * @sk: socket
1293 * @buf: message
1294 *
1295 * For all connection oriented messages, irrespective of importance,
1296 * the default overload value (i.e. 67MB) is set as limit.
1297 *
1298 * For all connectionless messages, by default new queue limits are
1299 * as belows:
1300 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001301 * TIPC_LOW_IMPORTANCE (4 MB)
1302 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1303 * TIPC_HIGH_IMPORTANCE (16 MB)
1304 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001305 *
1306 * Returns overload limit according to corresponding message importance
1307 */
1308static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1309{
1310 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001311
1312 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001313 return sysctl_tipc_rmem[2];
1314
1315 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1316 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001317}
1318
1319/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001320 * filter_rcv - validate incoming message
1321 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001322 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001323 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001324 * Enqueues message on receive queue if acceptable; optionally handles
1325 * disconnect indication for a connected socket.
1326 *
1327 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001328 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001329 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1330 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001331static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001333 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001335 unsigned int limit = rcvbuf_limit(sk, buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001336 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001339 if (msg_type(msg) > TIPC_DIRECT_MSG)
1340 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001341
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001343 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345 } else {
Ying Xue7e6c1312012-11-29 18:39:14 -05001346 res = filter_connect(tipc_sk(sk), &buf);
1347 if (res != TIPC_OK || buf == NULL)
1348 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 }
1350
1351 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001352 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1353 return TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001354
Ying Xueaba79f32013-01-20 23:30:09 +01001355 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001356 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001357 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001358 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001359
Ying Xuef288bef2012-08-21 11:16:57 +08001360 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 return TIPC_OK;
1362}
1363
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001364/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001365 * backlog_rcv - handle incoming message from backlog queue
1366 * @sk: socket
1367 * @buf: message
1368 *
1369 * Caller must hold socket lock, but not port lock.
1370 *
1371 * Returns 0
1372 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001373static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1374{
1375 u32 res;
1376
1377 res = filter_rcv(sk, buf);
1378 if (res)
1379 tipc_reject_msg(buf, res);
1380 return 0;
1381}
1382
1383/**
1384 * dispatch - handle incoming message
1385 * @tport: TIPC port that received message
1386 * @buf: message
1387 *
1388 * Called with port lock already taken.
1389 *
1390 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1391 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001392static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1393{
Ying Xuec0fee8a2013-06-17 10:54:46 -04001394 struct sock *sk = tport->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001395 u32 res;
1396
1397 /*
1398 * Process message if socket is unlocked; otherwise add to backlog queue
1399 *
1400 * This code is based on sk_receive_skb(), but must be distinct from it
1401 * since a TIPC-specific filter/reject mechanism is utilized
1402 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001403 bh_lock_sock(sk);
1404 if (!sock_owned_by_user(sk)) {
1405 res = filter_rcv(sk, buf);
1406 } else {
Ying Xueaba79f32013-01-20 23:30:09 +01001407 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
Zhu Yi53eecb12010-03-04 18:01:45 +00001408 res = TIPC_ERR_OVERLOAD;
1409 else
1410 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001411 }
1412 bh_unlock_sock(sk);
1413
1414 return res;
1415}
1416
1417/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418 * wakeupdispatch - wake up port after congestion
1419 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001420 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001421 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001423static void wakeupdispatch(struct tipc_port *tport)
1424{
Ying Xuec0fee8a2013-06-17 10:54:46 -04001425 struct sock *sk = tport->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426
Ying Xuef288bef2012-08-21 11:16:57 +08001427 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001428}
1429
1430/**
1431 * connect - establish a connection to another TIPC port
1432 * @sock: socket structure
1433 * @dest: socket address for destination port
1434 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001435 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001436 *
1437 * Returns 0 on success, errno otherwise
1438 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001439static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001440 int flags)
1441{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001442 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001443 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1444 struct msghdr m = {NULL,};
Allan Stephensa0f40f02011-05-26 13:44:34 -04001445 unsigned int timeout;
Allan Stephensb89741a2008-04-15 00:20:37 -07001446 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001447
Allan Stephens0c3141e2008-04-15 00:22:02 -07001448 lock_sock(sk);
1449
Allan Stephensb89741a2008-04-15 00:20:37 -07001450 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001451 if (sock->state == SS_READY) {
1452 res = -EOPNOTSUPP;
1453 goto exit;
1454 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001455
Allan Stephensb89741a2008-04-15 00:20:37 -07001456 /*
1457 * Reject connection attempt using multicast address
1458 *
1459 * Note: send_msg() validates the rest of the address fields,
1460 * so there's no need to do it here
1461 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001462 if (dst->addrtype == TIPC_ADDR_MCAST) {
1463 res = -EINVAL;
1464 goto exit;
1465 }
1466
Ying Xue584d24b2012-11-29 18:51:19 -05001467 timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468
Ying Xue584d24b2012-11-29 18:51:19 -05001469 switch (sock->state) {
1470 case SS_UNCONNECTED:
1471 /* Send a 'SYN-' to destination */
1472 m.msg_name = dest;
1473 m.msg_namelen = destlen;
1474
1475 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1476 * indicate send_msg() is never blocked.
1477 */
1478 if (!timeout)
1479 m.msg_flags = MSG_DONTWAIT;
1480
1481 res = send_msg(NULL, sock, &m, 0);
1482 if ((res < 0) && (res != -EWOULDBLOCK))
1483 goto exit;
1484
1485 /* Just entered SS_CONNECTING state; the only
1486 * difference is that return value in non-blocking
1487 * case is EINPROGRESS, rather than EALREADY.
1488 */
1489 res = -EINPROGRESS;
1490 break;
1491 case SS_CONNECTING:
1492 res = -EALREADY;
1493 break;
1494 case SS_CONNECTED:
1495 res = -EISCONN;
1496 break;
1497 default:
1498 res = -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001499 goto exit;
Allan Stephensb89741a2008-04-15 00:20:37 -07001500 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001501
Ying Xue584d24b2012-11-29 18:51:19 -05001502 if (sock->state == SS_CONNECTING) {
1503 if (!timeout)
1504 goto exit;
1505
1506 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1507 release_sock(sk);
1508 res = wait_event_interruptible_timeout(*sk_sleep(sk),
1509 sock->state != SS_CONNECTING,
1510 timeout ? (long)msecs_to_jiffies(timeout)
1511 : MAX_SCHEDULE_TIMEOUT);
1512 lock_sock(sk);
1513 if (res <= 0) {
1514 if (res == 0)
1515 res = -ETIMEDOUT;
1516 else
1517 ; /* leave "res" unchanged */
1518 goto exit;
1519 }
1520 }
1521
1522 if (unlikely(sock->state == SS_DISCONNECTING))
1523 res = sock_error(sk);
1524 else
1525 res = 0;
1526
Allan Stephens0c3141e2008-04-15 00:22:02 -07001527exit:
1528 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001529 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001530}
1531
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001532/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001533 * listen - allow socket to listen for incoming connections
1534 * @sock: socket structure
1535 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001536 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001537 * Returns 0 on success, errno otherwise
1538 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001539static int listen(struct socket *sock, int len)
1540{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001541 struct sock *sk = sock->sk;
1542 int res;
1543
1544 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001545
Ying Xue245f3d32011-07-06 06:01:13 -04001546 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001547 res = -EINVAL;
1548 else {
1549 sock->state = SS_LISTENING;
1550 res = 0;
1551 }
1552
1553 release_sock(sk);
1554 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001555}
1556
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001557/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001558 * accept - wait for connection request
1559 * @sock: listening socket
1560 * @newsock: new socket that is to be connected
1561 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001562 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001563 * Returns 0 on success, errno otherwise
1564 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001565static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001566{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001567 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001568 struct sk_buff *buf;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001569 struct tipc_sock *new_tsock;
1570 struct tipc_port *new_tport;
1571 struct tipc_msg *msg;
1572 u32 new_ref;
1573
Allan Stephens0c3141e2008-04-15 00:22:02 -07001574 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001575
Allan Stephens0c3141e2008-04-15 00:22:02 -07001576 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001577
Allan Stephens0c3141e2008-04-15 00:22:02 -07001578 if (sock->state != SS_LISTENING) {
1579 res = -EINVAL;
1580 goto exit;
1581 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001582
Allan Stephens0c3141e2008-04-15 00:22:02 -07001583 while (skb_queue_empty(&sk->sk_receive_queue)) {
1584 if (flags & O_NONBLOCK) {
1585 res = -EWOULDBLOCK;
1586 goto exit;
1587 }
1588 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001589 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001590 (!skb_queue_empty(&sk->sk_receive_queue)));
1591 lock_sock(sk);
1592 if (res)
1593 goto exit;
1594 }
1595
1596 buf = skb_peek(&sk->sk_receive_queue);
1597
Ying Xuec5fa7b32013-06-17 10:54:39 -04001598 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001599 if (res)
1600 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001601
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001602 new_sk = new_sock->sk;
1603 new_tsock = tipc_sk(new_sk);
1604 new_tport = new_tsock->p;
1605 new_ref = new_tport->ref;
1606 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001607
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001608 /* we lock on new_sk; but lockdep sees the lock on sk */
1609 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001610
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001611 /*
1612 * Reject any stray messages received by new socket
1613 * before the socket lock was taken (very, very unlikely)
1614 */
1615 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001616
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001617 /* Connect new socket to it's peer */
1618 new_tsock->peer_name.ref = msg_origport(msg);
1619 new_tsock->peer_name.node = msg_orignode(msg);
1620 tipc_connect(new_ref, &new_tsock->peer_name);
1621 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001622
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001623 tipc_set_portimportance(new_ref, msg_importance(msg));
1624 if (msg_named(msg)) {
1625 new_tport->conn_type = msg_nametype(msg);
1626 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001627 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001628
1629 /*
1630 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1631 * Respond to 'SYN+' by queuing it on new socket.
1632 */
1633 if (!msg_data_sz(msg)) {
1634 struct msghdr m = {NULL,};
1635
1636 advance_rx_queue(sk);
1637 send_packet(NULL, new_sock, &m, 0);
1638 } else {
1639 __skb_dequeue(&sk->sk_receive_queue);
1640 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001641 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001642 }
1643 release_sock(new_sk);
1644
Per Lidenb97bf3f2006-01-02 19:04:38 +01001645exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001646 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001647 return res;
1648}
1649
1650/**
1651 * shutdown - shutdown socket connection
1652 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001653 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001654 *
1655 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001656 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001657 * Returns 0 on success, errno otherwise
1658 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001659static int shutdown(struct socket *sock, int how)
1660{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001661 struct sock *sk = sock->sk;
1662 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663 struct sk_buff *buf;
1664 int res;
1665
Allan Stephense247a8f2008-03-06 15:05:38 -08001666 if (how != SHUT_RDWR)
1667 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001668
Allan Stephens0c3141e2008-04-15 00:22:02 -07001669 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670
1671 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001672 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001673 case SS_CONNECTED:
1674
Per Lidenb97bf3f2006-01-02 19:04:38 +01001675restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001676 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001677 buf = __skb_dequeue(&sk->sk_receive_queue);
1678 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001679 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001680 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001681 goto restart;
1682 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001683 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001684 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001685 } else {
1686 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001687 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001688
1689 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690
1691 /* fall through */
1692
1693 case SS_DISCONNECTING:
1694
Ying Xue75031152012-10-29 09:38:15 -04001695 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001696 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001697
1698 /* Wake up anyone sleeping in poll */
1699 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001700 res = 0;
1701 break;
1702
1703 default:
1704 res = -ENOTCONN;
1705 }
1706
Allan Stephens0c3141e2008-04-15 00:22:02 -07001707 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708 return res;
1709}
1710
1711/**
1712 * setsockopt - set socket option
1713 * @sock: socket structure
1714 * @lvl: option level
1715 * @opt: option identifier
1716 * @ov: pointer to new option value
1717 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001718 *
1719 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001720 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001721 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001722 * Returns 0 on success, errno otherwise
1723 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001724static int setsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
1725 unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001727 struct sock *sk = sock->sk;
1728 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001729 u32 value;
1730 int res;
1731
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001732 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1733 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001734 if (lvl != SOL_TIPC)
1735 return -ENOPROTOOPT;
1736 if (ol < sizeof(value))
1737 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001738 res = get_user(value, (u32 __user *)ov);
1739 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001740 return res;
1741
Allan Stephens0c3141e2008-04-15 00:22:02 -07001742 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001743
Per Lidenb97bf3f2006-01-02 19:04:38 +01001744 switch (opt) {
1745 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001746 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001747 break;
1748 case TIPC_SRC_DROPPABLE:
1749 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001750 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001751 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001752 res = -ENOPROTOOPT;
1753 break;
1754 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001755 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001756 break;
1757 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001758 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001759 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001760 break;
1761 default:
1762 res = -EINVAL;
1763 }
1764
Allan Stephens0c3141e2008-04-15 00:22:02 -07001765 release_sock(sk);
1766
Per Lidenb97bf3f2006-01-02 19:04:38 +01001767 return res;
1768}
1769
1770/**
1771 * getsockopt - get socket option
1772 * @sock: socket structure
1773 * @lvl: option level
1774 * @opt: option identifier
1775 * @ov: receptacle for option value
1776 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001777 *
1778 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001779 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001780 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001781 * Returns 0 on success, errno otherwise
1782 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001783static int getsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
1784 int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001785{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001786 struct sock *sk = sock->sk;
1787 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001788 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001789 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001790 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001791
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001792 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1793 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001794 if (lvl != SOL_TIPC)
1795 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001796 res = get_user(len, ol);
1797 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001798 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001799
Allan Stephens0c3141e2008-04-15 00:22:02 -07001800 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001801
1802 switch (opt) {
1803 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001804 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001805 break;
1806 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001807 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001808 break;
1809 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001810 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001811 break;
1812 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001813 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001814 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001815 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001816 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001817 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001818 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001819 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001820 value = skb_queue_len(&sk->sk_receive_queue);
1821 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001822 default:
1823 res = -EINVAL;
1824 }
1825
Allan Stephens0c3141e2008-04-15 00:22:02 -07001826 release_sock(sk);
1827
Paul Gortmaker25860c32010-12-31 18:59:31 +00001828 if (res)
1829 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001830
Paul Gortmaker25860c32010-12-31 18:59:31 +00001831 if (len < sizeof(value))
1832 return -EINVAL;
1833
1834 if (copy_to_user(ov, &value, sizeof(value)))
1835 return -EFAULT;
1836
1837 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001838}
1839
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001840/* Protocol switches for the various types of TIPC sockets */
1841
Florian Westphalbca65ea2008-02-07 18:18:01 -08001842static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001843 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001844 .family = AF_TIPC,
1845 .release = release,
1846 .bind = bind,
1847 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001848 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001849 .accept = sock_no_accept,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001850 .getname = get_name,
1851 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001852 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001853 .listen = sock_no_listen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001854 .shutdown = shutdown,
1855 .setsockopt = setsockopt,
1856 .getsockopt = getsockopt,
1857 .sendmsg = send_msg,
1858 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001859 .mmap = sock_no_mmap,
1860 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001861};
1862
Florian Westphalbca65ea2008-02-07 18:18:01 -08001863static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001864 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001865 .family = AF_TIPC,
1866 .release = release,
1867 .bind = bind,
1868 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001869 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001870 .accept = accept,
1871 .getname = get_name,
1872 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001873 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001874 .listen = listen,
1875 .shutdown = shutdown,
1876 .setsockopt = setsockopt,
1877 .getsockopt = getsockopt,
1878 .sendmsg = send_packet,
1879 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001880 .mmap = sock_no_mmap,
1881 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001882};
1883
Florian Westphalbca65ea2008-02-07 18:18:01 -08001884static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001885 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886 .family = AF_TIPC,
1887 .release = release,
1888 .bind = bind,
1889 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001890 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001891 .accept = accept,
1892 .getname = get_name,
1893 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001894 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001895 .listen = listen,
1896 .shutdown = shutdown,
1897 .setsockopt = setsockopt,
1898 .getsockopt = getsockopt,
1899 .sendmsg = send_stream,
1900 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001901 .mmap = sock_no_mmap,
1902 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001903};
1904
Florian Westphalbca65ea2008-02-07 18:18:01 -08001905static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001906 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04001908 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01001909};
1910
1911static struct proto tipc_proto = {
1912 .name = "TIPC",
1913 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04001914 .obj_size = sizeof(struct tipc_sock),
1915 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01001916};
1917
Ying Xuec5fa7b32013-06-17 10:54:39 -04001918static struct proto tipc_proto_kern = {
1919 .name = "TIPC",
1920 .obj_size = sizeof(struct tipc_sock),
1921 .sysctl_rmem = sysctl_tipc_rmem
1922};
1923
Per Lidenb97bf3f2006-01-02 19:04:38 +01001924/**
Per Liden4323add2006-01-18 00:38:21 +01001925 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001926 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001927 * Returns 0 on success, errno otherwise
1928 */
Per Liden4323add2006-01-18 00:38:21 +01001929int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001930{
1931 int res;
1932
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001933 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001934 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001935 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001936 goto out;
1937 }
1938
1939 res = sock_register(&tipc_family_ops);
1940 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001941 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001942 proto_unregister(&tipc_proto);
1943 goto out;
1944 }
1945
1946 sockets_enabled = 1;
1947 out:
1948 return res;
1949}
1950
1951/**
Per Liden4323add2006-01-18 00:38:21 +01001952 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001953 */
Per Liden4323add2006-01-18 00:38:21 +01001954void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001955{
1956 if (!sockets_enabled)
1957 return;
1958
1959 sockets_enabled = 0;
1960 sock_unregister(tipc_family_ops.family);
1961 proto_unregister(&tipc_proto);
1962}