blob: fb885977bd2a534e87006bb47f7db546508cbc1a [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 Maloye643df12012-11-27 06:15:29 -05004 * Copyright (c) 2001-2007, 2012 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"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039
Erik Hugne2cf8aa12012-06-29 00:16:37 -040040#include <linux/export.h>
41#include <net/sock.h>
42
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
48struct tipc_sock {
49 struct sock sk;
50 struct tipc_port *p;
Allan Stephens2da59912008-07-14 22:43:32 -070051 struct tipc_portid peer_name;
Allan Stephensa0f40f02011-05-26 13:44:34 -040052 unsigned int conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +010053};
54
Allan Stephens0c3141e2008-04-15 00:22:02 -070055#define tipc_sk(sk) ((struct tipc_sock *)(sk))
Joe Perchese3192692012-06-03 17:41:40 +000056#define tipc_sk_port(sk) (tipc_sk(sk)->p)
Per Lidenb97bf3f2006-01-02 19:04:38 +010057
Allan Stephens0c3141e2008-04-15 00:22:02 -070058static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +010059static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
60static void wakeupdispatch(struct tipc_port *tport);
Ying Xuef288bef2012-08-21 11:16:57 +080061static void tipc_data_ready(struct sock *sk, int len);
62static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080063static int tipc_release(struct socket *sock);
64static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Per Lidenb97bf3f2006-01-02 19:04:38 +010065
Florian Westphalbca65ea2008-02-07 18:18:01 -080066static const struct proto_ops packet_ops;
67static const struct proto_ops stream_ops;
68static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010069
70static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040071static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010072
Allan Stephense3ec9c72010-12-31 18:59:34 +000073static int sockets_enabled;
Per Lidenb97bf3f2006-01-02 19:04:38 +010074
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090075/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070076 * Revised TIPC socket locking policy:
77 *
78 * Most socket operations take the standard socket lock when they start
79 * and hold it until they finish (or until they need to sleep). Acquiring
80 * this lock grants the owner exclusive access to the fields of the socket
81 * data structures, with the exception of the backlog queue. A few socket
82 * operations can be done without taking the socket lock because they only
83 * read socket information that never changes during the life of the socket.
84 *
85 * Socket operations may acquire the lock for the associated TIPC port if they
86 * need to perform an operation on the port. If any routine needs to acquire
87 * both the socket lock and the port lock it must take the socket lock first
88 * to avoid the risk of deadlock.
89 *
90 * The dispatcher handling incoming messages cannot grab the socket lock in
91 * the standard fashion, since invoked it runs at the BH level and cannot block.
92 * Instead, it checks to see if the socket lock is currently owned by someone,
93 * and either handles the message itself or adds it to the socket's backlog
94 * queue; in the latter case the queued message is processed once the process
95 * owning the socket lock releases it.
96 *
97 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
98 * the problem of a blocked socket operation preventing any other operations
99 * from occurring. However, applications must be careful if they have
100 * multiple threads trying to send (or receive) on the same socket, as these
101 * operations might interfere with each other. For example, doing a connect
102 * and a receive at the same time might allow the receive to consume the
103 * ACK message meant for the connect. While additional work could be done
104 * to try and overcome this, it doesn't seem to be worthwhile at the present.
105 *
106 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
107 * that another operation that must be performed in a non-blocking manner is
108 * not delayed for very long because the lock has already been taken.
109 *
110 * NOTE: This code assumes that certain fields of a port/socket pair are
111 * constant over its lifetime; such fields can be examined without taking
112 * the socket lock and/or port lock, and do not need to be re-read even
113 * after resuming processing after waiting. These fields include:
114 * - socket type
115 * - pointer to socket sk structure (aka tipc_sock structure)
116 * - pointer to port structure
117 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119
120/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700121 * advance_rx_queue - discard first buffer in socket receive queue
122 *
123 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700125static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400127 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128}
129
130/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700131 * reject_rx_queue - reject all buffers in socket receive queue
132 *
133 * Caller must hold socket lock
134 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700135static void reject_rx_queue(struct sock *sk)
136{
137 struct sk_buff *buf;
138
Ying Xue9da3d472012-11-27 06:15:27 -0500139 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700140 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700141}
142
143/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400144 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700145 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146 * @sock: pre-allocated socket structure
147 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800148 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900149 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700150 * This routine creates additional data structures used by the TIPC socket,
151 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152 *
153 * Returns 0 on success, errno otherwise
154 */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400155static int tipc_sk_create(struct net *net, struct socket *sock, int protocol,
156 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700158 const struct proto_ops *ops;
159 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700161 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700162
163 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 if (unlikely(protocol != 0))
165 return -EPROTONOSUPPORT;
166
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 switch (sock->type) {
168 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700169 ops = &stream_ops;
170 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171 break;
172 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700173 ops = &packet_ops;
174 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 break;
176 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700178 ops = &msg_ops;
179 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 break;
Allan Stephens49978652006-06-25 23:47:18 -0700181 default:
Allan Stephens49978652006-06-25 23:47:18 -0700182 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 }
184
Allan Stephens0c3141e2008-04-15 00:22:02 -0700185 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400186 if (!kern)
187 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
188 else
189 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
190
Allan Stephens0c3141e2008-04-15 00:22:02 -0700191 if (sk == NULL)
192 return -ENOMEM;
193
194 /* Allocate TIPC port for socket to use */
Ying Xue3c5db8e2013-06-17 10:54:44 -0400195 tp_ptr = tipc_createport(sk, &dispatch, &wakeupdispatch,
196 TIPC_LOW_IMPORTANCE);
Allan Stephens0ea52242008-07-14 22:42:19 -0700197 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700198 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199 return -ENOMEM;
200 }
201
Allan Stephens0c3141e2008-04-15 00:22:02 -0700202 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700203 sock->ops = ops;
204 sock->state = state;
205
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700207 sk->sk_backlog_rcv = backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400208 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800209 sk->sk_data_ready = tipc_data_ready;
210 sk->sk_write_space = tipc_write_space;
Allan Stephens0ea52242008-07-14 22:42:19 -0700211 tipc_sk(sk)->p = tp_ptr;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400212 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700214 spin_unlock_bh(tp_ptr->lock);
215
Allan Stephens0c3141e2008-04-15 00:22:02 -0700216 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700217 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700218 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700219 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700220 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221
Per Lidenb97bf3f2006-01-02 19:04:38 +0100222 return 0;
223}
224
225/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400226 * tipc_sock_create_local - create TIPC socket from inside TIPC module
227 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
228 *
229 * We cannot use sock_creat_kern here because it bumps module user count.
230 * Since socket owner and creator is the same module we must make sure
231 * that module count remains zero for module local sockets, otherwise
232 * we cannot do rmmod.
233 *
234 * Returns 0 on success, errno otherwise
235 */
236int tipc_sock_create_local(int type, struct socket **res)
237{
238 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400239
240 rc = sock_create_lite(AF_TIPC, type, 0, res);
241 if (rc < 0) {
242 pr_err("Failed to create kernel socket\n");
243 return rc;
244 }
245 tipc_sk_create(&init_net, *res, 0, 1);
246
Ying Xuec5fa7b32013-06-17 10:54:39 -0400247 return 0;
248}
249
250/**
251 * tipc_sock_release_local - release socket created by tipc_sock_create_local
252 * @sock: the socket to be released.
253 *
254 * Module reference count is not incremented when such sockets are created,
255 * so we must keep it from being decremented when they are released.
256 */
257void tipc_sock_release_local(struct socket *sock)
258{
Ying Xue247f0f32014-02-18 16:06:46 +0800259 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400260 sock->ops = NULL;
261 sock_release(sock);
262}
263
264/**
265 * tipc_sock_accept_local - accept a connection on a socket created
266 * with tipc_sock_create_local. Use this function to avoid that
267 * module reference count is inadvertently incremented.
268 *
269 * @sock: the accepting socket
270 * @newsock: reference to the new socket to be created
271 * @flags: socket flags
272 */
273
274int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400275 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400276{
277 struct sock *sk = sock->sk;
278 int ret;
279
280 ret = sock_create_lite(sk->sk_family, sk->sk_type,
281 sk->sk_protocol, newsock);
282 if (ret < 0)
283 return ret;
284
Ying Xue247f0f32014-02-18 16:06:46 +0800285 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400286 if (ret < 0) {
287 sock_release(*newsock);
288 return ret;
289 }
290 (*newsock)->ops = sock->ops;
291 return ret;
292}
293
294/**
Ying Xue247f0f32014-02-18 16:06:46 +0800295 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296 * @sock: socket to destroy
297 *
298 * This routine cleans up any messages that are still queued on the socket.
299 * For DGRAM and RDM socket types, all queued messages are rejected.
300 * For SEQPACKET and STREAM socket types, the first message is rejected
301 * and any others are discarded. (If the first message on a STREAM socket
302 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900303 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 * NOTE: Rejected messages are not necessarily returned to the sender! They
305 * are returned or discarded according to the "destination droppable" setting
306 * specified for the message by the sender.
307 *
308 * Returns 0 on success, errno otherwise
309 */
Ying Xue247f0f32014-02-18 16:06:46 +0800310static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700313 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700315 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316
Allan Stephens0c3141e2008-04-15 00:22:02 -0700317 /*
318 * Exit if socket isn't fully initialized (occurs when a failed accept()
319 * releases a pre-allocated child socket that was never used)
320 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700321 if (sk == NULL)
322 return 0;
323
324 tport = tipc_sk_port(sk);
325 lock_sock(sk);
326
327 /*
328 * Reject all unreceived messages, except on an active connection
329 * (which disconnects locally & sends a 'FIN+' to peer)
330 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700332 buf = __skb_dequeue(&sk->sk_receive_queue);
333 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334 break;
Ying Xue40682432013-10-18 07:23:16 +0200335 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400336 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700337 else {
338 if ((sock->state == SS_CONNECTING) ||
339 (sock->state == SS_CONNECTED)) {
340 sock->state = SS_DISCONNECTING;
Ying Xue247f0f32014-02-18 16:06:46 +0800341 tipc_port_disconnect(tport->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700342 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700344 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 }
346
Allan Stephens0c3141e2008-04-15 00:22:02 -0700347 /*
348 * Delete TIPC port; this ensures no more messages are queued
349 * (also disconnects an active connection & sends a 'FIN-' to peer)
350 */
Ying Xue84602762013-12-27 10:18:28 +0800351 res = tipc_deleteport(tport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352
Allan Stephens0c3141e2008-04-15 00:22:02 -0700353 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100354 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355
Allan Stephens0c3141e2008-04-15 00:22:02 -0700356 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700357 sock->state = SS_DISCONNECTING;
358 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359
360 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700361 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363 return res;
364}
365
366/**
Ying Xue247f0f32014-02-18 16:06:46 +0800367 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368 * @sock: socket structure
369 * @uaddr: socket address describing name(s) and desired operation
370 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900371 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372 * Name and name sequence binding is indicated using a positive scope value;
373 * a negative scope value unbinds the specified name. Specifying no name
374 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900375 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700377 *
378 * NOTE: This routine doesn't need to take the socket lock since it doesn't
379 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380 */
Ying Xue247f0f32014-02-18 16:06:46 +0800381static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
382 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383{
Ying Xue84602762013-12-27 10:18:28 +0800384 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Ying Xue84602762013-12-27 10:18:28 +0800386 struct tipc_port *tport = tipc_sk_port(sock->sk);
387 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388
Ying Xue84602762013-12-27 10:18:28 +0800389 lock_sock(sk);
390 if (unlikely(!uaddr_len)) {
391 res = tipc_withdraw(tport, 0, NULL);
392 goto exit;
393 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900394
Ying Xue84602762013-12-27 10:18:28 +0800395 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
396 res = -EINVAL;
397 goto exit;
398 }
399 if (addr->family != AF_TIPC) {
400 res = -EAFNOSUPPORT;
401 goto exit;
402 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404 if (addr->addrtype == TIPC_ADDR_NAME)
405 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800406 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
407 res = -EAFNOSUPPORT;
408 goto exit;
409 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900410
Ying Xue13a2e892013-06-17 10:54:40 -0400411 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400412 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800413 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
414 res = -EACCES;
415 goto exit;
416 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400417
Ying Xue84602762013-12-27 10:18:28 +0800418 res = (addr->scope > 0) ?
419 tipc_publish(tport, addr->scope, &addr->addr.nameseq) :
420 tipc_withdraw(tport, -addr->scope, &addr->addr.nameseq);
421exit:
422 release_sock(sk);
423 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424}
425
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900426/**
Ying Xue247f0f32014-02-18 16:06:46 +0800427 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428 * @sock: socket structure
429 * @uaddr: area for returned socket address
430 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700431 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900432 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700434 *
Allan Stephens2da59912008-07-14 22:43:32 -0700435 * NOTE: This routine doesn't need to take the socket lock since it only
436 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000437 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438 */
Ying Xue247f0f32014-02-18 16:06:46 +0800439static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
440 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700443 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000445 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700446 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700447 if ((sock->state != SS_CONNECTED) &&
448 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
449 return -ENOTCONN;
450 addr->addr.id.ref = tsock->peer_name.ref;
451 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700452 } else {
Allan Stephensb924dcf2010-11-30 12:01:03 +0000453 addr->addr.id.ref = tsock->p->ref;
454 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700455 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456
457 *uaddr_len = sizeof(*addr);
458 addr->addrtype = TIPC_ADDR_ID;
459 addr->family = AF_TIPC;
460 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461 addr->addr.name.domain = 0;
462
Allan Stephens0c3141e2008-04-15 00:22:02 -0700463 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464}
465
466/**
Ying Xue247f0f32014-02-18 16:06:46 +0800467 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468 * @file: file structure associated with the socket
469 * @sock: socket for which to calculate the poll bits
470 * @wait: ???
471 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700472 * Returns pollmask value
473 *
474 * COMMENTARY:
475 * It appears that the usual socket locking mechanisms are not useful here
476 * since the pollmask info is potentially out-of-date the moment this routine
477 * exits. TCP and other protocols seem to rely on higher level poll routines
478 * to handle any preventable race conditions, so TIPC will do the same ...
479 *
480 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700481 *
Allan Stephensf662c072010-08-17 11:00:06 +0000482 * socket state flags set
483 * ------------ ---------
484 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200485 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000486 *
487 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
488 * no write flags
489 *
490 * connected POLLIN/POLLRDNORM if data in rx queue
491 * POLLOUT if port is not congested
492 *
493 * disconnecting POLLIN/POLLRDNORM/POLLHUP
494 * no write flags
495 *
496 * listening POLLIN if SYN in rx queue
497 * no write flags
498 *
499 * ready POLLIN/POLLRDNORM if data in rx queue
500 * [connectionless] POLLOUT (since port cannot be congested)
501 *
502 * IMPORTANT: The fact that a read or write operation is indicated does NOT
503 * imply that the operation will succeed, merely that it should be performed
504 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 */
Ying Xue247f0f32014-02-18 16:06:46 +0800506static unsigned int tipc_poll(struct file *file, struct socket *sock,
507 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508{
Allan Stephens9b674e82008-03-26 16:48:21 -0700509 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000510 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700511
Ying Xuef288bef2012-08-21 11:16:57 +0800512 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700513
Allan Stephensf662c072010-08-17 11:00:06 +0000514 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200515 case SS_UNCONNECTED:
516 if (!tipc_sk_port(sk)->congested)
517 mask |= POLLOUT;
518 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000519 case SS_READY:
520 case SS_CONNECTED:
521 if (!tipc_sk_port(sk)->congested)
522 mask |= POLLOUT;
523 /* fall thru' */
524 case SS_CONNECTING:
525 case SS_LISTENING:
526 if (!skb_queue_empty(&sk->sk_receive_queue))
527 mask |= (POLLIN | POLLRDNORM);
528 break;
529 case SS_DISCONNECTING:
530 mask = (POLLIN | POLLRDNORM | POLLHUP);
531 break;
532 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700533
534 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535}
536
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900537/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 * dest_name_check - verify user is permitted to send to specified port name
539 * @dest: destination address
540 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900541 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542 * Prevents restricted configuration commands from being issued by
543 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900544 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 * Returns 0 if permission is granted, otherwise errno
546 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800547static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548{
549 struct tipc_cfg_msg_hdr hdr;
550
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900551 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
552 return 0;
553 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
554 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900555 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
556 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557
Allan Stephens3f8dd942011-01-18 13:09:29 -0500558 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
559 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900560 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700562 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900564
Per Lidenb97bf3f2006-01-02 19:04:38 +0100565 return 0;
566}
567
Ying Xue3f405042014-01-17 09:50:05 +0800568static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
569{
570 struct sock *sk = sock->sk;
571 struct tipc_port *tport = tipc_sk_port(sk);
572 DEFINE_WAIT(wait);
573 int done;
574
575 do {
576 int err = sock_error(sk);
577 if (err)
578 return err;
579 if (sock->state == SS_DISCONNECTING)
580 return -EPIPE;
581 if (!*timeo_p)
582 return -EAGAIN;
583 if (signal_pending(current))
584 return sock_intr_errno(*timeo_p);
585
586 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
587 done = sk_wait_event(sk, timeo_p, !tport->congested);
588 finish_wait(sk_sleep(sk), &wait);
589 } while (!done);
590 return 0;
591}
592
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593/**
Ying Xue247f0f32014-02-18 16:06:46 +0800594 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700595 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596 * @sock: socket structure
597 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700598 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900599 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900601 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
603 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900604 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 * Returns the number of bytes sent on success, or errno otherwise
606 */
Ying Xue247f0f32014-02-18 16:06:46 +0800607static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
608 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700610 struct sock *sk = sock->sk;
611 struct tipc_port *tport = tipc_sk_port(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100612 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 int needs_conn;
Ying Xue3f405042014-01-17 09:50:05 +0800614 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615 int res = -EINVAL;
616
617 if (unlikely(!dest))
618 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700619 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
620 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621 return -EINVAL;
Ying Xue97f8b872013-01-31 21:51:47 +0100622 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400623 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624
Allan Stephens0c3141e2008-04-15 00:22:02 -0700625 if (iocb)
626 lock_sock(sk);
627
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 needs_conn = (sock->state != SS_READY);
629 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700630 if (sock->state == SS_LISTENING) {
631 res = -EPIPE;
632 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700633 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700634 if (sock->state != SS_UNCONNECTED) {
635 res = -EISCONN;
636 goto exit;
637 }
Erik Hugne5d21cb72013-06-17 10:54:38 -0400638 if (tport->published) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700639 res = -EOPNOTSUPP;
640 goto exit;
641 }
642 if (dest->addrtype == TIPC_ADDR_NAME) {
643 tport->conn_type = dest->addr.name.name.type;
644 tport->conn_instance = dest->addr.name.name.instance;
645 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646
647 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700648 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649 }
650
Ying Xue3f405042014-01-17 09:50:05 +0800651 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900652 do {
653 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000654 res = dest_name_check(dest, m);
655 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700656 break;
657 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900658 &dest->addr.name.name,
659 dest->addr.name.domain,
Allan Stephens26896902011-04-21 10:42:07 -0500660 m->msg_iov,
661 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000662 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700663 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900664 &dest->addr.id,
Allan Stephens26896902011-04-21 10:42:07 -0500665 m->msg_iov,
666 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000667 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668 if (needs_conn) {
669 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700670 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671 }
Allan Stephens2db99832010-12-31 18:59:33 +0000672 res = dest_name_check(dest, m);
673 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700674 break;
Ying Xue247f0f32014-02-18 16:06:46 +0800675 res = tipc_port_mcast_xmit(tport->ref,
676 &dest->addr.nameseq,
677 m->msg_iov,
678 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900679 }
680 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000681 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700682 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700683 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900684 }
Ying Xue3f405042014-01-17 09:50:05 +0800685 res = tipc_wait_for_sndmsg(sock, &timeo);
686 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700687 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900688 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700689
690exit:
691 if (iocb)
692 release_sock(sk);
693 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100694}
695
Ying Xue391a6dd2014-01-17 09:50:06 +0800696static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
697{
698 struct sock *sk = sock->sk;
699 struct tipc_port *tport = tipc_sk_port(sk);
700 DEFINE_WAIT(wait);
701 int done;
702
703 do {
704 int err = sock_error(sk);
705 if (err)
706 return err;
707 if (sock->state == SS_DISCONNECTING)
708 return -EPIPE;
709 else if (sock->state != SS_CONNECTED)
710 return -ENOTCONN;
711 if (!*timeo_p)
712 return -EAGAIN;
713 if (signal_pending(current))
714 return sock_intr_errno(*timeo_p);
715
716 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
717 done = sk_wait_event(sk, timeo_p,
718 (!tport->congested || !tport->connected));
719 finish_wait(sk_sleep(sk), &wait);
720 } while (!done);
721 return 0;
722}
723
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900724/**
Ying Xue247f0f32014-02-18 16:06:46 +0800725 * tipc_send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700726 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100727 * @sock: socket structure
728 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700729 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900730 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900732 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733 * Returns the number of bytes sent on success, or errno otherwise
734 */
Ying Xue247f0f32014-02-18 16:06:46 +0800735static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
736 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700738 struct sock *sk = sock->sk;
739 struct tipc_port *tport = tipc_sk_port(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100740 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue391a6dd2014-01-17 09:50:06 +0800741 int res = -EINVAL;
742 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743
744 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 if (unlikely(dest))
Ying Xue247f0f32014-02-18 16:06:46 +0800746 return tipc_sendmsg(iocb, sock, m, total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747
Ying Xue97f8b872013-01-31 21:51:47 +0100748 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400749 return -EMSGSIZE;
750
Allan Stephens0c3141e2008-04-15 00:22:02 -0700751 if (iocb)
752 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753
Ying Xue391a6dd2014-01-17 09:50:06 +0800754 if (unlikely(sock->state != SS_CONNECTED)) {
755 if (sock->state == SS_DISCONNECTING)
756 res = -EPIPE;
757 else
758 res = -ENOTCONN;
759 goto exit;
760 }
Ying Xue1d835872011-07-06 05:53:15 -0400761
Ying Xue391a6dd2014-01-17 09:50:06 +0800762 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900763 do {
Ying Xue9446b872013-10-18 07:23:15 +0200764 res = tipc_send(tport->ref, m->msg_iov, total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000765 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700766 break;
Ying Xue391a6dd2014-01-17 09:50:06 +0800767 res = tipc_wait_for_sndpkt(sock, &timeo);
768 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700769 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900770 } while (1);
Ying Xue391a6dd2014-01-17 09:50:06 +0800771exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700772 if (iocb)
773 release_sock(sk);
774 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775}
776
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900777/**
Ying Xue247f0f32014-02-18 16:06:46 +0800778 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779 * @iocb: (unused)
780 * @sock: socket structure
781 * @m: data to send
782 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900783 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100784 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900785 *
786 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700787 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788 */
Ying Xue247f0f32014-02-18 16:06:46 +0800789static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
790 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700792 struct sock *sk = sock->sk;
793 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794 struct msghdr my_msg;
795 struct iovec my_iov;
796 struct iovec *curr_iov;
797 int curr_iovlen;
798 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700799 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100800 int curr_left;
801 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700802 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100803 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900804
Allan Stephens0c3141e2008-04-15 00:22:02 -0700805 lock_sock(sk);
806
Allan Stephens05646c92007-06-10 17:25:24 -0700807 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900808 if (unlikely(sock->state != SS_CONNECTED)) {
wangweidong3b8401f2013-12-12 09:36:40 +0800809 if (sock->state == SS_UNCONNECTED)
Ying Xue247f0f32014-02-18 16:06:46 +0800810 res = tipc_send_packet(NULL, sock, m, total_len);
wangweidongb0555972013-12-27 10:09:39 +0800811 else
812 res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800813 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900814 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815
Allan Stephens0c3141e2008-04-15 00:22:02 -0700816 if (unlikely(m->msg_name)) {
817 res = -EISCONN;
818 goto exit;
819 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700820
Ying Xue97f8b872013-01-31 21:51:47 +0100821 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400822 res = -EMSGSIZE;
823 goto exit;
824 }
825
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900826 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100827 * Send each iovec entry using one or more messages
828 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900829 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830 * (i.e. one large iovec entry), but could be improved to pass sets
831 * of small iovec entries into send_packet().
832 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700833 curr_iov = m->msg_iov;
834 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100835 my_msg.msg_iov = &my_iov;
836 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700837 my_msg.msg_flags = m->msg_flags;
838 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700839 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840
Allan Stephens05646c92007-06-10 17:25:24 -0700841 hdr_size = msg_hdr_sz(&tport->phdr);
842
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843 while (curr_iovlen--) {
844 curr_start = curr_iov->iov_base;
845 curr_left = curr_iov->iov_len;
846
847 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700848 bytes_to_send = tport->max_pkt - hdr_size;
849 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
850 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
851 if (curr_left < bytes_to_send)
852 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100853 my_iov.iov_base = curr_start;
854 my_iov.iov_len = bytes_to_send;
Ying Xue247f0f32014-02-18 16:06:46 +0800855 res = tipc_send_packet(NULL, sock, &my_msg,
856 bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000857 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700858 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700859 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700860 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700861 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100862 curr_left -= bytes_to_send;
863 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700864 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865 }
866
867 curr_iov++;
868 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700869 res = bytes_sent;
870exit:
871 release_sock(sk);
872 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873}
874
875/**
876 * auto_connect - complete connection setup to a remote port
877 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900879 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 * Returns 0 on success, errno otherwise
881 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700882static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100883{
Allan Stephens2da59912008-07-14 22:43:32 -0700884 struct tipc_sock *tsock = tipc_sk(sock->sk);
Ying Xue584d24b2012-11-29 18:51:19 -0500885 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886
Allan Stephens2da59912008-07-14 22:43:32 -0700887 tsock->peer_name.ref = msg_origport(msg);
888 tsock->peer_name.node = msg_orignode(msg);
Ying Xue584d24b2012-11-29 18:51:19 -0500889 p_ptr = tipc_port_deref(tsock->p->ref);
890 if (!p_ptr)
891 return -EINVAL;
892
Ying Xue247f0f32014-02-18 16:06:46 +0800893 __tipc_port_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
Ying Xue584d24b2012-11-29 18:51:19 -0500894
895 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
896 return -EINVAL;
897 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898 sock->state = SS_CONNECTED;
899 return 0;
900}
901
902/**
903 * set_orig_addr - capture sender's address for received message
904 * @m: descriptor for message info
905 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900906 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907 * Note: Address is not captured if not requested by receiver.
908 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800909static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910{
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100911 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900913 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914 addr->family = AF_TIPC;
915 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000916 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100917 addr->addr.id.ref = msg_origport(msg);
918 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000919 addr->addr.name.domain = 0; /* could leave uninitialized */
920 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921 m->msg_namelen = sizeof(struct sockaddr_tipc);
922 }
923}
924
925/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900926 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927 * @m: descriptor for message info
928 * @msg: received message header
929 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900930 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900932 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100933 * Returns 0 if successful, otherwise errno
934 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800935static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400936 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100937{
938 u32 anc_data[3];
939 u32 err;
940 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700941 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942 int res;
943
944 if (likely(m->msg_controllen == 0))
945 return 0;
946
947 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100948 err = msg ? msg_errcode(msg) : 0;
949 if (unlikely(err)) {
950 anc_data[0] = err;
951 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000952 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
953 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100954 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000955 if (anc_data[1]) {
956 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
957 msg_data(msg));
958 if (res)
959 return res;
960 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100961 }
962
963 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
965 switch (dest_type) {
966 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700967 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968 anc_data[0] = msg_nametype(msg);
969 anc_data[1] = msg_namelower(msg);
970 anc_data[2] = msg_namelower(msg);
971 break;
972 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700973 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974 anc_data[0] = msg_nametype(msg);
975 anc_data[1] = msg_namelower(msg);
976 anc_data[2] = msg_nameupper(msg);
977 break;
978 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700979 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980 anc_data[0] = tport->conn_type;
981 anc_data[1] = tport->conn_instance;
982 anc_data[2] = tport->conn_instance;
983 break;
984 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700985 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100986 }
Allan Stephens2db99832010-12-31 18:59:33 +0000987 if (has_name) {
988 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
989 if (res)
990 return res;
991 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100992
993 return 0;
994}
995
Ying Xue9bbb4ec2014-01-17 09:50:07 +0800996static int tipc_wait_for_rcvmsg(struct socket *sock, long timeo)
997{
998 struct sock *sk = sock->sk;
999 DEFINE_WAIT(wait);
1000 int err;
1001
1002 for (;;) {
1003 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1004 if (skb_queue_empty(&sk->sk_receive_queue)) {
1005 if (sock->state == SS_DISCONNECTING) {
1006 err = -ENOTCONN;
1007 break;
1008 }
1009 release_sock(sk);
1010 timeo = schedule_timeout(timeo);
1011 lock_sock(sk);
1012 }
1013 err = 0;
1014 if (!skb_queue_empty(&sk->sk_receive_queue))
1015 break;
1016 err = sock_intr_errno(timeo);
1017 if (signal_pending(current))
1018 break;
1019 err = -EAGAIN;
1020 if (!timeo)
1021 break;
1022 }
1023 finish_wait(sk_sleep(sk), &wait);
1024 return err;
1025}
1026
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001027/**
Ying Xue247f0f32014-02-18 16:06:46 +08001028 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 * @iocb: (unused)
1030 * @m: descriptor for message info
1031 * @buf_len: total size of user buffer area
1032 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001033 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1035 * If the complete message doesn't fit in user area, truncate it.
1036 *
1037 * Returns size of returned message data, errno otherwise
1038 */
Ying Xue247f0f32014-02-18 16:06:46 +08001039static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1040 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001042 struct sock *sk = sock->sk;
1043 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001044 struct sk_buff *buf;
1045 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001046 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001047 unsigned int sz;
1048 u32 err;
1049 int res;
1050
Allan Stephens0c3141e2008-04-15 00:22:02 -07001051 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052 if (unlikely(!buf_len))
1053 return -EINVAL;
1054
Allan Stephens0c3141e2008-04-15 00:22:02 -07001055 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001056
Allan Stephens0c3141e2008-04-15 00:22:02 -07001057 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001058 res = -ENOTCONN;
1059 goto exit;
1060 }
1061
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001062 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001063restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064
Allan Stephens0c3141e2008-04-15 00:22:02 -07001065 /* Look for a message in receive queue; wait if necessary */
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001066 res = tipc_wait_for_rcvmsg(sock, timeo);
1067 if (res)
1068 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001069
1070 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001071 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072 msg = buf_msg(buf);
1073 sz = msg_data_sz(msg);
1074 err = msg_errcode(msg);
1075
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001078 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001079 goto restart;
1080 }
1081
1082 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083 set_orig_addr(m, msg);
1084
1085 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001086 res = anc_data_recv(m, msg, tport);
1087 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001088 goto exit;
1089
1090 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001091 if (!err) {
1092 if (unlikely(buf_len < sz)) {
1093 sz = buf_len;
1094 m->msg_flags |= MSG_TRUNC;
1095 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001096 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1097 m->msg_iov, sz);
1098 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001099 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001100 res = sz;
1101 } else {
1102 if ((sock->state == SS_READY) ||
1103 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1104 res = 0;
1105 else
1106 res = -ECONNRESET;
1107 }
1108
1109 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001110 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001111 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -07001112 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1113 tipc_acknowledge(tport->ref, tport->conn_unacked);
1114 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001115 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001117 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118 return res;
1119}
1120
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001121/**
Ying Xue247f0f32014-02-18 16:06:46 +08001122 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001123 * @iocb: (unused)
1124 * @m: descriptor for message info
1125 * @buf_len: total size of user buffer area
1126 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001127 *
1128 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129 * will optionally wait for more; never truncates data.
1130 *
1131 * Returns size of returned message data, errno otherwise
1132 */
Ying Xue247f0f32014-02-18 16:06:46 +08001133static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1134 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001136 struct sock *sk = sock->sk;
1137 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138 struct sk_buff *buf;
1139 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001140 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001142 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001145 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146
1147 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 if (unlikely(!buf_len))
1149 return -EINVAL;
1150
Allan Stephens0c3141e2008-04-15 00:22:02 -07001151 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001152
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001153 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154 res = -ENOTCONN;
1155 goto exit;
1156 }
1157
Florian Westphal3720d402010-08-17 11:00:04 +00001158 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001159 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001160
Allan Stephens0c3141e2008-04-15 00:22:02 -07001161restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001162 /* Look for a message in receive queue; wait if necessary */
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001163 res = tipc_wait_for_rcvmsg(sock, timeo);
1164 if (res)
1165 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001166
1167 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001168 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 msg = buf_msg(buf);
1170 sz = msg_data_sz(msg);
1171 err = msg_errcode(msg);
1172
1173 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001175 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176 goto restart;
1177 }
1178
1179 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 if (sz_copied == 0) {
1181 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001182 res = anc_data_recv(m, msg, tport);
1183 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184 goto exit;
1185 }
1186
1187 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001188 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001189 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190
Allan Stephens0232fd02011-02-21 09:45:40 -05001191 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192 needed = (buf_len - sz_copied);
1193 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001194
1195 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1196 m->msg_iov, sz_to_copy);
1197 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001199
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200 sz_copied += sz_to_copy;
1201
1202 if (sz_to_copy < sz) {
1203 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001204 TIPC_SKB_CB(buf)->handle =
1205 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206 goto exit;
1207 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 } else {
1209 if (sz_copied != 0)
1210 goto exit; /* can't add error msg to valid data */
1211
1212 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1213 res = 0;
1214 else
1215 res = -ECONNRESET;
1216 }
1217
1218 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001220 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1221 tipc_acknowledge(tport->ref, tport->conn_unacked);
1222 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001223 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001224
1225 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001226 if ((sz_copied < buf_len) && /* didn't get all requested data */
1227 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001228 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001229 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1230 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 goto restart;
1232
1233exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001234 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001235 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236}
1237
1238/**
Ying Xuef288bef2012-08-21 11:16:57 +08001239 * tipc_write_space - wake up thread if port congestion is released
1240 * @sk: socket
1241 */
1242static void tipc_write_space(struct sock *sk)
1243{
1244 struct socket_wq *wq;
1245
1246 rcu_read_lock();
1247 wq = rcu_dereference(sk->sk_wq);
1248 if (wq_has_sleeper(wq))
1249 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1250 POLLWRNORM | POLLWRBAND);
1251 rcu_read_unlock();
1252}
1253
1254/**
1255 * tipc_data_ready - wake up threads to indicate messages have been received
1256 * @sk: socket
1257 * @len: the length of messages
1258 */
1259static void tipc_data_ready(struct sock *sk, int len)
1260{
1261 struct socket_wq *wq;
1262
1263 rcu_read_lock();
1264 wq = rcu_dereference(sk->sk_wq);
1265 if (wq_has_sleeper(wq))
1266 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1267 POLLRDNORM | POLLRDBAND);
1268 rcu_read_unlock();
1269}
1270
1271/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001272 * filter_connect - Handle all incoming messages for a connection-based socket
1273 * @tsock: TIPC socket
1274 * @msg: message
1275 *
1276 * Returns TIPC error status code and socket error status code
1277 * once it encounters some errors
1278 */
1279static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
1280{
1281 struct socket *sock = tsock->sk.sk_socket;
1282 struct tipc_msg *msg = buf_msg(*buf);
Ying Xue584d24b2012-11-29 18:51:19 -05001283 struct sock *sk = &tsock->sk;
Ying Xue7e6c1312012-11-29 18:39:14 -05001284 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001285 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001286
1287 if (msg_mcast(msg))
1288 return retval;
1289
1290 switch ((int)sock->state) {
1291 case SS_CONNECTED:
1292 /* Accept only connection-based messages sent by peer */
1293 if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
1294 if (unlikely(msg_errcode(msg))) {
1295 sock->state = SS_DISCONNECTING;
Ying Xue247f0f32014-02-18 16:06:46 +08001296 __tipc_port_disconnect(tsock->p);
Ying Xue7e6c1312012-11-29 18:39:14 -05001297 }
1298 retval = TIPC_OK;
1299 }
1300 break;
1301 case SS_CONNECTING:
1302 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001303 if (unlikely(msg_errcode(msg))) {
1304 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001305 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001306 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001307 break;
1308 }
1309
1310 if (unlikely(!msg_connected(msg)))
1311 break;
1312
1313 res = auto_connect(sock, msg);
1314 if (res) {
1315 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001316 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001317 retval = TIPC_OK;
1318 break;
1319 }
1320
1321 /* If an incoming message is an 'ACK-', it should be
1322 * discarded here because it doesn't contain useful
1323 * data. In addition, we should try to wake up
1324 * connect() routine if sleeping.
1325 */
1326 if (msg_data_sz(msg) == 0) {
1327 kfree_skb(*buf);
1328 *buf = NULL;
1329 if (waitqueue_active(sk_sleep(sk)))
1330 wake_up_interruptible(sk_sleep(sk));
1331 }
1332 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001333 break;
1334 case SS_LISTENING:
1335 case SS_UNCONNECTED:
1336 /* Accept only SYN message */
1337 if (!msg_connected(msg) && !(msg_errcode(msg)))
1338 retval = TIPC_OK;
1339 break;
1340 case SS_DISCONNECTING:
1341 break;
1342 default:
1343 pr_err("Unknown socket state %u\n", sock->state);
1344 }
1345 return retval;
1346}
1347
1348/**
Ying Xueaba79f32013-01-20 23:30:09 +01001349 * rcvbuf_limit - get proper overload limit of socket receive queue
1350 * @sk: socket
1351 * @buf: message
1352 *
1353 * For all connection oriented messages, irrespective of importance,
1354 * the default overload value (i.e. 67MB) is set as limit.
1355 *
1356 * For all connectionless messages, by default new queue limits are
1357 * as belows:
1358 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001359 * TIPC_LOW_IMPORTANCE (4 MB)
1360 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1361 * TIPC_HIGH_IMPORTANCE (16 MB)
1362 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001363 *
1364 * Returns overload limit according to corresponding message importance
1365 */
1366static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1367{
1368 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001369
1370 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001371 return sysctl_tipc_rmem[2];
1372
1373 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1374 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001375}
1376
1377/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001378 * filter_rcv - validate incoming message
1379 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001381 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001382 * Enqueues message on receive queue if acceptable; optionally handles
1383 * disconnect indication for a connected socket.
1384 *
1385 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001386 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1388 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001389static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001391 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001393 unsigned int limit = rcvbuf_limit(sk, buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001394 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001395
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001397 if (msg_type(msg) > TIPC_DIRECT_MSG)
1398 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001399
Per Lidenb97bf3f2006-01-02 19:04:38 +01001400 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001401 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001402 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 } else {
Ying Xue7e6c1312012-11-29 18:39:14 -05001404 res = filter_connect(tipc_sk(sk), &buf);
1405 if (res != TIPC_OK || buf == NULL)
1406 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001407 }
1408
1409 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001410 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1411 return TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001412
Ying Xueaba79f32013-01-20 23:30:09 +01001413 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001414 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001415 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001416 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001417
Ying Xuef288bef2012-08-21 11:16:57 +08001418 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001419 return TIPC_OK;
1420}
1421
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001422/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001423 * backlog_rcv - handle incoming message from backlog queue
1424 * @sk: socket
1425 * @buf: message
1426 *
1427 * Caller must hold socket lock, but not port lock.
1428 *
1429 * Returns 0
1430 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001431static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1432{
1433 u32 res;
1434
1435 res = filter_rcv(sk, buf);
1436 if (res)
1437 tipc_reject_msg(buf, res);
1438 return 0;
1439}
1440
1441/**
1442 * dispatch - handle incoming message
1443 * @tport: TIPC port that received message
1444 * @buf: message
1445 *
1446 * Called with port lock already taken.
1447 *
1448 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1449 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001450static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1451{
Ying Xuec0fee8a2013-06-17 10:54:46 -04001452 struct sock *sk = tport->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001453 u32 res;
1454
1455 /*
1456 * Process message if socket is unlocked; otherwise add to backlog queue
1457 *
1458 * This code is based on sk_receive_skb(), but must be distinct from it
1459 * since a TIPC-specific filter/reject mechanism is utilized
1460 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001461 bh_lock_sock(sk);
1462 if (!sock_owned_by_user(sk)) {
1463 res = filter_rcv(sk, buf);
1464 } else {
Ying Xueaba79f32013-01-20 23:30:09 +01001465 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
Zhu Yi53eecb12010-03-04 18:01:45 +00001466 res = TIPC_ERR_OVERLOAD;
1467 else
1468 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001469 }
1470 bh_unlock_sock(sk);
1471
1472 return res;
1473}
1474
1475/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001476 * wakeupdispatch - wake up port after congestion
1477 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001478 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001479 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001480 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001481static void wakeupdispatch(struct tipc_port *tport)
1482{
Ying Xuec0fee8a2013-06-17 10:54:46 -04001483 struct sock *sk = tport->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484
Ying Xuef288bef2012-08-21 11:16:57 +08001485 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001486}
1487
Ying Xue78eb3a52014-01-17 09:50:03 +08001488static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1489{
1490 struct sock *sk = sock->sk;
1491 DEFINE_WAIT(wait);
1492 int done;
1493
1494 do {
1495 int err = sock_error(sk);
1496 if (err)
1497 return err;
1498 if (!*timeo_p)
1499 return -ETIMEDOUT;
1500 if (signal_pending(current))
1501 return sock_intr_errno(*timeo_p);
1502
1503 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1504 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1505 finish_wait(sk_sleep(sk), &wait);
1506 } while (!done);
1507 return 0;
1508}
1509
Per Lidenb97bf3f2006-01-02 19:04:38 +01001510/**
Ying Xue247f0f32014-02-18 16:06:46 +08001511 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001512 * @sock: socket structure
1513 * @dest: socket address for destination port
1514 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001515 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001516 *
1517 * Returns 0 on success, errno otherwise
1518 */
Ying Xue247f0f32014-02-18 16:06:46 +08001519static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1520 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001521{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001522 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001523 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1524 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001525 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1526 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001527 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001528
Allan Stephens0c3141e2008-04-15 00:22:02 -07001529 lock_sock(sk);
1530
Allan Stephensb89741a2008-04-15 00:20:37 -07001531 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001532 if (sock->state == SS_READY) {
1533 res = -EOPNOTSUPP;
1534 goto exit;
1535 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001536
Allan Stephensb89741a2008-04-15 00:20:37 -07001537 /*
1538 * Reject connection attempt using multicast address
1539 *
1540 * Note: send_msg() validates the rest of the address fields,
1541 * so there's no need to do it here
1542 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001543 if (dst->addrtype == TIPC_ADDR_MCAST) {
1544 res = -EINVAL;
1545 goto exit;
1546 }
1547
Ying Xue78eb3a52014-01-17 09:50:03 +08001548 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001549 switch (sock->state) {
1550 case SS_UNCONNECTED:
1551 /* Send a 'SYN-' to destination */
1552 m.msg_name = dest;
1553 m.msg_namelen = destlen;
1554
1555 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1556 * indicate send_msg() is never blocked.
1557 */
1558 if (!timeout)
1559 m.msg_flags = MSG_DONTWAIT;
1560
Ying Xue247f0f32014-02-18 16:06:46 +08001561 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001562 if ((res < 0) && (res != -EWOULDBLOCK))
1563 goto exit;
1564
1565 /* Just entered SS_CONNECTING state; the only
1566 * difference is that return value in non-blocking
1567 * case is EINPROGRESS, rather than EALREADY.
1568 */
1569 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001570 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001571 if (previous == SS_CONNECTING)
1572 res = -EALREADY;
1573 if (!timeout)
1574 goto exit;
1575 timeout = msecs_to_jiffies(timeout);
1576 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1577 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001578 break;
1579 case SS_CONNECTED:
1580 res = -EISCONN;
1581 break;
1582 default:
1583 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001584 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001585 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001586exit:
1587 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001588 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001589}
1590
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001591/**
Ying Xue247f0f32014-02-18 16:06:46 +08001592 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001593 * @sock: socket structure
1594 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001595 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001596 * Returns 0 on success, errno otherwise
1597 */
Ying Xue247f0f32014-02-18 16:06:46 +08001598static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001599{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001600 struct sock *sk = sock->sk;
1601 int res;
1602
1603 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001604
Ying Xue245f3d32011-07-06 06:01:13 -04001605 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001606 res = -EINVAL;
1607 else {
1608 sock->state = SS_LISTENING;
1609 res = 0;
1610 }
1611
1612 release_sock(sk);
1613 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001614}
1615
Ying Xue6398e232014-01-17 09:50:04 +08001616static int tipc_wait_for_accept(struct socket *sock, long timeo)
1617{
1618 struct sock *sk = sock->sk;
1619 DEFINE_WAIT(wait);
1620 int err;
1621
1622 /* True wake-one mechanism for incoming connections: only
1623 * one process gets woken up, not the 'whole herd'.
1624 * Since we do not 'race & poll' for established sockets
1625 * anymore, the common case will execute the loop only once.
1626 */
1627 for (;;) {
1628 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1629 TASK_INTERRUPTIBLE);
1630 if (skb_queue_empty(&sk->sk_receive_queue)) {
1631 release_sock(sk);
1632 timeo = schedule_timeout(timeo);
1633 lock_sock(sk);
1634 }
1635 err = 0;
1636 if (!skb_queue_empty(&sk->sk_receive_queue))
1637 break;
1638 err = -EINVAL;
1639 if (sock->state != SS_LISTENING)
1640 break;
1641 err = sock_intr_errno(timeo);
1642 if (signal_pending(current))
1643 break;
1644 err = -EAGAIN;
1645 if (!timeo)
1646 break;
1647 }
1648 finish_wait(sk_sleep(sk), &wait);
1649 return err;
1650}
1651
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001652/**
Ying Xue247f0f32014-02-18 16:06:46 +08001653 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001654 * @sock: listening socket
1655 * @newsock: new socket that is to be connected
1656 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001657 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001658 * Returns 0 on success, errno otherwise
1659 */
Ying Xue247f0f32014-02-18 16:06:46 +08001660static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001662 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663 struct sk_buff *buf;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001664 struct tipc_sock *new_tsock;
1665 struct tipc_port *new_tport;
1666 struct tipc_msg *msg;
1667 u32 new_ref;
Ying Xue6398e232014-01-17 09:50:04 +08001668 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001669 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670
Allan Stephens0c3141e2008-04-15 00:22:02 -07001671 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001672
Allan Stephens0c3141e2008-04-15 00:22:02 -07001673 if (sock->state != SS_LISTENING) {
1674 res = -EINVAL;
1675 goto exit;
1676 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001677
Ying Xue6398e232014-01-17 09:50:04 +08001678 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1679 res = tipc_wait_for_accept(sock, timeo);
1680 if (res)
1681 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001682
1683 buf = skb_peek(&sk->sk_receive_queue);
1684
Ying Xuec5fa7b32013-06-17 10:54:39 -04001685 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001686 if (res)
1687 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001688
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001689 new_sk = new_sock->sk;
1690 new_tsock = tipc_sk(new_sk);
1691 new_tport = new_tsock->p;
1692 new_ref = new_tport->ref;
1693 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001694
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001695 /* we lock on new_sk; but lockdep sees the lock on sk */
1696 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001697
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001698 /*
1699 * Reject any stray messages received by new socket
1700 * before the socket lock was taken (very, very unlikely)
1701 */
1702 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001703
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001704 /* Connect new socket to it's peer */
1705 new_tsock->peer_name.ref = msg_origport(msg);
1706 new_tsock->peer_name.node = msg_orignode(msg);
Ying Xue247f0f32014-02-18 16:06:46 +08001707 tipc_port_connect(new_ref, &new_tsock->peer_name);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001708 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001709
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001710 tipc_set_portimportance(new_ref, msg_importance(msg));
1711 if (msg_named(msg)) {
1712 new_tport->conn_type = msg_nametype(msg);
1713 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001714 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001715
1716 /*
1717 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1718 * Respond to 'SYN+' by queuing it on new socket.
1719 */
1720 if (!msg_data_sz(msg)) {
1721 struct msghdr m = {NULL,};
1722
1723 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001724 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001725 } else {
1726 __skb_dequeue(&sk->sk_receive_queue);
1727 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001728 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001729 }
1730 release_sock(new_sk);
1731
Per Lidenb97bf3f2006-01-02 19:04:38 +01001732exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001733 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001734 return res;
1735}
1736
1737/**
Ying Xue247f0f32014-02-18 16:06:46 +08001738 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001740 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741 *
1742 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001743 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001744 * Returns 0 on success, errno otherwise
1745 */
Ying Xue247f0f32014-02-18 16:06:46 +08001746static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001747{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001748 struct sock *sk = sock->sk;
1749 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001750 struct sk_buff *buf;
1751 int res;
1752
Allan Stephense247a8f2008-03-06 15:05:38 -08001753 if (how != SHUT_RDWR)
1754 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001755
Allan Stephens0c3141e2008-04-15 00:22:02 -07001756 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001757
1758 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001759 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001760 case SS_CONNECTED:
1761
Per Lidenb97bf3f2006-01-02 19:04:38 +01001762restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001763 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001764 buf = __skb_dequeue(&sk->sk_receive_queue);
1765 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001766 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001767 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001768 goto restart;
1769 }
Ying Xue247f0f32014-02-18 16:06:46 +08001770 tipc_port_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001771 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001772 } else {
Ying Xue247f0f32014-02-18 16:06:46 +08001773 tipc_port_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001775
1776 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001777
1778 /* fall through */
1779
1780 case SS_DISCONNECTING:
1781
Ying Xue75031152012-10-29 09:38:15 -04001782 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001783 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001784
1785 /* Wake up anyone sleeping in poll */
1786 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787 res = 0;
1788 break;
1789
1790 default:
1791 res = -ENOTCONN;
1792 }
1793
Allan Stephens0c3141e2008-04-15 00:22:02 -07001794 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001795 return res;
1796}
1797
1798/**
Ying Xue247f0f32014-02-18 16:06:46 +08001799 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800 * @sock: socket structure
1801 * @lvl: option level
1802 * @opt: option identifier
1803 * @ov: pointer to new option value
1804 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001805 *
1806 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001807 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001808 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001809 * Returns 0 on success, errno otherwise
1810 */
Ying Xue247f0f32014-02-18 16:06:46 +08001811static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1812 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001813{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001814 struct sock *sk = sock->sk;
1815 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816 u32 value;
1817 int res;
1818
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001819 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1820 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001821 if (lvl != SOL_TIPC)
1822 return -ENOPROTOOPT;
1823 if (ol < sizeof(value))
1824 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001825 res = get_user(value, (u32 __user *)ov);
1826 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001827 return res;
1828
Allan Stephens0c3141e2008-04-15 00:22:02 -07001829 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001830
Per Lidenb97bf3f2006-01-02 19:04:38 +01001831 switch (opt) {
1832 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001833 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834 break;
1835 case TIPC_SRC_DROPPABLE:
1836 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001837 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001838 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001839 res = -ENOPROTOOPT;
1840 break;
1841 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001842 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001843 break;
1844 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001845 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001846 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001847 break;
1848 default:
1849 res = -EINVAL;
1850 }
1851
Allan Stephens0c3141e2008-04-15 00:22:02 -07001852 release_sock(sk);
1853
Per Lidenb97bf3f2006-01-02 19:04:38 +01001854 return res;
1855}
1856
1857/**
Ying Xue247f0f32014-02-18 16:06:46 +08001858 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001859 * @sock: socket structure
1860 * @lvl: option level
1861 * @opt: option identifier
1862 * @ov: receptacle for option value
1863 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001864 *
1865 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001866 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001867 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001868 * Returns 0 on success, errno otherwise
1869 */
Ying Xue247f0f32014-02-18 16:06:46 +08001870static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1871 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001872{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001873 struct sock *sk = sock->sk;
1874 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001875 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001876 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001877 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001878
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001879 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1880 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001881 if (lvl != SOL_TIPC)
1882 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001883 res = get_user(len, ol);
1884 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001885 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886
Allan Stephens0c3141e2008-04-15 00:22:02 -07001887 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001888
1889 switch (opt) {
1890 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001891 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001892 break;
1893 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001894 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001895 break;
1896 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001897 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001898 break;
1899 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001900 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001901 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001902 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001903 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001904 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001905 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001906 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001907 value = skb_queue_len(&sk->sk_receive_queue);
1908 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001909 default:
1910 res = -EINVAL;
1911 }
1912
Allan Stephens0c3141e2008-04-15 00:22:02 -07001913 release_sock(sk);
1914
Paul Gortmaker25860c32010-12-31 18:59:31 +00001915 if (res)
1916 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001917
Paul Gortmaker25860c32010-12-31 18:59:31 +00001918 if (len < sizeof(value))
1919 return -EINVAL;
1920
1921 if (copy_to_user(ov, &value, sizeof(value)))
1922 return -EFAULT;
1923
1924 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001925}
1926
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001927/* Protocol switches for the various types of TIPC sockets */
1928
Florian Westphalbca65ea2008-02-07 18:18:01 -08001929static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001930 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001931 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001932 .release = tipc_release,
1933 .bind = tipc_bind,
1934 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001935 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001936 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08001937 .getname = tipc_getname,
1938 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001939 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001940 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08001941 .shutdown = tipc_shutdown,
1942 .setsockopt = tipc_setsockopt,
1943 .getsockopt = tipc_getsockopt,
1944 .sendmsg = tipc_sendmsg,
1945 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001946 .mmap = sock_no_mmap,
1947 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001948};
1949
Florian Westphalbca65ea2008-02-07 18:18:01 -08001950static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001951 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001952 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001953 .release = tipc_release,
1954 .bind = tipc_bind,
1955 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001956 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08001957 .accept = tipc_accept,
1958 .getname = tipc_getname,
1959 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001960 .ioctl = sock_no_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08001961 .listen = tipc_listen,
1962 .shutdown = tipc_shutdown,
1963 .setsockopt = tipc_setsockopt,
1964 .getsockopt = tipc_getsockopt,
1965 .sendmsg = tipc_send_packet,
1966 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001967 .mmap = sock_no_mmap,
1968 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001969};
1970
Florian Westphalbca65ea2008-02-07 18:18:01 -08001971static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001972 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001973 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001974 .release = tipc_release,
1975 .bind = tipc_bind,
1976 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001977 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08001978 .accept = tipc_accept,
1979 .getname = tipc_getname,
1980 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001981 .ioctl = sock_no_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08001982 .listen = tipc_listen,
1983 .shutdown = tipc_shutdown,
1984 .setsockopt = tipc_setsockopt,
1985 .getsockopt = tipc_getsockopt,
1986 .sendmsg = tipc_send_stream,
1987 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001988 .mmap = sock_no_mmap,
1989 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001990};
1991
Florian Westphalbca65ea2008-02-07 18:18:01 -08001992static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001993 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001994 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04001995 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01001996};
1997
1998static struct proto tipc_proto = {
1999 .name = "TIPC",
2000 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002001 .obj_size = sizeof(struct tipc_sock),
2002 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002003};
2004
Ying Xuec5fa7b32013-06-17 10:54:39 -04002005static struct proto tipc_proto_kern = {
2006 .name = "TIPC",
2007 .obj_size = sizeof(struct tipc_sock),
2008 .sysctl_rmem = sysctl_tipc_rmem
2009};
2010
Per Lidenb97bf3f2006-01-02 19:04:38 +01002011/**
Per Liden4323add2006-01-18 00:38:21 +01002012 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002013 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002014 * Returns 0 on success, errno otherwise
2015 */
Per Liden4323add2006-01-18 00:38:21 +01002016int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002017{
2018 int res;
2019
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002020 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002021 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002022 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002023 goto out;
2024 }
2025
2026 res = sock_register(&tipc_family_ops);
2027 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002028 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002029 proto_unregister(&tipc_proto);
2030 goto out;
2031 }
2032
2033 sockets_enabled = 1;
2034 out:
2035 return res;
2036}
2037
2038/**
Per Liden4323add2006-01-18 00:38:21 +01002039 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002040 */
Per Liden4323add2006-01-18 00:38:21 +01002041void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002042{
2043 if (!sockets_enabled)
2044 return;
2045
2046 sockets_enabled = 0;
2047 sock_unregister(tipc_family_ops.family);
2048 proto_unregister(&tipc_proto);
2049}