blob: 3f9912f87d0d1744a14ac3dbd9dd61cf9aa545c1 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon 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
Allan Stephens0c3141e2008-04-15 00:22:02 -070048static int 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);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700199 sk->sk_backlog_rcv = 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;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400203 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400204 tipc_port_unlock(port);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700205
Allan Stephens0c3141e2008-04-15 00:22:02 -0700206 if (sock->state == SS_READY) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400207 tipc_port_set_unreturnable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700208 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400209 tipc_port_set_unreliable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700210 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211 return 0;
212}
213
214/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400215 * tipc_sock_create_local - create TIPC socket from inside TIPC module
216 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
217 *
218 * We cannot use sock_creat_kern here because it bumps module user count.
219 * Since socket owner and creator is the same module we must make sure
220 * that module count remains zero for module local sockets, otherwise
221 * we cannot do rmmod.
222 *
223 * Returns 0 on success, errno otherwise
224 */
225int tipc_sock_create_local(int type, struct socket **res)
226{
227 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400228
229 rc = sock_create_lite(AF_TIPC, type, 0, res);
230 if (rc < 0) {
231 pr_err("Failed to create kernel socket\n");
232 return rc;
233 }
234 tipc_sk_create(&init_net, *res, 0, 1);
235
Ying Xuec5fa7b32013-06-17 10:54:39 -0400236 return 0;
237}
238
239/**
240 * tipc_sock_release_local - release socket created by tipc_sock_create_local
241 * @sock: the socket to be released.
242 *
243 * Module reference count is not incremented when such sockets are created,
244 * so we must keep it from being decremented when they are released.
245 */
246void tipc_sock_release_local(struct socket *sock)
247{
Ying Xue247f0f32014-02-18 16:06:46 +0800248 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400249 sock->ops = NULL;
250 sock_release(sock);
251}
252
253/**
254 * tipc_sock_accept_local - accept a connection on a socket created
255 * with tipc_sock_create_local. Use this function to avoid that
256 * module reference count is inadvertently incremented.
257 *
258 * @sock: the accepting socket
259 * @newsock: reference to the new socket to be created
260 * @flags: socket flags
261 */
262
263int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400264 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400265{
266 struct sock *sk = sock->sk;
267 int ret;
268
269 ret = sock_create_lite(sk->sk_family, sk->sk_type,
270 sk->sk_protocol, newsock);
271 if (ret < 0)
272 return ret;
273
Ying Xue247f0f32014-02-18 16:06:46 +0800274 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400275 if (ret < 0) {
276 sock_release(*newsock);
277 return ret;
278 }
279 (*newsock)->ops = sock->ops;
280 return ret;
281}
282
283/**
Ying Xue247f0f32014-02-18 16:06:46 +0800284 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285 * @sock: socket to destroy
286 *
287 * This routine cleans up any messages that are still queued on the socket.
288 * For DGRAM and RDM socket types, all queued messages are rejected.
289 * For SEQPACKET and STREAM socket types, the first message is rejected
290 * and any others are discarded. (If the first message on a STREAM socket
291 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900292 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293 * NOTE: Rejected messages are not necessarily returned to the sender! They
294 * are returned or discarded according to the "destination droppable" setting
295 * specified for the message by the sender.
296 *
297 * Returns 0 on success, errno otherwise
298 */
Ying Xue247f0f32014-02-18 16:06:46 +0800299static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400302 struct tipc_sock *tsk;
303 struct tipc_port *port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 struct sk_buff *buf;
305
Allan Stephens0c3141e2008-04-15 00:22:02 -0700306 /*
307 * Exit if socket isn't fully initialized (occurs when a failed accept()
308 * releases a pre-allocated child socket that was never used)
309 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700310 if (sk == NULL)
311 return 0;
312
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400313 tsk = tipc_sk(sk);
314 port = &tsk->port;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700315 lock_sock(sk);
316
317 /*
318 * Reject all unreceived messages, except on an active connection
319 * (which disconnects locally & sends a 'FIN+' to peer)
320 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700322 buf = __skb_dequeue(&sk->sk_receive_queue);
323 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 break;
Ying Xue40682432013-10-18 07:23:16 +0200325 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400326 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700327 else {
328 if ((sock->state == SS_CONNECTING) ||
329 (sock->state == SS_CONNECTED)) {
330 sock->state = SS_DISCONNECTING;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400331 tipc_port_disconnect(port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700332 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700334 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335 }
336
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400337 /* Destroy TIPC port; also disconnects an active connection and
338 * sends a 'FIN-' to peer.
Allan Stephens0c3141e2008-04-15 00:22:02 -0700339 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400340 tipc_port_destroy(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341
Allan Stephens0c3141e2008-04-15 00:22:02 -0700342 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100343 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344
Allan Stephens0c3141e2008-04-15 00:22:02 -0700345 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700346 sock->state = SS_DISCONNECTING;
347 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348
349 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700350 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200352 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353}
354
355/**
Ying Xue247f0f32014-02-18 16:06:46 +0800356 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357 * @sock: socket structure
358 * @uaddr: socket address describing name(s) and desired operation
359 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900360 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361 * Name and name sequence binding is indicated using a positive scope value;
362 * a negative scope value unbinds the specified name. Specifying no name
363 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900364 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700366 *
367 * NOTE: This routine doesn't need to take the socket lock since it doesn't
368 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369 */
Ying Xue247f0f32014-02-18 16:06:46 +0800370static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
371 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372{
Ying Xue84602762013-12-27 10:18:28 +0800373 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400375 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800376 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377
Ying Xue84602762013-12-27 10:18:28 +0800378 lock_sock(sk);
379 if (unlikely(!uaddr_len)) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400380 res = tipc_withdraw(&tsk->port, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800381 goto exit;
382 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900383
Ying Xue84602762013-12-27 10:18:28 +0800384 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
385 res = -EINVAL;
386 goto exit;
387 }
388 if (addr->family != AF_TIPC) {
389 res = -EAFNOSUPPORT;
390 goto exit;
391 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 if (addr->addrtype == TIPC_ADDR_NAME)
394 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800395 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
396 res = -EAFNOSUPPORT;
397 goto exit;
398 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900399
Ying Xue13a2e892013-06-17 10:54:40 -0400400 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400401 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800402 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
403 res = -EACCES;
404 goto exit;
405 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400406
Ying Xue84602762013-12-27 10:18:28 +0800407 res = (addr->scope > 0) ?
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400408 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
409 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800410exit:
411 release_sock(sk);
412 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413}
414
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900415/**
Ying Xue247f0f32014-02-18 16:06:46 +0800416 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 * @sock: socket structure
418 * @uaddr: area for returned socket address
419 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700420 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900421 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700423 *
Allan Stephens2da59912008-07-14 22:43:32 -0700424 * NOTE: This routine doesn't need to take the socket lock since it only
425 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000426 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 */
Ying Xue247f0f32014-02-18 16:06:46 +0800428static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
429 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400432 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000434 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700435 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700436 if ((sock->state != SS_CONNECTED) &&
437 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
438 return -ENOTCONN;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400439 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
440 addr->addr.id.node = tipc_port_peernode(&tsk->port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700441 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400442 addr->addr.id.ref = tsk->port.ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000443 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700444 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445
446 *uaddr_len = sizeof(*addr);
447 addr->addrtype = TIPC_ADDR_ID;
448 addr->family = AF_TIPC;
449 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 addr->addr.name.domain = 0;
451
Allan Stephens0c3141e2008-04-15 00:22:02 -0700452 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453}
454
455/**
Ying Xue247f0f32014-02-18 16:06:46 +0800456 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 * @file: file structure associated with the socket
458 * @sock: socket for which to calculate the poll bits
459 * @wait: ???
460 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700461 * Returns pollmask value
462 *
463 * COMMENTARY:
464 * It appears that the usual socket locking mechanisms are not useful here
465 * since the pollmask info is potentially out-of-date the moment this routine
466 * exits. TCP and other protocols seem to rely on higher level poll routines
467 * to handle any preventable race conditions, so TIPC will do the same ...
468 *
469 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700470 *
Allan Stephensf662c072010-08-17 11:00:06 +0000471 * socket state flags set
472 * ------------ ---------
473 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200474 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000475 *
476 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
477 * no write flags
478 *
479 * connected POLLIN/POLLRDNORM if data in rx queue
480 * POLLOUT if port is not congested
481 *
482 * disconnecting POLLIN/POLLRDNORM/POLLHUP
483 * no write flags
484 *
485 * listening POLLIN if SYN in rx queue
486 * no write flags
487 *
488 * ready POLLIN/POLLRDNORM if data in rx queue
489 * [connectionless] POLLOUT (since port cannot be congested)
490 *
491 * IMPORTANT: The fact that a read or write operation is indicated does NOT
492 * imply that the operation will succeed, merely that it should be performed
493 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494 */
Ying Xue247f0f32014-02-18 16:06:46 +0800495static unsigned int tipc_poll(struct file *file, struct socket *sock,
496 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497{
Allan Stephens9b674e82008-03-26 16:48:21 -0700498 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400499 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000500 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700501
Ying Xuef288bef2012-08-21 11:16:57 +0800502 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700503
Allan Stephensf662c072010-08-17 11:00:06 +0000504 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200505 case SS_UNCONNECTED:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400506 if (!tsk->port.congested)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200507 mask |= POLLOUT;
508 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000509 case SS_READY:
510 case SS_CONNECTED:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400511 if (!tsk->port.congested)
Allan Stephensf662c072010-08-17 11:00:06 +0000512 mask |= POLLOUT;
513 /* fall thru' */
514 case SS_CONNECTING:
515 case SS_LISTENING:
516 if (!skb_queue_empty(&sk->sk_receive_queue))
517 mask |= (POLLIN | POLLRDNORM);
518 break;
519 case SS_DISCONNECTING:
520 mask = (POLLIN | POLLRDNORM | POLLHUP);
521 break;
522 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700523
524 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525}
526
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900527/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528 * dest_name_check - verify user is permitted to send to specified port name
529 * @dest: destination address
530 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900531 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100532 * Prevents restricted configuration commands from being issued by
533 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900534 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 * Returns 0 if permission is granted, otherwise errno
536 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800537static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538{
539 struct tipc_cfg_msg_hdr hdr;
540
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900541 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
542 return 0;
543 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
544 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900545 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
546 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547
Allan Stephens3f8dd942011-01-18 13:09:29 -0500548 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
549 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900550 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700552 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900554
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555 return 0;
556}
557
Ying Xue3f405042014-01-17 09:50:05 +0800558static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
559{
560 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400561 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800562 DEFINE_WAIT(wait);
563 int done;
564
565 do {
566 int err = sock_error(sk);
567 if (err)
568 return err;
569 if (sock->state == SS_DISCONNECTING)
570 return -EPIPE;
571 if (!*timeo_p)
572 return -EAGAIN;
573 if (signal_pending(current))
574 return sock_intr_errno(*timeo_p);
575
576 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400577 done = sk_wait_event(sk, timeo_p, !tsk->port.congested);
Ying Xue3f405042014-01-17 09:50:05 +0800578 finish_wait(sk_sleep(sk), &wait);
579 } while (!done);
580 return 0;
581}
582
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400583
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584/**
Ying Xue247f0f32014-02-18 16:06:46 +0800585 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700586 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587 * @sock: socket structure
588 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700589 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900590 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900592 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
594 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900595 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596 * Returns the number of bytes sent on success, or errno otherwise
597 */
Ying Xue247f0f32014-02-18 16:06:46 +0800598static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
599 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700601 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400602 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400603 struct tipc_port *port = &tsk->port;
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100604 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 int needs_conn;
Ying Xue3f405042014-01-17 09:50:05 +0800606 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 int res = -EINVAL;
608
609 if (unlikely(!dest))
610 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700611 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
612 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 return -EINVAL;
Ying Xue97f8b872013-01-31 21:51:47 +0100614 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400615 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616
Allan Stephens0c3141e2008-04-15 00:22:02 -0700617 if (iocb)
618 lock_sock(sk);
619
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 needs_conn = (sock->state != SS_READY);
621 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700622 if (sock->state == SS_LISTENING) {
623 res = -EPIPE;
624 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700625 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700626 if (sock->state != SS_UNCONNECTED) {
627 res = -EISCONN;
628 goto exit;
629 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400630 if (tsk->port.published) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700631 res = -EOPNOTSUPP;
632 goto exit;
633 }
634 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400635 tsk->port.conn_type = dest->addr.name.name.type;
636 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700637 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638
639 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700640 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 }
642
Ying Xue3f405042014-01-17 09:50:05 +0800643 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900644 do {
645 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000646 res = dest_name_check(dest, m);
647 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700648 break;
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400649 res = tipc_send2name(port,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900650 &dest->addr.name.name,
651 dest->addr.name.domain,
Allan Stephens26896902011-04-21 10:42:07 -0500652 m->msg_iov,
653 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000654 } else if (dest->addrtype == TIPC_ADDR_ID) {
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400655 res = tipc_send2port(port,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900656 &dest->addr.id,
Allan Stephens26896902011-04-21 10:42:07 -0500657 m->msg_iov,
658 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000659 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 if (needs_conn) {
661 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700662 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663 }
Allan Stephens2db99832010-12-31 18:59:33 +0000664 res = dest_name_check(dest, m);
665 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700666 break;
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400667 res = tipc_port_mcast_xmit(port,
Ying Xue247f0f32014-02-18 16:06:46 +0800668 &dest->addr.nameseq,
669 m->msg_iov,
670 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900671 }
672 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000673 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700674 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700675 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900676 }
Ying Xue3f405042014-01-17 09:50:05 +0800677 res = tipc_wait_for_sndmsg(sock, &timeo);
678 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700679 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900680 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700681
682exit:
683 if (iocb)
684 release_sock(sk);
685 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686}
687
Ying Xue391a6dd2014-01-17 09:50:06 +0800688static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
689{
690 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400691 struct tipc_sock *tsk = tipc_sk(sk);
692 struct tipc_port *port = &tsk->port;
Ying Xue391a6dd2014-01-17 09:50:06 +0800693 DEFINE_WAIT(wait);
694 int done;
695
696 do {
697 int err = sock_error(sk);
698 if (err)
699 return err;
700 if (sock->state == SS_DISCONNECTING)
701 return -EPIPE;
702 else if (sock->state != SS_CONNECTED)
703 return -ENOTCONN;
704 if (!*timeo_p)
705 return -EAGAIN;
706 if (signal_pending(current))
707 return sock_intr_errno(*timeo_p);
708
709 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
710 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400711 (!port->congested || !port->connected));
Ying Xue391a6dd2014-01-17 09:50:06 +0800712 finish_wait(sk_sleep(sk), &wait);
713 } while (!done);
714 return 0;
715}
716
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900717/**
Ying Xue247f0f32014-02-18 16:06:46 +0800718 * tipc_send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700719 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100720 * @sock: socket structure
721 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700722 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900723 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900725 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726 * Returns the number of bytes sent on success, or errno otherwise
727 */
Ying Xue247f0f32014-02-18 16:06:46 +0800728static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
729 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700731 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400732 struct tipc_sock *tsk = tipc_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100733 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue391a6dd2014-01-17 09:50:06 +0800734 int res = -EINVAL;
735 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736
737 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100738 if (unlikely(dest))
Ying Xue247f0f32014-02-18 16:06:46 +0800739 return tipc_sendmsg(iocb, sock, m, total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100740
Ying Xue97f8b872013-01-31 21:51:47 +0100741 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400742 return -EMSGSIZE;
743
Allan Stephens0c3141e2008-04-15 00:22:02 -0700744 if (iocb)
745 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100746
Ying Xue391a6dd2014-01-17 09:50:06 +0800747 if (unlikely(sock->state != SS_CONNECTED)) {
748 if (sock->state == SS_DISCONNECTING)
749 res = -EPIPE;
750 else
751 res = -ENOTCONN;
752 goto exit;
753 }
Ying Xue1d835872011-07-06 05:53:15 -0400754
Ying Xue391a6dd2014-01-17 09:50:06 +0800755 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900756 do {
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400757 res = tipc_send(&tsk->port, m->msg_iov, total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000758 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700759 break;
Ying Xue391a6dd2014-01-17 09:50:06 +0800760 res = tipc_wait_for_sndpkt(sock, &timeo);
761 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700762 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900763 } while (1);
Ying Xue391a6dd2014-01-17 09:50:06 +0800764exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700765 if (iocb)
766 release_sock(sk);
767 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100768}
769
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900770/**
Ying Xue247f0f32014-02-18 16:06:46 +0800771 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100772 * @iocb: (unused)
773 * @sock: socket structure
774 * @m: data to send
775 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900776 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100777 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900778 *
779 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700780 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100781 */
Ying Xue247f0f32014-02-18 16:06:46 +0800782static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
783 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100784{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700785 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400786 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100787 struct msghdr my_msg;
788 struct iovec my_iov;
789 struct iovec *curr_iov;
790 int curr_iovlen;
791 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700792 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793 int curr_left;
794 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700795 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900797
Allan Stephens0c3141e2008-04-15 00:22:02 -0700798 lock_sock(sk);
799
Allan Stephens05646c92007-06-10 17:25:24 -0700800 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900801 if (unlikely(sock->state != SS_CONNECTED)) {
wangweidong3b8401f2013-12-12 09:36:40 +0800802 if (sock->state == SS_UNCONNECTED)
Ying Xue247f0f32014-02-18 16:06:46 +0800803 res = tipc_send_packet(NULL, sock, m, total_len);
wangweidongb0555972013-12-27 10:09:39 +0800804 else
805 res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800806 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900807 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808
Allan Stephens0c3141e2008-04-15 00:22:02 -0700809 if (unlikely(m->msg_name)) {
810 res = -EISCONN;
811 goto exit;
812 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700813
Ying Xue97f8b872013-01-31 21:51:47 +0100814 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400815 res = -EMSGSIZE;
816 goto exit;
817 }
818
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900819 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100820 * Send each iovec entry using one or more messages
821 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900822 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100823 * (i.e. one large iovec entry), but could be improved to pass sets
824 * of small iovec entries into send_packet().
825 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700826 curr_iov = m->msg_iov;
827 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828 my_msg.msg_iov = &my_iov;
829 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700830 my_msg.msg_flags = m->msg_flags;
831 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700832 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100833
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400834 hdr_size = msg_hdr_sz(&tsk->port.phdr);
Allan Stephens05646c92007-06-10 17:25:24 -0700835
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836 while (curr_iovlen--) {
837 curr_start = curr_iov->iov_base;
838 curr_left = curr_iov->iov_len;
839
840 while (curr_left) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400841 bytes_to_send = tsk->port.max_pkt - hdr_size;
Allan Stephens05646c92007-06-10 17:25:24 -0700842 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
843 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
844 if (curr_left < bytes_to_send)
845 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846 my_iov.iov_base = curr_start;
847 my_iov.iov_len = bytes_to_send;
Ying Xue247f0f32014-02-18 16:06:46 +0800848 res = tipc_send_packet(NULL, sock, &my_msg,
849 bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000850 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700851 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700852 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700853 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700854 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855 curr_left -= bytes_to_send;
856 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700857 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100858 }
859
860 curr_iov++;
861 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700862 res = bytes_sent;
863exit:
864 release_sock(sk);
865 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866}
867
868/**
869 * auto_connect - complete connection setup to a remote port
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400870 * @tsk: tipc socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900872 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873 * Returns 0 on success, errno otherwise
874 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400875static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100876{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400877 struct tipc_port *port = &tsk->port;
878 struct socket *sock = tsk->sk.sk_socket;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400879 struct tipc_portid peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400881 peer.ref = msg_origport(msg);
882 peer.node = msg_orignode(msg);
883
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400884 __tipc_port_connect(port->ref, port, &peer);
Ying Xue584d24b2012-11-29 18:51:19 -0500885
886 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
887 return -EINVAL;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400888 msg_set_importance(&port->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889 sock->state = SS_CONNECTED;
890 return 0;
891}
892
893/**
894 * set_orig_addr - capture sender's address for received message
895 * @m: descriptor for message info
896 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900897 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898 * Note: Address is not captured if not requested by receiver.
899 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800900static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901{
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100902 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900904 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905 addr->family = AF_TIPC;
906 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000907 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908 addr->addr.id.ref = msg_origport(msg);
909 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000910 addr->addr.name.domain = 0; /* could leave uninitialized */
911 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912 m->msg_namelen = sizeof(struct sockaddr_tipc);
913 }
914}
915
916/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900917 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100918 * @m: descriptor for message info
919 * @msg: received message header
920 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900921 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900923 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924 * Returns 0 if successful, otherwise errno
925 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800926static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400927 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928{
929 u32 anc_data[3];
930 u32 err;
931 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700932 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100933 int res;
934
935 if (likely(m->msg_controllen == 0))
936 return 0;
937
938 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100939 err = msg ? msg_errcode(msg) : 0;
940 if (unlikely(err)) {
941 anc_data[0] = err;
942 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000943 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
944 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100945 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000946 if (anc_data[1]) {
947 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
948 msg_data(msg));
949 if (res)
950 return res;
951 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100952 }
953
954 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
956 switch (dest_type) {
957 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700958 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959 anc_data[0] = msg_nametype(msg);
960 anc_data[1] = msg_namelower(msg);
961 anc_data[2] = msg_namelower(msg);
962 break;
963 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700964 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100965 anc_data[0] = msg_nametype(msg);
966 anc_data[1] = msg_namelower(msg);
967 anc_data[2] = msg_nameupper(msg);
968 break;
969 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700970 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100971 anc_data[0] = tport->conn_type;
972 anc_data[1] = tport->conn_instance;
973 anc_data[2] = tport->conn_instance;
974 break;
975 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700976 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100977 }
Allan Stephens2db99832010-12-31 18:59:33 +0000978 if (has_name) {
979 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
980 if (res)
981 return res;
982 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100983
984 return 0;
985}
986
Ying Xue9bbb4ec2014-01-17 09:50:07 +0800987static int tipc_wait_for_rcvmsg(struct socket *sock, long timeo)
988{
989 struct sock *sk = sock->sk;
990 DEFINE_WAIT(wait);
991 int err;
992
993 for (;;) {
994 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +0100995 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +0800996 if (sock->state == SS_DISCONNECTING) {
997 err = -ENOTCONN;
998 break;
999 }
1000 release_sock(sk);
1001 timeo = schedule_timeout(timeo);
1002 lock_sock(sk);
1003 }
1004 err = 0;
1005 if (!skb_queue_empty(&sk->sk_receive_queue))
1006 break;
1007 err = sock_intr_errno(timeo);
1008 if (signal_pending(current))
1009 break;
1010 err = -EAGAIN;
1011 if (!timeo)
1012 break;
1013 }
1014 finish_wait(sk_sleep(sk), &wait);
1015 return err;
1016}
1017
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001018/**
Ying Xue247f0f32014-02-18 16:06:46 +08001019 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020 * @iocb: (unused)
1021 * @m: descriptor for message info
1022 * @buf_len: total size of user buffer area
1023 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001024 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1026 * If the complete message doesn't fit in user area, truncate it.
1027 *
1028 * Returns size of returned message data, errno otherwise
1029 */
Ying Xue247f0f32014-02-18 16:06:46 +08001030static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1031 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001033 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001034 struct tipc_sock *tsk = tipc_sk(sk);
1035 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036 struct sk_buff *buf;
1037 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001038 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001039 unsigned int sz;
1040 u32 err;
1041 int res;
1042
Allan Stephens0c3141e2008-04-15 00:22:02 -07001043 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001044 if (unlikely(!buf_len))
1045 return -EINVAL;
1046
Allan Stephens0c3141e2008-04-15 00:22:02 -07001047 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048
Allan Stephens0c3141e2008-04-15 00:22:02 -07001049 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 res = -ENOTCONN;
1051 goto exit;
1052 }
1053
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001054 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001055restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001056
Allan Stephens0c3141e2008-04-15 00:22:02 -07001057 /* Look for a message in receive queue; wait if necessary */
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001058 res = tipc_wait_for_rcvmsg(sock, timeo);
1059 if (res)
1060 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001061
1062 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001063 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064 msg = buf_msg(buf);
1065 sz = msg_data_sz(msg);
1066 err = msg_errcode(msg);
1067
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001070 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 goto restart;
1072 }
1073
1074 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001075 set_orig_addr(m, msg);
1076
1077 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001078 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001079 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001080 goto exit;
1081
1082 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083 if (!err) {
1084 if (unlikely(buf_len < sz)) {
1085 sz = buf_len;
1086 m->msg_flags |= MSG_TRUNC;
1087 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001088 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1089 m->msg_iov, sz);
1090 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001091 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092 res = sz;
1093 } else {
1094 if ((sock->state == SS_READY) ||
1095 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1096 res = 0;
1097 else
1098 res = -ECONNRESET;
1099 }
1100
1101 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001102 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001103 if ((sock->state != SS_READY) &&
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001104 (++port->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1105 tipc_acknowledge(port->ref, port->conn_unacked);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001106 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001107 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001109 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001110 return res;
1111}
1112
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001113/**
Ying Xue247f0f32014-02-18 16:06:46 +08001114 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115 * @iocb: (unused)
1116 * @m: descriptor for message info
1117 * @buf_len: total size of user buffer area
1118 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001119 *
1120 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121 * will optionally wait for more; never truncates data.
1122 *
1123 * Returns size of returned message data, errno otherwise
1124 */
Ying Xue247f0f32014-02-18 16:06:46 +08001125static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1126 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001128 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001129 struct tipc_sock *tsk = tipc_sk(sk);
1130 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001131 struct sk_buff *buf;
1132 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001133 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001135 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001138 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001139
1140 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 if (unlikely(!buf_len))
1142 return -EINVAL;
1143
Allan Stephens0c3141e2008-04-15 00:22:02 -07001144 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001146 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001147 res = -ENOTCONN;
1148 goto exit;
1149 }
1150
Florian Westphal3720d402010-08-17 11:00:04 +00001151 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001152 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001153
Allan Stephens0c3141e2008-04-15 00:22:02 -07001154restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001155 /* Look for a message in receive queue; wait if necessary */
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001156 res = tipc_wait_for_rcvmsg(sock, timeo);
1157 if (res)
1158 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001159
1160 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001161 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001162 msg = buf_msg(buf);
1163 sz = msg_data_sz(msg);
1164 err = msg_errcode(msg);
1165
1166 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001168 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 goto restart;
1170 }
1171
1172 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 if (sz_copied == 0) {
1174 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001175 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001176 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177 goto exit;
1178 }
1179
1180 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001181 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001182 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183
Allan Stephens0232fd02011-02-21 09:45:40 -05001184 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185 needed = (buf_len - sz_copied);
1186 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001187
1188 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1189 m->msg_iov, sz_to_copy);
1190 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001192
Per Lidenb97bf3f2006-01-02 19:04:38 +01001193 sz_copied += sz_to_copy;
1194
1195 if (sz_to_copy < sz) {
1196 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001197 TIPC_SKB_CB(buf)->handle =
1198 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 goto exit;
1200 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201 } else {
1202 if (sz_copied != 0)
1203 goto exit; /* can't add error msg to valid data */
1204
1205 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1206 res = 0;
1207 else
1208 res = -ECONNRESET;
1209 }
1210
1211 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001212 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001213 if (unlikely(++port->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1214 tipc_acknowledge(port->ref, port->conn_unacked);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001215 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001216 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001217
1218 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001219 if ((sz_copied < buf_len) && /* didn't get all requested data */
1220 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001221 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001222 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1223 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001224 goto restart;
1225
1226exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001227 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001228 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001229}
1230
1231/**
Ying Xuef288bef2012-08-21 11:16:57 +08001232 * tipc_write_space - wake up thread if port congestion is released
1233 * @sk: socket
1234 */
1235static void tipc_write_space(struct sock *sk)
1236{
1237 struct socket_wq *wq;
1238
1239 rcu_read_lock();
1240 wq = rcu_dereference(sk->sk_wq);
1241 if (wq_has_sleeper(wq))
1242 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1243 POLLWRNORM | POLLWRBAND);
1244 rcu_read_unlock();
1245}
1246
1247/**
1248 * tipc_data_ready - wake up threads to indicate messages have been received
1249 * @sk: socket
1250 * @len: the length of messages
1251 */
David S. Miller676d2362014-04-11 16:15:36 -04001252static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001253{
1254 struct socket_wq *wq;
1255
1256 rcu_read_lock();
1257 wq = rcu_dereference(sk->sk_wq);
1258 if (wq_has_sleeper(wq))
1259 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1260 POLLRDNORM | POLLRDBAND);
1261 rcu_read_unlock();
1262}
1263
1264/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001265 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001266 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001267 * @msg: message
1268 *
1269 * Returns TIPC error status code and socket error status code
1270 * once it encounters some errors
1271 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001272static u32 filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001273{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001274 struct sock *sk = &tsk->sk;
1275 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001276 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001277 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001278
Ying Xue7e6c1312012-11-29 18:39:14 -05001279 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001280 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001281
1282 if (msg_mcast(msg))
1283 return retval;
1284
1285 switch ((int)sock->state) {
1286 case SS_CONNECTED:
1287 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001288 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001289 if (unlikely(msg_errcode(msg))) {
1290 sock->state = SS_DISCONNECTING;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001291 __tipc_port_disconnect(port);
Ying Xue7e6c1312012-11-29 18:39:14 -05001292 }
1293 retval = TIPC_OK;
1294 }
1295 break;
1296 case SS_CONNECTING:
1297 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001298 if (unlikely(msg_errcode(msg))) {
1299 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001300 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001301 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001302 break;
1303 }
1304
1305 if (unlikely(!msg_connected(msg)))
1306 break;
1307
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001308 res = auto_connect(tsk, msg);
Ying Xue584d24b2012-11-29 18:51:19 -05001309 if (res) {
1310 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001311 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001312 retval = TIPC_OK;
1313 break;
1314 }
1315
1316 /* If an incoming message is an 'ACK-', it should be
1317 * discarded here because it doesn't contain useful
1318 * data. In addition, we should try to wake up
1319 * connect() routine if sleeping.
1320 */
1321 if (msg_data_sz(msg) == 0) {
1322 kfree_skb(*buf);
1323 *buf = NULL;
1324 if (waitqueue_active(sk_sleep(sk)))
1325 wake_up_interruptible(sk_sleep(sk));
1326 }
1327 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001328 break;
1329 case SS_LISTENING:
1330 case SS_UNCONNECTED:
1331 /* Accept only SYN message */
1332 if (!msg_connected(msg) && !(msg_errcode(msg)))
1333 retval = TIPC_OK;
1334 break;
1335 case SS_DISCONNECTING:
1336 break;
1337 default:
1338 pr_err("Unknown socket state %u\n", sock->state);
1339 }
1340 return retval;
1341}
1342
1343/**
Ying Xueaba79f32013-01-20 23:30:09 +01001344 * rcvbuf_limit - get proper overload limit of socket receive queue
1345 * @sk: socket
1346 * @buf: message
1347 *
1348 * For all connection oriented messages, irrespective of importance,
1349 * the default overload value (i.e. 67MB) is set as limit.
1350 *
1351 * For all connectionless messages, by default new queue limits are
1352 * as belows:
1353 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001354 * TIPC_LOW_IMPORTANCE (4 MB)
1355 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1356 * TIPC_HIGH_IMPORTANCE (16 MB)
1357 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001358 *
1359 * Returns overload limit according to corresponding message importance
1360 */
1361static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1362{
1363 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001364
1365 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001366 return sysctl_tipc_rmem[2];
1367
1368 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1369 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001370}
1371
1372/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001373 * filter_rcv - validate incoming message
1374 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001376 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001377 * Enqueues message on receive queue if acceptable; optionally handles
1378 * disconnect indication for a connected socket.
1379 *
1380 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001381 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001382 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1383 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001384static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001386 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001387 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001388 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001389 unsigned int limit = rcvbuf_limit(sk, buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001390 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001391
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001393 if (msg_type(msg) > TIPC_DIRECT_MSG)
1394 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001395
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001397 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001399 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001400 res = filter_connect(tsk, &buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001401 if (res != TIPC_OK || buf == NULL)
1402 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 }
1404
1405 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001406 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1407 return TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001408
Ying Xueaba79f32013-01-20 23:30:09 +01001409 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001410 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001411 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001412 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001413
David S. Miller676d2362014-04-11 16:15:36 -04001414 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001415 return TIPC_OK;
1416}
1417
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001418/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001419 * backlog_rcv - handle incoming message from backlog queue
1420 * @sk: socket
1421 * @buf: message
1422 *
1423 * Caller must hold socket lock, but not port lock.
1424 *
1425 * Returns 0
1426 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001427static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1428{
1429 u32 res;
1430
1431 res = filter_rcv(sk, buf);
1432 if (res)
1433 tipc_reject_msg(buf, res);
1434 return 0;
1435}
1436
1437/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001438 * tipc_sk_rcv - handle incoming message
1439 * @sk: socket receiving message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001440 * @buf: message
1441 *
1442 * Called with port lock already taken.
1443 *
1444 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1445 */
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001446u32 tipc_sk_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001447{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001448 u32 res;
1449
1450 /*
1451 * Process message if socket is unlocked; otherwise add to backlog queue
1452 *
1453 * This code is based on sk_receive_skb(), but must be distinct from it
1454 * since a TIPC-specific filter/reject mechanism is utilized
1455 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001456 bh_lock_sock(sk);
1457 if (!sock_owned_by_user(sk)) {
1458 res = filter_rcv(sk, buf);
1459 } else {
Ying Xueaba79f32013-01-20 23:30:09 +01001460 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
Zhu Yi53eecb12010-03-04 18:01:45 +00001461 res = TIPC_ERR_OVERLOAD;
1462 else
1463 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001464 }
1465 bh_unlock_sock(sk);
1466
1467 return res;
1468}
1469
Ying Xue78eb3a52014-01-17 09:50:03 +08001470static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1471{
1472 struct sock *sk = sock->sk;
1473 DEFINE_WAIT(wait);
1474 int done;
1475
1476 do {
1477 int err = sock_error(sk);
1478 if (err)
1479 return err;
1480 if (!*timeo_p)
1481 return -ETIMEDOUT;
1482 if (signal_pending(current))
1483 return sock_intr_errno(*timeo_p);
1484
1485 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1486 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1487 finish_wait(sk_sleep(sk), &wait);
1488 } while (!done);
1489 return 0;
1490}
1491
Per Lidenb97bf3f2006-01-02 19:04:38 +01001492/**
Ying Xue247f0f32014-02-18 16:06:46 +08001493 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001494 * @sock: socket structure
1495 * @dest: socket address for destination port
1496 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001497 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001498 *
1499 * Returns 0 on success, errno otherwise
1500 */
Ying Xue247f0f32014-02-18 16:06:46 +08001501static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1502 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001503{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001504 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001505 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1506 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001507 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1508 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001509 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001510
Allan Stephens0c3141e2008-04-15 00:22:02 -07001511 lock_sock(sk);
1512
Allan Stephensb89741a2008-04-15 00:20:37 -07001513 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001514 if (sock->state == SS_READY) {
1515 res = -EOPNOTSUPP;
1516 goto exit;
1517 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001518
Allan Stephensb89741a2008-04-15 00:20:37 -07001519 /*
1520 * Reject connection attempt using multicast address
1521 *
1522 * Note: send_msg() validates the rest of the address fields,
1523 * so there's no need to do it here
1524 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001525 if (dst->addrtype == TIPC_ADDR_MCAST) {
1526 res = -EINVAL;
1527 goto exit;
1528 }
1529
Ying Xue78eb3a52014-01-17 09:50:03 +08001530 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001531 switch (sock->state) {
1532 case SS_UNCONNECTED:
1533 /* Send a 'SYN-' to destination */
1534 m.msg_name = dest;
1535 m.msg_namelen = destlen;
1536
1537 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1538 * indicate send_msg() is never blocked.
1539 */
1540 if (!timeout)
1541 m.msg_flags = MSG_DONTWAIT;
1542
Ying Xue247f0f32014-02-18 16:06:46 +08001543 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001544 if ((res < 0) && (res != -EWOULDBLOCK))
1545 goto exit;
1546
1547 /* Just entered SS_CONNECTING state; the only
1548 * difference is that return value in non-blocking
1549 * case is EINPROGRESS, rather than EALREADY.
1550 */
1551 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001552 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001553 if (previous == SS_CONNECTING)
1554 res = -EALREADY;
1555 if (!timeout)
1556 goto exit;
1557 timeout = msecs_to_jiffies(timeout);
1558 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1559 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001560 break;
1561 case SS_CONNECTED:
1562 res = -EISCONN;
1563 break;
1564 default:
1565 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001566 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001567 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001568exit:
1569 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001570 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001571}
1572
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001573/**
Ying Xue247f0f32014-02-18 16:06:46 +08001574 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001575 * @sock: socket structure
1576 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001577 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001578 * Returns 0 on success, errno otherwise
1579 */
Ying Xue247f0f32014-02-18 16:06:46 +08001580static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001581{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001582 struct sock *sk = sock->sk;
1583 int res;
1584
1585 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001586
Ying Xue245f3d32011-07-06 06:01:13 -04001587 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001588 res = -EINVAL;
1589 else {
1590 sock->state = SS_LISTENING;
1591 res = 0;
1592 }
1593
1594 release_sock(sk);
1595 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001596}
1597
Ying Xue6398e232014-01-17 09:50:04 +08001598static int tipc_wait_for_accept(struct socket *sock, long timeo)
1599{
1600 struct sock *sk = sock->sk;
1601 DEFINE_WAIT(wait);
1602 int err;
1603
1604 /* True wake-one mechanism for incoming connections: only
1605 * one process gets woken up, not the 'whole herd'.
1606 * Since we do not 'race & poll' for established sockets
1607 * anymore, the common case will execute the loop only once.
1608 */
1609 for (;;) {
1610 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1611 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001612 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001613 release_sock(sk);
1614 timeo = schedule_timeout(timeo);
1615 lock_sock(sk);
1616 }
1617 err = 0;
1618 if (!skb_queue_empty(&sk->sk_receive_queue))
1619 break;
1620 err = -EINVAL;
1621 if (sock->state != SS_LISTENING)
1622 break;
1623 err = sock_intr_errno(timeo);
1624 if (signal_pending(current))
1625 break;
1626 err = -EAGAIN;
1627 if (!timeo)
1628 break;
1629 }
1630 finish_wait(sk_sleep(sk), &wait);
1631 return err;
1632}
1633
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001634/**
Ying Xue247f0f32014-02-18 16:06:46 +08001635 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001636 * @sock: listening socket
1637 * @newsock: new socket that is to be connected
1638 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001639 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001640 * Returns 0 on success, errno otherwise
1641 */
Ying Xue247f0f32014-02-18 16:06:46 +08001642static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001643{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001644 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001645 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001646 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001647 struct tipc_msg *msg;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001648 struct tipc_portid peer;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001649 u32 new_ref;
Ying Xue6398e232014-01-17 09:50:04 +08001650 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001651 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652
Allan Stephens0c3141e2008-04-15 00:22:02 -07001653 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001654
Allan Stephens0c3141e2008-04-15 00:22:02 -07001655 if (sock->state != SS_LISTENING) {
1656 res = -EINVAL;
1657 goto exit;
1658 }
Ying Xue6398e232014-01-17 09:50:04 +08001659 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1660 res = tipc_wait_for_accept(sock, timeo);
1661 if (res)
1662 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001663
1664 buf = skb_peek(&sk->sk_receive_queue);
1665
Ying Xuec5fa7b32013-06-17 10:54:39 -04001666 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001667 if (res)
1668 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001669
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001670 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001671 new_port = &tipc_sk(new_sk)->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001672 new_ref = new_port->ref;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001673 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001674
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001675 /* we lock on new_sk; but lockdep sees the lock on sk */
1676 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001677
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001678 /*
1679 * Reject any stray messages received by new socket
1680 * before the socket lock was taken (very, very unlikely)
1681 */
1682 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001684 /* Connect new socket to it's peer */
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001685 peer.ref = msg_origport(msg);
1686 peer.node = msg_orignode(msg);
1687 tipc_port_connect(new_ref, &peer);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001688 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001689
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001690 tipc_port_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001691 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001692 new_port->conn_type = msg_nametype(msg);
1693 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001694 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001695
1696 /*
1697 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1698 * Respond to 'SYN+' by queuing it on new socket.
1699 */
1700 if (!msg_data_sz(msg)) {
1701 struct msghdr m = {NULL,};
1702
1703 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001704 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001705 } else {
1706 __skb_dequeue(&sk->sk_receive_queue);
1707 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001708 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001709 }
1710 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001711exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001712 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001713 return res;
1714}
1715
1716/**
Ying Xue247f0f32014-02-18 16:06:46 +08001717 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001718 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001719 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001720 *
1721 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001722 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001723 * Returns 0 on success, errno otherwise
1724 */
Ying Xue247f0f32014-02-18 16:06:46 +08001725static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001727 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001728 struct tipc_sock *tsk = tipc_sk(sk);
1729 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001730 struct sk_buff *buf;
1731 int res;
1732
Allan Stephense247a8f2008-03-06 15:05:38 -08001733 if (how != SHUT_RDWR)
1734 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001735
Allan Stephens0c3141e2008-04-15 00:22:02 -07001736 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001737
1738 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001739 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001740 case SS_CONNECTED:
1741
Per Lidenb97bf3f2006-01-02 19:04:38 +01001742restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001743 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001744 buf = __skb_dequeue(&sk->sk_receive_queue);
1745 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001746 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001747 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001748 goto restart;
1749 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001750 tipc_port_disconnect(port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001751 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001752 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001753 tipc_port_shutdown(port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001754 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001755
1756 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001757
1758 /* fall through */
1759
1760 case SS_DISCONNECTING:
1761
Ying Xue75031152012-10-29 09:38:15 -04001762 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001763 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001764
1765 /* Wake up anyone sleeping in poll */
1766 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001767 res = 0;
1768 break;
1769
1770 default:
1771 res = -ENOTCONN;
1772 }
1773
Allan Stephens0c3141e2008-04-15 00:22:02 -07001774 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001775 return res;
1776}
1777
1778/**
Ying Xue247f0f32014-02-18 16:06:46 +08001779 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001780 * @sock: socket structure
1781 * @lvl: option level
1782 * @opt: option identifier
1783 * @ov: pointer to new option value
1784 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001785 *
1786 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001788 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001789 * Returns 0 on success, errno otherwise
1790 */
Ying Xue247f0f32014-02-18 16:06:46 +08001791static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1792 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001794 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001795 struct tipc_sock *tsk = tipc_sk(sk);
1796 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001797 u32 value;
1798 int res;
1799
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001800 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1801 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001802 if (lvl != SOL_TIPC)
1803 return -ENOPROTOOPT;
1804 if (ol < sizeof(value))
1805 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001806 res = get_user(value, (u32 __user *)ov);
1807 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001808 return res;
1809
Allan Stephens0c3141e2008-04-15 00:22:02 -07001810 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001811
Per Lidenb97bf3f2006-01-02 19:04:38 +01001812 switch (opt) {
1813 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001814 tipc_port_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001815 break;
1816 case TIPC_SRC_DROPPABLE:
1817 if (sock->type != SOCK_STREAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001818 tipc_port_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001819 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001820 res = -ENOPROTOOPT;
1821 break;
1822 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001823 tipc_port_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001824 break;
1825 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001826 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001827 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001828 break;
1829 default:
1830 res = -EINVAL;
1831 }
1832
Allan Stephens0c3141e2008-04-15 00:22:02 -07001833 release_sock(sk);
1834
Per Lidenb97bf3f2006-01-02 19:04:38 +01001835 return res;
1836}
1837
1838/**
Ying Xue247f0f32014-02-18 16:06:46 +08001839 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001840 * @sock: socket structure
1841 * @lvl: option level
1842 * @opt: option identifier
1843 * @ov: receptacle for option value
1844 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001845 *
1846 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001847 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001848 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001849 * Returns 0 on success, errno otherwise
1850 */
Ying Xue247f0f32014-02-18 16:06:46 +08001851static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1852 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001853{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001854 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001855 struct tipc_sock *tsk = tipc_sk(sk);
1856 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001857 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001858 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001859 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001860
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001861 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1862 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001863 if (lvl != SOL_TIPC)
1864 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001865 res = get_user(len, ol);
1866 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001867 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001868
Allan Stephens0c3141e2008-04-15 00:22:02 -07001869 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001870
1871 switch (opt) {
1872 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001873 value = tipc_port_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001874 break;
1875 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001876 value = tipc_port_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001877 break;
1878 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001879 value = tipc_port_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001880 break;
1881 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001882 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001883 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001884 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001885 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001886 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001887 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001888 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001889 value = skb_queue_len(&sk->sk_receive_queue);
1890 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001891 default:
1892 res = -EINVAL;
1893 }
1894
Allan Stephens0c3141e2008-04-15 00:22:02 -07001895 release_sock(sk);
1896
Paul Gortmaker25860c32010-12-31 18:59:31 +00001897 if (res)
1898 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001899
Paul Gortmaker25860c32010-12-31 18:59:31 +00001900 if (len < sizeof(value))
1901 return -EINVAL;
1902
1903 if (copy_to_user(ov, &value, sizeof(value)))
1904 return -EFAULT;
1905
1906 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907}
1908
Erik Hugne78acb1f2014-04-24 16:26:47 +02001909int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
1910{
1911 struct tipc_sioc_ln_req lnr;
1912 void __user *argp = (void __user *)arg;
1913
1914 switch (cmd) {
1915 case SIOCGETLINKNAME:
1916 if (copy_from_user(&lnr, argp, sizeof(lnr)))
1917 return -EFAULT;
1918 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
1919 lnr.linkname, TIPC_MAX_LINK_NAME)) {
1920 if (copy_to_user(argp, &lnr, sizeof(lnr)))
1921 return -EFAULT;
1922 return 0;
1923 }
1924 return -EADDRNOTAVAIL;
1925 break;
1926 default:
1927 return -ENOIOCTLCMD;
1928 }
1929}
1930
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001931/* Protocol switches for the various types of TIPC sockets */
1932
Florian Westphalbca65ea2008-02-07 18:18:01 -08001933static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001934 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001935 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001936 .release = tipc_release,
1937 .bind = tipc_bind,
1938 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001939 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001940 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08001941 .getname = tipc_getname,
1942 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02001943 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001944 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08001945 .shutdown = tipc_shutdown,
1946 .setsockopt = tipc_setsockopt,
1947 .getsockopt = tipc_getsockopt,
1948 .sendmsg = tipc_sendmsg,
1949 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001950 .mmap = sock_no_mmap,
1951 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001952};
1953
Florian Westphalbca65ea2008-02-07 18:18:01 -08001954static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001955 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001956 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001957 .release = tipc_release,
1958 .bind = tipc_bind,
1959 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001960 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08001961 .accept = tipc_accept,
1962 .getname = tipc_getname,
1963 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02001964 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08001965 .listen = tipc_listen,
1966 .shutdown = tipc_shutdown,
1967 .setsockopt = tipc_setsockopt,
1968 .getsockopt = tipc_getsockopt,
1969 .sendmsg = tipc_send_packet,
1970 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001971 .mmap = sock_no_mmap,
1972 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001973};
1974
Florian Westphalbca65ea2008-02-07 18:18:01 -08001975static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001976 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001977 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001978 .release = tipc_release,
1979 .bind = tipc_bind,
1980 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001981 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08001982 .accept = tipc_accept,
1983 .getname = tipc_getname,
1984 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02001985 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08001986 .listen = tipc_listen,
1987 .shutdown = tipc_shutdown,
1988 .setsockopt = tipc_setsockopt,
1989 .getsockopt = tipc_getsockopt,
1990 .sendmsg = tipc_send_stream,
1991 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001992 .mmap = sock_no_mmap,
1993 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001994};
1995
Florian Westphalbca65ea2008-02-07 18:18:01 -08001996static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001997 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001998 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04001999 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002000};
2001
2002static struct proto tipc_proto = {
2003 .name = "TIPC",
2004 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002005 .obj_size = sizeof(struct tipc_sock),
2006 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002007};
2008
Ying Xuec5fa7b32013-06-17 10:54:39 -04002009static struct proto tipc_proto_kern = {
2010 .name = "TIPC",
2011 .obj_size = sizeof(struct tipc_sock),
2012 .sysctl_rmem = sysctl_tipc_rmem
2013};
2014
Per Lidenb97bf3f2006-01-02 19:04:38 +01002015/**
Per Liden4323add2006-01-18 00:38:21 +01002016 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002017 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002018 * Returns 0 on success, errno otherwise
2019 */
Per Liden4323add2006-01-18 00:38:21 +01002020int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002021{
2022 int res;
2023
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002024 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002025 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002026 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002027 goto out;
2028 }
2029
2030 res = sock_register(&tipc_family_ops);
2031 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002032 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002033 proto_unregister(&tipc_proto);
2034 goto out;
2035 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002036 out:
2037 return res;
2038}
2039
2040/**
Per Liden4323add2006-01-18 00:38:21 +01002041 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002042 */
Per Liden4323add2006-01-18 00:38:21 +01002043void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002044{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002045 sock_unregister(tipc_family_ops.family);
2046 proto_unregister(&tipc_proto);
2047}