blob: 8c5600cfcc3ed679a0371bd0c9b4aedb55270183 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * 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"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050039#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020040#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050041#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040042#include <linux/export.h>
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -050043#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040044
Per Lidenb97bf3f2006-01-02 19:04:38 +010045#define SS_LISTENING -1 /* socket is listening */
46#define SS_READY -2 /* socket is connectionless */
47
Allan Stephens3654ea02008-04-13 21:35:11 -070048#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Jon Paul Maloyac0074e2014-06-25 20:41:41 -050049#define TIPC_FWD_MSG 1
Per Lidenb97bf3f2006-01-02 19:04:38 +010050
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -040051static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -040052static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +080053static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080054static int tipc_release(struct socket *sock);
55static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Per Lidenb97bf3f2006-01-02 19:04:38 +010056
Florian Westphalbca65ea2008-02-07 18:18:01 -080057static const struct proto_ops packet_ops;
58static const struct proto_ops stream_ops;
59static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010060
61static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040062static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010063
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090064/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070065 * Revised TIPC socket locking policy:
66 *
67 * Most socket operations take the standard socket lock when they start
68 * and hold it until they finish (or until they need to sleep). Acquiring
69 * this lock grants the owner exclusive access to the fields of the socket
70 * data structures, with the exception of the backlog queue. A few socket
71 * operations can be done without taking the socket lock because they only
72 * read socket information that never changes during the life of the socket.
73 *
74 * Socket operations may acquire the lock for the associated TIPC port if they
75 * need to perform an operation on the port. If any routine needs to acquire
76 * both the socket lock and the port lock it must take the socket lock first
77 * to avoid the risk of deadlock.
78 *
79 * The dispatcher handling incoming messages cannot grab the socket lock in
80 * the standard fashion, since invoked it runs at the BH level and cannot block.
81 * Instead, it checks to see if the socket lock is currently owned by someone,
82 * and either handles the message itself or adds it to the socket's backlog
83 * queue; in the latter case the queued message is processed once the process
84 * owning the socket lock releases it.
85 *
86 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
87 * the problem of a blocked socket operation preventing any other operations
88 * from occurring. However, applications must be careful if they have
89 * multiple threads trying to send (or receive) on the same socket, as these
90 * operations might interfere with each other. For example, doing a connect
91 * and a receive at the same time might allow the receive to consume the
92 * ACK message meant for the connect. While additional work could be done
93 * to try and overcome this, it doesn't seem to be worthwhile at the present.
94 *
95 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
96 * that another operation that must be performed in a non-blocking manner is
97 * not delayed for very long because the lock has already been taken.
98 *
99 * NOTE: This code assumes that certain fields of a port/socket pair are
100 * constant over its lifetime; such fields can be examined without taking
101 * the socket lock and/or port lock, and do not need to be re-read even
102 * after resuming processing after waiting. These fields include:
103 * - socket type
104 * - pointer to socket sk structure (aka tipc_sock structure)
105 * - pointer to port structure
106 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400109#include "socket.h"
110
Per Lidenb97bf3f2006-01-02 19:04:38 +0100111/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700112 * advance_rx_queue - discard first buffer in socket receive queue
113 *
114 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100115 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700116static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100117{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400118 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119}
120
121/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700122 * reject_rx_queue - reject all buffers in socket receive queue
123 *
124 * Caller must hold socket lock
125 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700126static void reject_rx_queue(struct sock *sk)
127{
128 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500129 u32 dnode;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700130
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500131 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
132 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
133 tipc_link_xmit2(buf, dnode, 0);
134 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700135}
136
137/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400138 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700139 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140 * @sock: pre-allocated socket structure
141 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800142 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900143 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700144 * This routine creates additional data structures used by the TIPC socket,
145 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146 *
147 * Returns 0 on success, errno otherwise
148 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400149static int tipc_sk_create(struct net *net, struct socket *sock,
150 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700152 const struct proto_ops *ops;
153 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400155 struct tipc_sock *tsk;
156 struct tipc_port *port;
157 u32 ref;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700158
159 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 if (unlikely(protocol != 0))
161 return -EPROTONOSUPPORT;
162
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163 switch (sock->type) {
164 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700165 ops = &stream_ops;
166 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 break;
168 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700169 ops = &packet_ops;
170 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171 break;
172 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700174 ops = &msg_ops;
175 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176 break;
Allan Stephens49978652006-06-25 23:47:18 -0700177 default:
Allan Stephens49978652006-06-25 23:47:18 -0700178 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179 }
180
Allan Stephens0c3141e2008-04-15 00:22:02 -0700181 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400182 if (!kern)
183 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
184 else
185 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
186
Allan Stephens0c3141e2008-04-15 00:22:02 -0700187 if (sk == NULL)
188 return -ENOMEM;
189
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400190 tsk = tipc_sk(sk);
191 port = &tsk->port;
192
193 ref = tipc_port_init(port, TIPC_LOW_IMPORTANCE);
194 if (!ref) {
195 pr_warn("Socket registration failed, ref. table exhausted\n");
Allan Stephens0c3141e2008-04-15 00:22:02 -0700196 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197 return -ENOMEM;
198 }
199
Allan Stephens0c3141e2008-04-15 00:22:02 -0700200 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700201 sock->ops = ops;
202 sock->state = state;
203
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204 sock_init_data(sock, sk);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400205 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400206 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800207 sk->sk_data_ready = tipc_data_ready;
208 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400209 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500210 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400211 atomic_set(&tsk->dupl_rcvcnt, 0);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400212 tipc_port_unlock(port);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700213
Allan Stephens0c3141e2008-04-15 00:22:02 -0700214 if (sock->state == SS_READY) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400215 tipc_port_set_unreturnable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700216 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400217 tipc_port_set_unreliable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700218 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219 return 0;
220}
221
222/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400223 * tipc_sock_create_local - create TIPC socket from inside TIPC module
224 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
225 *
226 * We cannot use sock_creat_kern here because it bumps module user count.
227 * Since socket owner and creator is the same module we must make sure
228 * that module count remains zero for module local sockets, otherwise
229 * we cannot do rmmod.
230 *
231 * Returns 0 on success, errno otherwise
232 */
233int tipc_sock_create_local(int type, struct socket **res)
234{
235 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400236
237 rc = sock_create_lite(AF_TIPC, type, 0, res);
238 if (rc < 0) {
239 pr_err("Failed to create kernel socket\n");
240 return rc;
241 }
242 tipc_sk_create(&init_net, *res, 0, 1);
243
Ying Xuec5fa7b32013-06-17 10:54:39 -0400244 return 0;
245}
246
247/**
248 * tipc_sock_release_local - release socket created by tipc_sock_create_local
249 * @sock: the socket to be released.
250 *
251 * Module reference count is not incremented when such sockets are created,
252 * so we must keep it from being decremented when they are released.
253 */
254void tipc_sock_release_local(struct socket *sock)
255{
Ying Xue247f0f32014-02-18 16:06:46 +0800256 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400257 sock->ops = NULL;
258 sock_release(sock);
259}
260
261/**
262 * tipc_sock_accept_local - accept a connection on a socket created
263 * with tipc_sock_create_local. Use this function to avoid that
264 * module reference count is inadvertently incremented.
265 *
266 * @sock: the accepting socket
267 * @newsock: reference to the new socket to be created
268 * @flags: socket flags
269 */
270
271int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400272 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400273{
274 struct sock *sk = sock->sk;
275 int ret;
276
277 ret = sock_create_lite(sk->sk_family, sk->sk_type,
278 sk->sk_protocol, newsock);
279 if (ret < 0)
280 return ret;
281
Ying Xue247f0f32014-02-18 16:06:46 +0800282 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400283 if (ret < 0) {
284 sock_release(*newsock);
285 return ret;
286 }
287 (*newsock)->ops = sock->ops;
288 return ret;
289}
290
291/**
Ying Xue247f0f32014-02-18 16:06:46 +0800292 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293 * @sock: socket to destroy
294 *
295 * This routine cleans up any messages that are still queued on the socket.
296 * For DGRAM and RDM socket types, all queued messages are rejected.
297 * For SEQPACKET and STREAM socket types, the first message is rejected
298 * and any others are discarded. (If the first message on a STREAM socket
299 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900300 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301 * NOTE: Rejected messages are not necessarily returned to the sender! They
302 * are returned or discarded according to the "destination droppable" setting
303 * specified for the message by the sender.
304 *
305 * Returns 0 on success, errno otherwise
306 */
Ying Xue247f0f32014-02-18 16:06:46 +0800307static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400310 struct tipc_sock *tsk;
311 struct tipc_port *port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500313 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314
Allan Stephens0c3141e2008-04-15 00:22:02 -0700315 /*
316 * Exit if socket isn't fully initialized (occurs when a failed accept()
317 * releases a pre-allocated child socket that was never used)
318 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700319 if (sk == NULL)
320 return 0;
321
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400322 tsk = tipc_sk(sk);
323 port = &tsk->port;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700324 lock_sock(sk);
325
326 /*
327 * Reject all unreceived messages, except on an active connection
328 * (which disconnects locally & sends a 'FIN+' to peer)
329 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700331 buf = __skb_dequeue(&sk->sk_receive_queue);
332 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 break;
Ying Xue40682432013-10-18 07:23:16 +0200334 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400335 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700336 else {
337 if ((sock->state == SS_CONNECTING) ||
338 (sock->state == SS_CONNECTED)) {
339 sock->state = SS_DISCONNECTING;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400340 tipc_port_disconnect(port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700341 }
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500342 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
343 tipc_link_xmit2(buf, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700344 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 }
346
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400347 /* Destroy TIPC port; also disconnects an active connection and
348 * sends a 'FIN-' to peer.
Allan Stephens0c3141e2008-04-15 00:22:02 -0700349 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400350 tipc_port_destroy(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351
Allan Stephens0c3141e2008-04-15 00:22:02 -0700352 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100353 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354
Allan Stephens0c3141e2008-04-15 00:22:02 -0700355 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700356 sock->state = SS_DISCONNECTING;
357 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358
359 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700360 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200362 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363}
364
365/**
Ying Xue247f0f32014-02-18 16:06:46 +0800366 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367 * @sock: socket structure
368 * @uaddr: socket address describing name(s) and desired operation
369 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900370 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100371 * Name and name sequence binding is indicated using a positive scope value;
372 * a negative scope value unbinds the specified name. Specifying no name
373 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900374 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700376 *
377 * NOTE: This routine doesn't need to take the socket lock since it doesn't
378 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 */
Ying Xue247f0f32014-02-18 16:06:46 +0800380static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
381 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382{
Ying Xue84602762013-12-27 10:18:28 +0800383 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400385 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800386 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387
Ying Xue84602762013-12-27 10:18:28 +0800388 lock_sock(sk);
389 if (unlikely(!uaddr_len)) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400390 res = tipc_withdraw(&tsk->port, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800391 goto exit;
392 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900393
Ying Xue84602762013-12-27 10:18:28 +0800394 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
395 res = -EINVAL;
396 goto exit;
397 }
398 if (addr->family != AF_TIPC) {
399 res = -EAFNOSUPPORT;
400 goto exit;
401 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 if (addr->addrtype == TIPC_ADDR_NAME)
404 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800405 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
406 res = -EAFNOSUPPORT;
407 goto exit;
408 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900409
Ying Xue13a2e892013-06-17 10:54:40 -0400410 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400411 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800412 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
413 res = -EACCES;
414 goto exit;
415 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400416
Ying Xue84602762013-12-27 10:18:28 +0800417 res = (addr->scope > 0) ?
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400418 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
419 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800420exit:
421 release_sock(sk);
422 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423}
424
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900425/**
Ying Xue247f0f32014-02-18 16:06:46 +0800426 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 * @sock: socket structure
428 * @uaddr: area for returned socket address
429 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700430 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900431 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700433 *
Allan Stephens2da59912008-07-14 22:43:32 -0700434 * NOTE: This routine doesn't need to take the socket lock since it only
435 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000436 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437 */
Ying Xue247f0f32014-02-18 16:06:46 +0800438static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
439 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400442 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000444 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700445 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700446 if ((sock->state != SS_CONNECTED) &&
447 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
448 return -ENOTCONN;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400449 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
450 addr->addr.id.node = tipc_port_peernode(&tsk->port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700451 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400452 addr->addr.id.ref = tsk->port.ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000453 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700454 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455
456 *uaddr_len = sizeof(*addr);
457 addr->addrtype = TIPC_ADDR_ID;
458 addr->family = AF_TIPC;
459 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 addr->addr.name.domain = 0;
461
Allan Stephens0c3141e2008-04-15 00:22:02 -0700462 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463}
464
465/**
Ying Xue247f0f32014-02-18 16:06:46 +0800466 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467 * @file: file structure associated with the socket
468 * @sock: socket for which to calculate the poll bits
469 * @wait: ???
470 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700471 * Returns pollmask value
472 *
473 * COMMENTARY:
474 * It appears that the usual socket locking mechanisms are not useful here
475 * since the pollmask info is potentially out-of-date the moment this routine
476 * exits. TCP and other protocols seem to rely on higher level poll routines
477 * to handle any preventable race conditions, so TIPC will do the same ...
478 *
479 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700480 *
Allan Stephensf662c072010-08-17 11:00:06 +0000481 * socket state flags set
482 * ------------ ---------
483 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200484 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000485 *
486 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
487 * no write flags
488 *
489 * connected POLLIN/POLLRDNORM if data in rx queue
490 * POLLOUT if port is not congested
491 *
492 * disconnecting POLLIN/POLLRDNORM/POLLHUP
493 * no write flags
494 *
495 * listening POLLIN if SYN in rx queue
496 * no write flags
497 *
498 * ready POLLIN/POLLRDNORM if data in rx queue
499 * [connectionless] POLLOUT (since port cannot be congested)
500 *
501 * IMPORTANT: The fact that a read or write operation is indicated does NOT
502 * imply that the operation will succeed, merely that it should be performed
503 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 */
Ying Xue247f0f32014-02-18 16:06:46 +0800505static unsigned int tipc_poll(struct file *file, struct socket *sock,
506 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507{
Allan Stephens9b674e82008-03-26 16:48:21 -0700508 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400509 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000510 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700511
Ying Xuef288bef2012-08-21 11:16:57 +0800512 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700513
Allan Stephensf662c072010-08-17 11:00:06 +0000514 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200515 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500516 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200517 mask |= POLLOUT;
518 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000519 case SS_READY:
520 case SS_CONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500521 if (!tsk->link_cong && !tipc_sk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000522 mask |= POLLOUT;
523 /* fall thru' */
524 case SS_CONNECTING:
525 case SS_LISTENING:
526 if (!skb_queue_empty(&sk->sk_receive_queue))
527 mask |= (POLLIN | POLLRDNORM);
528 break;
529 case SS_DISCONNECTING:
530 mask = (POLLIN | POLLRDNORM | POLLHUP);
531 break;
532 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700533
534 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535}
536
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900537/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500538 * tipc_sk_proto_rcv - receive a connection mng protocol message
539 * @tsk: receiving socket
540 * @dnode: node to send response message to, if any
541 * @buf: buffer containing protocol message
542 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
543 * (CONN_PROBE_REPLY) message should be forwarded.
544 */
545int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode, struct sk_buff *buf)
546{
547 struct tipc_msg *msg = buf_msg(buf);
548 struct tipc_port *port = &tsk->port;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500549 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500550
551 /* Ignore if connection cannot be validated: */
552 if (!port->connected || !tipc_port_peer_msg(port, msg))
553 goto exit;
554
555 port->probing_state = TIPC_CONN_OK;
556
557 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500558 conn_cong = tipc_sk_conn_cong(tsk);
559 tsk->sent_unacked -= msg_msgcnt(msg);
560 if (conn_cong)
561 tipc_sock_wakeup(tsk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500562 } else if (msg_type(msg) == CONN_PROBE) {
563 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
564 return TIPC_OK;
565 msg_set_type(msg, CONN_PROBE_REPLY);
566 return TIPC_FWD_MSG;
567 }
568 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
569exit:
570 kfree_skb(buf);
571 return TIPC_OK;
572}
573
574/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100575 * dest_name_check - verify user is permitted to send to specified port name
576 * @dest: destination address
577 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900578 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100579 * Prevents restricted configuration commands from being issued by
580 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900581 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 * Returns 0 if permission is granted, otherwise errno
583 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800584static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585{
586 struct tipc_cfg_msg_hdr hdr;
587
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500588 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
589 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900590 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
591 return 0;
592 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
593 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900594 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
595 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596
Allan Stephens3f8dd942011-01-18 13:09:29 -0500597 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
598 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900599 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700601 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900603
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604 return 0;
605}
606
Ying Xue3f405042014-01-17 09:50:05 +0800607static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
608{
609 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400610 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800611 DEFINE_WAIT(wait);
612 int done;
613
614 do {
615 int err = sock_error(sk);
616 if (err)
617 return err;
618 if (sock->state == SS_DISCONNECTING)
619 return -EPIPE;
620 if (!*timeo_p)
621 return -EAGAIN;
622 if (signal_pending(current))
623 return sock_intr_errno(*timeo_p);
624
625 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500626 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800627 finish_wait(sk_sleep(sk), &wait);
628 } while (!done);
629 return 0;
630}
631
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500632/**
633 * tipc_sendmcast - send multicast message
634 * @sock: socket structure
635 * @seq: destination address
636 * @iov: message data to send
637 * @dsz: total length of message data
638 * @timeo: timeout to wait for wakeup
639 *
640 * Called from function tipc_sendmsg(), which has done all sanity checks
641 * Returns the number of bytes sent on success, or errno
642 */
643static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
644 struct iovec *iov, size_t dsz, long timeo)
645{
646 struct sock *sk = sock->sk;
647 struct tipc_sock *tsk = tipc_sk(sk);
648 int rc;
649
650 do {
651 if (sock->state != SS_READY) {
652 rc = -EOPNOTSUPP;
653 break;
654 }
655 rc = tipc_port_mcast_xmit(&tsk->port, seq, iov, dsz);
656 if (likely(rc >= 0)) {
657 if (sock->state != SS_READY)
658 sock->state = SS_CONNECTING;
659 break;
660 }
661 if (rc != -ELINKCONG)
662 break;
663 rc = tipc_wait_for_sndmsg(sock, &timeo);
664 } while (!rc);
665
666 return rc;
667}
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400668
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669/**
Ying Xue247f0f32014-02-18 16:06:46 +0800670 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700671 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 * @sock: socket structure
673 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500674 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900675 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900677 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
679 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900680 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681 * Returns the number of bytes sent on success, or errno otherwise
682 */
Ying Xue247f0f32014-02-18 16:06:46 +0800683static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500684 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500686 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700687 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400688 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400689 struct tipc_port *port = &tsk->port;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500690 struct tipc_msg *mhdr = &port->phdr;
691 struct iovec *iov = m->msg_iov;
692 u32 dnode, dport;
693 struct sk_buff *buf;
694 struct tipc_name_seq *seq = &dest->addr.nameseq;
695 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800696 long timeo;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500697 int rc = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698
699 if (unlikely(!dest))
700 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500701
Allan Stephens51f9cc12006-06-25 23:49:06 -0700702 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
703 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100704 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500705
706 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400707 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708
Allan Stephens0c3141e2008-04-15 00:22:02 -0700709 if (iocb)
710 lock_sock(sk);
711
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500712 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700713 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500714 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700715 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700716 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700717 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500718 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700719 goto exit;
720 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400721 if (tsk->port.published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500722 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700723 goto exit;
724 }
725 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400726 tsk->port.conn_type = dest->addr.name.name.type;
727 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700728 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500730 rc = dest_name_check(dest, m);
731 if (rc)
732 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733
Ying Xue3f405042014-01-17 09:50:05 +0800734 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500735
736 if (dest->addrtype == TIPC_ADDR_MCAST) {
737 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
738 goto exit;
739 } else if (dest->addrtype == TIPC_ADDR_NAME) {
740 u32 type = dest->addr.name.name.type;
741 u32 inst = dest->addr.name.name.instance;
742 u32 domain = dest->addr.name.domain;
743
744 dnode = domain;
745 msg_set_type(mhdr, TIPC_NAMED_MSG);
746 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
747 msg_set_nametype(mhdr, type);
748 msg_set_nameinst(mhdr, inst);
749 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
750 dport = tipc_nametbl_translate(type, inst, &dnode);
751 msg_set_destnode(mhdr, dnode);
752 msg_set_destport(mhdr, dport);
753 if (unlikely(!dport && !dnode)) {
754 rc = -EHOSTUNREACH;
755 goto exit;
756 }
757 } else if (dest->addrtype == TIPC_ADDR_ID) {
758 dnode = dest->addr.id.node;
759 msg_set_type(mhdr, TIPC_DIRECT_MSG);
760 msg_set_lookup_scope(mhdr, 0);
761 msg_set_destnode(mhdr, dnode);
762 msg_set_destport(mhdr, dest->addr.id.ref);
763 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
764 }
765
766new_mtu:
767 mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
768 rc = tipc_msg_build2(mhdr, iov, 0, dsz, mtu, &buf);
769 if (rc < 0)
770 goto exit;
771
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900772 do {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500773 rc = tipc_link_xmit2(buf, dnode, tsk->port.ref);
774 if (likely(rc >= 0)) {
775 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700776 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500777 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700778 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900779 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500780 if (rc == -EMSGSIZE)
781 goto new_mtu;
782
783 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700784 break;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500785
786 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400787 if (rc)
788 kfree_skb_list(buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500789 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700790exit:
791 if (iocb)
792 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500793
794 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100795}
796
Ying Xue391a6dd2014-01-17 09:50:06 +0800797static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
798{
799 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400800 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800801 DEFINE_WAIT(wait);
802 int done;
803
804 do {
805 int err = sock_error(sk);
806 if (err)
807 return err;
808 if (sock->state == SS_DISCONNECTING)
809 return -EPIPE;
810 else if (sock->state != SS_CONNECTED)
811 return -ENOTCONN;
812 if (!*timeo_p)
813 return -EAGAIN;
814 if (signal_pending(current))
815 return sock_intr_errno(*timeo_p);
816
817 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
818 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -0500819 (!tsk->link_cong &&
820 !tipc_sk_conn_cong(tsk)) ||
821 !tsk->port.connected);
Ying Xue391a6dd2014-01-17 09:50:06 +0800822 finish_wait(sk_sleep(sk), &wait);
823 } while (!done);
824 return 0;
825}
826
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900827/**
Ying Xue247f0f32014-02-18 16:06:46 +0800828 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829 * @iocb: (unused)
830 * @sock: socket structure
831 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500832 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900833 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900835 *
836 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700837 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100838 */
Ying Xue247f0f32014-02-18 16:06:46 +0800839static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500840 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100841{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700842 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400843 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500844 struct tipc_port *port = &tsk->port;
845 struct tipc_msg *mhdr = &port->phdr;
846 struct sk_buff *buf;
847 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
848 u32 ref = port->ref;
849 int rc = -EINVAL;
850 long timeo;
851 u32 dnode;
852 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900853
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500854 /* Handle implied connection establishment */
855 if (unlikely(dest)) {
856 rc = tipc_sendmsg(iocb, sock, m, dsz);
857 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -0500858 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500859 return rc;
860 }
861 if (dsz > (uint)INT_MAX)
862 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700863
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500864 if (iocb)
865 lock_sock(sk);
866
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900867 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500868 if (sock->state == SS_DISCONNECTING)
869 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +0800870 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500871 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800872 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900873 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100874
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500875 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
876 dnode = tipc_port_peernode(port);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500877
878next:
879 mtu = port->max_pkt;
880 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
881 rc = tipc_msg_build2(mhdr, m->msg_iov, sent, send, mtu, &buf);
882 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700883 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500884 do {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500885 if (likely(!tipc_sk_conn_cong(tsk))) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500886 rc = tipc_link_xmit2(buf, dnode, ref);
887 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500888 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500889 sent += send;
890 if (sent == dsz)
891 break;
892 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700893 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500894 if (rc == -EMSGSIZE) {
895 port->max_pkt = tipc_node_get_mtu(dnode, ref);
896 goto next;
897 }
898 if (rc != -ELINKCONG)
899 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500901 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400902 if (rc)
903 kfree_skb_list(buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500904 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700905exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500906 if (iocb)
907 release_sock(sk);
908 return sent ? sent : rc;
909}
910
911/**
912 * tipc_send_packet - send a connection-oriented message
913 * @iocb: if NULL, indicates that socket lock is already held
914 * @sock: socket structure
915 * @m: message to send
916 * @dsz: length of data to be transmitted
917 *
918 * Used for SOCK_SEQPACKET messages.
919 *
920 * Returns the number of bytes sent on success, or errno otherwise
921 */
922static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
923 struct msghdr *m, size_t dsz)
924{
925 if (dsz > TIPC_MAX_USER_MSG_SIZE)
926 return -EMSGSIZE;
927
928 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100929}
930
931/**
932 * auto_connect - complete connection setup to a remote port
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400933 * @tsk: tipc socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900935 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936 * Returns 0 on success, errno otherwise
937 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400938static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100939{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400940 struct tipc_port *port = &tsk->port;
941 struct socket *sock = tsk->sk.sk_socket;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400942 struct tipc_portid peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100943
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400944 peer.ref = msg_origport(msg);
945 peer.node = msg_orignode(msg);
946
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400947 __tipc_port_connect(port->ref, port, &peer);
Ying Xue584d24b2012-11-29 18:51:19 -0500948
949 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
950 return -EINVAL;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400951 msg_set_importance(&port->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100952 sock->state = SS_CONNECTED;
953 return 0;
954}
955
956/**
957 * set_orig_addr - capture sender's address for received message
958 * @m: descriptor for message info
959 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900960 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100961 * Note: Address is not captured if not requested by receiver.
962 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800963static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964{
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100965 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100966
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900967 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968 addr->family = AF_TIPC;
969 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000970 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100971 addr->addr.id.ref = msg_origport(msg);
972 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000973 addr->addr.name.domain = 0; /* could leave uninitialized */
974 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100975 m->msg_namelen = sizeof(struct sockaddr_tipc);
976 }
977}
978
979/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900980 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100981 * @m: descriptor for message info
982 * @msg: received message header
983 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900984 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900986 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100987 * Returns 0 if successful, otherwise errno
988 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800989static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400990 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991{
992 u32 anc_data[3];
993 u32 err;
994 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700995 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996 int res;
997
998 if (likely(m->msg_controllen == 0))
999 return 0;
1000
1001 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 err = msg ? msg_errcode(msg) : 0;
1003 if (unlikely(err)) {
1004 anc_data[0] = err;
1005 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001006 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1007 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001009 if (anc_data[1]) {
1010 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1011 msg_data(msg));
1012 if (res)
1013 return res;
1014 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001015 }
1016
1017 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1019 switch (dest_type) {
1020 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001021 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001022 anc_data[0] = msg_nametype(msg);
1023 anc_data[1] = msg_namelower(msg);
1024 anc_data[2] = msg_namelower(msg);
1025 break;
1026 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001027 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 anc_data[0] = msg_nametype(msg);
1029 anc_data[1] = msg_namelower(msg);
1030 anc_data[2] = msg_nameupper(msg);
1031 break;
1032 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001033 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034 anc_data[0] = tport->conn_type;
1035 anc_data[1] = tport->conn_instance;
1036 anc_data[2] = tport->conn_instance;
1037 break;
1038 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001039 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 }
Allan Stephens2db99832010-12-31 18:59:33 +00001041 if (has_name) {
1042 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1043 if (res)
1044 return res;
1045 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046
1047 return 0;
1048}
1049
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001050static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001051{
1052 struct sock *sk = sock->sk;
1053 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001054 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001055 int err;
1056
1057 for (;;) {
1058 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001059 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001060 if (sock->state == SS_DISCONNECTING) {
1061 err = -ENOTCONN;
1062 break;
1063 }
1064 release_sock(sk);
1065 timeo = schedule_timeout(timeo);
1066 lock_sock(sk);
1067 }
1068 err = 0;
1069 if (!skb_queue_empty(&sk->sk_receive_queue))
1070 break;
1071 err = sock_intr_errno(timeo);
1072 if (signal_pending(current))
1073 break;
1074 err = -EAGAIN;
1075 if (!timeo)
1076 break;
1077 }
1078 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001079 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001080 return err;
1081}
1082
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001083/**
Ying Xue247f0f32014-02-18 16:06:46 +08001084 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001085 * @iocb: (unused)
1086 * @m: descriptor for message info
1087 * @buf_len: total size of user buffer area
1088 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001089 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1091 * If the complete message doesn't fit in user area, truncate it.
1092 *
1093 * Returns size of returned message data, errno otherwise
1094 */
Ying Xue247f0f32014-02-18 16:06:46 +08001095static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1096 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001097{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001098 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001099 struct tipc_sock *tsk = tipc_sk(sk);
1100 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101 struct sk_buff *buf;
1102 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001103 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001104 unsigned int sz;
1105 u32 err;
1106 int res;
1107
Allan Stephens0c3141e2008-04-15 00:22:02 -07001108 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001109 if (unlikely(!buf_len))
1110 return -EINVAL;
1111
Allan Stephens0c3141e2008-04-15 00:22:02 -07001112 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001113
Allan Stephens0c3141e2008-04-15 00:22:02 -07001114 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115 res = -ENOTCONN;
1116 goto exit;
1117 }
1118
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001119 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001120restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121
Allan Stephens0c3141e2008-04-15 00:22:02 -07001122 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001123 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001124 if (res)
1125 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001126
1127 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001128 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129 msg = buf_msg(buf);
1130 sz = msg_data_sz(msg);
1131 err = msg_errcode(msg);
1132
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001135 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136 goto restart;
1137 }
1138
1139 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140 set_orig_addr(m, msg);
1141
1142 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001143 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001144 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 goto exit;
1146
1147 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 if (!err) {
1149 if (unlikely(buf_len < sz)) {
1150 sz = buf_len;
1151 m->msg_flags |= MSG_TRUNC;
1152 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001153 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1154 m->msg_iov, sz);
1155 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157 res = sz;
1158 } else {
1159 if ((sock->state == SS_READY) ||
1160 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1161 res = 0;
1162 else
1163 res = -ECONNRESET;
1164 }
1165
1166 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens990098062008-04-15 00:06:12 -07001168 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001169 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1170 tipc_acknowledge(port->ref, tsk->rcv_unacked);
1171 tsk->rcv_unacked = 0;
1172 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001173 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001174 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001176 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177 return res;
1178}
1179
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001180/**
Ying Xue247f0f32014-02-18 16:06:46 +08001181 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 * @iocb: (unused)
1183 * @m: descriptor for message info
1184 * @buf_len: total size of user buffer area
1185 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001186 *
1187 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001188 * will optionally wait for more; never truncates data.
1189 *
1190 * Returns size of returned message data, errno otherwise
1191 */
Ying Xue247f0f32014-02-18 16:06:46 +08001192static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1193 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001195 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001196 struct tipc_sock *tsk = tipc_sk(sk);
1197 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 struct sk_buff *buf;
1199 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001200 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001202 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001204 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001205 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206
1207 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 if (unlikely(!buf_len))
1209 return -EINVAL;
1210
Allan Stephens0c3141e2008-04-15 00:22:02 -07001211 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001212
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001213 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214 res = -ENOTCONN;
1215 goto exit;
1216 }
1217
Florian Westphal3720d402010-08-17 11:00:04 +00001218 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001219 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001220
Allan Stephens0c3141e2008-04-15 00:22:02 -07001221restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001222 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001223 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001224 if (res)
1225 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001226
1227 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001228 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001229 msg = buf_msg(buf);
1230 sz = msg_data_sz(msg);
1231 err = msg_errcode(msg);
1232
1233 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001235 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 goto restart;
1237 }
1238
1239 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240 if (sz_copied == 0) {
1241 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001242 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001243 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001244 goto exit;
1245 }
1246
1247 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001248 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001249 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001250
Allan Stephens0232fd02011-02-21 09:45:40 -05001251 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 needed = (buf_len - sz_copied);
1253 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001254
1255 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1256 m->msg_iov, sz_to_copy);
1257 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001258 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001259
Per Lidenb97bf3f2006-01-02 19:04:38 +01001260 sz_copied += sz_to_copy;
1261
1262 if (sz_to_copy < sz) {
1263 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001264 TIPC_SKB_CB(buf)->handle =
1265 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001266 goto exit;
1267 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268 } else {
1269 if (sz_copied != 0)
1270 goto exit; /* can't add error msg to valid data */
1271
1272 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1273 res = 0;
1274 else
1275 res = -ECONNRESET;
1276 }
1277
1278 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001280 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1281 tipc_acknowledge(port->ref, tsk->rcv_unacked);
1282 tsk->rcv_unacked = 0;
1283 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001284 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001285 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001286
1287 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001288 if ((sz_copied < buf_len) && /* didn't get all requested data */
1289 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001290 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001291 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1292 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293 goto restart;
1294
1295exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001296 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001297 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298}
1299
1300/**
Ying Xuef288bef2012-08-21 11:16:57 +08001301 * tipc_write_space - wake up thread if port congestion is released
1302 * @sk: socket
1303 */
1304static void tipc_write_space(struct sock *sk)
1305{
1306 struct socket_wq *wq;
1307
1308 rcu_read_lock();
1309 wq = rcu_dereference(sk->sk_wq);
1310 if (wq_has_sleeper(wq))
1311 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1312 POLLWRNORM | POLLWRBAND);
1313 rcu_read_unlock();
1314}
1315
1316/**
1317 * tipc_data_ready - wake up threads to indicate messages have been received
1318 * @sk: socket
1319 * @len: the length of messages
1320 */
David S. Miller676d2362014-04-11 16:15:36 -04001321static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001322{
1323 struct socket_wq *wq;
1324
1325 rcu_read_lock();
1326 wq = rcu_dereference(sk->sk_wq);
1327 if (wq_has_sleeper(wq))
1328 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1329 POLLRDNORM | POLLRDBAND);
1330 rcu_read_unlock();
1331}
1332
1333/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001334 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001335 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001336 * @msg: message
1337 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001338 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001339 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001340static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001341{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001342 struct sock *sk = &tsk->sk;
1343 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001344 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001345 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001346
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001347 int retval = -TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001348 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001349
1350 if (msg_mcast(msg))
1351 return retval;
1352
1353 switch ((int)sock->state) {
1354 case SS_CONNECTED:
1355 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001356 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001357 if (unlikely(msg_errcode(msg))) {
1358 sock->state = SS_DISCONNECTING;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001359 __tipc_port_disconnect(port);
Ying Xue7e6c1312012-11-29 18:39:14 -05001360 }
1361 retval = TIPC_OK;
1362 }
1363 break;
1364 case SS_CONNECTING:
1365 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001366 if (unlikely(msg_errcode(msg))) {
1367 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001368 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001369 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001370 break;
1371 }
1372
1373 if (unlikely(!msg_connected(msg)))
1374 break;
1375
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001376 res = auto_connect(tsk, msg);
Ying Xue584d24b2012-11-29 18:51:19 -05001377 if (res) {
1378 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001379 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001380 retval = TIPC_OK;
1381 break;
1382 }
1383
1384 /* If an incoming message is an 'ACK-', it should be
1385 * discarded here because it doesn't contain useful
1386 * data. In addition, we should try to wake up
1387 * connect() routine if sleeping.
1388 */
1389 if (msg_data_sz(msg) == 0) {
1390 kfree_skb(*buf);
1391 *buf = NULL;
1392 if (waitqueue_active(sk_sleep(sk)))
1393 wake_up_interruptible(sk_sleep(sk));
1394 }
1395 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001396 break;
1397 case SS_LISTENING:
1398 case SS_UNCONNECTED:
1399 /* Accept only SYN message */
1400 if (!msg_connected(msg) && !(msg_errcode(msg)))
1401 retval = TIPC_OK;
1402 break;
1403 case SS_DISCONNECTING:
1404 break;
1405 default:
1406 pr_err("Unknown socket state %u\n", sock->state);
1407 }
1408 return retval;
1409}
1410
1411/**
Ying Xueaba79f32013-01-20 23:30:09 +01001412 * rcvbuf_limit - get proper overload limit of socket receive queue
1413 * @sk: socket
1414 * @buf: message
1415 *
1416 * For all connection oriented messages, irrespective of importance,
1417 * the default overload value (i.e. 67MB) is set as limit.
1418 *
1419 * For all connectionless messages, by default new queue limits are
1420 * as belows:
1421 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001422 * TIPC_LOW_IMPORTANCE (4 MB)
1423 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1424 * TIPC_HIGH_IMPORTANCE (16 MB)
1425 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001426 *
1427 * Returns overload limit according to corresponding message importance
1428 */
1429static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1430{
1431 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001432
1433 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001434 return sysctl_tipc_rmem[2];
1435
1436 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1437 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001438}
1439
1440/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001441 * filter_rcv - validate incoming message
1442 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001443 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001444 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001445 * Enqueues message on receive queue if acceptable; optionally handles
1446 * disconnect indication for a connected socket.
1447 *
1448 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001449 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001450 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001451 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001452 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001453static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001454{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001455 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001456 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001457 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001458 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001459 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001460 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001461
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001462 if (unlikely(msg_user(msg) == CONN_MANAGER))
1463 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001464
Per Lidenb97bf3f2006-01-02 19:04:38 +01001465 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001466 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001467 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001468
Per Lidenb97bf3f2006-01-02 19:04:38 +01001469 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001470 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001471 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001472 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001473 rc = filter_connect(tsk, &buf);
1474 if (rc != TIPC_OK || buf == NULL)
1475 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001476 }
1477
1478 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001479 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001480 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001481
Ying Xueaba79f32013-01-20 23:30:09 +01001482 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001483 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001484 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001485 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001486
David S. Miller676d2362014-04-11 16:15:36 -04001487 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488 return TIPC_OK;
1489}
1490
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001491/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001492 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001493 * @sk: socket
1494 * @buf: message
1495 *
1496 * Caller must hold socket lock, but not port lock.
1497 *
1498 * Returns 0
1499 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001500static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001501{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001502 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001503 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001504 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001505 uint truesize = buf->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001506
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001507 rc = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001508
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001509 if (likely(!rc)) {
1510 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1511 atomic_add(truesize, &tsk->dupl_rcvcnt);
1512 return 0;
1513 }
1514
1515 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1516 return 0;
1517
1518 tipc_link_xmit2(buf, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001519
Allan Stephens0c3141e2008-04-15 00:22:02 -07001520 return 0;
1521}
1522
1523/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001524 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001525 * @buf: buffer containing arriving message
1526 * Consumes buffer
1527 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001528 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001529int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001530{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001531 struct tipc_sock *tsk;
1532 struct tipc_port *port;
1533 struct sock *sk;
1534 u32 dport = msg_destport(buf_msg(buf));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001535 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001536 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001537 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001538
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001539 /* Validate destination and message */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001540 port = tipc_port_lock(dport);
1541 if (unlikely(!port)) {
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001542 rc = tipc_msg_eval(buf, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001543 goto exit;
1544 }
1545
1546 tsk = tipc_port_to_sock(port);
1547 sk = &tsk->sk;
1548
1549 /* Queue message */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001550 bh_lock_sock(sk);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001551
Allan Stephens0c3141e2008-04-15 00:22:02 -07001552 if (!sock_owned_by_user(sk)) {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001553 rc = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001554 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001555 if (sk->sk_backlog.len == 0)
1556 atomic_set(&tsk->dupl_rcvcnt, 0);
1557 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1558 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001559 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001560 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001561 bh_unlock_sock(sk);
1562 tipc_port_unlock(port);
1563
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001564 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001565 return 0;
1566exit:
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001567 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001568 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001569
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001570 tipc_link_xmit2(buf, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001571 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001572}
1573
Ying Xue78eb3a52014-01-17 09:50:03 +08001574static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1575{
1576 struct sock *sk = sock->sk;
1577 DEFINE_WAIT(wait);
1578 int done;
1579
1580 do {
1581 int err = sock_error(sk);
1582 if (err)
1583 return err;
1584 if (!*timeo_p)
1585 return -ETIMEDOUT;
1586 if (signal_pending(current))
1587 return sock_intr_errno(*timeo_p);
1588
1589 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1590 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1591 finish_wait(sk_sleep(sk), &wait);
1592 } while (!done);
1593 return 0;
1594}
1595
Per Lidenb97bf3f2006-01-02 19:04:38 +01001596/**
Ying Xue247f0f32014-02-18 16:06:46 +08001597 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001598 * @sock: socket structure
1599 * @dest: socket address for destination port
1600 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001601 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001602 *
1603 * Returns 0 on success, errno otherwise
1604 */
Ying Xue247f0f32014-02-18 16:06:46 +08001605static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1606 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001607{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001608 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001609 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1610 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001611 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1612 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001613 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001614
Allan Stephens0c3141e2008-04-15 00:22:02 -07001615 lock_sock(sk);
1616
Allan Stephensb89741a2008-04-15 00:20:37 -07001617 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001618 if (sock->state == SS_READY) {
1619 res = -EOPNOTSUPP;
1620 goto exit;
1621 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001622
Allan Stephensb89741a2008-04-15 00:20:37 -07001623 /*
1624 * Reject connection attempt using multicast address
1625 *
1626 * Note: send_msg() validates the rest of the address fields,
1627 * so there's no need to do it here
1628 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001629 if (dst->addrtype == TIPC_ADDR_MCAST) {
1630 res = -EINVAL;
1631 goto exit;
1632 }
1633
Ying Xue78eb3a52014-01-17 09:50:03 +08001634 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001635 switch (sock->state) {
1636 case SS_UNCONNECTED:
1637 /* Send a 'SYN-' to destination */
1638 m.msg_name = dest;
1639 m.msg_namelen = destlen;
1640
1641 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1642 * indicate send_msg() is never blocked.
1643 */
1644 if (!timeout)
1645 m.msg_flags = MSG_DONTWAIT;
1646
Ying Xue247f0f32014-02-18 16:06:46 +08001647 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001648 if ((res < 0) && (res != -EWOULDBLOCK))
1649 goto exit;
1650
1651 /* Just entered SS_CONNECTING state; the only
1652 * difference is that return value in non-blocking
1653 * case is EINPROGRESS, rather than EALREADY.
1654 */
1655 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001656 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001657 if (previous == SS_CONNECTING)
1658 res = -EALREADY;
1659 if (!timeout)
1660 goto exit;
1661 timeout = msecs_to_jiffies(timeout);
1662 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1663 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001664 break;
1665 case SS_CONNECTED:
1666 res = -EISCONN;
1667 break;
1668 default:
1669 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001670 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001671 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001672exit:
1673 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001674 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001675}
1676
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001677/**
Ying Xue247f0f32014-02-18 16:06:46 +08001678 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679 * @sock: socket structure
1680 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001681 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001682 * Returns 0 on success, errno otherwise
1683 */
Ying Xue247f0f32014-02-18 16:06:46 +08001684static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001685{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001686 struct sock *sk = sock->sk;
1687 int res;
1688
1689 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690
Ying Xue245f3d32011-07-06 06:01:13 -04001691 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001692 res = -EINVAL;
1693 else {
1694 sock->state = SS_LISTENING;
1695 res = 0;
1696 }
1697
1698 release_sock(sk);
1699 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001700}
1701
Ying Xue6398e232014-01-17 09:50:04 +08001702static int tipc_wait_for_accept(struct socket *sock, long timeo)
1703{
1704 struct sock *sk = sock->sk;
1705 DEFINE_WAIT(wait);
1706 int err;
1707
1708 /* True wake-one mechanism for incoming connections: only
1709 * one process gets woken up, not the 'whole herd'.
1710 * Since we do not 'race & poll' for established sockets
1711 * anymore, the common case will execute the loop only once.
1712 */
1713 for (;;) {
1714 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1715 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001716 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001717 release_sock(sk);
1718 timeo = schedule_timeout(timeo);
1719 lock_sock(sk);
1720 }
1721 err = 0;
1722 if (!skb_queue_empty(&sk->sk_receive_queue))
1723 break;
1724 err = -EINVAL;
1725 if (sock->state != SS_LISTENING)
1726 break;
1727 err = sock_intr_errno(timeo);
1728 if (signal_pending(current))
1729 break;
1730 err = -EAGAIN;
1731 if (!timeo)
1732 break;
1733 }
1734 finish_wait(sk_sleep(sk), &wait);
1735 return err;
1736}
1737
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001738/**
Ying Xue247f0f32014-02-18 16:06:46 +08001739 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001740 * @sock: listening socket
1741 * @newsock: new socket that is to be connected
1742 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001743 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001744 * Returns 0 on success, errno otherwise
1745 */
Ying Xue247f0f32014-02-18 16:06:46 +08001746static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001747{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001748 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001750 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001751 struct tipc_msg *msg;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001752 struct tipc_portid peer;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001753 u32 new_ref;
Ying Xue6398e232014-01-17 09:50:04 +08001754 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001755 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001756
Allan Stephens0c3141e2008-04-15 00:22:02 -07001757 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001758
Allan Stephens0c3141e2008-04-15 00:22:02 -07001759 if (sock->state != SS_LISTENING) {
1760 res = -EINVAL;
1761 goto exit;
1762 }
Ying Xue6398e232014-01-17 09:50:04 +08001763 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1764 res = tipc_wait_for_accept(sock, timeo);
1765 if (res)
1766 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001767
1768 buf = skb_peek(&sk->sk_receive_queue);
1769
Ying Xuec5fa7b32013-06-17 10:54:39 -04001770 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001771 if (res)
1772 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001773
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001774 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001775 new_port = &tipc_sk(new_sk)->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001776 new_ref = new_port->ref;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001777 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001778
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001779 /* we lock on new_sk; but lockdep sees the lock on sk */
1780 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001781
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001782 /*
1783 * Reject any stray messages received by new socket
1784 * before the socket lock was taken (very, very unlikely)
1785 */
1786 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001788 /* Connect new socket to it's peer */
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001789 peer.ref = msg_origport(msg);
1790 peer.node = msg_orignode(msg);
1791 tipc_port_connect(new_ref, &peer);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001792 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001794 tipc_port_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001795 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001796 new_port->conn_type = msg_nametype(msg);
1797 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001798 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001799
1800 /*
1801 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1802 * Respond to 'SYN+' by queuing it on new socket.
1803 */
1804 if (!msg_data_sz(msg)) {
1805 struct msghdr m = {NULL,};
1806
1807 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001808 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001809 } else {
1810 __skb_dequeue(&sk->sk_receive_queue);
1811 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001812 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001813 }
1814 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001815exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001816 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001817 return res;
1818}
1819
1820/**
Ying Xue247f0f32014-02-18 16:06:46 +08001821 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001822 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001823 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001824 *
1825 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001826 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001827 * Returns 0 on success, errno otherwise
1828 */
Ying Xue247f0f32014-02-18 16:06:46 +08001829static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001830{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001831 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001832 struct tipc_sock *tsk = tipc_sk(sk);
1833 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001835 u32 peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001836 int res;
1837
Allan Stephense247a8f2008-03-06 15:05:38 -08001838 if (how != SHUT_RDWR)
1839 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001840
Allan Stephens0c3141e2008-04-15 00:22:02 -07001841 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001842
1843 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001844 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001845 case SS_CONNECTED:
1846
Per Lidenb97bf3f2006-01-02 19:04:38 +01001847restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001848 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001849 buf = __skb_dequeue(&sk->sk_receive_queue);
1850 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001851 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001852 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001853 goto restart;
1854 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001855 tipc_port_disconnect(port->ref);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001856 if (tipc_msg_reverse(buf, &peer, TIPC_CONN_SHUTDOWN))
1857 tipc_link_xmit2(buf, peer, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001858 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001859 tipc_port_shutdown(port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001860 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001861
1862 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001863
1864 /* fall through */
1865
1866 case SS_DISCONNECTING:
1867
Ying Xue75031152012-10-29 09:38:15 -04001868 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001869 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001870
1871 /* Wake up anyone sleeping in poll */
1872 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001873 res = 0;
1874 break;
1875
1876 default:
1877 res = -ENOTCONN;
1878 }
1879
Allan Stephens0c3141e2008-04-15 00:22:02 -07001880 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001881 return res;
1882}
1883
1884/**
Ying Xue247f0f32014-02-18 16:06:46 +08001885 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886 * @sock: socket structure
1887 * @lvl: option level
1888 * @opt: option identifier
1889 * @ov: pointer to new option value
1890 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001891 *
1892 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001893 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001894 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001895 * Returns 0 on success, errno otherwise
1896 */
Ying Xue247f0f32014-02-18 16:06:46 +08001897static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1898 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001899{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001900 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001901 struct tipc_sock *tsk = tipc_sk(sk);
1902 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001903 u32 value;
1904 int res;
1905
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001906 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1907 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001908 if (lvl != SOL_TIPC)
1909 return -ENOPROTOOPT;
1910 if (ol < sizeof(value))
1911 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001912 res = get_user(value, (u32 __user *)ov);
1913 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001914 return res;
1915
Allan Stephens0c3141e2008-04-15 00:22:02 -07001916 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001917
Per Lidenb97bf3f2006-01-02 19:04:38 +01001918 switch (opt) {
1919 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001920 tipc_port_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001921 break;
1922 case TIPC_SRC_DROPPABLE:
1923 if (sock->type != SOCK_STREAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001924 tipc_port_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001925 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001926 res = -ENOPROTOOPT;
1927 break;
1928 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001929 tipc_port_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001930 break;
1931 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001932 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001933 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001934 break;
1935 default:
1936 res = -EINVAL;
1937 }
1938
Allan Stephens0c3141e2008-04-15 00:22:02 -07001939 release_sock(sk);
1940
Per Lidenb97bf3f2006-01-02 19:04:38 +01001941 return res;
1942}
1943
1944/**
Ying Xue247f0f32014-02-18 16:06:46 +08001945 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001946 * @sock: socket structure
1947 * @lvl: option level
1948 * @opt: option identifier
1949 * @ov: receptacle for option value
1950 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001951 *
1952 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001953 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001954 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001955 * Returns 0 on success, errno otherwise
1956 */
Ying Xue247f0f32014-02-18 16:06:46 +08001957static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1958 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001959{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001960 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001961 struct tipc_sock *tsk = tipc_sk(sk);
1962 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001963 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001964 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001965 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001966
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001967 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1968 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001969 if (lvl != SOL_TIPC)
1970 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001971 res = get_user(len, ol);
1972 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001973 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001974
Allan Stephens0c3141e2008-04-15 00:22:02 -07001975 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001976
1977 switch (opt) {
1978 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001979 value = tipc_port_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001980 break;
1981 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001982 value = tipc_port_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001983 break;
1984 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001985 value = tipc_port_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001986 break;
1987 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001988 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001989 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001990 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001991 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001992 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001993 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001994 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001995 value = skb_queue_len(&sk->sk_receive_queue);
1996 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001997 default:
1998 res = -EINVAL;
1999 }
2000
Allan Stephens0c3141e2008-04-15 00:22:02 -07002001 release_sock(sk);
2002
Paul Gortmaker25860c32010-12-31 18:59:31 +00002003 if (res)
2004 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002005
Paul Gortmaker25860c32010-12-31 18:59:31 +00002006 if (len < sizeof(value))
2007 return -EINVAL;
2008
2009 if (copy_to_user(ov, &value, sizeof(value)))
2010 return -EFAULT;
2011
2012 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002013}
2014
Erik Hugne78acb1f2014-04-24 16:26:47 +02002015int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
2016{
2017 struct tipc_sioc_ln_req lnr;
2018 void __user *argp = (void __user *)arg;
2019
2020 switch (cmd) {
2021 case SIOCGETLINKNAME:
2022 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2023 return -EFAULT;
2024 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
2025 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2026 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2027 return -EFAULT;
2028 return 0;
2029 }
2030 return -EADDRNOTAVAIL;
2031 break;
2032 default:
2033 return -ENOIOCTLCMD;
2034 }
2035}
2036
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002037/* Protocol switches for the various types of TIPC sockets */
2038
Florian Westphalbca65ea2008-02-07 18:18:01 -08002039static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002040 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002041 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002042 .release = tipc_release,
2043 .bind = tipc_bind,
2044 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002045 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002046 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002047 .getname = tipc_getname,
2048 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002049 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002050 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002051 .shutdown = tipc_shutdown,
2052 .setsockopt = tipc_setsockopt,
2053 .getsockopt = tipc_getsockopt,
2054 .sendmsg = tipc_sendmsg,
2055 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002056 .mmap = sock_no_mmap,
2057 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002058};
2059
Florian Westphalbca65ea2008-02-07 18:18:01 -08002060static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002061 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002062 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002063 .release = tipc_release,
2064 .bind = tipc_bind,
2065 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002066 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002067 .accept = tipc_accept,
2068 .getname = tipc_getname,
2069 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002070 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002071 .listen = tipc_listen,
2072 .shutdown = tipc_shutdown,
2073 .setsockopt = tipc_setsockopt,
2074 .getsockopt = tipc_getsockopt,
2075 .sendmsg = tipc_send_packet,
2076 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002077 .mmap = sock_no_mmap,
2078 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002079};
2080
Florian Westphalbca65ea2008-02-07 18:18:01 -08002081static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002082 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002083 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002084 .release = tipc_release,
2085 .bind = tipc_bind,
2086 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002087 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002088 .accept = tipc_accept,
2089 .getname = tipc_getname,
2090 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002091 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002092 .listen = tipc_listen,
2093 .shutdown = tipc_shutdown,
2094 .setsockopt = tipc_setsockopt,
2095 .getsockopt = tipc_getsockopt,
2096 .sendmsg = tipc_send_stream,
2097 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002098 .mmap = sock_no_mmap,
2099 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002100};
2101
Florian Westphalbca65ea2008-02-07 18:18:01 -08002102static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002103 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002104 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002105 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002106};
2107
2108static struct proto tipc_proto = {
2109 .name = "TIPC",
2110 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002111 .obj_size = sizeof(struct tipc_sock),
2112 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002113};
2114
Ying Xuec5fa7b32013-06-17 10:54:39 -04002115static struct proto tipc_proto_kern = {
2116 .name = "TIPC",
2117 .obj_size = sizeof(struct tipc_sock),
2118 .sysctl_rmem = sysctl_tipc_rmem
2119};
2120
Per Lidenb97bf3f2006-01-02 19:04:38 +01002121/**
Per Liden4323add2006-01-18 00:38:21 +01002122 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002123 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002124 * Returns 0 on success, errno otherwise
2125 */
Per Liden4323add2006-01-18 00:38:21 +01002126int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002127{
2128 int res;
2129
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002130 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002131 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002132 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002133 goto out;
2134 }
2135
2136 res = sock_register(&tipc_family_ops);
2137 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002138 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002139 proto_unregister(&tipc_proto);
2140 goto out;
2141 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002142 out:
2143 return res;
2144}
2145
2146/**
Per Liden4323add2006-01-18 00:38:21 +01002147 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002148 */
Per Liden4323add2006-01-18 00:38:21 +01002149void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002150{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002151 sock_unregister(tipc_family_ops.family);
2152 proto_unregister(&tipc_proto);
2153}