blob: d838a4b66d3aed2eb2ef503405b4a20cd2809d42 [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;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500209 tsk->port.sent = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400210 atomic_set(&tsk->dupl_rcvcnt, 0);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400211 tipc_port_unlock(port);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700212
Allan Stephens0c3141e2008-04-15 00:22:02 -0700213 if (sock->state == SS_READY) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400214 tipc_port_set_unreturnable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700215 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400216 tipc_port_set_unreliable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700217 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218 return 0;
219}
220
221/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400222 * tipc_sock_create_local - create TIPC socket from inside TIPC module
223 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
224 *
225 * We cannot use sock_creat_kern here because it bumps module user count.
226 * Since socket owner and creator is the same module we must make sure
227 * that module count remains zero for module local sockets, otherwise
228 * we cannot do rmmod.
229 *
230 * Returns 0 on success, errno otherwise
231 */
232int tipc_sock_create_local(int type, struct socket **res)
233{
234 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400235
236 rc = sock_create_lite(AF_TIPC, type, 0, res);
237 if (rc < 0) {
238 pr_err("Failed to create kernel socket\n");
239 return rc;
240 }
241 tipc_sk_create(&init_net, *res, 0, 1);
242
Ying Xuec5fa7b32013-06-17 10:54:39 -0400243 return 0;
244}
245
246/**
247 * tipc_sock_release_local - release socket created by tipc_sock_create_local
248 * @sock: the socket to be released.
249 *
250 * Module reference count is not incremented when such sockets are created,
251 * so we must keep it from being decremented when they are released.
252 */
253void tipc_sock_release_local(struct socket *sock)
254{
Ying Xue247f0f32014-02-18 16:06:46 +0800255 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400256 sock->ops = NULL;
257 sock_release(sock);
258}
259
260/**
261 * tipc_sock_accept_local - accept a connection on a socket created
262 * with tipc_sock_create_local. Use this function to avoid that
263 * module reference count is inadvertently incremented.
264 *
265 * @sock: the accepting socket
266 * @newsock: reference to the new socket to be created
267 * @flags: socket flags
268 */
269
270int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400271 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400272{
273 struct sock *sk = sock->sk;
274 int ret;
275
276 ret = sock_create_lite(sk->sk_family, sk->sk_type,
277 sk->sk_protocol, newsock);
278 if (ret < 0)
279 return ret;
280
Ying Xue247f0f32014-02-18 16:06:46 +0800281 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400282 if (ret < 0) {
283 sock_release(*newsock);
284 return ret;
285 }
286 (*newsock)->ops = sock->ops;
287 return ret;
288}
289
290/**
Ying Xue247f0f32014-02-18 16:06:46 +0800291 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100292 * @sock: socket to destroy
293 *
294 * This routine cleans up any messages that are still queued on the socket.
295 * For DGRAM and RDM socket types, all queued messages are rejected.
296 * For SEQPACKET and STREAM socket types, the first message is rejected
297 * and any others are discarded. (If the first message on a STREAM socket
298 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900299 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 * NOTE: Rejected messages are not necessarily returned to the sender! They
301 * are returned or discarded according to the "destination droppable" setting
302 * specified for the message by the sender.
303 *
304 * Returns 0 on success, errno otherwise
305 */
Ying Xue247f0f32014-02-18 16:06:46 +0800306static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400309 struct tipc_sock *tsk;
310 struct tipc_port *port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500312 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313
Allan Stephens0c3141e2008-04-15 00:22:02 -0700314 /*
315 * Exit if socket isn't fully initialized (occurs when a failed accept()
316 * releases a pre-allocated child socket that was never used)
317 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700318 if (sk == NULL)
319 return 0;
320
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400321 tsk = tipc_sk(sk);
322 port = &tsk->port;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700323 lock_sock(sk);
324
325 /*
326 * Reject all unreceived messages, except on an active connection
327 * (which disconnects locally & sends a 'FIN+' to peer)
328 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700330 buf = __skb_dequeue(&sk->sk_receive_queue);
331 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332 break;
Ying Xue40682432013-10-18 07:23:16 +0200333 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400334 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700335 else {
336 if ((sock->state == SS_CONNECTING) ||
337 (sock->state == SS_CONNECTED)) {
338 sock->state = SS_DISCONNECTING;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400339 tipc_port_disconnect(port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700340 }
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500341 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
342 tipc_link_xmit2(buf, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700343 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344 }
345
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400346 /* Destroy TIPC port; also disconnects an active connection and
347 * sends a 'FIN-' to peer.
Allan Stephens0c3141e2008-04-15 00:22:02 -0700348 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400349 tipc_port_destroy(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350
Allan Stephens0c3141e2008-04-15 00:22:02 -0700351 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100352 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353
Allan Stephens0c3141e2008-04-15 00:22:02 -0700354 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700355 sock->state = SS_DISCONNECTING;
356 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357
358 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700359 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200361 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362}
363
364/**
Ying Xue247f0f32014-02-18 16:06:46 +0800365 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 * @sock: socket structure
367 * @uaddr: socket address describing name(s) and desired operation
368 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900369 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100370 * Name and name sequence binding is indicated using a positive scope value;
371 * a negative scope value unbinds the specified name. Specifying no name
372 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900373 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700375 *
376 * NOTE: This routine doesn't need to take the socket lock since it doesn't
377 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378 */
Ying Xue247f0f32014-02-18 16:06:46 +0800379static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
380 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381{
Ying Xue84602762013-12-27 10:18:28 +0800382 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400384 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800385 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386
Ying Xue84602762013-12-27 10:18:28 +0800387 lock_sock(sk);
388 if (unlikely(!uaddr_len)) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400389 res = tipc_withdraw(&tsk->port, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800390 goto exit;
391 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900392
Ying Xue84602762013-12-27 10:18:28 +0800393 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
394 res = -EINVAL;
395 goto exit;
396 }
397 if (addr->family != AF_TIPC) {
398 res = -EAFNOSUPPORT;
399 goto exit;
400 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402 if (addr->addrtype == TIPC_ADDR_NAME)
403 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800404 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
405 res = -EAFNOSUPPORT;
406 goto exit;
407 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900408
Ying Xue13a2e892013-06-17 10:54:40 -0400409 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400410 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800411 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
412 res = -EACCES;
413 goto exit;
414 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400415
Ying Xue84602762013-12-27 10:18:28 +0800416 res = (addr->scope > 0) ?
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400417 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
418 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800419exit:
420 release_sock(sk);
421 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422}
423
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900424/**
Ying Xue247f0f32014-02-18 16:06:46 +0800425 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 * @sock: socket structure
427 * @uaddr: area for returned socket address
428 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700429 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900430 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700432 *
Allan Stephens2da59912008-07-14 22:43:32 -0700433 * NOTE: This routine doesn't need to take the socket lock since it only
434 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000435 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100436 */
Ying Xue247f0f32014-02-18 16:06:46 +0800437static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
438 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400441 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000443 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700444 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700445 if ((sock->state != SS_CONNECTED) &&
446 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
447 return -ENOTCONN;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400448 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
449 addr->addr.id.node = tipc_port_peernode(&tsk->port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700450 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400451 addr->addr.id.ref = tsk->port.ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000452 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700453 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454
455 *uaddr_len = sizeof(*addr);
456 addr->addrtype = TIPC_ADDR_ID;
457 addr->family = AF_TIPC;
458 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 addr->addr.name.domain = 0;
460
Allan Stephens0c3141e2008-04-15 00:22:02 -0700461 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462}
463
464/**
Ying Xue247f0f32014-02-18 16:06:46 +0800465 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466 * @file: file structure associated with the socket
467 * @sock: socket for which to calculate the poll bits
468 * @wait: ???
469 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700470 * Returns pollmask value
471 *
472 * COMMENTARY:
473 * It appears that the usual socket locking mechanisms are not useful here
474 * since the pollmask info is potentially out-of-date the moment this routine
475 * exits. TCP and other protocols seem to rely on higher level poll routines
476 * to handle any preventable race conditions, so TIPC will do the same ...
477 *
478 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700479 *
Allan Stephensf662c072010-08-17 11:00:06 +0000480 * socket state flags set
481 * ------------ ---------
482 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200483 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000484 *
485 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
486 * no write flags
487 *
488 * connected POLLIN/POLLRDNORM if data in rx queue
489 * POLLOUT if port is not congested
490 *
491 * disconnecting POLLIN/POLLRDNORM/POLLHUP
492 * no write flags
493 *
494 * listening POLLIN if SYN in rx queue
495 * no write flags
496 *
497 * ready POLLIN/POLLRDNORM if data in rx queue
498 * [connectionless] POLLOUT (since port cannot be congested)
499 *
500 * IMPORTANT: The fact that a read or write operation is indicated does NOT
501 * imply that the operation will succeed, merely that it should be performed
502 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503 */
Ying Xue247f0f32014-02-18 16:06:46 +0800504static unsigned int tipc_poll(struct file *file, struct socket *sock,
505 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506{
Allan Stephens9b674e82008-03-26 16:48:21 -0700507 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400508 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000509 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700510
Ying Xuef288bef2012-08-21 11:16:57 +0800511 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700512
Allan Stephensf662c072010-08-17 11:00:06 +0000513 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200514 case SS_UNCONNECTED:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400515 if (!tsk->port.congested)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200516 mask |= POLLOUT;
517 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000518 case SS_READY:
519 case SS_CONNECTED:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400520 if (!tsk->port.congested)
Allan Stephensf662c072010-08-17 11:00:06 +0000521 mask |= POLLOUT;
522 /* fall thru' */
523 case SS_CONNECTING:
524 case SS_LISTENING:
525 if (!skb_queue_empty(&sk->sk_receive_queue))
526 mask |= (POLLIN | POLLRDNORM);
527 break;
528 case SS_DISCONNECTING:
529 mask = (POLLIN | POLLRDNORM | POLLHUP);
530 break;
531 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700532
533 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534}
535
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900536/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 * dest_name_check - verify user is permitted to send to specified port name
538 * @dest: destination address
539 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900540 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100541 * Prevents restricted configuration commands from being issued by
542 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900543 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 * Returns 0 if permission is granted, otherwise errno
545 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800546static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547{
548 struct tipc_cfg_msg_hdr hdr;
549
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500550 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
551 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900552 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
553 return 0;
554 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
555 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900556 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
557 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100558
Allan Stephens3f8dd942011-01-18 13:09:29 -0500559 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
560 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900561 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100562 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700563 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900565
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 return 0;
567}
568
Ying Xue3f405042014-01-17 09:50:05 +0800569static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
570{
571 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400572 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800573 DEFINE_WAIT(wait);
574 int done;
575
576 do {
577 int err = sock_error(sk);
578 if (err)
579 return err;
580 if (sock->state == SS_DISCONNECTING)
581 return -EPIPE;
582 if (!*timeo_p)
583 return -EAGAIN;
584 if (signal_pending(current))
585 return sock_intr_errno(*timeo_p);
586
587 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400588 done = sk_wait_event(sk, timeo_p, !tsk->port.congested);
Ying Xue3f405042014-01-17 09:50:05 +0800589 finish_wait(sk_sleep(sk), &wait);
590 } while (!done);
591 return 0;
592}
593
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500594/**
595 * tipc_sendmcast - send multicast message
596 * @sock: socket structure
597 * @seq: destination address
598 * @iov: message data to send
599 * @dsz: total length of message data
600 * @timeo: timeout to wait for wakeup
601 *
602 * Called from function tipc_sendmsg(), which has done all sanity checks
603 * Returns the number of bytes sent on success, or errno
604 */
605static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
606 struct iovec *iov, size_t dsz, long timeo)
607{
608 struct sock *sk = sock->sk;
609 struct tipc_sock *tsk = tipc_sk(sk);
610 int rc;
611
612 do {
613 if (sock->state != SS_READY) {
614 rc = -EOPNOTSUPP;
615 break;
616 }
617 rc = tipc_port_mcast_xmit(&tsk->port, seq, iov, dsz);
618 if (likely(rc >= 0)) {
619 if (sock->state != SS_READY)
620 sock->state = SS_CONNECTING;
621 break;
622 }
623 if (rc != -ELINKCONG)
624 break;
625 rc = tipc_wait_for_sndmsg(sock, &timeo);
626 } while (!rc);
627
628 return rc;
629}
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400630
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631/**
Ying Xue247f0f32014-02-18 16:06:46 +0800632 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700633 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634 * @sock: socket structure
635 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500636 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900637 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900639 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
641 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900642 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643 * Returns the number of bytes sent on success, or errno otherwise
644 */
Ying Xue247f0f32014-02-18 16:06:46 +0800645static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500646 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500648 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700649 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400650 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400651 struct tipc_port *port = &tsk->port;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500652 struct tipc_msg *mhdr = &port->phdr;
653 struct iovec *iov = m->msg_iov;
654 u32 dnode, dport;
655 struct sk_buff *buf;
656 struct tipc_name_seq *seq = &dest->addr.nameseq;
657 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800658 long timeo;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500659 int rc = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660
661 if (unlikely(!dest))
662 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500663
Allan Stephens51f9cc12006-06-25 23:49:06 -0700664 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
665 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500667
668 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400669 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670
Allan Stephens0c3141e2008-04-15 00:22:02 -0700671 if (iocb)
672 lock_sock(sk);
673
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500674 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700675 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500676 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700677 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700678 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700679 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500680 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700681 goto exit;
682 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400683 if (tsk->port.published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500684 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700685 goto exit;
686 }
687 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400688 tsk->port.conn_type = dest->addr.name.name.type;
689 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700690 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100691 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500692 rc = dest_name_check(dest, m);
693 if (rc)
694 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100695
Ying Xue3f405042014-01-17 09:50:05 +0800696 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500697
698 if (dest->addrtype == TIPC_ADDR_MCAST) {
699 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
700 goto exit;
701 } else if (dest->addrtype == TIPC_ADDR_NAME) {
702 u32 type = dest->addr.name.name.type;
703 u32 inst = dest->addr.name.name.instance;
704 u32 domain = dest->addr.name.domain;
705
706 dnode = domain;
707 msg_set_type(mhdr, TIPC_NAMED_MSG);
708 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
709 msg_set_nametype(mhdr, type);
710 msg_set_nameinst(mhdr, inst);
711 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
712 dport = tipc_nametbl_translate(type, inst, &dnode);
713 msg_set_destnode(mhdr, dnode);
714 msg_set_destport(mhdr, dport);
715 if (unlikely(!dport && !dnode)) {
716 rc = -EHOSTUNREACH;
717 goto exit;
718 }
719 } else if (dest->addrtype == TIPC_ADDR_ID) {
720 dnode = dest->addr.id.node;
721 msg_set_type(mhdr, TIPC_DIRECT_MSG);
722 msg_set_lookup_scope(mhdr, 0);
723 msg_set_destnode(mhdr, dnode);
724 msg_set_destport(mhdr, dest->addr.id.ref);
725 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
726 }
727
728new_mtu:
729 mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
730 rc = tipc_msg_build2(mhdr, iov, 0, dsz, mtu, &buf);
731 if (rc < 0)
732 goto exit;
733
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900734 do {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500735 rc = tipc_link_xmit2(buf, dnode, tsk->port.ref);
736 if (likely(rc >= 0)) {
737 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700738 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500739 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700740 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900741 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500742 if (rc == -EMSGSIZE)
743 goto new_mtu;
744
745 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700746 break;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500747
748 rc = tipc_wait_for_sndmsg(sock, &timeo);
749 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700750
751exit:
752 if (iocb)
753 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500754
755 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100756}
757
Ying Xue391a6dd2014-01-17 09:50:06 +0800758static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
759{
760 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400761 struct tipc_sock *tsk = tipc_sk(sk);
762 struct tipc_port *port = &tsk->port;
Ying Xue391a6dd2014-01-17 09:50:06 +0800763 DEFINE_WAIT(wait);
764 int done;
765
766 do {
767 int err = sock_error(sk);
768 if (err)
769 return err;
770 if (sock->state == SS_DISCONNECTING)
771 return -EPIPE;
772 else if (sock->state != SS_CONNECTED)
773 return -ENOTCONN;
774 if (!*timeo_p)
775 return -EAGAIN;
776 if (signal_pending(current))
777 return sock_intr_errno(*timeo_p);
778
779 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
780 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400781 (!port->congested || !port->connected));
Ying Xue391a6dd2014-01-17 09:50:06 +0800782 finish_wait(sk_sleep(sk), &wait);
783 } while (!done);
784 return 0;
785}
786
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900787/**
Ying Xue247f0f32014-02-18 16:06:46 +0800788 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789 * @iocb: (unused)
790 * @sock: socket structure
791 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500792 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900793 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900795 *
796 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700797 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798 */
Ying Xue247f0f32014-02-18 16:06:46 +0800799static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500800 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100801{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700802 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400803 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500804 struct tipc_port *port = &tsk->port;
805 struct tipc_msg *mhdr = &port->phdr;
806 struct sk_buff *buf;
807 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
808 u32 ref = port->ref;
809 int rc = -EINVAL;
810 long timeo;
811 u32 dnode;
812 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900813
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500814 /* Handle implied connection establishment */
815 if (unlikely(dest)) {
816 rc = tipc_sendmsg(iocb, sock, m, dsz);
817 if (dsz && (dsz == rc))
818 tsk->port.sent = 1;
819 return rc;
820 }
821 if (dsz > (uint)INT_MAX)
822 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700823
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500824 if (iocb)
825 lock_sock(sk);
826
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900827 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500828 if (sock->state == SS_DISCONNECTING)
829 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +0800830 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500831 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800832 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900833 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500835 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
836 dnode = tipc_port_peernode(port);
837 port->congested = 1;
838
839next:
840 mtu = port->max_pkt;
841 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
842 rc = tipc_msg_build2(mhdr, m->msg_iov, sent, send, mtu, &buf);
843 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700844 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500845 do {
846 port->congested = 1;
847 if (likely(!tipc_port_congested(port))) {
848 rc = tipc_link_xmit2(buf, dnode, ref);
849 if (likely(!rc)) {
850 port->sent++;
851 sent += send;
852 if (sent == dsz)
853 break;
854 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700855 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500856 if (rc == -EMSGSIZE) {
857 port->max_pkt = tipc_node_get_mtu(dnode, ref);
858 goto next;
859 }
860 if (rc != -ELINKCONG)
861 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100862 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500863 rc = tipc_wait_for_sndpkt(sock, &timeo);
864 } while (!rc);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500866 port->congested = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700867exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500868 if (iocb)
869 release_sock(sk);
870 return sent ? sent : rc;
871}
872
873/**
874 * tipc_send_packet - send a connection-oriented message
875 * @iocb: if NULL, indicates that socket lock is already held
876 * @sock: socket structure
877 * @m: message to send
878 * @dsz: length of data to be transmitted
879 *
880 * Used for SOCK_SEQPACKET messages.
881 *
882 * Returns the number of bytes sent on success, or errno otherwise
883 */
884static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
885 struct msghdr *m, size_t dsz)
886{
887 if (dsz > TIPC_MAX_USER_MSG_SIZE)
888 return -EMSGSIZE;
889
890 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891}
892
893/**
894 * auto_connect - complete connection setup to a remote port
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400895 * @tsk: tipc socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100896 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900897 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898 * Returns 0 on success, errno otherwise
899 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400900static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400902 struct tipc_port *port = &tsk->port;
903 struct socket *sock = tsk->sk.sk_socket;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400904 struct tipc_portid peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400906 peer.ref = msg_origport(msg);
907 peer.node = msg_orignode(msg);
908
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400909 __tipc_port_connect(port->ref, port, &peer);
Ying Xue584d24b2012-11-29 18:51:19 -0500910
911 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
912 return -EINVAL;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400913 msg_set_importance(&port->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914 sock->state = SS_CONNECTED;
915 return 0;
916}
917
918/**
919 * set_orig_addr - capture sender's address for received message
920 * @m: descriptor for message info
921 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900922 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100923 * Note: Address is not captured if not requested by receiver.
924 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800925static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926{
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100927 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900929 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100930 addr->family = AF_TIPC;
931 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000932 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100933 addr->addr.id.ref = msg_origport(msg);
934 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000935 addr->addr.name.domain = 0; /* could leave uninitialized */
936 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100937 m->msg_namelen = sizeof(struct sockaddr_tipc);
938 }
939}
940
941/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900942 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100943 * @m: descriptor for message info
944 * @msg: received message header
945 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900946 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900948 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100949 * Returns 0 if successful, otherwise errno
950 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800951static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400952 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100953{
954 u32 anc_data[3];
955 u32 err;
956 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700957 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958 int res;
959
960 if (likely(m->msg_controllen == 0))
961 return 0;
962
963 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964 err = msg ? msg_errcode(msg) : 0;
965 if (unlikely(err)) {
966 anc_data[0] = err;
967 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000968 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
969 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000971 if (anc_data[1]) {
972 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
973 msg_data(msg));
974 if (res)
975 return res;
976 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100977 }
978
979 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
981 switch (dest_type) {
982 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700983 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984 anc_data[0] = msg_nametype(msg);
985 anc_data[1] = msg_namelower(msg);
986 anc_data[2] = msg_namelower(msg);
987 break;
988 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700989 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990 anc_data[0] = msg_nametype(msg);
991 anc_data[1] = msg_namelower(msg);
992 anc_data[2] = msg_nameupper(msg);
993 break;
994 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700995 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996 anc_data[0] = tport->conn_type;
997 anc_data[1] = tport->conn_instance;
998 anc_data[2] = tport->conn_instance;
999 break;
1000 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001001 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 }
Allan Stephens2db99832010-12-31 18:59:33 +00001003 if (has_name) {
1004 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1005 if (res)
1006 return res;
1007 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008
1009 return 0;
1010}
1011
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001012static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001013{
1014 struct sock *sk = sock->sk;
1015 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001016 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001017 int err;
1018
1019 for (;;) {
1020 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001021 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001022 if (sock->state == SS_DISCONNECTING) {
1023 err = -ENOTCONN;
1024 break;
1025 }
1026 release_sock(sk);
1027 timeo = schedule_timeout(timeo);
1028 lock_sock(sk);
1029 }
1030 err = 0;
1031 if (!skb_queue_empty(&sk->sk_receive_queue))
1032 break;
1033 err = sock_intr_errno(timeo);
1034 if (signal_pending(current))
1035 break;
1036 err = -EAGAIN;
1037 if (!timeo)
1038 break;
1039 }
1040 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001041 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001042 return err;
1043}
1044
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001045/**
Ying Xue247f0f32014-02-18 16:06:46 +08001046 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001047 * @iocb: (unused)
1048 * @m: descriptor for message info
1049 * @buf_len: total size of user buffer area
1050 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001051 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1053 * If the complete message doesn't fit in user area, truncate it.
1054 *
1055 * Returns size of returned message data, errno otherwise
1056 */
Ying Xue247f0f32014-02-18 16:06:46 +08001057static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1058 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001060 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001061 struct tipc_sock *tsk = tipc_sk(sk);
1062 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001063 struct sk_buff *buf;
1064 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001065 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 unsigned int sz;
1067 u32 err;
1068 int res;
1069
Allan Stephens0c3141e2008-04-15 00:22:02 -07001070 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 if (unlikely(!buf_len))
1072 return -EINVAL;
1073
Allan Stephens0c3141e2008-04-15 00:22:02 -07001074 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001075
Allan Stephens0c3141e2008-04-15 00:22:02 -07001076 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077 res = -ENOTCONN;
1078 goto exit;
1079 }
1080
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001081 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001082restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083
Allan Stephens0c3141e2008-04-15 00:22:02 -07001084 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001085 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001086 if (res)
1087 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001088
1089 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001090 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001091 msg = buf_msg(buf);
1092 sz = msg_data_sz(msg);
1093 err = msg_errcode(msg);
1094
Per Lidenb97bf3f2006-01-02 19:04:38 +01001095 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001097 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001098 goto restart;
1099 }
1100
1101 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001102 set_orig_addr(m, msg);
1103
1104 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001105 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001106 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001107 goto exit;
1108
1109 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001110 if (!err) {
1111 if (unlikely(buf_len < sz)) {
1112 sz = buf_len;
1113 m->msg_flags |= MSG_TRUNC;
1114 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001115 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1116 m->msg_iov, sz);
1117 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001119 res = sz;
1120 } else {
1121 if ((sock->state == SS_READY) ||
1122 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1123 res = 0;
1124 else
1125 res = -ECONNRESET;
1126 }
1127
1128 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001130 if ((sock->state != SS_READY) &&
Jon Paul Maloy6163a192014-05-14 05:39:08 -04001131 (++port->conn_unacked >= TIPC_CONNACK_INTV))
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001132 tipc_acknowledge(port->ref, port->conn_unacked);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001133 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001134 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001136 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 return res;
1138}
1139
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001140/**
Ying Xue247f0f32014-02-18 16:06:46 +08001141 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142 * @iocb: (unused)
1143 * @m: descriptor for message info
1144 * @buf_len: total size of user buffer area
1145 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001146 *
1147 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 * will optionally wait for more; never truncates data.
1149 *
1150 * Returns size of returned message data, errno otherwise
1151 */
Ying Xue247f0f32014-02-18 16:06:46 +08001152static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1153 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001155 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001156 struct tipc_sock *tsk = tipc_sk(sk);
1157 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001158 struct sk_buff *buf;
1159 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001160 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001162 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001165 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166
1167 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 if (unlikely(!buf_len))
1169 return -EINVAL;
1170
Allan Stephens0c3141e2008-04-15 00:22:02 -07001171 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001173 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 res = -ENOTCONN;
1175 goto exit;
1176 }
1177
Florian Westphal3720d402010-08-17 11:00:04 +00001178 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001179 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001180
Allan Stephens0c3141e2008-04-15 00:22:02 -07001181restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001182 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001183 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001184 if (res)
1185 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001186
1187 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001188 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 msg = buf_msg(buf);
1190 sz = msg_data_sz(msg);
1191 err = msg_errcode(msg);
1192
1193 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001195 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 goto restart;
1197 }
1198
1199 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200 if (sz_copied == 0) {
1201 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001202 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001203 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001204 goto exit;
1205 }
1206
1207 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001209 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001210
Allan Stephens0232fd02011-02-21 09:45:40 -05001211 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001212 needed = (buf_len - sz_copied);
1213 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001214
1215 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1216 m->msg_iov, sz_to_copy);
1217 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001219
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 sz_copied += sz_to_copy;
1221
1222 if (sz_to_copy < sz) {
1223 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001224 TIPC_SKB_CB(buf)->handle =
1225 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226 goto exit;
1227 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 } else {
1229 if (sz_copied != 0)
1230 goto exit; /* can't add error msg to valid data */
1231
1232 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1233 res = 0;
1234 else
1235 res = -ECONNRESET;
1236 }
1237
1238 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001239 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy6163a192014-05-14 05:39:08 -04001240 if (unlikely(++port->conn_unacked >= TIPC_CONNACK_INTV))
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001241 tipc_acknowledge(port->ref, port->conn_unacked);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001242 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001243 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001244
1245 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001246 if ((sz_copied < buf_len) && /* didn't get all requested data */
1247 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001248 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001249 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1250 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001251 goto restart;
1252
1253exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001254 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001255 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001256}
1257
1258/**
Ying Xuef288bef2012-08-21 11:16:57 +08001259 * tipc_write_space - wake up thread if port congestion is released
1260 * @sk: socket
1261 */
1262static void tipc_write_space(struct sock *sk)
1263{
1264 struct socket_wq *wq;
1265
1266 rcu_read_lock();
1267 wq = rcu_dereference(sk->sk_wq);
1268 if (wq_has_sleeper(wq))
1269 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1270 POLLWRNORM | POLLWRBAND);
1271 rcu_read_unlock();
1272}
1273
1274/**
1275 * tipc_data_ready - wake up threads to indicate messages have been received
1276 * @sk: socket
1277 * @len: the length of messages
1278 */
David S. Miller676d2362014-04-11 16:15:36 -04001279static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001280{
1281 struct socket_wq *wq;
1282
1283 rcu_read_lock();
1284 wq = rcu_dereference(sk->sk_wq);
1285 if (wq_has_sleeper(wq))
1286 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1287 POLLRDNORM | POLLRDBAND);
1288 rcu_read_unlock();
1289}
1290
1291/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001292 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001293 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001294 * @msg: message
1295 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001296 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001297 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001298static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001299{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001300 struct sock *sk = &tsk->sk;
1301 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001302 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001303 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001304
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001305 int retval = -TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001306 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001307
1308 if (msg_mcast(msg))
1309 return retval;
1310
1311 switch ((int)sock->state) {
1312 case SS_CONNECTED:
1313 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001314 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001315 if (unlikely(msg_errcode(msg))) {
1316 sock->state = SS_DISCONNECTING;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001317 __tipc_port_disconnect(port);
Ying Xue7e6c1312012-11-29 18:39:14 -05001318 }
1319 retval = TIPC_OK;
1320 }
1321 break;
1322 case SS_CONNECTING:
1323 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001324 if (unlikely(msg_errcode(msg))) {
1325 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001326 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001327 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001328 break;
1329 }
1330
1331 if (unlikely(!msg_connected(msg)))
1332 break;
1333
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001334 res = auto_connect(tsk, msg);
Ying Xue584d24b2012-11-29 18:51:19 -05001335 if (res) {
1336 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001337 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001338 retval = TIPC_OK;
1339 break;
1340 }
1341
1342 /* If an incoming message is an 'ACK-', it should be
1343 * discarded here because it doesn't contain useful
1344 * data. In addition, we should try to wake up
1345 * connect() routine if sleeping.
1346 */
1347 if (msg_data_sz(msg) == 0) {
1348 kfree_skb(*buf);
1349 *buf = NULL;
1350 if (waitqueue_active(sk_sleep(sk)))
1351 wake_up_interruptible(sk_sleep(sk));
1352 }
1353 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001354 break;
1355 case SS_LISTENING:
1356 case SS_UNCONNECTED:
1357 /* Accept only SYN message */
1358 if (!msg_connected(msg) && !(msg_errcode(msg)))
1359 retval = TIPC_OK;
1360 break;
1361 case SS_DISCONNECTING:
1362 break;
1363 default:
1364 pr_err("Unknown socket state %u\n", sock->state);
1365 }
1366 return retval;
1367}
1368
1369/**
Ying Xueaba79f32013-01-20 23:30:09 +01001370 * rcvbuf_limit - get proper overload limit of socket receive queue
1371 * @sk: socket
1372 * @buf: message
1373 *
1374 * For all connection oriented messages, irrespective of importance,
1375 * the default overload value (i.e. 67MB) is set as limit.
1376 *
1377 * For all connectionless messages, by default new queue limits are
1378 * as belows:
1379 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001380 * TIPC_LOW_IMPORTANCE (4 MB)
1381 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1382 * TIPC_HIGH_IMPORTANCE (16 MB)
1383 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001384 *
1385 * Returns overload limit according to corresponding message importance
1386 */
1387static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1388{
1389 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001390
1391 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001392 return sysctl_tipc_rmem[2];
1393
1394 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1395 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001396}
1397
1398/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001399 * filter_rcv - validate incoming message
1400 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001401 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001402 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001403 * Enqueues message on receive queue if acceptable; optionally handles
1404 * disconnect indication for a connected socket.
1405 *
1406 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001407 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001408 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
1409 * to be rejected.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001410 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001411static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001412{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001413 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001414 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001415 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001416 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001417 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001419 if (unlikely(msg_user(msg) == CONN_MANAGER)) {
1420 tipc_port_proto_rcv(&tsk->port, buf);
1421 return TIPC_OK;
1422 }
1423
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001425 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001426 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001427
Per Lidenb97bf3f2006-01-02 19:04:38 +01001428 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001429 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001430 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001431 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001432 rc = filter_connect(tsk, &buf);
1433 if (rc != TIPC_OK || buf == NULL)
1434 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001435 }
1436
1437 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001438 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001439 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001440
Ying Xueaba79f32013-01-20 23:30:09 +01001441 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001442 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001443 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001444 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001445
David S. Miller676d2362014-04-11 16:15:36 -04001446 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001447 return TIPC_OK;
1448}
1449
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001450/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001451 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001452 * @sk: socket
1453 * @buf: message
1454 *
1455 * Caller must hold socket lock, but not port lock.
1456 *
1457 * Returns 0
1458 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001459static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001460{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001461 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001462 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001463 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001464 uint truesize = buf->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001465
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001466 rc = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001467
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001468 if (unlikely(rc && tipc_msg_reverse(buf, &onode, -rc)))
1469 tipc_link_xmit2(buf, onode, 0);
1470 else if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001471 atomic_add(truesize, &tsk->dupl_rcvcnt);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001472
Allan Stephens0c3141e2008-04-15 00:22:02 -07001473 return 0;
1474}
1475
1476/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001477 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001478 * @buf: buffer containing arriving message
1479 * Consumes buffer
1480 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001481 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001482int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001483{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001484 struct tipc_sock *tsk;
1485 struct tipc_port *port;
1486 struct sock *sk;
1487 u32 dport = msg_destport(buf_msg(buf));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001488 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001489 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001490 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001491
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001492 /* Validate destination and message */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001493 port = tipc_port_lock(dport);
1494 if (unlikely(!port)) {
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001495 rc = tipc_msg_eval(buf, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001496 goto exit;
1497 }
1498
1499 tsk = tipc_port_to_sock(port);
1500 sk = &tsk->sk;
1501
1502 /* Queue message */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001503 bh_lock_sock(sk);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001504
Allan Stephens0c3141e2008-04-15 00:22:02 -07001505 if (!sock_owned_by_user(sk)) {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001506 rc = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001507 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001508 if (sk->sk_backlog.len == 0)
1509 atomic_set(&tsk->dupl_rcvcnt, 0);
1510 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1511 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001512 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001513 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001514 bh_unlock_sock(sk);
1515 tipc_port_unlock(port);
1516
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001517 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001518 return 0;
1519exit:
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001520 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001521 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001522
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001523 tipc_link_xmit2(buf, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001524 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001525}
1526
Ying Xue78eb3a52014-01-17 09:50:03 +08001527static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1528{
1529 struct sock *sk = sock->sk;
1530 DEFINE_WAIT(wait);
1531 int done;
1532
1533 do {
1534 int err = sock_error(sk);
1535 if (err)
1536 return err;
1537 if (!*timeo_p)
1538 return -ETIMEDOUT;
1539 if (signal_pending(current))
1540 return sock_intr_errno(*timeo_p);
1541
1542 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1543 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1544 finish_wait(sk_sleep(sk), &wait);
1545 } while (!done);
1546 return 0;
1547}
1548
Per Lidenb97bf3f2006-01-02 19:04:38 +01001549/**
Ying Xue247f0f32014-02-18 16:06:46 +08001550 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001551 * @sock: socket structure
1552 * @dest: socket address for destination port
1553 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001554 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001555 *
1556 * Returns 0 on success, errno otherwise
1557 */
Ying Xue247f0f32014-02-18 16:06:46 +08001558static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1559 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001560{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001561 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001562 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1563 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001564 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1565 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001566 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001567
Allan Stephens0c3141e2008-04-15 00:22:02 -07001568 lock_sock(sk);
1569
Allan Stephensb89741a2008-04-15 00:20:37 -07001570 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001571 if (sock->state == SS_READY) {
1572 res = -EOPNOTSUPP;
1573 goto exit;
1574 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001575
Allan Stephensb89741a2008-04-15 00:20:37 -07001576 /*
1577 * Reject connection attempt using multicast address
1578 *
1579 * Note: send_msg() validates the rest of the address fields,
1580 * so there's no need to do it here
1581 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001582 if (dst->addrtype == TIPC_ADDR_MCAST) {
1583 res = -EINVAL;
1584 goto exit;
1585 }
1586
Ying Xue78eb3a52014-01-17 09:50:03 +08001587 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001588 switch (sock->state) {
1589 case SS_UNCONNECTED:
1590 /* Send a 'SYN-' to destination */
1591 m.msg_name = dest;
1592 m.msg_namelen = destlen;
1593
1594 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1595 * indicate send_msg() is never blocked.
1596 */
1597 if (!timeout)
1598 m.msg_flags = MSG_DONTWAIT;
1599
Ying Xue247f0f32014-02-18 16:06:46 +08001600 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001601 if ((res < 0) && (res != -EWOULDBLOCK))
1602 goto exit;
1603
1604 /* Just entered SS_CONNECTING state; the only
1605 * difference is that return value in non-blocking
1606 * case is EINPROGRESS, rather than EALREADY.
1607 */
1608 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001609 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001610 if (previous == SS_CONNECTING)
1611 res = -EALREADY;
1612 if (!timeout)
1613 goto exit;
1614 timeout = msecs_to_jiffies(timeout);
1615 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1616 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001617 break;
1618 case SS_CONNECTED:
1619 res = -EISCONN;
1620 break;
1621 default:
1622 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001623 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001624 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001625exit:
1626 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001627 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001628}
1629
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001630/**
Ying Xue247f0f32014-02-18 16:06:46 +08001631 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001632 * @sock: socket structure
1633 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001634 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001635 * Returns 0 on success, errno otherwise
1636 */
Ying Xue247f0f32014-02-18 16:06:46 +08001637static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001638{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001639 struct sock *sk = sock->sk;
1640 int res;
1641
1642 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001643
Ying Xue245f3d32011-07-06 06:01:13 -04001644 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001645 res = -EINVAL;
1646 else {
1647 sock->state = SS_LISTENING;
1648 res = 0;
1649 }
1650
1651 release_sock(sk);
1652 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001653}
1654
Ying Xue6398e232014-01-17 09:50:04 +08001655static int tipc_wait_for_accept(struct socket *sock, long timeo)
1656{
1657 struct sock *sk = sock->sk;
1658 DEFINE_WAIT(wait);
1659 int err;
1660
1661 /* True wake-one mechanism for incoming connections: only
1662 * one process gets woken up, not the 'whole herd'.
1663 * Since we do not 'race & poll' for established sockets
1664 * anymore, the common case will execute the loop only once.
1665 */
1666 for (;;) {
1667 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1668 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001669 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001670 release_sock(sk);
1671 timeo = schedule_timeout(timeo);
1672 lock_sock(sk);
1673 }
1674 err = 0;
1675 if (!skb_queue_empty(&sk->sk_receive_queue))
1676 break;
1677 err = -EINVAL;
1678 if (sock->state != SS_LISTENING)
1679 break;
1680 err = sock_intr_errno(timeo);
1681 if (signal_pending(current))
1682 break;
1683 err = -EAGAIN;
1684 if (!timeo)
1685 break;
1686 }
1687 finish_wait(sk_sleep(sk), &wait);
1688 return err;
1689}
1690
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001691/**
Ying Xue247f0f32014-02-18 16:06:46 +08001692 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001693 * @sock: listening socket
1694 * @newsock: new socket that is to be connected
1695 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001696 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001697 * Returns 0 on success, errno otherwise
1698 */
Ying Xue247f0f32014-02-18 16:06:46 +08001699static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001700{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001701 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001703 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001704 struct tipc_msg *msg;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001705 struct tipc_portid peer;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001706 u32 new_ref;
Ying Xue6398e232014-01-17 09:50:04 +08001707 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001708 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001709
Allan Stephens0c3141e2008-04-15 00:22:02 -07001710 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001711
Allan Stephens0c3141e2008-04-15 00:22:02 -07001712 if (sock->state != SS_LISTENING) {
1713 res = -EINVAL;
1714 goto exit;
1715 }
Ying Xue6398e232014-01-17 09:50:04 +08001716 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1717 res = tipc_wait_for_accept(sock, timeo);
1718 if (res)
1719 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001720
1721 buf = skb_peek(&sk->sk_receive_queue);
1722
Ying Xuec5fa7b32013-06-17 10:54:39 -04001723 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001724 if (res)
1725 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001726
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001727 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001728 new_port = &tipc_sk(new_sk)->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001729 new_ref = new_port->ref;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001730 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001731
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001732 /* we lock on new_sk; but lockdep sees the lock on sk */
1733 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001734
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001735 /*
1736 * Reject any stray messages received by new socket
1737 * before the socket lock was taken (very, very unlikely)
1738 */
1739 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001740
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001741 /* Connect new socket to it's peer */
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001742 peer.ref = msg_origport(msg);
1743 peer.node = msg_orignode(msg);
1744 tipc_port_connect(new_ref, &peer);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001745 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001747 tipc_port_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001748 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001749 new_port->conn_type = msg_nametype(msg);
1750 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001751 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001752
1753 /*
1754 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1755 * Respond to 'SYN+' by queuing it on new socket.
1756 */
1757 if (!msg_data_sz(msg)) {
1758 struct msghdr m = {NULL,};
1759
1760 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001761 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001762 } else {
1763 __skb_dequeue(&sk->sk_receive_queue);
1764 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001765 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001766 }
1767 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001768exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001769 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001770 return res;
1771}
1772
1773/**
Ying Xue247f0f32014-02-18 16:06:46 +08001774 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001775 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001776 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001777 *
1778 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001779 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001780 * Returns 0 on success, errno otherwise
1781 */
Ying Xue247f0f32014-02-18 16:06:46 +08001782static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001783{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001784 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001785 struct tipc_sock *tsk = tipc_sk(sk);
1786 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001788 u32 peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001789 int res;
1790
Allan Stephense247a8f2008-03-06 15:05:38 -08001791 if (how != SHUT_RDWR)
1792 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793
Allan Stephens0c3141e2008-04-15 00:22:02 -07001794 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001795
1796 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001797 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001798 case SS_CONNECTED:
1799
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001801 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001802 buf = __skb_dequeue(&sk->sk_receive_queue);
1803 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001804 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001805 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001806 goto restart;
1807 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001808 tipc_port_disconnect(port->ref);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001809 if (tipc_msg_reverse(buf, &peer, TIPC_CONN_SHUTDOWN))
1810 tipc_link_xmit2(buf, peer, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001811 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001812 tipc_port_shutdown(port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001813 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001814
1815 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816
1817 /* fall through */
1818
1819 case SS_DISCONNECTING:
1820
Ying Xue75031152012-10-29 09:38:15 -04001821 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001822 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001823
1824 /* Wake up anyone sleeping in poll */
1825 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826 res = 0;
1827 break;
1828
1829 default:
1830 res = -ENOTCONN;
1831 }
1832
Allan Stephens0c3141e2008-04-15 00:22:02 -07001833 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834 return res;
1835}
1836
1837/**
Ying Xue247f0f32014-02-18 16:06:46 +08001838 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001839 * @sock: socket structure
1840 * @lvl: option level
1841 * @opt: option identifier
1842 * @ov: pointer to new option value
1843 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001844 *
1845 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001846 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001847 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001848 * Returns 0 on success, errno otherwise
1849 */
Ying Xue247f0f32014-02-18 16:06:46 +08001850static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1851 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001852{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001853 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001854 struct tipc_sock *tsk = tipc_sk(sk);
1855 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001856 u32 value;
1857 int res;
1858
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001859 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1860 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001861 if (lvl != SOL_TIPC)
1862 return -ENOPROTOOPT;
1863 if (ol < sizeof(value))
1864 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001865 res = get_user(value, (u32 __user *)ov);
1866 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001867 return res;
1868
Allan Stephens0c3141e2008-04-15 00:22:02 -07001869 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001870
Per Lidenb97bf3f2006-01-02 19:04:38 +01001871 switch (opt) {
1872 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001873 tipc_port_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001874 break;
1875 case TIPC_SRC_DROPPABLE:
1876 if (sock->type != SOCK_STREAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001877 tipc_port_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001878 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001879 res = -ENOPROTOOPT;
1880 break;
1881 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001882 tipc_port_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001883 break;
1884 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001885 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001886 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001887 break;
1888 default:
1889 res = -EINVAL;
1890 }
1891
Allan Stephens0c3141e2008-04-15 00:22:02 -07001892 release_sock(sk);
1893
Per Lidenb97bf3f2006-01-02 19:04:38 +01001894 return res;
1895}
1896
1897/**
Ying Xue247f0f32014-02-18 16:06:46 +08001898 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001899 * @sock: socket structure
1900 * @lvl: option level
1901 * @opt: option identifier
1902 * @ov: receptacle for option value
1903 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001904 *
1905 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001906 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001907 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001908 * Returns 0 on success, errno otherwise
1909 */
Ying Xue247f0f32014-02-18 16:06:46 +08001910static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1911 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001912{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001913 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001914 struct tipc_sock *tsk = tipc_sk(sk);
1915 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001916 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001917 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001918 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001919
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001920 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1921 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001922 if (lvl != SOL_TIPC)
1923 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001924 res = get_user(len, ol);
1925 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001926 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001927
Allan Stephens0c3141e2008-04-15 00:22:02 -07001928 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001929
1930 switch (opt) {
1931 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001932 value = tipc_port_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001933 break;
1934 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001935 value = tipc_port_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001936 break;
1937 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001938 value = tipc_port_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001939 break;
1940 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001941 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001942 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001943 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001944 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001945 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001946 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001947 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001948 value = skb_queue_len(&sk->sk_receive_queue);
1949 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001950 default:
1951 res = -EINVAL;
1952 }
1953
Allan Stephens0c3141e2008-04-15 00:22:02 -07001954 release_sock(sk);
1955
Paul Gortmaker25860c32010-12-31 18:59:31 +00001956 if (res)
1957 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001958
Paul Gortmaker25860c32010-12-31 18:59:31 +00001959 if (len < sizeof(value))
1960 return -EINVAL;
1961
1962 if (copy_to_user(ov, &value, sizeof(value)))
1963 return -EFAULT;
1964
1965 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001966}
1967
Erik Hugne78acb1f2014-04-24 16:26:47 +02001968int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
1969{
1970 struct tipc_sioc_ln_req lnr;
1971 void __user *argp = (void __user *)arg;
1972
1973 switch (cmd) {
1974 case SIOCGETLINKNAME:
1975 if (copy_from_user(&lnr, argp, sizeof(lnr)))
1976 return -EFAULT;
1977 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
1978 lnr.linkname, TIPC_MAX_LINK_NAME)) {
1979 if (copy_to_user(argp, &lnr, sizeof(lnr)))
1980 return -EFAULT;
1981 return 0;
1982 }
1983 return -EADDRNOTAVAIL;
1984 break;
1985 default:
1986 return -ENOIOCTLCMD;
1987 }
1988}
1989
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001990/* Protocol switches for the various types of TIPC sockets */
1991
Florian Westphalbca65ea2008-02-07 18:18:01 -08001992static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001993 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001994 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001995 .release = tipc_release,
1996 .bind = tipc_bind,
1997 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001998 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001999 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002000 .getname = tipc_getname,
2001 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002002 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002003 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002004 .shutdown = tipc_shutdown,
2005 .setsockopt = tipc_setsockopt,
2006 .getsockopt = tipc_getsockopt,
2007 .sendmsg = tipc_sendmsg,
2008 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002009 .mmap = sock_no_mmap,
2010 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002011};
2012
Florian Westphalbca65ea2008-02-07 18:18:01 -08002013static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002014 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002015 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002016 .release = tipc_release,
2017 .bind = tipc_bind,
2018 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002019 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002020 .accept = tipc_accept,
2021 .getname = tipc_getname,
2022 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002023 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002024 .listen = tipc_listen,
2025 .shutdown = tipc_shutdown,
2026 .setsockopt = tipc_setsockopt,
2027 .getsockopt = tipc_getsockopt,
2028 .sendmsg = tipc_send_packet,
2029 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002030 .mmap = sock_no_mmap,
2031 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002032};
2033
Florian Westphalbca65ea2008-02-07 18:18:01 -08002034static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002035 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002036 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002037 .release = tipc_release,
2038 .bind = tipc_bind,
2039 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002040 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002041 .accept = tipc_accept,
2042 .getname = tipc_getname,
2043 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002044 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002045 .listen = tipc_listen,
2046 .shutdown = tipc_shutdown,
2047 .setsockopt = tipc_setsockopt,
2048 .getsockopt = tipc_getsockopt,
2049 .sendmsg = tipc_send_stream,
2050 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002051 .mmap = sock_no_mmap,
2052 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002053};
2054
Florian Westphalbca65ea2008-02-07 18:18:01 -08002055static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002056 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002057 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002058 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002059};
2060
2061static struct proto tipc_proto = {
2062 .name = "TIPC",
2063 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002064 .obj_size = sizeof(struct tipc_sock),
2065 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002066};
2067
Ying Xuec5fa7b32013-06-17 10:54:39 -04002068static struct proto tipc_proto_kern = {
2069 .name = "TIPC",
2070 .obj_size = sizeof(struct tipc_sock),
2071 .sysctl_rmem = sysctl_tipc_rmem
2072};
2073
Per Lidenb97bf3f2006-01-02 19:04:38 +01002074/**
Per Liden4323add2006-01-18 00:38:21 +01002075 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002076 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002077 * Returns 0 on success, errno otherwise
2078 */
Per Liden4323add2006-01-18 00:38:21 +01002079int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002080{
2081 int res;
2082
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002083 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002084 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002085 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002086 goto out;
2087 }
2088
2089 res = sock_register(&tipc_family_ops);
2090 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002091 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002092 proto_unregister(&tipc_proto);
2093 goto out;
2094 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002095 out:
2096 return res;
2097}
2098
2099/**
Per Liden4323add2006-01-18 00:38:21 +01002100 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002101 */
Per Liden4323add2006-01-18 00:38:21 +01002102void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002103{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002104 sock_unregister(tipc_family_ops.family);
2105 proto_unregister(&tipc_proto);
2106}