blob: 9cea92ee6c820d57e35cd015e8074032f37bfb69 [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
Allan Stephens0c3141e2008-04-15 00:22:02 -070047static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Ying Xuef288bef2012-08-21 11:16:57 +080048static void tipc_data_ready(struct sock *sk, int len);
49static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080050static int tipc_release(struct socket *sock);
51static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Per Lidenb97bf3f2006-01-02 19:04:38 +010052
Florian Westphalbca65ea2008-02-07 18:18:01 -080053static const struct proto_ops packet_ops;
54static const struct proto_ops stream_ops;
55static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010056
57static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040058static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010059
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090060/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070061 * Revised TIPC socket locking policy:
62 *
63 * Most socket operations take the standard socket lock when they start
64 * and hold it until they finish (or until they need to sleep). Acquiring
65 * this lock grants the owner exclusive access to the fields of the socket
66 * data structures, with the exception of the backlog queue. A few socket
67 * operations can be done without taking the socket lock because they only
68 * read socket information that never changes during the life of the socket.
69 *
70 * Socket operations may acquire the lock for the associated TIPC port if they
71 * need to perform an operation on the port. If any routine needs to acquire
72 * both the socket lock and the port lock it must take the socket lock first
73 * to avoid the risk of deadlock.
74 *
75 * The dispatcher handling incoming messages cannot grab the socket lock in
76 * the standard fashion, since invoked it runs at the BH level and cannot block.
77 * Instead, it checks to see if the socket lock is currently owned by someone,
78 * and either handles the message itself or adds it to the socket's backlog
79 * queue; in the latter case the queued message is processed once the process
80 * owning the socket lock releases it.
81 *
82 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
83 * the problem of a blocked socket operation preventing any other operations
84 * from occurring. However, applications must be careful if they have
85 * multiple threads trying to send (or receive) on the same socket, as these
86 * operations might interfere with each other. For example, doing a connect
87 * and a receive at the same time might allow the receive to consume the
88 * ACK message meant for the connect. While additional work could be done
89 * to try and overcome this, it doesn't seem to be worthwhile at the present.
90 *
91 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
92 * that another operation that must be performed in a non-blocking manner is
93 * not delayed for very long because the lock has already been taken.
94 *
95 * NOTE: This code assumes that certain fields of a port/socket pair are
96 * constant over its lifetime; such fields can be examined without taking
97 * the socket lock and/or port lock, and do not need to be re-read even
98 * after resuming processing after waiting. These fields include:
99 * - socket type
100 * - pointer to socket sk structure (aka tipc_sock structure)
101 * - pointer to port structure
102 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100103 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400105#include "socket.h"
106
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700108 * advance_rx_queue - discard first buffer in socket receive queue
109 *
110 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100111 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700112static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400114 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100115}
116
117/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700118 * reject_rx_queue - reject all buffers in socket receive queue
119 *
120 * Caller must hold socket lock
121 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700122static void reject_rx_queue(struct sock *sk)
123{
124 struct sk_buff *buf;
125
Ying Xue9da3d472012-11-27 06:15:27 -0500126 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700127 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700128}
129
130/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400131 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700132 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100133 * @sock: pre-allocated socket structure
134 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800135 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900136 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700137 * This routine creates additional data structures used by the TIPC socket,
138 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139 *
140 * Returns 0 on success, errno otherwise
141 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400142static int tipc_sk_create(struct net *net, struct socket *sock,
143 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700145 const struct proto_ops *ops;
146 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100147 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400148 struct tipc_sock *tsk;
149 struct tipc_port *port;
150 u32 ref;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700151
152 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 if (unlikely(protocol != 0))
154 return -EPROTONOSUPPORT;
155
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156 switch (sock->type) {
157 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700158 ops = &stream_ops;
159 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 break;
161 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700162 ops = &packet_ops;
163 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 break;
165 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700167 ops = &msg_ops;
168 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 break;
Allan Stephens49978652006-06-25 23:47:18 -0700170 default:
Allan Stephens49978652006-06-25 23:47:18 -0700171 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172 }
173
Allan Stephens0c3141e2008-04-15 00:22:02 -0700174 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400175 if (!kern)
176 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
177 else
178 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
179
Allan Stephens0c3141e2008-04-15 00:22:02 -0700180 if (sk == NULL)
181 return -ENOMEM;
182
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400183 tsk = tipc_sk(sk);
184 port = &tsk->port;
185
186 ref = tipc_port_init(port, TIPC_LOW_IMPORTANCE);
187 if (!ref) {
188 pr_warn("Socket registration failed, ref. table exhausted\n");
Allan Stephens0c3141e2008-04-15 00:22:02 -0700189 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100190 return -ENOMEM;
191 }
192
Allan Stephens0c3141e2008-04-15 00:22:02 -0700193 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700194 sock->ops = ops;
195 sock->state = state;
196
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700198 sk->sk_backlog_rcv = backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400199 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800200 sk->sk_data_ready = tipc_data_ready;
201 sk->sk_write_space = tipc_write_space;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400202 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400203 tipc_port_unlock(port);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700204
Allan Stephens0c3141e2008-04-15 00:22:02 -0700205 if (sock->state == SS_READY) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400206 tipc_port_set_unreturnable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700207 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400208 tipc_port_set_unreliable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700209 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210 return 0;
211}
212
213/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400214 * tipc_sock_create_local - create TIPC socket from inside TIPC module
215 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
216 *
217 * We cannot use sock_creat_kern here because it bumps module user count.
218 * Since socket owner and creator is the same module we must make sure
219 * that module count remains zero for module local sockets, otherwise
220 * we cannot do rmmod.
221 *
222 * Returns 0 on success, errno otherwise
223 */
224int tipc_sock_create_local(int type, struct socket **res)
225{
226 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400227
228 rc = sock_create_lite(AF_TIPC, type, 0, res);
229 if (rc < 0) {
230 pr_err("Failed to create kernel socket\n");
231 return rc;
232 }
233 tipc_sk_create(&init_net, *res, 0, 1);
234
Ying Xuec5fa7b32013-06-17 10:54:39 -0400235 return 0;
236}
237
238/**
239 * tipc_sock_release_local - release socket created by tipc_sock_create_local
240 * @sock: the socket to be released.
241 *
242 * Module reference count is not incremented when such sockets are created,
243 * so we must keep it from being decremented when they are released.
244 */
245void tipc_sock_release_local(struct socket *sock)
246{
Ying Xue247f0f32014-02-18 16:06:46 +0800247 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400248 sock->ops = NULL;
249 sock_release(sock);
250}
251
252/**
253 * tipc_sock_accept_local - accept a connection on a socket created
254 * with tipc_sock_create_local. Use this function to avoid that
255 * module reference count is inadvertently incremented.
256 *
257 * @sock: the accepting socket
258 * @newsock: reference to the new socket to be created
259 * @flags: socket flags
260 */
261
262int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400263 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400264{
265 struct sock *sk = sock->sk;
266 int ret;
267
268 ret = sock_create_lite(sk->sk_family, sk->sk_type,
269 sk->sk_protocol, newsock);
270 if (ret < 0)
271 return ret;
272
Ying Xue247f0f32014-02-18 16:06:46 +0800273 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400274 if (ret < 0) {
275 sock_release(*newsock);
276 return ret;
277 }
278 (*newsock)->ops = sock->ops;
279 return ret;
280}
281
282/**
Ying Xue247f0f32014-02-18 16:06:46 +0800283 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284 * @sock: socket to destroy
285 *
286 * This routine cleans up any messages that are still queued on the socket.
287 * For DGRAM and RDM socket types, all queued messages are rejected.
288 * For SEQPACKET and STREAM socket types, the first message is rejected
289 * and any others are discarded. (If the first message on a STREAM socket
290 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900291 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100292 * NOTE: Rejected messages are not necessarily returned to the sender! They
293 * are returned or discarded according to the "destination droppable" setting
294 * specified for the message by the sender.
295 *
296 * Returns 0 on success, errno otherwise
297 */
Ying Xue247f0f32014-02-18 16:06:46 +0800298static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400301 struct tipc_sock *tsk;
302 struct tipc_port *port;
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
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400313 tsk = tipc_sk(sk);
314 port = &tsk->port;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700315 lock_sock(sk);
316
317 /*
318 * Reject all unreceived messages, except on an active connection
319 * (which disconnects locally & sends a 'FIN+' to peer)
320 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700322 buf = __skb_dequeue(&sk->sk_receive_queue);
323 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 break;
Ying Xue40682432013-10-18 07:23:16 +0200325 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400326 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700327 else {
328 if ((sock->state == SS_CONNECTING) ||
329 (sock->state == SS_CONNECTED)) {
330 sock->state = SS_DISCONNECTING;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400331 tipc_port_disconnect(port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700332 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700334 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335 }
336
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400337 /* Destroy TIPC port; also disconnects an active connection and
338 * sends a 'FIN-' to peer.
Allan Stephens0c3141e2008-04-15 00:22:02 -0700339 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400340 tipc_port_destroy(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341
Allan Stephens0c3141e2008-04-15 00:22:02 -0700342 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100343 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344
Allan Stephens0c3141e2008-04-15 00:22:02 -0700345 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700346 sock->state = SS_DISCONNECTING;
347 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348
349 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700350 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351
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;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400375 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800376 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377
Ying Xue84602762013-12-27 10:18:28 +0800378 lock_sock(sk);
379 if (unlikely(!uaddr_len)) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400380 res = tipc_withdraw(&tsk->port, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800381 goto exit;
382 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900383
Ying Xue84602762013-12-27 10:18:28 +0800384 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
385 res = -EINVAL;
386 goto exit;
387 }
388 if (addr->family != AF_TIPC) {
389 res = -EAFNOSUPPORT;
390 goto exit;
391 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 if (addr->addrtype == TIPC_ADDR_NAME)
394 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800395 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
396 res = -EAFNOSUPPORT;
397 goto exit;
398 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900399
Ying Xue13a2e892013-06-17 10:54:40 -0400400 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400401 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800402 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
403 res = -EACCES;
404 goto exit;
405 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400406
Ying Xue84602762013-12-27 10:18:28 +0800407 res = (addr->scope > 0) ?
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400408 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
409 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800410exit:
411 release_sock(sk);
412 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413}
414
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900415/**
Ying Xue247f0f32014-02-18 16:06:46 +0800416 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 * @sock: socket structure
418 * @uaddr: area for returned socket address
419 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700420 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900421 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700423 *
Allan Stephens2da59912008-07-14 22:43:32 -0700424 * NOTE: This routine doesn't need to take the socket lock since it only
425 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000426 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 */
Ying Xue247f0f32014-02-18 16:06:46 +0800428static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
429 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100431 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400432 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000434 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700435 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700436 if ((sock->state != SS_CONNECTED) &&
437 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
438 return -ENOTCONN;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400439 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
440 addr->addr.id.node = tipc_port_peernode(&tsk->port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700441 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400442 addr->addr.id.ref = tsk->port.ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000443 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700444 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445
446 *uaddr_len = sizeof(*addr);
447 addr->addrtype = TIPC_ADDR_ID;
448 addr->family = AF_TIPC;
449 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 addr->addr.name.domain = 0;
451
Allan Stephens0c3141e2008-04-15 00:22:02 -0700452 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100453}
454
455/**
Ying Xue247f0f32014-02-18 16:06:46 +0800456 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 * @file: file structure associated with the socket
458 * @sock: socket for which to calculate the poll bits
459 * @wait: ???
460 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700461 * Returns pollmask value
462 *
463 * COMMENTARY:
464 * It appears that the usual socket locking mechanisms are not useful here
465 * since the pollmask info is potentially out-of-date the moment this routine
466 * exits. TCP and other protocols seem to rely on higher level poll routines
467 * to handle any preventable race conditions, so TIPC will do the same ...
468 *
469 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700470 *
Allan Stephensf662c072010-08-17 11:00:06 +0000471 * socket state flags set
472 * ------------ ---------
473 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200474 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000475 *
476 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
477 * no write flags
478 *
479 * connected POLLIN/POLLRDNORM if data in rx queue
480 * POLLOUT if port is not congested
481 *
482 * disconnecting POLLIN/POLLRDNORM/POLLHUP
483 * no write flags
484 *
485 * listening POLLIN if SYN in rx queue
486 * no write flags
487 *
488 * ready POLLIN/POLLRDNORM if data in rx queue
489 * [connectionless] POLLOUT (since port cannot be congested)
490 *
491 * IMPORTANT: The fact that a read or write operation is indicated does NOT
492 * imply that the operation will succeed, merely that it should be performed
493 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494 */
Ying Xue247f0f32014-02-18 16:06:46 +0800495static unsigned int tipc_poll(struct file *file, struct socket *sock,
496 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497{
Allan Stephens9b674e82008-03-26 16:48:21 -0700498 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400499 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000500 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700501
Ying Xuef288bef2012-08-21 11:16:57 +0800502 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700503
Allan Stephensf662c072010-08-17 11:00:06 +0000504 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200505 case SS_UNCONNECTED:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400506 if (!tsk->port.congested)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200507 mask |= POLLOUT;
508 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000509 case SS_READY:
510 case SS_CONNECTED:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400511 if (!tsk->port.congested)
Allan Stephensf662c072010-08-17 11:00:06 +0000512 mask |= POLLOUT;
513 /* fall thru' */
514 case SS_CONNECTING:
515 case SS_LISTENING:
516 if (!skb_queue_empty(&sk->sk_receive_queue))
517 mask |= (POLLIN | POLLRDNORM);
518 break;
519 case SS_DISCONNECTING:
520 mask = (POLLIN | POLLRDNORM | POLLHUP);
521 break;
522 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700523
524 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525}
526
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900527/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528 * dest_name_check - verify user is permitted to send to specified port name
529 * @dest: destination address
530 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900531 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100532 * Prevents restricted configuration commands from being issued by
533 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900534 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 * Returns 0 if permission is granted, otherwise errno
536 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800537static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538{
539 struct tipc_cfg_msg_hdr hdr;
540
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900541 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
542 return 0;
543 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
544 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900545 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
546 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547
Allan Stephens3f8dd942011-01-18 13:09:29 -0500548 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
549 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900550 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700552 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900554
Per Lidenb97bf3f2006-01-02 19:04:38 +0100555 return 0;
556}
557
Ying Xue3f405042014-01-17 09:50:05 +0800558static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
559{
560 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400561 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800562 DEFINE_WAIT(wait);
563 int done;
564
565 do {
566 int err = sock_error(sk);
567 if (err)
568 return err;
569 if (sock->state == SS_DISCONNECTING)
570 return -EPIPE;
571 if (!*timeo_p)
572 return -EAGAIN;
573 if (signal_pending(current))
574 return sock_intr_errno(*timeo_p);
575
576 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400577 done = sk_wait_event(sk, timeo_p, !tsk->port.congested);
Ying Xue3f405042014-01-17 09:50:05 +0800578 finish_wait(sk_sleep(sk), &wait);
579 } while (!done);
580 return 0;
581}
582
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400583
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584/**
Ying Xue247f0f32014-02-18 16:06:46 +0800585 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700586 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587 * @sock: socket structure
588 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700589 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900590 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900592 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
594 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900595 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596 * Returns the number of bytes sent on success, or errno otherwise
597 */
Ying Xue247f0f32014-02-18 16:06:46 +0800598static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
599 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700601 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400602 struct tipc_sock *tsk = tipc_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100603 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604 int needs_conn;
Ying Xue3f405042014-01-17 09:50:05 +0800605 long timeo;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400606 u32 ref = tsk->port.ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 int res = -EINVAL;
608
609 if (unlikely(!dest))
610 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700611 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
612 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 return -EINVAL;
Ying Xue97f8b872013-01-31 21:51:47 +0100614 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400615 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616
Allan Stephens0c3141e2008-04-15 00:22:02 -0700617 if (iocb)
618 lock_sock(sk);
619
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 needs_conn = (sock->state != SS_READY);
621 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700622 if (sock->state == SS_LISTENING) {
623 res = -EPIPE;
624 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700625 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700626 if (sock->state != SS_UNCONNECTED) {
627 res = -EISCONN;
628 goto exit;
629 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400630 if (tsk->port.published) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700631 res = -EOPNOTSUPP;
632 goto exit;
633 }
634 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400635 tsk->port.conn_type = dest->addr.name.name.type;
636 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700637 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638
639 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700640 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 }
642
Ying Xue3f405042014-01-17 09:50:05 +0800643 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900644 do {
645 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000646 res = dest_name_check(dest, m);
647 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700648 break;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400649 res = tipc_send2name(ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900650 &dest->addr.name.name,
651 dest->addr.name.domain,
Allan Stephens26896902011-04-21 10:42:07 -0500652 m->msg_iov,
653 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000654 } else if (dest->addrtype == TIPC_ADDR_ID) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400655 res = tipc_send2port(ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900656 &dest->addr.id,
Allan Stephens26896902011-04-21 10:42:07 -0500657 m->msg_iov,
658 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000659 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100660 if (needs_conn) {
661 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700662 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663 }
Allan Stephens2db99832010-12-31 18:59:33 +0000664 res = dest_name_check(dest, m);
665 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700666 break;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400667 res = tipc_port_mcast_xmit(ref,
Ying Xue247f0f32014-02-18 16:06:46 +0800668 &dest->addr.nameseq,
669 m->msg_iov,
670 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900671 }
672 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000673 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700674 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700675 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900676 }
Ying Xue3f405042014-01-17 09:50:05 +0800677 res = tipc_wait_for_sndmsg(sock, &timeo);
678 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700679 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900680 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700681
682exit:
683 if (iocb)
684 release_sock(sk);
685 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686}
687
Ying Xue391a6dd2014-01-17 09:50:06 +0800688static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
689{
690 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400691 struct tipc_sock *tsk = tipc_sk(sk);
692 struct tipc_port *port = &tsk->port;
Ying Xue391a6dd2014-01-17 09:50:06 +0800693 DEFINE_WAIT(wait);
694 int done;
695
696 do {
697 int err = sock_error(sk);
698 if (err)
699 return err;
700 if (sock->state == SS_DISCONNECTING)
701 return -EPIPE;
702 else if (sock->state != SS_CONNECTED)
703 return -ENOTCONN;
704 if (!*timeo_p)
705 return -EAGAIN;
706 if (signal_pending(current))
707 return sock_intr_errno(*timeo_p);
708
709 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
710 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400711 (!port->congested || !port->connected));
Ying Xue391a6dd2014-01-17 09:50:06 +0800712 finish_wait(sk_sleep(sk), &wait);
713 } while (!done);
714 return 0;
715}
716
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900717/**
Ying Xue247f0f32014-02-18 16:06:46 +0800718 * tipc_send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700719 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100720 * @sock: socket structure
721 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700722 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900723 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900725 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726 * Returns the number of bytes sent on success, or errno otherwise
727 */
Ying Xue247f0f32014-02-18 16:06:46 +0800728static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
729 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700731 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400732 struct tipc_sock *tsk = tipc_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100733 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Ying Xue391a6dd2014-01-17 09:50:06 +0800734 int res = -EINVAL;
735 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736
737 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100738 if (unlikely(dest))
Ying Xue247f0f32014-02-18 16:06:46 +0800739 return tipc_sendmsg(iocb, sock, m, total_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100740
Ying Xue97f8b872013-01-31 21:51:47 +0100741 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400742 return -EMSGSIZE;
743
Allan Stephens0c3141e2008-04-15 00:22:02 -0700744 if (iocb)
745 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100746
Ying Xue391a6dd2014-01-17 09:50:06 +0800747 if (unlikely(sock->state != SS_CONNECTED)) {
748 if (sock->state == SS_DISCONNECTING)
749 res = -EPIPE;
750 else
751 res = -ENOTCONN;
752 goto exit;
753 }
Ying Xue1d835872011-07-06 05:53:15 -0400754
Ying Xue391a6dd2014-01-17 09:50:06 +0800755 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900756 do {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400757 res = tipc_send(tsk->port.ref, m->msg_iov, total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000758 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700759 break;
Ying Xue391a6dd2014-01-17 09:50:06 +0800760 res = tipc_wait_for_sndpkt(sock, &timeo);
761 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700762 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900763 } while (1);
Ying Xue391a6dd2014-01-17 09:50:06 +0800764exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700765 if (iocb)
766 release_sock(sk);
767 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100768}
769
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900770/**
Ying Xue247f0f32014-02-18 16:06:46 +0800771 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100772 * @iocb: (unused)
773 * @sock: socket structure
774 * @m: data to send
775 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900776 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100777 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900778 *
779 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700780 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100781 */
Ying Xue247f0f32014-02-18 16:06:46 +0800782static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
783 struct msghdr *m, size_t total_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100784{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700785 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400786 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100787 struct msghdr my_msg;
788 struct iovec my_iov;
789 struct iovec *curr_iov;
790 int curr_iovlen;
791 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700792 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793 int curr_left;
794 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700795 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900797
Allan Stephens0c3141e2008-04-15 00:22:02 -0700798 lock_sock(sk);
799
Allan Stephens05646c92007-06-10 17:25:24 -0700800 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900801 if (unlikely(sock->state != SS_CONNECTED)) {
wangweidong3b8401f2013-12-12 09:36:40 +0800802 if (sock->state == SS_UNCONNECTED)
Ying Xue247f0f32014-02-18 16:06:46 +0800803 res = tipc_send_packet(NULL, sock, m, total_len);
wangweidongb0555972013-12-27 10:09:39 +0800804 else
805 res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800806 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900807 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808
Allan Stephens0c3141e2008-04-15 00:22:02 -0700809 if (unlikely(m->msg_name)) {
810 res = -EISCONN;
811 goto exit;
812 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700813
Ying Xue97f8b872013-01-31 21:51:47 +0100814 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400815 res = -EMSGSIZE;
816 goto exit;
817 }
818
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900819 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100820 * Send each iovec entry using one or more messages
821 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900822 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100823 * (i.e. one large iovec entry), but could be improved to pass sets
824 * of small iovec entries into send_packet().
825 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700826 curr_iov = m->msg_iov;
827 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828 my_msg.msg_iov = &my_iov;
829 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700830 my_msg.msg_flags = m->msg_flags;
831 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700832 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100833
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400834 hdr_size = msg_hdr_sz(&tsk->port.phdr);
Allan Stephens05646c92007-06-10 17:25:24 -0700835
Per Lidenb97bf3f2006-01-02 19:04:38 +0100836 while (curr_iovlen--) {
837 curr_start = curr_iov->iov_base;
838 curr_left = curr_iov->iov_len;
839
840 while (curr_left) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400841 bytes_to_send = tsk->port.max_pkt - hdr_size;
Allan Stephens05646c92007-06-10 17:25:24 -0700842 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
843 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
844 if (curr_left < bytes_to_send)
845 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846 my_iov.iov_base = curr_start;
847 my_iov.iov_len = bytes_to_send;
Ying Xue247f0f32014-02-18 16:06:46 +0800848 res = tipc_send_packet(NULL, sock, &my_msg,
849 bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000850 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700851 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700852 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700853 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700854 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855 curr_left -= bytes_to_send;
856 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700857 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100858 }
859
860 curr_iov++;
861 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700862 res = bytes_sent;
863exit:
864 release_sock(sk);
865 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866}
867
868/**
869 * auto_connect - complete connection setup to a remote port
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400870 * @tsk: tipc socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900872 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873 * Returns 0 on success, errno otherwise
874 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400875static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100876{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400877 struct tipc_port *port = &tsk->port;
878 struct socket *sock = tsk->sk.sk_socket;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400879 struct tipc_portid peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400881 peer.ref = msg_origport(msg);
882 peer.node = msg_orignode(msg);
883
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400884 port = tipc_port_deref(port->ref);
885 if (!port)
Ying Xue584d24b2012-11-29 18:51:19 -0500886 return -EINVAL;
887
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400888 __tipc_port_connect(port->ref, port, &peer);
Ying Xue584d24b2012-11-29 18:51:19 -0500889
890 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
891 return -EINVAL;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400892 msg_set_importance(&port->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893 sock->state = SS_CONNECTED;
894 return 0;
895}
896
897/**
898 * set_orig_addr - capture sender's address for received message
899 * @m: descriptor for message info
900 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900901 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902 * Note: Address is not captured if not requested by receiver.
903 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800904static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905{
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100906 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900908 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100909 addr->family = AF_TIPC;
910 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000911 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912 addr->addr.id.ref = msg_origport(msg);
913 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000914 addr->addr.name.domain = 0; /* could leave uninitialized */
915 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916 m->msg_namelen = sizeof(struct sockaddr_tipc);
917 }
918}
919
920/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900921 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922 * @m: descriptor for message info
923 * @msg: received message header
924 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900925 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900927 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100928 * Returns 0 if successful, otherwise errno
929 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800930static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400931 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100932{
933 u32 anc_data[3];
934 u32 err;
935 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700936 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100937 int res;
938
939 if (likely(m->msg_controllen == 0))
940 return 0;
941
942 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100943 err = msg ? msg_errcode(msg) : 0;
944 if (unlikely(err)) {
945 anc_data[0] = err;
946 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000947 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
948 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100949 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000950 if (anc_data[1]) {
951 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
952 msg_data(msg));
953 if (res)
954 return res;
955 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100956 }
957
958 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
960 switch (dest_type) {
961 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700962 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100963 anc_data[0] = msg_nametype(msg);
964 anc_data[1] = msg_namelower(msg);
965 anc_data[2] = msg_namelower(msg);
966 break;
967 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700968 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969 anc_data[0] = msg_nametype(msg);
970 anc_data[1] = msg_namelower(msg);
971 anc_data[2] = msg_nameupper(msg);
972 break;
973 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700974 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100975 anc_data[0] = tport->conn_type;
976 anc_data[1] = tport->conn_instance;
977 anc_data[2] = tport->conn_instance;
978 break;
979 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700980 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100981 }
Allan Stephens2db99832010-12-31 18:59:33 +0000982 if (has_name) {
983 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
984 if (res)
985 return res;
986 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100987
988 return 0;
989}
990
Ying Xue9bbb4ec2014-01-17 09:50:07 +0800991static int tipc_wait_for_rcvmsg(struct socket *sock, long timeo)
992{
993 struct sock *sk = sock->sk;
994 DEFINE_WAIT(wait);
995 int err;
996
997 for (;;) {
998 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
999 if (skb_queue_empty(&sk->sk_receive_queue)) {
1000 if (sock->state == SS_DISCONNECTING) {
1001 err = -ENOTCONN;
1002 break;
1003 }
1004 release_sock(sk);
1005 timeo = schedule_timeout(timeo);
1006 lock_sock(sk);
1007 }
1008 err = 0;
1009 if (!skb_queue_empty(&sk->sk_receive_queue))
1010 break;
1011 err = sock_intr_errno(timeo);
1012 if (signal_pending(current))
1013 break;
1014 err = -EAGAIN;
1015 if (!timeo)
1016 break;
1017 }
1018 finish_wait(sk_sleep(sk), &wait);
1019 return err;
1020}
1021
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001022/**
Ying Xue247f0f32014-02-18 16:06:46 +08001023 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 * @iocb: (unused)
1025 * @m: descriptor for message info
1026 * @buf_len: total size of user buffer area
1027 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001028 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1030 * If the complete message doesn't fit in user area, truncate it.
1031 *
1032 * Returns size of returned message data, errno otherwise
1033 */
Ying Xue247f0f32014-02-18 16:06:46 +08001034static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1035 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001037 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001038 struct tipc_sock *tsk = tipc_sk(sk);
1039 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 struct sk_buff *buf;
1041 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001042 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043 unsigned int sz;
1044 u32 err;
1045 int res;
1046
Allan Stephens0c3141e2008-04-15 00:22:02 -07001047 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 if (unlikely(!buf_len))
1049 return -EINVAL;
1050
Allan Stephens0c3141e2008-04-15 00:22:02 -07001051 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052
Allan Stephens0c3141e2008-04-15 00:22:02 -07001053 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054 res = -ENOTCONN;
1055 goto exit;
1056 }
1057
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001058 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001059restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060
Allan Stephens0c3141e2008-04-15 00:22:02 -07001061 /* Look for a message in receive queue; wait if necessary */
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001062 res = tipc_wait_for_rcvmsg(sock, timeo);
1063 if (res)
1064 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001065
1066 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001067 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068 msg = buf_msg(buf);
1069 sz = msg_data_sz(msg);
1070 err = msg_errcode(msg);
1071
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001074 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001075 goto restart;
1076 }
1077
1078 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001079 set_orig_addr(m, msg);
1080
1081 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001082 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001083 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084 goto exit;
1085
1086 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001087 if (!err) {
1088 if (unlikely(buf_len < sz)) {
1089 sz = buf_len;
1090 m->msg_flags |= MSG_TRUNC;
1091 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001092 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1093 m->msg_iov, sz);
1094 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001095 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 res = sz;
1097 } else {
1098 if ((sock->state == SS_READY) ||
1099 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1100 res = 0;
1101 else
1102 res = -ECONNRESET;
1103 }
1104
1105 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001106 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001107 if ((sock->state != SS_READY) &&
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001108 (++port->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1109 tipc_acknowledge(port->ref, port->conn_unacked);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001110 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001111 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001112exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001113 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114 return res;
1115}
1116
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001117/**
Ying Xue247f0f32014-02-18 16:06:46 +08001118 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001119 * @iocb: (unused)
1120 * @m: descriptor for message info
1121 * @buf_len: total size of user buffer area
1122 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001123 *
1124 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001125 * will optionally wait for more; never truncates data.
1126 *
1127 * Returns size of returned message data, errno otherwise
1128 */
Ying Xue247f0f32014-02-18 16:06:46 +08001129static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1130 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001131{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001132 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001133 struct tipc_sock *tsk = tipc_sk(sk);
1134 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135 struct sk_buff *buf;
1136 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001137 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001139 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001142 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143
1144 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 if (unlikely(!buf_len))
1146 return -EINVAL;
1147
Allan Stephens0c3141e2008-04-15 00:22:02 -07001148 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001149
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001150 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001151 res = -ENOTCONN;
1152 goto exit;
1153 }
1154
Florian Westphal3720d402010-08-17 11:00:04 +00001155 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001156 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001157
Allan Stephens0c3141e2008-04-15 00:22:02 -07001158restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001159 /* Look for a message in receive queue; wait if necessary */
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001160 res = tipc_wait_for_rcvmsg(sock, timeo);
1161 if (res)
1162 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001163
1164 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001165 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166 msg = buf_msg(buf);
1167 sz = msg_data_sz(msg);
1168 err = msg_errcode(msg);
1169
1170 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001172 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 goto restart;
1174 }
1175
1176 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177 if (sz_copied == 0) {
1178 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001179 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001180 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001181 goto exit;
1182 }
1183
1184 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001186 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001187
Allan Stephens0232fd02011-02-21 09:45:40 -05001188 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 needed = (buf_len - sz_copied);
1190 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001191
1192 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1193 m->msg_iov, sz_to_copy);
1194 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001196
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197 sz_copied += sz_to_copy;
1198
1199 if (sz_to_copy < sz) {
1200 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001201 TIPC_SKB_CB(buf)->handle =
1202 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 goto exit;
1204 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205 } else {
1206 if (sz_copied != 0)
1207 goto exit; /* can't add error msg to valid data */
1208
1209 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1210 res = 0;
1211 else
1212 res = -ECONNRESET;
1213 }
1214
1215 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001217 if (unlikely(++port->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1218 tipc_acknowledge(port->ref, port->conn_unacked);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001219 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001220 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001221
1222 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001223 if ((sz_copied < buf_len) && /* didn't get all requested data */
1224 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001225 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001226 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1227 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 goto restart;
1229
1230exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001231 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001232 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233}
1234
1235/**
Ying Xuef288bef2012-08-21 11:16:57 +08001236 * tipc_write_space - wake up thread if port congestion is released
1237 * @sk: socket
1238 */
1239static void tipc_write_space(struct sock *sk)
1240{
1241 struct socket_wq *wq;
1242
1243 rcu_read_lock();
1244 wq = rcu_dereference(sk->sk_wq);
1245 if (wq_has_sleeper(wq))
1246 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1247 POLLWRNORM | POLLWRBAND);
1248 rcu_read_unlock();
1249}
1250
1251/**
1252 * tipc_data_ready - wake up threads to indicate messages have been received
1253 * @sk: socket
1254 * @len: the length of messages
1255 */
1256static void tipc_data_ready(struct sock *sk, int len)
1257{
1258 struct socket_wq *wq;
1259
1260 rcu_read_lock();
1261 wq = rcu_dereference(sk->sk_wq);
1262 if (wq_has_sleeper(wq))
1263 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1264 POLLRDNORM | POLLRDBAND);
1265 rcu_read_unlock();
1266}
1267
1268/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001269 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001270 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001271 * @msg: message
1272 *
1273 * Returns TIPC error status code and socket error status code
1274 * once it encounters some errors
1275 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001276static u32 filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001277{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001278 struct sock *sk = &tsk->sk;
1279 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001280 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001281 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001282
Ying Xue7e6c1312012-11-29 18:39:14 -05001283 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001284 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001285
1286 if (msg_mcast(msg))
1287 return retval;
1288
1289 switch ((int)sock->state) {
1290 case SS_CONNECTED:
1291 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001292 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001293 if (unlikely(msg_errcode(msg))) {
1294 sock->state = SS_DISCONNECTING;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001295 __tipc_port_disconnect(port);
Ying Xue7e6c1312012-11-29 18:39:14 -05001296 }
1297 retval = TIPC_OK;
1298 }
1299 break;
1300 case SS_CONNECTING:
1301 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001302 if (unlikely(msg_errcode(msg))) {
1303 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001304 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001305 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001306 break;
1307 }
1308
1309 if (unlikely(!msg_connected(msg)))
1310 break;
1311
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001312 res = auto_connect(tsk, msg);
Ying Xue584d24b2012-11-29 18:51:19 -05001313 if (res) {
1314 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001315 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001316 retval = TIPC_OK;
1317 break;
1318 }
1319
1320 /* If an incoming message is an 'ACK-', it should be
1321 * discarded here because it doesn't contain useful
1322 * data. In addition, we should try to wake up
1323 * connect() routine if sleeping.
1324 */
1325 if (msg_data_sz(msg) == 0) {
1326 kfree_skb(*buf);
1327 *buf = NULL;
1328 if (waitqueue_active(sk_sleep(sk)))
1329 wake_up_interruptible(sk_sleep(sk));
1330 }
1331 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001332 break;
1333 case SS_LISTENING:
1334 case SS_UNCONNECTED:
1335 /* Accept only SYN message */
1336 if (!msg_connected(msg) && !(msg_errcode(msg)))
1337 retval = TIPC_OK;
1338 break;
1339 case SS_DISCONNECTING:
1340 break;
1341 default:
1342 pr_err("Unknown socket state %u\n", sock->state);
1343 }
1344 return retval;
1345}
1346
1347/**
Ying Xueaba79f32013-01-20 23:30:09 +01001348 * rcvbuf_limit - get proper overload limit of socket receive queue
1349 * @sk: socket
1350 * @buf: message
1351 *
1352 * For all connection oriented messages, irrespective of importance,
1353 * the default overload value (i.e. 67MB) is set as limit.
1354 *
1355 * For all connectionless messages, by default new queue limits are
1356 * as belows:
1357 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001358 * TIPC_LOW_IMPORTANCE (4 MB)
1359 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1360 * TIPC_HIGH_IMPORTANCE (16 MB)
1361 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001362 *
1363 * Returns overload limit according to corresponding message importance
1364 */
1365static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1366{
1367 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001368
1369 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001370 return sysctl_tipc_rmem[2];
1371
1372 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1373 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001374}
1375
1376/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001377 * filter_rcv - validate incoming message
1378 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001379 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001380 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001381 * Enqueues message on receive queue if acceptable; optionally handles
1382 * disconnect indication for a connected socket.
1383 *
1384 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001385 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001386 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1387 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001388static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001389{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001390 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001391 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001393 unsigned int limit = rcvbuf_limit(sk, buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001394 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001395
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001397 if (msg_type(msg) > TIPC_DIRECT_MSG)
1398 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001399
Per Lidenb97bf3f2006-01-02 19:04:38 +01001400 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001401 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001402 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001404 res = filter_connect(tsk, &buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001405 if (res != TIPC_OK || buf == NULL)
1406 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001407 }
1408
1409 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001410 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1411 return TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001412
Ying Xueaba79f32013-01-20 23:30:09 +01001413 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001414 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001415 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001416 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001417
Ying Xuef288bef2012-08-21 11:16:57 +08001418 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001419 return TIPC_OK;
1420}
1421
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001422/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001423 * backlog_rcv - handle incoming message from backlog queue
1424 * @sk: socket
1425 * @buf: message
1426 *
1427 * Caller must hold socket lock, but not port lock.
1428 *
1429 * Returns 0
1430 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001431static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1432{
1433 u32 res;
1434
1435 res = filter_rcv(sk, buf);
1436 if (res)
1437 tipc_reject_msg(buf, res);
1438 return 0;
1439}
1440
1441/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001442 * tipc_sk_rcv - handle incoming message
1443 * @sk: socket receiving message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001444 * @buf: message
1445 *
1446 * Called with port lock already taken.
1447 *
1448 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1449 */
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001450u32 tipc_sk_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001451{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001452 u32 res;
1453
1454 /*
1455 * Process message if socket is unlocked; otherwise add to backlog queue
1456 *
1457 * This code is based on sk_receive_skb(), but must be distinct from it
1458 * since a TIPC-specific filter/reject mechanism is utilized
1459 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001460 bh_lock_sock(sk);
1461 if (!sock_owned_by_user(sk)) {
1462 res = filter_rcv(sk, buf);
1463 } else {
Ying Xueaba79f32013-01-20 23:30:09 +01001464 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
Zhu Yi53eecb12010-03-04 18:01:45 +00001465 res = TIPC_ERR_OVERLOAD;
1466 else
1467 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001468 }
1469 bh_unlock_sock(sk);
1470
1471 return res;
1472}
1473
Ying Xue78eb3a52014-01-17 09:50:03 +08001474static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1475{
1476 struct sock *sk = sock->sk;
1477 DEFINE_WAIT(wait);
1478 int done;
1479
1480 do {
1481 int err = sock_error(sk);
1482 if (err)
1483 return err;
1484 if (!*timeo_p)
1485 return -ETIMEDOUT;
1486 if (signal_pending(current))
1487 return sock_intr_errno(*timeo_p);
1488
1489 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1490 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1491 finish_wait(sk_sleep(sk), &wait);
1492 } while (!done);
1493 return 0;
1494}
1495
Per Lidenb97bf3f2006-01-02 19:04:38 +01001496/**
Ying Xue247f0f32014-02-18 16:06:46 +08001497 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001498 * @sock: socket structure
1499 * @dest: socket address for destination port
1500 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001501 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001502 *
1503 * Returns 0 on success, errno otherwise
1504 */
Ying Xue247f0f32014-02-18 16:06:46 +08001505static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1506 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001507{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001508 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001509 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1510 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001511 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1512 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001513 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001514
Allan Stephens0c3141e2008-04-15 00:22:02 -07001515 lock_sock(sk);
1516
Allan Stephensb89741a2008-04-15 00:20:37 -07001517 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001518 if (sock->state == SS_READY) {
1519 res = -EOPNOTSUPP;
1520 goto exit;
1521 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001522
Allan Stephensb89741a2008-04-15 00:20:37 -07001523 /*
1524 * Reject connection attempt using multicast address
1525 *
1526 * Note: send_msg() validates the rest of the address fields,
1527 * so there's no need to do it here
1528 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001529 if (dst->addrtype == TIPC_ADDR_MCAST) {
1530 res = -EINVAL;
1531 goto exit;
1532 }
1533
Ying Xue78eb3a52014-01-17 09:50:03 +08001534 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001535 switch (sock->state) {
1536 case SS_UNCONNECTED:
1537 /* Send a 'SYN-' to destination */
1538 m.msg_name = dest;
1539 m.msg_namelen = destlen;
1540
1541 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1542 * indicate send_msg() is never blocked.
1543 */
1544 if (!timeout)
1545 m.msg_flags = MSG_DONTWAIT;
1546
Ying Xue247f0f32014-02-18 16:06:46 +08001547 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001548 if ((res < 0) && (res != -EWOULDBLOCK))
1549 goto exit;
1550
1551 /* Just entered SS_CONNECTING state; the only
1552 * difference is that return value in non-blocking
1553 * case is EINPROGRESS, rather than EALREADY.
1554 */
1555 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001556 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001557 if (previous == SS_CONNECTING)
1558 res = -EALREADY;
1559 if (!timeout)
1560 goto exit;
1561 timeout = msecs_to_jiffies(timeout);
1562 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1563 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001564 break;
1565 case SS_CONNECTED:
1566 res = -EISCONN;
1567 break;
1568 default:
1569 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001570 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001571 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001572exit:
1573 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001574 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001575}
1576
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001577/**
Ying Xue247f0f32014-02-18 16:06:46 +08001578 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001579 * @sock: socket structure
1580 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001581 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001582 * Returns 0 on success, errno otherwise
1583 */
Ying Xue247f0f32014-02-18 16:06:46 +08001584static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001585{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001586 struct sock *sk = sock->sk;
1587 int res;
1588
1589 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001590
Ying Xue245f3d32011-07-06 06:01:13 -04001591 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001592 res = -EINVAL;
1593 else {
1594 sock->state = SS_LISTENING;
1595 res = 0;
1596 }
1597
1598 release_sock(sk);
1599 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600}
1601
Ying Xue6398e232014-01-17 09:50:04 +08001602static int tipc_wait_for_accept(struct socket *sock, long timeo)
1603{
1604 struct sock *sk = sock->sk;
1605 DEFINE_WAIT(wait);
1606 int err;
1607
1608 /* True wake-one mechanism for incoming connections: only
1609 * one process gets woken up, not the 'whole herd'.
1610 * Since we do not 'race & poll' for established sockets
1611 * anymore, the common case will execute the loop only once.
1612 */
1613 for (;;) {
1614 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1615 TASK_INTERRUPTIBLE);
1616 if (skb_queue_empty(&sk->sk_receive_queue)) {
1617 release_sock(sk);
1618 timeo = schedule_timeout(timeo);
1619 lock_sock(sk);
1620 }
1621 err = 0;
1622 if (!skb_queue_empty(&sk->sk_receive_queue))
1623 break;
1624 err = -EINVAL;
1625 if (sock->state != SS_LISTENING)
1626 break;
1627 err = sock_intr_errno(timeo);
1628 if (signal_pending(current))
1629 break;
1630 err = -EAGAIN;
1631 if (!timeo)
1632 break;
1633 }
1634 finish_wait(sk_sleep(sk), &wait);
1635 return err;
1636}
1637
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001638/**
Ying Xue247f0f32014-02-18 16:06:46 +08001639 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001640 * @sock: listening socket
1641 * @newsock: new socket that is to be connected
1642 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001643 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001644 * Returns 0 on success, errno otherwise
1645 */
Ying Xue247f0f32014-02-18 16:06:46 +08001646static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001647{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001648 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001649 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001650 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001651 struct tipc_msg *msg;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001652 struct tipc_portid peer;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001653 u32 new_ref;
Ying Xue6398e232014-01-17 09:50:04 +08001654 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001655 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656
Allan Stephens0c3141e2008-04-15 00:22:02 -07001657 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001658
Allan Stephens0c3141e2008-04-15 00:22:02 -07001659 if (sock->state != SS_LISTENING) {
1660 res = -EINVAL;
1661 goto exit;
1662 }
Ying Xue6398e232014-01-17 09:50:04 +08001663 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1664 res = tipc_wait_for_accept(sock, timeo);
1665 if (res)
1666 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001667
1668 buf = skb_peek(&sk->sk_receive_queue);
1669
Ying Xuec5fa7b32013-06-17 10:54:39 -04001670 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001671 if (res)
1672 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001673
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001674 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001675 new_port = &tipc_sk(new_sk)->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001676 new_ref = new_port->ref;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001677 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001678
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001679 /* we lock on new_sk; but lockdep sees the lock on sk */
1680 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001681
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001682 /*
1683 * Reject any stray messages received by new socket
1684 * before the socket lock was taken (very, very unlikely)
1685 */
1686 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001687
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001688 /* Connect new socket to it's peer */
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001689 peer.ref = msg_origport(msg);
1690 peer.node = msg_orignode(msg);
1691 tipc_port_connect(new_ref, &peer);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001692 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001693
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001694 tipc_port_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001695 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001696 new_port->conn_type = msg_nametype(msg);
1697 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001698 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001699
1700 /*
1701 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1702 * Respond to 'SYN+' by queuing it on new socket.
1703 */
1704 if (!msg_data_sz(msg)) {
1705 struct msghdr m = {NULL,};
1706
1707 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001708 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001709 } else {
1710 __skb_dequeue(&sk->sk_receive_queue);
1711 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001712 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001713 }
1714 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001715exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001716 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001717 return res;
1718}
1719
1720/**
Ying Xue247f0f32014-02-18 16:06:46 +08001721 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001722 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001723 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001724 *
1725 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001726 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001727 * Returns 0 on success, errno otherwise
1728 */
Ying Xue247f0f32014-02-18 16:06:46 +08001729static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001730{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001731 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001732 struct tipc_sock *tsk = tipc_sk(sk);
1733 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001734 struct sk_buff *buf;
1735 int res;
1736
Allan Stephense247a8f2008-03-06 15:05:38 -08001737 if (how != SHUT_RDWR)
1738 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739
Allan Stephens0c3141e2008-04-15 00:22:02 -07001740 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741
1742 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001743 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001744 case SS_CONNECTED:
1745
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001747 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001748 buf = __skb_dequeue(&sk->sk_receive_queue);
1749 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001750 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001751 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001752 goto restart;
1753 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001754 tipc_port_disconnect(port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001755 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001756 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001757 tipc_port_shutdown(port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001758 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001759
1760 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001761
1762 /* fall through */
1763
1764 case SS_DISCONNECTING:
1765
Ying Xue75031152012-10-29 09:38:15 -04001766 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001767 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001768
1769 /* Wake up anyone sleeping in poll */
1770 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001771 res = 0;
1772 break;
1773
1774 default:
1775 res = -ENOTCONN;
1776 }
1777
Allan Stephens0c3141e2008-04-15 00:22:02 -07001778 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001779 return res;
1780}
1781
1782/**
Ying Xue247f0f32014-02-18 16:06:46 +08001783 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001784 * @sock: socket structure
1785 * @lvl: option level
1786 * @opt: option identifier
1787 * @ov: pointer to new option value
1788 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001789 *
1790 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001791 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001792 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793 * Returns 0 on success, errno otherwise
1794 */
Ying Xue247f0f32014-02-18 16:06:46 +08001795static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1796 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001797{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001798 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001799 struct tipc_sock *tsk = tipc_sk(sk);
1800 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001801 u32 value;
1802 int res;
1803
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001804 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1805 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001806 if (lvl != SOL_TIPC)
1807 return -ENOPROTOOPT;
1808 if (ol < sizeof(value))
1809 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001810 res = get_user(value, (u32 __user *)ov);
1811 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001812 return res;
1813
Allan Stephens0c3141e2008-04-15 00:22:02 -07001814 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001815
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816 switch (opt) {
1817 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001818 tipc_port_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001819 break;
1820 case TIPC_SRC_DROPPABLE:
1821 if (sock->type != SOCK_STREAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001822 tipc_port_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001823 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001824 res = -ENOPROTOOPT;
1825 break;
1826 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001827 tipc_port_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001828 break;
1829 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001830 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001831 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001832 break;
1833 default:
1834 res = -EINVAL;
1835 }
1836
Allan Stephens0c3141e2008-04-15 00:22:02 -07001837 release_sock(sk);
1838
Per Lidenb97bf3f2006-01-02 19:04:38 +01001839 return res;
1840}
1841
1842/**
Ying Xue247f0f32014-02-18 16:06:46 +08001843 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001844 * @sock: socket structure
1845 * @lvl: option level
1846 * @opt: option identifier
1847 * @ov: receptacle for option value
1848 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001849 *
1850 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001851 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001852 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001853 * Returns 0 on success, errno otherwise
1854 */
Ying Xue247f0f32014-02-18 16:06:46 +08001855static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1856 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001857{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001858 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001859 struct tipc_sock *tsk = tipc_sk(sk);
1860 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001861 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001862 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001863 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001864
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001865 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1866 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001867 if (lvl != SOL_TIPC)
1868 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001869 res = get_user(len, ol);
1870 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001871 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001872
Allan Stephens0c3141e2008-04-15 00:22:02 -07001873 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001874
1875 switch (opt) {
1876 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001877 value = tipc_port_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001878 break;
1879 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001880 value = tipc_port_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001881 break;
1882 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001883 value = tipc_port_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001884 break;
1885 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001886 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001887 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001888 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001889 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001890 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001891 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001892 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001893 value = skb_queue_len(&sk->sk_receive_queue);
1894 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001895 default:
1896 res = -EINVAL;
1897 }
1898
Allan Stephens0c3141e2008-04-15 00:22:02 -07001899 release_sock(sk);
1900
Paul Gortmaker25860c32010-12-31 18:59:31 +00001901 if (res)
1902 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001903
Paul Gortmaker25860c32010-12-31 18:59:31 +00001904 if (len < sizeof(value))
1905 return -EINVAL;
1906
1907 if (copy_to_user(ov, &value, sizeof(value)))
1908 return -EFAULT;
1909
1910 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001911}
1912
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001913/* Protocol switches for the various types of TIPC sockets */
1914
Florian Westphalbca65ea2008-02-07 18:18:01 -08001915static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001916 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001917 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001918 .release = tipc_release,
1919 .bind = tipc_bind,
1920 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001921 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001922 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08001923 .getname = tipc_getname,
1924 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001925 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001926 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08001927 .shutdown = tipc_shutdown,
1928 .setsockopt = tipc_setsockopt,
1929 .getsockopt = tipc_getsockopt,
1930 .sendmsg = tipc_sendmsg,
1931 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001932 .mmap = sock_no_mmap,
1933 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001934};
1935
Florian Westphalbca65ea2008-02-07 18:18:01 -08001936static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001937 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001938 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001939 .release = tipc_release,
1940 .bind = tipc_bind,
1941 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001942 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08001943 .accept = tipc_accept,
1944 .getname = tipc_getname,
1945 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001946 .ioctl = sock_no_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08001947 .listen = tipc_listen,
1948 .shutdown = tipc_shutdown,
1949 .setsockopt = tipc_setsockopt,
1950 .getsockopt = tipc_getsockopt,
1951 .sendmsg = tipc_send_packet,
1952 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001953 .mmap = sock_no_mmap,
1954 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001955};
1956
Florian Westphalbca65ea2008-02-07 18:18:01 -08001957static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001958 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001959 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08001960 .release = tipc_release,
1961 .bind = tipc_bind,
1962 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001963 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08001964 .accept = tipc_accept,
1965 .getname = tipc_getname,
1966 .poll = tipc_poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001967 .ioctl = sock_no_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08001968 .listen = tipc_listen,
1969 .shutdown = tipc_shutdown,
1970 .setsockopt = tipc_setsockopt,
1971 .getsockopt = tipc_getsockopt,
1972 .sendmsg = tipc_send_stream,
1973 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001974 .mmap = sock_no_mmap,
1975 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001976};
1977
Florian Westphalbca65ea2008-02-07 18:18:01 -08001978static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001979 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001980 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04001981 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01001982};
1983
1984static struct proto tipc_proto = {
1985 .name = "TIPC",
1986 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04001987 .obj_size = sizeof(struct tipc_sock),
1988 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01001989};
1990
Ying Xuec5fa7b32013-06-17 10:54:39 -04001991static struct proto tipc_proto_kern = {
1992 .name = "TIPC",
1993 .obj_size = sizeof(struct tipc_sock),
1994 .sysctl_rmem = sysctl_tipc_rmem
1995};
1996
Per Lidenb97bf3f2006-01-02 19:04:38 +01001997/**
Per Liden4323add2006-01-18 00:38:21 +01001998 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001999 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002000 * Returns 0 on success, errno otherwise
2001 */
Per Liden4323add2006-01-18 00:38:21 +01002002int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002003{
2004 int res;
2005
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002006 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002007 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002008 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002009 goto out;
2010 }
2011
2012 res = sock_register(&tipc_family_ops);
2013 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002014 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002015 proto_unregister(&tipc_proto);
2016 goto out;
2017 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002018 out:
2019 return res;
2020}
2021
2022/**
Per Liden4323add2006-01-18 00:38:21 +01002023 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002024 */
Per Liden4323add2006-01-18 00:38:21 +01002025void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002026{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002027 sock_unregister(tipc_family_ops.family);
2028 proto_unregister(&tipc_proto);
2029}