blob: 5690751cbfa52aeff45a123e91dd042b7e137a9b [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"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050039#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020040#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050041#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040042#include <linux/export.h>
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -050043#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040044
Per Lidenb97bf3f2006-01-02 19:04:38 +010045#define SS_LISTENING -1 /* socket is listening */
46#define SS_READY -2 /* socket is connectionless */
47
Allan Stephens3654ea02008-04-13 21:35:11 -070048#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010049
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -040050static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -040051static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +080052static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080053static int tipc_release(struct socket *sock);
54static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Per Lidenb97bf3f2006-01-02 19:04:38 +010055
Florian Westphalbca65ea2008-02-07 18:18:01 -080056static const struct proto_ops packet_ops;
57static const struct proto_ops stream_ops;
58static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010059
60static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040061static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010062
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090063/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070064 * Revised TIPC socket locking policy:
65 *
66 * Most socket operations take the standard socket lock when they start
67 * and hold it until they finish (or until they need to sleep). Acquiring
68 * this lock grants the owner exclusive access to the fields of the socket
69 * data structures, with the exception of the backlog queue. A few socket
70 * operations can be done without taking the socket lock because they only
71 * read socket information that never changes during the life of the socket.
72 *
73 * Socket operations may acquire the lock for the associated TIPC port if they
74 * need to perform an operation on the port. If any routine needs to acquire
75 * both the socket lock and the port lock it must take the socket lock first
76 * to avoid the risk of deadlock.
77 *
78 * The dispatcher handling incoming messages cannot grab the socket lock in
79 * the standard fashion, since invoked it runs at the BH level and cannot block.
80 * Instead, it checks to see if the socket lock is currently owned by someone,
81 * and either handles the message itself or adds it to the socket's backlog
82 * queue; in the latter case the queued message is processed once the process
83 * owning the socket lock releases it.
84 *
85 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
86 * the problem of a blocked socket operation preventing any other operations
87 * from occurring. However, applications must be careful if they have
88 * multiple threads trying to send (or receive) on the same socket, as these
89 * operations might interfere with each other. For example, doing a connect
90 * and a receive at the same time might allow the receive to consume the
91 * ACK message meant for the connect. While additional work could be done
92 * to try and overcome this, it doesn't seem to be worthwhile at the present.
93 *
94 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
95 * that another operation that must be performed in a non-blocking manner is
96 * not delayed for very long because the lock has already been taken.
97 *
98 * NOTE: This code assumes that certain fields of a port/socket pair are
99 * constant over its lifetime; such fields can be examined without taking
100 * the socket lock and/or port lock, and do not need to be re-read even
101 * after resuming processing after waiting. These fields include:
102 * - socket type
103 * - pointer to socket sk structure (aka tipc_sock structure)
104 * - pointer to port structure
105 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100106 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400108#include "socket.h"
109
Per Lidenb97bf3f2006-01-02 19:04:38 +0100110/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700111 * advance_rx_queue - discard first buffer in socket receive queue
112 *
113 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700115static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400117 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118}
119
120/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700121 * reject_rx_queue - reject all buffers in socket receive queue
122 *
123 * Caller must hold socket lock
124 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700125static void reject_rx_queue(struct sock *sk)
126{
127 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500128 u32 dnode;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700129
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500130 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
131 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
132 tipc_link_xmit2(buf, dnode, 0);
133 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700134}
135
136/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400137 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700138 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139 * @sock: pre-allocated socket structure
140 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800141 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900142 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700143 * This routine creates additional data structures used by the TIPC socket,
144 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145 *
146 * Returns 0 on success, errno otherwise
147 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400148static int tipc_sk_create(struct net *net, struct socket *sock,
149 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700151 const struct proto_ops *ops;
152 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400154 struct tipc_sock *tsk;
155 struct tipc_port *port;
156 u32 ref;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700157
158 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159 if (unlikely(protocol != 0))
160 return -EPROTONOSUPPORT;
161
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162 switch (sock->type) {
163 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700164 ops = &stream_ops;
165 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 break;
167 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700168 ops = &packet_ops;
169 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 break;
171 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700173 ops = &msg_ops;
174 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 break;
Allan Stephens49978652006-06-25 23:47:18 -0700176 default:
Allan Stephens49978652006-06-25 23:47:18 -0700177 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 }
179
Allan Stephens0c3141e2008-04-15 00:22:02 -0700180 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400181 if (!kern)
182 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
183 else
184 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
185
Allan Stephens0c3141e2008-04-15 00:22:02 -0700186 if (sk == NULL)
187 return -ENOMEM;
188
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400189 tsk = tipc_sk(sk);
190 port = &tsk->port;
191
192 ref = tipc_port_init(port, TIPC_LOW_IMPORTANCE);
193 if (!ref) {
194 pr_warn("Socket registration failed, ref. table exhausted\n");
Allan Stephens0c3141e2008-04-15 00:22:02 -0700195 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196 return -ENOMEM;
197 }
198
Allan Stephens0c3141e2008-04-15 00:22:02 -0700199 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700200 sock->ops = ops;
201 sock->state = state;
202
Per Lidenb97bf3f2006-01-02 19:04:38 +0100203 sock_init_data(sock, sk);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400204 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400205 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800206 sk->sk_data_ready = tipc_data_ready;
207 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400208 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
209 atomic_set(&tsk->dupl_rcvcnt, 0);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400210 tipc_port_unlock(port);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700211
Allan Stephens0c3141e2008-04-15 00:22:02 -0700212 if (sock->state == SS_READY) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400213 tipc_port_set_unreturnable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700214 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400215 tipc_port_set_unreliable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700216 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217 return 0;
218}
219
220/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400221 * tipc_sock_create_local - create TIPC socket from inside TIPC module
222 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
223 *
224 * We cannot use sock_creat_kern here because it bumps module user count.
225 * Since socket owner and creator is the same module we must make sure
226 * that module count remains zero for module local sockets, otherwise
227 * we cannot do rmmod.
228 *
229 * Returns 0 on success, errno otherwise
230 */
231int tipc_sock_create_local(int type, struct socket **res)
232{
233 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400234
235 rc = sock_create_lite(AF_TIPC, type, 0, res);
236 if (rc < 0) {
237 pr_err("Failed to create kernel socket\n");
238 return rc;
239 }
240 tipc_sk_create(&init_net, *res, 0, 1);
241
Ying Xuec5fa7b32013-06-17 10:54:39 -0400242 return 0;
243}
244
245/**
246 * tipc_sock_release_local - release socket created by tipc_sock_create_local
247 * @sock: the socket to be released.
248 *
249 * Module reference count is not incremented when such sockets are created,
250 * so we must keep it from being decremented when they are released.
251 */
252void tipc_sock_release_local(struct socket *sock)
253{
Ying Xue247f0f32014-02-18 16:06:46 +0800254 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400255 sock->ops = NULL;
256 sock_release(sock);
257}
258
259/**
260 * tipc_sock_accept_local - accept a connection on a socket created
261 * with tipc_sock_create_local. Use this function to avoid that
262 * module reference count is inadvertently incremented.
263 *
264 * @sock: the accepting socket
265 * @newsock: reference to the new socket to be created
266 * @flags: socket flags
267 */
268
269int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400270 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400271{
272 struct sock *sk = sock->sk;
273 int ret;
274
275 ret = sock_create_lite(sk->sk_family, sk->sk_type,
276 sk->sk_protocol, newsock);
277 if (ret < 0)
278 return ret;
279
Ying Xue247f0f32014-02-18 16:06:46 +0800280 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400281 if (ret < 0) {
282 sock_release(*newsock);
283 return ret;
284 }
285 (*newsock)->ops = sock->ops;
286 return ret;
287}
288
289/**
Ying Xue247f0f32014-02-18 16:06:46 +0800290 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 * @sock: socket to destroy
292 *
293 * This routine cleans up any messages that are still queued on the socket.
294 * For DGRAM and RDM socket types, all queued messages are rejected.
295 * For SEQPACKET and STREAM socket types, the first message is rejected
296 * and any others are discarded. (If the first message on a STREAM socket
297 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900298 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299 * NOTE: Rejected messages are not necessarily returned to the sender! They
300 * are returned or discarded according to the "destination droppable" setting
301 * specified for the message by the sender.
302 *
303 * Returns 0 on success, errno otherwise
304 */
Ying Xue247f0f32014-02-18 16:06:46 +0800305static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400308 struct tipc_sock *tsk;
309 struct tipc_port *port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500311 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312
Allan Stephens0c3141e2008-04-15 00:22:02 -0700313 /*
314 * Exit if socket isn't fully initialized (occurs when a failed accept()
315 * releases a pre-allocated child socket that was never used)
316 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700317 if (sk == NULL)
318 return 0;
319
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400320 tsk = tipc_sk(sk);
321 port = &tsk->port;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700322 lock_sock(sk);
323
324 /*
325 * Reject all unreceived messages, except on an active connection
326 * (which disconnects locally & sends a 'FIN+' to peer)
327 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100328 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700329 buf = __skb_dequeue(&sk->sk_receive_queue);
330 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331 break;
Ying Xue40682432013-10-18 07:23:16 +0200332 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400333 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700334 else {
335 if ((sock->state == SS_CONNECTING) ||
336 (sock->state == SS_CONNECTED)) {
337 sock->state = SS_DISCONNECTING;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400338 tipc_port_disconnect(port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700339 }
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500340 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
341 tipc_link_xmit2(buf, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700342 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 }
344
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400345 /* Destroy TIPC port; also disconnects an active connection and
346 * sends a 'FIN-' to peer.
Allan Stephens0c3141e2008-04-15 00:22:02 -0700347 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400348 tipc_port_destroy(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349
Allan Stephens0c3141e2008-04-15 00:22:02 -0700350 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100351 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352
Allan Stephens0c3141e2008-04-15 00:22:02 -0700353 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700354 sock->state = SS_DISCONNECTING;
355 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356
357 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700358 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200360 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361}
362
363/**
Ying Xue247f0f32014-02-18 16:06:46 +0800364 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365 * @sock: socket structure
366 * @uaddr: socket address describing name(s) and desired operation
367 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900368 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369 * Name and name sequence binding is indicated using a positive scope value;
370 * a negative scope value unbinds the specified name. Specifying no name
371 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900372 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700374 *
375 * NOTE: This routine doesn't need to take the socket lock since it doesn't
376 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377 */
Ying Xue247f0f32014-02-18 16:06:46 +0800378static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
379 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380{
Ying Xue84602762013-12-27 10:18:28 +0800381 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400383 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800384 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385
Ying Xue84602762013-12-27 10:18:28 +0800386 lock_sock(sk);
387 if (unlikely(!uaddr_len)) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400388 res = tipc_withdraw(&tsk->port, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800389 goto exit;
390 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900391
Ying Xue84602762013-12-27 10:18:28 +0800392 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
393 res = -EINVAL;
394 goto exit;
395 }
396 if (addr->family != AF_TIPC) {
397 res = -EAFNOSUPPORT;
398 goto exit;
399 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 if (addr->addrtype == TIPC_ADDR_NAME)
402 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800403 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
404 res = -EAFNOSUPPORT;
405 goto exit;
406 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900407
Ying Xue13a2e892013-06-17 10:54:40 -0400408 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400409 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800410 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
411 res = -EACCES;
412 goto exit;
413 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400414
Ying Xue84602762013-12-27 10:18:28 +0800415 res = (addr->scope > 0) ?
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400416 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
417 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800418exit:
419 release_sock(sk);
420 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421}
422
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900423/**
Ying Xue247f0f32014-02-18 16:06:46 +0800424 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425 * @sock: socket structure
426 * @uaddr: area for returned socket address
427 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700428 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900429 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700431 *
Allan Stephens2da59912008-07-14 22:43:32 -0700432 * NOTE: This routine doesn't need to take the socket lock since it only
433 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000434 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435 */
Ying Xue247f0f32014-02-18 16:06:46 +0800436static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
437 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400440 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000442 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700443 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700444 if ((sock->state != SS_CONNECTED) &&
445 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
446 return -ENOTCONN;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400447 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
448 addr->addr.id.node = tipc_port_peernode(&tsk->port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700449 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400450 addr->addr.id.ref = tsk->port.ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000451 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700452 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453
454 *uaddr_len = sizeof(*addr);
455 addr->addrtype = TIPC_ADDR_ID;
456 addr->family = AF_TIPC;
457 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458 addr->addr.name.domain = 0;
459
Allan Stephens0c3141e2008-04-15 00:22:02 -0700460 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461}
462
463/**
Ying Xue247f0f32014-02-18 16:06:46 +0800464 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465 * @file: file structure associated with the socket
466 * @sock: socket for which to calculate the poll bits
467 * @wait: ???
468 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700469 * Returns pollmask value
470 *
471 * COMMENTARY:
472 * It appears that the usual socket locking mechanisms are not useful here
473 * since the pollmask info is potentially out-of-date the moment this routine
474 * exits. TCP and other protocols seem to rely on higher level poll routines
475 * to handle any preventable race conditions, so TIPC will do the same ...
476 *
477 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700478 *
Allan Stephensf662c072010-08-17 11:00:06 +0000479 * socket state flags set
480 * ------------ ---------
481 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200482 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000483 *
484 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
485 * no write flags
486 *
487 * connected POLLIN/POLLRDNORM if data in rx queue
488 * POLLOUT if port is not congested
489 *
490 * disconnecting POLLIN/POLLRDNORM/POLLHUP
491 * no write flags
492 *
493 * listening POLLIN if SYN in rx queue
494 * no write flags
495 *
496 * ready POLLIN/POLLRDNORM if data in rx queue
497 * [connectionless] POLLOUT (since port cannot be congested)
498 *
499 * IMPORTANT: The fact that a read or write operation is indicated does NOT
500 * imply that the operation will succeed, merely that it should be performed
501 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502 */
Ying Xue247f0f32014-02-18 16:06:46 +0800503static unsigned int tipc_poll(struct file *file, struct socket *sock,
504 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505{
Allan Stephens9b674e82008-03-26 16:48:21 -0700506 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400507 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000508 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700509
Ying Xuef288bef2012-08-21 11:16:57 +0800510 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700511
Allan Stephensf662c072010-08-17 11:00:06 +0000512 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200513 case SS_UNCONNECTED:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400514 if (!tsk->port.congested)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200515 mask |= POLLOUT;
516 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000517 case SS_READY:
518 case SS_CONNECTED:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400519 if (!tsk->port.congested)
Allan Stephensf662c072010-08-17 11:00:06 +0000520 mask |= POLLOUT;
521 /* fall thru' */
522 case SS_CONNECTING:
523 case SS_LISTENING:
524 if (!skb_queue_empty(&sk->sk_receive_queue))
525 mask |= (POLLIN | POLLRDNORM);
526 break;
527 case SS_DISCONNECTING:
528 mask = (POLLIN | POLLRDNORM | POLLHUP);
529 break;
530 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700531
532 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533}
534
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900535/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536 * dest_name_check - verify user is permitted to send to specified port name
537 * @dest: destination address
538 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900539 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 * Prevents restricted configuration commands from being issued by
541 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900542 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 * Returns 0 if permission is granted, otherwise errno
544 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800545static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546{
547 struct tipc_cfg_msg_hdr hdr;
548
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500549 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
550 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900551 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
552 return 0;
553 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
554 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900555 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
556 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557
Allan Stephens3f8dd942011-01-18 13:09:29 -0500558 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
559 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900560 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700562 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900564
Per Lidenb97bf3f2006-01-02 19:04:38 +0100565 return 0;
566}
567
Ying Xue3f405042014-01-17 09:50:05 +0800568static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
569{
570 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400571 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800572 DEFINE_WAIT(wait);
573 int done;
574
575 do {
576 int err = sock_error(sk);
577 if (err)
578 return err;
579 if (sock->state == SS_DISCONNECTING)
580 return -EPIPE;
581 if (!*timeo_p)
582 return -EAGAIN;
583 if (signal_pending(current))
584 return sock_intr_errno(*timeo_p);
585
586 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400587 done = sk_wait_event(sk, timeo_p, !tsk->port.congested);
Ying Xue3f405042014-01-17 09:50:05 +0800588 finish_wait(sk_sleep(sk), &wait);
589 } while (!done);
590 return 0;
591}
592
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500593/**
594 * tipc_sendmcast - send multicast message
595 * @sock: socket structure
596 * @seq: destination address
597 * @iov: message data to send
598 * @dsz: total length of message data
599 * @timeo: timeout to wait for wakeup
600 *
601 * Called from function tipc_sendmsg(), which has done all sanity checks
602 * Returns the number of bytes sent on success, or errno
603 */
604static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
605 struct iovec *iov, size_t dsz, long timeo)
606{
607 struct sock *sk = sock->sk;
608 struct tipc_sock *tsk = tipc_sk(sk);
609 int rc;
610
611 do {
612 if (sock->state != SS_READY) {
613 rc = -EOPNOTSUPP;
614 break;
615 }
616 rc = tipc_port_mcast_xmit(&tsk->port, seq, iov, dsz);
617 if (likely(rc >= 0)) {
618 if (sock->state != SS_READY)
619 sock->state = SS_CONNECTING;
620 break;
621 }
622 if (rc != -ELINKCONG)
623 break;
624 rc = tipc_wait_for_sndmsg(sock, &timeo);
625 } while (!rc);
626
627 return rc;
628}
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400629
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630/**
Ying Xue247f0f32014-02-18 16:06:46 +0800631 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700632 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 * @sock: socket structure
634 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500635 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900636 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100637 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900638 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100639 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
640 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900641 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 * Returns the number of bytes sent on success, or errno otherwise
643 */
Ying Xue247f0f32014-02-18 16:06:46 +0800644static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500645 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500647 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700648 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400649 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400650 struct tipc_port *port = &tsk->port;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500651 struct tipc_msg *mhdr = &port->phdr;
652 struct iovec *iov = m->msg_iov;
653 u32 dnode, dport;
654 struct sk_buff *buf;
655 struct tipc_name_seq *seq = &dest->addr.nameseq;
656 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800657 long timeo;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500658 int rc = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659
660 if (unlikely(!dest))
661 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500662
Allan Stephens51f9cc12006-06-25 23:49:06 -0700663 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
664 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500666
667 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400668 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669
Allan Stephens0c3141e2008-04-15 00:22:02 -0700670 if (iocb)
671 lock_sock(sk);
672
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500673 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700674 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500675 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700676 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700677 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700678 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500679 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700680 goto exit;
681 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400682 if (tsk->port.published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500683 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700684 goto exit;
685 }
686 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400687 tsk->port.conn_type = dest->addr.name.name.type;
688 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700689 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500691 rc = dest_name_check(dest, m);
692 if (rc)
693 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100694
Ying Xue3f405042014-01-17 09:50:05 +0800695 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500696
697 if (dest->addrtype == TIPC_ADDR_MCAST) {
698 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
699 goto exit;
700 } else if (dest->addrtype == TIPC_ADDR_NAME) {
701 u32 type = dest->addr.name.name.type;
702 u32 inst = dest->addr.name.name.instance;
703 u32 domain = dest->addr.name.domain;
704
705 dnode = domain;
706 msg_set_type(mhdr, TIPC_NAMED_MSG);
707 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
708 msg_set_nametype(mhdr, type);
709 msg_set_nameinst(mhdr, inst);
710 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
711 dport = tipc_nametbl_translate(type, inst, &dnode);
712 msg_set_destnode(mhdr, dnode);
713 msg_set_destport(mhdr, dport);
714 if (unlikely(!dport && !dnode)) {
715 rc = -EHOSTUNREACH;
716 goto exit;
717 }
718 } else if (dest->addrtype == TIPC_ADDR_ID) {
719 dnode = dest->addr.id.node;
720 msg_set_type(mhdr, TIPC_DIRECT_MSG);
721 msg_set_lookup_scope(mhdr, 0);
722 msg_set_destnode(mhdr, dnode);
723 msg_set_destport(mhdr, dest->addr.id.ref);
724 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
725 }
726
727new_mtu:
728 mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
729 rc = tipc_msg_build2(mhdr, iov, 0, dsz, mtu, &buf);
730 if (rc < 0)
731 goto exit;
732
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900733 do {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500734 rc = tipc_link_xmit2(buf, dnode, tsk->port.ref);
735 if (likely(rc >= 0)) {
736 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700737 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500738 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700739 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900740 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500741 if (rc == -EMSGSIZE)
742 goto new_mtu;
743
744 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700745 break;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500746
747 rc = tipc_wait_for_sndmsg(sock, &timeo);
748 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700749
750exit:
751 if (iocb)
752 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500753
754 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755}
756
Ying Xue391a6dd2014-01-17 09:50:06 +0800757static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
758{
759 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400760 struct tipc_sock *tsk = tipc_sk(sk);
761 struct tipc_port *port = &tsk->port;
Ying Xue391a6dd2014-01-17 09:50:06 +0800762 DEFINE_WAIT(wait);
763 int done;
764
765 do {
766 int err = sock_error(sk);
767 if (err)
768 return err;
769 if (sock->state == SS_DISCONNECTING)
770 return -EPIPE;
771 else if (sock->state != SS_CONNECTED)
772 return -ENOTCONN;
773 if (!*timeo_p)
774 return -EAGAIN;
775 if (signal_pending(current))
776 return sock_intr_errno(*timeo_p);
777
778 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
779 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400780 (!port->congested || !port->connected));
Ying Xue391a6dd2014-01-17 09:50:06 +0800781 finish_wait(sk_sleep(sk), &wait);
782 } while (!done);
783 return 0;
784}
785
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900786/**
Ying Xue247f0f32014-02-18 16:06:46 +0800787 * tipc_send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700788 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789 * @sock: socket structure
790 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700791 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900792 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900794 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100795 * Returns the number of bytes sent on success, or errno otherwise
796 */
Ying Xue247f0f32014-02-18 16:06:46 +0800797static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
798 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100799{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700800 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400801 struct tipc_sock *tsk = tipc_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100802 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue391a6dd2014-01-17 09:50:06 +0800803 int res = -EINVAL;
804 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805
806 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100807 if (unlikely(dest))
Ying Xue247f0f32014-02-18 16:06:46 +0800808 return tipc_sendmsg(iocb, sock, m, total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100809
Ying Xue97f8b872013-01-31 21:51:47 +0100810 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400811 return -EMSGSIZE;
812
Allan Stephens0c3141e2008-04-15 00:22:02 -0700813 if (iocb)
814 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815
Ying Xue391a6dd2014-01-17 09:50:06 +0800816 if (unlikely(sock->state != SS_CONNECTED)) {
817 if (sock->state == SS_DISCONNECTING)
818 res = -EPIPE;
819 else
820 res = -ENOTCONN;
821 goto exit;
822 }
Ying Xue1d835872011-07-06 05:53:15 -0400823
Ying Xue391a6dd2014-01-17 09:50:06 +0800824 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900825 do {
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400826 res = tipc_send(&tsk->port, m->msg_iov, total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000827 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700828 break;
Ying Xue391a6dd2014-01-17 09:50:06 +0800829 res = tipc_wait_for_sndpkt(sock, &timeo);
830 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700831 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900832 } while (1);
Ying Xue391a6dd2014-01-17 09:50:06 +0800833exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700834 if (iocb)
835 release_sock(sk);
836 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100837}
838
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900839/**
Ying Xue247f0f32014-02-18 16:06:46 +0800840 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100841 * @iocb: (unused)
842 * @sock: socket structure
843 * @m: data to send
844 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900845 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900847 *
848 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700849 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100850 */
Ying Xue247f0f32014-02-18 16:06:46 +0800851static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
852 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100853{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700854 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400855 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100856 struct msghdr my_msg;
857 struct iovec my_iov;
858 struct iovec *curr_iov;
859 int curr_iovlen;
860 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700861 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100862 int curr_left;
863 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700864 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900866
Allan Stephens0c3141e2008-04-15 00:22:02 -0700867 lock_sock(sk);
868
Allan Stephens05646c92007-06-10 17:25:24 -0700869 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900870 if (unlikely(sock->state != SS_CONNECTED)) {
wangweidong3b8401f2013-12-12 09:36:40 +0800871 if (sock->state == SS_UNCONNECTED)
Ying Xue247f0f32014-02-18 16:06:46 +0800872 res = tipc_send_packet(NULL, sock, m, total_len);
wangweidongb0555972013-12-27 10:09:39 +0800873 else
874 res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800875 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900876 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100877
Allan Stephens0c3141e2008-04-15 00:22:02 -0700878 if (unlikely(m->msg_name)) {
879 res = -EISCONN;
880 goto exit;
881 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700882
Ying Xue97f8b872013-01-31 21:51:47 +0100883 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400884 res = -EMSGSIZE;
885 goto exit;
886 }
887
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900888 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889 * Send each iovec entry using one or more messages
890 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900891 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892 * (i.e. one large iovec entry), but could be improved to pass sets
893 * of small iovec entries into send_packet().
894 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700895 curr_iov = m->msg_iov;
896 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897 my_msg.msg_iov = &my_iov;
898 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700899 my_msg.msg_flags = m->msg_flags;
900 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700901 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400903 hdr_size = msg_hdr_sz(&tsk->port.phdr);
Allan Stephens05646c92007-06-10 17:25:24 -0700904
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905 while (curr_iovlen--) {
906 curr_start = curr_iov->iov_base;
907 curr_left = curr_iov->iov_len;
908
909 while (curr_left) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400910 bytes_to_send = tsk->port.max_pkt - hdr_size;
Allan Stephens05646c92007-06-10 17:25:24 -0700911 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
912 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
913 if (curr_left < bytes_to_send)
914 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100915 my_iov.iov_base = curr_start;
916 my_iov.iov_len = bytes_to_send;
Ying Xue247f0f32014-02-18 16:06:46 +0800917 res = tipc_send_packet(NULL, sock, &my_msg,
918 bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000919 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700920 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700921 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700922 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700923 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924 curr_left -= bytes_to_send;
925 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700926 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927 }
928
929 curr_iov++;
930 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700931 res = bytes_sent;
932exit:
933 release_sock(sk);
934 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935}
936
937/**
938 * auto_connect - complete connection setup to a remote port
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400939 * @tsk: tipc socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900941 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942 * Returns 0 on success, errno otherwise
943 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400944static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100945{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400946 struct tipc_port *port = &tsk->port;
947 struct socket *sock = tsk->sk.sk_socket;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400948 struct tipc_portid peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100949
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400950 peer.ref = msg_origport(msg);
951 peer.node = msg_orignode(msg);
952
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400953 __tipc_port_connect(port->ref, port, &peer);
Ying Xue584d24b2012-11-29 18:51:19 -0500954
955 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
956 return -EINVAL;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400957 msg_set_importance(&port->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958 sock->state = SS_CONNECTED;
959 return 0;
960}
961
962/**
963 * set_orig_addr - capture sender's address for received message
964 * @m: descriptor for message info
965 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900966 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100967 * Note: Address is not captured if not requested by receiver.
968 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800969static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970{
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100971 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100972
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900973 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974 addr->family = AF_TIPC;
975 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000976 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100977 addr->addr.id.ref = msg_origport(msg);
978 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000979 addr->addr.name.domain = 0; /* could leave uninitialized */
980 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100981 m->msg_namelen = sizeof(struct sockaddr_tipc);
982 }
983}
984
985/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900986 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100987 * @m: descriptor for message info
988 * @msg: received message header
989 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900990 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900992 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993 * Returns 0 if successful, otherwise errno
994 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800995static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400996 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100997{
998 u32 anc_data[3];
999 u32 err;
1000 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001001 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 int res;
1003
1004 if (likely(m->msg_controllen == 0))
1005 return 0;
1006
1007 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008 err = msg ? msg_errcode(msg) : 0;
1009 if (unlikely(err)) {
1010 anc_data[0] = err;
1011 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001012 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1013 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001015 if (anc_data[1]) {
1016 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1017 msg_data(msg));
1018 if (res)
1019 return res;
1020 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001021 }
1022
1023 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1025 switch (dest_type) {
1026 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001027 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 anc_data[0] = msg_nametype(msg);
1029 anc_data[1] = msg_namelower(msg);
1030 anc_data[2] = msg_namelower(msg);
1031 break;
1032 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001033 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034 anc_data[0] = msg_nametype(msg);
1035 anc_data[1] = msg_namelower(msg);
1036 anc_data[2] = msg_nameupper(msg);
1037 break;
1038 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001039 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 anc_data[0] = tport->conn_type;
1041 anc_data[1] = tport->conn_instance;
1042 anc_data[2] = tport->conn_instance;
1043 break;
1044 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001045 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046 }
Allan Stephens2db99832010-12-31 18:59:33 +00001047 if (has_name) {
1048 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1049 if (res)
1050 return res;
1051 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052
1053 return 0;
1054}
1055
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001056static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001057{
1058 struct sock *sk = sock->sk;
1059 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001060 long timeo = *timeop;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001061 int err;
1062
1063 for (;;) {
1064 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001065 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001066 if (sock->state == SS_DISCONNECTING) {
1067 err = -ENOTCONN;
1068 break;
1069 }
1070 release_sock(sk);
1071 timeo = schedule_timeout(timeo);
1072 lock_sock(sk);
1073 }
1074 err = 0;
1075 if (!skb_queue_empty(&sk->sk_receive_queue))
1076 break;
1077 err = sock_intr_errno(timeo);
1078 if (signal_pending(current))
1079 break;
1080 err = -EAGAIN;
1081 if (!timeo)
1082 break;
1083 }
1084 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001085 *timeop = timeo;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001086 return err;
1087}
1088
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001089/**
Ying Xue247f0f32014-02-18 16:06:46 +08001090 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001091 * @iocb: (unused)
1092 * @m: descriptor for message info
1093 * @buf_len: total size of user buffer area
1094 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001095 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1097 * If the complete message doesn't fit in user area, truncate it.
1098 *
1099 * Returns size of returned message data, errno otherwise
1100 */
Ying Xue247f0f32014-02-18 16:06:46 +08001101static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1102 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001104 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001105 struct tipc_sock *tsk = tipc_sk(sk);
1106 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001107 struct sk_buff *buf;
1108 struct tipc_msg *msg;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001109 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001110 unsigned int sz;
1111 u32 err;
1112 int res;
1113
Allan Stephens0c3141e2008-04-15 00:22:02 -07001114 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115 if (unlikely(!buf_len))
1116 return -EINVAL;
1117
Allan Stephens0c3141e2008-04-15 00:22:02 -07001118 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001119
Allan Stephens0c3141e2008-04-15 00:22:02 -07001120 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121 res = -ENOTCONN;
1122 goto exit;
1123 }
1124
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001125 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001126restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127
Allan Stephens0c3141e2008-04-15 00:22:02 -07001128 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001129 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001130 if (res)
1131 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001132
1133 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001134 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135 msg = buf_msg(buf);
1136 sz = msg_data_sz(msg);
1137 err = msg_errcode(msg);
1138
Per Lidenb97bf3f2006-01-02 19:04:38 +01001139 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001141 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142 goto restart;
1143 }
1144
1145 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146 set_orig_addr(m, msg);
1147
1148 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001149 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001150 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001151 goto exit;
1152
1153 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154 if (!err) {
1155 if (unlikely(buf_len < sz)) {
1156 sz = buf_len;
1157 m->msg_flags |= MSG_TRUNC;
1158 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001159 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1160 m->msg_iov, sz);
1161 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001162 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 res = sz;
1164 } else {
1165 if ((sock->state == SS_READY) ||
1166 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1167 res = 0;
1168 else
1169 res = -ECONNRESET;
1170 }
1171
1172 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001174 if ((sock->state != SS_READY) &&
Jon Paul Maloy6163a192014-05-14 05:39:08 -04001175 (++port->conn_unacked >= TIPC_CONNACK_INTV))
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001176 tipc_acknowledge(port->ref, port->conn_unacked);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001177 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001178 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001180 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001181 return res;
1182}
1183
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001184/**
Ying Xue247f0f32014-02-18 16:06:46 +08001185 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 * @iocb: (unused)
1187 * @m: descriptor for message info
1188 * @buf_len: total size of user buffer area
1189 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001190 *
1191 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192 * will optionally wait for more; never truncates data.
1193 *
1194 * Returns size of returned message data, errno otherwise
1195 */
Ying Xue247f0f32014-02-18 16:06:46 +08001196static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1197 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001199 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001200 struct tipc_sock *tsk = tipc_sk(sk);
1201 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202 struct sk_buff *buf;
1203 struct tipc_msg *msg;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001204 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001206 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001209 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001210
1211 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001212 if (unlikely(!buf_len))
1213 return -EINVAL;
1214
Allan Stephens0c3141e2008-04-15 00:22:02 -07001215 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001217 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218 res = -ENOTCONN;
1219 goto exit;
1220 }
1221
Florian Westphal3720d402010-08-17 11:00:04 +00001222 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001223 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001224
Allan Stephens0c3141e2008-04-15 00:22:02 -07001225restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001226 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001227 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001228 if (res)
1229 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001230
1231 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001232 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233 msg = buf_msg(buf);
1234 sz = msg_data_sz(msg);
1235 err = msg_errcode(msg);
1236
1237 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001239 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240 goto restart;
1241 }
1242
1243 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001244 if (sz_copied == 0) {
1245 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001246 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001247 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001248 goto exit;
1249 }
1250
1251 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001253 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001254
Allan Stephens0232fd02011-02-21 09:45:40 -05001255 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001256 needed = (buf_len - sz_copied);
1257 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001258
1259 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1260 m->msg_iov, sz_to_copy);
1261 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001263
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264 sz_copied += sz_to_copy;
1265
1266 if (sz_to_copy < sz) {
1267 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001268 TIPC_SKB_CB(buf)->handle =
1269 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001270 goto exit;
1271 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 } else {
1273 if (sz_copied != 0)
1274 goto exit; /* can't add error msg to valid data */
1275
1276 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1277 res = 0;
1278 else
1279 res = -ECONNRESET;
1280 }
1281
1282 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001283 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy6163a192014-05-14 05:39:08 -04001284 if (unlikely(++port->conn_unacked >= TIPC_CONNACK_INTV))
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001285 tipc_acknowledge(port->ref, port->conn_unacked);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001286 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001287 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001288
1289 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001290 if ((sz_copied < buf_len) && /* didn't get all requested data */
1291 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001292 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001293 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1294 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295 goto restart;
1296
1297exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001298 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001299 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001300}
1301
1302/**
Ying Xuef288bef2012-08-21 11:16:57 +08001303 * tipc_write_space - wake up thread if port congestion is released
1304 * @sk: socket
1305 */
1306static void tipc_write_space(struct sock *sk)
1307{
1308 struct socket_wq *wq;
1309
1310 rcu_read_lock();
1311 wq = rcu_dereference(sk->sk_wq);
1312 if (wq_has_sleeper(wq))
1313 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1314 POLLWRNORM | POLLWRBAND);
1315 rcu_read_unlock();
1316}
1317
1318/**
1319 * tipc_data_ready - wake up threads to indicate messages have been received
1320 * @sk: socket
1321 * @len: the length of messages
1322 */
David S. Miller676d2362014-04-11 16:15:36 -04001323static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001324{
1325 struct socket_wq *wq;
1326
1327 rcu_read_lock();
1328 wq = rcu_dereference(sk->sk_wq);
1329 if (wq_has_sleeper(wq))
1330 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1331 POLLRDNORM | POLLRDBAND);
1332 rcu_read_unlock();
1333}
1334
1335/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001336 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001337 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001338 * @msg: message
1339 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001340 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001341 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001342static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001343{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001344 struct sock *sk = &tsk->sk;
1345 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001346 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001347 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001348
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001349 int retval = -TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001350 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001351
1352 if (msg_mcast(msg))
1353 return retval;
1354
1355 switch ((int)sock->state) {
1356 case SS_CONNECTED:
1357 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001358 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001359 if (unlikely(msg_errcode(msg))) {
1360 sock->state = SS_DISCONNECTING;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001361 __tipc_port_disconnect(port);
Ying Xue7e6c1312012-11-29 18:39:14 -05001362 }
1363 retval = TIPC_OK;
1364 }
1365 break;
1366 case SS_CONNECTING:
1367 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001368 if (unlikely(msg_errcode(msg))) {
1369 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001370 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001371 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001372 break;
1373 }
1374
1375 if (unlikely(!msg_connected(msg)))
1376 break;
1377
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001378 res = auto_connect(tsk, msg);
Ying Xue584d24b2012-11-29 18:51:19 -05001379 if (res) {
1380 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001381 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001382 retval = TIPC_OK;
1383 break;
1384 }
1385
1386 /* If an incoming message is an 'ACK-', it should be
1387 * discarded here because it doesn't contain useful
1388 * data. In addition, we should try to wake up
1389 * connect() routine if sleeping.
1390 */
1391 if (msg_data_sz(msg) == 0) {
1392 kfree_skb(*buf);
1393 *buf = NULL;
1394 if (waitqueue_active(sk_sleep(sk)))
1395 wake_up_interruptible(sk_sleep(sk));
1396 }
1397 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001398 break;
1399 case SS_LISTENING:
1400 case SS_UNCONNECTED:
1401 /* Accept only SYN message */
1402 if (!msg_connected(msg) && !(msg_errcode(msg)))
1403 retval = TIPC_OK;
1404 break;
1405 case SS_DISCONNECTING:
1406 break;
1407 default:
1408 pr_err("Unknown socket state %u\n", sock->state);
1409 }
1410 return retval;
1411}
1412
1413/**
Ying Xueaba79f32013-01-20 23:30:09 +01001414 * rcvbuf_limit - get proper overload limit of socket receive queue
1415 * @sk: socket
1416 * @buf: message
1417 *
1418 * For all connection oriented messages, irrespective of importance,
1419 * the default overload value (i.e. 67MB) is set as limit.
1420 *
1421 * For all connectionless messages, by default new queue limits are
1422 * as belows:
1423 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001424 * TIPC_LOW_IMPORTANCE (4 MB)
1425 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1426 * TIPC_HIGH_IMPORTANCE (16 MB)
1427 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001428 *
1429 * Returns overload limit according to corresponding message importance
1430 */
1431static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1432{
1433 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001434
1435 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001436 return sysctl_tipc_rmem[2];
1437
1438 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1439 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001440}
1441
1442/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001443 * filter_rcv - validate incoming message
1444 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001445 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001446 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001447 * Enqueues message on receive queue if acceptable; optionally handles
1448 * disconnect indication for a connected socket.
1449 *
1450 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001451 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001452 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
1453 * to be rejected.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001454 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001455static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001456{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001457 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001458 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001459 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001460 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001461 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001462
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001464 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001465 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001466
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001468 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001469 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001471 rc = filter_connect(tsk, &buf);
1472 if (rc != TIPC_OK || buf == NULL)
1473 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001474 }
1475
1476 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001477 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001478 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479
Ying Xueaba79f32013-01-20 23:30:09 +01001480 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001481 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001482 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001483 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001484
David S. Miller676d2362014-04-11 16:15:36 -04001485 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001486 return TIPC_OK;
1487}
1488
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001489/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001490 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001491 * @sk: socket
1492 * @buf: message
1493 *
1494 * Caller must hold socket lock, but not port lock.
1495 *
1496 * Returns 0
1497 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001498static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001499{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001500 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001501 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001502 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001503 uint truesize = buf->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001504
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001505 rc = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001506
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001507 if (unlikely(rc && tipc_msg_reverse(buf, &onode, -rc)))
1508 tipc_link_xmit2(buf, onode, 0);
1509 else if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001510 atomic_add(truesize, &tsk->dupl_rcvcnt);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001511
Allan Stephens0c3141e2008-04-15 00:22:02 -07001512 return 0;
1513}
1514
1515/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001516 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001517 * @buf: buffer containing arriving message
1518 * Consumes buffer
1519 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001520 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001521int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001522{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001523 struct tipc_sock *tsk;
1524 struct tipc_port *port;
1525 struct sock *sk;
1526 u32 dport = msg_destport(buf_msg(buf));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001527 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001528 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001529 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001530
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001531 /* Validate destination and message */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001532 port = tipc_port_lock(dport);
1533 if (unlikely(!port)) {
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001534 rc = tipc_msg_eval(buf, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001535 goto exit;
1536 }
1537
1538 tsk = tipc_port_to_sock(port);
1539 sk = &tsk->sk;
1540
1541 /* Queue message */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001542 bh_lock_sock(sk);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001543
Allan Stephens0c3141e2008-04-15 00:22:02 -07001544 if (!sock_owned_by_user(sk)) {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001545 rc = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001546 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001547 if (sk->sk_backlog.len == 0)
1548 atomic_set(&tsk->dupl_rcvcnt, 0);
1549 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1550 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001551 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001552 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001553 bh_unlock_sock(sk);
1554 tipc_port_unlock(port);
1555
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001556 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001557 return 0;
1558exit:
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001559 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001560 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001561
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001562 tipc_link_xmit2(buf, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001563 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001564}
1565
Ying Xue78eb3a52014-01-17 09:50:03 +08001566static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1567{
1568 struct sock *sk = sock->sk;
1569 DEFINE_WAIT(wait);
1570 int done;
1571
1572 do {
1573 int err = sock_error(sk);
1574 if (err)
1575 return err;
1576 if (!*timeo_p)
1577 return -ETIMEDOUT;
1578 if (signal_pending(current))
1579 return sock_intr_errno(*timeo_p);
1580
1581 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1582 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1583 finish_wait(sk_sleep(sk), &wait);
1584 } while (!done);
1585 return 0;
1586}
1587
Per Lidenb97bf3f2006-01-02 19:04:38 +01001588/**
Ying Xue247f0f32014-02-18 16:06:46 +08001589 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001590 * @sock: socket structure
1591 * @dest: socket address for destination port
1592 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001593 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001594 *
1595 * Returns 0 on success, errno otherwise
1596 */
Ying Xue247f0f32014-02-18 16:06:46 +08001597static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1598 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001599{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001600 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001601 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1602 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001603 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1604 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001605 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606
Allan Stephens0c3141e2008-04-15 00:22:02 -07001607 lock_sock(sk);
1608
Allan Stephensb89741a2008-04-15 00:20:37 -07001609 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001610 if (sock->state == SS_READY) {
1611 res = -EOPNOTSUPP;
1612 goto exit;
1613 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001614
Allan Stephensb89741a2008-04-15 00:20:37 -07001615 /*
1616 * Reject connection attempt using multicast address
1617 *
1618 * Note: send_msg() validates the rest of the address fields,
1619 * so there's no need to do it here
1620 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001621 if (dst->addrtype == TIPC_ADDR_MCAST) {
1622 res = -EINVAL;
1623 goto exit;
1624 }
1625
Ying Xue78eb3a52014-01-17 09:50:03 +08001626 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001627 switch (sock->state) {
1628 case SS_UNCONNECTED:
1629 /* Send a 'SYN-' to destination */
1630 m.msg_name = dest;
1631 m.msg_namelen = destlen;
1632
1633 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1634 * indicate send_msg() is never blocked.
1635 */
1636 if (!timeout)
1637 m.msg_flags = MSG_DONTWAIT;
1638
Ying Xue247f0f32014-02-18 16:06:46 +08001639 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001640 if ((res < 0) && (res != -EWOULDBLOCK))
1641 goto exit;
1642
1643 /* Just entered SS_CONNECTING state; the only
1644 * difference is that return value in non-blocking
1645 * case is EINPROGRESS, rather than EALREADY.
1646 */
1647 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001648 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001649 if (previous == SS_CONNECTING)
1650 res = -EALREADY;
1651 if (!timeout)
1652 goto exit;
1653 timeout = msecs_to_jiffies(timeout);
1654 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1655 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001656 break;
1657 case SS_CONNECTED:
1658 res = -EISCONN;
1659 break;
1660 default:
1661 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001662 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001663 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001664exit:
1665 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001666 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001667}
1668
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001669/**
Ying Xue247f0f32014-02-18 16:06:46 +08001670 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001671 * @sock: socket structure
1672 * @len: (unused)
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_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001677{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001678 struct sock *sk = sock->sk;
1679 int res;
1680
1681 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001682
Ying Xue245f3d32011-07-06 06:01:13 -04001683 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001684 res = -EINVAL;
1685 else {
1686 sock->state = SS_LISTENING;
1687 res = 0;
1688 }
1689
1690 release_sock(sk);
1691 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001692}
1693
Ying Xue6398e232014-01-17 09:50:04 +08001694static int tipc_wait_for_accept(struct socket *sock, long timeo)
1695{
1696 struct sock *sk = sock->sk;
1697 DEFINE_WAIT(wait);
1698 int err;
1699
1700 /* True wake-one mechanism for incoming connections: only
1701 * one process gets woken up, not the 'whole herd'.
1702 * Since we do not 'race & poll' for established sockets
1703 * anymore, the common case will execute the loop only once.
1704 */
1705 for (;;) {
1706 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1707 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001708 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001709 release_sock(sk);
1710 timeo = schedule_timeout(timeo);
1711 lock_sock(sk);
1712 }
1713 err = 0;
1714 if (!skb_queue_empty(&sk->sk_receive_queue))
1715 break;
1716 err = -EINVAL;
1717 if (sock->state != SS_LISTENING)
1718 break;
1719 err = sock_intr_errno(timeo);
1720 if (signal_pending(current))
1721 break;
1722 err = -EAGAIN;
1723 if (!timeo)
1724 break;
1725 }
1726 finish_wait(sk_sleep(sk), &wait);
1727 return err;
1728}
1729
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001730/**
Ying Xue247f0f32014-02-18 16:06:46 +08001731 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001732 * @sock: listening socket
1733 * @newsock: new socket that is to be connected
1734 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001735 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001736 * Returns 0 on success, errno otherwise
1737 */
Ying Xue247f0f32014-02-18 16:06:46 +08001738static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001740 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001742 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001743 struct tipc_msg *msg;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001744 struct tipc_portid peer;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001745 u32 new_ref;
Ying Xue6398e232014-01-17 09:50:04 +08001746 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001747 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001748
Allan Stephens0c3141e2008-04-15 00:22:02 -07001749 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001750
Allan Stephens0c3141e2008-04-15 00:22:02 -07001751 if (sock->state != SS_LISTENING) {
1752 res = -EINVAL;
1753 goto exit;
1754 }
Ying Xue6398e232014-01-17 09:50:04 +08001755 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1756 res = tipc_wait_for_accept(sock, timeo);
1757 if (res)
1758 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001759
1760 buf = skb_peek(&sk->sk_receive_queue);
1761
Ying Xuec5fa7b32013-06-17 10:54:39 -04001762 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001763 if (res)
1764 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001765
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001766 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001767 new_port = &tipc_sk(new_sk)->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001768 new_ref = new_port->ref;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001769 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001770
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001771 /* we lock on new_sk; but lockdep sees the lock on sk */
1772 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001773
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001774 /*
1775 * Reject any stray messages received by new socket
1776 * before the socket lock was taken (very, very unlikely)
1777 */
1778 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001779
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001780 /* Connect new socket to it's peer */
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001781 peer.ref = msg_origport(msg);
1782 peer.node = msg_orignode(msg);
1783 tipc_port_connect(new_ref, &peer);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001784 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001785
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001786 tipc_port_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001787 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001788 new_port->conn_type = msg_nametype(msg);
1789 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001790 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001791
1792 /*
1793 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1794 * Respond to 'SYN+' by queuing it on new socket.
1795 */
1796 if (!msg_data_sz(msg)) {
1797 struct msghdr m = {NULL,};
1798
1799 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001800 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001801 } else {
1802 __skb_dequeue(&sk->sk_receive_queue);
1803 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001804 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001805 }
1806 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001807exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001808 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001809 return res;
1810}
1811
1812/**
Ying Xue247f0f32014-02-18 16:06:46 +08001813 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001815 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816 *
1817 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001818 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001819 * Returns 0 on success, errno otherwise
1820 */
Ying Xue247f0f32014-02-18 16:06:46 +08001821static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001822{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001823 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001824 struct tipc_sock *tsk = tipc_sk(sk);
1825 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001827 u32 peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001828 int res;
1829
Allan Stephense247a8f2008-03-06 15:05:38 -08001830 if (how != SHUT_RDWR)
1831 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001832
Allan Stephens0c3141e2008-04-15 00:22:02 -07001833 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834
1835 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001836 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001837 case SS_CONNECTED:
1838
Per Lidenb97bf3f2006-01-02 19:04:38 +01001839restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001840 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001841 buf = __skb_dequeue(&sk->sk_receive_queue);
1842 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001843 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001844 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001845 goto restart;
1846 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001847 tipc_port_disconnect(port->ref);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001848 if (tipc_msg_reverse(buf, &peer, TIPC_CONN_SHUTDOWN))
1849 tipc_link_xmit2(buf, peer, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001850 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001851 tipc_port_shutdown(port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001852 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001853
1854 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855
1856 /* fall through */
1857
1858 case SS_DISCONNECTING:
1859
Ying Xue75031152012-10-29 09:38:15 -04001860 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001861 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001862
1863 /* Wake up anyone sleeping in poll */
1864 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001865 res = 0;
1866 break;
1867
1868 default:
1869 res = -ENOTCONN;
1870 }
1871
Allan Stephens0c3141e2008-04-15 00:22:02 -07001872 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001873 return res;
1874}
1875
1876/**
Ying Xue247f0f32014-02-18 16:06:46 +08001877 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001878 * @sock: socket structure
1879 * @lvl: option level
1880 * @opt: option identifier
1881 * @ov: pointer to new option value
1882 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001883 *
1884 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001885 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001886 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001887 * Returns 0 on success, errno otherwise
1888 */
Ying Xue247f0f32014-02-18 16:06:46 +08001889static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1890 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001891{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001892 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001893 struct tipc_sock *tsk = tipc_sk(sk);
1894 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001895 u32 value;
1896 int res;
1897
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001898 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1899 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001900 if (lvl != SOL_TIPC)
1901 return -ENOPROTOOPT;
1902 if (ol < sizeof(value))
1903 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001904 res = get_user(value, (u32 __user *)ov);
1905 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001906 return res;
1907
Allan Stephens0c3141e2008-04-15 00:22:02 -07001908 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001909
Per Lidenb97bf3f2006-01-02 19:04:38 +01001910 switch (opt) {
1911 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001912 tipc_port_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001913 break;
1914 case TIPC_SRC_DROPPABLE:
1915 if (sock->type != SOCK_STREAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001916 tipc_port_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001917 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001918 res = -ENOPROTOOPT;
1919 break;
1920 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001921 tipc_port_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001922 break;
1923 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001924 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001925 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001926 break;
1927 default:
1928 res = -EINVAL;
1929 }
1930
Allan Stephens0c3141e2008-04-15 00:22:02 -07001931 release_sock(sk);
1932
Per Lidenb97bf3f2006-01-02 19:04:38 +01001933 return res;
1934}
1935
1936/**
Ying Xue247f0f32014-02-18 16:06:46 +08001937 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001938 * @sock: socket structure
1939 * @lvl: option level
1940 * @opt: option identifier
1941 * @ov: receptacle for option value
1942 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001943 *
1944 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001945 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001946 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001947 * Returns 0 on success, errno otherwise
1948 */
Ying Xue247f0f32014-02-18 16:06:46 +08001949static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1950 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001951{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001952 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001953 struct tipc_sock *tsk = tipc_sk(sk);
1954 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001955 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001956 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001957 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001958
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001959 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1960 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001961 if (lvl != SOL_TIPC)
1962 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001963 res = get_user(len, ol);
1964 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001965 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001966
Allan Stephens0c3141e2008-04-15 00:22:02 -07001967 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001968
1969 switch (opt) {
1970 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001971 value = tipc_port_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001972 break;
1973 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001974 value = tipc_port_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001975 break;
1976 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001977 value = tipc_port_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001978 break;
1979 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001980 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001981 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001982 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001983 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001984 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001985 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001986 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001987 value = skb_queue_len(&sk->sk_receive_queue);
1988 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001989 default:
1990 res = -EINVAL;
1991 }
1992
Allan Stephens0c3141e2008-04-15 00:22:02 -07001993 release_sock(sk);
1994
Paul Gortmaker25860c32010-12-31 18:59:31 +00001995 if (res)
1996 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001997
Paul Gortmaker25860c32010-12-31 18:59:31 +00001998 if (len < sizeof(value))
1999 return -EINVAL;
2000
2001 if (copy_to_user(ov, &value, sizeof(value)))
2002 return -EFAULT;
2003
2004 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002005}
2006
Erik Hugne78acb1f2014-04-24 16:26:47 +02002007int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
2008{
2009 struct tipc_sioc_ln_req lnr;
2010 void __user *argp = (void __user *)arg;
2011
2012 switch (cmd) {
2013 case SIOCGETLINKNAME:
2014 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2015 return -EFAULT;
2016 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
2017 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2018 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2019 return -EFAULT;
2020 return 0;
2021 }
2022 return -EADDRNOTAVAIL;
2023 break;
2024 default:
2025 return -ENOIOCTLCMD;
2026 }
2027}
2028
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002029/* Protocol switches for the various types of TIPC sockets */
2030
Florian Westphalbca65ea2008-02-07 18:18:01 -08002031static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002032 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002033 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002034 .release = tipc_release,
2035 .bind = tipc_bind,
2036 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002037 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002038 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002039 .getname = tipc_getname,
2040 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002041 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002042 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002043 .shutdown = tipc_shutdown,
2044 .setsockopt = tipc_setsockopt,
2045 .getsockopt = tipc_getsockopt,
2046 .sendmsg = tipc_sendmsg,
2047 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002048 .mmap = sock_no_mmap,
2049 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002050};
2051
Florian Westphalbca65ea2008-02-07 18:18:01 -08002052static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002053 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002054 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002055 .release = tipc_release,
2056 .bind = tipc_bind,
2057 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002058 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002059 .accept = tipc_accept,
2060 .getname = tipc_getname,
2061 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002062 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002063 .listen = tipc_listen,
2064 .shutdown = tipc_shutdown,
2065 .setsockopt = tipc_setsockopt,
2066 .getsockopt = tipc_getsockopt,
2067 .sendmsg = tipc_send_packet,
2068 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002069 .mmap = sock_no_mmap,
2070 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002071};
2072
Florian Westphalbca65ea2008-02-07 18:18:01 -08002073static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002074 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002075 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002076 .release = tipc_release,
2077 .bind = tipc_bind,
2078 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002079 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002080 .accept = tipc_accept,
2081 .getname = tipc_getname,
2082 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002083 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002084 .listen = tipc_listen,
2085 .shutdown = tipc_shutdown,
2086 .setsockopt = tipc_setsockopt,
2087 .getsockopt = tipc_getsockopt,
2088 .sendmsg = tipc_send_stream,
2089 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002090 .mmap = sock_no_mmap,
2091 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002092};
2093
Florian Westphalbca65ea2008-02-07 18:18:01 -08002094static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002095 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002096 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002097 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002098};
2099
2100static struct proto tipc_proto = {
2101 .name = "TIPC",
2102 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002103 .obj_size = sizeof(struct tipc_sock),
2104 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002105};
2106
Ying Xuec5fa7b32013-06-17 10:54:39 -04002107static struct proto tipc_proto_kern = {
2108 .name = "TIPC",
2109 .obj_size = sizeof(struct tipc_sock),
2110 .sysctl_rmem = sysctl_tipc_rmem
2111};
2112
Per Lidenb97bf3f2006-01-02 19:04:38 +01002113/**
Per Liden4323add2006-01-18 00:38:21 +01002114 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002115 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002116 * Returns 0 on success, errno otherwise
2117 */
Per Liden4323add2006-01-18 00:38:21 +01002118int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002119{
2120 int res;
2121
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002122 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002123 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002124 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002125 goto out;
2126 }
2127
2128 res = sock_register(&tipc_family_ops);
2129 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002130 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002131 proto_unregister(&tipc_proto);
2132 goto out;
2133 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002134 out:
2135 return res;
2136}
2137
2138/**
Per Liden4323add2006-01-18 00:38:21 +01002139 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002140 */
Per Liden4323add2006-01-18 00:38:21 +01002141void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002142{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002143 sock_unregister(tipc_family_ops.family);
2144 proto_unregister(&tipc_proto);
2145}