blob: a8be4d2001f7adfa2e6aec773bd6bd4a0a97a71c [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>
Erik Hugne2cf8aa12012-06-29 00:16:37 -040043
Per Lidenb97bf3f2006-01-02 19:04:38 +010044#define SS_LISTENING -1 /* socket is listening */
45#define SS_READY -2 /* socket is connectionless */
46
Allan Stephens3654ea02008-04-13 21:35:11 -070047#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Jon Paul Maloyac0074e2014-06-25 20:41:41 -050048#define TIPC_FWD_MSG 1
Per Lidenb97bf3f2006-01-02 19:04:38 +010049
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -040050static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -040051static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +080052static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080053static int tipc_release(struct socket *sock);
54static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -040055static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
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))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400133 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500134 }
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))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400343 tipc_link_xmit(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
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400537/**
538 * tipc_sendmcast - send multicast message
539 * @sock: socket structure
540 * @seq: destination address
541 * @iov: message data to send
542 * @dsz: total length of message data
543 * @timeo: timeout to wait for wakeup
544 *
545 * Called from function tipc_sendmsg(), which has done all sanity checks
546 * Returns the number of bytes sent on success, or errno
547 */
548static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
549 struct iovec *iov, size_t dsz, long timeo)
550{
551 struct sock *sk = sock->sk;
552 struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr;
553 struct sk_buff *buf;
554 uint mtu;
555 int rc;
556
557 msg_set_type(mhdr, TIPC_MCAST_MSG);
558 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
559 msg_set_destport(mhdr, 0);
560 msg_set_destnode(mhdr, 0);
561 msg_set_nametype(mhdr, seq->type);
562 msg_set_namelower(mhdr, seq->lower);
563 msg_set_nameupper(mhdr, seq->upper);
564 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
565
566new_mtu:
567 mtu = tipc_bclink_get_mtu();
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400568 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400569 if (unlikely(rc < 0))
570 return rc;
571
572 do {
573 rc = tipc_bclink_xmit(buf);
574 if (likely(rc >= 0)) {
575 rc = dsz;
576 break;
577 }
578 if (rc == -EMSGSIZE)
579 goto new_mtu;
580 if (rc != -ELINKCONG)
581 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400582 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400583 rc = tipc_wait_for_sndmsg(sock, &timeo);
584 if (rc)
585 kfree_skb_list(buf);
586 } while (!rc);
587 return rc;
588}
589
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400590/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
591 */
592void tipc_sk_mcast_rcv(struct sk_buff *buf)
593{
594 struct tipc_msg *msg = buf_msg(buf);
595 struct tipc_port_list dports = {0, NULL, };
596 struct tipc_port_list *item;
597 struct sk_buff *b;
598 uint i, last, dst = 0;
599 u32 scope = TIPC_CLUSTER_SCOPE;
600
601 if (in_own_node(msg_orignode(msg)))
602 scope = TIPC_NODE_SCOPE;
603
604 /* Create destination port list: */
605 tipc_nametbl_mc_translate(msg_nametype(msg),
606 msg_namelower(msg),
607 msg_nameupper(msg),
608 scope,
609 &dports);
610 last = dports.count;
611 if (!last) {
612 kfree_skb(buf);
613 return;
614 }
615
616 for (item = &dports; item; item = item->next) {
617 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
618 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
619 if (!b) {
620 pr_warn("Failed do clone mcast rcv buffer\n");
621 continue;
622 }
623 msg_set_destport(msg, item->ports[i]);
624 tipc_sk_rcv(b);
625 }
626 }
627 tipc_port_list_free(&dports);
628}
629
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900630/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500631 * tipc_sk_proto_rcv - receive a connection mng protocol message
632 * @tsk: receiving socket
633 * @dnode: node to send response message to, if any
634 * @buf: buffer containing protocol message
635 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
636 * (CONN_PROBE_REPLY) message should be forwarded.
637 */
Wei Yongjun52f50ce2014-07-20 13:14:28 +0800638static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
639 struct sk_buff *buf)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500640{
641 struct tipc_msg *msg = buf_msg(buf);
642 struct tipc_port *port = &tsk->port;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500643 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500644
645 /* Ignore if connection cannot be validated: */
646 if (!port->connected || !tipc_port_peer_msg(port, msg))
647 goto exit;
648
649 port->probing_state = TIPC_CONN_OK;
650
651 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500652 conn_cong = tipc_sk_conn_cong(tsk);
653 tsk->sent_unacked -= msg_msgcnt(msg);
654 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400655 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500656 } else if (msg_type(msg) == CONN_PROBE) {
657 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
658 return TIPC_OK;
659 msg_set_type(msg, CONN_PROBE_REPLY);
660 return TIPC_FWD_MSG;
661 }
662 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
663exit:
664 kfree_skb(buf);
665 return TIPC_OK;
666}
667
668/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 * dest_name_check - verify user is permitted to send to specified port name
670 * @dest: destination address
671 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900672 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673 * Prevents restricted configuration commands from being issued by
674 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900675 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 * Returns 0 if permission is granted, otherwise errno
677 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800678static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679{
680 struct tipc_cfg_msg_hdr hdr;
681
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500682 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
683 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900684 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
685 return 0;
686 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
687 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900688 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
689 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690
Allan Stephens3f8dd942011-01-18 13:09:29 -0500691 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
692 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900693 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100694 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700695 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100696 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900697
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698 return 0;
699}
700
Ying Xue3f405042014-01-17 09:50:05 +0800701static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
702{
703 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400704 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800705 DEFINE_WAIT(wait);
706 int done;
707
708 do {
709 int err = sock_error(sk);
710 if (err)
711 return err;
712 if (sock->state == SS_DISCONNECTING)
713 return -EPIPE;
714 if (!*timeo_p)
715 return -EAGAIN;
716 if (signal_pending(current))
717 return sock_intr_errno(*timeo_p);
718
719 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500720 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800721 finish_wait(sk_sleep(sk), &wait);
722 } while (!done);
723 return 0;
724}
725
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500726/**
Ying Xue247f0f32014-02-18 16:06:46 +0800727 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700728 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 * @sock: socket structure
730 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500731 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900732 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900734 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
736 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900737 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100738 * Returns the number of bytes sent on success, or errno otherwise
739 */
Ying Xue247f0f32014-02-18 16:06:46 +0800740static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500741 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500743 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700744 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400745 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400746 struct tipc_port *port = &tsk->port;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500747 struct tipc_msg *mhdr = &port->phdr;
748 struct iovec *iov = m->msg_iov;
749 u32 dnode, dport;
750 struct sk_buff *buf;
751 struct tipc_name_seq *seq = &dest->addr.nameseq;
752 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800753 long timeo;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500754 int rc = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755
756 if (unlikely(!dest))
757 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500758
Allan Stephens51f9cc12006-06-25 23:49:06 -0700759 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
760 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500762
763 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400764 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100765
Allan Stephens0c3141e2008-04-15 00:22:02 -0700766 if (iocb)
767 lock_sock(sk);
768
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500769 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700770 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500771 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700772 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700773 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700774 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500775 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700776 goto exit;
777 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400778 if (tsk->port.published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500779 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700780 goto exit;
781 }
782 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400783 tsk->port.conn_type = dest->addr.name.name.type;
784 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700785 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100786 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500787 rc = dest_name_check(dest, m);
788 if (rc)
789 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100790
Ying Xue3f405042014-01-17 09:50:05 +0800791 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500792
793 if (dest->addrtype == TIPC_ADDR_MCAST) {
794 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
795 goto exit;
796 } else if (dest->addrtype == TIPC_ADDR_NAME) {
797 u32 type = dest->addr.name.name.type;
798 u32 inst = dest->addr.name.name.instance;
799 u32 domain = dest->addr.name.domain;
800
801 dnode = domain;
802 msg_set_type(mhdr, TIPC_NAMED_MSG);
803 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
804 msg_set_nametype(mhdr, type);
805 msg_set_nameinst(mhdr, inst);
806 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
807 dport = tipc_nametbl_translate(type, inst, &dnode);
808 msg_set_destnode(mhdr, dnode);
809 msg_set_destport(mhdr, dport);
810 if (unlikely(!dport && !dnode)) {
811 rc = -EHOSTUNREACH;
812 goto exit;
813 }
814 } else if (dest->addrtype == TIPC_ADDR_ID) {
815 dnode = dest->addr.id.node;
816 msg_set_type(mhdr, TIPC_DIRECT_MSG);
817 msg_set_lookup_scope(mhdr, 0);
818 msg_set_destnode(mhdr, dnode);
819 msg_set_destport(mhdr, dest->addr.id.ref);
820 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
821 }
822
823new_mtu:
824 mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400825 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500826 if (rc < 0)
827 goto exit;
828
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900829 do {
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400830 TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong;
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400831 rc = tipc_link_xmit(buf, dnode, tsk->port.ref);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500832 if (likely(rc >= 0)) {
833 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700834 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500835 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700836 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900837 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500838 if (rc == -EMSGSIZE)
839 goto new_mtu;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500840 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700841 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400842 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500843 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400844 if (rc)
845 kfree_skb_list(buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500846 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700847exit:
848 if (iocb)
849 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500850
851 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100852}
853
Ying Xue391a6dd2014-01-17 09:50:06 +0800854static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
855{
856 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400857 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800858 DEFINE_WAIT(wait);
859 int done;
860
861 do {
862 int err = sock_error(sk);
863 if (err)
864 return err;
865 if (sock->state == SS_DISCONNECTING)
866 return -EPIPE;
867 else if (sock->state != SS_CONNECTED)
868 return -ENOTCONN;
869 if (!*timeo_p)
870 return -EAGAIN;
871 if (signal_pending(current))
872 return sock_intr_errno(*timeo_p);
873
874 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
875 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -0500876 (!tsk->link_cong &&
877 !tipc_sk_conn_cong(tsk)) ||
878 !tsk->port.connected);
Ying Xue391a6dd2014-01-17 09:50:06 +0800879 finish_wait(sk_sleep(sk), &wait);
880 } while (!done);
881 return 0;
882}
883
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900884/**
Ying Xue247f0f32014-02-18 16:06:46 +0800885 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886 * @iocb: (unused)
887 * @sock: socket structure
888 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500889 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900890 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900892 *
893 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700894 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100895 */
Ying Xue247f0f32014-02-18 16:06:46 +0800896static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500897 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700899 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400900 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500901 struct tipc_port *port = &tsk->port;
902 struct tipc_msg *mhdr = &port->phdr;
903 struct sk_buff *buf;
904 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
905 u32 ref = port->ref;
906 int rc = -EINVAL;
907 long timeo;
908 u32 dnode;
909 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900910
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500911 /* Handle implied connection establishment */
912 if (unlikely(dest)) {
913 rc = tipc_sendmsg(iocb, sock, m, dsz);
914 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -0500915 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500916 return rc;
917 }
918 if (dsz > (uint)INT_MAX)
919 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700920
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500921 if (iocb)
922 lock_sock(sk);
923
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900924 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500925 if (sock->state == SS_DISCONNECTING)
926 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +0800927 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500928 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800929 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900930 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500932 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
933 dnode = tipc_port_peernode(port);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500934
935next:
936 mtu = port->max_pkt;
937 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400938 rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500939 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700940 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500941 do {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500942 if (likely(!tipc_sk_conn_cong(tsk))) {
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400943 rc = tipc_link_xmit(buf, dnode, ref);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500944 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500945 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500946 sent += send;
947 if (sent == dsz)
948 break;
949 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700950 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500951 if (rc == -EMSGSIZE) {
952 port->max_pkt = tipc_node_get_mtu(dnode, ref);
953 goto next;
954 }
955 if (rc != -ELINKCONG)
956 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400957 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500959 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400960 if (rc)
961 kfree_skb_list(buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500962 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700963exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500964 if (iocb)
965 release_sock(sk);
966 return sent ? sent : rc;
967}
968
969/**
970 * tipc_send_packet - send a connection-oriented message
971 * @iocb: if NULL, indicates that socket lock is already held
972 * @sock: socket structure
973 * @m: message to send
974 * @dsz: length of data to be transmitted
975 *
976 * Used for SOCK_SEQPACKET messages.
977 *
978 * Returns the number of bytes sent on success, or errno otherwise
979 */
980static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
981 struct msghdr *m, size_t dsz)
982{
983 if (dsz > TIPC_MAX_USER_MSG_SIZE)
984 return -EMSGSIZE;
985
986 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100987}
988
989/**
990 * auto_connect - complete connection setup to a remote port
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400991 * @tsk: tipc socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100992 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900993 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100994 * Returns 0 on success, errno otherwise
995 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400996static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100997{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400998 struct tipc_port *port = &tsk->port;
999 struct socket *sock = tsk->sk.sk_socket;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001000 struct tipc_portid peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001001
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001002 peer.ref = msg_origport(msg);
1003 peer.node = msg_orignode(msg);
1004
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001005 __tipc_port_connect(port->ref, port, &peer);
Ying Xue584d24b2012-11-29 18:51:19 -05001006
1007 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
1008 return -EINVAL;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001009 msg_set_importance(&port->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010 sock->state = SS_CONNECTED;
1011 return 0;
1012}
1013
1014/**
1015 * set_orig_addr - capture sender's address for received message
1016 * @m: descriptor for message info
1017 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001018 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001019 * Note: Address is not captured if not requested by receiver.
1020 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001021static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001022{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001023 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001025 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 addr->family = AF_TIPC;
1027 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001028 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 addr->addr.id.ref = msg_origport(msg);
1030 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001031 addr->addr.name.domain = 0; /* could leave uninitialized */
1032 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001033 m->msg_namelen = sizeof(struct sockaddr_tipc);
1034 }
1035}
1036
1037/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001038 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001039 * @m: descriptor for message info
1040 * @msg: received message header
1041 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001042 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001044 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045 * Returns 0 if successful, otherwise errno
1046 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001047static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001048 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049{
1050 u32 anc_data[3];
1051 u32 err;
1052 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001053 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054 int res;
1055
1056 if (likely(m->msg_controllen == 0))
1057 return 0;
1058
1059 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 err = msg ? msg_errcode(msg) : 0;
1061 if (unlikely(err)) {
1062 anc_data[0] = err;
1063 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001064 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1065 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001067 if (anc_data[1]) {
1068 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1069 msg_data(msg));
1070 if (res)
1071 return res;
1072 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 }
1074
1075 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1077 switch (dest_type) {
1078 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001079 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001080 anc_data[0] = msg_nametype(msg);
1081 anc_data[1] = msg_namelower(msg);
1082 anc_data[2] = msg_namelower(msg);
1083 break;
1084 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001085 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001086 anc_data[0] = msg_nametype(msg);
1087 anc_data[1] = msg_namelower(msg);
1088 anc_data[2] = msg_nameupper(msg);
1089 break;
1090 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001091 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092 anc_data[0] = tport->conn_type;
1093 anc_data[1] = tport->conn_instance;
1094 anc_data[2] = tport->conn_instance;
1095 break;
1096 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001097 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001098 }
Allan Stephens2db99832010-12-31 18:59:33 +00001099 if (has_name) {
1100 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1101 if (res)
1102 return res;
1103 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001104
1105 return 0;
1106}
1107
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001108static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001109{
1110 struct sock *sk = sock->sk;
1111 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001112 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001113 int err;
1114
1115 for (;;) {
1116 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001117 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001118 if (sock->state == SS_DISCONNECTING) {
1119 err = -ENOTCONN;
1120 break;
1121 }
1122 release_sock(sk);
1123 timeo = schedule_timeout(timeo);
1124 lock_sock(sk);
1125 }
1126 err = 0;
1127 if (!skb_queue_empty(&sk->sk_receive_queue))
1128 break;
1129 err = sock_intr_errno(timeo);
1130 if (signal_pending(current))
1131 break;
1132 err = -EAGAIN;
1133 if (!timeo)
1134 break;
1135 }
1136 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001137 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001138 return err;
1139}
1140
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001141/**
Ying Xue247f0f32014-02-18 16:06:46 +08001142 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143 * @iocb: (unused)
1144 * @m: descriptor for message info
1145 * @buf_len: total size of user buffer area
1146 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001147 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1149 * If the complete message doesn't fit in user area, truncate it.
1150 *
1151 * Returns size of returned message data, errno otherwise
1152 */
Ying Xue247f0f32014-02-18 16:06:46 +08001153static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1154 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001156 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001157 struct tipc_sock *tsk = tipc_sk(sk);
1158 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 struct sk_buff *buf;
1160 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001161 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001162 unsigned int sz;
1163 u32 err;
1164 int res;
1165
Allan Stephens0c3141e2008-04-15 00:22:02 -07001166 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 if (unlikely(!buf_len))
1168 return -EINVAL;
1169
Allan Stephens0c3141e2008-04-15 00:22:02 -07001170 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171
Allan Stephens0c3141e2008-04-15 00:22:02 -07001172 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 res = -ENOTCONN;
1174 goto exit;
1175 }
1176
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001177 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001178restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179
Allan Stephens0c3141e2008-04-15 00:22:02 -07001180 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001181 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001182 if (res)
1183 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001184
1185 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001186 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001187 msg = buf_msg(buf);
1188 sz = msg_data_sz(msg);
1189 err = msg_errcode(msg);
1190
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001193 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 goto restart;
1195 }
1196
1197 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 set_orig_addr(m, msg);
1199
1200 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001201 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001202 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 goto exit;
1204
1205 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206 if (!err) {
1207 if (unlikely(buf_len < sz)) {
1208 sz = buf_len;
1209 m->msg_flags |= MSG_TRUNC;
1210 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001211 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1212 m->msg_iov, sz);
1213 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215 res = sz;
1216 } else {
1217 if ((sock->state == SS_READY) ||
1218 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1219 res = 0;
1220 else
1221 res = -ECONNRESET;
1222 }
1223
1224 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001226 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001227 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1228 tipc_acknowledge(port->ref, tsk->rcv_unacked);
1229 tsk->rcv_unacked = 0;
1230 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001231 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001232 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001234 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235 return res;
1236}
1237
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001238/**
Ying Xue247f0f32014-02-18 16:06:46 +08001239 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240 * @iocb: (unused)
1241 * @m: descriptor for message info
1242 * @buf_len: total size of user buffer area
1243 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001244 *
1245 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001246 * will optionally wait for more; never truncates data.
1247 *
1248 * Returns size of returned message data, errno otherwise
1249 */
Ying Xue247f0f32014-02-18 16:06:46 +08001250static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1251 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001253 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001254 struct tipc_sock *tsk = tipc_sk(sk);
1255 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001256 struct sk_buff *buf;
1257 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001258 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001260 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001263 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264
1265 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001266 if (unlikely(!buf_len))
1267 return -EINVAL;
1268
Allan Stephens0c3141e2008-04-15 00:22:02 -07001269 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001270
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001271 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 res = -ENOTCONN;
1273 goto exit;
1274 }
1275
Florian Westphal3720d402010-08-17 11:00:04 +00001276 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001277 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001278
Allan Stephens0c3141e2008-04-15 00:22:02 -07001279restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001280 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001281 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001282 if (res)
1283 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001284
1285 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001286 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001287 msg = buf_msg(buf);
1288 sz = msg_data_sz(msg);
1289 err = msg_errcode(msg);
1290
1291 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001293 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001294 goto restart;
1295 }
1296
1297 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298 if (sz_copied == 0) {
1299 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001300 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001301 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001302 goto exit;
1303 }
1304
1305 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001307 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001308
Allan Stephens0232fd02011-02-21 09:45:40 -05001309 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310 needed = (buf_len - sz_copied);
1311 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001312
1313 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1314 m->msg_iov, sz_to_copy);
1315 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001317
Per Lidenb97bf3f2006-01-02 19:04:38 +01001318 sz_copied += sz_to_copy;
1319
1320 if (sz_to_copy < sz) {
1321 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001322 TIPC_SKB_CB(buf)->handle =
1323 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324 goto exit;
1325 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 } else {
1327 if (sz_copied != 0)
1328 goto exit; /* can't add error msg to valid data */
1329
1330 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1331 res = 0;
1332 else
1333 res = -ECONNRESET;
1334 }
1335
1336 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001338 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1339 tipc_acknowledge(port->ref, tsk->rcv_unacked);
1340 tsk->rcv_unacked = 0;
1341 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001342 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001343 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344
1345 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001346 if ((sz_copied < buf_len) && /* didn't get all requested data */
1347 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001348 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001349 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1350 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351 goto restart;
1352
1353exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001354 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001355 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356}
1357
1358/**
Ying Xuef288bef2012-08-21 11:16:57 +08001359 * tipc_write_space - wake up thread if port congestion is released
1360 * @sk: socket
1361 */
1362static void tipc_write_space(struct sock *sk)
1363{
1364 struct socket_wq *wq;
1365
1366 rcu_read_lock();
1367 wq = rcu_dereference(sk->sk_wq);
1368 if (wq_has_sleeper(wq))
1369 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1370 POLLWRNORM | POLLWRBAND);
1371 rcu_read_unlock();
1372}
1373
1374/**
1375 * tipc_data_ready - wake up threads to indicate messages have been received
1376 * @sk: socket
1377 * @len: the length of messages
1378 */
David S. Miller676d2362014-04-11 16:15:36 -04001379static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001380{
1381 struct socket_wq *wq;
1382
1383 rcu_read_lock();
1384 wq = rcu_dereference(sk->sk_wq);
1385 if (wq_has_sleeper(wq))
1386 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1387 POLLRDNORM | POLLRDBAND);
1388 rcu_read_unlock();
1389}
1390
1391/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001392 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001393 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001394 * @msg: message
1395 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001396 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001397 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001398static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001399{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001400 struct sock *sk = &tsk->sk;
1401 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001402 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001403 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001404
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001405 int retval = -TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001406 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001407
1408 if (msg_mcast(msg))
1409 return retval;
1410
1411 switch ((int)sock->state) {
1412 case SS_CONNECTED:
1413 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001414 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001415 if (unlikely(msg_errcode(msg))) {
1416 sock->state = SS_DISCONNECTING;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001417 __tipc_port_disconnect(port);
Ying Xue7e6c1312012-11-29 18:39:14 -05001418 }
1419 retval = TIPC_OK;
1420 }
1421 break;
1422 case SS_CONNECTING:
1423 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001424 if (unlikely(msg_errcode(msg))) {
1425 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001426 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001427 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001428 break;
1429 }
1430
1431 if (unlikely(!msg_connected(msg)))
1432 break;
1433
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001434 res = auto_connect(tsk, msg);
Ying Xue584d24b2012-11-29 18:51:19 -05001435 if (res) {
1436 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001437 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001438 retval = TIPC_OK;
1439 break;
1440 }
1441
1442 /* If an incoming message is an 'ACK-', it should be
1443 * discarded here because it doesn't contain useful
1444 * data. In addition, we should try to wake up
1445 * connect() routine if sleeping.
1446 */
1447 if (msg_data_sz(msg) == 0) {
1448 kfree_skb(*buf);
1449 *buf = NULL;
1450 if (waitqueue_active(sk_sleep(sk)))
1451 wake_up_interruptible(sk_sleep(sk));
1452 }
1453 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001454 break;
1455 case SS_LISTENING:
1456 case SS_UNCONNECTED:
1457 /* Accept only SYN message */
1458 if (!msg_connected(msg) && !(msg_errcode(msg)))
1459 retval = TIPC_OK;
1460 break;
1461 case SS_DISCONNECTING:
1462 break;
1463 default:
1464 pr_err("Unknown socket state %u\n", sock->state);
1465 }
1466 return retval;
1467}
1468
1469/**
Ying Xueaba79f32013-01-20 23:30:09 +01001470 * rcvbuf_limit - get proper overload limit of socket receive queue
1471 * @sk: socket
1472 * @buf: message
1473 *
1474 * For all connection oriented messages, irrespective of importance,
1475 * the default overload value (i.e. 67MB) is set as limit.
1476 *
1477 * For all connectionless messages, by default new queue limits are
1478 * as belows:
1479 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001480 * TIPC_LOW_IMPORTANCE (4 MB)
1481 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1482 * TIPC_HIGH_IMPORTANCE (16 MB)
1483 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001484 *
1485 * Returns overload limit according to corresponding message importance
1486 */
1487static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1488{
1489 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001490
1491 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001492 return sysctl_tipc_rmem[2];
1493
1494 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1495 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001496}
1497
1498/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001499 * filter_rcv - validate incoming message
1500 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001501 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001502 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001503 * Enqueues message on receive queue if acceptable; optionally handles
1504 * disconnect indication for a connected socket.
1505 *
1506 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001507 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001508 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001509 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001510 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001511static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001512{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001513 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001514 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001515 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001516 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001517 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001518 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001519
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001520 if (unlikely(msg_user(msg) == CONN_MANAGER))
1521 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001522
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001523 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1524 kfree_skb(buf);
1525 tsk->link_cong = 0;
1526 sk->sk_write_space(sk);
1527 return TIPC_OK;
1528 }
1529
Per Lidenb97bf3f2006-01-02 19:04:38 +01001530 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001531 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001532 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001533
Per Lidenb97bf3f2006-01-02 19:04:38 +01001534 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001535 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001536 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001537 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001538 rc = filter_connect(tsk, &buf);
1539 if (rc != TIPC_OK || buf == NULL)
1540 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001541 }
1542
1543 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001544 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001545 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001546
Ying Xueaba79f32013-01-20 23:30:09 +01001547 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001548 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001549 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001550 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001551
David S. Miller676d2362014-04-11 16:15:36 -04001552 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001553 return TIPC_OK;
1554}
1555
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001556/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001557 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001558 * @sk: socket
1559 * @buf: message
1560 *
1561 * Caller must hold socket lock, but not port lock.
1562 *
1563 * Returns 0
1564 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001565static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001566{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001567 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001568 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001569 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001570 uint truesize = buf->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001571
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001572 rc = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001573
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001574 if (likely(!rc)) {
1575 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1576 atomic_add(truesize, &tsk->dupl_rcvcnt);
1577 return 0;
1578 }
1579
1580 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1581 return 0;
1582
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001583 tipc_link_xmit(buf, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001584
Allan Stephens0c3141e2008-04-15 00:22:02 -07001585 return 0;
1586}
1587
1588/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001589 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001590 * @buf: buffer containing arriving message
1591 * Consumes buffer
1592 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001593 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001594int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001595{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001596 struct tipc_sock *tsk;
1597 struct tipc_port *port;
1598 struct sock *sk;
1599 u32 dport = msg_destport(buf_msg(buf));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001600 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001601 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001602 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001603
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001604 /* Validate destination and message */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001605 port = tipc_port_lock(dport);
1606 if (unlikely(!port)) {
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001607 rc = tipc_msg_eval(buf, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001608 goto exit;
1609 }
1610
1611 tsk = tipc_port_to_sock(port);
1612 sk = &tsk->sk;
1613
1614 /* Queue message */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001615 bh_lock_sock(sk);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001616
Allan Stephens0c3141e2008-04-15 00:22:02 -07001617 if (!sock_owned_by_user(sk)) {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001618 rc = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001619 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001620 if (sk->sk_backlog.len == 0)
1621 atomic_set(&tsk->dupl_rcvcnt, 0);
1622 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1623 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001624 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001625 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001626 bh_unlock_sock(sk);
1627 tipc_port_unlock(port);
1628
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001629 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001630 return 0;
1631exit:
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001632 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001633 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001634
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001635 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001636 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001637}
1638
Ying Xue78eb3a52014-01-17 09:50:03 +08001639static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1640{
1641 struct sock *sk = sock->sk;
1642 DEFINE_WAIT(wait);
1643 int done;
1644
1645 do {
1646 int err = sock_error(sk);
1647 if (err)
1648 return err;
1649 if (!*timeo_p)
1650 return -ETIMEDOUT;
1651 if (signal_pending(current))
1652 return sock_intr_errno(*timeo_p);
1653
1654 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1655 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1656 finish_wait(sk_sleep(sk), &wait);
1657 } while (!done);
1658 return 0;
1659}
1660
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661/**
Ying Xue247f0f32014-02-18 16:06:46 +08001662 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663 * @sock: socket structure
1664 * @dest: socket address for destination port
1665 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001666 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001667 *
1668 * Returns 0 on success, errno otherwise
1669 */
Ying Xue247f0f32014-02-18 16:06:46 +08001670static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1671 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001672{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001673 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001674 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1675 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001676 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1677 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001678 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679
Allan Stephens0c3141e2008-04-15 00:22:02 -07001680 lock_sock(sk);
1681
Allan Stephensb89741a2008-04-15 00:20:37 -07001682 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001683 if (sock->state == SS_READY) {
1684 res = -EOPNOTSUPP;
1685 goto exit;
1686 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001687
Allan Stephensb89741a2008-04-15 00:20:37 -07001688 /*
1689 * Reject connection attempt using multicast address
1690 *
1691 * Note: send_msg() validates the rest of the address fields,
1692 * so there's no need to do it here
1693 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001694 if (dst->addrtype == TIPC_ADDR_MCAST) {
1695 res = -EINVAL;
1696 goto exit;
1697 }
1698
Ying Xue78eb3a52014-01-17 09:50:03 +08001699 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001700 switch (sock->state) {
1701 case SS_UNCONNECTED:
1702 /* Send a 'SYN-' to destination */
1703 m.msg_name = dest;
1704 m.msg_namelen = destlen;
1705
1706 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1707 * indicate send_msg() is never blocked.
1708 */
1709 if (!timeout)
1710 m.msg_flags = MSG_DONTWAIT;
1711
Ying Xue247f0f32014-02-18 16:06:46 +08001712 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001713 if ((res < 0) && (res != -EWOULDBLOCK))
1714 goto exit;
1715
1716 /* Just entered SS_CONNECTING state; the only
1717 * difference is that return value in non-blocking
1718 * case is EINPROGRESS, rather than EALREADY.
1719 */
1720 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001721 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001722 if (previous == SS_CONNECTING)
1723 res = -EALREADY;
1724 if (!timeout)
1725 goto exit;
1726 timeout = msecs_to_jiffies(timeout);
1727 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1728 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001729 break;
1730 case SS_CONNECTED:
1731 res = -EISCONN;
1732 break;
1733 default:
1734 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001735 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001736 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001737exit:
1738 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001739 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001740}
1741
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001742/**
Ying Xue247f0f32014-02-18 16:06:46 +08001743 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001744 * @sock: socket structure
1745 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001746 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001747 * Returns 0 on success, errno otherwise
1748 */
Ying Xue247f0f32014-02-18 16:06:46 +08001749static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001750{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001751 struct sock *sk = sock->sk;
1752 int res;
1753
1754 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001755
Ying Xue245f3d32011-07-06 06:01:13 -04001756 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001757 res = -EINVAL;
1758 else {
1759 sock->state = SS_LISTENING;
1760 res = 0;
1761 }
1762
1763 release_sock(sk);
1764 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001765}
1766
Ying Xue6398e232014-01-17 09:50:04 +08001767static int tipc_wait_for_accept(struct socket *sock, long timeo)
1768{
1769 struct sock *sk = sock->sk;
1770 DEFINE_WAIT(wait);
1771 int err;
1772
1773 /* True wake-one mechanism for incoming connections: only
1774 * one process gets woken up, not the 'whole herd'.
1775 * Since we do not 'race & poll' for established sockets
1776 * anymore, the common case will execute the loop only once.
1777 */
1778 for (;;) {
1779 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1780 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001781 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001782 release_sock(sk);
1783 timeo = schedule_timeout(timeo);
1784 lock_sock(sk);
1785 }
1786 err = 0;
1787 if (!skb_queue_empty(&sk->sk_receive_queue))
1788 break;
1789 err = -EINVAL;
1790 if (sock->state != SS_LISTENING)
1791 break;
1792 err = sock_intr_errno(timeo);
1793 if (signal_pending(current))
1794 break;
1795 err = -EAGAIN;
1796 if (!timeo)
1797 break;
1798 }
1799 finish_wait(sk_sleep(sk), &wait);
1800 return err;
1801}
1802
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001803/**
Ying Xue247f0f32014-02-18 16:06:46 +08001804 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001805 * @sock: listening socket
1806 * @newsock: new socket that is to be connected
1807 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001808 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001809 * Returns 0 on success, errno otherwise
1810 */
Ying Xue247f0f32014-02-18 16:06:46 +08001811static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001812{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001813 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001815 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001816 struct tipc_msg *msg;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001817 struct tipc_portid peer;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001818 u32 new_ref;
Ying Xue6398e232014-01-17 09:50:04 +08001819 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001820 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001821
Allan Stephens0c3141e2008-04-15 00:22:02 -07001822 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001823
Allan Stephens0c3141e2008-04-15 00:22:02 -07001824 if (sock->state != SS_LISTENING) {
1825 res = -EINVAL;
1826 goto exit;
1827 }
Ying Xue6398e232014-01-17 09:50:04 +08001828 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1829 res = tipc_wait_for_accept(sock, timeo);
1830 if (res)
1831 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001832
1833 buf = skb_peek(&sk->sk_receive_queue);
1834
Ying Xuec5fa7b32013-06-17 10:54:39 -04001835 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001836 if (res)
1837 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001838
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001839 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001840 new_port = &tipc_sk(new_sk)->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001841 new_ref = new_port->ref;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001842 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001843
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001844 /* we lock on new_sk; but lockdep sees the lock on sk */
1845 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001846
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001847 /*
1848 * Reject any stray messages received by new socket
1849 * before the socket lock was taken (very, very unlikely)
1850 */
1851 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001852
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001853 /* Connect new socket to it's peer */
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001854 peer.ref = msg_origport(msg);
1855 peer.node = msg_orignode(msg);
1856 tipc_port_connect(new_ref, &peer);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001857 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001858
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001859 tipc_port_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001860 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001861 new_port->conn_type = msg_nametype(msg);
1862 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001863 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001864
1865 /*
1866 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1867 * Respond to 'SYN+' by queuing it on new socket.
1868 */
1869 if (!msg_data_sz(msg)) {
1870 struct msghdr m = {NULL,};
1871
1872 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001873 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001874 } else {
1875 __skb_dequeue(&sk->sk_receive_queue);
1876 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001877 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001878 }
1879 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001880exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001881 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001882 return res;
1883}
1884
1885/**
Ying Xue247f0f32014-02-18 16:06:46 +08001886 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001887 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001888 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001889 *
1890 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001891 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001892 * Returns 0 on success, errno otherwise
1893 */
Ying Xue247f0f32014-02-18 16:06:46 +08001894static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001895{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001896 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001897 struct tipc_sock *tsk = tipc_sk(sk);
1898 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001899 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001900 u32 peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001901 int res;
1902
Allan Stephense247a8f2008-03-06 15:05:38 -08001903 if (how != SHUT_RDWR)
1904 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001905
Allan Stephens0c3141e2008-04-15 00:22:02 -07001906 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907
1908 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001909 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001910 case SS_CONNECTED:
1911
Per Lidenb97bf3f2006-01-02 19:04:38 +01001912restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001913 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001914 buf = __skb_dequeue(&sk->sk_receive_queue);
1915 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001916 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001917 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001918 goto restart;
1919 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001920 tipc_port_disconnect(port->ref);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001921 if (tipc_msg_reverse(buf, &peer, TIPC_CONN_SHUTDOWN))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001922 tipc_link_xmit(buf, peer, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001923 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001924 tipc_port_shutdown(port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001925 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001926
1927 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001928
1929 /* fall through */
1930
1931 case SS_DISCONNECTING:
1932
Ying Xue75031152012-10-29 09:38:15 -04001933 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001934 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001935
1936 /* Wake up anyone sleeping in poll */
1937 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001938 res = 0;
1939 break;
1940
1941 default:
1942 res = -ENOTCONN;
1943 }
1944
Allan Stephens0c3141e2008-04-15 00:22:02 -07001945 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001946 return res;
1947}
1948
1949/**
Ying Xue247f0f32014-02-18 16:06:46 +08001950 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001951 * @sock: socket structure
1952 * @lvl: option level
1953 * @opt: option identifier
1954 * @ov: pointer to new option value
1955 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001956 *
1957 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001958 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001959 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001960 * Returns 0 on success, errno otherwise
1961 */
Ying Xue247f0f32014-02-18 16:06:46 +08001962static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1963 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001964{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001965 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001966 struct tipc_sock *tsk = tipc_sk(sk);
1967 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001968 u32 value;
1969 int res;
1970
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001971 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1972 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001973 if (lvl != SOL_TIPC)
1974 return -ENOPROTOOPT;
1975 if (ol < sizeof(value))
1976 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001977 res = get_user(value, (u32 __user *)ov);
1978 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001979 return res;
1980
Allan Stephens0c3141e2008-04-15 00:22:02 -07001981 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001982
Per Lidenb97bf3f2006-01-02 19:04:38 +01001983 switch (opt) {
1984 case TIPC_IMPORTANCE:
Erik Hugneac32c7f2014-08-15 16:44:35 +02001985 res = tipc_port_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001986 break;
1987 case TIPC_SRC_DROPPABLE:
1988 if (sock->type != SOCK_STREAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001989 tipc_port_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001990 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001991 res = -ENOPROTOOPT;
1992 break;
1993 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001994 tipc_port_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001995 break;
1996 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001997 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001998 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001999 break;
2000 default:
2001 res = -EINVAL;
2002 }
2003
Allan Stephens0c3141e2008-04-15 00:22:02 -07002004 release_sock(sk);
2005
Per Lidenb97bf3f2006-01-02 19:04:38 +01002006 return res;
2007}
2008
2009/**
Ying Xue247f0f32014-02-18 16:06:46 +08002010 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002011 * @sock: socket structure
2012 * @lvl: option level
2013 * @opt: option identifier
2014 * @ov: receptacle for option value
2015 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002016 *
2017 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002018 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002019 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002020 * Returns 0 on success, errno otherwise
2021 */
Ying Xue247f0f32014-02-18 16:06:46 +08002022static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2023 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002024{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002025 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002026 struct tipc_sock *tsk = tipc_sk(sk);
2027 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002028 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002029 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002030 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002031
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002032 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2033 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002034 if (lvl != SOL_TIPC)
2035 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002036 res = get_user(len, ol);
2037 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002038 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002039
Allan Stephens0c3141e2008-04-15 00:22:02 -07002040 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002041
2042 switch (opt) {
2043 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002044 value = tipc_port_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002045 break;
2046 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002047 value = tipc_port_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002048 break;
2049 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002050 value = tipc_port_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002051 break;
2052 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002053 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002054 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002055 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002056 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002057 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002058 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002059 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002060 value = skb_queue_len(&sk->sk_receive_queue);
2061 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002062 default:
2063 res = -EINVAL;
2064 }
2065
Allan Stephens0c3141e2008-04-15 00:22:02 -07002066 release_sock(sk);
2067
Paul Gortmaker25860c32010-12-31 18:59:31 +00002068 if (res)
2069 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002070
Paul Gortmaker25860c32010-12-31 18:59:31 +00002071 if (len < sizeof(value))
2072 return -EINVAL;
2073
2074 if (copy_to_user(ov, &value, sizeof(value)))
2075 return -EFAULT;
2076
2077 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002078}
2079
Wei Yongjun52f50ce2014-07-20 13:14:28 +08002080static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002081{
2082 struct tipc_sioc_ln_req lnr;
2083 void __user *argp = (void __user *)arg;
2084
2085 switch (cmd) {
2086 case SIOCGETLINKNAME:
2087 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2088 return -EFAULT;
2089 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
2090 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2091 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2092 return -EFAULT;
2093 return 0;
2094 }
2095 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002096 default:
2097 return -ENOIOCTLCMD;
2098 }
2099}
2100
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002101/* Protocol switches for the various types of TIPC sockets */
2102
Florian Westphalbca65ea2008-02-07 18:18:01 -08002103static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002104 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002105 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002106 .release = tipc_release,
2107 .bind = tipc_bind,
2108 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002109 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002110 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002111 .getname = tipc_getname,
2112 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002113 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002114 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002115 .shutdown = tipc_shutdown,
2116 .setsockopt = tipc_setsockopt,
2117 .getsockopt = tipc_getsockopt,
2118 .sendmsg = tipc_sendmsg,
2119 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002120 .mmap = sock_no_mmap,
2121 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002122};
2123
Florian Westphalbca65ea2008-02-07 18:18:01 -08002124static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002125 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002126 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002127 .release = tipc_release,
2128 .bind = tipc_bind,
2129 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002130 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002131 .accept = tipc_accept,
2132 .getname = tipc_getname,
2133 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002134 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002135 .listen = tipc_listen,
2136 .shutdown = tipc_shutdown,
2137 .setsockopt = tipc_setsockopt,
2138 .getsockopt = tipc_getsockopt,
2139 .sendmsg = tipc_send_packet,
2140 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002141 .mmap = sock_no_mmap,
2142 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002143};
2144
Florian Westphalbca65ea2008-02-07 18:18:01 -08002145static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002146 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002147 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002148 .release = tipc_release,
2149 .bind = tipc_bind,
2150 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002151 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002152 .accept = tipc_accept,
2153 .getname = tipc_getname,
2154 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002155 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002156 .listen = tipc_listen,
2157 .shutdown = tipc_shutdown,
2158 .setsockopt = tipc_setsockopt,
2159 .getsockopt = tipc_getsockopt,
2160 .sendmsg = tipc_send_stream,
2161 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002162 .mmap = sock_no_mmap,
2163 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002164};
2165
Florian Westphalbca65ea2008-02-07 18:18:01 -08002166static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002167 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002168 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002169 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002170};
2171
2172static struct proto tipc_proto = {
2173 .name = "TIPC",
2174 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002175 .obj_size = sizeof(struct tipc_sock),
2176 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002177};
2178
Ying Xuec5fa7b32013-06-17 10:54:39 -04002179static struct proto tipc_proto_kern = {
2180 .name = "TIPC",
2181 .obj_size = sizeof(struct tipc_sock),
2182 .sysctl_rmem = sysctl_tipc_rmem
2183};
2184
Per Lidenb97bf3f2006-01-02 19:04:38 +01002185/**
Per Liden4323add2006-01-18 00:38:21 +01002186 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002187 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002188 * Returns 0 on success, errno otherwise
2189 */
Per Liden4323add2006-01-18 00:38:21 +01002190int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002191{
2192 int res;
2193
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002194 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002195 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002196 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002197 goto out;
2198 }
2199
2200 res = sock_register(&tipc_family_ops);
2201 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002202 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002203 proto_unregister(&tipc_proto);
2204 goto out;
2205 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002206 out:
2207 return res;
2208}
2209
2210/**
Per Liden4323add2006-01-18 00:38:21 +01002211 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002212 */
Per Liden4323add2006-01-18 00:38:21 +01002213void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002214{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002215 sock_unregister(tipc_family_ops.family);
2216 proto_unregister(&tipc_proto);
2217}