blob: a65105818fe501147c29295583da4d5eaa27becf [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04004 * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000038#include "port.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050039#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020040#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050041#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040042#include <linux/export.h>
Erik Hugne2cf8aa12012-06-29 00:16:37 -040043
Per Lidenb97bf3f2006-01-02 19:04:38 +010044#define SS_LISTENING -1 /* socket is listening */
45#define SS_READY -2 /* socket is connectionless */
46
Allan Stephens3654ea02008-04-13 21:35:11 -070047#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Jon Paul Maloydadebc02014-08-22 18:09:11 -040048#define CONN_PROBING_INTERVAL 3600000 /* [ms] => 1 h */
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);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -040056static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Jon Paul Maloy57289012014-08-22 18:09:09 -040057static void tipc_sk_timeout(unsigned long ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +010058
Florian Westphalbca65ea2008-02-07 18:18:01 -080059static const struct proto_ops packet_ops;
60static const struct proto_ops stream_ops;
61static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010062
63static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040064static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010065
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090066/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070067 * Revised TIPC socket locking policy:
68 *
69 * Most socket operations take the standard socket lock when they start
70 * and hold it until they finish (or until they need to sleep). Acquiring
71 * this lock grants the owner exclusive access to the fields of the socket
72 * data structures, with the exception of the backlog queue. A few socket
73 * operations can be done without taking the socket lock because they only
74 * read socket information that never changes during the life of the socket.
75 *
76 * Socket operations may acquire the lock for the associated TIPC port if they
77 * need to perform an operation on the port. If any routine needs to acquire
78 * both the socket lock and the port lock it must take the socket lock first
79 * to avoid the risk of deadlock.
80 *
81 * The dispatcher handling incoming messages cannot grab the socket lock in
82 * the standard fashion, since invoked it runs at the BH level and cannot block.
83 * Instead, it checks to see if the socket lock is currently owned by someone,
84 * and either handles the message itself or adds it to the socket's backlog
85 * queue; in the latter case the queued message is processed once the process
86 * owning the socket lock releases it.
87 *
88 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
89 * the problem of a blocked socket operation preventing any other operations
90 * from occurring. However, applications must be careful if they have
91 * multiple threads trying to send (or receive) on the same socket, as these
92 * operations might interfere with each other. For example, doing a connect
93 * and a receive at the same time might allow the receive to consume the
94 * ACK message meant for the connect. While additional work could be done
95 * to try and overcome this, it doesn't seem to be worthwhile at the present.
96 *
97 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
98 * that another operation that must be performed in a non-blocking manner is
99 * not delayed for very long because the lock has already been taken.
100 *
101 * NOTE: This code assumes that certain fields of a port/socket pair are
102 * constant over its lifetime; such fields can be examined without taking
103 * the socket lock and/or port lock, and do not need to be re-read even
104 * after resuming processing after waiting. These fields include:
105 * - socket type
106 * - pointer to socket sk structure (aka tipc_sock structure)
107 * - pointer to port structure
108 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100109 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100110
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400111#include "socket.h"
112
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700114 * advance_rx_queue - discard first buffer in socket receive queue
115 *
116 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100117 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700118static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400120 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121}
122
123/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700124 * reject_rx_queue - reject all buffers in socket receive queue
125 *
126 * Caller must hold socket lock
127 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700128static void reject_rx_queue(struct sock *sk)
129{
130 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500131 u32 dnode;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700132
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500133 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
134 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400135 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500136 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700137}
138
139/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400140 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700141 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100142 * @sock: pre-allocated socket structure
143 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800144 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900145 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700146 * This routine creates additional data structures used by the TIPC socket,
147 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148 *
149 * Returns 0 on success, errno otherwise
150 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400151static int tipc_sk_create(struct net *net, struct socket *sock,
152 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700154 const struct proto_ops *ops;
155 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400157 struct tipc_sock *tsk;
158 struct tipc_port *port;
159 u32 ref;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700160
161 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162 if (unlikely(protocol != 0))
163 return -EPROTONOSUPPORT;
164
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 switch (sock->type) {
166 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700167 ops = &stream_ops;
168 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 break;
170 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700171 ops = &packet_ops;
172 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 break;
174 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100175 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700176 ops = &msg_ops;
177 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 break;
Allan Stephens49978652006-06-25 23:47:18 -0700179 default:
Allan Stephens49978652006-06-25 23:47:18 -0700180 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 }
182
Allan Stephens0c3141e2008-04-15 00:22:02 -0700183 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400184 if (!kern)
185 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
186 else
187 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
188
Allan Stephens0c3141e2008-04-15 00:22:02 -0700189 if (sk == NULL)
190 return -ENOMEM;
191
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400192 tsk = tipc_sk(sk);
193 port = &tsk->port;
194
195 ref = tipc_port_init(port, TIPC_LOW_IMPORTANCE);
196 if (!ref) {
197 pr_warn("Socket registration failed, ref. table exhausted\n");
Allan Stephens0c3141e2008-04-15 00:22:02 -0700198 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199 return -ENOMEM;
200 }
201
Allan Stephens0c3141e2008-04-15 00:22:02 -0700202 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700203 sock->ops = ops;
204 sock->state = state;
205
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206 sock_init_data(sock, sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -0400207 k_init_timer(&port->timer, (Handler)tipc_sk_timeout, ref);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400208 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400209 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800210 sk->sk_data_ready = tipc_data_ready;
211 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400212 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500213 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400214 atomic_set(&tsk->dupl_rcvcnt, 0);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400215 tipc_port_unlock(port);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700216
Allan Stephens0c3141e2008-04-15 00:22:02 -0700217 if (sock->state == SS_READY) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400218 tipc_port_set_unreturnable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700219 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400220 tipc_port_set_unreliable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700221 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100222 return 0;
223}
224
225/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400226 * tipc_sock_create_local - create TIPC socket from inside TIPC module
227 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
228 *
229 * We cannot use sock_creat_kern here because it bumps module user count.
230 * Since socket owner and creator is the same module we must make sure
231 * that module count remains zero for module local sockets, otherwise
232 * we cannot do rmmod.
233 *
234 * Returns 0 on success, errno otherwise
235 */
236int tipc_sock_create_local(int type, struct socket **res)
237{
238 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400239
240 rc = sock_create_lite(AF_TIPC, type, 0, res);
241 if (rc < 0) {
242 pr_err("Failed to create kernel socket\n");
243 return rc;
244 }
245 tipc_sk_create(&init_net, *res, 0, 1);
246
Ying Xuec5fa7b32013-06-17 10:54:39 -0400247 return 0;
248}
249
250/**
251 * tipc_sock_release_local - release socket created by tipc_sock_create_local
252 * @sock: the socket to be released.
253 *
254 * Module reference count is not incremented when such sockets are created,
255 * so we must keep it from being decremented when they are released.
256 */
257void tipc_sock_release_local(struct socket *sock)
258{
Ying Xue247f0f32014-02-18 16:06:46 +0800259 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400260 sock->ops = NULL;
261 sock_release(sock);
262}
263
264/**
265 * tipc_sock_accept_local - accept a connection on a socket created
266 * with tipc_sock_create_local. Use this function to avoid that
267 * module reference count is inadvertently incremented.
268 *
269 * @sock: the accepting socket
270 * @newsock: reference to the new socket to be created
271 * @flags: socket flags
272 */
273
274int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400275 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400276{
277 struct sock *sk = sock->sk;
278 int ret;
279
280 ret = sock_create_lite(sk->sk_family, sk->sk_type,
281 sk->sk_protocol, newsock);
282 if (ret < 0)
283 return ret;
284
Ying Xue247f0f32014-02-18 16:06:46 +0800285 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400286 if (ret < 0) {
287 sock_release(*newsock);
288 return ret;
289 }
290 (*newsock)->ops = sock->ops;
291 return ret;
292}
293
294/**
Ying Xue247f0f32014-02-18 16:06:46 +0800295 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296 * @sock: socket to destroy
297 *
298 * This routine cleans up any messages that are still queued on the socket.
299 * For DGRAM and RDM socket types, all queued messages are rejected.
300 * For SEQPACKET and STREAM socket types, the first message is rejected
301 * and any others are discarded. (If the first message on a STREAM socket
302 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900303 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 * NOTE: Rejected messages are not necessarily returned to the sender! They
305 * are returned or discarded according to the "destination droppable" setting
306 * specified for the message by the sender.
307 *
308 * Returns 0 on success, errno otherwise
309 */
Ying Xue247f0f32014-02-18 16:06:46 +0800310static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400313 struct tipc_sock *tsk;
314 struct tipc_port *port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500316 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317
Allan Stephens0c3141e2008-04-15 00:22:02 -0700318 /*
319 * Exit if socket isn't fully initialized (occurs when a failed accept()
320 * releases a pre-allocated child socket that was never used)
321 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700322 if (sk == NULL)
323 return 0;
324
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400325 tsk = tipc_sk(sk);
326 port = &tsk->port;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700327 lock_sock(sk);
328
329 /*
330 * Reject all unreceived messages, except on an active connection
331 * (which disconnects locally & sends a 'FIN+' to peer)
332 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700334 buf = __skb_dequeue(&sk->sk_receive_queue);
335 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336 break;
Ying Xue40682432013-10-18 07:23:16 +0200337 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400338 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700339 else {
340 if ((sock->state == SS_CONNECTING) ||
341 (sock->state == SS_CONNECTED)) {
342 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -0400343 port->connected = 0;
344 tipc_node_remove_conn(tipc_port_peernode(port),
345 port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700346 }
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500347 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400348 tipc_link_xmit(buf, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700349 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100350 }
351
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400352 /* Destroy TIPC port; also disconnects an active connection and
353 * sends a 'FIN-' to peer.
Allan Stephens0c3141e2008-04-15 00:22:02 -0700354 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400355 tipc_port_destroy(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356
Allan Stephens0c3141e2008-04-15 00:22:02 -0700357 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100358 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359
Allan Stephens0c3141e2008-04-15 00:22:02 -0700360 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700361 sock->state = SS_DISCONNECTING;
362 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363
364 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700365 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200367 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368}
369
370/**
Ying Xue247f0f32014-02-18 16:06:46 +0800371 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372 * @sock: socket structure
373 * @uaddr: socket address describing name(s) and desired operation
374 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900375 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376 * Name and name sequence binding is indicated using a positive scope value;
377 * a negative scope value unbinds the specified name. Specifying no name
378 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900379 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700381 *
382 * NOTE: This routine doesn't need to take the socket lock since it doesn't
383 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384 */
Ying Xue247f0f32014-02-18 16:06:46 +0800385static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
386 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387{
Ying Xue84602762013-12-27 10:18:28 +0800388 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400390 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800391 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392
Ying Xue84602762013-12-27 10:18:28 +0800393 lock_sock(sk);
394 if (unlikely(!uaddr_len)) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400395 res = tipc_withdraw(&tsk->port, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800396 goto exit;
397 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900398
Ying Xue84602762013-12-27 10:18:28 +0800399 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
400 res = -EINVAL;
401 goto exit;
402 }
403 if (addr->family != AF_TIPC) {
404 res = -EAFNOSUPPORT;
405 goto exit;
406 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408 if (addr->addrtype == TIPC_ADDR_NAME)
409 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800410 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
411 res = -EAFNOSUPPORT;
412 goto exit;
413 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900414
Ying Xue13a2e892013-06-17 10:54:40 -0400415 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400416 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800417 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
418 res = -EACCES;
419 goto exit;
420 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400421
Ying Xue84602762013-12-27 10:18:28 +0800422 res = (addr->scope > 0) ?
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400423 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
424 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800425exit:
426 release_sock(sk);
427 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428}
429
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900430/**
Ying Xue247f0f32014-02-18 16:06:46 +0800431 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432 * @sock: socket structure
433 * @uaddr: area for returned socket address
434 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700435 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900436 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700438 *
Allan Stephens2da59912008-07-14 22:43:32 -0700439 * NOTE: This routine doesn't need to take the socket lock since it only
440 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000441 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 */
Ying Xue247f0f32014-02-18 16:06:46 +0800443static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
444 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400447 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000449 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700450 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700451 if ((sock->state != SS_CONNECTED) &&
452 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
453 return -ENOTCONN;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400454 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
455 addr->addr.id.node = tipc_port_peernode(&tsk->port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700456 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400457 addr->addr.id.ref = tsk->port.ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000458 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700459 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460
461 *uaddr_len = sizeof(*addr);
462 addr->addrtype = TIPC_ADDR_ID;
463 addr->family = AF_TIPC;
464 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465 addr->addr.name.domain = 0;
466
Allan Stephens0c3141e2008-04-15 00:22:02 -0700467 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468}
469
470/**
Ying Xue247f0f32014-02-18 16:06:46 +0800471 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472 * @file: file structure associated with the socket
473 * @sock: socket for which to calculate the poll bits
474 * @wait: ???
475 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700476 * Returns pollmask value
477 *
478 * COMMENTARY:
479 * It appears that the usual socket locking mechanisms are not useful here
480 * since the pollmask info is potentially out-of-date the moment this routine
481 * exits. TCP and other protocols seem to rely on higher level poll routines
482 * to handle any preventable race conditions, so TIPC will do the same ...
483 *
484 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700485 *
Allan Stephensf662c072010-08-17 11:00:06 +0000486 * socket state flags set
487 * ------------ ---------
488 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200489 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000490 *
491 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
492 * no write flags
493 *
494 * connected POLLIN/POLLRDNORM if data in rx queue
495 * POLLOUT if port is not congested
496 *
497 * disconnecting POLLIN/POLLRDNORM/POLLHUP
498 * no write flags
499 *
500 * listening POLLIN if SYN in rx queue
501 * no write flags
502 *
503 * ready POLLIN/POLLRDNORM if data in rx queue
504 * [connectionless] POLLOUT (since port cannot be congested)
505 *
506 * IMPORTANT: The fact that a read or write operation is indicated does NOT
507 * imply that the operation will succeed, merely that it should be performed
508 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 */
Ying Xue247f0f32014-02-18 16:06:46 +0800510static unsigned int tipc_poll(struct file *file, struct socket *sock,
511 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512{
Allan Stephens9b674e82008-03-26 16:48:21 -0700513 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400514 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000515 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700516
Ying Xuef288bef2012-08-21 11:16:57 +0800517 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700518
Allan Stephensf662c072010-08-17 11:00:06 +0000519 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200520 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500521 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200522 mask |= POLLOUT;
523 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000524 case SS_READY:
525 case SS_CONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500526 if (!tsk->link_cong && !tipc_sk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000527 mask |= POLLOUT;
528 /* fall thru' */
529 case SS_CONNECTING:
530 case SS_LISTENING:
531 if (!skb_queue_empty(&sk->sk_receive_queue))
532 mask |= (POLLIN | POLLRDNORM);
533 break;
534 case SS_DISCONNECTING:
535 mask = (POLLIN | POLLRDNORM | POLLHUP);
536 break;
537 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700538
539 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540}
541
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400542/**
543 * tipc_sendmcast - send multicast message
544 * @sock: socket structure
545 * @seq: destination address
546 * @iov: message data to send
547 * @dsz: total length of message data
548 * @timeo: timeout to wait for wakeup
549 *
550 * Called from function tipc_sendmsg(), which has done all sanity checks
551 * Returns the number of bytes sent on success, or errno
552 */
553static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
554 struct iovec *iov, size_t dsz, long timeo)
555{
556 struct sock *sk = sock->sk;
557 struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr;
558 struct sk_buff *buf;
559 uint mtu;
560 int rc;
561
562 msg_set_type(mhdr, TIPC_MCAST_MSG);
563 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
564 msg_set_destport(mhdr, 0);
565 msg_set_destnode(mhdr, 0);
566 msg_set_nametype(mhdr, seq->type);
567 msg_set_namelower(mhdr, seq->lower);
568 msg_set_nameupper(mhdr, seq->upper);
569 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
570
571new_mtu:
572 mtu = tipc_bclink_get_mtu();
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400573 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400574 if (unlikely(rc < 0))
575 return rc;
576
577 do {
578 rc = tipc_bclink_xmit(buf);
579 if (likely(rc >= 0)) {
580 rc = dsz;
581 break;
582 }
583 if (rc == -EMSGSIZE)
584 goto new_mtu;
585 if (rc != -ELINKCONG)
586 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400587 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400588 rc = tipc_wait_for_sndmsg(sock, &timeo);
589 if (rc)
590 kfree_skb_list(buf);
591 } while (!rc);
592 return rc;
593}
594
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400595/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
596 */
597void tipc_sk_mcast_rcv(struct sk_buff *buf)
598{
599 struct tipc_msg *msg = buf_msg(buf);
600 struct tipc_port_list dports = {0, NULL, };
601 struct tipc_port_list *item;
602 struct sk_buff *b;
603 uint i, last, dst = 0;
604 u32 scope = TIPC_CLUSTER_SCOPE;
605
606 if (in_own_node(msg_orignode(msg)))
607 scope = TIPC_NODE_SCOPE;
608
609 /* Create destination port list: */
610 tipc_nametbl_mc_translate(msg_nametype(msg),
611 msg_namelower(msg),
612 msg_nameupper(msg),
613 scope,
614 &dports);
615 last = dports.count;
616 if (!last) {
617 kfree_skb(buf);
618 return;
619 }
620
621 for (item = &dports; item; item = item->next) {
622 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
623 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
624 if (!b) {
625 pr_warn("Failed do clone mcast rcv buffer\n");
626 continue;
627 }
628 msg_set_destport(msg, item->ports[i]);
629 tipc_sk_rcv(b);
630 }
631 }
632 tipc_port_list_free(&dports);
633}
634
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900635/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500636 * tipc_sk_proto_rcv - receive a connection mng protocol message
637 * @tsk: receiving socket
638 * @dnode: node to send response message to, if any
639 * @buf: buffer containing protocol message
640 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
641 * (CONN_PROBE_REPLY) message should be forwarded.
642 */
Wei Yongjun52f50ce2014-07-20 13:14:28 +0800643static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
644 struct sk_buff *buf)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500645{
646 struct tipc_msg *msg = buf_msg(buf);
647 struct tipc_port *port = &tsk->port;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500648 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500649
650 /* Ignore if connection cannot be validated: */
651 if (!port->connected || !tipc_port_peer_msg(port, msg))
652 goto exit;
653
654 port->probing_state = TIPC_CONN_OK;
655
656 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500657 conn_cong = tipc_sk_conn_cong(tsk);
658 tsk->sent_unacked -= msg_msgcnt(msg);
659 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400660 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500661 } else if (msg_type(msg) == CONN_PROBE) {
662 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
663 return TIPC_OK;
664 msg_set_type(msg, CONN_PROBE_REPLY);
665 return TIPC_FWD_MSG;
666 }
667 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
668exit:
669 kfree_skb(buf);
670 return TIPC_OK;
671}
672
673/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674 * dest_name_check - verify user is permitted to send to specified port name
675 * @dest: destination address
676 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900677 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678 * Prevents restricted configuration commands from being issued by
679 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900680 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681 * Returns 0 if permission is granted, otherwise errno
682 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800683static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100684{
685 struct tipc_cfg_msg_hdr hdr;
686
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500687 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
688 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900689 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
690 return 0;
691 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
692 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900693 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
694 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100695
Allan Stephens3f8dd942011-01-18 13:09:29 -0500696 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
697 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900698 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100699 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700700 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100701 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900702
Per Lidenb97bf3f2006-01-02 19:04:38 +0100703 return 0;
704}
705
Ying Xue3f405042014-01-17 09:50:05 +0800706static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
707{
708 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400709 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800710 DEFINE_WAIT(wait);
711 int done;
712
713 do {
714 int err = sock_error(sk);
715 if (err)
716 return err;
717 if (sock->state == SS_DISCONNECTING)
718 return -EPIPE;
719 if (!*timeo_p)
720 return -EAGAIN;
721 if (signal_pending(current))
722 return sock_intr_errno(*timeo_p);
723
724 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500725 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800726 finish_wait(sk_sleep(sk), &wait);
727 } while (!done);
728 return 0;
729}
730
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500731/**
Ying Xue247f0f32014-02-18 16:06:46 +0800732 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700733 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734 * @sock: socket structure
735 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500736 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900737 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100738 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900739 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100740 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
741 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900742 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743 * Returns the number of bytes sent on success, or errno otherwise
744 */
Ying Xue247f0f32014-02-18 16:06:46 +0800745static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500746 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500748 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700749 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400750 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400751 struct tipc_port *port = &tsk->port;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500752 struct tipc_msg *mhdr = &port->phdr;
753 struct iovec *iov = m->msg_iov;
754 u32 dnode, dport;
755 struct sk_buff *buf;
756 struct tipc_name_seq *seq = &dest->addr.nameseq;
757 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800758 long timeo;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500759 int rc = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760
761 if (unlikely(!dest))
762 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500763
Allan Stephens51f9cc12006-06-25 23:49:06 -0700764 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
765 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500767
768 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400769 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100770
Allan Stephens0c3141e2008-04-15 00:22:02 -0700771 if (iocb)
772 lock_sock(sk);
773
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500774 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700775 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500776 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700777 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700778 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700779 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500780 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700781 goto exit;
782 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400783 if (tsk->port.published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500784 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700785 goto exit;
786 }
787 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400788 tsk->port.conn_type = dest->addr.name.name.type;
789 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700790 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500792 rc = dest_name_check(dest, m);
793 if (rc)
794 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100795
Ying Xue3f405042014-01-17 09:50:05 +0800796 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500797
798 if (dest->addrtype == TIPC_ADDR_MCAST) {
799 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
800 goto exit;
801 } else if (dest->addrtype == TIPC_ADDR_NAME) {
802 u32 type = dest->addr.name.name.type;
803 u32 inst = dest->addr.name.name.instance;
804 u32 domain = dest->addr.name.domain;
805
806 dnode = domain;
807 msg_set_type(mhdr, TIPC_NAMED_MSG);
808 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
809 msg_set_nametype(mhdr, type);
810 msg_set_nameinst(mhdr, inst);
811 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
812 dport = tipc_nametbl_translate(type, inst, &dnode);
813 msg_set_destnode(mhdr, dnode);
814 msg_set_destport(mhdr, dport);
815 if (unlikely(!dport && !dnode)) {
816 rc = -EHOSTUNREACH;
817 goto exit;
818 }
819 } else if (dest->addrtype == TIPC_ADDR_ID) {
820 dnode = dest->addr.id.node;
821 msg_set_type(mhdr, TIPC_DIRECT_MSG);
822 msg_set_lookup_scope(mhdr, 0);
823 msg_set_destnode(mhdr, dnode);
824 msg_set_destport(mhdr, dest->addr.id.ref);
825 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
826 }
827
828new_mtu:
829 mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400830 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500831 if (rc < 0)
832 goto exit;
833
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900834 do {
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400835 TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong;
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400836 rc = tipc_link_xmit(buf, dnode, tsk->port.ref);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500837 if (likely(rc >= 0)) {
838 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700839 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500840 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700841 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900842 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500843 if (rc == -EMSGSIZE)
844 goto new_mtu;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500845 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700846 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400847 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500848 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400849 if (rc)
850 kfree_skb_list(buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500851 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700852exit:
853 if (iocb)
854 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500855
856 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857}
858
Ying Xue391a6dd2014-01-17 09:50:06 +0800859static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
860{
861 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400862 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800863 DEFINE_WAIT(wait);
864 int done;
865
866 do {
867 int err = sock_error(sk);
868 if (err)
869 return err;
870 if (sock->state == SS_DISCONNECTING)
871 return -EPIPE;
872 else if (sock->state != SS_CONNECTED)
873 return -ENOTCONN;
874 if (!*timeo_p)
875 return -EAGAIN;
876 if (signal_pending(current))
877 return sock_intr_errno(*timeo_p);
878
879 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
880 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -0500881 (!tsk->link_cong &&
882 !tipc_sk_conn_cong(tsk)) ||
883 !tsk->port.connected);
Ying Xue391a6dd2014-01-17 09:50:06 +0800884 finish_wait(sk_sleep(sk), &wait);
885 } while (!done);
886 return 0;
887}
888
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900889/**
Ying Xue247f0f32014-02-18 16:06:46 +0800890 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 * @iocb: (unused)
892 * @sock: socket structure
893 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500894 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900895 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100896 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900897 *
898 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700899 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900 */
Ying Xue247f0f32014-02-18 16:06:46 +0800901static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500902 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700904 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400905 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500906 struct tipc_port *port = &tsk->port;
907 struct tipc_msg *mhdr = &port->phdr;
908 struct sk_buff *buf;
909 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
910 u32 ref = port->ref;
911 int rc = -EINVAL;
912 long timeo;
913 u32 dnode;
914 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900915
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500916 /* Handle implied connection establishment */
917 if (unlikely(dest)) {
918 rc = tipc_sendmsg(iocb, sock, m, dsz);
919 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -0500920 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500921 return rc;
922 }
923 if (dsz > (uint)INT_MAX)
924 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700925
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500926 if (iocb)
927 lock_sock(sk);
928
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900929 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500930 if (sock->state == SS_DISCONNECTING)
931 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +0800932 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500933 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800934 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900935 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500937 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
938 dnode = tipc_port_peernode(port);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500939
940next:
941 mtu = port->max_pkt;
942 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400943 rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500944 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700945 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500946 do {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500947 if (likely(!tipc_sk_conn_cong(tsk))) {
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400948 rc = tipc_link_xmit(buf, dnode, ref);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500949 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500950 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500951 sent += send;
952 if (sent == dsz)
953 break;
954 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700955 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500956 if (rc == -EMSGSIZE) {
957 port->max_pkt = tipc_node_get_mtu(dnode, ref);
958 goto next;
959 }
960 if (rc != -ELINKCONG)
961 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400962 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100963 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500964 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400965 if (rc)
966 kfree_skb_list(buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500967 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700968exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500969 if (iocb)
970 release_sock(sk);
971 return sent ? sent : rc;
972}
973
974/**
975 * tipc_send_packet - send a connection-oriented message
976 * @iocb: if NULL, indicates that socket lock is already held
977 * @sock: socket structure
978 * @m: message to send
979 * @dsz: length of data to be transmitted
980 *
981 * Used for SOCK_SEQPACKET messages.
982 *
983 * Returns the number of bytes sent on success, or errno otherwise
984 */
985static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
986 struct msghdr *m, size_t dsz)
987{
988 if (dsz > TIPC_MAX_USER_MSG_SIZE)
989 return -EMSGSIZE;
990
991 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100992}
993
Jon Paul Maloydadebc02014-08-22 18:09:11 -0400994/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995 */
Jon Paul Maloydadebc02014-08-22 18:09:11 -0400996static void tipc_sk_finish_conn(struct tipc_port *port, u32 peer_port,
997 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100998{
Jon Paul Maloydadebc02014-08-22 18:09:11 -0400999 struct tipc_msg *msg = &port->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001000
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001001 msg_set_destnode(msg, peer_node);
1002 msg_set_destport(msg, peer_port);
1003 msg_set_type(msg, TIPC_CONN_MSG);
1004 msg_set_lookup_scope(msg, 0);
1005 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001006
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001007 port->probing_interval = CONN_PROBING_INTERVAL;
1008 port->probing_state = TIPC_CONN_OK;
1009 port->connected = 1;
1010 k_start_timer(&port->timer, port->probing_interval);
1011 tipc_node_add_conn(peer_node, port->ref, peer_port);
1012 port->max_pkt = tipc_node_get_mtu(peer_node, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013}
1014
1015/**
1016 * set_orig_addr - capture sender's address for received message
1017 * @m: descriptor for message info
1018 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001019 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020 * Note: Address is not captured if not requested by receiver.
1021 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001022static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001024 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001026 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027 addr->family = AF_TIPC;
1028 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001029 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030 addr->addr.id.ref = msg_origport(msg);
1031 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001032 addr->addr.name.domain = 0; /* could leave uninitialized */
1033 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034 m->msg_namelen = sizeof(struct sockaddr_tipc);
1035 }
1036}
1037
1038/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001039 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 * @m: descriptor for message info
1041 * @msg: received message header
1042 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001043 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001044 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001045 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046 * Returns 0 if successful, otherwise errno
1047 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001048static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001049 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050{
1051 u32 anc_data[3];
1052 u32 err;
1053 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001054 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 int res;
1056
1057 if (likely(m->msg_controllen == 0))
1058 return 0;
1059
1060 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061 err = msg ? msg_errcode(msg) : 0;
1062 if (unlikely(err)) {
1063 anc_data[0] = err;
1064 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001065 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1066 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001068 if (anc_data[1]) {
1069 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1070 msg_data(msg));
1071 if (res)
1072 return res;
1073 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 }
1075
1076 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1078 switch (dest_type) {
1079 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001080 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001081 anc_data[0] = msg_nametype(msg);
1082 anc_data[1] = msg_namelower(msg);
1083 anc_data[2] = msg_namelower(msg);
1084 break;
1085 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001086 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001087 anc_data[0] = msg_nametype(msg);
1088 anc_data[1] = msg_namelower(msg);
1089 anc_data[2] = msg_nameupper(msg);
1090 break;
1091 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001092 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093 anc_data[0] = tport->conn_type;
1094 anc_data[1] = tport->conn_instance;
1095 anc_data[2] = tport->conn_instance;
1096 break;
1097 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001098 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001099 }
Allan Stephens2db99832010-12-31 18:59:33 +00001100 if (has_name) {
1101 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1102 if (res)
1103 return res;
1104 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001105
1106 return 0;
1107}
1108
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001109static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001110{
1111 struct sock *sk = sock->sk;
1112 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001113 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001114 int err;
1115
1116 for (;;) {
1117 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001118 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001119 if (sock->state == SS_DISCONNECTING) {
1120 err = -ENOTCONN;
1121 break;
1122 }
1123 release_sock(sk);
1124 timeo = schedule_timeout(timeo);
1125 lock_sock(sk);
1126 }
1127 err = 0;
1128 if (!skb_queue_empty(&sk->sk_receive_queue))
1129 break;
1130 err = sock_intr_errno(timeo);
1131 if (signal_pending(current))
1132 break;
1133 err = -EAGAIN;
1134 if (!timeo)
1135 break;
1136 }
1137 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001138 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001139 return err;
1140}
1141
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001142/**
Ying Xue247f0f32014-02-18 16:06:46 +08001143 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144 * @iocb: (unused)
1145 * @m: descriptor for message info
1146 * @buf_len: total size of user buffer area
1147 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001148 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001149 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1150 * If the complete message doesn't fit in user area, truncate it.
1151 *
1152 * Returns size of returned message data, errno otherwise
1153 */
Ying Xue247f0f32014-02-18 16:06:46 +08001154static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1155 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001157 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001158 struct tipc_sock *tsk = tipc_sk(sk);
1159 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160 struct sk_buff *buf;
1161 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001162 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 unsigned int sz;
1164 u32 err;
1165 int res;
1166
Allan Stephens0c3141e2008-04-15 00:22:02 -07001167 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 if (unlikely(!buf_len))
1169 return -EINVAL;
1170
Allan Stephens0c3141e2008-04-15 00:22:02 -07001171 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172
Allan Stephens0c3141e2008-04-15 00:22:02 -07001173 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 res = -ENOTCONN;
1175 goto exit;
1176 }
1177
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001178 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001179restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180
Allan Stephens0c3141e2008-04-15 00:22:02 -07001181 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001182 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001183 if (res)
1184 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001185
1186 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001187 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001188 msg = buf_msg(buf);
1189 sz = msg_data_sz(msg);
1190 err = msg_errcode(msg);
1191
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001193 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001194 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 goto restart;
1196 }
1197
1198 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 set_orig_addr(m, msg);
1200
1201 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001202 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001203 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001204 goto exit;
1205
1206 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 if (!err) {
1208 if (unlikely(buf_len < sz)) {
1209 sz = buf_len;
1210 m->msg_flags |= MSG_TRUNC;
1211 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001212 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1213 m->msg_iov, sz);
1214 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216 res = sz;
1217 } else {
1218 if ((sock->state == SS_READY) ||
1219 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1220 res = 0;
1221 else
1222 res = -ECONNRESET;
1223 }
1224
1225 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001227 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001228 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1229 tipc_acknowledge(port->ref, tsk->rcv_unacked);
1230 tsk->rcv_unacked = 0;
1231 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001232 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001233 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001235 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 return res;
1237}
1238
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001239/**
Ying Xue247f0f32014-02-18 16:06:46 +08001240 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241 * @iocb: (unused)
1242 * @m: descriptor for message info
1243 * @buf_len: total size of user buffer area
1244 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001245 *
1246 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001247 * will optionally wait for more; never truncates data.
1248 *
1249 * Returns size of returned message data, errno otherwise
1250 */
Ying Xue247f0f32014-02-18 16:06:46 +08001251static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1252 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001253{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001254 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001255 struct tipc_sock *tsk = tipc_sk(sk);
1256 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001257 struct sk_buff *buf;
1258 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001259 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001260 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001261 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001263 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001264 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001265
1266 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001267 if (unlikely(!buf_len))
1268 return -EINVAL;
1269
Allan Stephens0c3141e2008-04-15 00:22:02 -07001270 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001271
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001272 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001273 res = -ENOTCONN;
1274 goto exit;
1275 }
1276
Florian Westphal3720d402010-08-17 11:00:04 +00001277 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001278 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001279
Allan Stephens0c3141e2008-04-15 00:22:02 -07001280restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001281 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001282 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001283 if (res)
1284 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001285
1286 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001287 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001288 msg = buf_msg(buf);
1289 sz = msg_data_sz(msg);
1290 err = msg_errcode(msg);
1291
1292 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001294 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295 goto restart;
1296 }
1297
1298 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001299 if (sz_copied == 0) {
1300 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001301 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001302 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001303 goto exit;
1304 }
1305
1306 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001307 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001308 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001309
Allan Stephens0232fd02011-02-21 09:45:40 -05001310 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001311 needed = (buf_len - sz_copied);
1312 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001313
1314 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1315 m->msg_iov, sz_to_copy);
1316 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001317 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001318
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319 sz_copied += sz_to_copy;
1320
1321 if (sz_to_copy < sz) {
1322 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001323 TIPC_SKB_CB(buf)->handle =
1324 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325 goto exit;
1326 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001327 } else {
1328 if (sz_copied != 0)
1329 goto exit; /* can't add error msg to valid data */
1330
1331 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1332 res = 0;
1333 else
1334 res = -ECONNRESET;
1335 }
1336
1337 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001339 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1340 tipc_acknowledge(port->ref, tsk->rcv_unacked);
1341 tsk->rcv_unacked = 0;
1342 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001343 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001344 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001345
1346 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001347 if ((sz_copied < buf_len) && /* didn't get all requested data */
1348 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001349 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001350 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1351 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352 goto restart;
1353
1354exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001355 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001356 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357}
1358
1359/**
Ying Xuef288bef2012-08-21 11:16:57 +08001360 * tipc_write_space - wake up thread if port congestion is released
1361 * @sk: socket
1362 */
1363static void tipc_write_space(struct sock *sk)
1364{
1365 struct socket_wq *wq;
1366
1367 rcu_read_lock();
1368 wq = rcu_dereference(sk->sk_wq);
1369 if (wq_has_sleeper(wq))
1370 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1371 POLLWRNORM | POLLWRBAND);
1372 rcu_read_unlock();
1373}
1374
1375/**
1376 * tipc_data_ready - wake up threads to indicate messages have been received
1377 * @sk: socket
1378 * @len: the length of messages
1379 */
David S. Miller676d2362014-04-11 16:15:36 -04001380static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001381{
1382 struct socket_wq *wq;
1383
1384 rcu_read_lock();
1385 wq = rcu_dereference(sk->sk_wq);
1386 if (wq_has_sleeper(wq))
1387 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1388 POLLRDNORM | POLLRDBAND);
1389 rcu_read_unlock();
1390}
1391
1392/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001393 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001394 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001395 * @msg: message
1396 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001397 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001398 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001399static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001400{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001401 struct sock *sk = &tsk->sk;
1402 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001403 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001404 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001405
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001406 int retval = -TIPC_ERR_NO_PORT;
Ying Xue7e6c1312012-11-29 18:39:14 -05001407
1408 if (msg_mcast(msg))
1409 return retval;
1410
1411 switch ((int)sock->state) {
1412 case SS_CONNECTED:
1413 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001414 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001415 if (unlikely(msg_errcode(msg))) {
1416 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001417 port->connected = 0;
1418 /* let timer expire on it's own */
1419 tipc_node_remove_conn(tipc_port_peernode(port),
1420 port->ref);
Ying Xue7e6c1312012-11-29 18:39:14 -05001421 }
1422 retval = TIPC_OK;
1423 }
1424 break;
1425 case SS_CONNECTING:
1426 /* Accept only ACK or NACK message */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001427
1428 if (unlikely(!msg_connected(msg)))
1429 break;
1430
Ying Xue584d24b2012-11-29 18:51:19 -05001431 if (unlikely(msg_errcode(msg))) {
1432 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001433 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001434 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001435 break;
1436 }
1437
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001438 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
Ying Xue584d24b2012-11-29 18:51:19 -05001439 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001440 sk->sk_err = EINVAL;
Ying Xue584d24b2012-11-29 18:51:19 -05001441 retval = TIPC_OK;
1442 break;
1443 }
1444
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001445 tipc_sk_finish_conn(port, msg_origport(msg), msg_orignode(msg));
1446 msg_set_importance(&port->phdr, msg_importance(msg));
1447 sock->state = SS_CONNECTED;
1448
Ying Xue584d24b2012-11-29 18:51:19 -05001449 /* If an incoming message is an 'ACK-', it should be
1450 * discarded here because it doesn't contain useful
1451 * data. In addition, we should try to wake up
1452 * connect() routine if sleeping.
1453 */
1454 if (msg_data_sz(msg) == 0) {
1455 kfree_skb(*buf);
1456 *buf = NULL;
1457 if (waitqueue_active(sk_sleep(sk)))
1458 wake_up_interruptible(sk_sleep(sk));
1459 }
1460 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001461 break;
1462 case SS_LISTENING:
1463 case SS_UNCONNECTED:
1464 /* Accept only SYN message */
1465 if (!msg_connected(msg) && !(msg_errcode(msg)))
1466 retval = TIPC_OK;
1467 break;
1468 case SS_DISCONNECTING:
1469 break;
1470 default:
1471 pr_err("Unknown socket state %u\n", sock->state);
1472 }
1473 return retval;
1474}
1475
1476/**
Ying Xueaba79f32013-01-20 23:30:09 +01001477 * rcvbuf_limit - get proper overload limit of socket receive queue
1478 * @sk: socket
1479 * @buf: message
1480 *
1481 * For all connection oriented messages, irrespective of importance,
1482 * the default overload value (i.e. 67MB) is set as limit.
1483 *
1484 * For all connectionless messages, by default new queue limits are
1485 * as belows:
1486 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001487 * TIPC_LOW_IMPORTANCE (4 MB)
1488 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1489 * TIPC_HIGH_IMPORTANCE (16 MB)
1490 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001491 *
1492 * Returns overload limit according to corresponding message importance
1493 */
1494static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1495{
1496 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001497
1498 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001499 return sysctl_tipc_rmem[2];
1500
1501 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1502 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001503}
1504
1505/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001506 * filter_rcv - validate incoming message
1507 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001508 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001509 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001510 * Enqueues message on receive queue if acceptable; optionally handles
1511 * disconnect indication for a connected socket.
1512 *
1513 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001514 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001515 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001516 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001517 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001518static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001519{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001520 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001521 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001522 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001523 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001524 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001525 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001526
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001527 if (unlikely(msg_user(msg) == CONN_MANAGER))
1528 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001529
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001530 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1531 kfree_skb(buf);
1532 tsk->link_cong = 0;
1533 sk->sk_write_space(sk);
1534 return TIPC_OK;
1535 }
1536
Per Lidenb97bf3f2006-01-02 19:04:38 +01001537 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001538 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001539 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001540
Per Lidenb97bf3f2006-01-02 19:04:38 +01001541 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001542 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001543 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001544 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001545 rc = filter_connect(tsk, &buf);
1546 if (rc != TIPC_OK || buf == NULL)
1547 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001548 }
1549
1550 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001551 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001552 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001553
Ying Xueaba79f32013-01-20 23:30:09 +01001554 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001555 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001556 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001557 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001558
David S. Miller676d2362014-04-11 16:15:36 -04001559 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001560 return TIPC_OK;
1561}
1562
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001563/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001564 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001565 * @sk: socket
1566 * @buf: message
1567 *
1568 * Caller must hold socket lock, but not port lock.
1569 *
1570 * Returns 0
1571 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001572static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001573{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001574 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001575 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001576 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001577 uint truesize = buf->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001578
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001579 rc = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001580
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001581 if (likely(!rc)) {
1582 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1583 atomic_add(truesize, &tsk->dupl_rcvcnt);
1584 return 0;
1585 }
1586
1587 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1588 return 0;
1589
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001590 tipc_link_xmit(buf, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001591
Allan Stephens0c3141e2008-04-15 00:22:02 -07001592 return 0;
1593}
1594
1595/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001596 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001597 * @buf: buffer containing arriving message
1598 * Consumes buffer
1599 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001600 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001601int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001602{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001603 struct tipc_sock *tsk;
1604 struct tipc_port *port;
1605 struct sock *sk;
1606 u32 dport = msg_destport(buf_msg(buf));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001607 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001608 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001609 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001610
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001611 /* Validate destination and message */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001612 port = tipc_port_lock(dport);
1613 if (unlikely(!port)) {
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001614 rc = tipc_msg_eval(buf, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001615 goto exit;
1616 }
1617
1618 tsk = tipc_port_to_sock(port);
1619 sk = &tsk->sk;
1620
1621 /* Queue message */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001622 bh_lock_sock(sk);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001623
Allan Stephens0c3141e2008-04-15 00:22:02 -07001624 if (!sock_owned_by_user(sk)) {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001625 rc = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001626 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001627 if (sk->sk_backlog.len == 0)
1628 atomic_set(&tsk->dupl_rcvcnt, 0);
1629 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1630 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001631 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001632 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001633 bh_unlock_sock(sk);
1634 tipc_port_unlock(port);
1635
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001636 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001637 return 0;
1638exit:
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001639 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001640 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001641
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001642 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001643 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001644}
1645
Ying Xue78eb3a52014-01-17 09:50:03 +08001646static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1647{
1648 struct sock *sk = sock->sk;
1649 DEFINE_WAIT(wait);
1650 int done;
1651
1652 do {
1653 int err = sock_error(sk);
1654 if (err)
1655 return err;
1656 if (!*timeo_p)
1657 return -ETIMEDOUT;
1658 if (signal_pending(current))
1659 return sock_intr_errno(*timeo_p);
1660
1661 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1662 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1663 finish_wait(sk_sleep(sk), &wait);
1664 } while (!done);
1665 return 0;
1666}
1667
Per Lidenb97bf3f2006-01-02 19:04:38 +01001668/**
Ying Xue247f0f32014-02-18 16:06:46 +08001669 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670 * @sock: socket structure
1671 * @dest: socket address for destination port
1672 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001673 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674 *
1675 * Returns 0 on success, errno otherwise
1676 */
Ying Xue247f0f32014-02-18 16:06:46 +08001677static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1678 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001680 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001681 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1682 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001683 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1684 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001685 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001686
Allan Stephens0c3141e2008-04-15 00:22:02 -07001687 lock_sock(sk);
1688
Allan Stephensb89741a2008-04-15 00:20:37 -07001689 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001690 if (sock->state == SS_READY) {
1691 res = -EOPNOTSUPP;
1692 goto exit;
1693 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001694
Allan Stephensb89741a2008-04-15 00:20:37 -07001695 /*
1696 * Reject connection attempt using multicast address
1697 *
1698 * Note: send_msg() validates the rest of the address fields,
1699 * so there's no need to do it here
1700 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001701 if (dst->addrtype == TIPC_ADDR_MCAST) {
1702 res = -EINVAL;
1703 goto exit;
1704 }
1705
Ying Xue78eb3a52014-01-17 09:50:03 +08001706 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001707 switch (sock->state) {
1708 case SS_UNCONNECTED:
1709 /* Send a 'SYN-' to destination */
1710 m.msg_name = dest;
1711 m.msg_namelen = destlen;
1712
1713 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1714 * indicate send_msg() is never blocked.
1715 */
1716 if (!timeout)
1717 m.msg_flags = MSG_DONTWAIT;
1718
Ying Xue247f0f32014-02-18 16:06:46 +08001719 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001720 if ((res < 0) && (res != -EWOULDBLOCK))
1721 goto exit;
1722
1723 /* Just entered SS_CONNECTING state; the only
1724 * difference is that return value in non-blocking
1725 * case is EINPROGRESS, rather than EALREADY.
1726 */
1727 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001728 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001729 if (previous == SS_CONNECTING)
1730 res = -EALREADY;
1731 if (!timeout)
1732 goto exit;
1733 timeout = msecs_to_jiffies(timeout);
1734 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1735 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001736 break;
1737 case SS_CONNECTED:
1738 res = -EISCONN;
1739 break;
1740 default:
1741 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001742 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001743 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001744exit:
1745 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001746 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001747}
1748
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001749/**
Ying Xue247f0f32014-02-18 16:06:46 +08001750 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001751 * @sock: socket structure
1752 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001753 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001754 * Returns 0 on success, errno otherwise
1755 */
Ying Xue247f0f32014-02-18 16:06:46 +08001756static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001757{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001758 struct sock *sk = sock->sk;
1759 int res;
1760
1761 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001762
Ying Xue245f3d32011-07-06 06:01:13 -04001763 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001764 res = -EINVAL;
1765 else {
1766 sock->state = SS_LISTENING;
1767 res = 0;
1768 }
1769
1770 release_sock(sk);
1771 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001772}
1773
Ying Xue6398e232014-01-17 09:50:04 +08001774static int tipc_wait_for_accept(struct socket *sock, long timeo)
1775{
1776 struct sock *sk = sock->sk;
1777 DEFINE_WAIT(wait);
1778 int err;
1779
1780 /* True wake-one mechanism for incoming connections: only
1781 * one process gets woken up, not the 'whole herd'.
1782 * Since we do not 'race & poll' for established sockets
1783 * anymore, the common case will execute the loop only once.
1784 */
1785 for (;;) {
1786 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1787 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001788 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001789 release_sock(sk);
1790 timeo = schedule_timeout(timeo);
1791 lock_sock(sk);
1792 }
1793 err = 0;
1794 if (!skb_queue_empty(&sk->sk_receive_queue))
1795 break;
1796 err = -EINVAL;
1797 if (sock->state != SS_LISTENING)
1798 break;
1799 err = sock_intr_errno(timeo);
1800 if (signal_pending(current))
1801 break;
1802 err = -EAGAIN;
1803 if (!timeo)
1804 break;
1805 }
1806 finish_wait(sk_sleep(sk), &wait);
1807 return err;
1808}
1809
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001810/**
Ying Xue247f0f32014-02-18 16:06:46 +08001811 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001812 * @sock: listening socket
1813 * @newsock: new socket that is to be connected
1814 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001815 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816 * Returns 0 on success, errno otherwise
1817 */
Ying Xue247f0f32014-02-18 16:06:46 +08001818static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001819{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001820 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001821 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001822 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001823 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08001824 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001825 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826
Allan Stephens0c3141e2008-04-15 00:22:02 -07001827 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001828
Allan Stephens0c3141e2008-04-15 00:22:02 -07001829 if (sock->state != SS_LISTENING) {
1830 res = -EINVAL;
1831 goto exit;
1832 }
Ying Xue6398e232014-01-17 09:50:04 +08001833 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1834 res = tipc_wait_for_accept(sock, timeo);
1835 if (res)
1836 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001837
1838 buf = skb_peek(&sk->sk_receive_queue);
1839
Ying Xuec5fa7b32013-06-17 10:54:39 -04001840 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001841 if (res)
1842 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001843
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001844 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001845 new_port = &tipc_sk(new_sk)->port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001846 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001847
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001848 /* we lock on new_sk; but lockdep sees the lock on sk */
1849 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001850
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001851 /*
1852 * Reject any stray messages received by new socket
1853 * before the socket lock was taken (very, very unlikely)
1854 */
1855 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001856
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001857 /* Connect new socket to it's peer */
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001858 tipc_sk_finish_conn(new_port, msg_origport(msg), msg_orignode(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001859 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001860
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001861 tipc_port_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001862 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001863 new_port->conn_type = msg_nametype(msg);
1864 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001865 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001866
1867 /*
1868 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1869 * Respond to 'SYN+' by queuing it on new socket.
1870 */
1871 if (!msg_data_sz(msg)) {
1872 struct msghdr m = {NULL,};
1873
1874 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001875 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001876 } else {
1877 __skb_dequeue(&sk->sk_receive_queue);
1878 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001879 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001880 }
1881 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001882exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001883 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001884 return res;
1885}
1886
1887/**
Ying Xue247f0f32014-02-18 16:06:46 +08001888 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001889 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001890 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001891 *
1892 * Terminates connection (if necessary), then purges socket's receive queue.
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_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001897{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001898 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001899 struct tipc_sock *tsk = tipc_sk(sk);
1900 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001901 struct sk_buff *buf;
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04001902 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001903 int res;
1904
Allan Stephense247a8f2008-03-06 15:05:38 -08001905 if (how != SHUT_RDWR)
1906 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907
Allan Stephens0c3141e2008-04-15 00:22:02 -07001908 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001909
1910 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001911 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001912 case SS_CONNECTED:
1913
Per Lidenb97bf3f2006-01-02 19:04:38 +01001914restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001915 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001916 buf = __skb_dequeue(&sk->sk_receive_queue);
1917 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001918 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001919 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001920 goto restart;
1921 }
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04001922 if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN))
1923 tipc_link_xmit(buf, dnode, port->ref);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001924 tipc_node_remove_conn(dnode, port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001925 } else {
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04001926 dnode = tipc_port_peernode(port);
1927 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
1928 TIPC_CONN_MSG, SHORT_H_SIZE,
1929 0, dnode, tipc_own_addr,
1930 tipc_port_peerport(port),
1931 port->ref, TIPC_CONN_SHUTDOWN);
1932 tipc_link_xmit(buf, dnode, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001933 }
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001934 port->connected = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001935 sock->state = SS_DISCONNECTING;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001936 tipc_node_remove_conn(dnode, port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001937 /* fall through */
1938
1939 case SS_DISCONNECTING:
1940
Ying Xue75031152012-10-29 09:38:15 -04001941 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001942 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001943
1944 /* Wake up anyone sleeping in poll */
1945 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001946 res = 0;
1947 break;
1948
1949 default:
1950 res = -ENOTCONN;
1951 }
1952
Allan Stephens0c3141e2008-04-15 00:22:02 -07001953 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001954 return res;
1955}
1956
Jon Paul Maloy57289012014-08-22 18:09:09 -04001957static void tipc_sk_timeout(unsigned long ref)
1958{
1959 struct tipc_port *port = tipc_port_lock(ref);
1960 struct tipc_sock *tsk;
1961 struct sock *sk;
1962 struct sk_buff *buf = NULL;
1963 struct tipc_msg *msg = NULL;
1964 u32 peer_port, peer_node;
1965
1966 if (!port)
1967 return;
1968
1969 if (!port->connected) {
1970 tipc_port_unlock(port);
1971 return;
1972 }
1973 tsk = tipc_port_to_sock(port);
1974 sk = &tsk->sk;
1975 bh_lock_sock(sk);
1976 peer_port = tipc_port_peerport(port);
1977 peer_node = tipc_port_peernode(port);
1978
1979 if (port->probing_state == TIPC_CONN_PROBING) {
1980 /* Previous probe not answered -> self abort */
1981 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
1982 SHORT_H_SIZE, 0, tipc_own_addr,
1983 peer_node, ref, peer_port,
1984 TIPC_ERR_NO_PORT);
1985 } else {
1986 buf = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
1987 0, peer_node, tipc_own_addr,
1988 peer_port, ref, TIPC_OK);
1989 port->probing_state = TIPC_CONN_PROBING;
1990 k_start_timer(&port->timer, port->probing_interval);
1991 }
1992 bh_unlock_sock(sk);
1993 tipc_port_unlock(port);
1994 if (!buf)
1995 return;
1996
1997 msg = buf_msg(buf);
1998 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
1999}
2000
Per Lidenb97bf3f2006-01-02 19:04:38 +01002001/**
Ying Xue247f0f32014-02-18 16:06:46 +08002002 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002003 * @sock: socket structure
2004 * @lvl: option level
2005 * @opt: option identifier
2006 * @ov: pointer to new option value
2007 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002008 *
2009 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002010 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002011 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002012 * Returns 0 on success, errno otherwise
2013 */
Ying Xue247f0f32014-02-18 16:06:46 +08002014static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2015 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002016{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002017 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002018 struct tipc_sock *tsk = tipc_sk(sk);
2019 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002020 u32 value;
2021 int res;
2022
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002023 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2024 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002025 if (lvl != SOL_TIPC)
2026 return -ENOPROTOOPT;
2027 if (ol < sizeof(value))
2028 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002029 res = get_user(value, (u32 __user *)ov);
2030 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002031 return res;
2032
Allan Stephens0c3141e2008-04-15 00:22:02 -07002033 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002034
Per Lidenb97bf3f2006-01-02 19:04:38 +01002035 switch (opt) {
2036 case TIPC_IMPORTANCE:
Erik Hugneac32c7f2014-08-15 16:44:35 +02002037 res = tipc_port_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002038 break;
2039 case TIPC_SRC_DROPPABLE:
2040 if (sock->type != SOCK_STREAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002041 tipc_port_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002042 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002043 res = -ENOPROTOOPT;
2044 break;
2045 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002046 tipc_port_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002047 break;
2048 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002049 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002050 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002051 break;
2052 default:
2053 res = -EINVAL;
2054 }
2055
Allan Stephens0c3141e2008-04-15 00:22:02 -07002056 release_sock(sk);
2057
Per Lidenb97bf3f2006-01-02 19:04:38 +01002058 return res;
2059}
2060
2061/**
Ying Xue247f0f32014-02-18 16:06:46 +08002062 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002063 * @sock: socket structure
2064 * @lvl: option level
2065 * @opt: option identifier
2066 * @ov: receptacle for option value
2067 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002068 *
2069 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002070 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002071 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002072 * Returns 0 on success, errno otherwise
2073 */
Ying Xue247f0f32014-02-18 16:06:46 +08002074static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2075 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002076{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002077 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002078 struct tipc_sock *tsk = tipc_sk(sk);
2079 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002080 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002081 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002082 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002083
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002084 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2085 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002086 if (lvl != SOL_TIPC)
2087 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002088 res = get_user(len, ol);
2089 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002090 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002091
Allan Stephens0c3141e2008-04-15 00:22:02 -07002092 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002093
2094 switch (opt) {
2095 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002096 value = tipc_port_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002097 break;
2098 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002099 value = tipc_port_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002100 break;
2101 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002102 value = tipc_port_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002103 break;
2104 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002105 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002106 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002107 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002108 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002109 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002110 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002111 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002112 value = skb_queue_len(&sk->sk_receive_queue);
2113 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002114 default:
2115 res = -EINVAL;
2116 }
2117
Allan Stephens0c3141e2008-04-15 00:22:02 -07002118 release_sock(sk);
2119
Paul Gortmaker25860c32010-12-31 18:59:31 +00002120 if (res)
2121 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002122
Paul Gortmaker25860c32010-12-31 18:59:31 +00002123 if (len < sizeof(value))
2124 return -EINVAL;
2125
2126 if (copy_to_user(ov, &value, sizeof(value)))
2127 return -EFAULT;
2128
2129 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002130}
2131
Wei Yongjun52f50ce2014-07-20 13:14:28 +08002132static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002133{
2134 struct tipc_sioc_ln_req lnr;
2135 void __user *argp = (void __user *)arg;
2136
2137 switch (cmd) {
2138 case SIOCGETLINKNAME:
2139 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2140 return -EFAULT;
2141 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
2142 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2143 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2144 return -EFAULT;
2145 return 0;
2146 }
2147 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002148 default:
2149 return -ENOIOCTLCMD;
2150 }
2151}
2152
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002153/* Protocol switches for the various types of TIPC sockets */
2154
Florian Westphalbca65ea2008-02-07 18:18:01 -08002155static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002156 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002157 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002158 .release = tipc_release,
2159 .bind = tipc_bind,
2160 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002161 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002162 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002163 .getname = tipc_getname,
2164 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002165 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002166 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002167 .shutdown = tipc_shutdown,
2168 .setsockopt = tipc_setsockopt,
2169 .getsockopt = tipc_getsockopt,
2170 .sendmsg = tipc_sendmsg,
2171 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002172 .mmap = sock_no_mmap,
2173 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002174};
2175
Florian Westphalbca65ea2008-02-07 18:18:01 -08002176static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002177 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002178 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002179 .release = tipc_release,
2180 .bind = tipc_bind,
2181 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002182 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002183 .accept = tipc_accept,
2184 .getname = tipc_getname,
2185 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002186 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002187 .listen = tipc_listen,
2188 .shutdown = tipc_shutdown,
2189 .setsockopt = tipc_setsockopt,
2190 .getsockopt = tipc_getsockopt,
2191 .sendmsg = tipc_send_packet,
2192 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002193 .mmap = sock_no_mmap,
2194 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002195};
2196
Florian Westphalbca65ea2008-02-07 18:18:01 -08002197static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002198 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002199 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002200 .release = tipc_release,
2201 .bind = tipc_bind,
2202 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002203 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002204 .accept = tipc_accept,
2205 .getname = tipc_getname,
2206 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002207 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002208 .listen = tipc_listen,
2209 .shutdown = tipc_shutdown,
2210 .setsockopt = tipc_setsockopt,
2211 .getsockopt = tipc_getsockopt,
2212 .sendmsg = tipc_send_stream,
2213 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002214 .mmap = sock_no_mmap,
2215 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002216};
2217
Florian Westphalbca65ea2008-02-07 18:18:01 -08002218static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002219 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002220 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002221 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002222};
2223
2224static struct proto tipc_proto = {
2225 .name = "TIPC",
2226 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002227 .obj_size = sizeof(struct tipc_sock),
2228 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002229};
2230
Ying Xuec5fa7b32013-06-17 10:54:39 -04002231static struct proto tipc_proto_kern = {
2232 .name = "TIPC",
2233 .obj_size = sizeof(struct tipc_sock),
2234 .sysctl_rmem = sysctl_tipc_rmem
2235};
2236
Per Lidenb97bf3f2006-01-02 19:04:38 +01002237/**
Per Liden4323add2006-01-18 00:38:21 +01002238 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002239 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002240 * Returns 0 on success, errno otherwise
2241 */
Per Liden4323add2006-01-18 00:38:21 +01002242int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002243{
2244 int res;
2245
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002246 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002247 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002248 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002249 goto out;
2250 }
2251
2252 res = sock_register(&tipc_family_ops);
2253 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002254 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002255 proto_unregister(&tipc_proto);
2256 goto out;
2257 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002258 out:
2259 return res;
2260}
2261
2262/**
Per Liden4323add2006-01-18 00:38:21 +01002263 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002264 */
Per Liden4323add2006-01-18 00:38:21 +01002265void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002266{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002267 sock_unregister(tipc_family_ops.family);
2268 proto_unregister(&tipc_proto);
2269}