blob: 912665d409de256bddb30b4d47f41b8f597d4ab8 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04004 * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000038#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039
Erik Hugne2cf8aa12012-06-29 00:16:37 -040040#include <linux/export.h>
Erik Hugne2cf8aa12012-06-29 00:16:37 -040041
Per Lidenb97bf3f2006-01-02 19:04:38 +010042#define SS_LISTENING -1 /* socket is listening */
43#define SS_READY -2 /* socket is connectionless */
44
Allan Stephens3654ea02008-04-13 21:35:11 -070045#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010046
Per Lidenb97bf3f2006-01-02 19:04:38 +010047
Allan Stephens0c3141e2008-04-15 00:22:02 -070048static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +010049static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
50static void wakeupdispatch(struct tipc_port *tport);
Ying Xuef288bef2012-08-21 11:16:57 +080051static void tipc_data_ready(struct sock *sk, int len);
52static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080053static int tipc_release(struct socket *sock);
54static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Per Lidenb97bf3f2006-01-02 19:04:38 +010055
Florian Westphalbca65ea2008-02-07 18:18:01 -080056static const struct proto_ops packet_ops;
57static const struct proto_ops stream_ops;
58static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010059
60static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040061static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010062
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090063/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070064 * Revised TIPC socket locking policy:
65 *
66 * Most socket operations take the standard socket lock when they start
67 * and hold it until they finish (or until they need to sleep). Acquiring
68 * this lock grants the owner exclusive access to the fields of the socket
69 * data structures, with the exception of the backlog queue. A few socket
70 * operations can be done without taking the socket lock because they only
71 * read socket information that never changes during the life of the socket.
72 *
73 * Socket operations may acquire the lock for the associated TIPC port if they
74 * need to perform an operation on the port. If any routine needs to acquire
75 * both the socket lock and the port lock it must take the socket lock first
76 * to avoid the risk of deadlock.
77 *
78 * The dispatcher handling incoming messages cannot grab the socket lock in
79 * the standard fashion, since invoked it runs at the BH level and cannot block.
80 * Instead, it checks to see if the socket lock is currently owned by someone,
81 * and either handles the message itself or adds it to the socket's backlog
82 * queue; in the latter case the queued message is processed once the process
83 * owning the socket lock releases it.
84 *
85 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
86 * the problem of a blocked socket operation preventing any other operations
87 * from occurring. However, applications must be careful if they have
88 * multiple threads trying to send (or receive) on the same socket, as these
89 * operations might interfere with each other. For example, doing a connect
90 * and a receive at the same time might allow the receive to consume the
91 * ACK message meant for the connect. While additional work could be done
92 * to try and overcome this, it doesn't seem to be worthwhile at the present.
93 *
94 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
95 * that another operation that must be performed in a non-blocking manner is
96 * not delayed for very long because the lock has already been taken.
97 *
98 * NOTE: This code assumes that certain fields of a port/socket pair are
99 * constant over its lifetime; such fields can be examined without taking
100 * the socket lock and/or port lock, and do not need to be re-read even
101 * after resuming processing after waiting. These fields include:
102 * - socket type
103 * - pointer to socket sk structure (aka tipc_sock structure)
104 * - pointer to port structure
105 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100106 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400108#include "socket.h"
109
Per Lidenb97bf3f2006-01-02 19:04:38 +0100110/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700111 * advance_rx_queue - discard first buffer in socket receive queue
112 *
113 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700115static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400117 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118}
119
120/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700121 * reject_rx_queue - reject all buffers in socket receive queue
122 *
123 * Caller must hold socket lock
124 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700125static void reject_rx_queue(struct sock *sk)
126{
127 struct sk_buff *buf;
128
Ying Xue9da3d472012-11-27 06:15:27 -0500129 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700130 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700131}
132
133/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400134 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700135 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136 * @sock: pre-allocated socket structure
137 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800138 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900139 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700140 * This routine creates additional data structures used by the TIPC socket,
141 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100142 *
143 * Returns 0 on success, errno otherwise
144 */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400145static int tipc_sk_create(struct net *net, struct socket *sock, int protocol,
146 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100147{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700148 const struct proto_ops *ops;
149 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100150 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700151 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700152
153 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154 if (unlikely(protocol != 0))
155 return -EPROTONOSUPPORT;
156
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 switch (sock->type) {
158 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700159 ops = &stream_ops;
160 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161 break;
162 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700163 ops = &packet_ops;
164 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 break;
166 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700168 ops = &msg_ops;
169 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 break;
Allan Stephens49978652006-06-25 23:47:18 -0700171 default:
Allan Stephens49978652006-06-25 23:47:18 -0700172 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 }
174
Allan Stephens0c3141e2008-04-15 00:22:02 -0700175 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400176 if (!kern)
177 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
178 else
179 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
180
Allan Stephens0c3141e2008-04-15 00:22:02 -0700181 if (sk == NULL)
182 return -ENOMEM;
183
184 /* Allocate TIPC port for socket to use */
Ying Xue3c5db8e2013-06-17 10:54:44 -0400185 tp_ptr = tipc_createport(sk, &dispatch, &wakeupdispatch,
186 TIPC_LOW_IMPORTANCE);
Allan Stephens0ea52242008-07-14 22:42:19 -0700187 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700188 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100189 return -ENOMEM;
190 }
191
Allan Stephens0c3141e2008-04-15 00:22:02 -0700192 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700193 sock->ops = ops;
194 sock->state = state;
195
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700197 sk->sk_backlog_rcv = backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400198 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800199 sk->sk_data_ready = tipc_data_ready;
200 sk->sk_write_space = tipc_write_space;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400201 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700203 spin_unlock_bh(tp_ptr->lock);
204
Allan Stephens0c3141e2008-04-15 00:22:02 -0700205 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700206 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700207 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700208 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700209 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211 return 0;
212}
213
214/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400215 * tipc_sock_create_local - create TIPC socket from inside TIPC module
216 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
217 *
218 * We cannot use sock_creat_kern here because it bumps module user count.
219 * Since socket owner and creator is the same module we must make sure
220 * that module count remains zero for module local sockets, otherwise
221 * we cannot do rmmod.
222 *
223 * Returns 0 on success, errno otherwise
224 */
225int tipc_sock_create_local(int type, struct socket **res)
226{
227 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400228
229 rc = sock_create_lite(AF_TIPC, type, 0, res);
230 if (rc < 0) {
231 pr_err("Failed to create kernel socket\n");
232 return rc;
233 }
234 tipc_sk_create(&init_net, *res, 0, 1);
235
Ying Xuec5fa7b32013-06-17 10:54:39 -0400236 return 0;
237}
238
239/**
240 * tipc_sock_release_local - release socket created by tipc_sock_create_local
241 * @sock: the socket to be released.
242 *
243 * Module reference count is not incremented when such sockets are created,
244 * so we must keep it from being decremented when they are released.
245 */
246void tipc_sock_release_local(struct socket *sock)
247{
Ying Xue247f0f32014-02-18 16:06:46 +0800248 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400249 sock->ops = NULL;
250 sock_release(sock);
251}
252
253/**
254 * tipc_sock_accept_local - accept a connection on a socket created
255 * with tipc_sock_create_local. Use this function to avoid that
256 * module reference count is inadvertently incremented.
257 *
258 * @sock: the accepting socket
259 * @newsock: reference to the new socket to be created
260 * @flags: socket flags
261 */
262
263int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400264 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400265{
266 struct sock *sk = sock->sk;
267 int ret;
268
269 ret = sock_create_lite(sk->sk_family, sk->sk_type,
270 sk->sk_protocol, newsock);
271 if (ret < 0)
272 return ret;
273
Ying Xue247f0f32014-02-18 16:06:46 +0800274 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400275 if (ret < 0) {
276 sock_release(*newsock);
277 return ret;
278 }
279 (*newsock)->ops = sock->ops;
280 return ret;
281}
282
283/**
Ying Xue247f0f32014-02-18 16:06:46 +0800284 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285 * @sock: socket to destroy
286 *
287 * This routine cleans up any messages that are still queued on the socket.
288 * For DGRAM and RDM socket types, all queued messages are rejected.
289 * For SEQPACKET and STREAM socket types, the first message is rejected
290 * and any others are discarded. (If the first message on a STREAM socket
291 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900292 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293 * NOTE: Rejected messages are not necessarily returned to the sender! They
294 * are returned or discarded according to the "destination droppable" setting
295 * specified for the message by the sender.
296 *
297 * Returns 0 on success, errno otherwise
298 */
Ying Xue247f0f32014-02-18 16:06:46 +0800299static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700302 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700304 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305
Allan Stephens0c3141e2008-04-15 00:22:02 -0700306 /*
307 * Exit if socket isn't fully initialized (occurs when a failed accept()
308 * releases a pre-allocated child socket that was never used)
309 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700310 if (sk == NULL)
311 return 0;
312
313 tport = tipc_sk_port(sk);
314 lock_sock(sk);
315
316 /*
317 * Reject all unreceived messages, except on an active connection
318 * (which disconnects locally & sends a 'FIN+' to peer)
319 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700321 buf = __skb_dequeue(&sk->sk_receive_queue);
322 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100323 break;
Ying Xue40682432013-10-18 07:23:16 +0200324 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400325 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700326 else {
327 if ((sock->state == SS_CONNECTING) ||
328 (sock->state == SS_CONNECTED)) {
329 sock->state = SS_DISCONNECTING;
Ying Xue247f0f32014-02-18 16:06:46 +0800330 tipc_port_disconnect(tport->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700331 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700333 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334 }
335
Allan Stephens0c3141e2008-04-15 00:22:02 -0700336 /*
337 * Delete TIPC port; this ensures no more messages are queued
338 * (also disconnects an active connection & sends a 'FIN-' to peer)
339 */
Ying Xue84602762013-12-27 10:18:28 +0800340 res = tipc_deleteport(tport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341
Allan Stephens0c3141e2008-04-15 00:22:02 -0700342 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100343 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344
Allan Stephens0c3141e2008-04-15 00:22:02 -0700345 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700346 sock->state = SS_DISCONNECTING;
347 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348
349 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700350 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352 return res;
353}
354
355/**
Ying Xue247f0f32014-02-18 16:06:46 +0800356 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357 * @sock: socket structure
358 * @uaddr: socket address describing name(s) and desired operation
359 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900360 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361 * Name and name sequence binding is indicated using a positive scope value;
362 * a negative scope value unbinds the specified name. Specifying no name
363 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900364 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700366 *
367 * NOTE: This routine doesn't need to take the socket lock since it doesn't
368 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369 */
Ying Xue247f0f32014-02-18 16:06:46 +0800370static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
371 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372{
Ying Xue84602762013-12-27 10:18:28 +0800373 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Ying Xue84602762013-12-27 10:18:28 +0800375 struct tipc_port *tport = tipc_sk_port(sock->sk);
376 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377
Ying Xue84602762013-12-27 10:18:28 +0800378 lock_sock(sk);
379 if (unlikely(!uaddr_len)) {
380 res = tipc_withdraw(tport, 0, NULL);
381 goto exit;
382 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900383
Ying Xue84602762013-12-27 10:18:28 +0800384 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
385 res = -EINVAL;
386 goto exit;
387 }
388 if (addr->family != AF_TIPC) {
389 res = -EAFNOSUPPORT;
390 goto exit;
391 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 if (addr->addrtype == TIPC_ADDR_NAME)
394 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800395 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
396 res = -EAFNOSUPPORT;
397 goto exit;
398 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900399
Ying Xue13a2e892013-06-17 10:54:40 -0400400 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400401 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800402 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
403 res = -EACCES;
404 goto exit;
405 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400406
Ying Xue84602762013-12-27 10:18:28 +0800407 res = (addr->scope > 0) ?
408 tipc_publish(tport, addr->scope, &addr->addr.nameseq) :
409 tipc_withdraw(tport, -addr->scope, &addr->addr.nameseq);
410exit:
411 release_sock(sk);
412 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413}
414
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900415/**
Ying Xue247f0f32014-02-18 16:06:46 +0800416 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 * @sock: socket structure
418 * @uaddr: area for returned socket address
419 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700420 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900421 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700423 *
Allan Stephens2da59912008-07-14 22:43:32 -0700424 * NOTE: This routine doesn't need to take the socket lock since it only
425 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000426 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 */
Ying Xue247f0f32014-02-18 16:06:46 +0800428static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
429 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400432 struct tipc_port *port = tipc_sk_port(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000434 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700435 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700436 if ((sock->state != SS_CONNECTED) &&
437 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
438 return -ENOTCONN;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400439 addr->addr.id.ref = tipc_port_peerport(port);
440 addr->addr.id.node = tipc_port_peernode(port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700441 } else {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400442 addr->addr.id.ref = port->ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000443 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700444 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445
446 *uaddr_len = sizeof(*addr);
447 addr->addrtype = TIPC_ADDR_ID;
448 addr->family = AF_TIPC;
449 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 addr->addr.name.domain = 0;
451
Allan Stephens0c3141e2008-04-15 00:22:02 -0700452 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453}
454
455/**
Ying Xue247f0f32014-02-18 16:06:46 +0800456 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 * @file: file structure associated with the socket
458 * @sock: socket for which to calculate the poll bits
459 * @wait: ???
460 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700461 * Returns pollmask value
462 *
463 * COMMENTARY:
464 * It appears that the usual socket locking mechanisms are not useful here
465 * since the pollmask info is potentially out-of-date the moment this routine
466 * exits. TCP and other protocols seem to rely on higher level poll routines
467 * to handle any preventable race conditions, so TIPC will do the same ...
468 *
469 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700470 *
Allan Stephensf662c072010-08-17 11:00:06 +0000471 * socket state flags set
472 * ------------ ---------
473 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200474 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000475 *
476 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
477 * no write flags
478 *
479 * connected POLLIN/POLLRDNORM if data in rx queue
480 * POLLOUT if port is not congested
481 *
482 * disconnecting POLLIN/POLLRDNORM/POLLHUP
483 * no write flags
484 *
485 * listening POLLIN if SYN in rx queue
486 * no write flags
487 *
488 * ready POLLIN/POLLRDNORM if data in rx queue
489 * [connectionless] POLLOUT (since port cannot be congested)
490 *
491 * IMPORTANT: The fact that a read or write operation is indicated does NOT
492 * imply that the operation will succeed, merely that it should be performed
493 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494 */
Ying Xue247f0f32014-02-18 16:06:46 +0800495static unsigned int tipc_poll(struct file *file, struct socket *sock,
496 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497{
Allan Stephens9b674e82008-03-26 16:48:21 -0700498 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000499 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700500
Ying Xuef288bef2012-08-21 11:16:57 +0800501 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700502
Allan Stephensf662c072010-08-17 11:00:06 +0000503 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200504 case SS_UNCONNECTED:
505 if (!tipc_sk_port(sk)->congested)
506 mask |= POLLOUT;
507 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000508 case SS_READY:
509 case SS_CONNECTED:
510 if (!tipc_sk_port(sk)->congested)
511 mask |= POLLOUT;
512 /* fall thru' */
513 case SS_CONNECTING:
514 case SS_LISTENING:
515 if (!skb_queue_empty(&sk->sk_receive_queue))
516 mask |= (POLLIN | POLLRDNORM);
517 break;
518 case SS_DISCONNECTING:
519 mask = (POLLIN | POLLRDNORM | POLLHUP);
520 break;
521 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700522
523 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524}
525
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900526/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527 * dest_name_check - verify user is permitted to send to specified port name
528 * @dest: destination address
529 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900530 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531 * Prevents restricted configuration commands from being issued by
532 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900533 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534 * Returns 0 if permission is granted, otherwise errno
535 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800536static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537{
538 struct tipc_cfg_msg_hdr hdr;
539
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900540 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
541 return 0;
542 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
543 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900544 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
545 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546
Allan Stephens3f8dd942011-01-18 13:09:29 -0500547 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
548 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900549 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700551 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900553
Per Lidenb97bf3f2006-01-02 19:04:38 +0100554 return 0;
555}
556
Ying Xue3f405042014-01-17 09:50:05 +0800557static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
558{
559 struct sock *sk = sock->sk;
560 struct tipc_port *tport = tipc_sk_port(sk);
561 DEFINE_WAIT(wait);
562 int done;
563
564 do {
565 int err = sock_error(sk);
566 if (err)
567 return err;
568 if (sock->state == SS_DISCONNECTING)
569 return -EPIPE;
570 if (!*timeo_p)
571 return -EAGAIN;
572 if (signal_pending(current))
573 return sock_intr_errno(*timeo_p);
574
575 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
576 done = sk_wait_event(sk, timeo_p, !tport->congested);
577 finish_wait(sk_sleep(sk), &wait);
578 } while (!done);
579 return 0;
580}
581
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582/**
Ying Xue247f0f32014-02-18 16:06:46 +0800583 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700584 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 * @sock: socket structure
586 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700587 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900588 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900590 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
592 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900593 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 * Returns the number of bytes sent on success, or errno otherwise
595 */
Ying Xue247f0f32014-02-18 16:06:46 +0800596static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
597 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700599 struct sock *sk = sock->sk;
600 struct tipc_port *tport = tipc_sk_port(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100601 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602 int needs_conn;
Ying Xue3f405042014-01-17 09:50:05 +0800603 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604 int res = -EINVAL;
605
606 if (unlikely(!dest))
607 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700608 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
609 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 return -EINVAL;
Ying Xue97f8b872013-01-31 21:51:47 +0100611 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400612 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613
Allan Stephens0c3141e2008-04-15 00:22:02 -0700614 if (iocb)
615 lock_sock(sk);
616
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 needs_conn = (sock->state != SS_READY);
618 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700619 if (sock->state == SS_LISTENING) {
620 res = -EPIPE;
621 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700622 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700623 if (sock->state != SS_UNCONNECTED) {
624 res = -EISCONN;
625 goto exit;
626 }
Erik Hugne5d21cb72013-06-17 10:54:38 -0400627 if (tport->published) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700628 res = -EOPNOTSUPP;
629 goto exit;
630 }
631 if (dest->addrtype == TIPC_ADDR_NAME) {
632 tport->conn_type = dest->addr.name.name.type;
633 tport->conn_instance = dest->addr.name.name.instance;
634 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635
636 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700637 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 }
639
Ying Xue3f405042014-01-17 09:50:05 +0800640 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900641 do {
642 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000643 res = dest_name_check(dest, m);
644 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700645 break;
646 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900647 &dest->addr.name.name,
648 dest->addr.name.domain,
Allan Stephens26896902011-04-21 10:42:07 -0500649 m->msg_iov,
650 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000651 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700652 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900653 &dest->addr.id,
Allan Stephens26896902011-04-21 10:42:07 -0500654 m->msg_iov,
655 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000656 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 if (needs_conn) {
658 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700659 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 }
Allan Stephens2db99832010-12-31 18:59:33 +0000661 res = dest_name_check(dest, m);
662 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700663 break;
Ying Xue247f0f32014-02-18 16:06:46 +0800664 res = tipc_port_mcast_xmit(tport->ref,
665 &dest->addr.nameseq,
666 m->msg_iov,
667 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900668 }
669 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000670 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700671 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700672 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900673 }
Ying Xue3f405042014-01-17 09:50:05 +0800674 res = tipc_wait_for_sndmsg(sock, &timeo);
675 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700676 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900677 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700678
679exit:
680 if (iocb)
681 release_sock(sk);
682 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683}
684
Ying Xue391a6dd2014-01-17 09:50:06 +0800685static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
686{
687 struct sock *sk = sock->sk;
688 struct tipc_port *tport = tipc_sk_port(sk);
689 DEFINE_WAIT(wait);
690 int done;
691
692 do {
693 int err = sock_error(sk);
694 if (err)
695 return err;
696 if (sock->state == SS_DISCONNECTING)
697 return -EPIPE;
698 else if (sock->state != SS_CONNECTED)
699 return -ENOTCONN;
700 if (!*timeo_p)
701 return -EAGAIN;
702 if (signal_pending(current))
703 return sock_intr_errno(*timeo_p);
704
705 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
706 done = sk_wait_event(sk, timeo_p,
707 (!tport->congested || !tport->connected));
708 finish_wait(sk_sleep(sk), &wait);
709 } while (!done);
710 return 0;
711}
712
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900713/**
Ying Xue247f0f32014-02-18 16:06:46 +0800714 * tipc_send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700715 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716 * @sock: socket structure
717 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700718 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900719 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100720 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900721 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100722 * Returns the number of bytes sent on success, or errno otherwise
723 */
Ying Xue247f0f32014-02-18 16:06:46 +0800724static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
725 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700727 struct sock *sk = sock->sk;
728 struct tipc_port *tport = tipc_sk_port(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100729 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue391a6dd2014-01-17 09:50:06 +0800730 int res = -EINVAL;
731 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732
733 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734 if (unlikely(dest))
Ying Xue247f0f32014-02-18 16:06:46 +0800735 return tipc_sendmsg(iocb, sock, m, total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736
Ying Xue97f8b872013-01-31 21:51:47 +0100737 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400738 return -EMSGSIZE;
739
Allan Stephens0c3141e2008-04-15 00:22:02 -0700740 if (iocb)
741 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742
Ying Xue391a6dd2014-01-17 09:50:06 +0800743 if (unlikely(sock->state != SS_CONNECTED)) {
744 if (sock->state == SS_DISCONNECTING)
745 res = -EPIPE;
746 else
747 res = -ENOTCONN;
748 goto exit;
749 }
Ying Xue1d835872011-07-06 05:53:15 -0400750
Ying Xue391a6dd2014-01-17 09:50:06 +0800751 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900752 do {
Ying Xue9446b872013-10-18 07:23:15 +0200753 res = tipc_send(tport->ref, m->msg_iov, total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000754 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700755 break;
Ying Xue391a6dd2014-01-17 09:50:06 +0800756 res = tipc_wait_for_sndpkt(sock, &timeo);
757 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700758 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900759 } while (1);
Ying Xue391a6dd2014-01-17 09:50:06 +0800760exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700761 if (iocb)
762 release_sock(sk);
763 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764}
765
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900766/**
Ying Xue247f0f32014-02-18 16:06:46 +0800767 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100768 * @iocb: (unused)
769 * @sock: socket structure
770 * @m: data to send
771 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900772 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900774 *
775 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700776 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100777 */
Ying Xue247f0f32014-02-18 16:06:46 +0800778static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
779 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700781 struct sock *sk = sock->sk;
782 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100783 struct msghdr my_msg;
784 struct iovec my_iov;
785 struct iovec *curr_iov;
786 int curr_iovlen;
787 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700788 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789 int curr_left;
790 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700791 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900793
Allan Stephens0c3141e2008-04-15 00:22:02 -0700794 lock_sock(sk);
795
Allan Stephens05646c92007-06-10 17:25:24 -0700796 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900797 if (unlikely(sock->state != SS_CONNECTED)) {
wangweidong3b8401f2013-12-12 09:36:40 +0800798 if (sock->state == SS_UNCONNECTED)
Ying Xue247f0f32014-02-18 16:06:46 +0800799 res = tipc_send_packet(NULL, sock, m, total_len);
wangweidongb0555972013-12-27 10:09:39 +0800800 else
801 res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800802 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900803 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804
Allan Stephens0c3141e2008-04-15 00:22:02 -0700805 if (unlikely(m->msg_name)) {
806 res = -EISCONN;
807 goto exit;
808 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700809
Ying Xue97f8b872013-01-31 21:51:47 +0100810 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400811 res = -EMSGSIZE;
812 goto exit;
813 }
814
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900815 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100816 * Send each iovec entry using one or more messages
817 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900818 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100819 * (i.e. one large iovec entry), but could be improved to pass sets
820 * of small iovec entries into send_packet().
821 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700822 curr_iov = m->msg_iov;
823 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824 my_msg.msg_iov = &my_iov;
825 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700826 my_msg.msg_flags = m->msg_flags;
827 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700828 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829
Allan Stephens05646c92007-06-10 17:25:24 -0700830 hdr_size = msg_hdr_sz(&tport->phdr);
831
Per Lidenb97bf3f2006-01-02 19:04:38 +0100832 while (curr_iovlen--) {
833 curr_start = curr_iov->iov_base;
834 curr_left = curr_iov->iov_len;
835
836 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700837 bytes_to_send = tport->max_pkt - hdr_size;
838 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
839 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
840 if (curr_left < bytes_to_send)
841 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100842 my_iov.iov_base = curr_start;
843 my_iov.iov_len = bytes_to_send;
Ying Xue247f0f32014-02-18 16:06:46 +0800844 res = tipc_send_packet(NULL, sock, &my_msg,
845 bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000846 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700847 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700848 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700849 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700850 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 curr_left -= bytes_to_send;
852 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700853 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854 }
855
856 curr_iov++;
857 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700858 res = bytes_sent;
859exit:
860 release_sock(sk);
861 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100862}
863
864/**
865 * auto_connect - complete connection setup to a remote port
866 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900868 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100869 * Returns 0 on success, errno otherwise
870 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700871static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872{
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400873 struct tipc_port *p_ptr = tipc_sk_port(sock->sk);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400874 struct tipc_portid peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100875
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400876 peer.ref = msg_origport(msg);
877 peer.node = msg_orignode(msg);
878
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400879 p_ptr = tipc_port_deref(p_ptr->ref);
Ying Xue584d24b2012-11-29 18:51:19 -0500880 if (!p_ptr)
881 return -EINVAL;
882
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400883 __tipc_port_connect(p_ptr->ref, p_ptr, &peer);
Ying Xue584d24b2012-11-29 18:51:19 -0500884
885 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
886 return -EINVAL;
887 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100888 sock->state = SS_CONNECTED;
889 return 0;
890}
891
892/**
893 * set_orig_addr - capture sender's address for received message
894 * @m: descriptor for message info
895 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900896 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897 * Note: Address is not captured if not requested by receiver.
898 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800899static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900{
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100901 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900903 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904 addr->family = AF_TIPC;
905 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000906 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907 addr->addr.id.ref = msg_origport(msg);
908 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000909 addr->addr.name.domain = 0; /* could leave uninitialized */
910 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100911 m->msg_namelen = sizeof(struct sockaddr_tipc);
912 }
913}
914
915/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900916 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100917 * @m: descriptor for message info
918 * @msg: received message header
919 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900920 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900922 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100923 * Returns 0 if successful, otherwise errno
924 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800925static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400926 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927{
928 u32 anc_data[3];
929 u32 err;
930 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700931 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100932 int res;
933
934 if (likely(m->msg_controllen == 0))
935 return 0;
936
937 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100938 err = msg ? msg_errcode(msg) : 0;
939 if (unlikely(err)) {
940 anc_data[0] = err;
941 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000942 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
943 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100944 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000945 if (anc_data[1]) {
946 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
947 msg_data(msg));
948 if (res)
949 return res;
950 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951 }
952
953 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100954 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
955 switch (dest_type) {
956 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700957 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958 anc_data[0] = msg_nametype(msg);
959 anc_data[1] = msg_namelower(msg);
960 anc_data[2] = msg_namelower(msg);
961 break;
962 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700963 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964 anc_data[0] = msg_nametype(msg);
965 anc_data[1] = msg_namelower(msg);
966 anc_data[2] = msg_nameupper(msg);
967 break;
968 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700969 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970 anc_data[0] = tport->conn_type;
971 anc_data[1] = tport->conn_instance;
972 anc_data[2] = tport->conn_instance;
973 break;
974 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700975 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976 }
Allan Stephens2db99832010-12-31 18:59:33 +0000977 if (has_name) {
978 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
979 if (res)
980 return res;
981 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100982
983 return 0;
984}
985
Ying Xue9bbb4ec2014-01-17 09:50:07 +0800986static int tipc_wait_for_rcvmsg(struct socket *sock, long timeo)
987{
988 struct sock *sk = sock->sk;
989 DEFINE_WAIT(wait);
990 int err;
991
992 for (;;) {
993 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
994 if (skb_queue_empty(&sk->sk_receive_queue)) {
995 if (sock->state == SS_DISCONNECTING) {
996 err = -ENOTCONN;
997 break;
998 }
999 release_sock(sk);
1000 timeo = schedule_timeout(timeo);
1001 lock_sock(sk);
1002 }
1003 err = 0;
1004 if (!skb_queue_empty(&sk->sk_receive_queue))
1005 break;
1006 err = sock_intr_errno(timeo);
1007 if (signal_pending(current))
1008 break;
1009 err = -EAGAIN;
1010 if (!timeo)
1011 break;
1012 }
1013 finish_wait(sk_sleep(sk), &wait);
1014 return err;
1015}
1016
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001017/**
Ying Xue247f0f32014-02-18 16:06:46 +08001018 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001019 * @iocb: (unused)
1020 * @m: descriptor for message info
1021 * @buf_len: total size of user buffer area
1022 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001023 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1025 * If the complete message doesn't fit in user area, truncate it.
1026 *
1027 * Returns size of returned message data, errno otherwise
1028 */
Ying Xue247f0f32014-02-18 16:06:46 +08001029static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1030 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001032 struct sock *sk = sock->sk;
1033 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034 struct sk_buff *buf;
1035 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001036 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037 unsigned int sz;
1038 u32 err;
1039 int res;
1040
Allan Stephens0c3141e2008-04-15 00:22:02 -07001041 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001042 if (unlikely(!buf_len))
1043 return -EINVAL;
1044
Allan Stephens0c3141e2008-04-15 00:22:02 -07001045 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046
Allan Stephens0c3141e2008-04-15 00:22:02 -07001047 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 res = -ENOTCONN;
1049 goto exit;
1050 }
1051
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001052 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001053restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054
Allan Stephens0c3141e2008-04-15 00:22:02 -07001055 /* Look for a message in receive queue; wait if necessary */
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001056 res = tipc_wait_for_rcvmsg(sock, timeo);
1057 if (res)
1058 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001059
1060 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001061 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 msg = buf_msg(buf);
1063 sz = msg_data_sz(msg);
1064 err = msg_errcode(msg);
1065
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001068 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 goto restart;
1070 }
1071
1072 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 set_orig_addr(m, msg);
1074
1075 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001076 res = anc_data_recv(m, msg, tport);
1077 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 goto exit;
1079
1080 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001081 if (!err) {
1082 if (unlikely(buf_len < sz)) {
1083 sz = buf_len;
1084 m->msg_flags |= MSG_TRUNC;
1085 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001086 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1087 m->msg_iov, sz);
1088 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090 res = sz;
1091 } else {
1092 if ((sock->state == SS_READY) ||
1093 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1094 res = 0;
1095 else
1096 res = -ECONNRESET;
1097 }
1098
1099 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001100 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001101 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -07001102 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1103 tipc_acknowledge(tport->ref, tport->conn_unacked);
1104 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001105 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001106exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001107 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108 return res;
1109}
1110
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001111/**
Ying Xue247f0f32014-02-18 16:06:46 +08001112 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001113 * @iocb: (unused)
1114 * @m: descriptor for message info
1115 * @buf_len: total size of user buffer area
1116 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001117 *
1118 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001119 * will optionally wait for more; never truncates data.
1120 *
1121 * Returns size of returned message data, errno otherwise
1122 */
Ying Xue247f0f32014-02-18 16:06:46 +08001123static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1124 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001125{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001126 struct sock *sk = sock->sk;
1127 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001128 struct sk_buff *buf;
1129 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001130 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001131 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001132 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001135 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136
1137 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138 if (unlikely(!buf_len))
1139 return -EINVAL;
1140
Allan Stephens0c3141e2008-04-15 00:22:02 -07001141 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001143 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144 res = -ENOTCONN;
1145 goto exit;
1146 }
1147
Florian Westphal3720d402010-08-17 11:00:04 +00001148 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001149 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001150
Allan Stephens0c3141e2008-04-15 00:22:02 -07001151restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001152 /* Look for a message in receive queue; wait if necessary */
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001153 res = tipc_wait_for_rcvmsg(sock, timeo);
1154 if (res)
1155 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001156
1157 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001158 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 msg = buf_msg(buf);
1160 sz = msg_data_sz(msg);
1161 err = msg_errcode(msg);
1162
1163 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001165 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166 goto restart;
1167 }
1168
1169 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170 if (sz_copied == 0) {
1171 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001172 res = anc_data_recv(m, msg, tport);
1173 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 goto exit;
1175 }
1176
1177 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001179 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180
Allan Stephens0232fd02011-02-21 09:45:40 -05001181 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 needed = (buf_len - sz_copied);
1183 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001184
1185 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1186 m->msg_iov, sz_to_copy);
1187 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001188 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001189
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190 sz_copied += sz_to_copy;
1191
1192 if (sz_to_copy < sz) {
1193 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001194 TIPC_SKB_CB(buf)->handle =
1195 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 goto exit;
1197 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 } else {
1199 if (sz_copied != 0)
1200 goto exit; /* can't add error msg to valid data */
1201
1202 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1203 res = 0;
1204 else
1205 res = -ECONNRESET;
1206 }
1207
1208 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001209 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001210 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1211 tipc_acknowledge(tport->ref, tport->conn_unacked);
1212 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001213 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214
1215 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001216 if ((sz_copied < buf_len) && /* didn't get all requested data */
1217 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001218 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001219 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1220 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001221 goto restart;
1222
1223exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001224 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001225 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226}
1227
1228/**
Ying Xuef288bef2012-08-21 11:16:57 +08001229 * tipc_write_space - wake up thread if port congestion is released
1230 * @sk: socket
1231 */
1232static void tipc_write_space(struct sock *sk)
1233{
1234 struct socket_wq *wq;
1235
1236 rcu_read_lock();
1237 wq = rcu_dereference(sk->sk_wq);
1238 if (wq_has_sleeper(wq))
1239 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1240 POLLWRNORM | POLLWRBAND);
1241 rcu_read_unlock();
1242}
1243
1244/**
1245 * tipc_data_ready - wake up threads to indicate messages have been received
1246 * @sk: socket
1247 * @len: the length of messages
1248 */
1249static void tipc_data_ready(struct sock *sk, int len)
1250{
1251 struct socket_wq *wq;
1252
1253 rcu_read_lock();
1254 wq = rcu_dereference(sk->sk_wq);
1255 if (wq_has_sleeper(wq))
1256 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1257 POLLRDNORM | POLLRDBAND);
1258 rcu_read_unlock();
1259}
1260
1261/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001262 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001263 * @port: TIPC port
Ying Xue7e6c1312012-11-29 18:39:14 -05001264 * @msg: message
1265 *
1266 * Returns TIPC error status code and socket error status code
1267 * once it encounters some errors
1268 */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001269static u32 filter_connect(struct tipc_port *port, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001270{
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001271 struct sock *sk = tipc_port_to_sk(port);
1272 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001273 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001274
Ying Xue7e6c1312012-11-29 18:39:14 -05001275 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001276 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001277
1278 if (msg_mcast(msg))
1279 return retval;
1280
1281 switch ((int)sock->state) {
1282 case SS_CONNECTED:
1283 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001284 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001285 if (unlikely(msg_errcode(msg))) {
1286 sock->state = SS_DISCONNECTING;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001287 __tipc_port_disconnect(port);
Ying Xue7e6c1312012-11-29 18:39:14 -05001288 }
1289 retval = TIPC_OK;
1290 }
1291 break;
1292 case SS_CONNECTING:
1293 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001294 if (unlikely(msg_errcode(msg))) {
1295 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001296 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001297 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001298 break;
1299 }
1300
1301 if (unlikely(!msg_connected(msg)))
1302 break;
1303
1304 res = auto_connect(sock, msg);
1305 if (res) {
1306 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001307 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001308 retval = TIPC_OK;
1309 break;
1310 }
1311
1312 /* If an incoming message is an 'ACK-', it should be
1313 * discarded here because it doesn't contain useful
1314 * data. In addition, we should try to wake up
1315 * connect() routine if sleeping.
1316 */
1317 if (msg_data_sz(msg) == 0) {
1318 kfree_skb(*buf);
1319 *buf = NULL;
1320 if (waitqueue_active(sk_sleep(sk)))
1321 wake_up_interruptible(sk_sleep(sk));
1322 }
1323 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001324 break;
1325 case SS_LISTENING:
1326 case SS_UNCONNECTED:
1327 /* Accept only SYN message */
1328 if (!msg_connected(msg) && !(msg_errcode(msg)))
1329 retval = TIPC_OK;
1330 break;
1331 case SS_DISCONNECTING:
1332 break;
1333 default:
1334 pr_err("Unknown socket state %u\n", sock->state);
1335 }
1336 return retval;
1337}
1338
1339/**
Ying Xueaba79f32013-01-20 23:30:09 +01001340 * rcvbuf_limit - get proper overload limit of socket receive queue
1341 * @sk: socket
1342 * @buf: message
1343 *
1344 * For all connection oriented messages, irrespective of importance,
1345 * the default overload value (i.e. 67MB) is set as limit.
1346 *
1347 * For all connectionless messages, by default new queue limits are
1348 * as belows:
1349 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001350 * TIPC_LOW_IMPORTANCE (4 MB)
1351 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1352 * TIPC_HIGH_IMPORTANCE (16 MB)
1353 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001354 *
1355 * Returns overload limit according to corresponding message importance
1356 */
1357static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1358{
1359 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001360
1361 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001362 return sysctl_tipc_rmem[2];
1363
1364 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1365 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001366}
1367
1368/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001369 * filter_rcv - validate incoming message
1370 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001372 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001373 * Enqueues message on receive queue if acceptable; optionally handles
1374 * disconnect indication for a connected socket.
1375 *
1376 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001377 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1379 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001380static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001382 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001384 unsigned int limit = rcvbuf_limit(sk, buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001385 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001388 if (msg_type(msg) > TIPC_DIRECT_MSG)
1389 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001390
Per Lidenb97bf3f2006-01-02 19:04:38 +01001391 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001392 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001393 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394 } else {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001395 res = filter_connect(tipc_sk_port(sk), &buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001396 if (res != TIPC_OK || buf == NULL)
1397 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398 }
1399
1400 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001401 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1402 return TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403
Ying Xueaba79f32013-01-20 23:30:09 +01001404 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001405 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001406 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001407 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001408
Ying Xuef288bef2012-08-21 11:16:57 +08001409 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001410 return TIPC_OK;
1411}
1412
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001413/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001414 * backlog_rcv - handle incoming message from backlog queue
1415 * @sk: socket
1416 * @buf: message
1417 *
1418 * Caller must hold socket lock, but not port lock.
1419 *
1420 * Returns 0
1421 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001422static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1423{
1424 u32 res;
1425
1426 res = filter_rcv(sk, buf);
1427 if (res)
1428 tipc_reject_msg(buf, res);
1429 return 0;
1430}
1431
1432/**
1433 * dispatch - handle incoming message
1434 * @tport: TIPC port that received message
1435 * @buf: message
1436 *
1437 * Called with port lock already taken.
1438 *
1439 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1440 */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001441static u32 dispatch(struct tipc_port *port, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001442{
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001443 struct sock *sk = tipc_port_to_sk(port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001444 u32 res;
1445
1446 /*
1447 * Process message if socket is unlocked; otherwise add to backlog queue
1448 *
1449 * This code is based on sk_receive_skb(), but must be distinct from it
1450 * since a TIPC-specific filter/reject mechanism is utilized
1451 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001452 bh_lock_sock(sk);
1453 if (!sock_owned_by_user(sk)) {
1454 res = filter_rcv(sk, buf);
1455 } else {
Ying Xueaba79f32013-01-20 23:30:09 +01001456 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
Zhu Yi53eecb12010-03-04 18:01:45 +00001457 res = TIPC_ERR_OVERLOAD;
1458 else
1459 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001460 }
1461 bh_unlock_sock(sk);
1462
1463 return res;
1464}
1465
1466/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467 * wakeupdispatch - wake up port after congestion
1468 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001469 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001470 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001471 */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001472static void wakeupdispatch(struct tipc_port *port)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473{
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001474 struct sock *sk = tipc_port_to_sk(port);
Ying Xuef288bef2012-08-21 11:16:57 +08001475 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001476}
1477
Ying Xue78eb3a52014-01-17 09:50:03 +08001478static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1479{
1480 struct sock *sk = sock->sk;
1481 DEFINE_WAIT(wait);
1482 int done;
1483
1484 do {
1485 int err = sock_error(sk);
1486 if (err)
1487 return err;
1488 if (!*timeo_p)
1489 return -ETIMEDOUT;
1490 if (signal_pending(current))
1491 return sock_intr_errno(*timeo_p);
1492
1493 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1494 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1495 finish_wait(sk_sleep(sk), &wait);
1496 } while (!done);
1497 return 0;
1498}
1499
Per Lidenb97bf3f2006-01-02 19:04:38 +01001500/**
Ying Xue247f0f32014-02-18 16:06:46 +08001501 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001502 * @sock: socket structure
1503 * @dest: socket address for destination port
1504 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001505 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001506 *
1507 * Returns 0 on success, errno otherwise
1508 */
Ying Xue247f0f32014-02-18 16:06:46 +08001509static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1510 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001512 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001513 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1514 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001515 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1516 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001517 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001518
Allan Stephens0c3141e2008-04-15 00:22:02 -07001519 lock_sock(sk);
1520
Allan Stephensb89741a2008-04-15 00:20:37 -07001521 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001522 if (sock->state == SS_READY) {
1523 res = -EOPNOTSUPP;
1524 goto exit;
1525 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001526
Allan Stephensb89741a2008-04-15 00:20:37 -07001527 /*
1528 * Reject connection attempt using multicast address
1529 *
1530 * Note: send_msg() validates the rest of the address fields,
1531 * so there's no need to do it here
1532 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001533 if (dst->addrtype == TIPC_ADDR_MCAST) {
1534 res = -EINVAL;
1535 goto exit;
1536 }
1537
Ying Xue78eb3a52014-01-17 09:50:03 +08001538 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001539 switch (sock->state) {
1540 case SS_UNCONNECTED:
1541 /* Send a 'SYN-' to destination */
1542 m.msg_name = dest;
1543 m.msg_namelen = destlen;
1544
1545 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1546 * indicate send_msg() is never blocked.
1547 */
1548 if (!timeout)
1549 m.msg_flags = MSG_DONTWAIT;
1550
Ying Xue247f0f32014-02-18 16:06:46 +08001551 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001552 if ((res < 0) && (res != -EWOULDBLOCK))
1553 goto exit;
1554
1555 /* Just entered SS_CONNECTING state; the only
1556 * difference is that return value in non-blocking
1557 * case is EINPROGRESS, rather than EALREADY.
1558 */
1559 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001560 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001561 if (previous == SS_CONNECTING)
1562 res = -EALREADY;
1563 if (!timeout)
1564 goto exit;
1565 timeout = msecs_to_jiffies(timeout);
1566 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1567 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001568 break;
1569 case SS_CONNECTED:
1570 res = -EISCONN;
1571 break;
1572 default:
1573 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001574 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001575 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001576exit:
1577 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001578 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001579}
1580
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001581/**
Ying Xue247f0f32014-02-18 16:06:46 +08001582 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001583 * @sock: socket structure
1584 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001585 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001586 * Returns 0 on success, errno otherwise
1587 */
Ying Xue247f0f32014-02-18 16:06:46 +08001588static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001589{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001590 struct sock *sk = sock->sk;
1591 int res;
1592
1593 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001594
Ying Xue245f3d32011-07-06 06:01:13 -04001595 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001596 res = -EINVAL;
1597 else {
1598 sock->state = SS_LISTENING;
1599 res = 0;
1600 }
1601
1602 release_sock(sk);
1603 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001604}
1605
Ying Xue6398e232014-01-17 09:50:04 +08001606static int tipc_wait_for_accept(struct socket *sock, long timeo)
1607{
1608 struct sock *sk = sock->sk;
1609 DEFINE_WAIT(wait);
1610 int err;
1611
1612 /* True wake-one mechanism for incoming connections: only
1613 * one process gets woken up, not the 'whole herd'.
1614 * Since we do not 'race & poll' for established sockets
1615 * anymore, the common case will execute the loop only once.
1616 */
1617 for (;;) {
1618 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1619 TASK_INTERRUPTIBLE);
1620 if (skb_queue_empty(&sk->sk_receive_queue)) {
1621 release_sock(sk);
1622 timeo = schedule_timeout(timeo);
1623 lock_sock(sk);
1624 }
1625 err = 0;
1626 if (!skb_queue_empty(&sk->sk_receive_queue))
1627 break;
1628 err = -EINVAL;
1629 if (sock->state != SS_LISTENING)
1630 break;
1631 err = sock_intr_errno(timeo);
1632 if (signal_pending(current))
1633 break;
1634 err = -EAGAIN;
1635 if (!timeo)
1636 break;
1637 }
1638 finish_wait(sk_sleep(sk), &wait);
1639 return err;
1640}
1641
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001642/**
Ying Xue247f0f32014-02-18 16:06:46 +08001643 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001644 * @sock: listening socket
1645 * @newsock: new socket that is to be connected
1646 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001647 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001648 * Returns 0 on success, errno otherwise
1649 */
Ying Xue247f0f32014-02-18 16:06:46 +08001650static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001651{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001652 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001653 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001654 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001655 struct tipc_msg *msg;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001656 struct tipc_portid peer;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001657 u32 new_ref;
Ying Xue6398e232014-01-17 09:50:04 +08001658 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001659 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001660
Allan Stephens0c3141e2008-04-15 00:22:02 -07001661 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001662
Allan Stephens0c3141e2008-04-15 00:22:02 -07001663 if (sock->state != SS_LISTENING) {
1664 res = -EINVAL;
1665 goto exit;
1666 }
Ying Xue6398e232014-01-17 09:50:04 +08001667 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1668 res = tipc_wait_for_accept(sock, timeo);
1669 if (res)
1670 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001671
1672 buf = skb_peek(&sk->sk_receive_queue);
1673
Ying Xuec5fa7b32013-06-17 10:54:39 -04001674 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001675 if (res)
1676 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001677
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001678 new_sk = new_sock->sk;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001679 new_port = tipc_sk_port(new_sk);
1680 new_ref = new_port->ref;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001681 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001682
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001683 /* we lock on new_sk; but lockdep sees the lock on sk */
1684 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001685
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001686 /*
1687 * Reject any stray messages received by new socket
1688 * before the socket lock was taken (very, very unlikely)
1689 */
1690 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001691
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001692 /* Connect new socket to it's peer */
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001693 peer.ref = msg_origport(msg);
1694 peer.node = msg_orignode(msg);
1695 tipc_port_connect(new_ref, &peer);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001696 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001697
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001698 tipc_set_portimportance(new_ref, msg_importance(msg));
1699 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001700 new_port->conn_type = msg_nametype(msg);
1701 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001703
1704 /*
1705 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1706 * Respond to 'SYN+' by queuing it on new socket.
1707 */
1708 if (!msg_data_sz(msg)) {
1709 struct msghdr m = {NULL,};
1710
1711 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001712 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001713 } else {
1714 __skb_dequeue(&sk->sk_receive_queue);
1715 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001716 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001717 }
1718 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001720 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001721 return res;
1722}
1723
1724/**
Ying Xue247f0f32014-02-18 16:06:46 +08001725 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001727 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001728 *
1729 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001730 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001731 * Returns 0 on success, errno otherwise
1732 */
Ying Xue247f0f32014-02-18 16:06:46 +08001733static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001734{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001735 struct sock *sk = sock->sk;
1736 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001737 struct sk_buff *buf;
1738 int res;
1739
Allan Stephense247a8f2008-03-06 15:05:38 -08001740 if (how != SHUT_RDWR)
1741 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001742
Allan Stephens0c3141e2008-04-15 00:22:02 -07001743 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001744
1745 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001746 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001747 case SS_CONNECTED:
1748
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001750 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001751 buf = __skb_dequeue(&sk->sk_receive_queue);
1752 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001753 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001754 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001755 goto restart;
1756 }
Ying Xue247f0f32014-02-18 16:06:46 +08001757 tipc_port_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001758 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001759 } else {
Ying Xue247f0f32014-02-18 16:06:46 +08001760 tipc_port_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001761 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001762
1763 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001764
1765 /* fall through */
1766
1767 case SS_DISCONNECTING:
1768
Ying Xue75031152012-10-29 09:38:15 -04001769 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001770 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001771
1772 /* Wake up anyone sleeping in poll */
1773 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774 res = 0;
1775 break;
1776
1777 default:
1778 res = -ENOTCONN;
1779 }
1780
Allan Stephens0c3141e2008-04-15 00:22:02 -07001781 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001782 return res;
1783}
1784
1785/**
Ying Xue247f0f32014-02-18 16:06:46 +08001786 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787 * @sock: socket structure
1788 * @lvl: option level
1789 * @opt: option identifier
1790 * @ov: pointer to new option value
1791 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001792 *
1793 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001794 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001795 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001796 * Returns 0 on success, errno otherwise
1797 */
Ying Xue247f0f32014-02-18 16:06:46 +08001798static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1799 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001801 struct sock *sk = sock->sk;
1802 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001803 u32 value;
1804 int res;
1805
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001806 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1807 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001808 if (lvl != SOL_TIPC)
1809 return -ENOPROTOOPT;
1810 if (ol < sizeof(value))
1811 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001812 res = get_user(value, (u32 __user *)ov);
1813 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814 return res;
1815
Allan Stephens0c3141e2008-04-15 00:22:02 -07001816 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001817
Per Lidenb97bf3f2006-01-02 19:04:38 +01001818 switch (opt) {
1819 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001820 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001821 break;
1822 case TIPC_SRC_DROPPABLE:
1823 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001824 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001825 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826 res = -ENOPROTOOPT;
1827 break;
1828 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001829 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001830 break;
1831 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001832 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001833 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834 break;
1835 default:
1836 res = -EINVAL;
1837 }
1838
Allan Stephens0c3141e2008-04-15 00:22:02 -07001839 release_sock(sk);
1840
Per Lidenb97bf3f2006-01-02 19:04:38 +01001841 return res;
1842}
1843
1844/**
Ying Xue247f0f32014-02-18 16:06:46 +08001845 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001846 * @sock: socket structure
1847 * @lvl: option level
1848 * @opt: option identifier
1849 * @ov: receptacle for option value
1850 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001851 *
1852 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001853 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001854 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855 * Returns 0 on success, errno otherwise
1856 */
Ying Xue247f0f32014-02-18 16:06:46 +08001857static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1858 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001859{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001860 struct sock *sk = sock->sk;
1861 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001862 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001863 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001864 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001865
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001866 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1867 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001868 if (lvl != SOL_TIPC)
1869 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001870 res = get_user(len, ol);
1871 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001872 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001873
Allan Stephens0c3141e2008-04-15 00:22:02 -07001874 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001875
1876 switch (opt) {
1877 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001878 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001879 break;
1880 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001881 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001882 break;
1883 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001884 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001885 break;
1886 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001887 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001888 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001889 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001890 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001891 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001892 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001893 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001894 value = skb_queue_len(&sk->sk_receive_queue);
1895 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001896 default:
1897 res = -EINVAL;
1898 }
1899
Allan Stephens0c3141e2008-04-15 00:22:02 -07001900 release_sock(sk);
1901
Paul Gortmaker25860c32010-12-31 18:59:31 +00001902 if (res)
1903 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001904
Paul Gortmaker25860c32010-12-31 18:59:31 +00001905 if (len < sizeof(value))
1906 return -EINVAL;
1907
1908 if (copy_to_user(ov, &value, sizeof(value)))
1909 return -EFAULT;
1910
1911 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001912}
1913
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001914/* Protocol switches for the various types of TIPC sockets */
1915
Florian Westphalbca65ea2008-02-07 18:18:01 -08001916static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001917 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001918 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001919 .release = tipc_release,
1920 .bind = tipc_bind,
1921 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001922 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001923 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08001924 .getname = tipc_getname,
1925 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001926 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001927 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08001928 .shutdown = tipc_shutdown,
1929 .setsockopt = tipc_setsockopt,
1930 .getsockopt = tipc_getsockopt,
1931 .sendmsg = tipc_sendmsg,
1932 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001933 .mmap = sock_no_mmap,
1934 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001935};
1936
Florian Westphalbca65ea2008-02-07 18:18:01 -08001937static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001938 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001939 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001940 .release = tipc_release,
1941 .bind = tipc_bind,
1942 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001943 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08001944 .accept = tipc_accept,
1945 .getname = tipc_getname,
1946 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001947 .ioctl = sock_no_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08001948 .listen = tipc_listen,
1949 .shutdown = tipc_shutdown,
1950 .setsockopt = tipc_setsockopt,
1951 .getsockopt = tipc_getsockopt,
1952 .sendmsg = tipc_send_packet,
1953 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001954 .mmap = sock_no_mmap,
1955 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001956};
1957
Florian Westphalbca65ea2008-02-07 18:18:01 -08001958static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001959 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001960 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001961 .release = tipc_release,
1962 .bind = tipc_bind,
1963 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001964 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08001965 .accept = tipc_accept,
1966 .getname = tipc_getname,
1967 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001968 .ioctl = sock_no_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08001969 .listen = tipc_listen,
1970 .shutdown = tipc_shutdown,
1971 .setsockopt = tipc_setsockopt,
1972 .getsockopt = tipc_getsockopt,
1973 .sendmsg = tipc_send_stream,
1974 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001975 .mmap = sock_no_mmap,
1976 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001977};
1978
Florian Westphalbca65ea2008-02-07 18:18:01 -08001979static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001980 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001981 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04001982 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01001983};
1984
1985static struct proto tipc_proto = {
1986 .name = "TIPC",
1987 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04001988 .obj_size = sizeof(struct tipc_sock),
1989 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01001990};
1991
Ying Xuec5fa7b32013-06-17 10:54:39 -04001992static struct proto tipc_proto_kern = {
1993 .name = "TIPC",
1994 .obj_size = sizeof(struct tipc_sock),
1995 .sysctl_rmem = sysctl_tipc_rmem
1996};
1997
Per Lidenb97bf3f2006-01-02 19:04:38 +01001998/**
Per Liden4323add2006-01-18 00:38:21 +01001999 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002000 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002001 * Returns 0 on success, errno otherwise
2002 */
Per Liden4323add2006-01-18 00:38:21 +01002003int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002004{
2005 int res;
2006
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002007 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002008 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002009 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002010 goto out;
2011 }
2012
2013 res = sock_register(&tipc_family_ops);
2014 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002015 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002016 proto_unregister(&tipc_proto);
2017 goto out;
2018 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002019 out:
2020 return res;
2021}
2022
2023/**
Per Liden4323add2006-01-18 00:38:21 +01002024 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002025 */
Per Liden4323add2006-01-18 00:38:21 +01002026void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002027{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002028 sock_unregister(tipc_family_ops.family);
2029 proto_unregister(&tipc_proto);
2030}