blob: ac08966f285867673b55b62a83d7f9beb57f2a4c [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04002* net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04004 * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000038#include "port.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020039#include "node.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040
Erik Hugne2cf8aa12012-06-29 00:16:37 -040041#include <linux/export.h>
Erik Hugne2cf8aa12012-06-29 00:16:37 -040042
Per Lidenb97bf3f2006-01-02 19:04:38 +010043#define SS_LISTENING -1 /* socket is listening */
44#define SS_READY -2 /* socket is connectionless */
45
Allan Stephens3654ea02008-04-13 21:35:11 -070046#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010047
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -040048static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -040049static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +080050static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080051static int tipc_release(struct socket *sock);
52static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Per Lidenb97bf3f2006-01-02 19:04:38 +010053
Florian Westphalbca65ea2008-02-07 18:18:01 -080054static const struct proto_ops packet_ops;
55static const struct proto_ops stream_ops;
56static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010057
58static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040059static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010060
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090061/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070062 * Revised TIPC socket locking policy:
63 *
64 * Most socket operations take the standard socket lock when they start
65 * and hold it until they finish (or until they need to sleep). Acquiring
66 * this lock grants the owner exclusive access to the fields of the socket
67 * data structures, with the exception of the backlog queue. A few socket
68 * operations can be done without taking the socket lock because they only
69 * read socket information that never changes during the life of the socket.
70 *
71 * Socket operations may acquire the lock for the associated TIPC port if they
72 * need to perform an operation on the port. If any routine needs to acquire
73 * both the socket lock and the port lock it must take the socket lock first
74 * to avoid the risk of deadlock.
75 *
76 * The dispatcher handling incoming messages cannot grab the socket lock in
77 * the standard fashion, since invoked it runs at the BH level and cannot block.
78 * Instead, it checks to see if the socket lock is currently owned by someone,
79 * and either handles the message itself or adds it to the socket's backlog
80 * queue; in the latter case the queued message is processed once the process
81 * owning the socket lock releases it.
82 *
83 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
84 * the problem of a blocked socket operation preventing any other operations
85 * from occurring. However, applications must be careful if they have
86 * multiple threads trying to send (or receive) on the same socket, as these
87 * operations might interfere with each other. For example, doing a connect
88 * and a receive at the same time might allow the receive to consume the
89 * ACK message meant for the connect. While additional work could be done
90 * to try and overcome this, it doesn't seem to be worthwhile at the present.
91 *
92 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
93 * that another operation that must be performed in a non-blocking manner is
94 * not delayed for very long because the lock has already been taken.
95 *
96 * NOTE: This code assumes that certain fields of a port/socket pair are
97 * constant over its lifetime; such fields can be examined without taking
98 * the socket lock and/or port lock, and do not need to be re-read even
99 * after resuming processing after waiting. These fields include:
100 * - socket type
101 * - pointer to socket sk structure (aka tipc_sock structure)
102 * - pointer to port structure
103 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400106#include "socket.h"
107
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700109 * advance_rx_queue - discard first buffer in socket receive queue
110 *
111 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700113static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400115 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116}
117
118/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700119 * reject_rx_queue - reject all buffers in socket receive queue
120 *
121 * Caller must hold socket lock
122 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700123static void reject_rx_queue(struct sock *sk)
124{
125 struct sk_buff *buf;
126
Ying Xue9da3d472012-11-27 06:15:27 -0500127 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700128 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700129}
130
131/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400132 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700133 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100134 * @sock: pre-allocated socket structure
135 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800136 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900137 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700138 * This routine creates additional data structures used by the TIPC socket,
139 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140 *
141 * Returns 0 on success, errno otherwise
142 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400143static int tipc_sk_create(struct net *net, struct socket *sock,
144 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700146 const struct proto_ops *ops;
147 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400149 struct tipc_sock *tsk;
150 struct tipc_port *port;
151 u32 ref;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700152
153 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154 if (unlikely(protocol != 0))
155 return -EPROTONOSUPPORT;
156
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 switch (sock->type) {
158 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700159 ops = &stream_ops;
160 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161 break;
162 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700163 ops = &packet_ops;
164 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 break;
166 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700168 ops = &msg_ops;
169 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 break;
Allan Stephens49978652006-06-25 23:47:18 -0700171 default:
Allan Stephens49978652006-06-25 23:47:18 -0700172 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 }
174
Allan Stephens0c3141e2008-04-15 00:22:02 -0700175 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400176 if (!kern)
177 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
178 else
179 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
180
Allan Stephens0c3141e2008-04-15 00:22:02 -0700181 if (sk == NULL)
182 return -ENOMEM;
183
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400184 tsk = tipc_sk(sk);
185 port = &tsk->port;
186
187 ref = tipc_port_init(port, TIPC_LOW_IMPORTANCE);
188 if (!ref) {
189 pr_warn("Socket registration failed, ref. table exhausted\n");
Allan Stephens0c3141e2008-04-15 00:22:02 -0700190 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191 return -ENOMEM;
192 }
193
Allan Stephens0c3141e2008-04-15 00:22:02 -0700194 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700195 sock->ops = ops;
196 sock->state = state;
197
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198 sock_init_data(sock, sk);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400199 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400200 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800201 sk->sk_data_ready = tipc_data_ready;
202 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400203 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
204 atomic_set(&tsk->dupl_rcvcnt, 0);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400205 tipc_port_unlock(port);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700206
Allan Stephens0c3141e2008-04-15 00:22:02 -0700207 if (sock->state == SS_READY) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400208 tipc_port_set_unreturnable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700209 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400210 tipc_port_set_unreliable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700211 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 return 0;
213}
214
215/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400216 * tipc_sock_create_local - create TIPC socket from inside TIPC module
217 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
218 *
219 * We cannot use sock_creat_kern here because it bumps module user count.
220 * Since socket owner and creator is the same module we must make sure
221 * that module count remains zero for module local sockets, otherwise
222 * we cannot do rmmod.
223 *
224 * Returns 0 on success, errno otherwise
225 */
226int tipc_sock_create_local(int type, struct socket **res)
227{
228 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400229
230 rc = sock_create_lite(AF_TIPC, type, 0, res);
231 if (rc < 0) {
232 pr_err("Failed to create kernel socket\n");
233 return rc;
234 }
235 tipc_sk_create(&init_net, *res, 0, 1);
236
Ying Xuec5fa7b32013-06-17 10:54:39 -0400237 return 0;
238}
239
240/**
241 * tipc_sock_release_local - release socket created by tipc_sock_create_local
242 * @sock: the socket to be released.
243 *
244 * Module reference count is not incremented when such sockets are created,
245 * so we must keep it from being decremented when they are released.
246 */
247void tipc_sock_release_local(struct socket *sock)
248{
Ying Xue247f0f32014-02-18 16:06:46 +0800249 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400250 sock->ops = NULL;
251 sock_release(sock);
252}
253
254/**
255 * tipc_sock_accept_local - accept a connection on a socket created
256 * with tipc_sock_create_local. Use this function to avoid that
257 * module reference count is inadvertently incremented.
258 *
259 * @sock: the accepting socket
260 * @newsock: reference to the new socket to be created
261 * @flags: socket flags
262 */
263
264int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400265 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400266{
267 struct sock *sk = sock->sk;
268 int ret;
269
270 ret = sock_create_lite(sk->sk_family, sk->sk_type,
271 sk->sk_protocol, newsock);
272 if (ret < 0)
273 return ret;
274
Ying Xue247f0f32014-02-18 16:06:46 +0800275 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400276 if (ret < 0) {
277 sock_release(*newsock);
278 return ret;
279 }
280 (*newsock)->ops = sock->ops;
281 return ret;
282}
283
284/**
Ying Xue247f0f32014-02-18 16:06:46 +0800285 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286 * @sock: socket to destroy
287 *
288 * This routine cleans up any messages that are still queued on the socket.
289 * For DGRAM and RDM socket types, all queued messages are rejected.
290 * For SEQPACKET and STREAM socket types, the first message is rejected
291 * and any others are discarded. (If the first message on a STREAM socket
292 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900293 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 * NOTE: Rejected messages are not necessarily returned to the sender! They
295 * are returned or discarded according to the "destination droppable" setting
296 * specified for the message by the sender.
297 *
298 * Returns 0 on success, errno otherwise
299 */
Ying Xue247f0f32014-02-18 16:06:46 +0800300static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400303 struct tipc_sock *tsk;
304 struct tipc_port *port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305 struct sk_buff *buf;
306
Allan Stephens0c3141e2008-04-15 00:22:02 -0700307 /*
308 * Exit if socket isn't fully initialized (occurs when a failed accept()
309 * releases a pre-allocated child socket that was never used)
310 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700311 if (sk == NULL)
312 return 0;
313
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400314 tsk = tipc_sk(sk);
315 port = &tsk->port;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700316 lock_sock(sk);
317
318 /*
319 * Reject all unreceived messages, except on an active connection
320 * (which disconnects locally & sends a 'FIN+' to peer)
321 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700323 buf = __skb_dequeue(&sk->sk_receive_queue);
324 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 break;
Ying Xue40682432013-10-18 07:23:16 +0200326 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400327 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700328 else {
329 if ((sock->state == SS_CONNECTING) ||
330 (sock->state == SS_CONNECTED)) {
331 sock->state = SS_DISCONNECTING;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400332 tipc_port_disconnect(port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700333 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700335 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336 }
337
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400338 /* Destroy TIPC port; also disconnects an active connection and
339 * sends a 'FIN-' to peer.
Allan Stephens0c3141e2008-04-15 00:22:02 -0700340 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400341 tipc_port_destroy(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342
Allan Stephens0c3141e2008-04-15 00:22:02 -0700343 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100344 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345
Allan Stephens0c3141e2008-04-15 00:22:02 -0700346 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700347 sock->state = SS_DISCONNECTING;
348 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349
350 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700351 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200353 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354}
355
356/**
Ying Xue247f0f32014-02-18 16:06:46 +0800357 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358 * @sock: socket structure
359 * @uaddr: socket address describing name(s) and desired operation
360 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900361 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362 * Name and name sequence binding is indicated using a positive scope value;
363 * a negative scope value unbinds the specified name. Specifying no name
364 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900365 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700367 *
368 * NOTE: This routine doesn't need to take the socket lock since it doesn't
369 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100370 */
Ying Xue247f0f32014-02-18 16:06:46 +0800371static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
372 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373{
Ying Xue84602762013-12-27 10:18:28 +0800374 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400376 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800377 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378
Ying Xue84602762013-12-27 10:18:28 +0800379 lock_sock(sk);
380 if (unlikely(!uaddr_len)) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400381 res = tipc_withdraw(&tsk->port, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800382 goto exit;
383 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900384
Ying Xue84602762013-12-27 10:18:28 +0800385 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
386 res = -EINVAL;
387 goto exit;
388 }
389 if (addr->family != AF_TIPC) {
390 res = -EAFNOSUPPORT;
391 goto exit;
392 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394 if (addr->addrtype == TIPC_ADDR_NAME)
395 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800396 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
397 res = -EAFNOSUPPORT;
398 goto exit;
399 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900400
Ying Xue13a2e892013-06-17 10:54:40 -0400401 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400402 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800403 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
404 res = -EACCES;
405 goto exit;
406 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400407
Ying Xue84602762013-12-27 10:18:28 +0800408 res = (addr->scope > 0) ?
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400409 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
410 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800411exit:
412 release_sock(sk);
413 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414}
415
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900416/**
Ying Xue247f0f32014-02-18 16:06:46 +0800417 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100418 * @sock: socket structure
419 * @uaddr: area for returned socket address
420 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700421 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900422 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700424 *
Allan Stephens2da59912008-07-14 22:43:32 -0700425 * NOTE: This routine doesn't need to take the socket lock since it only
426 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000427 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428 */
Ying Xue247f0f32014-02-18 16:06:46 +0800429static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
430 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400433 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000435 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700436 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700437 if ((sock->state != SS_CONNECTED) &&
438 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
439 return -ENOTCONN;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400440 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
441 addr->addr.id.node = tipc_port_peernode(&tsk->port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700442 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400443 addr->addr.id.ref = tsk->port.ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000444 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700445 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446
447 *uaddr_len = sizeof(*addr);
448 addr->addrtype = TIPC_ADDR_ID;
449 addr->family = AF_TIPC;
450 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451 addr->addr.name.domain = 0;
452
Allan Stephens0c3141e2008-04-15 00:22:02 -0700453 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454}
455
456/**
Ying Xue247f0f32014-02-18 16:06:46 +0800457 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458 * @file: file structure associated with the socket
459 * @sock: socket for which to calculate the poll bits
460 * @wait: ???
461 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700462 * Returns pollmask value
463 *
464 * COMMENTARY:
465 * It appears that the usual socket locking mechanisms are not useful here
466 * since the pollmask info is potentially out-of-date the moment this routine
467 * exits. TCP and other protocols seem to rely on higher level poll routines
468 * to handle any preventable race conditions, so TIPC will do the same ...
469 *
470 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700471 *
Allan Stephensf662c072010-08-17 11:00:06 +0000472 * socket state flags set
473 * ------------ ---------
474 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200475 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000476 *
477 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
478 * no write flags
479 *
480 * connected POLLIN/POLLRDNORM if data in rx queue
481 * POLLOUT if port is not congested
482 *
483 * disconnecting POLLIN/POLLRDNORM/POLLHUP
484 * no write flags
485 *
486 * listening POLLIN if SYN in rx queue
487 * no write flags
488 *
489 * ready POLLIN/POLLRDNORM if data in rx queue
490 * [connectionless] POLLOUT (since port cannot be congested)
491 *
492 * IMPORTANT: The fact that a read or write operation is indicated does NOT
493 * imply that the operation will succeed, merely that it should be performed
494 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495 */
Ying Xue247f0f32014-02-18 16:06:46 +0800496static unsigned int tipc_poll(struct file *file, struct socket *sock,
497 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498{
Allan Stephens9b674e82008-03-26 16:48:21 -0700499 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400500 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000501 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700502
Ying Xuef288bef2012-08-21 11:16:57 +0800503 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700504
Allan Stephensf662c072010-08-17 11:00:06 +0000505 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200506 case SS_UNCONNECTED:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400507 if (!tsk->port.congested)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200508 mask |= POLLOUT;
509 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000510 case SS_READY:
511 case SS_CONNECTED:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400512 if (!tsk->port.congested)
Allan Stephensf662c072010-08-17 11:00:06 +0000513 mask |= POLLOUT;
514 /* fall thru' */
515 case SS_CONNECTING:
516 case SS_LISTENING:
517 if (!skb_queue_empty(&sk->sk_receive_queue))
518 mask |= (POLLIN | POLLRDNORM);
519 break;
520 case SS_DISCONNECTING:
521 mask = (POLLIN | POLLRDNORM | POLLHUP);
522 break;
523 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700524
525 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526}
527
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900528/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529 * dest_name_check - verify user is permitted to send to specified port name
530 * @dest: destination address
531 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900532 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533 * Prevents restricted configuration commands from being issued by
534 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900535 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536 * Returns 0 if permission is granted, otherwise errno
537 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800538static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539{
540 struct tipc_cfg_msg_hdr hdr;
541
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900542 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
543 return 0;
544 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
545 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900546 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
547 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548
Allan Stephens3f8dd942011-01-18 13:09:29 -0500549 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
550 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900551 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700553 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900555
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556 return 0;
557}
558
Ying Xue3f405042014-01-17 09:50:05 +0800559static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
560{
561 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400562 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800563 DEFINE_WAIT(wait);
564 int done;
565
566 do {
567 int err = sock_error(sk);
568 if (err)
569 return err;
570 if (sock->state == SS_DISCONNECTING)
571 return -EPIPE;
572 if (!*timeo_p)
573 return -EAGAIN;
574 if (signal_pending(current))
575 return sock_intr_errno(*timeo_p);
576
577 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400578 done = sk_wait_event(sk, timeo_p, !tsk->port.congested);
Ying Xue3f405042014-01-17 09:50:05 +0800579 finish_wait(sk_sleep(sk), &wait);
580 } while (!done);
581 return 0;
582}
583
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400584
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585/**
Ying Xue247f0f32014-02-18 16:06:46 +0800586 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700587 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588 * @sock: socket structure
589 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700590 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900591 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900593 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
595 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900596 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597 * Returns the number of bytes sent on success, or errno otherwise
598 */
Ying Xue247f0f32014-02-18 16:06:46 +0800599static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
600 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700602 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400603 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400604 struct tipc_port *port = &tsk->port;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100605 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 int needs_conn;
Ying Xue3f405042014-01-17 09:50:05 +0800607 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 int res = -EINVAL;
609
610 if (unlikely(!dest))
611 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700612 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
613 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 return -EINVAL;
Ying Xue97f8b872013-01-31 21:51:47 +0100615 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400616 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617
Allan Stephens0c3141e2008-04-15 00:22:02 -0700618 if (iocb)
619 lock_sock(sk);
620
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621 needs_conn = (sock->state != SS_READY);
622 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700623 if (sock->state == SS_LISTENING) {
624 res = -EPIPE;
625 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700626 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700627 if (sock->state != SS_UNCONNECTED) {
628 res = -EISCONN;
629 goto exit;
630 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400631 if (tsk->port.published) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700632 res = -EOPNOTSUPP;
633 goto exit;
634 }
635 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400636 tsk->port.conn_type = dest->addr.name.name.type;
637 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700638 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100639
640 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700641 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 }
643
Ying Xue3f405042014-01-17 09:50:05 +0800644 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900645 do {
646 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000647 res = dest_name_check(dest, m);
648 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700649 break;
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400650 res = tipc_send2name(port,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900651 &dest->addr.name.name,
652 dest->addr.name.domain,
Allan Stephens26896902011-04-21 10:42:07 -0500653 m->msg_iov,
654 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000655 } else if (dest->addrtype == TIPC_ADDR_ID) {
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400656 res = tipc_send2port(port,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900657 &dest->addr.id,
Allan Stephens26896902011-04-21 10:42:07 -0500658 m->msg_iov,
659 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000660 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661 if (needs_conn) {
662 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700663 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664 }
Allan Stephens2db99832010-12-31 18:59:33 +0000665 res = dest_name_check(dest, m);
666 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700667 break;
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400668 res = tipc_port_mcast_xmit(port,
Ying Xue247f0f32014-02-18 16:06:46 +0800669 &dest->addr.nameseq,
670 m->msg_iov,
671 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900672 }
673 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000674 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700675 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700676 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900677 }
Ying Xue3f405042014-01-17 09:50:05 +0800678 res = tipc_wait_for_sndmsg(sock, &timeo);
679 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700680 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900681 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700682
683exit:
684 if (iocb)
685 release_sock(sk);
686 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100687}
688
Ying Xue391a6dd2014-01-17 09:50:06 +0800689static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
690{
691 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400692 struct tipc_sock *tsk = tipc_sk(sk);
693 struct tipc_port *port = &tsk->port;
Ying Xue391a6dd2014-01-17 09:50:06 +0800694 DEFINE_WAIT(wait);
695 int done;
696
697 do {
698 int err = sock_error(sk);
699 if (err)
700 return err;
701 if (sock->state == SS_DISCONNECTING)
702 return -EPIPE;
703 else if (sock->state != SS_CONNECTED)
704 return -ENOTCONN;
705 if (!*timeo_p)
706 return -EAGAIN;
707 if (signal_pending(current))
708 return sock_intr_errno(*timeo_p);
709
710 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
711 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400712 (!port->congested || !port->connected));
Ying Xue391a6dd2014-01-17 09:50:06 +0800713 finish_wait(sk_sleep(sk), &wait);
714 } while (!done);
715 return 0;
716}
717
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900718/**
Ying Xue247f0f32014-02-18 16:06:46 +0800719 * tipc_send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700720 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100721 * @sock: socket structure
722 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700723 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900724 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100725 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900726 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100727 * Returns the number of bytes sent on success, or errno otherwise
728 */
Ying Xue247f0f32014-02-18 16:06:46 +0800729static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
730 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700732 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400733 struct tipc_sock *tsk = tipc_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100734 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue391a6dd2014-01-17 09:50:06 +0800735 int res = -EINVAL;
736 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737
738 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100739 if (unlikely(dest))
Ying Xue247f0f32014-02-18 16:06:46 +0800740 return tipc_sendmsg(iocb, sock, m, total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741
Ying Xue97f8b872013-01-31 21:51:47 +0100742 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400743 return -EMSGSIZE;
744
Allan Stephens0c3141e2008-04-15 00:22:02 -0700745 if (iocb)
746 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747
Ying Xue391a6dd2014-01-17 09:50:06 +0800748 if (unlikely(sock->state != SS_CONNECTED)) {
749 if (sock->state == SS_DISCONNECTING)
750 res = -EPIPE;
751 else
752 res = -ENOTCONN;
753 goto exit;
754 }
Ying Xue1d835872011-07-06 05:53:15 -0400755
Ying Xue391a6dd2014-01-17 09:50:06 +0800756 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900757 do {
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400758 res = tipc_send(&tsk->port, m->msg_iov, total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000759 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700760 break;
Ying Xue391a6dd2014-01-17 09:50:06 +0800761 res = tipc_wait_for_sndpkt(sock, &timeo);
762 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700763 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900764 } while (1);
Ying Xue391a6dd2014-01-17 09:50:06 +0800765exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700766 if (iocb)
767 release_sock(sk);
768 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100769}
770
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900771/**
Ying Xue247f0f32014-02-18 16:06:46 +0800772 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 * @iocb: (unused)
774 * @sock: socket structure
775 * @m: data to send
776 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900777 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900779 *
780 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700781 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782 */
Ying Xue247f0f32014-02-18 16:06:46 +0800783static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
784 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700786 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400787 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788 struct msghdr my_msg;
789 struct iovec my_iov;
790 struct iovec *curr_iov;
791 int curr_iovlen;
792 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700793 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794 int curr_left;
795 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700796 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100797 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900798
Allan Stephens0c3141e2008-04-15 00:22:02 -0700799 lock_sock(sk);
800
Allan Stephens05646c92007-06-10 17:25:24 -0700801 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900802 if (unlikely(sock->state != SS_CONNECTED)) {
wangweidong3b8401f2013-12-12 09:36:40 +0800803 if (sock->state == SS_UNCONNECTED)
Ying Xue247f0f32014-02-18 16:06:46 +0800804 res = tipc_send_packet(NULL, sock, m, total_len);
wangweidongb0555972013-12-27 10:09:39 +0800805 else
806 res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800807 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900808 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100809
Allan Stephens0c3141e2008-04-15 00:22:02 -0700810 if (unlikely(m->msg_name)) {
811 res = -EISCONN;
812 goto exit;
813 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700814
Ying Xue97f8b872013-01-31 21:51:47 +0100815 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400816 res = -EMSGSIZE;
817 goto exit;
818 }
819
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900820 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100821 * Send each iovec entry using one or more messages
822 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900823 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824 * (i.e. one large iovec entry), but could be improved to pass sets
825 * of small iovec entries into send_packet().
826 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700827 curr_iov = m->msg_iov;
828 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829 my_msg.msg_iov = &my_iov;
830 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700831 my_msg.msg_flags = m->msg_flags;
832 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700833 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400835 hdr_size = msg_hdr_sz(&tsk->port.phdr);
Allan Stephens05646c92007-06-10 17:25:24 -0700836
Per Lidenb97bf3f2006-01-02 19:04:38 +0100837 while (curr_iovlen--) {
838 curr_start = curr_iov->iov_base;
839 curr_left = curr_iov->iov_len;
840
841 while (curr_left) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400842 bytes_to_send = tsk->port.max_pkt - hdr_size;
Allan Stephens05646c92007-06-10 17:25:24 -0700843 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
844 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
845 if (curr_left < bytes_to_send)
846 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 my_iov.iov_base = curr_start;
848 my_iov.iov_len = bytes_to_send;
Ying Xue247f0f32014-02-18 16:06:46 +0800849 res = tipc_send_packet(NULL, sock, &my_msg,
850 bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000851 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700852 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700853 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700854 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700855 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100856 curr_left -= bytes_to_send;
857 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700858 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859 }
860
861 curr_iov++;
862 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700863 res = bytes_sent;
864exit:
865 release_sock(sk);
866 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867}
868
869/**
870 * auto_connect - complete connection setup to a remote port
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400871 * @tsk: tipc socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900873 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100874 * Returns 0 on success, errno otherwise
875 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400876static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100877{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400878 struct tipc_port *port = &tsk->port;
879 struct socket *sock = tsk->sk.sk_socket;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400880 struct tipc_portid peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400882 peer.ref = msg_origport(msg);
883 peer.node = msg_orignode(msg);
884
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400885 __tipc_port_connect(port->ref, port, &peer);
Ying Xue584d24b2012-11-29 18:51:19 -0500886
887 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
888 return -EINVAL;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400889 msg_set_importance(&port->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100890 sock->state = SS_CONNECTED;
891 return 0;
892}
893
894/**
895 * set_orig_addr - capture sender's address for received message
896 * @m: descriptor for message info
897 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900898 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899 * Note: Address is not captured if not requested by receiver.
900 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800901static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902{
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100903 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900905 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906 addr->family = AF_TIPC;
907 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000908 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100909 addr->addr.id.ref = msg_origport(msg);
910 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000911 addr->addr.name.domain = 0; /* could leave uninitialized */
912 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913 m->msg_namelen = sizeof(struct sockaddr_tipc);
914 }
915}
916
917/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900918 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100919 * @m: descriptor for message info
920 * @msg: received message header
921 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900922 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100923 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900924 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100925 * Returns 0 if successful, otherwise errno
926 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800927static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400928 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100929{
930 u32 anc_data[3];
931 u32 err;
932 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700933 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934 int res;
935
936 if (likely(m->msg_controllen == 0))
937 return 0;
938
939 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940 err = msg ? msg_errcode(msg) : 0;
941 if (unlikely(err)) {
942 anc_data[0] = err;
943 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000944 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
945 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000947 if (anc_data[1]) {
948 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
949 msg_data(msg));
950 if (res)
951 return res;
952 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100953 }
954
955 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100956 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
957 switch (dest_type) {
958 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700959 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100960 anc_data[0] = msg_nametype(msg);
961 anc_data[1] = msg_namelower(msg);
962 anc_data[2] = msg_namelower(msg);
963 break;
964 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700965 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100966 anc_data[0] = msg_nametype(msg);
967 anc_data[1] = msg_namelower(msg);
968 anc_data[2] = msg_nameupper(msg);
969 break;
970 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700971 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100972 anc_data[0] = tport->conn_type;
973 anc_data[1] = tport->conn_instance;
974 anc_data[2] = tport->conn_instance;
975 break;
976 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700977 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100978 }
Allan Stephens2db99832010-12-31 18:59:33 +0000979 if (has_name) {
980 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
981 if (res)
982 return res;
983 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984
985 return 0;
986}
987
Ying Xue9bbb4ec2014-01-17 09:50:07 +0800988static int tipc_wait_for_rcvmsg(struct socket *sock, long timeo)
989{
990 struct sock *sk = sock->sk;
991 DEFINE_WAIT(wait);
992 int err;
993
994 for (;;) {
995 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +0100996 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +0800997 if (sock->state == SS_DISCONNECTING) {
998 err = -ENOTCONN;
999 break;
1000 }
1001 release_sock(sk);
1002 timeo = schedule_timeout(timeo);
1003 lock_sock(sk);
1004 }
1005 err = 0;
1006 if (!skb_queue_empty(&sk->sk_receive_queue))
1007 break;
1008 err = sock_intr_errno(timeo);
1009 if (signal_pending(current))
1010 break;
1011 err = -EAGAIN;
1012 if (!timeo)
1013 break;
1014 }
1015 finish_wait(sk_sleep(sk), &wait);
1016 return err;
1017}
1018
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001019/**
Ying Xue247f0f32014-02-18 16:06:46 +08001020 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001021 * @iocb: (unused)
1022 * @m: descriptor for message info
1023 * @buf_len: total size of user buffer area
1024 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001025 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1027 * If the complete message doesn't fit in user area, truncate it.
1028 *
1029 * Returns size of returned message data, errno otherwise
1030 */
Ying Xue247f0f32014-02-18 16:06:46 +08001031static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1032 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001033{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001034 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001035 struct tipc_sock *tsk = tipc_sk(sk);
1036 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037 struct sk_buff *buf;
1038 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001039 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 unsigned int sz;
1041 u32 err;
1042 int res;
1043
Allan Stephens0c3141e2008-04-15 00:22:02 -07001044 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045 if (unlikely(!buf_len))
1046 return -EINVAL;
1047
Allan Stephens0c3141e2008-04-15 00:22:02 -07001048 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049
Allan Stephens0c3141e2008-04-15 00:22:02 -07001050 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001051 res = -ENOTCONN;
1052 goto exit;
1053 }
1054
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001055 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001056restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001057
Allan Stephens0c3141e2008-04-15 00:22:02 -07001058 /* Look for a message in receive queue; wait if necessary */
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001059 res = tipc_wait_for_rcvmsg(sock, timeo);
1060 if (res)
1061 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001062
1063 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001064 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065 msg = buf_msg(buf);
1066 sz = msg_data_sz(msg);
1067 err = msg_errcode(msg);
1068
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001070 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001071 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072 goto restart;
1073 }
1074
1075 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076 set_orig_addr(m, msg);
1077
1078 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001079 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001080 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001081 goto exit;
1082
1083 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084 if (!err) {
1085 if (unlikely(buf_len < sz)) {
1086 sz = buf_len;
1087 m->msg_flags |= MSG_TRUNC;
1088 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001089 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1090 m->msg_iov, sz);
1091 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093 res = sz;
1094 } else {
1095 if ((sock->state == SS_READY) ||
1096 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1097 res = 0;
1098 else
1099 res = -ECONNRESET;
1100 }
1101
1102 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001103 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001104 if ((sock->state != SS_READY) &&
Jon Paul Maloy6163a192014-05-14 05:39:08 -04001105 (++port->conn_unacked >= TIPC_CONNACK_INTV))
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001106 tipc_acknowledge(port->ref, port->conn_unacked);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001107 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001108 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001109exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001110 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001111 return res;
1112}
1113
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001114/**
Ying Xue247f0f32014-02-18 16:06:46 +08001115 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116 * @iocb: (unused)
1117 * @m: descriptor for message info
1118 * @buf_len: total size of user buffer area
1119 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001120 *
1121 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122 * will optionally wait for more; never truncates data.
1123 *
1124 * Returns size of returned message data, errno otherwise
1125 */
Ying Xue247f0f32014-02-18 16:06:46 +08001126static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1127 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001128{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001129 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001130 struct tipc_sock *tsk = tipc_sk(sk);
1131 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132 struct sk_buff *buf;
1133 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001134 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001136 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001139 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140
1141 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142 if (unlikely(!buf_len))
1143 return -EINVAL;
1144
Allan Stephens0c3141e2008-04-15 00:22:02 -07001145 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001147 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 res = -ENOTCONN;
1149 goto exit;
1150 }
1151
Florian Westphal3720d402010-08-17 11:00:04 +00001152 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001153 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001154
Allan Stephens0c3141e2008-04-15 00:22:02 -07001155restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001156 /* Look for a message in receive queue; wait if necessary */
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001157 res = tipc_wait_for_rcvmsg(sock, timeo);
1158 if (res)
1159 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001160
1161 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001162 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 msg = buf_msg(buf);
1164 sz = msg_data_sz(msg);
1165 err = msg_errcode(msg);
1166
1167 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001169 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170 goto restart;
1171 }
1172
1173 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 if (sz_copied == 0) {
1175 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001176 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001177 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178 goto exit;
1179 }
1180
1181 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001183 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184
Allan Stephens0232fd02011-02-21 09:45:40 -05001185 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 needed = (buf_len - sz_copied);
1187 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001188
1189 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1190 m->msg_iov, sz_to_copy);
1191 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001193
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 sz_copied += sz_to_copy;
1195
1196 if (sz_to_copy < sz) {
1197 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001198 TIPC_SKB_CB(buf)->handle =
1199 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200 goto exit;
1201 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202 } else {
1203 if (sz_copied != 0)
1204 goto exit; /* can't add error msg to valid data */
1205
1206 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1207 res = 0;
1208 else
1209 res = -ECONNRESET;
1210 }
1211
1212 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy6163a192014-05-14 05:39:08 -04001214 if (unlikely(++port->conn_unacked >= TIPC_CONNACK_INTV))
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001215 tipc_acknowledge(port->ref, port->conn_unacked);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001216 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001217 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218
1219 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001220 if ((sz_copied < buf_len) && /* didn't get all requested data */
1221 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001222 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001223 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1224 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225 goto restart;
1226
1227exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001228 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001229 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230}
1231
1232/**
Ying Xuef288bef2012-08-21 11:16:57 +08001233 * tipc_write_space - wake up thread if port congestion is released
1234 * @sk: socket
1235 */
1236static void tipc_write_space(struct sock *sk)
1237{
1238 struct socket_wq *wq;
1239
1240 rcu_read_lock();
1241 wq = rcu_dereference(sk->sk_wq);
1242 if (wq_has_sleeper(wq))
1243 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1244 POLLWRNORM | POLLWRBAND);
1245 rcu_read_unlock();
1246}
1247
1248/**
1249 * tipc_data_ready - wake up threads to indicate messages have been received
1250 * @sk: socket
1251 * @len: the length of messages
1252 */
David S. Miller676d2362014-04-11 16:15:36 -04001253static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001254{
1255 struct socket_wq *wq;
1256
1257 rcu_read_lock();
1258 wq = rcu_dereference(sk->sk_wq);
1259 if (wq_has_sleeper(wq))
1260 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1261 POLLRDNORM | POLLRDBAND);
1262 rcu_read_unlock();
1263}
1264
1265/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001266 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001267 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001268 * @msg: message
1269 *
1270 * Returns TIPC error status code and socket error status code
1271 * once it encounters some errors
1272 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001273static u32 filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001274{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001275 struct sock *sk = &tsk->sk;
1276 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001277 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001278 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001279
Ying Xue7e6c1312012-11-29 18:39:14 -05001280 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001281 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001282
1283 if (msg_mcast(msg))
1284 return retval;
1285
1286 switch ((int)sock->state) {
1287 case SS_CONNECTED:
1288 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001289 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001290 if (unlikely(msg_errcode(msg))) {
1291 sock->state = SS_DISCONNECTING;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001292 __tipc_port_disconnect(port);
Ying Xue7e6c1312012-11-29 18:39:14 -05001293 }
1294 retval = TIPC_OK;
1295 }
1296 break;
1297 case SS_CONNECTING:
1298 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001299 if (unlikely(msg_errcode(msg))) {
1300 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001301 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001302 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001303 break;
1304 }
1305
1306 if (unlikely(!msg_connected(msg)))
1307 break;
1308
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001309 res = auto_connect(tsk, msg);
Ying Xue584d24b2012-11-29 18:51:19 -05001310 if (res) {
1311 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001312 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001313 retval = TIPC_OK;
1314 break;
1315 }
1316
1317 /* If an incoming message is an 'ACK-', it should be
1318 * discarded here because it doesn't contain useful
1319 * data. In addition, we should try to wake up
1320 * connect() routine if sleeping.
1321 */
1322 if (msg_data_sz(msg) == 0) {
1323 kfree_skb(*buf);
1324 *buf = NULL;
1325 if (waitqueue_active(sk_sleep(sk)))
1326 wake_up_interruptible(sk_sleep(sk));
1327 }
1328 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001329 break;
1330 case SS_LISTENING:
1331 case SS_UNCONNECTED:
1332 /* Accept only SYN message */
1333 if (!msg_connected(msg) && !(msg_errcode(msg)))
1334 retval = TIPC_OK;
1335 break;
1336 case SS_DISCONNECTING:
1337 break;
1338 default:
1339 pr_err("Unknown socket state %u\n", sock->state);
1340 }
1341 return retval;
1342}
1343
1344/**
Ying Xueaba79f32013-01-20 23:30:09 +01001345 * rcvbuf_limit - get proper overload limit of socket receive queue
1346 * @sk: socket
1347 * @buf: message
1348 *
1349 * For all connection oriented messages, irrespective of importance,
1350 * the default overload value (i.e. 67MB) is set as limit.
1351 *
1352 * For all connectionless messages, by default new queue limits are
1353 * as belows:
1354 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001355 * TIPC_LOW_IMPORTANCE (4 MB)
1356 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1357 * TIPC_HIGH_IMPORTANCE (16 MB)
1358 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001359 *
1360 * Returns overload limit according to corresponding message importance
1361 */
1362static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1363{
1364 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001365
1366 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001367 return sysctl_tipc_rmem[2];
1368
1369 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1370 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001371}
1372
1373/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001374 * filter_rcv - validate incoming message
1375 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001377 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001378 * Enqueues message on receive queue if acceptable; optionally handles
1379 * disconnect indication for a connected socket.
1380 *
1381 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001382 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1384 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001385static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001387 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001388 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001389 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001390 unsigned int limit = rcvbuf_limit(sk, buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001391 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392
Per Lidenb97bf3f2006-01-02 19:04:38 +01001393 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001394 if (msg_type(msg) > TIPC_DIRECT_MSG)
1395 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001396
Per Lidenb97bf3f2006-01-02 19:04:38 +01001397 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001398 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001399 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001400 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001401 res = filter_connect(tsk, &buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001402 if (res != TIPC_OK || buf == NULL)
1403 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001404 }
1405
1406 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001407 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1408 return TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001409
Ying Xueaba79f32013-01-20 23:30:09 +01001410 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001411 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001412 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001413 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001414
David S. Miller676d2362014-04-11 16:15:36 -04001415 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001416 return TIPC_OK;
1417}
1418
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001419/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001420 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001421 * @sk: socket
1422 * @buf: message
1423 *
1424 * Caller must hold socket lock, but not port lock.
1425 *
1426 * Returns 0
1427 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001428static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001429{
1430 u32 res;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001431 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001432
1433 res = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001434 if (unlikely(res))
Allan Stephens0c3141e2008-04-15 00:22:02 -07001435 tipc_reject_msg(buf, res);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001436
1437 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1438 atomic_add(buf->truesize, &tsk->dupl_rcvcnt);
1439
Allan Stephens0c3141e2008-04-15 00:22:02 -07001440 return 0;
1441}
1442
1443/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001444 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001445 * @buf: buffer containing arriving message
1446 * Consumes buffer
1447 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001448 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001449int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001450{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001451 struct tipc_sock *tsk;
1452 struct tipc_port *port;
1453 struct sock *sk;
1454 u32 dport = msg_destport(buf_msg(buf));
1455 int err = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001456 uint limit;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001457
1458 /* Forward unresolved named message */
1459 if (unlikely(!dport)) {
1460 tipc_net_route_msg(buf);
1461 return 0;
1462 }
1463
1464 /* Validate destination */
1465 port = tipc_port_lock(dport);
1466 if (unlikely(!port)) {
1467 err = TIPC_ERR_NO_PORT;
1468 goto exit;
1469 }
1470
1471 tsk = tipc_port_to_sock(port);
1472 sk = &tsk->sk;
1473
1474 /* Queue message */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001475 bh_lock_sock(sk);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001476
Allan Stephens0c3141e2008-04-15 00:22:02 -07001477 if (!sock_owned_by_user(sk)) {
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001478 err = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001479 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001480 if (sk->sk_backlog.len == 0)
1481 atomic_set(&tsk->dupl_rcvcnt, 0);
1482 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1483 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001484 err = TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001485 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001486
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001487 bh_unlock_sock(sk);
1488 tipc_port_unlock(port);
1489
1490 if (likely(!err))
1491 return 0;
1492exit:
1493 tipc_reject_msg(buf, err);
1494 return -EHOSTUNREACH;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001495}
1496
Ying Xue78eb3a52014-01-17 09:50:03 +08001497static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1498{
1499 struct sock *sk = sock->sk;
1500 DEFINE_WAIT(wait);
1501 int done;
1502
1503 do {
1504 int err = sock_error(sk);
1505 if (err)
1506 return err;
1507 if (!*timeo_p)
1508 return -ETIMEDOUT;
1509 if (signal_pending(current))
1510 return sock_intr_errno(*timeo_p);
1511
1512 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1513 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1514 finish_wait(sk_sleep(sk), &wait);
1515 } while (!done);
1516 return 0;
1517}
1518
Per Lidenb97bf3f2006-01-02 19:04:38 +01001519/**
Ying Xue247f0f32014-02-18 16:06:46 +08001520 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001521 * @sock: socket structure
1522 * @dest: socket address for destination port
1523 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001524 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001525 *
1526 * Returns 0 on success, errno otherwise
1527 */
Ying Xue247f0f32014-02-18 16:06:46 +08001528static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1529 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001530{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001531 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001532 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1533 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001534 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1535 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001536 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001537
Allan Stephens0c3141e2008-04-15 00:22:02 -07001538 lock_sock(sk);
1539
Allan Stephensb89741a2008-04-15 00:20:37 -07001540 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001541 if (sock->state == SS_READY) {
1542 res = -EOPNOTSUPP;
1543 goto exit;
1544 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001545
Allan Stephensb89741a2008-04-15 00:20:37 -07001546 /*
1547 * Reject connection attempt using multicast address
1548 *
1549 * Note: send_msg() validates the rest of the address fields,
1550 * so there's no need to do it here
1551 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001552 if (dst->addrtype == TIPC_ADDR_MCAST) {
1553 res = -EINVAL;
1554 goto exit;
1555 }
1556
Ying Xue78eb3a52014-01-17 09:50:03 +08001557 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001558 switch (sock->state) {
1559 case SS_UNCONNECTED:
1560 /* Send a 'SYN-' to destination */
1561 m.msg_name = dest;
1562 m.msg_namelen = destlen;
1563
1564 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1565 * indicate send_msg() is never blocked.
1566 */
1567 if (!timeout)
1568 m.msg_flags = MSG_DONTWAIT;
1569
Ying Xue247f0f32014-02-18 16:06:46 +08001570 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001571 if ((res < 0) && (res != -EWOULDBLOCK))
1572 goto exit;
1573
1574 /* Just entered SS_CONNECTING state; the only
1575 * difference is that return value in non-blocking
1576 * case is EINPROGRESS, rather than EALREADY.
1577 */
1578 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001579 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001580 if (previous == SS_CONNECTING)
1581 res = -EALREADY;
1582 if (!timeout)
1583 goto exit;
1584 timeout = msecs_to_jiffies(timeout);
1585 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1586 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001587 break;
1588 case SS_CONNECTED:
1589 res = -EISCONN;
1590 break;
1591 default:
1592 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001593 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001594 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001595exit:
1596 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001597 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001598}
1599
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001600/**
Ying Xue247f0f32014-02-18 16:06:46 +08001601 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001602 * @sock: socket structure
1603 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001604 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001605 * Returns 0 on success, errno otherwise
1606 */
Ying Xue247f0f32014-02-18 16:06:46 +08001607static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001608{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001609 struct sock *sk = sock->sk;
1610 int res;
1611
1612 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001613
Ying Xue245f3d32011-07-06 06:01:13 -04001614 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001615 res = -EINVAL;
1616 else {
1617 sock->state = SS_LISTENING;
1618 res = 0;
1619 }
1620
1621 release_sock(sk);
1622 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001623}
1624
Ying Xue6398e232014-01-17 09:50:04 +08001625static int tipc_wait_for_accept(struct socket *sock, long timeo)
1626{
1627 struct sock *sk = sock->sk;
1628 DEFINE_WAIT(wait);
1629 int err;
1630
1631 /* True wake-one mechanism for incoming connections: only
1632 * one process gets woken up, not the 'whole herd'.
1633 * Since we do not 'race & poll' for established sockets
1634 * anymore, the common case will execute the loop only once.
1635 */
1636 for (;;) {
1637 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1638 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001639 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001640 release_sock(sk);
1641 timeo = schedule_timeout(timeo);
1642 lock_sock(sk);
1643 }
1644 err = 0;
1645 if (!skb_queue_empty(&sk->sk_receive_queue))
1646 break;
1647 err = -EINVAL;
1648 if (sock->state != SS_LISTENING)
1649 break;
1650 err = sock_intr_errno(timeo);
1651 if (signal_pending(current))
1652 break;
1653 err = -EAGAIN;
1654 if (!timeo)
1655 break;
1656 }
1657 finish_wait(sk_sleep(sk), &wait);
1658 return err;
1659}
1660
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001661/**
Ying Xue247f0f32014-02-18 16:06:46 +08001662 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663 * @sock: listening socket
1664 * @newsock: new socket that is to be connected
1665 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001666 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001667 * Returns 0 on success, errno otherwise
1668 */
Ying Xue247f0f32014-02-18 16:06:46 +08001669static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001671 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001672 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001673 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001674 struct tipc_msg *msg;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001675 struct tipc_portid peer;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001676 u32 new_ref;
Ying Xue6398e232014-01-17 09:50:04 +08001677 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001678 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679
Allan Stephens0c3141e2008-04-15 00:22:02 -07001680 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001681
Allan Stephens0c3141e2008-04-15 00:22:02 -07001682 if (sock->state != SS_LISTENING) {
1683 res = -EINVAL;
1684 goto exit;
1685 }
Ying Xue6398e232014-01-17 09:50:04 +08001686 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1687 res = tipc_wait_for_accept(sock, timeo);
1688 if (res)
1689 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001690
1691 buf = skb_peek(&sk->sk_receive_queue);
1692
Ying Xuec5fa7b32013-06-17 10:54:39 -04001693 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001694 if (res)
1695 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001696
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001697 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001698 new_port = &tipc_sk(new_sk)->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001699 new_ref = new_port->ref;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001700 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001701
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001702 /* we lock on new_sk; but lockdep sees the lock on sk */
1703 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001704
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001705 /*
1706 * Reject any stray messages received by new socket
1707 * before the socket lock was taken (very, very unlikely)
1708 */
1709 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001710
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001711 /* Connect new socket to it's peer */
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001712 peer.ref = msg_origport(msg);
1713 peer.node = msg_orignode(msg);
1714 tipc_port_connect(new_ref, &peer);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001715 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001716
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001717 tipc_port_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001718 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001719 new_port->conn_type = msg_nametype(msg);
1720 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001721 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001722
1723 /*
1724 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1725 * Respond to 'SYN+' by queuing it on new socket.
1726 */
1727 if (!msg_data_sz(msg)) {
1728 struct msghdr m = {NULL,};
1729
1730 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001731 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001732 } else {
1733 __skb_dequeue(&sk->sk_receive_queue);
1734 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001735 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001736 }
1737 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001738exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001739 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001740 return res;
1741}
1742
1743/**
Ying Xue247f0f32014-02-18 16:06:46 +08001744 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001745 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001746 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001747 *
1748 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001749 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001750 * Returns 0 on success, errno otherwise
1751 */
Ying Xue247f0f32014-02-18 16:06:46 +08001752static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001753{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001754 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001755 struct tipc_sock *tsk = tipc_sk(sk);
1756 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001757 struct sk_buff *buf;
1758 int res;
1759
Allan Stephense247a8f2008-03-06 15:05:38 -08001760 if (how != SHUT_RDWR)
1761 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001762
Allan Stephens0c3141e2008-04-15 00:22:02 -07001763 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001764
1765 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001766 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001767 case SS_CONNECTED:
1768
Per Lidenb97bf3f2006-01-02 19:04:38 +01001769restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001770 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001771 buf = __skb_dequeue(&sk->sk_receive_queue);
1772 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001773 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001774 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001775 goto restart;
1776 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001777 tipc_port_disconnect(port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001778 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001779 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001780 tipc_port_shutdown(port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001781 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001782
1783 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001784
1785 /* fall through */
1786
1787 case SS_DISCONNECTING:
1788
Ying Xue75031152012-10-29 09:38:15 -04001789 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001790 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001791
1792 /* Wake up anyone sleeping in poll */
1793 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001794 res = 0;
1795 break;
1796
1797 default:
1798 res = -ENOTCONN;
1799 }
1800
Allan Stephens0c3141e2008-04-15 00:22:02 -07001801 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001802 return res;
1803}
1804
1805/**
Ying Xue247f0f32014-02-18 16:06:46 +08001806 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001807 * @sock: socket structure
1808 * @lvl: option level
1809 * @opt: option identifier
1810 * @ov: pointer to new option value
1811 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001812 *
1813 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001815 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816 * Returns 0 on success, errno otherwise
1817 */
Ying Xue247f0f32014-02-18 16:06:46 +08001818static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1819 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001820{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001821 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001822 struct tipc_sock *tsk = tipc_sk(sk);
1823 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001824 u32 value;
1825 int res;
1826
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001827 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1828 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829 if (lvl != SOL_TIPC)
1830 return -ENOPROTOOPT;
1831 if (ol < sizeof(value))
1832 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001833 res = get_user(value, (u32 __user *)ov);
1834 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001835 return res;
1836
Allan Stephens0c3141e2008-04-15 00:22:02 -07001837 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001838
Per Lidenb97bf3f2006-01-02 19:04:38 +01001839 switch (opt) {
1840 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001841 tipc_port_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001842 break;
1843 case TIPC_SRC_DROPPABLE:
1844 if (sock->type != SOCK_STREAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001845 tipc_port_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001846 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001847 res = -ENOPROTOOPT;
1848 break;
1849 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001850 tipc_port_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001851 break;
1852 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001853 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001854 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855 break;
1856 default:
1857 res = -EINVAL;
1858 }
1859
Allan Stephens0c3141e2008-04-15 00:22:02 -07001860 release_sock(sk);
1861
Per Lidenb97bf3f2006-01-02 19:04:38 +01001862 return res;
1863}
1864
1865/**
Ying Xue247f0f32014-02-18 16:06:46 +08001866 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001867 * @sock: socket structure
1868 * @lvl: option level
1869 * @opt: option identifier
1870 * @ov: receptacle for option value
1871 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001872 *
1873 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001874 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001875 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001876 * Returns 0 on success, errno otherwise
1877 */
Ying Xue247f0f32014-02-18 16:06:46 +08001878static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1879 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001880{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001881 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001882 struct tipc_sock *tsk = tipc_sk(sk);
1883 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001884 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001885 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001886 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001887
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001888 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1889 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001890 if (lvl != SOL_TIPC)
1891 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001892 res = get_user(len, ol);
1893 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001894 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001895
Allan Stephens0c3141e2008-04-15 00:22:02 -07001896 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001897
1898 switch (opt) {
1899 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001900 value = tipc_port_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001901 break;
1902 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001903 value = tipc_port_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001904 break;
1905 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001906 value = tipc_port_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907 break;
1908 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001909 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001910 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001911 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001912 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001913 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001914 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001915 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001916 value = skb_queue_len(&sk->sk_receive_queue);
1917 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001918 default:
1919 res = -EINVAL;
1920 }
1921
Allan Stephens0c3141e2008-04-15 00:22:02 -07001922 release_sock(sk);
1923
Paul Gortmaker25860c32010-12-31 18:59:31 +00001924 if (res)
1925 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001926
Paul Gortmaker25860c32010-12-31 18:59:31 +00001927 if (len < sizeof(value))
1928 return -EINVAL;
1929
1930 if (copy_to_user(ov, &value, sizeof(value)))
1931 return -EFAULT;
1932
1933 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001934}
1935
Erik Hugne78acb1f2014-04-24 16:26:47 +02001936int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
1937{
1938 struct tipc_sioc_ln_req lnr;
1939 void __user *argp = (void __user *)arg;
1940
1941 switch (cmd) {
1942 case SIOCGETLINKNAME:
1943 if (copy_from_user(&lnr, argp, sizeof(lnr)))
1944 return -EFAULT;
1945 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
1946 lnr.linkname, TIPC_MAX_LINK_NAME)) {
1947 if (copy_to_user(argp, &lnr, sizeof(lnr)))
1948 return -EFAULT;
1949 return 0;
1950 }
1951 return -EADDRNOTAVAIL;
1952 break;
1953 default:
1954 return -ENOIOCTLCMD;
1955 }
1956}
1957
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001958/* Protocol switches for the various types of TIPC sockets */
1959
Florian Westphalbca65ea2008-02-07 18:18:01 -08001960static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001961 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001962 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001963 .release = tipc_release,
1964 .bind = tipc_bind,
1965 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001966 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001967 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08001968 .getname = tipc_getname,
1969 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02001970 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001971 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08001972 .shutdown = tipc_shutdown,
1973 .setsockopt = tipc_setsockopt,
1974 .getsockopt = tipc_getsockopt,
1975 .sendmsg = tipc_sendmsg,
1976 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001977 .mmap = sock_no_mmap,
1978 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001979};
1980
Florian Westphalbca65ea2008-02-07 18:18:01 -08001981static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001982 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001983 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001984 .release = tipc_release,
1985 .bind = tipc_bind,
1986 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001987 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08001988 .accept = tipc_accept,
1989 .getname = tipc_getname,
1990 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02001991 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08001992 .listen = tipc_listen,
1993 .shutdown = tipc_shutdown,
1994 .setsockopt = tipc_setsockopt,
1995 .getsockopt = tipc_getsockopt,
1996 .sendmsg = tipc_send_packet,
1997 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001998 .mmap = sock_no_mmap,
1999 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002000};
2001
Florian Westphalbca65ea2008-02-07 18:18:01 -08002002static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002003 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002004 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002005 .release = tipc_release,
2006 .bind = tipc_bind,
2007 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002008 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002009 .accept = tipc_accept,
2010 .getname = tipc_getname,
2011 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002012 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002013 .listen = tipc_listen,
2014 .shutdown = tipc_shutdown,
2015 .setsockopt = tipc_setsockopt,
2016 .getsockopt = tipc_getsockopt,
2017 .sendmsg = tipc_send_stream,
2018 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002019 .mmap = sock_no_mmap,
2020 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002021};
2022
Florian Westphalbca65ea2008-02-07 18:18:01 -08002023static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002024 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002025 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002026 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002027};
2028
2029static struct proto tipc_proto = {
2030 .name = "TIPC",
2031 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002032 .obj_size = sizeof(struct tipc_sock),
2033 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002034};
2035
Ying Xuec5fa7b32013-06-17 10:54:39 -04002036static struct proto tipc_proto_kern = {
2037 .name = "TIPC",
2038 .obj_size = sizeof(struct tipc_sock),
2039 .sysctl_rmem = sysctl_tipc_rmem
2040};
2041
Per Lidenb97bf3f2006-01-02 19:04:38 +01002042/**
Per Liden4323add2006-01-18 00:38:21 +01002043 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002044 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002045 * Returns 0 on success, errno otherwise
2046 */
Per Liden4323add2006-01-18 00:38:21 +01002047int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002048{
2049 int res;
2050
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002051 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002052 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002053 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002054 goto out;
2055 }
2056
2057 res = sock_register(&tipc_family_ops);
2058 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002059 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002060 proto_unregister(&tipc_proto);
2061 goto out;
2062 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002063 out:
2064 return res;
2065}
2066
2067/**
Per Liden4323add2006-01-18 00:38:21 +01002068 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002069 */
Per Liden4323add2006-01-18 00:38:21 +01002070void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002071{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002072 sock_unregister(tipc_family_ops.family);
2073 proto_unregister(&tipc_proto);
2074}