blob: 1762323156af262cb9cfdbd10d7c33fe51249288 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04004 * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000038#include "port.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050039#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020040#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050041#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040042#include <linux/export.h>
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -050043#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040044
Per Lidenb97bf3f2006-01-02 19:04:38 +010045#define SS_LISTENING -1 /* socket is listening */
46#define SS_READY -2 /* socket is connectionless */
47
Allan Stephens3654ea02008-04-13 21:35:11 -070048#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Jon Paul Maloyac0074e2014-06-25 20:41:41 -050049#define TIPC_FWD_MSG 1
Per Lidenb97bf3f2006-01-02 19:04:38 +010050
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -040051static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -040052static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +080053static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080054static int tipc_release(struct socket *sock);
55static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Per Lidenb97bf3f2006-01-02 19:04:38 +010056
Florian Westphalbca65ea2008-02-07 18:18:01 -080057static const struct proto_ops packet_ops;
58static const struct proto_ops stream_ops;
59static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010060
61static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040062static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010063
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090064/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070065 * Revised TIPC socket locking policy:
66 *
67 * Most socket operations take the standard socket lock when they start
68 * and hold it until they finish (or until they need to sleep). Acquiring
69 * this lock grants the owner exclusive access to the fields of the socket
70 * data structures, with the exception of the backlog queue. A few socket
71 * operations can be done without taking the socket lock because they only
72 * read socket information that never changes during the life of the socket.
73 *
74 * Socket operations may acquire the lock for the associated TIPC port if they
75 * need to perform an operation on the port. If any routine needs to acquire
76 * both the socket lock and the port lock it must take the socket lock first
77 * to avoid the risk of deadlock.
78 *
79 * The dispatcher handling incoming messages cannot grab the socket lock in
80 * the standard fashion, since invoked it runs at the BH level and cannot block.
81 * Instead, it checks to see if the socket lock is currently owned by someone,
82 * and either handles the message itself or adds it to the socket's backlog
83 * queue; in the latter case the queued message is processed once the process
84 * owning the socket lock releases it.
85 *
86 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
87 * the problem of a blocked socket operation preventing any other operations
88 * from occurring. However, applications must be careful if they have
89 * multiple threads trying to send (or receive) on the same socket, as these
90 * operations might interfere with each other. For example, doing a connect
91 * and a receive at the same time might allow the receive to consume the
92 * ACK message meant for the connect. While additional work could be done
93 * to try and overcome this, it doesn't seem to be worthwhile at the present.
94 *
95 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
96 * that another operation that must be performed in a non-blocking manner is
97 * not delayed for very long because the lock has already been taken.
98 *
99 * NOTE: This code assumes that certain fields of a port/socket pair are
100 * constant over its lifetime; such fields can be examined without taking
101 * the socket lock and/or port lock, and do not need to be re-read even
102 * after resuming processing after waiting. These fields include:
103 * - socket type
104 * - pointer to socket sk structure (aka tipc_sock structure)
105 * - pointer to port structure
106 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400109#include "socket.h"
110
Per Lidenb97bf3f2006-01-02 19:04:38 +0100111/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700112 * advance_rx_queue - discard first buffer in socket receive queue
113 *
114 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100115 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700116static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100117{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400118 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119}
120
121/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700122 * reject_rx_queue - reject all buffers in socket receive queue
123 *
124 * Caller must hold socket lock
125 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700126static void reject_rx_queue(struct sock *sk)
127{
128 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500129 u32 dnode;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700130
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500131 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
132 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
133 tipc_link_xmit2(buf, dnode, 0);
134 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700135}
136
137/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400138 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700139 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140 * @sock: pre-allocated socket structure
141 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800142 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900143 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700144 * This routine creates additional data structures used by the TIPC socket,
145 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146 *
147 * Returns 0 on success, errno otherwise
148 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400149static int tipc_sk_create(struct net *net, struct socket *sock,
150 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700152 const struct proto_ops *ops;
153 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400155 struct tipc_sock *tsk;
156 struct tipc_port *port;
157 u32 ref;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700158
159 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 if (unlikely(protocol != 0))
161 return -EPROTONOSUPPORT;
162
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163 switch (sock->type) {
164 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700165 ops = &stream_ops;
166 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 break;
168 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700169 ops = &packet_ops;
170 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171 break;
172 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700174 ops = &msg_ops;
175 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176 break;
Allan Stephens49978652006-06-25 23:47:18 -0700177 default:
Allan Stephens49978652006-06-25 23:47:18 -0700178 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179 }
180
Allan Stephens0c3141e2008-04-15 00:22:02 -0700181 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400182 if (!kern)
183 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
184 else
185 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
186
Allan Stephens0c3141e2008-04-15 00:22:02 -0700187 if (sk == NULL)
188 return -ENOMEM;
189
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400190 tsk = tipc_sk(sk);
191 port = &tsk->port;
192
193 ref = tipc_port_init(port, TIPC_LOW_IMPORTANCE);
194 if (!ref) {
195 pr_warn("Socket registration failed, ref. table exhausted\n");
Allan Stephens0c3141e2008-04-15 00:22:02 -0700196 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197 return -ENOMEM;
198 }
199
Allan Stephens0c3141e2008-04-15 00:22:02 -0700200 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700201 sock->ops = ops;
202 sock->state = state;
203
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204 sock_init_data(sock, sk);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400205 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400206 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800207 sk->sk_data_ready = tipc_data_ready;
208 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400209 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500210 tsk->port.sent = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400211 atomic_set(&tsk->dupl_rcvcnt, 0);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400212 tipc_port_unlock(port);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700213
Allan Stephens0c3141e2008-04-15 00:22:02 -0700214 if (sock->state == SS_READY) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400215 tipc_port_set_unreturnable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700216 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400217 tipc_port_set_unreliable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700218 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219 return 0;
220}
221
222/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400223 * tipc_sock_create_local - create TIPC socket from inside TIPC module
224 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
225 *
226 * We cannot use sock_creat_kern here because it bumps module user count.
227 * Since socket owner and creator is the same module we must make sure
228 * that module count remains zero for module local sockets, otherwise
229 * we cannot do rmmod.
230 *
231 * Returns 0 on success, errno otherwise
232 */
233int tipc_sock_create_local(int type, struct socket **res)
234{
235 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400236
237 rc = sock_create_lite(AF_TIPC, type, 0, res);
238 if (rc < 0) {
239 pr_err("Failed to create kernel socket\n");
240 return rc;
241 }
242 tipc_sk_create(&init_net, *res, 0, 1);
243
Ying Xuec5fa7b32013-06-17 10:54:39 -0400244 return 0;
245}
246
247/**
248 * tipc_sock_release_local - release socket created by tipc_sock_create_local
249 * @sock: the socket to be released.
250 *
251 * Module reference count is not incremented when such sockets are created,
252 * so we must keep it from being decremented when they are released.
253 */
254void tipc_sock_release_local(struct socket *sock)
255{
Ying Xue247f0f32014-02-18 16:06:46 +0800256 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400257 sock->ops = NULL;
258 sock_release(sock);
259}
260
261/**
262 * tipc_sock_accept_local - accept a connection on a socket created
263 * with tipc_sock_create_local. Use this function to avoid that
264 * module reference count is inadvertently incremented.
265 *
266 * @sock: the accepting socket
267 * @newsock: reference to the new socket to be created
268 * @flags: socket flags
269 */
270
271int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400272 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400273{
274 struct sock *sk = sock->sk;
275 int ret;
276
277 ret = sock_create_lite(sk->sk_family, sk->sk_type,
278 sk->sk_protocol, newsock);
279 if (ret < 0)
280 return ret;
281
Ying Xue247f0f32014-02-18 16:06:46 +0800282 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400283 if (ret < 0) {
284 sock_release(*newsock);
285 return ret;
286 }
287 (*newsock)->ops = sock->ops;
288 return ret;
289}
290
291/**
Ying Xue247f0f32014-02-18 16:06:46 +0800292 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293 * @sock: socket to destroy
294 *
295 * This routine cleans up any messages that are still queued on the socket.
296 * For DGRAM and RDM socket types, all queued messages are rejected.
297 * For SEQPACKET and STREAM socket types, the first message is rejected
298 * and any others are discarded. (If the first message on a STREAM socket
299 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900300 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301 * NOTE: Rejected messages are not necessarily returned to the sender! They
302 * are returned or discarded according to the "destination droppable" setting
303 * specified for the message by the sender.
304 *
305 * Returns 0 on success, errno otherwise
306 */
Ying Xue247f0f32014-02-18 16:06:46 +0800307static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400310 struct tipc_sock *tsk;
311 struct tipc_port *port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500313 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314
Allan Stephens0c3141e2008-04-15 00:22:02 -0700315 /*
316 * Exit if socket isn't fully initialized (occurs when a failed accept()
317 * releases a pre-allocated child socket that was never used)
318 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700319 if (sk == NULL)
320 return 0;
321
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400322 tsk = tipc_sk(sk);
323 port = &tsk->port;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700324 lock_sock(sk);
325
326 /*
327 * Reject all unreceived messages, except on an active connection
328 * (which disconnects locally & sends a 'FIN+' to peer)
329 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700331 buf = __skb_dequeue(&sk->sk_receive_queue);
332 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 break;
Ying Xue40682432013-10-18 07:23:16 +0200334 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400335 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700336 else {
337 if ((sock->state == SS_CONNECTING) ||
338 (sock->state == SS_CONNECTED)) {
339 sock->state = SS_DISCONNECTING;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400340 tipc_port_disconnect(port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700341 }
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500342 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
343 tipc_link_xmit2(buf, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700344 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 }
346
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400347 /* Destroy TIPC port; also disconnects an active connection and
348 * sends a 'FIN-' to peer.
Allan Stephens0c3141e2008-04-15 00:22:02 -0700349 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400350 tipc_port_destroy(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351
Allan Stephens0c3141e2008-04-15 00:22:02 -0700352 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100353 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354
Allan Stephens0c3141e2008-04-15 00:22:02 -0700355 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700356 sock->state = SS_DISCONNECTING;
357 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358
359 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700360 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200362 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363}
364
365/**
Ying Xue247f0f32014-02-18 16:06:46 +0800366 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367 * @sock: socket structure
368 * @uaddr: socket address describing name(s) and desired operation
369 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900370 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100371 * Name and name sequence binding is indicated using a positive scope value;
372 * a negative scope value unbinds the specified name. Specifying no name
373 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900374 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700376 *
377 * NOTE: This routine doesn't need to take the socket lock since it doesn't
378 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 */
Ying Xue247f0f32014-02-18 16:06:46 +0800380static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
381 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382{
Ying Xue84602762013-12-27 10:18:28 +0800383 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400385 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800386 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387
Ying Xue84602762013-12-27 10:18:28 +0800388 lock_sock(sk);
389 if (unlikely(!uaddr_len)) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400390 res = tipc_withdraw(&tsk->port, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800391 goto exit;
392 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900393
Ying Xue84602762013-12-27 10:18:28 +0800394 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
395 res = -EINVAL;
396 goto exit;
397 }
398 if (addr->family != AF_TIPC) {
399 res = -EAFNOSUPPORT;
400 goto exit;
401 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100402
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 if (addr->addrtype == TIPC_ADDR_NAME)
404 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800405 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
406 res = -EAFNOSUPPORT;
407 goto exit;
408 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900409
Ying Xue13a2e892013-06-17 10:54:40 -0400410 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400411 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800412 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
413 res = -EACCES;
414 goto exit;
415 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400416
Ying Xue84602762013-12-27 10:18:28 +0800417 res = (addr->scope > 0) ?
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400418 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
419 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800420exit:
421 release_sock(sk);
422 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423}
424
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900425/**
Ying Xue247f0f32014-02-18 16:06:46 +0800426 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 * @sock: socket structure
428 * @uaddr: area for returned socket address
429 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700430 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900431 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700433 *
Allan Stephens2da59912008-07-14 22:43:32 -0700434 * NOTE: This routine doesn't need to take the socket lock since it only
435 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000436 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437 */
Ying Xue247f0f32014-02-18 16:06:46 +0800438static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
439 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400442 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000444 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700445 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700446 if ((sock->state != SS_CONNECTED) &&
447 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
448 return -ENOTCONN;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400449 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
450 addr->addr.id.node = tipc_port_peernode(&tsk->port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700451 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400452 addr->addr.id.ref = tsk->port.ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000453 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700454 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455
456 *uaddr_len = sizeof(*addr);
457 addr->addrtype = TIPC_ADDR_ID;
458 addr->family = AF_TIPC;
459 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 addr->addr.name.domain = 0;
461
Allan Stephens0c3141e2008-04-15 00:22:02 -0700462 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463}
464
465/**
Ying Xue247f0f32014-02-18 16:06:46 +0800466 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467 * @file: file structure associated with the socket
468 * @sock: socket for which to calculate the poll bits
469 * @wait: ???
470 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700471 * Returns pollmask value
472 *
473 * COMMENTARY:
474 * It appears that the usual socket locking mechanisms are not useful here
475 * since the pollmask info is potentially out-of-date the moment this routine
476 * exits. TCP and other protocols seem to rely on higher level poll routines
477 * to handle any preventable race conditions, so TIPC will do the same ...
478 *
479 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700480 *
Allan Stephensf662c072010-08-17 11:00:06 +0000481 * socket state flags set
482 * ------------ ---------
483 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200484 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000485 *
486 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
487 * no write flags
488 *
489 * connected POLLIN/POLLRDNORM if data in rx queue
490 * POLLOUT if port is not congested
491 *
492 * disconnecting POLLIN/POLLRDNORM/POLLHUP
493 * no write flags
494 *
495 * listening POLLIN if SYN in rx queue
496 * no write flags
497 *
498 * ready POLLIN/POLLRDNORM if data in rx queue
499 * [connectionless] POLLOUT (since port cannot be congested)
500 *
501 * IMPORTANT: The fact that a read or write operation is indicated does NOT
502 * imply that the operation will succeed, merely that it should be performed
503 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504 */
Ying Xue247f0f32014-02-18 16:06:46 +0800505static unsigned int tipc_poll(struct file *file, struct socket *sock,
506 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507{
Allan Stephens9b674e82008-03-26 16:48:21 -0700508 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400509 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000510 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700511
Ying Xuef288bef2012-08-21 11:16:57 +0800512 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700513
Allan Stephensf662c072010-08-17 11:00:06 +0000514 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200515 case SS_UNCONNECTED:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400516 if (!tsk->port.congested)
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 Maloy58ed9442014-03-12 11:31:12 -0400521 if (!tsk->port.congested)
Allan Stephensf662c072010-08-17 11:00:06 +0000522 mask |= POLLOUT;
523 /* fall thru' */
524 case SS_CONNECTING:
525 case SS_LISTENING:
526 if (!skb_queue_empty(&sk->sk_receive_queue))
527 mask |= (POLLIN | POLLRDNORM);
528 break;
529 case SS_DISCONNECTING:
530 mask = (POLLIN | POLLRDNORM | POLLHUP);
531 break;
532 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700533
534 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535}
536
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900537/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500538 * tipc_sk_proto_rcv - receive a connection mng protocol message
539 * @tsk: receiving socket
540 * @dnode: node to send response message to, if any
541 * @buf: buffer containing protocol message
542 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
543 * (CONN_PROBE_REPLY) message should be forwarded.
544 */
545int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode, struct sk_buff *buf)
546{
547 struct tipc_msg *msg = buf_msg(buf);
548 struct tipc_port *port = &tsk->port;
549 int wakeable;
550
551 /* Ignore if connection cannot be validated: */
552 if (!port->connected || !tipc_port_peer_msg(port, msg))
553 goto exit;
554
555 port->probing_state = TIPC_CONN_OK;
556
557 if (msg_type(msg) == CONN_ACK) {
558 wakeable = tipc_port_congested(port) && port->congested;
559 port->acked += msg_msgcnt(msg);
560 if (!tipc_port_congested(port)) {
561 port->congested = 0;
562 if (wakeable)
563 tipc_port_wakeup(port);
564 }
565 } else if (msg_type(msg) == CONN_PROBE) {
566 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
567 return TIPC_OK;
568 msg_set_type(msg, CONN_PROBE_REPLY);
569 return TIPC_FWD_MSG;
570 }
571 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
572exit:
573 kfree_skb(buf);
574 return TIPC_OK;
575}
576
577/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 * dest_name_check - verify user is permitted to send to specified port name
579 * @dest: destination address
580 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900581 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 * Prevents restricted configuration commands from being issued by
583 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900584 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 * Returns 0 if permission is granted, otherwise errno
586 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800587static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588{
589 struct tipc_cfg_msg_hdr hdr;
590
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500591 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
592 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900593 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
594 return 0;
595 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
596 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900597 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
598 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599
Allan Stephens3f8dd942011-01-18 13:09:29 -0500600 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
601 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900602 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700604 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900606
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 return 0;
608}
609
Ying Xue3f405042014-01-17 09:50:05 +0800610static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
611{
612 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400613 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800614 DEFINE_WAIT(wait);
615 int done;
616
617 do {
618 int err = sock_error(sk);
619 if (err)
620 return err;
621 if (sock->state == SS_DISCONNECTING)
622 return -EPIPE;
623 if (!*timeo_p)
624 return -EAGAIN;
625 if (signal_pending(current))
626 return sock_intr_errno(*timeo_p);
627
628 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400629 done = sk_wait_event(sk, timeo_p, !tsk->port.congested);
Ying Xue3f405042014-01-17 09:50:05 +0800630 finish_wait(sk_sleep(sk), &wait);
631 } while (!done);
632 return 0;
633}
634
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500635/**
636 * tipc_sendmcast - send multicast message
637 * @sock: socket structure
638 * @seq: destination address
639 * @iov: message data to send
640 * @dsz: total length of message data
641 * @timeo: timeout to wait for wakeup
642 *
643 * Called from function tipc_sendmsg(), which has done all sanity checks
644 * Returns the number of bytes sent on success, or errno
645 */
646static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
647 struct iovec *iov, size_t dsz, long timeo)
648{
649 struct sock *sk = sock->sk;
650 struct tipc_sock *tsk = tipc_sk(sk);
651 int rc;
652
653 do {
654 if (sock->state != SS_READY) {
655 rc = -EOPNOTSUPP;
656 break;
657 }
658 rc = tipc_port_mcast_xmit(&tsk->port, seq, iov, dsz);
659 if (likely(rc >= 0)) {
660 if (sock->state != SS_READY)
661 sock->state = SS_CONNECTING;
662 break;
663 }
664 if (rc != -ELINKCONG)
665 break;
666 rc = tipc_wait_for_sndmsg(sock, &timeo);
667 } while (!rc);
668
669 return rc;
670}
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400671
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672/**
Ying Xue247f0f32014-02-18 16:06:46 +0800673 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700674 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675 * @sock: socket structure
676 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500677 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900678 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900680 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
682 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900683 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100684 * Returns the number of bytes sent on success, or errno otherwise
685 */
Ying Xue247f0f32014-02-18 16:06:46 +0800686static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500687 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100688{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500689 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700690 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400691 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400692 struct tipc_port *port = &tsk->port;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500693 struct tipc_msg *mhdr = &port->phdr;
694 struct iovec *iov = m->msg_iov;
695 u32 dnode, dport;
696 struct sk_buff *buf;
697 struct tipc_name_seq *seq = &dest->addr.nameseq;
698 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800699 long timeo;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500700 int rc = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100701
702 if (unlikely(!dest))
703 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500704
Allan Stephens51f9cc12006-06-25 23:49:06 -0700705 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
706 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100707 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500708
709 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400710 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711
Allan Stephens0c3141e2008-04-15 00:22:02 -0700712 if (iocb)
713 lock_sock(sk);
714
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500715 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700716 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500717 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700718 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700719 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700720 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500721 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700722 goto exit;
723 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400724 if (tsk->port.published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500725 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700726 goto exit;
727 }
728 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400729 tsk->port.conn_type = dest->addr.name.name.type;
730 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700731 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500733 rc = dest_name_check(dest, m);
734 if (rc)
735 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736
Ying Xue3f405042014-01-17 09:50:05 +0800737 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500738
739 if (dest->addrtype == TIPC_ADDR_MCAST) {
740 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
741 goto exit;
742 } else if (dest->addrtype == TIPC_ADDR_NAME) {
743 u32 type = dest->addr.name.name.type;
744 u32 inst = dest->addr.name.name.instance;
745 u32 domain = dest->addr.name.domain;
746
747 dnode = domain;
748 msg_set_type(mhdr, TIPC_NAMED_MSG);
749 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
750 msg_set_nametype(mhdr, type);
751 msg_set_nameinst(mhdr, inst);
752 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
753 dport = tipc_nametbl_translate(type, inst, &dnode);
754 msg_set_destnode(mhdr, dnode);
755 msg_set_destport(mhdr, dport);
756 if (unlikely(!dport && !dnode)) {
757 rc = -EHOSTUNREACH;
758 goto exit;
759 }
760 } else if (dest->addrtype == TIPC_ADDR_ID) {
761 dnode = dest->addr.id.node;
762 msg_set_type(mhdr, TIPC_DIRECT_MSG);
763 msg_set_lookup_scope(mhdr, 0);
764 msg_set_destnode(mhdr, dnode);
765 msg_set_destport(mhdr, dest->addr.id.ref);
766 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
767 }
768
769new_mtu:
770 mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
771 rc = tipc_msg_build2(mhdr, iov, 0, dsz, mtu, &buf);
772 if (rc < 0)
773 goto exit;
774
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900775 do {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500776 rc = tipc_link_xmit2(buf, dnode, tsk->port.ref);
777 if (likely(rc >= 0)) {
778 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700779 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500780 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700781 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900782 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500783 if (rc == -EMSGSIZE)
784 goto new_mtu;
785
786 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700787 break;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500788
789 rc = tipc_wait_for_sndmsg(sock, &timeo);
790 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700791
792exit:
793 if (iocb)
794 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500795
796 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100797}
798
Ying Xue391a6dd2014-01-17 09:50:06 +0800799static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
800{
801 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400802 struct tipc_sock *tsk = tipc_sk(sk);
803 struct tipc_port *port = &tsk->port;
Ying Xue391a6dd2014-01-17 09:50:06 +0800804 DEFINE_WAIT(wait);
805 int done;
806
807 do {
808 int err = sock_error(sk);
809 if (err)
810 return err;
811 if (sock->state == SS_DISCONNECTING)
812 return -EPIPE;
813 else if (sock->state != SS_CONNECTED)
814 return -ENOTCONN;
815 if (!*timeo_p)
816 return -EAGAIN;
817 if (signal_pending(current))
818 return sock_intr_errno(*timeo_p);
819
820 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
821 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400822 (!port->congested || !port->connected));
Ying Xue391a6dd2014-01-17 09:50:06 +0800823 finish_wait(sk_sleep(sk), &wait);
824 } while (!done);
825 return 0;
826}
827
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900828/**
Ying Xue247f0f32014-02-18 16:06:46 +0800829 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830 * @iocb: (unused)
831 * @sock: socket structure
832 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500833 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900834 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100835 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900836 *
837 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700838 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100839 */
Ying Xue247f0f32014-02-18 16:06:46 +0800840static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500841 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100842{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700843 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400844 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500845 struct tipc_port *port = &tsk->port;
846 struct tipc_msg *mhdr = &port->phdr;
847 struct sk_buff *buf;
848 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
849 u32 ref = port->ref;
850 int rc = -EINVAL;
851 long timeo;
852 u32 dnode;
853 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900854
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500855 /* Handle implied connection establishment */
856 if (unlikely(dest)) {
857 rc = tipc_sendmsg(iocb, sock, m, dsz);
858 if (dsz && (dsz == rc))
859 tsk->port.sent = 1;
860 return rc;
861 }
862 if (dsz > (uint)INT_MAX)
863 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700864
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500865 if (iocb)
866 lock_sock(sk);
867
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900868 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500869 if (sock->state == SS_DISCONNECTING)
870 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +0800871 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500872 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800873 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900874 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100875
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500876 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
877 dnode = tipc_port_peernode(port);
878 port->congested = 1;
879
880next:
881 mtu = port->max_pkt;
882 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
883 rc = tipc_msg_build2(mhdr, m->msg_iov, sent, send, mtu, &buf);
884 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700885 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500886 do {
887 port->congested = 1;
888 if (likely(!tipc_port_congested(port))) {
889 rc = tipc_link_xmit2(buf, dnode, ref);
890 if (likely(!rc)) {
891 port->sent++;
892 sent += send;
893 if (sent == dsz)
894 break;
895 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700896 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500897 if (rc == -EMSGSIZE) {
898 port->max_pkt = tipc_node_get_mtu(dnode, ref);
899 goto next;
900 }
901 if (rc != -ELINKCONG)
902 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500904 rc = tipc_wait_for_sndpkt(sock, &timeo);
905 } while (!rc);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500907 port->congested = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700908exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500909 if (iocb)
910 release_sock(sk);
911 return sent ? sent : rc;
912}
913
914/**
915 * tipc_send_packet - send a connection-oriented message
916 * @iocb: if NULL, indicates that socket lock is already held
917 * @sock: socket structure
918 * @m: message to send
919 * @dsz: length of data to be transmitted
920 *
921 * Used for SOCK_SEQPACKET messages.
922 *
923 * Returns the number of bytes sent on success, or errno otherwise
924 */
925static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
926 struct msghdr *m, size_t dsz)
927{
928 if (dsz > TIPC_MAX_USER_MSG_SIZE)
929 return -EMSGSIZE;
930
931 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100932}
933
934/**
935 * auto_connect - complete connection setup to a remote port
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400936 * @tsk: tipc socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100937 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900938 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100939 * Returns 0 on success, errno otherwise
940 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400941static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400943 struct tipc_port *port = &tsk->port;
944 struct socket *sock = tsk->sk.sk_socket;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400945 struct tipc_portid peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400947 peer.ref = msg_origport(msg);
948 peer.node = msg_orignode(msg);
949
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400950 __tipc_port_connect(port->ref, port, &peer);
Ying Xue584d24b2012-11-29 18:51:19 -0500951
952 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
953 return -EINVAL;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400954 msg_set_importance(&port->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955 sock->state = SS_CONNECTED;
956 return 0;
957}
958
959/**
960 * set_orig_addr - capture sender's address for received message
961 * @m: descriptor for message info
962 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900963 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964 * Note: Address is not captured if not requested by receiver.
965 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800966static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100967{
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100968 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900970 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100971 addr->family = AF_TIPC;
972 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000973 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974 addr->addr.id.ref = msg_origport(msg);
975 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000976 addr->addr.name.domain = 0; /* could leave uninitialized */
977 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100978 m->msg_namelen = sizeof(struct sockaddr_tipc);
979 }
980}
981
982/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900983 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984 * @m: descriptor for message info
985 * @msg: received message header
986 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900987 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100988 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900989 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990 * Returns 0 if successful, otherwise errno
991 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800992static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400993 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100994{
995 u32 anc_data[3];
996 u32 err;
997 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700998 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100999 int res;
1000
1001 if (likely(m->msg_controllen == 0))
1002 return 0;
1003
1004 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001005 err = msg ? msg_errcode(msg) : 0;
1006 if (unlikely(err)) {
1007 anc_data[0] = err;
1008 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001009 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1010 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001012 if (anc_data[1]) {
1013 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1014 msg_data(msg));
1015 if (res)
1016 return res;
1017 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 }
1019
1020 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001021 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1022 switch (dest_type) {
1023 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001024 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 anc_data[0] = msg_nametype(msg);
1026 anc_data[1] = msg_namelower(msg);
1027 anc_data[2] = msg_namelower(msg);
1028 break;
1029 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001030 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031 anc_data[0] = msg_nametype(msg);
1032 anc_data[1] = msg_namelower(msg);
1033 anc_data[2] = msg_nameupper(msg);
1034 break;
1035 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001036 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037 anc_data[0] = tport->conn_type;
1038 anc_data[1] = tport->conn_instance;
1039 anc_data[2] = tport->conn_instance;
1040 break;
1041 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001042 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043 }
Allan Stephens2db99832010-12-31 18:59:33 +00001044 if (has_name) {
1045 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1046 if (res)
1047 return res;
1048 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049
1050 return 0;
1051}
1052
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001053static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001054{
1055 struct sock *sk = sock->sk;
1056 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001057 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001058 int err;
1059
1060 for (;;) {
1061 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001062 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001063 if (sock->state == SS_DISCONNECTING) {
1064 err = -ENOTCONN;
1065 break;
1066 }
1067 release_sock(sk);
1068 timeo = schedule_timeout(timeo);
1069 lock_sock(sk);
1070 }
1071 err = 0;
1072 if (!skb_queue_empty(&sk->sk_receive_queue))
1073 break;
1074 err = sock_intr_errno(timeo);
1075 if (signal_pending(current))
1076 break;
1077 err = -EAGAIN;
1078 if (!timeo)
1079 break;
1080 }
1081 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001082 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001083 return err;
1084}
1085
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001086/**
Ying Xue247f0f32014-02-18 16:06:46 +08001087 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001088 * @iocb: (unused)
1089 * @m: descriptor for message info
1090 * @buf_len: total size of user buffer area
1091 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001092 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1094 * If the complete message doesn't fit in user area, truncate it.
1095 *
1096 * Returns size of returned message data, errno otherwise
1097 */
Ying Xue247f0f32014-02-18 16:06:46 +08001098static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1099 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001100{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001101 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001102 struct tipc_sock *tsk = tipc_sk(sk);
1103 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001104 struct sk_buff *buf;
1105 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001106 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001107 unsigned int sz;
1108 u32 err;
1109 int res;
1110
Allan Stephens0c3141e2008-04-15 00:22:02 -07001111 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001112 if (unlikely(!buf_len))
1113 return -EINVAL;
1114
Allan Stephens0c3141e2008-04-15 00:22:02 -07001115 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116
Allan Stephens0c3141e2008-04-15 00:22:02 -07001117 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118 res = -ENOTCONN;
1119 goto exit;
1120 }
1121
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001122 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001123restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124
Allan Stephens0c3141e2008-04-15 00:22:02 -07001125 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001126 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001127 if (res)
1128 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001129
1130 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001131 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132 msg = buf_msg(buf);
1133 sz = msg_data_sz(msg);
1134 err = msg_errcode(msg);
1135
Per Lidenb97bf3f2006-01-02 19:04:38 +01001136 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001138 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001139 goto restart;
1140 }
1141
1142 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143 set_orig_addr(m, msg);
1144
1145 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001146 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001147 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 goto exit;
1149
1150 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001151 if (!err) {
1152 if (unlikely(buf_len < sz)) {
1153 sz = buf_len;
1154 m->msg_flags |= MSG_TRUNC;
1155 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001156 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1157 m->msg_iov, sz);
1158 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160 res = sz;
1161 } else {
1162 if ((sock->state == SS_READY) ||
1163 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1164 res = 0;
1165 else
1166 res = -ECONNRESET;
1167 }
1168
1169 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001171 if ((sock->state != SS_READY) &&
Jon Paul Maloy6163a192014-05-14 05:39:08 -04001172 (++port->conn_unacked >= TIPC_CONNACK_INTV))
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001173 tipc_acknowledge(port->ref, port->conn_unacked);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001174 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001175 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001177 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178 return res;
1179}
1180
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001181/**
Ying Xue247f0f32014-02-18 16:06:46 +08001182 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183 * @iocb: (unused)
1184 * @m: descriptor for message info
1185 * @buf_len: total size of user buffer area
1186 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001187 *
1188 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 * will optionally wait for more; never truncates data.
1190 *
1191 * Returns size of returned message data, errno otherwise
1192 */
Ying Xue247f0f32014-02-18 16:06:46 +08001193static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1194 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001196 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001197 struct tipc_sock *tsk = tipc_sk(sk);
1198 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 struct sk_buff *buf;
1200 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001201 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001203 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001204 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001206 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207
1208 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001209 if (unlikely(!buf_len))
1210 return -EINVAL;
1211
Allan Stephens0c3141e2008-04-15 00:22:02 -07001212 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001214 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215 res = -ENOTCONN;
1216 goto exit;
1217 }
1218
Florian Westphal3720d402010-08-17 11:00:04 +00001219 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001220 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001221
Allan Stephens0c3141e2008-04-15 00:22:02 -07001222restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001223 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001224 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001225 if (res)
1226 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001227
1228 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001229 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230 msg = buf_msg(buf);
1231 sz = msg_data_sz(msg);
1232 err = msg_errcode(msg);
1233
1234 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001236 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 goto restart;
1238 }
1239
1240 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241 if (sz_copied == 0) {
1242 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001243 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001244 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001245 goto exit;
1246 }
1247
1248 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001249 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001250 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001251
Allan Stephens0232fd02011-02-21 09:45:40 -05001252 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001253 needed = (buf_len - sz_copied);
1254 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001255
1256 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1257 m->msg_iov, sz_to_copy);
1258 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001260
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261 sz_copied += sz_to_copy;
1262
1263 if (sz_to_copy < sz) {
1264 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001265 TIPC_SKB_CB(buf)->handle =
1266 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001267 goto exit;
1268 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001269 } else {
1270 if (sz_copied != 0)
1271 goto exit; /* can't add error msg to valid data */
1272
1273 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1274 res = 0;
1275 else
1276 res = -ECONNRESET;
1277 }
1278
1279 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001280 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy6163a192014-05-14 05:39:08 -04001281 if (unlikely(++port->conn_unacked >= TIPC_CONNACK_INTV))
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001282 tipc_acknowledge(port->ref, port->conn_unacked);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001283 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001284 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285
1286 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001287 if ((sz_copied < buf_len) && /* didn't get all requested data */
1288 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001289 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001290 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1291 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 goto restart;
1293
1294exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001295 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001296 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001297}
1298
1299/**
Ying Xuef288bef2012-08-21 11:16:57 +08001300 * tipc_write_space - wake up thread if port congestion is released
1301 * @sk: socket
1302 */
1303static void tipc_write_space(struct sock *sk)
1304{
1305 struct socket_wq *wq;
1306
1307 rcu_read_lock();
1308 wq = rcu_dereference(sk->sk_wq);
1309 if (wq_has_sleeper(wq))
1310 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1311 POLLWRNORM | POLLWRBAND);
1312 rcu_read_unlock();
1313}
1314
1315/**
1316 * tipc_data_ready - wake up threads to indicate messages have been received
1317 * @sk: socket
1318 * @len: the length of messages
1319 */
David S. Miller676d2362014-04-11 16:15:36 -04001320static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001321{
1322 struct socket_wq *wq;
1323
1324 rcu_read_lock();
1325 wq = rcu_dereference(sk->sk_wq);
1326 if (wq_has_sleeper(wq))
1327 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1328 POLLRDNORM | POLLRDBAND);
1329 rcu_read_unlock();
1330}
1331
1332/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001333 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001334 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001335 * @msg: message
1336 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001337 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001338 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001339static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001340{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001341 struct sock *sk = &tsk->sk;
1342 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001343 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001344 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001345
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001346 int retval = -TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001347 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001348
1349 if (msg_mcast(msg))
1350 return retval;
1351
1352 switch ((int)sock->state) {
1353 case SS_CONNECTED:
1354 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001355 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001356 if (unlikely(msg_errcode(msg))) {
1357 sock->state = SS_DISCONNECTING;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001358 __tipc_port_disconnect(port);
Ying Xue7e6c1312012-11-29 18:39:14 -05001359 }
1360 retval = TIPC_OK;
1361 }
1362 break;
1363 case SS_CONNECTING:
1364 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001365 if (unlikely(msg_errcode(msg))) {
1366 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001367 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001368 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001369 break;
1370 }
1371
1372 if (unlikely(!msg_connected(msg)))
1373 break;
1374
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001375 res = auto_connect(tsk, msg);
Ying Xue584d24b2012-11-29 18:51:19 -05001376 if (res) {
1377 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001378 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001379 retval = TIPC_OK;
1380 break;
1381 }
1382
1383 /* If an incoming message is an 'ACK-', it should be
1384 * discarded here because it doesn't contain useful
1385 * data. In addition, we should try to wake up
1386 * connect() routine if sleeping.
1387 */
1388 if (msg_data_sz(msg) == 0) {
1389 kfree_skb(*buf);
1390 *buf = NULL;
1391 if (waitqueue_active(sk_sleep(sk)))
1392 wake_up_interruptible(sk_sleep(sk));
1393 }
1394 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001395 break;
1396 case SS_LISTENING:
1397 case SS_UNCONNECTED:
1398 /* Accept only SYN message */
1399 if (!msg_connected(msg) && !(msg_errcode(msg)))
1400 retval = TIPC_OK;
1401 break;
1402 case SS_DISCONNECTING:
1403 break;
1404 default:
1405 pr_err("Unknown socket state %u\n", sock->state);
1406 }
1407 return retval;
1408}
1409
1410/**
Ying Xueaba79f32013-01-20 23:30:09 +01001411 * rcvbuf_limit - get proper overload limit of socket receive queue
1412 * @sk: socket
1413 * @buf: message
1414 *
1415 * For all connection oriented messages, irrespective of importance,
1416 * the default overload value (i.e. 67MB) is set as limit.
1417 *
1418 * For all connectionless messages, by default new queue limits are
1419 * as belows:
1420 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001421 * TIPC_LOW_IMPORTANCE (4 MB)
1422 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1423 * TIPC_HIGH_IMPORTANCE (16 MB)
1424 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001425 *
1426 * Returns overload limit according to corresponding message importance
1427 */
1428static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1429{
1430 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001431
1432 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001433 return sysctl_tipc_rmem[2];
1434
1435 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1436 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001437}
1438
1439/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001440 * filter_rcv - validate incoming message
1441 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001442 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001443 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001444 * Enqueues message on receive queue if acceptable; optionally handles
1445 * disconnect indication for a connected socket.
1446 *
1447 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001448 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001449 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001450 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001451 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001452static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001453{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001454 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001455 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001456 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001457 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001458 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001459 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001460
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001461 if (unlikely(msg_user(msg) == CONN_MANAGER))
1462 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001463
Per Lidenb97bf3f2006-01-02 19:04:38 +01001464 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001465 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001466 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001467
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001469 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001470 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001471 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001472 rc = filter_connect(tsk, &buf);
1473 if (rc != TIPC_OK || buf == NULL)
1474 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475 }
1476
1477 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001478 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001479 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001480
Ying Xueaba79f32013-01-20 23:30:09 +01001481 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001482 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001483 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001484 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001485
David S. Miller676d2362014-04-11 16:15:36 -04001486 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001487 return TIPC_OK;
1488}
1489
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001490/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001491 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001492 * @sk: socket
1493 * @buf: message
1494 *
1495 * Caller must hold socket lock, but not port lock.
1496 *
1497 * Returns 0
1498 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001499static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001500{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001501 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001502 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001503 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001504 uint truesize = buf->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001505
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001506 rc = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001507
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001508 if (likely(!rc)) {
1509 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1510 atomic_add(truesize, &tsk->dupl_rcvcnt);
1511 return 0;
1512 }
1513
1514 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1515 return 0;
1516
1517 tipc_link_xmit2(buf, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001518
Allan Stephens0c3141e2008-04-15 00:22:02 -07001519 return 0;
1520}
1521
1522/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001523 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001524 * @buf: buffer containing arriving message
1525 * Consumes buffer
1526 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001527 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001528int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001529{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001530 struct tipc_sock *tsk;
1531 struct tipc_port *port;
1532 struct sock *sk;
1533 u32 dport = msg_destport(buf_msg(buf));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001534 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001535 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001536 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001537
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001538 /* Validate destination and message */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001539 port = tipc_port_lock(dport);
1540 if (unlikely(!port)) {
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001541 rc = tipc_msg_eval(buf, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001542 goto exit;
1543 }
1544
1545 tsk = tipc_port_to_sock(port);
1546 sk = &tsk->sk;
1547
1548 /* Queue message */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001549 bh_lock_sock(sk);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001550
Allan Stephens0c3141e2008-04-15 00:22:02 -07001551 if (!sock_owned_by_user(sk)) {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001552 rc = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001553 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001554 if (sk->sk_backlog.len == 0)
1555 atomic_set(&tsk->dupl_rcvcnt, 0);
1556 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1557 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001558 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001559 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001560 bh_unlock_sock(sk);
1561 tipc_port_unlock(port);
1562
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001563 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001564 return 0;
1565exit:
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001566 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001567 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001568
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001569 tipc_link_xmit2(buf, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001570 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001571}
1572
Ying Xue78eb3a52014-01-17 09:50:03 +08001573static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1574{
1575 struct sock *sk = sock->sk;
1576 DEFINE_WAIT(wait);
1577 int done;
1578
1579 do {
1580 int err = sock_error(sk);
1581 if (err)
1582 return err;
1583 if (!*timeo_p)
1584 return -ETIMEDOUT;
1585 if (signal_pending(current))
1586 return sock_intr_errno(*timeo_p);
1587
1588 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1589 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1590 finish_wait(sk_sleep(sk), &wait);
1591 } while (!done);
1592 return 0;
1593}
1594
Per Lidenb97bf3f2006-01-02 19:04:38 +01001595/**
Ying Xue247f0f32014-02-18 16:06:46 +08001596 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001597 * @sock: socket structure
1598 * @dest: socket address for destination port
1599 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001600 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001601 *
1602 * Returns 0 on success, errno otherwise
1603 */
Ying Xue247f0f32014-02-18 16:06:46 +08001604static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1605 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001607 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001608 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1609 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001610 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1611 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001612 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001613
Allan Stephens0c3141e2008-04-15 00:22:02 -07001614 lock_sock(sk);
1615
Allan Stephensb89741a2008-04-15 00:20:37 -07001616 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001617 if (sock->state == SS_READY) {
1618 res = -EOPNOTSUPP;
1619 goto exit;
1620 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001621
Allan Stephensb89741a2008-04-15 00:20:37 -07001622 /*
1623 * Reject connection attempt using multicast address
1624 *
1625 * Note: send_msg() validates the rest of the address fields,
1626 * so there's no need to do it here
1627 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001628 if (dst->addrtype == TIPC_ADDR_MCAST) {
1629 res = -EINVAL;
1630 goto exit;
1631 }
1632
Ying Xue78eb3a52014-01-17 09:50:03 +08001633 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001634 switch (sock->state) {
1635 case SS_UNCONNECTED:
1636 /* Send a 'SYN-' to destination */
1637 m.msg_name = dest;
1638 m.msg_namelen = destlen;
1639
1640 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1641 * indicate send_msg() is never blocked.
1642 */
1643 if (!timeout)
1644 m.msg_flags = MSG_DONTWAIT;
1645
Ying Xue247f0f32014-02-18 16:06:46 +08001646 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001647 if ((res < 0) && (res != -EWOULDBLOCK))
1648 goto exit;
1649
1650 /* Just entered SS_CONNECTING state; the only
1651 * difference is that return value in non-blocking
1652 * case is EINPROGRESS, rather than EALREADY.
1653 */
1654 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001655 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001656 if (previous == SS_CONNECTING)
1657 res = -EALREADY;
1658 if (!timeout)
1659 goto exit;
1660 timeout = msecs_to_jiffies(timeout);
1661 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1662 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001663 break;
1664 case SS_CONNECTED:
1665 res = -EISCONN;
1666 break;
1667 default:
1668 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001669 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001670 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001671exit:
1672 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001673 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674}
1675
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001676/**
Ying Xue247f0f32014-02-18 16:06:46 +08001677 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678 * @sock: socket structure
1679 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001680 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001681 * Returns 0 on success, errno otherwise
1682 */
Ying Xue247f0f32014-02-18 16:06:46 +08001683static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001684{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001685 struct sock *sk = sock->sk;
1686 int res;
1687
1688 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001689
Ying Xue245f3d32011-07-06 06:01:13 -04001690 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001691 res = -EINVAL;
1692 else {
1693 sock->state = SS_LISTENING;
1694 res = 0;
1695 }
1696
1697 release_sock(sk);
1698 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699}
1700
Ying Xue6398e232014-01-17 09:50:04 +08001701static int tipc_wait_for_accept(struct socket *sock, long timeo)
1702{
1703 struct sock *sk = sock->sk;
1704 DEFINE_WAIT(wait);
1705 int err;
1706
1707 /* True wake-one mechanism for incoming connections: only
1708 * one process gets woken up, not the 'whole herd'.
1709 * Since we do not 'race & poll' for established sockets
1710 * anymore, the common case will execute the loop only once.
1711 */
1712 for (;;) {
1713 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1714 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001715 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001716 release_sock(sk);
1717 timeo = schedule_timeout(timeo);
1718 lock_sock(sk);
1719 }
1720 err = 0;
1721 if (!skb_queue_empty(&sk->sk_receive_queue))
1722 break;
1723 err = -EINVAL;
1724 if (sock->state != SS_LISTENING)
1725 break;
1726 err = sock_intr_errno(timeo);
1727 if (signal_pending(current))
1728 break;
1729 err = -EAGAIN;
1730 if (!timeo)
1731 break;
1732 }
1733 finish_wait(sk_sleep(sk), &wait);
1734 return err;
1735}
1736
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001737/**
Ying Xue247f0f32014-02-18 16:06:46 +08001738 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739 * @sock: listening socket
1740 * @newsock: new socket that is to be connected
1741 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001742 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001743 * Returns 0 on success, errno otherwise
1744 */
Ying Xue247f0f32014-02-18 16:06:46 +08001745static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001747 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001748 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001749 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001750 struct tipc_msg *msg;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001751 struct tipc_portid peer;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001752 u32 new_ref;
Ying Xue6398e232014-01-17 09:50:04 +08001753 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001754 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001755
Allan Stephens0c3141e2008-04-15 00:22:02 -07001756 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001757
Allan Stephens0c3141e2008-04-15 00:22:02 -07001758 if (sock->state != SS_LISTENING) {
1759 res = -EINVAL;
1760 goto exit;
1761 }
Ying Xue6398e232014-01-17 09:50:04 +08001762 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1763 res = tipc_wait_for_accept(sock, timeo);
1764 if (res)
1765 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001766
1767 buf = skb_peek(&sk->sk_receive_queue);
1768
Ying Xuec5fa7b32013-06-17 10:54:39 -04001769 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001770 if (res)
1771 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001772
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001773 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001774 new_port = &tipc_sk(new_sk)->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001775 new_ref = new_port->ref;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001776 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001777
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001778 /* we lock on new_sk; but lockdep sees the lock on sk */
1779 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001780
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001781 /*
1782 * Reject any stray messages received by new socket
1783 * before the socket lock was taken (very, very unlikely)
1784 */
1785 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001786
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001787 /* Connect new socket to it's peer */
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001788 peer.ref = msg_origport(msg);
1789 peer.node = msg_orignode(msg);
1790 tipc_port_connect(new_ref, &peer);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001791 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001792
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001793 tipc_port_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001794 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001795 new_port->conn_type = msg_nametype(msg);
1796 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001797 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001798
1799 /*
1800 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1801 * Respond to 'SYN+' by queuing it on new socket.
1802 */
1803 if (!msg_data_sz(msg)) {
1804 struct msghdr m = {NULL,};
1805
1806 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001807 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001808 } else {
1809 __skb_dequeue(&sk->sk_receive_queue);
1810 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001811 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001812 }
1813 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001815 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816 return res;
1817}
1818
1819/**
Ying Xue247f0f32014-02-18 16:06:46 +08001820 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001821 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001822 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001823 *
1824 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001825 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826 * Returns 0 on success, errno otherwise
1827 */
Ying Xue247f0f32014-02-18 16:06:46 +08001828static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001830 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001831 struct tipc_sock *tsk = tipc_sk(sk);
1832 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001833 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001834 u32 peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001835 int res;
1836
Allan Stephense247a8f2008-03-06 15:05:38 -08001837 if (how != SHUT_RDWR)
1838 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001839
Allan Stephens0c3141e2008-04-15 00:22:02 -07001840 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001841
1842 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001843 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001844 case SS_CONNECTED:
1845
Per Lidenb97bf3f2006-01-02 19:04:38 +01001846restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001847 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001848 buf = __skb_dequeue(&sk->sk_receive_queue);
1849 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001850 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001851 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001852 goto restart;
1853 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001854 tipc_port_disconnect(port->ref);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001855 if (tipc_msg_reverse(buf, &peer, TIPC_CONN_SHUTDOWN))
1856 tipc_link_xmit2(buf, peer, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001857 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001858 tipc_port_shutdown(port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001859 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001860
1861 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001862
1863 /* fall through */
1864
1865 case SS_DISCONNECTING:
1866
Ying Xue75031152012-10-29 09:38:15 -04001867 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001868 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001869
1870 /* Wake up anyone sleeping in poll */
1871 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001872 res = 0;
1873 break;
1874
1875 default:
1876 res = -ENOTCONN;
1877 }
1878
Allan Stephens0c3141e2008-04-15 00:22:02 -07001879 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001880 return res;
1881}
1882
1883/**
Ying Xue247f0f32014-02-18 16:06:46 +08001884 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001885 * @sock: socket structure
1886 * @lvl: option level
1887 * @opt: option identifier
1888 * @ov: pointer to new option value
1889 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001890 *
1891 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001892 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001893 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001894 * Returns 0 on success, errno otherwise
1895 */
Ying Xue247f0f32014-02-18 16:06:46 +08001896static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1897 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001898{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001899 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001900 struct tipc_sock *tsk = tipc_sk(sk);
1901 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001902 u32 value;
1903 int res;
1904
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001905 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1906 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907 if (lvl != SOL_TIPC)
1908 return -ENOPROTOOPT;
1909 if (ol < sizeof(value))
1910 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001911 res = get_user(value, (u32 __user *)ov);
1912 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001913 return res;
1914
Allan Stephens0c3141e2008-04-15 00:22:02 -07001915 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001916
Per Lidenb97bf3f2006-01-02 19:04:38 +01001917 switch (opt) {
1918 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001919 tipc_port_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001920 break;
1921 case TIPC_SRC_DROPPABLE:
1922 if (sock->type != SOCK_STREAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001923 tipc_port_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001924 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001925 res = -ENOPROTOOPT;
1926 break;
1927 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001928 tipc_port_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001929 break;
1930 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001931 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001932 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001933 break;
1934 default:
1935 res = -EINVAL;
1936 }
1937
Allan Stephens0c3141e2008-04-15 00:22:02 -07001938 release_sock(sk);
1939
Per Lidenb97bf3f2006-01-02 19:04:38 +01001940 return res;
1941}
1942
1943/**
Ying Xue247f0f32014-02-18 16:06:46 +08001944 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001945 * @sock: socket structure
1946 * @lvl: option level
1947 * @opt: option identifier
1948 * @ov: receptacle for option value
1949 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001950 *
1951 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001952 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001953 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001954 * Returns 0 on success, errno otherwise
1955 */
Ying Xue247f0f32014-02-18 16:06:46 +08001956static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1957 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001958{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001959 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001960 struct tipc_sock *tsk = tipc_sk(sk);
1961 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001962 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001963 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001964 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001965
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001966 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1967 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001968 if (lvl != SOL_TIPC)
1969 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001970 res = get_user(len, ol);
1971 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001972 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001973
Allan Stephens0c3141e2008-04-15 00:22:02 -07001974 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001975
1976 switch (opt) {
1977 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001978 value = tipc_port_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001979 break;
1980 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001981 value = tipc_port_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001982 break;
1983 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001984 value = tipc_port_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001985 break;
1986 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001987 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001988 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001989 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001990 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001991 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001992 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001993 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001994 value = skb_queue_len(&sk->sk_receive_queue);
1995 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001996 default:
1997 res = -EINVAL;
1998 }
1999
Allan Stephens0c3141e2008-04-15 00:22:02 -07002000 release_sock(sk);
2001
Paul Gortmaker25860c32010-12-31 18:59:31 +00002002 if (res)
2003 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002004
Paul Gortmaker25860c32010-12-31 18:59:31 +00002005 if (len < sizeof(value))
2006 return -EINVAL;
2007
2008 if (copy_to_user(ov, &value, sizeof(value)))
2009 return -EFAULT;
2010
2011 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002012}
2013
Erik Hugne78acb1f2014-04-24 16:26:47 +02002014int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
2015{
2016 struct tipc_sioc_ln_req lnr;
2017 void __user *argp = (void __user *)arg;
2018
2019 switch (cmd) {
2020 case SIOCGETLINKNAME:
2021 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2022 return -EFAULT;
2023 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
2024 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2025 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2026 return -EFAULT;
2027 return 0;
2028 }
2029 return -EADDRNOTAVAIL;
2030 break;
2031 default:
2032 return -ENOIOCTLCMD;
2033 }
2034}
2035
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002036/* Protocol switches for the various types of TIPC sockets */
2037
Florian Westphalbca65ea2008-02-07 18:18:01 -08002038static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002039 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002040 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002041 .release = tipc_release,
2042 .bind = tipc_bind,
2043 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002044 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002045 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002046 .getname = tipc_getname,
2047 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002048 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002049 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002050 .shutdown = tipc_shutdown,
2051 .setsockopt = tipc_setsockopt,
2052 .getsockopt = tipc_getsockopt,
2053 .sendmsg = tipc_sendmsg,
2054 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002055 .mmap = sock_no_mmap,
2056 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002057};
2058
Florian Westphalbca65ea2008-02-07 18:18:01 -08002059static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002060 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002061 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002062 .release = tipc_release,
2063 .bind = tipc_bind,
2064 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002065 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002066 .accept = tipc_accept,
2067 .getname = tipc_getname,
2068 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002069 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002070 .listen = tipc_listen,
2071 .shutdown = tipc_shutdown,
2072 .setsockopt = tipc_setsockopt,
2073 .getsockopt = tipc_getsockopt,
2074 .sendmsg = tipc_send_packet,
2075 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002076 .mmap = sock_no_mmap,
2077 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002078};
2079
Florian Westphalbca65ea2008-02-07 18:18:01 -08002080static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002081 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002082 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002083 .release = tipc_release,
2084 .bind = tipc_bind,
2085 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002086 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002087 .accept = tipc_accept,
2088 .getname = tipc_getname,
2089 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002090 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002091 .listen = tipc_listen,
2092 .shutdown = tipc_shutdown,
2093 .setsockopt = tipc_setsockopt,
2094 .getsockopt = tipc_getsockopt,
2095 .sendmsg = tipc_send_stream,
2096 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002097 .mmap = sock_no_mmap,
2098 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002099};
2100
Florian Westphalbca65ea2008-02-07 18:18:01 -08002101static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002102 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002103 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002104 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002105};
2106
2107static struct proto tipc_proto = {
2108 .name = "TIPC",
2109 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002110 .obj_size = sizeof(struct tipc_sock),
2111 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002112};
2113
Ying Xuec5fa7b32013-06-17 10:54:39 -04002114static struct proto tipc_proto_kern = {
2115 .name = "TIPC",
2116 .obj_size = sizeof(struct tipc_sock),
2117 .sysctl_rmem = sysctl_tipc_rmem
2118};
2119
Per Lidenb97bf3f2006-01-02 19:04:38 +01002120/**
Per Liden4323add2006-01-18 00:38:21 +01002121 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002122 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002123 * Returns 0 on success, errno otherwise
2124 */
Per Liden4323add2006-01-18 00:38:21 +01002125int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002126{
2127 int res;
2128
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002129 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002130 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002131 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002132 goto out;
2133 }
2134
2135 res = sock_register(&tipc_family_ops);
2136 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002137 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002138 proto_unregister(&tipc_proto);
2139 goto out;
2140 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002141 out:
2142 return res;
2143}
2144
2145/**
Per Liden4323add2006-01-18 00:38:21 +01002146 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002147 */
Per Liden4323add2006-01-18 00:38:21 +01002148void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002149{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002150 sock_unregister(tipc_family_ops.family);
2151 proto_unregister(&tipc_proto);
2152}