blob: 62655772adda72933c299f00e3c1a45309df060d [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 Stephensa0f40f02011-05-26 13:44:34 -040051 unsigned int conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +010052};
53
Allan Stephens0c3141e2008-04-15 00:22:02 -070054#define tipc_sk(sk) ((struct tipc_sock *)(sk))
Joe Perchese3192692012-06-03 17:41:40 +000055#define tipc_sk_port(sk) (tipc_sk(sk)->p)
Per Lidenb97bf3f2006-01-02 19:04:38 +010056
Allan Stephens0c3141e2008-04-15 00:22:02 -070057static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +010058static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
59static void wakeupdispatch(struct tipc_port *tport);
Ying Xuef288bef2012-08-21 11:16:57 +080060static void tipc_data_ready(struct sock *sk, int len);
61static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080062static int tipc_release(struct socket *sock);
63static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Per Lidenb97bf3f2006-01-02 19:04:38 +010064
Florian Westphalbca65ea2008-02-07 18:18:01 -080065static const struct proto_ops packet_ops;
66static const struct proto_ops stream_ops;
67static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010068
69static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040070static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010071
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090072/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070073 * Revised TIPC socket locking policy:
74 *
75 * Most socket operations take the standard socket lock when they start
76 * and hold it until they finish (or until they need to sleep). Acquiring
77 * this lock grants the owner exclusive access to the fields of the socket
78 * data structures, with the exception of the backlog queue. A few socket
79 * operations can be done without taking the socket lock because they only
80 * read socket information that never changes during the life of the socket.
81 *
82 * Socket operations may acquire the lock for the associated TIPC port if they
83 * need to perform an operation on the port. If any routine needs to acquire
84 * both the socket lock and the port lock it must take the socket lock first
85 * to avoid the risk of deadlock.
86 *
87 * The dispatcher handling incoming messages cannot grab the socket lock in
88 * the standard fashion, since invoked it runs at the BH level and cannot block.
89 * Instead, it checks to see if the socket lock is currently owned by someone,
90 * and either handles the message itself or adds it to the socket's backlog
91 * queue; in the latter case the queued message is processed once the process
92 * owning the socket lock releases it.
93 *
94 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
95 * the problem of a blocked socket operation preventing any other operations
96 * from occurring. However, applications must be careful if they have
97 * multiple threads trying to send (or receive) on the same socket, as these
98 * operations might interfere with each other. For example, doing a connect
99 * and a receive at the same time might allow the receive to consume the
100 * ACK message meant for the connect. While additional work could be done
101 * to try and overcome this, it doesn't seem to be worthwhile at the present.
102 *
103 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
104 * that another operation that must be performed in a non-blocking manner is
105 * not delayed for very long because the lock has already been taken.
106 *
107 * NOTE: This code assumes that certain fields of a port/socket pair are
108 * constant over its lifetime; such fields can be examined without taking
109 * the socket lock and/or port lock, and do not need to be re-read even
110 * after resuming processing after waiting. These fields include:
111 * - socket type
112 * - pointer to socket sk structure (aka tipc_sock structure)
113 * - pointer to port structure
114 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100115 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116
117/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700118 * advance_rx_queue - discard first buffer in socket receive queue
119 *
120 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700122static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400124 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125}
126
127/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700128 * reject_rx_queue - reject all buffers in socket receive queue
129 *
130 * Caller must hold socket lock
131 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700132static void reject_rx_queue(struct sock *sk)
133{
134 struct sk_buff *buf;
135
Ying Xue9da3d472012-11-27 06:15:27 -0500136 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700137 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700138}
139
140/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400141 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700142 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143 * @sock: pre-allocated socket structure
144 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800145 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900146 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700147 * This routine creates additional data structures used by the TIPC socket,
148 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100149 *
150 * Returns 0 on success, errno otherwise
151 */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400152static int tipc_sk_create(struct net *net, struct socket *sock, int protocol,
153 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700155 const struct proto_ops *ops;
156 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700158 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700159
160 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161 if (unlikely(protocol != 0))
162 return -EPROTONOSUPPORT;
163
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 switch (sock->type) {
165 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700166 ops = &stream_ops;
167 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168 break;
169 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700170 ops = &packet_ops;
171 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172 break;
173 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700175 ops = &msg_ops;
176 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 break;
Allan Stephens49978652006-06-25 23:47:18 -0700178 default:
Allan Stephens49978652006-06-25 23:47:18 -0700179 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 }
181
Allan Stephens0c3141e2008-04-15 00:22:02 -0700182 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400183 if (!kern)
184 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
185 else
186 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
187
Allan Stephens0c3141e2008-04-15 00:22:02 -0700188 if (sk == NULL)
189 return -ENOMEM;
190
191 /* Allocate TIPC port for socket to use */
Ying Xue3c5db8e2013-06-17 10:54:44 -0400192 tp_ptr = tipc_createport(sk, &dispatch, &wakeupdispatch,
193 TIPC_LOW_IMPORTANCE);
Allan Stephens0ea52242008-07-14 22:42:19 -0700194 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700195 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196 return -ENOMEM;
197 }
198
Allan Stephens0c3141e2008-04-15 00:22:02 -0700199 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700200 sock->ops = ops;
201 sock->state = state;
202
Per Lidenb97bf3f2006-01-02 19:04:38 +0100203 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700204 sk->sk_backlog_rcv = backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400205 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800206 sk->sk_data_ready = tipc_data_ready;
207 sk->sk_write_space = tipc_write_space;
Allan Stephens0ea52242008-07-14 22:42:19 -0700208 tipc_sk(sk)->p = tp_ptr;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400209 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700211 spin_unlock_bh(tp_ptr->lock);
212
Allan Stephens0c3141e2008-04-15 00:22:02 -0700213 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700214 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700215 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700216 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700217 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219 return 0;
220}
221
222/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400223 * tipc_sock_create_local - create TIPC socket from inside TIPC module
224 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
225 *
226 * We cannot use sock_creat_kern here because it bumps module user count.
227 * Since socket owner and creator is the same module we must make sure
228 * that module count remains zero for module local sockets, otherwise
229 * we cannot do rmmod.
230 *
231 * Returns 0 on success, errno otherwise
232 */
233int tipc_sock_create_local(int type, struct socket **res)
234{
235 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400236
237 rc = sock_create_lite(AF_TIPC, type, 0, res);
238 if (rc < 0) {
239 pr_err("Failed to create kernel socket\n");
240 return rc;
241 }
242 tipc_sk_create(&init_net, *res, 0, 1);
243
Ying Xuec5fa7b32013-06-17 10:54:39 -0400244 return 0;
245}
246
247/**
248 * tipc_sock_release_local - release socket created by tipc_sock_create_local
249 * @sock: the socket to be released.
250 *
251 * Module reference count is not incremented when such sockets are created,
252 * so we must keep it from being decremented when they are released.
253 */
254void tipc_sock_release_local(struct socket *sock)
255{
Ying Xue247f0f32014-02-18 16:06:46 +0800256 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400257 sock->ops = NULL;
258 sock_release(sock);
259}
260
261/**
262 * tipc_sock_accept_local - accept a connection on a socket created
263 * with tipc_sock_create_local. Use this function to avoid that
264 * module reference count is inadvertently incremented.
265 *
266 * @sock: the accepting socket
267 * @newsock: reference to the new socket to be created
268 * @flags: socket flags
269 */
270
271int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400272 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400273{
274 struct sock *sk = sock->sk;
275 int ret;
276
277 ret = sock_create_lite(sk->sk_family, sk->sk_type,
278 sk->sk_protocol, newsock);
279 if (ret < 0)
280 return ret;
281
Ying Xue247f0f32014-02-18 16:06:46 +0800282 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400283 if (ret < 0) {
284 sock_release(*newsock);
285 return ret;
286 }
287 (*newsock)->ops = sock->ops;
288 return ret;
289}
290
291/**
Ying Xue247f0f32014-02-18 16:06:46 +0800292 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293 * @sock: socket to destroy
294 *
295 * This routine cleans up any messages that are still queued on the socket.
296 * For DGRAM and RDM socket types, all queued messages are rejected.
297 * For SEQPACKET and STREAM socket types, the first message is rejected
298 * and any others are discarded. (If the first message on a STREAM socket
299 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900300 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301 * NOTE: Rejected messages are not necessarily returned to the sender! They
302 * are returned or discarded according to the "destination droppable" setting
303 * specified for the message by the sender.
304 *
305 * Returns 0 on success, errno otherwise
306 */
Ying Xue247f0f32014-02-18 16:06:46 +0800307static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700310 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700312 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313
Allan Stephens0c3141e2008-04-15 00:22:02 -0700314 /*
315 * Exit if socket isn't fully initialized (occurs when a failed accept()
316 * releases a pre-allocated child socket that was never used)
317 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700318 if (sk == NULL)
319 return 0;
320
321 tport = tipc_sk_port(sk);
322 lock_sock(sk);
323
324 /*
325 * Reject all unreceived messages, except on an active connection
326 * (which disconnects locally & sends a 'FIN+' to peer)
327 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100328 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700329 buf = __skb_dequeue(&sk->sk_receive_queue);
330 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331 break;
Ying Xue40682432013-10-18 07:23:16 +0200332 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400333 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700334 else {
335 if ((sock->state == SS_CONNECTING) ||
336 (sock->state == SS_CONNECTED)) {
337 sock->state = SS_DISCONNECTING;
Ying Xue247f0f32014-02-18 16:06:46 +0800338 tipc_port_disconnect(tport->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700339 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700341 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342 }
343
Allan Stephens0c3141e2008-04-15 00:22:02 -0700344 /*
345 * Delete TIPC port; this ensures no more messages are queued
346 * (also disconnects an active connection & sends a 'FIN-' to peer)
347 */
Ying Xue84602762013-12-27 10:18:28 +0800348 res = tipc_deleteport(tport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349
Allan Stephens0c3141e2008-04-15 00:22:02 -0700350 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100351 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352
Allan Stephens0c3141e2008-04-15 00:22:02 -0700353 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700354 sock->state = SS_DISCONNECTING;
355 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356
357 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700358 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360 return res;
361}
362
363/**
Ying Xue247f0f32014-02-18 16:06:46 +0800364 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365 * @sock: socket structure
366 * @uaddr: socket address describing name(s) and desired operation
367 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900368 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369 * Name and name sequence binding is indicated using a positive scope value;
370 * a negative scope value unbinds the specified name. Specifying no name
371 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900372 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700374 *
375 * NOTE: This routine doesn't need to take the socket lock since it doesn't
376 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377 */
Ying Xue247f0f32014-02-18 16:06:46 +0800378static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
379 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380{
Ying Xue84602762013-12-27 10:18:28 +0800381 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Ying Xue84602762013-12-27 10:18:28 +0800383 struct tipc_port *tport = tipc_sk_port(sock->sk);
384 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385
Ying Xue84602762013-12-27 10:18:28 +0800386 lock_sock(sk);
387 if (unlikely(!uaddr_len)) {
388 res = tipc_withdraw(tport, 0, NULL);
389 goto exit;
390 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900391
Ying Xue84602762013-12-27 10:18:28 +0800392 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
393 res = -EINVAL;
394 goto exit;
395 }
396 if (addr->family != AF_TIPC) {
397 res = -EAFNOSUPPORT;
398 goto exit;
399 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 if (addr->addrtype == TIPC_ADDR_NAME)
402 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800403 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
404 res = -EAFNOSUPPORT;
405 goto exit;
406 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900407
Ying Xue13a2e892013-06-17 10:54:40 -0400408 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400409 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800410 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
411 res = -EACCES;
412 goto exit;
413 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400414
Ying Xue84602762013-12-27 10:18:28 +0800415 res = (addr->scope > 0) ?
416 tipc_publish(tport, addr->scope, &addr->addr.nameseq) :
417 tipc_withdraw(tport, -addr->scope, &addr->addr.nameseq);
418exit:
419 release_sock(sk);
420 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421}
422
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900423/**
Ying Xue247f0f32014-02-18 16:06:46 +0800424 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425 * @sock: socket structure
426 * @uaddr: area for returned socket address
427 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700428 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900429 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700431 *
Allan Stephens2da59912008-07-14 22:43:32 -0700432 * NOTE: This routine doesn't need to take the socket lock since it only
433 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000434 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435 */
Ying Xue247f0f32014-02-18 16:06:46 +0800436static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
437 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700440 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000442 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700443 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700444 if ((sock->state != SS_CONNECTED) &&
445 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
446 return -ENOTCONN;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400447
448 addr->addr.id.ref = tipc_port_peerport(tsock->p);
449 addr->addr.id.node = tipc_port_peernode(tsock->p);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700450 } else {
Allan Stephensb924dcf2010-11-30 12:01:03 +0000451 addr->addr.id.ref = tsock->p->ref;
452 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700453 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454
455 *uaddr_len = sizeof(*addr);
456 addr->addrtype = TIPC_ADDR_ID;
457 addr->family = AF_TIPC;
458 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 addr->addr.name.domain = 0;
460
Allan Stephens0c3141e2008-04-15 00:22:02 -0700461 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462}
463
464/**
Ying Xue247f0f32014-02-18 16:06:46 +0800465 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466 * @file: file structure associated with the socket
467 * @sock: socket for which to calculate the poll bits
468 * @wait: ???
469 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700470 * Returns pollmask value
471 *
472 * COMMENTARY:
473 * It appears that the usual socket locking mechanisms are not useful here
474 * since the pollmask info is potentially out-of-date the moment this routine
475 * exits. TCP and other protocols seem to rely on higher level poll routines
476 * to handle any preventable race conditions, so TIPC will do the same ...
477 *
478 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700479 *
Allan Stephensf662c072010-08-17 11:00:06 +0000480 * socket state flags set
481 * ------------ ---------
482 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200483 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000484 *
485 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
486 * no write flags
487 *
488 * connected POLLIN/POLLRDNORM if data in rx queue
489 * POLLOUT if port is not congested
490 *
491 * disconnecting POLLIN/POLLRDNORM/POLLHUP
492 * no write flags
493 *
494 * listening POLLIN if SYN in rx queue
495 * no write flags
496 *
497 * ready POLLIN/POLLRDNORM if data in rx queue
498 * [connectionless] POLLOUT (since port cannot be congested)
499 *
500 * IMPORTANT: The fact that a read or write operation is indicated does NOT
501 * imply that the operation will succeed, merely that it should be performed
502 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503 */
Ying Xue247f0f32014-02-18 16:06:46 +0800504static unsigned int tipc_poll(struct file *file, struct socket *sock,
505 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506{
Allan Stephens9b674e82008-03-26 16:48:21 -0700507 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000508 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700509
Ying Xuef288bef2012-08-21 11:16:57 +0800510 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700511
Allan Stephensf662c072010-08-17 11:00:06 +0000512 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200513 case SS_UNCONNECTED:
514 if (!tipc_sk_port(sk)->congested)
515 mask |= POLLOUT;
516 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000517 case SS_READY:
518 case SS_CONNECTED:
519 if (!tipc_sk_port(sk)->congested)
520 mask |= POLLOUT;
521 /* fall thru' */
522 case SS_CONNECTING:
523 case SS_LISTENING:
524 if (!skb_queue_empty(&sk->sk_receive_queue))
525 mask |= (POLLIN | POLLRDNORM);
526 break;
527 case SS_DISCONNECTING:
528 mask = (POLLIN | POLLRDNORM | POLLHUP);
529 break;
530 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700531
532 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533}
534
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900535/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536 * dest_name_check - verify user is permitted to send to specified port name
537 * @dest: destination address
538 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900539 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540 * Prevents restricted configuration commands from being issued by
541 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900542 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 * Returns 0 if permission is granted, otherwise errno
544 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800545static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546{
547 struct tipc_cfg_msg_hdr hdr;
548
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900549 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
550 return 0;
551 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
552 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900553 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
554 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555
Allan Stephens3f8dd942011-01-18 13:09:29 -0500556 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
557 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900558 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700560 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900562
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 return 0;
564}
565
Ying Xue3f405042014-01-17 09:50:05 +0800566static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
567{
568 struct sock *sk = sock->sk;
569 struct tipc_port *tport = tipc_sk_port(sk);
570 DEFINE_WAIT(wait);
571 int done;
572
573 do {
574 int err = sock_error(sk);
575 if (err)
576 return err;
577 if (sock->state == SS_DISCONNECTING)
578 return -EPIPE;
579 if (!*timeo_p)
580 return -EAGAIN;
581 if (signal_pending(current))
582 return sock_intr_errno(*timeo_p);
583
584 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
585 done = sk_wait_event(sk, timeo_p, !tport->congested);
586 finish_wait(sk_sleep(sk), &wait);
587 } while (!done);
588 return 0;
589}
590
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591/**
Ying Xue247f0f32014-02-18 16:06:46 +0800592 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700593 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 * @sock: socket structure
595 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700596 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900597 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900599 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
601 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900602 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603 * Returns the number of bytes sent on success, or errno otherwise
604 */
Ying Xue247f0f32014-02-18 16:06:46 +0800605static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
606 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700608 struct sock *sk = sock->sk;
609 struct tipc_port *tport = tipc_sk_port(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100610 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 int needs_conn;
Ying Xue3f405042014-01-17 09:50:05 +0800612 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 int res = -EINVAL;
614
615 if (unlikely(!dest))
616 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700617 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
618 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 return -EINVAL;
Ying Xue97f8b872013-01-31 21:51:47 +0100620 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400621 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622
Allan Stephens0c3141e2008-04-15 00:22:02 -0700623 if (iocb)
624 lock_sock(sk);
625
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 needs_conn = (sock->state != SS_READY);
627 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700628 if (sock->state == SS_LISTENING) {
629 res = -EPIPE;
630 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700631 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700632 if (sock->state != SS_UNCONNECTED) {
633 res = -EISCONN;
634 goto exit;
635 }
Erik Hugne5d21cb72013-06-17 10:54:38 -0400636 if (tport->published) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700637 res = -EOPNOTSUPP;
638 goto exit;
639 }
640 if (dest->addrtype == TIPC_ADDR_NAME) {
641 tport->conn_type = dest->addr.name.name.type;
642 tport->conn_instance = dest->addr.name.name.instance;
643 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644
645 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700646 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647 }
648
Ying Xue3f405042014-01-17 09:50:05 +0800649 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900650 do {
651 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000652 res = dest_name_check(dest, m);
653 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700654 break;
655 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900656 &dest->addr.name.name,
657 dest->addr.name.domain,
Allan Stephens26896902011-04-21 10:42:07 -0500658 m->msg_iov,
659 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000660 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700661 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900662 &dest->addr.id,
Allan Stephens26896902011-04-21 10:42:07 -0500663 m->msg_iov,
664 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000665 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 if (needs_conn) {
667 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700668 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 }
Allan Stephens2db99832010-12-31 18:59:33 +0000670 res = dest_name_check(dest, m);
671 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700672 break;
Ying Xue247f0f32014-02-18 16:06:46 +0800673 res = tipc_port_mcast_xmit(tport->ref,
674 &dest->addr.nameseq,
675 m->msg_iov,
676 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900677 }
678 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000679 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700680 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700681 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900682 }
Ying Xue3f405042014-01-17 09:50:05 +0800683 res = tipc_wait_for_sndmsg(sock, &timeo);
684 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700685 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900686 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700687
688exit:
689 if (iocb)
690 release_sock(sk);
691 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100692}
693
Ying Xue391a6dd2014-01-17 09:50:06 +0800694static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
695{
696 struct sock *sk = sock->sk;
697 struct tipc_port *tport = tipc_sk_port(sk);
698 DEFINE_WAIT(wait);
699 int done;
700
701 do {
702 int err = sock_error(sk);
703 if (err)
704 return err;
705 if (sock->state == SS_DISCONNECTING)
706 return -EPIPE;
707 else if (sock->state != SS_CONNECTED)
708 return -ENOTCONN;
709 if (!*timeo_p)
710 return -EAGAIN;
711 if (signal_pending(current))
712 return sock_intr_errno(*timeo_p);
713
714 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
715 done = sk_wait_event(sk, timeo_p,
716 (!tport->congested || !tport->connected));
717 finish_wait(sk_sleep(sk), &wait);
718 } while (!done);
719 return 0;
720}
721
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900722/**
Ying Xue247f0f32014-02-18 16:06:46 +0800723 * tipc_send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700724 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100725 * @sock: socket structure
726 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700727 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900728 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900730 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731 * Returns the number of bytes sent on success, or errno otherwise
732 */
Ying Xue247f0f32014-02-18 16:06:46 +0800733static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
734 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700736 struct sock *sk = sock->sk;
737 struct tipc_port *tport = tipc_sk_port(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100738 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue391a6dd2014-01-17 09:50:06 +0800739 int res = -EINVAL;
740 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741
742 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743 if (unlikely(dest))
Ying Xue247f0f32014-02-18 16:06:46 +0800744 return tipc_sendmsg(iocb, sock, m, total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745
Ying Xue97f8b872013-01-31 21:51:47 +0100746 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400747 return -EMSGSIZE;
748
Allan Stephens0c3141e2008-04-15 00:22:02 -0700749 if (iocb)
750 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751
Ying Xue391a6dd2014-01-17 09:50:06 +0800752 if (unlikely(sock->state != SS_CONNECTED)) {
753 if (sock->state == SS_DISCONNECTING)
754 res = -EPIPE;
755 else
756 res = -ENOTCONN;
757 goto exit;
758 }
Ying Xue1d835872011-07-06 05:53:15 -0400759
Ying Xue391a6dd2014-01-17 09:50:06 +0800760 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900761 do {
Ying Xue9446b872013-10-18 07:23:15 +0200762 res = tipc_send(tport->ref, m->msg_iov, total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000763 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700764 break;
Ying Xue391a6dd2014-01-17 09:50:06 +0800765 res = tipc_wait_for_sndpkt(sock, &timeo);
766 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700767 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900768 } while (1);
Ying Xue391a6dd2014-01-17 09:50:06 +0800769exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700770 if (iocb)
771 release_sock(sk);
772 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773}
774
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900775/**
Ying Xue247f0f32014-02-18 16:06:46 +0800776 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100777 * @iocb: (unused)
778 * @sock: socket structure
779 * @m: data to send
780 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900781 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900783 *
784 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700785 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100786 */
Ying Xue247f0f32014-02-18 16:06:46 +0800787static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
788 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700790 struct sock *sk = sock->sk;
791 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792 struct msghdr my_msg;
793 struct iovec my_iov;
794 struct iovec *curr_iov;
795 int curr_iovlen;
796 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700797 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798 int curr_left;
799 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700800 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100801 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900802
Allan Stephens0c3141e2008-04-15 00:22:02 -0700803 lock_sock(sk);
804
Allan Stephens05646c92007-06-10 17:25:24 -0700805 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900806 if (unlikely(sock->state != SS_CONNECTED)) {
wangweidong3b8401f2013-12-12 09:36:40 +0800807 if (sock->state == SS_UNCONNECTED)
Ying Xue247f0f32014-02-18 16:06:46 +0800808 res = tipc_send_packet(NULL, sock, m, total_len);
wangweidongb0555972013-12-27 10:09:39 +0800809 else
810 res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800811 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900812 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813
Allan Stephens0c3141e2008-04-15 00:22:02 -0700814 if (unlikely(m->msg_name)) {
815 res = -EISCONN;
816 goto exit;
817 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700818
Ying Xue97f8b872013-01-31 21:51:47 +0100819 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400820 res = -EMSGSIZE;
821 goto exit;
822 }
823
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900824 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825 * Send each iovec entry using one or more messages
826 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900827 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828 * (i.e. one large iovec entry), but could be improved to pass sets
829 * of small iovec entries into send_packet().
830 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700831 curr_iov = m->msg_iov;
832 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100833 my_msg.msg_iov = &my_iov;
834 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700835 my_msg.msg_flags = m->msg_flags;
836 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700837 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100838
Allan Stephens05646c92007-06-10 17:25:24 -0700839 hdr_size = msg_hdr_sz(&tport->phdr);
840
Per Lidenb97bf3f2006-01-02 19:04:38 +0100841 while (curr_iovlen--) {
842 curr_start = curr_iov->iov_base;
843 curr_left = curr_iov->iov_len;
844
845 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700846 bytes_to_send = tport->max_pkt - hdr_size;
847 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
848 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
849 if (curr_left < bytes_to_send)
850 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 my_iov.iov_base = curr_start;
852 my_iov.iov_len = bytes_to_send;
Ying Xue247f0f32014-02-18 16:06:46 +0800853 res = tipc_send_packet(NULL, sock, &my_msg,
854 bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000855 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700856 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700857 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700858 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700859 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100860 curr_left -= bytes_to_send;
861 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700862 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863 }
864
865 curr_iov++;
866 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700867 res = bytes_sent;
868exit:
869 release_sock(sk);
870 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871}
872
873/**
874 * auto_connect - complete connection setup to a remote port
875 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100876 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900877 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878 * Returns 0 on success, errno otherwise
879 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700880static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881{
Allan Stephens2da59912008-07-14 22:43:32 -0700882 struct tipc_sock *tsock = tipc_sk(sock->sk);
Ying Xue584d24b2012-11-29 18:51:19 -0500883 struct tipc_port *p_ptr;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400884 struct tipc_portid peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400886 peer.ref = msg_origport(msg);
887 peer.node = msg_orignode(msg);
888
Ying Xue584d24b2012-11-29 18:51:19 -0500889 p_ptr = tipc_port_deref(tsock->p->ref);
890 if (!p_ptr)
891 return -EINVAL;
892
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400893 __tipc_port_connect(p_ptr->ref, p_ptr, &peer);
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;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001667 struct tipc_portid peer;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001668 u32 new_ref;
Ying Xue6398e232014-01-17 09:50:04 +08001669 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001670 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001671
Allan Stephens0c3141e2008-04-15 00:22:02 -07001672 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001673
Allan Stephens0c3141e2008-04-15 00:22:02 -07001674 if (sock->state != SS_LISTENING) {
1675 res = -EINVAL;
1676 goto exit;
1677 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678
Ying Xue6398e232014-01-17 09:50:04 +08001679 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1680 res = tipc_wait_for_accept(sock, timeo);
1681 if (res)
1682 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001683
1684 buf = skb_peek(&sk->sk_receive_queue);
1685
Ying Xuec5fa7b32013-06-17 10:54:39 -04001686 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001687 if (res)
1688 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001689
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001690 new_sk = new_sock->sk;
1691 new_tsock = tipc_sk(new_sk);
1692 new_tport = new_tsock->p;
1693 new_ref = new_tport->ref;
1694 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001695
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001696 /* we lock on new_sk; but lockdep sees the lock on sk */
1697 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001698
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001699 /*
1700 * Reject any stray messages received by new socket
1701 * before the socket lock was taken (very, very unlikely)
1702 */
1703 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001704
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001705 /* Connect new socket to it's peer */
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001706 peer.ref = msg_origport(msg);
1707 peer.node = msg_orignode(msg);
1708 tipc_port_connect(new_ref, &peer);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001709 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001710
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001711 tipc_set_portimportance(new_ref, msg_importance(msg));
1712 if (msg_named(msg)) {
1713 new_tport->conn_type = msg_nametype(msg);
1714 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001715 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001716
1717 /*
1718 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1719 * Respond to 'SYN+' by queuing it on new socket.
1720 */
1721 if (!msg_data_sz(msg)) {
1722 struct msghdr m = {NULL,};
1723
1724 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001725 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001726 } else {
1727 __skb_dequeue(&sk->sk_receive_queue);
1728 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001729 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001730 }
1731 release_sock(new_sk);
1732
Per Lidenb97bf3f2006-01-02 19:04:38 +01001733exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001734 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001735 return res;
1736}
1737
1738/**
Ying Xue247f0f32014-02-18 16:06:46 +08001739 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001740 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001741 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001742 *
1743 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001744 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001745 * Returns 0 on success, errno otherwise
1746 */
Ying Xue247f0f32014-02-18 16:06:46 +08001747static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001748{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001749 struct sock *sk = sock->sk;
1750 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001751 struct sk_buff *buf;
1752 int res;
1753
Allan Stephense247a8f2008-03-06 15:05:38 -08001754 if (how != SHUT_RDWR)
1755 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001756
Allan Stephens0c3141e2008-04-15 00:22:02 -07001757 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001758
1759 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001760 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001761 case SS_CONNECTED:
1762
Per Lidenb97bf3f2006-01-02 19:04:38 +01001763restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001764 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001765 buf = __skb_dequeue(&sk->sk_receive_queue);
1766 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001767 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001768 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001769 goto restart;
1770 }
Ying Xue247f0f32014-02-18 16:06:46 +08001771 tipc_port_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001772 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001773 } else {
Ying Xue247f0f32014-02-18 16:06:46 +08001774 tipc_port_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001775 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001776
1777 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001778
1779 /* fall through */
1780
1781 case SS_DISCONNECTING:
1782
Ying Xue75031152012-10-29 09:38:15 -04001783 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001784 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001785
1786 /* Wake up anyone sleeping in poll */
1787 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001788 res = 0;
1789 break;
1790
1791 default:
1792 res = -ENOTCONN;
1793 }
1794
Allan Stephens0c3141e2008-04-15 00:22:02 -07001795 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001796 return res;
1797}
1798
1799/**
Ying Xue247f0f32014-02-18 16:06:46 +08001800 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001801 * @sock: socket structure
1802 * @lvl: option level
1803 * @opt: option identifier
1804 * @ov: pointer to new option value
1805 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001806 *
1807 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001808 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001809 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001810 * Returns 0 on success, errno otherwise
1811 */
Ying Xue247f0f32014-02-18 16:06:46 +08001812static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1813 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001815 struct sock *sk = sock->sk;
1816 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001817 u32 value;
1818 int res;
1819
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001820 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1821 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001822 if (lvl != SOL_TIPC)
1823 return -ENOPROTOOPT;
1824 if (ol < sizeof(value))
1825 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001826 res = get_user(value, (u32 __user *)ov);
1827 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001828 return res;
1829
Allan Stephens0c3141e2008-04-15 00:22:02 -07001830 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001831
Per Lidenb97bf3f2006-01-02 19:04:38 +01001832 switch (opt) {
1833 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001834 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001835 break;
1836 case TIPC_SRC_DROPPABLE:
1837 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001838 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001839 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001840 res = -ENOPROTOOPT;
1841 break;
1842 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001843 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001844 break;
1845 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001846 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001847 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001848 break;
1849 default:
1850 res = -EINVAL;
1851 }
1852
Allan Stephens0c3141e2008-04-15 00:22:02 -07001853 release_sock(sk);
1854
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855 return res;
1856}
1857
1858/**
Ying Xue247f0f32014-02-18 16:06:46 +08001859 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001860 * @sock: socket structure
1861 * @lvl: option level
1862 * @opt: option identifier
1863 * @ov: receptacle for option value
1864 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001865 *
1866 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001867 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001868 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001869 * Returns 0 on success, errno otherwise
1870 */
Ying Xue247f0f32014-02-18 16:06:46 +08001871static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1872 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001873{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001874 struct sock *sk = sock->sk;
1875 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001876 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001877 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001878 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001879
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001880 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1881 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001882 if (lvl != SOL_TIPC)
1883 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001884 res = get_user(len, ol);
1885 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001886 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001887
Allan Stephens0c3141e2008-04-15 00:22:02 -07001888 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001889
1890 switch (opt) {
1891 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001892 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001893 break;
1894 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001895 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001896 break;
1897 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001898 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001899 break;
1900 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001901 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001902 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001903 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001904 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001905 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001906 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001907 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001908 value = skb_queue_len(&sk->sk_receive_queue);
1909 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001910 default:
1911 res = -EINVAL;
1912 }
1913
Allan Stephens0c3141e2008-04-15 00:22:02 -07001914 release_sock(sk);
1915
Paul Gortmaker25860c32010-12-31 18:59:31 +00001916 if (res)
1917 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001918
Paul Gortmaker25860c32010-12-31 18:59:31 +00001919 if (len < sizeof(value))
1920 return -EINVAL;
1921
1922 if (copy_to_user(ov, &value, sizeof(value)))
1923 return -EFAULT;
1924
1925 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001926}
1927
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001928/* Protocol switches for the various types of TIPC sockets */
1929
Florian Westphalbca65ea2008-02-07 18:18:01 -08001930static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001931 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001932 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001933 .release = tipc_release,
1934 .bind = tipc_bind,
1935 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001936 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001937 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08001938 .getname = tipc_getname,
1939 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001940 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001941 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08001942 .shutdown = tipc_shutdown,
1943 .setsockopt = tipc_setsockopt,
1944 .getsockopt = tipc_getsockopt,
1945 .sendmsg = tipc_sendmsg,
1946 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001947 .mmap = sock_no_mmap,
1948 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001949};
1950
Florian Westphalbca65ea2008-02-07 18:18:01 -08001951static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001952 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001953 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001954 .release = tipc_release,
1955 .bind = tipc_bind,
1956 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001957 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08001958 .accept = tipc_accept,
1959 .getname = tipc_getname,
1960 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001961 .ioctl = sock_no_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08001962 .listen = tipc_listen,
1963 .shutdown = tipc_shutdown,
1964 .setsockopt = tipc_setsockopt,
1965 .getsockopt = tipc_getsockopt,
1966 .sendmsg = tipc_send_packet,
1967 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001968 .mmap = sock_no_mmap,
1969 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001970};
1971
Florian Westphalbca65ea2008-02-07 18:18:01 -08001972static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001973 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001974 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001975 .release = tipc_release,
1976 .bind = tipc_bind,
1977 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001978 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08001979 .accept = tipc_accept,
1980 .getname = tipc_getname,
1981 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001982 .ioctl = sock_no_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08001983 .listen = tipc_listen,
1984 .shutdown = tipc_shutdown,
1985 .setsockopt = tipc_setsockopt,
1986 .getsockopt = tipc_getsockopt,
1987 .sendmsg = tipc_send_stream,
1988 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001989 .mmap = sock_no_mmap,
1990 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001991};
1992
Florian Westphalbca65ea2008-02-07 18:18:01 -08001993static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001994 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001995 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04001996 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01001997};
1998
1999static struct proto tipc_proto = {
2000 .name = "TIPC",
2001 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002002 .obj_size = sizeof(struct tipc_sock),
2003 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002004};
2005
Ying Xuec5fa7b32013-06-17 10:54:39 -04002006static struct proto tipc_proto_kern = {
2007 .name = "TIPC",
2008 .obj_size = sizeof(struct tipc_sock),
2009 .sysctl_rmem = sysctl_tipc_rmem
2010};
2011
Per Lidenb97bf3f2006-01-02 19:04:38 +01002012/**
Per Liden4323add2006-01-18 00:38:21 +01002013 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002014 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002015 * Returns 0 on success, errno otherwise
2016 */
Per Liden4323add2006-01-18 00:38:21 +01002017int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002018{
2019 int res;
2020
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002021 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002022 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002023 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002024 goto out;
2025 }
2026
2027 res = sock_register(&tipc_family_ops);
2028 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002029 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002030 proto_unregister(&tipc_proto);
2031 goto out;
2032 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002033 out:
2034 return res;
2035}
2036
2037/**
Per Liden4323add2006-01-18 00:38:21 +01002038 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002039 */
Per Liden4323add2006-01-18 00:38:21 +01002040void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002041{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002042 sock_unregister(tipc_family_ops.family);
2043 proto_unregister(&tipc_proto);
2044}