blob: e642ed5b3602047bda2ffe5ba79b0d8d64602a44 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04004 * Copyright (c) 2001-2007, 2012-2014, 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"
Erik Hugne78acb1f2014-04-24 16:26:47 +020039#include "node.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040
Erik Hugne2cf8aa12012-06-29 00:16:37 -040041#include <linux/export.h>
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -050042#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040043
Per Lidenb97bf3f2006-01-02 19:04:38 +010044#define SS_LISTENING -1 /* socket is listening */
45#define SS_READY -2 /* socket is connectionless */
46
Allan Stephens3654ea02008-04-13 21:35:11 -070047#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010048
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -040049static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -040050static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +080051static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080052static int tipc_release(struct socket *sock);
53static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Per Lidenb97bf3f2006-01-02 19:04:38 +010054
Florian Westphalbca65ea2008-02-07 18:18:01 -080055static const struct proto_ops packet_ops;
56static const struct proto_ops stream_ops;
57static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010058
59static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040060static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010061
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090062/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070063 * Revised TIPC socket locking policy:
64 *
65 * Most socket operations take the standard socket lock when they start
66 * and hold it until they finish (or until they need to sleep). Acquiring
67 * this lock grants the owner exclusive access to the fields of the socket
68 * data structures, with the exception of the backlog queue. A few socket
69 * operations can be done without taking the socket lock because they only
70 * read socket information that never changes during the life of the socket.
71 *
72 * Socket operations may acquire the lock for the associated TIPC port if they
73 * need to perform an operation on the port. If any routine needs to acquire
74 * both the socket lock and the port lock it must take the socket lock first
75 * to avoid the risk of deadlock.
76 *
77 * The dispatcher handling incoming messages cannot grab the socket lock in
78 * the standard fashion, since invoked it runs at the BH level and cannot block.
79 * Instead, it checks to see if the socket lock is currently owned by someone,
80 * and either handles the message itself or adds it to the socket's backlog
81 * queue; in the latter case the queued message is processed once the process
82 * owning the socket lock releases it.
83 *
84 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
85 * the problem of a blocked socket operation preventing any other operations
86 * from occurring. However, applications must be careful if they have
87 * multiple threads trying to send (or receive) on the same socket, as these
88 * operations might interfere with each other. For example, doing a connect
89 * and a receive at the same time might allow the receive to consume the
90 * ACK message meant for the connect. While additional work could be done
91 * to try and overcome this, it doesn't seem to be worthwhile at the present.
92 *
93 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
94 * that another operation that must be performed in a non-blocking manner is
95 * not delayed for very long because the lock has already been taken.
96 *
97 * NOTE: This code assumes that certain fields of a port/socket pair are
98 * constant over its lifetime; such fields can be examined without taking
99 * the socket lock and/or port lock, and do not need to be re-read even
100 * after resuming processing after waiting. These fields include:
101 * - socket type
102 * - pointer to socket sk structure (aka tipc_sock structure)
103 * - pointer to port structure
104 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100106
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400107#include "socket.h"
108
Per Lidenb97bf3f2006-01-02 19:04:38 +0100109/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700110 * advance_rx_queue - discard first buffer in socket receive queue
111 *
112 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700114static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100115{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400116 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100117}
118
119/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700120 * reject_rx_queue - reject all buffers in socket receive queue
121 *
122 * Caller must hold socket lock
123 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700124static void reject_rx_queue(struct sock *sk)
125{
126 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500127 u32 dnode;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700128
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500129 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
130 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
131 tipc_link_xmit2(buf, dnode, 0);
132 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700133}
134
135/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400136 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700137 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100138 * @sock: pre-allocated socket structure
139 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800140 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900141 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700142 * This routine creates additional data structures used by the TIPC socket,
143 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 *
145 * Returns 0 on success, errno otherwise
146 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400147static int tipc_sk_create(struct net *net, struct socket *sock,
148 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100149{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700150 const struct proto_ops *ops;
151 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400153 struct tipc_sock *tsk;
154 struct tipc_port *port;
155 u32 ref;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700156
157 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158 if (unlikely(protocol != 0))
159 return -EPROTONOSUPPORT;
160
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161 switch (sock->type) {
162 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700163 ops = &stream_ops;
164 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 break;
166 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700167 ops = &packet_ops;
168 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 break;
170 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700172 ops = &msg_ops;
173 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 break;
Allan Stephens49978652006-06-25 23:47:18 -0700175 default:
Allan Stephens49978652006-06-25 23:47:18 -0700176 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 }
178
Allan Stephens0c3141e2008-04-15 00:22:02 -0700179 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400180 if (!kern)
181 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
182 else
183 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
184
Allan Stephens0c3141e2008-04-15 00:22:02 -0700185 if (sk == NULL)
186 return -ENOMEM;
187
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400188 tsk = tipc_sk(sk);
189 port = &tsk->port;
190
191 ref = tipc_port_init(port, TIPC_LOW_IMPORTANCE);
192 if (!ref) {
193 pr_warn("Socket registration failed, ref. table exhausted\n");
Allan Stephens0c3141e2008-04-15 00:22:02 -0700194 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100195 return -ENOMEM;
196 }
197
Allan Stephens0c3141e2008-04-15 00:22:02 -0700198 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700199 sock->ops = ops;
200 sock->state = state;
201
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202 sock_init_data(sock, sk);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400203 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400204 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800205 sk->sk_data_ready = tipc_data_ready;
206 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400207 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
208 atomic_set(&tsk->dupl_rcvcnt, 0);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400209 tipc_port_unlock(port);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700210
Allan Stephens0c3141e2008-04-15 00:22:02 -0700211 if (sock->state == SS_READY) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400212 tipc_port_set_unreturnable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700213 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400214 tipc_port_set_unreliable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700215 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 return 0;
217}
218
219/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400220 * tipc_sock_create_local - create TIPC socket from inside TIPC module
221 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
222 *
223 * We cannot use sock_creat_kern here because it bumps module user count.
224 * Since socket owner and creator is the same module we must make sure
225 * that module count remains zero for module local sockets, otherwise
226 * we cannot do rmmod.
227 *
228 * Returns 0 on success, errno otherwise
229 */
230int tipc_sock_create_local(int type, struct socket **res)
231{
232 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400233
234 rc = sock_create_lite(AF_TIPC, type, 0, res);
235 if (rc < 0) {
236 pr_err("Failed to create kernel socket\n");
237 return rc;
238 }
239 tipc_sk_create(&init_net, *res, 0, 1);
240
Ying Xuec5fa7b32013-06-17 10:54:39 -0400241 return 0;
242}
243
244/**
245 * tipc_sock_release_local - release socket created by tipc_sock_create_local
246 * @sock: the socket to be released.
247 *
248 * Module reference count is not incremented when such sockets are created,
249 * so we must keep it from being decremented when they are released.
250 */
251void tipc_sock_release_local(struct socket *sock)
252{
Ying Xue247f0f32014-02-18 16:06:46 +0800253 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400254 sock->ops = NULL;
255 sock_release(sock);
256}
257
258/**
259 * tipc_sock_accept_local - accept a connection on a socket created
260 * with tipc_sock_create_local. Use this function to avoid that
261 * module reference count is inadvertently incremented.
262 *
263 * @sock: the accepting socket
264 * @newsock: reference to the new socket to be created
265 * @flags: socket flags
266 */
267
268int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400269 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400270{
271 struct sock *sk = sock->sk;
272 int ret;
273
274 ret = sock_create_lite(sk->sk_family, sk->sk_type,
275 sk->sk_protocol, newsock);
276 if (ret < 0)
277 return ret;
278
Ying Xue247f0f32014-02-18 16:06:46 +0800279 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400280 if (ret < 0) {
281 sock_release(*newsock);
282 return ret;
283 }
284 (*newsock)->ops = sock->ops;
285 return ret;
286}
287
288/**
Ying Xue247f0f32014-02-18 16:06:46 +0800289 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290 * @sock: socket to destroy
291 *
292 * This routine cleans up any messages that are still queued on the socket.
293 * For DGRAM and RDM socket types, all queued messages are rejected.
294 * For SEQPACKET and STREAM socket types, the first message is rejected
295 * and any others are discarded. (If the first message on a STREAM socket
296 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900297 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298 * NOTE: Rejected messages are not necessarily returned to the sender! They
299 * are returned or discarded according to the "destination droppable" setting
300 * specified for the message by the sender.
301 *
302 * Returns 0 on success, errno otherwise
303 */
Ying Xue247f0f32014-02-18 16:06:46 +0800304static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400307 struct tipc_sock *tsk;
308 struct tipc_port *port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500310 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311
Allan Stephens0c3141e2008-04-15 00:22:02 -0700312 /*
313 * Exit if socket isn't fully initialized (occurs when a failed accept()
314 * releases a pre-allocated child socket that was never used)
315 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700316 if (sk == NULL)
317 return 0;
318
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400319 tsk = tipc_sk(sk);
320 port = &tsk->port;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700321 lock_sock(sk);
322
323 /*
324 * Reject all unreceived messages, except on an active connection
325 * (which disconnects locally & sends a 'FIN+' to peer)
326 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700328 buf = __skb_dequeue(&sk->sk_receive_queue);
329 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 break;
Ying Xue40682432013-10-18 07:23:16 +0200331 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400332 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700333 else {
334 if ((sock->state == SS_CONNECTING) ||
335 (sock->state == SS_CONNECTED)) {
336 sock->state = SS_DISCONNECTING;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400337 tipc_port_disconnect(port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700338 }
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500339 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
340 tipc_link_xmit2(buf, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700341 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342 }
343
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400344 /* Destroy TIPC port; also disconnects an active connection and
345 * sends a 'FIN-' to peer.
Allan Stephens0c3141e2008-04-15 00:22:02 -0700346 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400347 tipc_port_destroy(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348
Allan Stephens0c3141e2008-04-15 00:22:02 -0700349 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100350 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351
Allan Stephens0c3141e2008-04-15 00:22:02 -0700352 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700353 sock->state = SS_DISCONNECTING;
354 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355
356 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700357 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200359 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360}
361
362/**
Ying Xue247f0f32014-02-18 16:06:46 +0800363 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364 * @sock: socket structure
365 * @uaddr: socket address describing name(s) and desired operation
366 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900367 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368 * Name and name sequence binding is indicated using a positive scope value;
369 * a negative scope value unbinds the specified name. Specifying no name
370 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900371 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700373 *
374 * NOTE: This routine doesn't need to take the socket lock since it doesn't
375 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376 */
Ying Xue247f0f32014-02-18 16:06:46 +0800377static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
378 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379{
Ying Xue84602762013-12-27 10:18:28 +0800380 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400382 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800383 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384
Ying Xue84602762013-12-27 10:18:28 +0800385 lock_sock(sk);
386 if (unlikely(!uaddr_len)) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400387 res = tipc_withdraw(&tsk->port, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800388 goto exit;
389 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900390
Ying Xue84602762013-12-27 10:18:28 +0800391 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
392 res = -EINVAL;
393 goto exit;
394 }
395 if (addr->family != AF_TIPC) {
396 res = -EAFNOSUPPORT;
397 goto exit;
398 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100399
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400 if (addr->addrtype == TIPC_ADDR_NAME)
401 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800402 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
403 res = -EAFNOSUPPORT;
404 goto exit;
405 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900406
Ying Xue13a2e892013-06-17 10:54:40 -0400407 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400408 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800409 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
410 res = -EACCES;
411 goto exit;
412 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400413
Ying Xue84602762013-12-27 10:18:28 +0800414 res = (addr->scope > 0) ?
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400415 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
416 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800417exit:
418 release_sock(sk);
419 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420}
421
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900422/**
Ying Xue247f0f32014-02-18 16:06:46 +0800423 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 * @sock: socket structure
425 * @uaddr: area for returned socket address
426 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700427 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900428 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700430 *
Allan Stephens2da59912008-07-14 22:43:32 -0700431 * NOTE: This routine doesn't need to take the socket lock since it only
432 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000433 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434 */
Ying Xue247f0f32014-02-18 16:06:46 +0800435static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
436 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400439 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000441 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700442 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700443 if ((sock->state != SS_CONNECTED) &&
444 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
445 return -ENOTCONN;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400446 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
447 addr->addr.id.node = tipc_port_peernode(&tsk->port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700448 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400449 addr->addr.id.ref = tsk->port.ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000450 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700451 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100452
453 *uaddr_len = sizeof(*addr);
454 addr->addrtype = TIPC_ADDR_ID;
455 addr->family = AF_TIPC;
456 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 addr->addr.name.domain = 0;
458
Allan Stephens0c3141e2008-04-15 00:22:02 -0700459 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460}
461
462/**
Ying Xue247f0f32014-02-18 16:06:46 +0800463 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 * @file: file structure associated with the socket
465 * @sock: socket for which to calculate the poll bits
466 * @wait: ???
467 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700468 * Returns pollmask value
469 *
470 * COMMENTARY:
471 * It appears that the usual socket locking mechanisms are not useful here
472 * since the pollmask info is potentially out-of-date the moment this routine
473 * exits. TCP and other protocols seem to rely on higher level poll routines
474 * to handle any preventable race conditions, so TIPC will do the same ...
475 *
476 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700477 *
Allan Stephensf662c072010-08-17 11:00:06 +0000478 * socket state flags set
479 * ------------ ---------
480 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200481 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000482 *
483 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
484 * no write flags
485 *
486 * connected POLLIN/POLLRDNORM if data in rx queue
487 * POLLOUT if port is not congested
488 *
489 * disconnecting POLLIN/POLLRDNORM/POLLHUP
490 * no write flags
491 *
492 * listening POLLIN if SYN in rx queue
493 * no write flags
494 *
495 * ready POLLIN/POLLRDNORM if data in rx queue
496 * [connectionless] POLLOUT (since port cannot be congested)
497 *
498 * IMPORTANT: The fact that a read or write operation is indicated does NOT
499 * imply that the operation will succeed, merely that it should be performed
500 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 */
Ying Xue247f0f32014-02-18 16:06:46 +0800502static unsigned int tipc_poll(struct file *file, struct socket *sock,
503 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504{
Allan Stephens9b674e82008-03-26 16:48:21 -0700505 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400506 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000507 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700508
Ying Xuef288bef2012-08-21 11:16:57 +0800509 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700510
Allan Stephensf662c072010-08-17 11:00:06 +0000511 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200512 case SS_UNCONNECTED:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400513 if (!tsk->port.congested)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200514 mask |= POLLOUT;
515 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000516 case SS_READY:
517 case SS_CONNECTED:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400518 if (!tsk->port.congested)
Allan Stephensf662c072010-08-17 11:00:06 +0000519 mask |= POLLOUT;
520 /* fall thru' */
521 case SS_CONNECTING:
522 case SS_LISTENING:
523 if (!skb_queue_empty(&sk->sk_receive_queue))
524 mask |= (POLLIN | POLLRDNORM);
525 break;
526 case SS_DISCONNECTING:
527 mask = (POLLIN | POLLRDNORM | POLLHUP);
528 break;
529 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700530
531 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100532}
533
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900534/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 * dest_name_check - verify user is permitted to send to specified port name
536 * @dest: destination address
537 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900538 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539 * Prevents restricted configuration commands from being issued by
540 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900541 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542 * Returns 0 if permission is granted, otherwise errno
543 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800544static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545{
546 struct tipc_cfg_msg_hdr hdr;
547
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900548 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
549 return 0;
550 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
551 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900552 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
553 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554
Allan Stephens3f8dd942011-01-18 13:09:29 -0500555 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
556 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900557 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100558 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700559 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900561
Per Lidenb97bf3f2006-01-02 19:04:38 +0100562 return 0;
563}
564
Ying Xue3f405042014-01-17 09:50:05 +0800565static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
566{
567 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400568 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800569 DEFINE_WAIT(wait);
570 int done;
571
572 do {
573 int err = sock_error(sk);
574 if (err)
575 return err;
576 if (sock->state == SS_DISCONNECTING)
577 return -EPIPE;
578 if (!*timeo_p)
579 return -EAGAIN;
580 if (signal_pending(current))
581 return sock_intr_errno(*timeo_p);
582
583 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400584 done = sk_wait_event(sk, timeo_p, !tsk->port.congested);
Ying Xue3f405042014-01-17 09:50:05 +0800585 finish_wait(sk_sleep(sk), &wait);
586 } while (!done);
587 return 0;
588}
589
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400590
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591/**
Ying Xue247f0f32014-02-18 16:06:46 +0800592 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700593 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 * @sock: socket structure
595 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700596 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900597 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900599 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
601 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900602 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603 * Returns the number of bytes sent on success, or errno otherwise
604 */
Ying Xue247f0f32014-02-18 16:06:46 +0800605static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
606 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700608 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400609 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400610 struct tipc_port *port = &tsk->port;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100611 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 int needs_conn;
Ying Xue3f405042014-01-17 09:50:05 +0800613 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 int res = -EINVAL;
615
616 if (unlikely(!dest))
617 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700618 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
619 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 return -EINVAL;
Ying Xue97f8b872013-01-31 21:51:47 +0100621 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400622 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623
Allan Stephens0c3141e2008-04-15 00:22:02 -0700624 if (iocb)
625 lock_sock(sk);
626
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 needs_conn = (sock->state != SS_READY);
628 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700629 if (sock->state == SS_LISTENING) {
630 res = -EPIPE;
631 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700632 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700633 if (sock->state != SS_UNCONNECTED) {
634 res = -EISCONN;
635 goto exit;
636 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400637 if (tsk->port.published) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700638 res = -EOPNOTSUPP;
639 goto exit;
640 }
641 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400642 tsk->port.conn_type = dest->addr.name.name.type;
643 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700644 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645
646 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700647 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 }
649
Ying Xue3f405042014-01-17 09:50:05 +0800650 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900651 do {
652 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000653 res = dest_name_check(dest, m);
654 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700655 break;
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400656 res = tipc_send2name(port,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900657 &dest->addr.name.name,
658 dest->addr.name.domain,
Allan Stephens26896902011-04-21 10:42:07 -0500659 m->msg_iov,
660 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000661 } else if (dest->addrtype == TIPC_ADDR_ID) {
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400662 res = tipc_send2port(port,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900663 &dest->addr.id,
Allan Stephens26896902011-04-21 10:42:07 -0500664 m->msg_iov,
665 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000666 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667 if (needs_conn) {
668 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700669 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 }
Allan Stephens2db99832010-12-31 18:59:33 +0000671 res = dest_name_check(dest, m);
672 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700673 break;
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400674 res = tipc_port_mcast_xmit(port,
Ying Xue247f0f32014-02-18 16:06:46 +0800675 &dest->addr.nameseq,
676 m->msg_iov,
677 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900678 }
679 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000680 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700681 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700682 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900683 }
Ying Xue3f405042014-01-17 09:50:05 +0800684 res = tipc_wait_for_sndmsg(sock, &timeo);
685 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700686 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900687 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700688
689exit:
690 if (iocb)
691 release_sock(sk);
692 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693}
694
Ying Xue391a6dd2014-01-17 09:50:06 +0800695static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
696{
697 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400698 struct tipc_sock *tsk = tipc_sk(sk);
699 struct tipc_port *port = &tsk->port;
Ying Xue391a6dd2014-01-17 09:50:06 +0800700 DEFINE_WAIT(wait);
701 int done;
702
703 do {
704 int err = sock_error(sk);
705 if (err)
706 return err;
707 if (sock->state == SS_DISCONNECTING)
708 return -EPIPE;
709 else if (sock->state != SS_CONNECTED)
710 return -ENOTCONN;
711 if (!*timeo_p)
712 return -EAGAIN;
713 if (signal_pending(current))
714 return sock_intr_errno(*timeo_p);
715
716 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
717 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400718 (!port->congested || !port->connected));
Ying Xue391a6dd2014-01-17 09:50:06 +0800719 finish_wait(sk_sleep(sk), &wait);
720 } while (!done);
721 return 0;
722}
723
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900724/**
Ying Xue247f0f32014-02-18 16:06:46 +0800725 * tipc_send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700726 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100727 * @sock: socket structure
728 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700729 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900730 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900732 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733 * Returns the number of bytes sent on success, or errno otherwise
734 */
Ying Xue247f0f32014-02-18 16:06:46 +0800735static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
736 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700738 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400739 struct tipc_sock *tsk = tipc_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100740 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue391a6dd2014-01-17 09:50:06 +0800741 int res = -EINVAL;
742 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743
744 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 if (unlikely(dest))
Ying Xue247f0f32014-02-18 16:06:46 +0800746 return tipc_sendmsg(iocb, sock, m, total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747
Ying Xue97f8b872013-01-31 21:51:47 +0100748 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400749 return -EMSGSIZE;
750
Allan Stephens0c3141e2008-04-15 00:22:02 -0700751 if (iocb)
752 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753
Ying Xue391a6dd2014-01-17 09:50:06 +0800754 if (unlikely(sock->state != SS_CONNECTED)) {
755 if (sock->state == SS_DISCONNECTING)
756 res = -EPIPE;
757 else
758 res = -ENOTCONN;
759 goto exit;
760 }
Ying Xue1d835872011-07-06 05:53:15 -0400761
Ying Xue391a6dd2014-01-17 09:50:06 +0800762 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900763 do {
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400764 res = tipc_send(&tsk->port, m->msg_iov, total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000765 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700766 break;
Ying Xue391a6dd2014-01-17 09:50:06 +0800767 res = tipc_wait_for_sndpkt(sock, &timeo);
768 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700769 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900770 } while (1);
Ying Xue391a6dd2014-01-17 09:50:06 +0800771exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700772 if (iocb)
773 release_sock(sk);
774 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775}
776
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900777/**
Ying Xue247f0f32014-02-18 16:06:46 +0800778 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779 * @iocb: (unused)
780 * @sock: socket structure
781 * @m: data to send
782 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900783 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100784 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900785 *
786 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700787 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788 */
Ying Xue247f0f32014-02-18 16:06:46 +0800789static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
790 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700792 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400793 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794 struct msghdr my_msg;
795 struct iovec my_iov;
796 struct iovec *curr_iov;
797 int curr_iovlen;
798 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700799 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100800 int curr_left;
801 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700802 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100803 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900804
Allan Stephens0c3141e2008-04-15 00:22:02 -0700805 lock_sock(sk);
806
Allan Stephens05646c92007-06-10 17:25:24 -0700807 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900808 if (unlikely(sock->state != SS_CONNECTED)) {
wangweidong3b8401f2013-12-12 09:36:40 +0800809 if (sock->state == SS_UNCONNECTED)
Ying Xue247f0f32014-02-18 16:06:46 +0800810 res = tipc_send_packet(NULL, sock, m, total_len);
wangweidongb0555972013-12-27 10:09:39 +0800811 else
812 res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800813 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900814 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815
Allan Stephens0c3141e2008-04-15 00:22:02 -0700816 if (unlikely(m->msg_name)) {
817 res = -EISCONN;
818 goto exit;
819 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700820
Ying Xue97f8b872013-01-31 21:51:47 +0100821 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400822 res = -EMSGSIZE;
823 goto exit;
824 }
825
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900826 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100827 * Send each iovec entry using one or more messages
828 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900829 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830 * (i.e. one large iovec entry), but could be improved to pass sets
831 * of small iovec entries into send_packet().
832 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700833 curr_iov = m->msg_iov;
834 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100835 my_msg.msg_iov = &my_iov;
836 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700837 my_msg.msg_flags = m->msg_flags;
838 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700839 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400841 hdr_size = msg_hdr_sz(&tsk->port.phdr);
Allan Stephens05646c92007-06-10 17:25:24 -0700842
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843 while (curr_iovlen--) {
844 curr_start = curr_iov->iov_base;
845 curr_left = curr_iov->iov_len;
846
847 while (curr_left) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400848 bytes_to_send = tsk->port.max_pkt - hdr_size;
Allan Stephens05646c92007-06-10 17:25:24 -0700849 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
850 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
851 if (curr_left < bytes_to_send)
852 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100853 my_iov.iov_base = curr_start;
854 my_iov.iov_len = bytes_to_send;
Ying Xue247f0f32014-02-18 16:06:46 +0800855 res = tipc_send_packet(NULL, sock, &my_msg,
856 bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000857 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700858 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700859 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700860 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700861 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100862 curr_left -= bytes_to_send;
863 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700864 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865 }
866
867 curr_iov++;
868 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700869 res = bytes_sent;
870exit:
871 release_sock(sk);
872 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873}
874
875/**
876 * auto_connect - complete connection setup to a remote port
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400877 * @tsk: tipc socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900879 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 * Returns 0 on success, errno otherwise
881 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400882static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100883{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400884 struct tipc_port *port = &tsk->port;
885 struct socket *sock = tsk->sk.sk_socket;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400886 struct tipc_portid peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100887
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400888 peer.ref = msg_origport(msg);
889 peer.node = msg_orignode(msg);
890
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400891 __tipc_port_connect(port->ref, port, &peer);
Ying Xue584d24b2012-11-29 18:51:19 -0500892
893 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
894 return -EINVAL;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400895 msg_set_importance(&port->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100896 sock->state = SS_CONNECTED;
897 return 0;
898}
899
900/**
901 * set_orig_addr - capture sender's address for received message
902 * @m: descriptor for message info
903 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900904 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905 * Note: Address is not captured if not requested by receiver.
906 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800907static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908{
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100909 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900911 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912 addr->family = AF_TIPC;
913 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000914 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100915 addr->addr.id.ref = msg_origport(msg);
916 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000917 addr->addr.name.domain = 0; /* could leave uninitialized */
918 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100919 m->msg_namelen = sizeof(struct sockaddr_tipc);
920 }
921}
922
923/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900924 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100925 * @m: descriptor for message info
926 * @msg: received message header
927 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900928 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100929 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900930 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931 * Returns 0 if successful, otherwise errno
932 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800933static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400934 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935{
936 u32 anc_data[3];
937 u32 err;
938 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700939 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940 int res;
941
942 if (likely(m->msg_controllen == 0))
943 return 0;
944
945 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946 err = msg ? msg_errcode(msg) : 0;
947 if (unlikely(err)) {
948 anc_data[0] = err;
949 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000950 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
951 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100952 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000953 if (anc_data[1]) {
954 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
955 msg_data(msg));
956 if (res)
957 return res;
958 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959 }
960
961 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100962 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
963 switch (dest_type) {
964 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700965 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100966 anc_data[0] = msg_nametype(msg);
967 anc_data[1] = msg_namelower(msg);
968 anc_data[2] = msg_namelower(msg);
969 break;
970 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700971 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100972 anc_data[0] = msg_nametype(msg);
973 anc_data[1] = msg_namelower(msg);
974 anc_data[2] = msg_nameupper(msg);
975 break;
976 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700977 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100978 anc_data[0] = tport->conn_type;
979 anc_data[1] = tport->conn_instance;
980 anc_data[2] = tport->conn_instance;
981 break;
982 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700983 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984 }
Allan Stephens2db99832010-12-31 18:59:33 +0000985 if (has_name) {
986 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
987 if (res)
988 return res;
989 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990
991 return 0;
992}
993
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -0400994static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +0800995{
996 struct sock *sk = sock->sk;
997 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -0400998 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +0800999 int err;
1000
1001 for (;;) {
1002 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001003 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001004 if (sock->state == SS_DISCONNECTING) {
1005 err = -ENOTCONN;
1006 break;
1007 }
1008 release_sock(sk);
1009 timeo = schedule_timeout(timeo);
1010 lock_sock(sk);
1011 }
1012 err = 0;
1013 if (!skb_queue_empty(&sk->sk_receive_queue))
1014 break;
1015 err = sock_intr_errno(timeo);
1016 if (signal_pending(current))
1017 break;
1018 err = -EAGAIN;
1019 if (!timeo)
1020 break;
1021 }
1022 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001023 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001024 return err;
1025}
1026
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001027/**
Ying Xue247f0f32014-02-18 16:06:46 +08001028 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 * @iocb: (unused)
1030 * @m: descriptor for message info
1031 * @buf_len: total size of user buffer area
1032 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001033 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1035 * If the complete message doesn't fit in user area, truncate it.
1036 *
1037 * Returns size of returned message data, errno otherwise
1038 */
Ying Xue247f0f32014-02-18 16:06:46 +08001039static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1040 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001042 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001043 struct tipc_sock *tsk = tipc_sk(sk);
1044 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045 struct sk_buff *buf;
1046 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001047 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 unsigned int sz;
1049 u32 err;
1050 int res;
1051
Allan Stephens0c3141e2008-04-15 00:22:02 -07001052 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001053 if (unlikely(!buf_len))
1054 return -EINVAL;
1055
Allan Stephens0c3141e2008-04-15 00:22:02 -07001056 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001057
Allan Stephens0c3141e2008-04-15 00:22:02 -07001058 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059 res = -ENOTCONN;
1060 goto exit;
1061 }
1062
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001063 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001064restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065
Allan Stephens0c3141e2008-04-15 00:22:02 -07001066 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001067 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001068 if (res)
1069 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001070
1071 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001072 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 msg = buf_msg(buf);
1074 sz = msg_data_sz(msg);
1075 err = msg_errcode(msg);
1076
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001079 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001080 goto restart;
1081 }
1082
1083 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084 set_orig_addr(m, msg);
1085
1086 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001087 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001088 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089 goto exit;
1090
1091 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092 if (!err) {
1093 if (unlikely(buf_len < sz)) {
1094 sz = buf_len;
1095 m->msg_flags |= MSG_TRUNC;
1096 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001097 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1098 m->msg_iov, sz);
1099 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001100 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101 res = sz;
1102 } else {
1103 if ((sock->state == SS_READY) ||
1104 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1105 res = 0;
1106 else
1107 res = -ECONNRESET;
1108 }
1109
1110 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001111 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001112 if ((sock->state != SS_READY) &&
Jon Paul Maloy6163a192014-05-14 05:39:08 -04001113 (++port->conn_unacked >= TIPC_CONNACK_INTV))
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001114 tipc_acknowledge(port->ref, port->conn_unacked);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001115 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001116 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001118 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001119 return res;
1120}
1121
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001122/**
Ying Xue247f0f32014-02-18 16:06:46 +08001123 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124 * @iocb: (unused)
1125 * @m: descriptor for message info
1126 * @buf_len: total size of user buffer area
1127 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001128 *
1129 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130 * will optionally wait for more; never truncates data.
1131 *
1132 * Returns size of returned message data, errno otherwise
1133 */
Ying Xue247f0f32014-02-18 16:06:46 +08001134static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1135 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001137 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001138 struct tipc_sock *tsk = tipc_sk(sk);
1139 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140 struct sk_buff *buf;
1141 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001142 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001144 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001147 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148
1149 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150 if (unlikely(!buf_len))
1151 return -EINVAL;
1152
Allan Stephens0c3141e2008-04-15 00:22:02 -07001153 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001155 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156 res = -ENOTCONN;
1157 goto exit;
1158 }
1159
Florian Westphal3720d402010-08-17 11:00:04 +00001160 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001161 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001162
Allan Stephens0c3141e2008-04-15 00:22:02 -07001163restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001164 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001165 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001166 if (res)
1167 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001168
1169 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001170 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171 msg = buf_msg(buf);
1172 sz = msg_data_sz(msg);
1173 err = msg_errcode(msg);
1174
1175 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001177 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178 goto restart;
1179 }
1180
1181 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 if (sz_copied == 0) {
1183 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001184 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001185 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 goto exit;
1187 }
1188
1189 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001191 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192
Allan Stephens0232fd02011-02-21 09:45:40 -05001193 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 needed = (buf_len - sz_copied);
1195 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001196
1197 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1198 m->msg_iov, sz_to_copy);
1199 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001201
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202 sz_copied += sz_to_copy;
1203
1204 if (sz_to_copy < sz) {
1205 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001206 TIPC_SKB_CB(buf)->handle =
1207 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 goto exit;
1209 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001210 } else {
1211 if (sz_copied != 0)
1212 goto exit; /* can't add error msg to valid data */
1213
1214 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1215 res = 0;
1216 else
1217 res = -ECONNRESET;
1218 }
1219
1220 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001221 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy6163a192014-05-14 05:39:08 -04001222 if (unlikely(++port->conn_unacked >= TIPC_CONNACK_INTV))
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001223 tipc_acknowledge(port->ref, port->conn_unacked);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001224 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001225 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226
1227 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001228 if ((sz_copied < buf_len) && /* didn't get all requested data */
1229 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001230 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001231 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1232 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233 goto restart;
1234
1235exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001236 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001237 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238}
1239
1240/**
Ying Xuef288bef2012-08-21 11:16:57 +08001241 * tipc_write_space - wake up thread if port congestion is released
1242 * @sk: socket
1243 */
1244static void tipc_write_space(struct sock *sk)
1245{
1246 struct socket_wq *wq;
1247
1248 rcu_read_lock();
1249 wq = rcu_dereference(sk->sk_wq);
1250 if (wq_has_sleeper(wq))
1251 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1252 POLLWRNORM | POLLWRBAND);
1253 rcu_read_unlock();
1254}
1255
1256/**
1257 * tipc_data_ready - wake up threads to indicate messages have been received
1258 * @sk: socket
1259 * @len: the length of messages
1260 */
David S. Miller676d2362014-04-11 16:15:36 -04001261static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001262{
1263 struct socket_wq *wq;
1264
1265 rcu_read_lock();
1266 wq = rcu_dereference(sk->sk_wq);
1267 if (wq_has_sleeper(wq))
1268 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1269 POLLRDNORM | POLLRDBAND);
1270 rcu_read_unlock();
1271}
1272
1273/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001274 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001275 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001276 * @msg: message
1277 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001278 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001279 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001280static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001281{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001282 struct sock *sk = &tsk->sk;
1283 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001284 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001285 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001286
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001287 int retval = -TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001288 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001289
1290 if (msg_mcast(msg))
1291 return retval;
1292
1293 switch ((int)sock->state) {
1294 case SS_CONNECTED:
1295 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001296 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001297 if (unlikely(msg_errcode(msg))) {
1298 sock->state = SS_DISCONNECTING;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001299 __tipc_port_disconnect(port);
Ying Xue7e6c1312012-11-29 18:39:14 -05001300 }
1301 retval = TIPC_OK;
1302 }
1303 break;
1304 case SS_CONNECTING:
1305 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001306 if (unlikely(msg_errcode(msg))) {
1307 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001308 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001309 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001310 break;
1311 }
1312
1313 if (unlikely(!msg_connected(msg)))
1314 break;
1315
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001316 res = auto_connect(tsk, msg);
Ying Xue584d24b2012-11-29 18:51:19 -05001317 if (res) {
1318 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001319 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001320 retval = TIPC_OK;
1321 break;
1322 }
1323
1324 /* If an incoming message is an 'ACK-', it should be
1325 * discarded here because it doesn't contain useful
1326 * data. In addition, we should try to wake up
1327 * connect() routine if sleeping.
1328 */
1329 if (msg_data_sz(msg) == 0) {
1330 kfree_skb(*buf);
1331 *buf = NULL;
1332 if (waitqueue_active(sk_sleep(sk)))
1333 wake_up_interruptible(sk_sleep(sk));
1334 }
1335 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001336 break;
1337 case SS_LISTENING:
1338 case SS_UNCONNECTED:
1339 /* Accept only SYN message */
1340 if (!msg_connected(msg) && !(msg_errcode(msg)))
1341 retval = TIPC_OK;
1342 break;
1343 case SS_DISCONNECTING:
1344 break;
1345 default:
1346 pr_err("Unknown socket state %u\n", sock->state);
1347 }
1348 return retval;
1349}
1350
1351/**
Ying Xueaba79f32013-01-20 23:30:09 +01001352 * rcvbuf_limit - get proper overload limit of socket receive queue
1353 * @sk: socket
1354 * @buf: message
1355 *
1356 * For all connection oriented messages, irrespective of importance,
1357 * the default overload value (i.e. 67MB) is set as limit.
1358 *
1359 * For all connectionless messages, by default new queue limits are
1360 * as belows:
1361 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001362 * TIPC_LOW_IMPORTANCE (4 MB)
1363 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1364 * TIPC_HIGH_IMPORTANCE (16 MB)
1365 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001366 *
1367 * Returns overload limit according to corresponding message importance
1368 */
1369static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1370{
1371 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001372
1373 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001374 return sysctl_tipc_rmem[2];
1375
1376 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1377 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001378}
1379
1380/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001381 * filter_rcv - validate incoming message
1382 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001384 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001385 * Enqueues message on receive queue if acceptable; optionally handles
1386 * disconnect indication for a connected socket.
1387 *
1388 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001389 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001390 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
1391 * to be rejected.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001393static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001395 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001396 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001397 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001398 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001399 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001400
Per Lidenb97bf3f2006-01-02 19:04:38 +01001401 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001402 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001403 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001404
Per Lidenb97bf3f2006-01-02 19:04:38 +01001405 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001406 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001407 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001408 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001409 rc = filter_connect(tsk, &buf);
1410 if (rc != TIPC_OK || buf == NULL)
1411 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001412 }
1413
1414 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001415 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001416 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417
Ying Xueaba79f32013-01-20 23:30:09 +01001418 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001419 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001420 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001421 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001422
David S. Miller676d2362014-04-11 16:15:36 -04001423 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424 return TIPC_OK;
1425}
1426
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001427/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001428 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001429 * @sk: socket
1430 * @buf: message
1431 *
1432 * Caller must hold socket lock, but not port lock.
1433 *
1434 * Returns 0
1435 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001436static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001437{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001438 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001439 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001440 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001441 uint truesize = buf->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001442
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001443 rc = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001444
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001445 if (unlikely(rc && tipc_msg_reverse(buf, &onode, -rc)))
1446 tipc_link_xmit2(buf, onode, 0);
1447 else if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001448 atomic_add(truesize, &tsk->dupl_rcvcnt);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001449
Allan Stephens0c3141e2008-04-15 00:22:02 -07001450 return 0;
1451}
1452
1453/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001454 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001455 * @buf: buffer containing arriving message
1456 * Consumes buffer
1457 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001458 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001459int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001460{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001461 struct tipc_sock *tsk;
1462 struct tipc_port *port;
1463 struct sock *sk;
1464 u32 dport = msg_destport(buf_msg(buf));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001465 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001466 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001467 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001468
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001469 /* Validate destination and message */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001470 port = tipc_port_lock(dport);
1471 if (unlikely(!port)) {
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001472 rc = tipc_msg_eval(buf, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001473 goto exit;
1474 }
1475
1476 tsk = tipc_port_to_sock(port);
1477 sk = &tsk->sk;
1478
1479 /* Queue message */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001480 bh_lock_sock(sk);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001481
Allan Stephens0c3141e2008-04-15 00:22:02 -07001482 if (!sock_owned_by_user(sk)) {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001483 rc = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001484 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001485 if (sk->sk_backlog.len == 0)
1486 atomic_set(&tsk->dupl_rcvcnt, 0);
1487 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1488 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001489 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001490 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001491 bh_unlock_sock(sk);
1492 tipc_port_unlock(port);
1493
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001494 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001495 return 0;
1496exit:
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001497 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001498 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001499
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001500 tipc_link_xmit2(buf, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001501 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001502}
1503
Ying Xue78eb3a52014-01-17 09:50:03 +08001504static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1505{
1506 struct sock *sk = sock->sk;
1507 DEFINE_WAIT(wait);
1508 int done;
1509
1510 do {
1511 int err = sock_error(sk);
1512 if (err)
1513 return err;
1514 if (!*timeo_p)
1515 return -ETIMEDOUT;
1516 if (signal_pending(current))
1517 return sock_intr_errno(*timeo_p);
1518
1519 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1520 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1521 finish_wait(sk_sleep(sk), &wait);
1522 } while (!done);
1523 return 0;
1524}
1525
Per Lidenb97bf3f2006-01-02 19:04:38 +01001526/**
Ying Xue247f0f32014-02-18 16:06:46 +08001527 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001528 * @sock: socket structure
1529 * @dest: socket address for destination port
1530 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001531 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001532 *
1533 * Returns 0 on success, errno otherwise
1534 */
Ying Xue247f0f32014-02-18 16:06:46 +08001535static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1536 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001537{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001538 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001539 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1540 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001541 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1542 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001543 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001544
Allan Stephens0c3141e2008-04-15 00:22:02 -07001545 lock_sock(sk);
1546
Allan Stephensb89741a2008-04-15 00:20:37 -07001547 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001548 if (sock->state == SS_READY) {
1549 res = -EOPNOTSUPP;
1550 goto exit;
1551 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001552
Allan Stephensb89741a2008-04-15 00:20:37 -07001553 /*
1554 * Reject connection attempt using multicast address
1555 *
1556 * Note: send_msg() validates the rest of the address fields,
1557 * so there's no need to do it here
1558 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001559 if (dst->addrtype == TIPC_ADDR_MCAST) {
1560 res = -EINVAL;
1561 goto exit;
1562 }
1563
Ying Xue78eb3a52014-01-17 09:50:03 +08001564 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001565 switch (sock->state) {
1566 case SS_UNCONNECTED:
1567 /* Send a 'SYN-' to destination */
1568 m.msg_name = dest;
1569 m.msg_namelen = destlen;
1570
1571 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1572 * indicate send_msg() is never blocked.
1573 */
1574 if (!timeout)
1575 m.msg_flags = MSG_DONTWAIT;
1576
Ying Xue247f0f32014-02-18 16:06:46 +08001577 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001578 if ((res < 0) && (res != -EWOULDBLOCK))
1579 goto exit;
1580
1581 /* Just entered SS_CONNECTING state; the only
1582 * difference is that return value in non-blocking
1583 * case is EINPROGRESS, rather than EALREADY.
1584 */
1585 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001586 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001587 if (previous == SS_CONNECTING)
1588 res = -EALREADY;
1589 if (!timeout)
1590 goto exit;
1591 timeout = msecs_to_jiffies(timeout);
1592 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1593 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001594 break;
1595 case SS_CONNECTED:
1596 res = -EISCONN;
1597 break;
1598 default:
1599 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001600 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001601 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001602exit:
1603 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001604 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001605}
1606
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001607/**
Ying Xue247f0f32014-02-18 16:06:46 +08001608 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001609 * @sock: socket structure
1610 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001611 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001612 * Returns 0 on success, errno otherwise
1613 */
Ying Xue247f0f32014-02-18 16:06:46 +08001614static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001615{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001616 struct sock *sk = sock->sk;
1617 int res;
1618
1619 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001620
Ying Xue245f3d32011-07-06 06:01:13 -04001621 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001622 res = -EINVAL;
1623 else {
1624 sock->state = SS_LISTENING;
1625 res = 0;
1626 }
1627
1628 release_sock(sk);
1629 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630}
1631
Ying Xue6398e232014-01-17 09:50:04 +08001632static int tipc_wait_for_accept(struct socket *sock, long timeo)
1633{
1634 struct sock *sk = sock->sk;
1635 DEFINE_WAIT(wait);
1636 int err;
1637
1638 /* True wake-one mechanism for incoming connections: only
1639 * one process gets woken up, not the 'whole herd'.
1640 * Since we do not 'race & poll' for established sockets
1641 * anymore, the common case will execute the loop only once.
1642 */
1643 for (;;) {
1644 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1645 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001646 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001647 release_sock(sk);
1648 timeo = schedule_timeout(timeo);
1649 lock_sock(sk);
1650 }
1651 err = 0;
1652 if (!skb_queue_empty(&sk->sk_receive_queue))
1653 break;
1654 err = -EINVAL;
1655 if (sock->state != SS_LISTENING)
1656 break;
1657 err = sock_intr_errno(timeo);
1658 if (signal_pending(current))
1659 break;
1660 err = -EAGAIN;
1661 if (!timeo)
1662 break;
1663 }
1664 finish_wait(sk_sleep(sk), &wait);
1665 return err;
1666}
1667
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001668/**
Ying Xue247f0f32014-02-18 16:06:46 +08001669 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670 * @sock: listening socket
1671 * @newsock: new socket that is to be connected
1672 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001673 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674 * Returns 0 on success, errno otherwise
1675 */
Ying Xue247f0f32014-02-18 16:06:46 +08001676static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001677{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001678 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001680 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001681 struct tipc_msg *msg;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001682 struct tipc_portid peer;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001683 u32 new_ref;
Ying Xue6398e232014-01-17 09:50:04 +08001684 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001685 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001686
Allan Stephens0c3141e2008-04-15 00:22:02 -07001687 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001688
Allan Stephens0c3141e2008-04-15 00:22:02 -07001689 if (sock->state != SS_LISTENING) {
1690 res = -EINVAL;
1691 goto exit;
1692 }
Ying Xue6398e232014-01-17 09:50:04 +08001693 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1694 res = tipc_wait_for_accept(sock, timeo);
1695 if (res)
1696 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001697
1698 buf = skb_peek(&sk->sk_receive_queue);
1699
Ying Xuec5fa7b32013-06-17 10:54:39 -04001700 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001701 if (res)
1702 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001703
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001704 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001705 new_port = &tipc_sk(new_sk)->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001706 new_ref = new_port->ref;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001707 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001708
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001709 /* we lock on new_sk; but lockdep sees the lock on sk */
1710 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001711
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001712 /*
1713 * Reject any stray messages received by new socket
1714 * before the socket lock was taken (very, very unlikely)
1715 */
1716 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001717
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001718 /* Connect new socket to it's peer */
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001719 peer.ref = msg_origport(msg);
1720 peer.node = msg_orignode(msg);
1721 tipc_port_connect(new_ref, &peer);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001722 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001723
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001724 tipc_port_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001725 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001726 new_port->conn_type = msg_nametype(msg);
1727 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001728 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001729
1730 /*
1731 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1732 * Respond to 'SYN+' by queuing it on new socket.
1733 */
1734 if (!msg_data_sz(msg)) {
1735 struct msghdr m = {NULL,};
1736
1737 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001738 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001739 } else {
1740 __skb_dequeue(&sk->sk_receive_queue);
1741 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001742 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001743 }
1744 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001745exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001746 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001747 return res;
1748}
1749
1750/**
Ying Xue247f0f32014-02-18 16:06:46 +08001751 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001752 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001753 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001754 *
1755 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001756 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001757 * Returns 0 on success, errno otherwise
1758 */
Ying Xue247f0f32014-02-18 16:06:46 +08001759static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001760{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001761 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001762 struct tipc_sock *tsk = tipc_sk(sk);
1763 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001764 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001765 u32 peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001766 int res;
1767
Allan Stephense247a8f2008-03-06 15:05:38 -08001768 if (how != SHUT_RDWR)
1769 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001770
Allan Stephens0c3141e2008-04-15 00:22:02 -07001771 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001772
1773 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001774 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001775 case SS_CONNECTED:
1776
Per Lidenb97bf3f2006-01-02 19:04:38 +01001777restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001778 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001779 buf = __skb_dequeue(&sk->sk_receive_queue);
1780 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001781 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001782 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001783 goto restart;
1784 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001785 tipc_port_disconnect(port->ref);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001786 if (tipc_msg_reverse(buf, &peer, TIPC_CONN_SHUTDOWN))
1787 tipc_link_xmit2(buf, peer, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001788 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001789 tipc_port_shutdown(port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001790 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001791
1792 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793
1794 /* fall through */
1795
1796 case SS_DISCONNECTING:
1797
Ying Xue75031152012-10-29 09:38:15 -04001798 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001799 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001800
1801 /* Wake up anyone sleeping in poll */
1802 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001803 res = 0;
1804 break;
1805
1806 default:
1807 res = -ENOTCONN;
1808 }
1809
Allan Stephens0c3141e2008-04-15 00:22:02 -07001810 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001811 return res;
1812}
1813
1814/**
Ying Xue247f0f32014-02-18 16:06:46 +08001815 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816 * @sock: socket structure
1817 * @lvl: option level
1818 * @opt: option identifier
1819 * @ov: pointer to new option value
1820 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001821 *
1822 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001823 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001824 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001825 * Returns 0 on success, errno otherwise
1826 */
Ying Xue247f0f32014-02-18 16:06:46 +08001827static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1828 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001830 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001831 struct tipc_sock *tsk = tipc_sk(sk);
1832 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001833 u32 value;
1834 int res;
1835
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001836 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1837 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001838 if (lvl != SOL_TIPC)
1839 return -ENOPROTOOPT;
1840 if (ol < sizeof(value))
1841 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001842 res = get_user(value, (u32 __user *)ov);
1843 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001844 return res;
1845
Allan Stephens0c3141e2008-04-15 00:22:02 -07001846 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001847
Per Lidenb97bf3f2006-01-02 19:04:38 +01001848 switch (opt) {
1849 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001850 tipc_port_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001851 break;
1852 case TIPC_SRC_DROPPABLE:
1853 if (sock->type != SOCK_STREAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001854 tipc_port_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001855 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001856 res = -ENOPROTOOPT;
1857 break;
1858 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001859 tipc_port_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001860 break;
1861 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001862 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001863 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001864 break;
1865 default:
1866 res = -EINVAL;
1867 }
1868
Allan Stephens0c3141e2008-04-15 00:22:02 -07001869 release_sock(sk);
1870
Per Lidenb97bf3f2006-01-02 19:04:38 +01001871 return res;
1872}
1873
1874/**
Ying Xue247f0f32014-02-18 16:06:46 +08001875 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001876 * @sock: socket structure
1877 * @lvl: option level
1878 * @opt: option identifier
1879 * @ov: receptacle for option value
1880 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001881 *
1882 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001883 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001884 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001885 * Returns 0 on success, errno otherwise
1886 */
Ying Xue247f0f32014-02-18 16:06:46 +08001887static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1888 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001889{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001890 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001891 struct tipc_sock *tsk = tipc_sk(sk);
1892 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001893 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001894 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001895 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001896
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001897 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1898 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001899 if (lvl != SOL_TIPC)
1900 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001901 res = get_user(len, ol);
1902 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001903 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001904
Allan Stephens0c3141e2008-04-15 00:22:02 -07001905 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001906
1907 switch (opt) {
1908 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001909 value = tipc_port_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001910 break;
1911 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001912 value = tipc_port_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001913 break;
1914 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001915 value = tipc_port_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001916 break;
1917 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001918 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001919 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001920 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001921 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001922 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001923 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001924 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001925 value = skb_queue_len(&sk->sk_receive_queue);
1926 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001927 default:
1928 res = -EINVAL;
1929 }
1930
Allan Stephens0c3141e2008-04-15 00:22:02 -07001931 release_sock(sk);
1932
Paul Gortmaker25860c32010-12-31 18:59:31 +00001933 if (res)
1934 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001935
Paul Gortmaker25860c32010-12-31 18:59:31 +00001936 if (len < sizeof(value))
1937 return -EINVAL;
1938
1939 if (copy_to_user(ov, &value, sizeof(value)))
1940 return -EFAULT;
1941
1942 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001943}
1944
Erik Hugne78acb1f2014-04-24 16:26:47 +02001945int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
1946{
1947 struct tipc_sioc_ln_req lnr;
1948 void __user *argp = (void __user *)arg;
1949
1950 switch (cmd) {
1951 case SIOCGETLINKNAME:
1952 if (copy_from_user(&lnr, argp, sizeof(lnr)))
1953 return -EFAULT;
1954 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
1955 lnr.linkname, TIPC_MAX_LINK_NAME)) {
1956 if (copy_to_user(argp, &lnr, sizeof(lnr)))
1957 return -EFAULT;
1958 return 0;
1959 }
1960 return -EADDRNOTAVAIL;
1961 break;
1962 default:
1963 return -ENOIOCTLCMD;
1964 }
1965}
1966
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001967/* Protocol switches for the various types of TIPC sockets */
1968
Florian Westphalbca65ea2008-02-07 18:18:01 -08001969static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001970 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001971 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001972 .release = tipc_release,
1973 .bind = tipc_bind,
1974 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001975 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001976 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08001977 .getname = tipc_getname,
1978 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02001979 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001980 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08001981 .shutdown = tipc_shutdown,
1982 .setsockopt = tipc_setsockopt,
1983 .getsockopt = tipc_getsockopt,
1984 .sendmsg = tipc_sendmsg,
1985 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001986 .mmap = sock_no_mmap,
1987 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001988};
1989
Florian Westphalbca65ea2008-02-07 18:18:01 -08001990static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001991 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001992 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001993 .release = tipc_release,
1994 .bind = tipc_bind,
1995 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001996 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08001997 .accept = tipc_accept,
1998 .getname = tipc_getname,
1999 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002000 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002001 .listen = tipc_listen,
2002 .shutdown = tipc_shutdown,
2003 .setsockopt = tipc_setsockopt,
2004 .getsockopt = tipc_getsockopt,
2005 .sendmsg = tipc_send_packet,
2006 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002007 .mmap = sock_no_mmap,
2008 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002009};
2010
Florian Westphalbca65ea2008-02-07 18:18:01 -08002011static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002012 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002013 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002014 .release = tipc_release,
2015 .bind = tipc_bind,
2016 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002017 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002018 .accept = tipc_accept,
2019 .getname = tipc_getname,
2020 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002021 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002022 .listen = tipc_listen,
2023 .shutdown = tipc_shutdown,
2024 .setsockopt = tipc_setsockopt,
2025 .getsockopt = tipc_getsockopt,
2026 .sendmsg = tipc_send_stream,
2027 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002028 .mmap = sock_no_mmap,
2029 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002030};
2031
Florian Westphalbca65ea2008-02-07 18:18:01 -08002032static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002033 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002034 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002035 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002036};
2037
2038static struct proto tipc_proto = {
2039 .name = "TIPC",
2040 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002041 .obj_size = sizeof(struct tipc_sock),
2042 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002043};
2044
Ying Xuec5fa7b32013-06-17 10:54:39 -04002045static struct proto tipc_proto_kern = {
2046 .name = "TIPC",
2047 .obj_size = sizeof(struct tipc_sock),
2048 .sysctl_rmem = sysctl_tipc_rmem
2049};
2050
Per Lidenb97bf3f2006-01-02 19:04:38 +01002051/**
Per Liden4323add2006-01-18 00:38:21 +01002052 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002053 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002054 * Returns 0 on success, errno otherwise
2055 */
Per Liden4323add2006-01-18 00:38:21 +01002056int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002057{
2058 int res;
2059
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002060 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002061 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002062 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002063 goto out;
2064 }
2065
2066 res = sock_register(&tipc_family_ops);
2067 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002068 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002069 proto_unregister(&tipc_proto);
2070 goto out;
2071 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002072 out:
2073 return res;
2074}
2075
2076/**
Per Liden4323add2006-01-18 00:38:21 +01002077 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002078 */
Per Liden4323add2006-01-18 00:38:21 +01002079void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002080{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002081 sock_unregister(tipc_family_ops.family);
2082 proto_unregister(&tipc_proto);
2083}