blob: f202d4790ce3068dcc6ddcc42459d29ab8cd1c1e [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04004 * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000038#include "port.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050039#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020040#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050041#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040042#include <linux/export.h>
Erik Hugne2cf8aa12012-06-29 00:16:37 -040043
Per Lidenb97bf3f2006-01-02 19:04:38 +010044#define SS_LISTENING -1 /* socket is listening */
45#define SS_READY -2 /* socket is connectionless */
46
Allan Stephens3654ea02008-04-13 21:35:11 -070047#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Jon Paul Maloyac0074e2014-06-25 20:41:41 -050048#define TIPC_FWD_MSG 1
Per Lidenb97bf3f2006-01-02 19:04:38 +010049
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -040050static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -040051static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +080052static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080053static int tipc_release(struct socket *sock);
54static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -040055static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
Jon Paul Maloy57289012014-08-22 18:09:09 -040056static void tipc_sk_timeout(unsigned long ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +010057
Florian Westphalbca65ea2008-02-07 18:18:01 -080058static const struct proto_ops packet_ops;
59static const struct proto_ops stream_ops;
60static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010061
62static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040063static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010064
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090065/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070066 * Revised TIPC socket locking policy:
67 *
68 * Most socket operations take the standard socket lock when they start
69 * and hold it until they finish (or until they need to sleep). Acquiring
70 * this lock grants the owner exclusive access to the fields of the socket
71 * data structures, with the exception of the backlog queue. A few socket
72 * operations can be done without taking the socket lock because they only
73 * read socket information that never changes during the life of the socket.
74 *
75 * Socket operations may acquire the lock for the associated TIPC port if they
76 * need to perform an operation on the port. If any routine needs to acquire
77 * both the socket lock and the port lock it must take the socket lock first
78 * to avoid the risk of deadlock.
79 *
80 * The dispatcher handling incoming messages cannot grab the socket lock in
81 * the standard fashion, since invoked it runs at the BH level and cannot block.
82 * Instead, it checks to see if the socket lock is currently owned by someone,
83 * and either handles the message itself or adds it to the socket's backlog
84 * queue; in the latter case the queued message is processed once the process
85 * owning the socket lock releases it.
86 *
87 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
88 * the problem of a blocked socket operation preventing any other operations
89 * from occurring. However, applications must be careful if they have
90 * multiple threads trying to send (or receive) on the same socket, as these
91 * operations might interfere with each other. For example, doing a connect
92 * and a receive at the same time might allow the receive to consume the
93 * ACK message meant for the connect. While additional work could be done
94 * to try and overcome this, it doesn't seem to be worthwhile at the present.
95 *
96 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
97 * that another operation that must be performed in a non-blocking manner is
98 * not delayed for very long because the lock has already been taken.
99 *
100 * NOTE: This code assumes that certain fields of a port/socket pair are
101 * constant over its lifetime; such fields can be examined without taking
102 * the socket lock and/or port lock, and do not need to be re-read even
103 * after resuming processing after waiting. These fields include:
104 * - socket type
105 * - pointer to socket sk structure (aka tipc_sock structure)
106 * - pointer to port structure
107 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100109
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400110#include "socket.h"
111
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700113 * advance_rx_queue - discard first buffer in socket receive queue
114 *
115 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700117static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400119 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120}
121
122/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700123 * reject_rx_queue - reject all buffers in socket receive queue
124 *
125 * Caller must hold socket lock
126 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700127static void reject_rx_queue(struct sock *sk)
128{
129 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500130 u32 dnode;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700131
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500132 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
133 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400134 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500135 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700136}
137
138/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400139 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700140 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100141 * @sock: pre-allocated socket structure
142 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800143 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900144 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700145 * This routine creates additional data structures used by the TIPC socket,
146 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100147 *
148 * Returns 0 on success, errno otherwise
149 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400150static int tipc_sk_create(struct net *net, struct socket *sock,
151 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700153 const struct proto_ops *ops;
154 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400156 struct tipc_sock *tsk;
157 struct tipc_port *port;
158 u32 ref;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700159
160 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161 if (unlikely(protocol != 0))
162 return -EPROTONOSUPPORT;
163
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164 switch (sock->type) {
165 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700166 ops = &stream_ops;
167 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168 break;
169 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700170 ops = &packet_ops;
171 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172 break;
173 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700175 ops = &msg_ops;
176 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 break;
Allan Stephens49978652006-06-25 23:47:18 -0700178 default:
Allan Stephens49978652006-06-25 23:47:18 -0700179 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 }
181
Allan Stephens0c3141e2008-04-15 00:22:02 -0700182 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400183 if (!kern)
184 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
185 else
186 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
187
Allan Stephens0c3141e2008-04-15 00:22:02 -0700188 if (sk == NULL)
189 return -ENOMEM;
190
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400191 tsk = tipc_sk(sk);
192 port = &tsk->port;
193
194 ref = tipc_port_init(port, TIPC_LOW_IMPORTANCE);
195 if (!ref) {
196 pr_warn("Socket registration failed, ref. table exhausted\n");
Allan Stephens0c3141e2008-04-15 00:22:02 -0700197 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198 return -ENOMEM;
199 }
200
Allan Stephens0c3141e2008-04-15 00:22:02 -0700201 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700202 sock->ops = ops;
203 sock->state = state;
204
Per Lidenb97bf3f2006-01-02 19:04:38 +0100205 sock_init_data(sock, sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -0400206 k_init_timer(&port->timer, (Handler)tipc_sk_timeout, ref);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400207 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400208 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800209 sk->sk_data_ready = tipc_data_ready;
210 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400211 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500212 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400213 atomic_set(&tsk->dupl_rcvcnt, 0);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400214 tipc_port_unlock(port);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700215
Allan Stephens0c3141e2008-04-15 00:22:02 -0700216 if (sock->state == SS_READY) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400217 tipc_port_set_unreturnable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700218 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400219 tipc_port_set_unreliable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700220 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100221 return 0;
222}
223
224/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400225 * tipc_sock_create_local - create TIPC socket from inside TIPC module
226 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
227 *
228 * We cannot use sock_creat_kern here because it bumps module user count.
229 * Since socket owner and creator is the same module we must make sure
230 * that module count remains zero for module local sockets, otherwise
231 * we cannot do rmmod.
232 *
233 * Returns 0 on success, errno otherwise
234 */
235int tipc_sock_create_local(int type, struct socket **res)
236{
237 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400238
239 rc = sock_create_lite(AF_TIPC, type, 0, res);
240 if (rc < 0) {
241 pr_err("Failed to create kernel socket\n");
242 return rc;
243 }
244 tipc_sk_create(&init_net, *res, 0, 1);
245
Ying Xuec5fa7b32013-06-17 10:54:39 -0400246 return 0;
247}
248
249/**
250 * tipc_sock_release_local - release socket created by tipc_sock_create_local
251 * @sock: the socket to be released.
252 *
253 * Module reference count is not incremented when such sockets are created,
254 * so we must keep it from being decremented when they are released.
255 */
256void tipc_sock_release_local(struct socket *sock)
257{
Ying Xue247f0f32014-02-18 16:06:46 +0800258 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400259 sock->ops = NULL;
260 sock_release(sock);
261}
262
263/**
264 * tipc_sock_accept_local - accept a connection on a socket created
265 * with tipc_sock_create_local. Use this function to avoid that
266 * module reference count is inadvertently incremented.
267 *
268 * @sock: the accepting socket
269 * @newsock: reference to the new socket to be created
270 * @flags: socket flags
271 */
272
273int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400274 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400275{
276 struct sock *sk = sock->sk;
277 int ret;
278
279 ret = sock_create_lite(sk->sk_family, sk->sk_type,
280 sk->sk_protocol, newsock);
281 if (ret < 0)
282 return ret;
283
Ying Xue247f0f32014-02-18 16:06:46 +0800284 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400285 if (ret < 0) {
286 sock_release(*newsock);
287 return ret;
288 }
289 (*newsock)->ops = sock->ops;
290 return ret;
291}
292
293/**
Ying Xue247f0f32014-02-18 16:06:46 +0800294 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100295 * @sock: socket to destroy
296 *
297 * This routine cleans up any messages that are still queued on the socket.
298 * For DGRAM and RDM socket types, all queued messages are rejected.
299 * For SEQPACKET and STREAM socket types, the first message is rejected
300 * and any others are discarded. (If the first message on a STREAM socket
301 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900302 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303 * NOTE: Rejected messages are not necessarily returned to the sender! They
304 * are returned or discarded according to the "destination droppable" setting
305 * specified for the message by the sender.
306 *
307 * Returns 0 on success, errno otherwise
308 */
Ying Xue247f0f32014-02-18 16:06:46 +0800309static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400312 struct tipc_sock *tsk;
313 struct tipc_port *port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500315 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316
Allan Stephens0c3141e2008-04-15 00:22:02 -0700317 /*
318 * Exit if socket isn't fully initialized (occurs when a failed accept()
319 * releases a pre-allocated child socket that was never used)
320 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700321 if (sk == NULL)
322 return 0;
323
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400324 tsk = tipc_sk(sk);
325 port = &tsk->port;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700326 lock_sock(sk);
327
328 /*
329 * Reject all unreceived messages, except on an active connection
330 * (which disconnects locally & sends a 'FIN+' to peer)
331 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700333 buf = __skb_dequeue(&sk->sk_receive_queue);
334 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335 break;
Ying Xue40682432013-10-18 07:23:16 +0200336 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400337 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700338 else {
339 if ((sock->state == SS_CONNECTING) ||
340 (sock->state == SS_CONNECTED)) {
341 sock->state = SS_DISCONNECTING;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400342 tipc_port_disconnect(port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700343 }
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500344 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400345 tipc_link_xmit(buf, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700346 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 }
348
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400349 /* Destroy TIPC port; also disconnects an active connection and
350 * sends a 'FIN-' to peer.
Allan Stephens0c3141e2008-04-15 00:22:02 -0700351 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400352 tipc_port_destroy(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353
Allan Stephens0c3141e2008-04-15 00:22:02 -0700354 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100355 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356
Allan Stephens0c3141e2008-04-15 00:22:02 -0700357 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700358 sock->state = SS_DISCONNECTING;
359 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360
361 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700362 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200364 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365}
366
367/**
Ying Xue247f0f32014-02-18 16:06:46 +0800368 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369 * @sock: socket structure
370 * @uaddr: socket address describing name(s) and desired operation
371 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900372 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373 * Name and name sequence binding is indicated using a positive scope value;
374 * a negative scope value unbinds the specified name. Specifying no name
375 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900376 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700378 *
379 * NOTE: This routine doesn't need to take the socket lock since it doesn't
380 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381 */
Ying Xue247f0f32014-02-18 16:06:46 +0800382static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
383 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384{
Ying Xue84602762013-12-27 10:18:28 +0800385 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400387 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800388 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389
Ying Xue84602762013-12-27 10:18:28 +0800390 lock_sock(sk);
391 if (unlikely(!uaddr_len)) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400392 res = tipc_withdraw(&tsk->port, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800393 goto exit;
394 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900395
Ying Xue84602762013-12-27 10:18:28 +0800396 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
397 res = -EINVAL;
398 goto exit;
399 }
400 if (addr->family != AF_TIPC) {
401 res = -EAFNOSUPPORT;
402 goto exit;
403 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405 if (addr->addrtype == TIPC_ADDR_NAME)
406 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800407 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
408 res = -EAFNOSUPPORT;
409 goto exit;
410 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900411
Ying Xue13a2e892013-06-17 10:54:40 -0400412 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400413 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800414 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
415 res = -EACCES;
416 goto exit;
417 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400418
Ying Xue84602762013-12-27 10:18:28 +0800419 res = (addr->scope > 0) ?
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400420 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
421 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800422exit:
423 release_sock(sk);
424 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425}
426
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900427/**
Ying Xue247f0f32014-02-18 16:06:46 +0800428 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429 * @sock: socket structure
430 * @uaddr: area for returned socket address
431 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700432 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900433 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700435 *
Allan Stephens2da59912008-07-14 22:43:32 -0700436 * NOTE: This routine doesn't need to take the socket lock since it only
437 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000438 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439 */
Ying Xue247f0f32014-02-18 16:06:46 +0800440static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
441 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400444 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000446 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700447 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700448 if ((sock->state != SS_CONNECTED) &&
449 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
450 return -ENOTCONN;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400451 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
452 addr->addr.id.node = tipc_port_peernode(&tsk->port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700453 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400454 addr->addr.id.ref = tsk->port.ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000455 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700456 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457
458 *uaddr_len = sizeof(*addr);
459 addr->addrtype = TIPC_ADDR_ID;
460 addr->family = AF_TIPC;
461 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462 addr->addr.name.domain = 0;
463
Allan Stephens0c3141e2008-04-15 00:22:02 -0700464 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465}
466
467/**
Ying Xue247f0f32014-02-18 16:06:46 +0800468 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469 * @file: file structure associated with the socket
470 * @sock: socket for which to calculate the poll bits
471 * @wait: ???
472 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700473 * Returns pollmask value
474 *
475 * COMMENTARY:
476 * It appears that the usual socket locking mechanisms are not useful here
477 * since the pollmask info is potentially out-of-date the moment this routine
478 * exits. TCP and other protocols seem to rely on higher level poll routines
479 * to handle any preventable race conditions, so TIPC will do the same ...
480 *
481 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700482 *
Allan Stephensf662c072010-08-17 11:00:06 +0000483 * socket state flags set
484 * ------------ ---------
485 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200486 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000487 *
488 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
489 * no write flags
490 *
491 * connected POLLIN/POLLRDNORM if data in rx queue
492 * POLLOUT if port is not congested
493 *
494 * disconnecting POLLIN/POLLRDNORM/POLLHUP
495 * no write flags
496 *
497 * listening POLLIN if SYN in rx queue
498 * no write flags
499 *
500 * ready POLLIN/POLLRDNORM if data in rx queue
501 * [connectionless] POLLOUT (since port cannot be congested)
502 *
503 * IMPORTANT: The fact that a read or write operation is indicated does NOT
504 * imply that the operation will succeed, merely that it should be performed
505 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 */
Ying Xue247f0f32014-02-18 16:06:46 +0800507static unsigned int tipc_poll(struct file *file, struct socket *sock,
508 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509{
Allan Stephens9b674e82008-03-26 16:48:21 -0700510 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400511 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000512 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700513
Ying Xuef288bef2012-08-21 11:16:57 +0800514 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700515
Allan Stephensf662c072010-08-17 11:00:06 +0000516 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200517 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500518 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200519 mask |= POLLOUT;
520 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000521 case SS_READY:
522 case SS_CONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500523 if (!tsk->link_cong && !tipc_sk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000524 mask |= POLLOUT;
525 /* fall thru' */
526 case SS_CONNECTING:
527 case SS_LISTENING:
528 if (!skb_queue_empty(&sk->sk_receive_queue))
529 mask |= (POLLIN | POLLRDNORM);
530 break;
531 case SS_DISCONNECTING:
532 mask = (POLLIN | POLLRDNORM | POLLHUP);
533 break;
534 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700535
536 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537}
538
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400539/**
540 * tipc_sendmcast - send multicast message
541 * @sock: socket structure
542 * @seq: destination address
543 * @iov: message data to send
544 * @dsz: total length of message data
545 * @timeo: timeout to wait for wakeup
546 *
547 * Called from function tipc_sendmsg(), which has done all sanity checks
548 * Returns the number of bytes sent on success, or errno
549 */
550static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
551 struct iovec *iov, size_t dsz, long timeo)
552{
553 struct sock *sk = sock->sk;
554 struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr;
555 struct sk_buff *buf;
556 uint mtu;
557 int rc;
558
559 msg_set_type(mhdr, TIPC_MCAST_MSG);
560 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
561 msg_set_destport(mhdr, 0);
562 msg_set_destnode(mhdr, 0);
563 msg_set_nametype(mhdr, seq->type);
564 msg_set_namelower(mhdr, seq->lower);
565 msg_set_nameupper(mhdr, seq->upper);
566 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
567
568new_mtu:
569 mtu = tipc_bclink_get_mtu();
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400570 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400571 if (unlikely(rc < 0))
572 return rc;
573
574 do {
575 rc = tipc_bclink_xmit(buf);
576 if (likely(rc >= 0)) {
577 rc = dsz;
578 break;
579 }
580 if (rc == -EMSGSIZE)
581 goto new_mtu;
582 if (rc != -ELINKCONG)
583 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400584 tipc_sk(sk)->link_cong = 1;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400585 rc = tipc_wait_for_sndmsg(sock, &timeo);
586 if (rc)
587 kfree_skb_list(buf);
588 } while (!rc);
589 return rc;
590}
591
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400592/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
593 */
594void tipc_sk_mcast_rcv(struct sk_buff *buf)
595{
596 struct tipc_msg *msg = buf_msg(buf);
597 struct tipc_port_list dports = {0, NULL, };
598 struct tipc_port_list *item;
599 struct sk_buff *b;
600 uint i, last, dst = 0;
601 u32 scope = TIPC_CLUSTER_SCOPE;
602
603 if (in_own_node(msg_orignode(msg)))
604 scope = TIPC_NODE_SCOPE;
605
606 /* Create destination port list: */
607 tipc_nametbl_mc_translate(msg_nametype(msg),
608 msg_namelower(msg),
609 msg_nameupper(msg),
610 scope,
611 &dports);
612 last = dports.count;
613 if (!last) {
614 kfree_skb(buf);
615 return;
616 }
617
618 for (item = &dports; item; item = item->next) {
619 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
620 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
621 if (!b) {
622 pr_warn("Failed do clone mcast rcv buffer\n");
623 continue;
624 }
625 msg_set_destport(msg, item->ports[i]);
626 tipc_sk_rcv(b);
627 }
628 }
629 tipc_port_list_free(&dports);
630}
631
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900632/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500633 * tipc_sk_proto_rcv - receive a connection mng protocol message
634 * @tsk: receiving socket
635 * @dnode: node to send response message to, if any
636 * @buf: buffer containing protocol message
637 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
638 * (CONN_PROBE_REPLY) message should be forwarded.
639 */
Wei Yongjun52f50ce2014-07-20 13:14:28 +0800640static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
641 struct sk_buff *buf)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500642{
643 struct tipc_msg *msg = buf_msg(buf);
644 struct tipc_port *port = &tsk->port;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500645 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500646
647 /* Ignore if connection cannot be validated: */
648 if (!port->connected || !tipc_port_peer_msg(port, msg))
649 goto exit;
650
651 port->probing_state = TIPC_CONN_OK;
652
653 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500654 conn_cong = tipc_sk_conn_cong(tsk);
655 tsk->sent_unacked -= msg_msgcnt(msg);
656 if (conn_cong)
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400657 tsk->sk.sk_write_space(&tsk->sk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500658 } else if (msg_type(msg) == CONN_PROBE) {
659 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
660 return TIPC_OK;
661 msg_set_type(msg, CONN_PROBE_REPLY);
662 return TIPC_FWD_MSG;
663 }
664 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
665exit:
666 kfree_skb(buf);
667 return TIPC_OK;
668}
669
670/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671 * dest_name_check - verify user is permitted to send to specified port name
672 * @dest: destination address
673 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900674 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675 * Prevents restricted configuration commands from being issued by
676 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900677 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678 * Returns 0 if permission is granted, otherwise errno
679 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800680static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681{
682 struct tipc_cfg_msg_hdr hdr;
683
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500684 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
685 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900686 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
687 return 0;
688 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
689 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900690 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
691 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100692
Allan Stephens3f8dd942011-01-18 13:09:29 -0500693 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
694 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900695 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100696 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700697 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900699
Per Lidenb97bf3f2006-01-02 19:04:38 +0100700 return 0;
701}
702
Ying Xue3f405042014-01-17 09:50:05 +0800703static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
704{
705 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400706 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800707 DEFINE_WAIT(wait);
708 int done;
709
710 do {
711 int err = sock_error(sk);
712 if (err)
713 return err;
714 if (sock->state == SS_DISCONNECTING)
715 return -EPIPE;
716 if (!*timeo_p)
717 return -EAGAIN;
718 if (signal_pending(current))
719 return sock_intr_errno(*timeo_p);
720
721 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500722 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800723 finish_wait(sk_sleep(sk), &wait);
724 } while (!done);
725 return 0;
726}
727
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500728/**
Ying Xue247f0f32014-02-18 16:06:46 +0800729 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700730 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731 * @sock: socket structure
732 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500733 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900734 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900736 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
738 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900739 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100740 * Returns the number of bytes sent on success, or errno otherwise
741 */
Ying Xue247f0f32014-02-18 16:06:46 +0800742static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500743 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100744{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500745 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700746 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400747 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400748 struct tipc_port *port = &tsk->port;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500749 struct tipc_msg *mhdr = &port->phdr;
750 struct iovec *iov = m->msg_iov;
751 u32 dnode, dport;
752 struct sk_buff *buf;
753 struct tipc_name_seq *seq = &dest->addr.nameseq;
754 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800755 long timeo;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500756 int rc = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100757
758 if (unlikely(!dest))
759 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500760
Allan Stephens51f9cc12006-06-25 23:49:06 -0700761 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
762 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500764
765 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400766 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100767
Allan Stephens0c3141e2008-04-15 00:22:02 -0700768 if (iocb)
769 lock_sock(sk);
770
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500771 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700772 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500773 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700774 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700775 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700776 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500777 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700778 goto exit;
779 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400780 if (tsk->port.published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500781 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700782 goto exit;
783 }
784 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400785 tsk->port.conn_type = dest->addr.name.name.type;
786 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700787 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500789 rc = dest_name_check(dest, m);
790 if (rc)
791 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792
Ying Xue3f405042014-01-17 09:50:05 +0800793 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500794
795 if (dest->addrtype == TIPC_ADDR_MCAST) {
796 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
797 goto exit;
798 } else if (dest->addrtype == TIPC_ADDR_NAME) {
799 u32 type = dest->addr.name.name.type;
800 u32 inst = dest->addr.name.name.instance;
801 u32 domain = dest->addr.name.domain;
802
803 dnode = domain;
804 msg_set_type(mhdr, TIPC_NAMED_MSG);
805 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
806 msg_set_nametype(mhdr, type);
807 msg_set_nameinst(mhdr, inst);
808 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
809 dport = tipc_nametbl_translate(type, inst, &dnode);
810 msg_set_destnode(mhdr, dnode);
811 msg_set_destport(mhdr, dport);
812 if (unlikely(!dport && !dnode)) {
813 rc = -EHOSTUNREACH;
814 goto exit;
815 }
816 } else if (dest->addrtype == TIPC_ADDR_ID) {
817 dnode = dest->addr.id.node;
818 msg_set_type(mhdr, TIPC_DIRECT_MSG);
819 msg_set_lookup_scope(mhdr, 0);
820 msg_set_destnode(mhdr, dnode);
821 msg_set_destport(mhdr, dest->addr.id.ref);
822 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
823 }
824
825new_mtu:
826 mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400827 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500828 if (rc < 0)
829 goto exit;
830
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900831 do {
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400832 TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong;
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400833 rc = tipc_link_xmit(buf, dnode, tsk->port.ref);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500834 if (likely(rc >= 0)) {
835 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700836 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500837 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700838 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900839 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500840 if (rc == -EMSGSIZE)
841 goto new_mtu;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500842 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700843 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400844 tsk->link_cong = 1;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500845 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400846 if (rc)
847 kfree_skb_list(buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500848 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700849exit:
850 if (iocb)
851 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500852
853 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854}
855
Ying Xue391a6dd2014-01-17 09:50:06 +0800856static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
857{
858 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400859 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800860 DEFINE_WAIT(wait);
861 int done;
862
863 do {
864 int err = sock_error(sk);
865 if (err)
866 return err;
867 if (sock->state == SS_DISCONNECTING)
868 return -EPIPE;
869 else if (sock->state != SS_CONNECTED)
870 return -ENOTCONN;
871 if (!*timeo_p)
872 return -EAGAIN;
873 if (signal_pending(current))
874 return sock_intr_errno(*timeo_p);
875
876 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
877 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -0500878 (!tsk->link_cong &&
879 !tipc_sk_conn_cong(tsk)) ||
880 !tsk->port.connected);
Ying Xue391a6dd2014-01-17 09:50:06 +0800881 finish_wait(sk_sleep(sk), &wait);
882 } while (!done);
883 return 0;
884}
885
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900886/**
Ying Xue247f0f32014-02-18 16:06:46 +0800887 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100888 * @iocb: (unused)
889 * @sock: socket structure
890 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500891 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900892 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900894 *
895 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700896 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897 */
Ying Xue247f0f32014-02-18 16:06:46 +0800898static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500899 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700901 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400902 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500903 struct tipc_port *port = &tsk->port;
904 struct tipc_msg *mhdr = &port->phdr;
905 struct sk_buff *buf;
906 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
907 u32 ref = port->ref;
908 int rc = -EINVAL;
909 long timeo;
910 u32 dnode;
911 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900912
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500913 /* Handle implied connection establishment */
914 if (unlikely(dest)) {
915 rc = tipc_sendmsg(iocb, sock, m, dsz);
916 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -0500917 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500918 return rc;
919 }
920 if (dsz > (uint)INT_MAX)
921 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700922
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500923 if (iocb)
924 lock_sock(sk);
925
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900926 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500927 if (sock->state == SS_DISCONNECTING)
928 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +0800929 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500930 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800931 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900932 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100933
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500934 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
935 dnode = tipc_port_peernode(port);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500936
937next:
938 mtu = port->max_pkt;
939 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400940 rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500941 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700942 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500943 do {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500944 if (likely(!tipc_sk_conn_cong(tsk))) {
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -0400945 rc = tipc_link_xmit(buf, dnode, ref);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500946 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500947 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500948 sent += send;
949 if (sent == dsz)
950 break;
951 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700952 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500953 if (rc == -EMSGSIZE) {
954 port->max_pkt = tipc_node_get_mtu(dnode, ref);
955 goto next;
956 }
957 if (rc != -ELINKCONG)
958 break;
Jon Paul Maloy50100a52014-08-22 18:09:07 -0400959 tsk->link_cong = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100960 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500961 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400962 if (rc)
963 kfree_skb_list(buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500964 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700965exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500966 if (iocb)
967 release_sock(sk);
968 return sent ? sent : rc;
969}
970
971/**
972 * tipc_send_packet - send a connection-oriented message
973 * @iocb: if NULL, indicates that socket lock is already held
974 * @sock: socket structure
975 * @m: message to send
976 * @dsz: length of data to be transmitted
977 *
978 * Used for SOCK_SEQPACKET messages.
979 *
980 * Returns the number of bytes sent on success, or errno otherwise
981 */
982static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
983 struct msghdr *m, size_t dsz)
984{
985 if (dsz > TIPC_MAX_USER_MSG_SIZE)
986 return -EMSGSIZE;
987
988 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989}
990
991/**
992 * auto_connect - complete connection setup to a remote port
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400993 * @tsk: tipc socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100994 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900995 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996 * Returns 0 on success, errno otherwise
997 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400998static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100999{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001000 struct tipc_port *port = &tsk->port;
1001 struct socket *sock = tsk->sk.sk_socket;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001002 struct tipc_portid peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001003
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001004 peer.ref = msg_origport(msg);
1005 peer.node = msg_orignode(msg);
1006
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001007 __tipc_port_connect(port->ref, port, &peer);
Ying Xue584d24b2012-11-29 18:51:19 -05001008
1009 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
1010 return -EINVAL;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001011 msg_set_importance(&port->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001012 sock->state = SS_CONNECTED;
1013 return 0;
1014}
1015
1016/**
1017 * set_orig_addr - capture sender's address for received message
1018 * @m: descriptor for message info
1019 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001020 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001021 * Note: Address is not captured if not requested by receiver.
1022 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001023static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001025 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001027 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 addr->family = AF_TIPC;
1029 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001030 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031 addr->addr.id.ref = msg_origport(msg);
1032 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001033 addr->addr.name.domain = 0; /* could leave uninitialized */
1034 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035 m->msg_namelen = sizeof(struct sockaddr_tipc);
1036 }
1037}
1038
1039/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001040 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041 * @m: descriptor for message info
1042 * @msg: received message header
1043 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001044 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001046 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001047 * Returns 0 if successful, otherwise errno
1048 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001049static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001050 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001051{
1052 u32 anc_data[3];
1053 u32 err;
1054 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001055 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001056 int res;
1057
1058 if (likely(m->msg_controllen == 0))
1059 return 0;
1060
1061 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 err = msg ? msg_errcode(msg) : 0;
1063 if (unlikely(err)) {
1064 anc_data[0] = err;
1065 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001066 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1067 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001069 if (anc_data[1]) {
1070 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1071 msg_data(msg));
1072 if (res)
1073 return res;
1074 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001075 }
1076
1077 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1079 switch (dest_type) {
1080 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001081 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 anc_data[0] = msg_nametype(msg);
1083 anc_data[1] = msg_namelower(msg);
1084 anc_data[2] = msg_namelower(msg);
1085 break;
1086 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001087 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001088 anc_data[0] = msg_nametype(msg);
1089 anc_data[1] = msg_namelower(msg);
1090 anc_data[2] = msg_nameupper(msg);
1091 break;
1092 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001093 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001094 anc_data[0] = tport->conn_type;
1095 anc_data[1] = tport->conn_instance;
1096 anc_data[2] = tport->conn_instance;
1097 break;
1098 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001099 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001100 }
Allan Stephens2db99832010-12-31 18:59:33 +00001101 if (has_name) {
1102 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1103 if (res)
1104 return res;
1105 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001106
1107 return 0;
1108}
1109
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001110static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001111{
1112 struct sock *sk = sock->sk;
1113 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001114 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001115 int err;
1116
1117 for (;;) {
1118 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001119 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001120 if (sock->state == SS_DISCONNECTING) {
1121 err = -ENOTCONN;
1122 break;
1123 }
1124 release_sock(sk);
1125 timeo = schedule_timeout(timeo);
1126 lock_sock(sk);
1127 }
1128 err = 0;
1129 if (!skb_queue_empty(&sk->sk_receive_queue))
1130 break;
1131 err = sock_intr_errno(timeo);
1132 if (signal_pending(current))
1133 break;
1134 err = -EAGAIN;
1135 if (!timeo)
1136 break;
1137 }
1138 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001139 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001140 return err;
1141}
1142
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001143/**
Ying Xue247f0f32014-02-18 16:06:46 +08001144 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 * @iocb: (unused)
1146 * @m: descriptor for message info
1147 * @buf_len: total size of user buffer area
1148 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001149 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1151 * If the complete message doesn't fit in user area, truncate it.
1152 *
1153 * Returns size of returned message data, errno otherwise
1154 */
Ying Xue247f0f32014-02-18 16:06:46 +08001155static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1156 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001158 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001159 struct tipc_sock *tsk = tipc_sk(sk);
1160 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 struct sk_buff *buf;
1162 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001163 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164 unsigned int sz;
1165 u32 err;
1166 int res;
1167
Allan Stephens0c3141e2008-04-15 00:22:02 -07001168 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 if (unlikely(!buf_len))
1170 return -EINVAL;
1171
Allan Stephens0c3141e2008-04-15 00:22:02 -07001172 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173
Allan Stephens0c3141e2008-04-15 00:22:02 -07001174 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175 res = -ENOTCONN;
1176 goto exit;
1177 }
1178
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001179 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001180restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001181
Allan Stephens0c3141e2008-04-15 00:22:02 -07001182 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001183 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001184 if (res)
1185 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001186
1187 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001188 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 msg = buf_msg(buf);
1190 sz = msg_data_sz(msg);
1191 err = msg_errcode(msg);
1192
Per Lidenb97bf3f2006-01-02 19:04:38 +01001193 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001195 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 goto restart;
1197 }
1198
1199 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200 set_orig_addr(m, msg);
1201
1202 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001203 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001204 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205 goto exit;
1206
1207 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 if (!err) {
1209 if (unlikely(buf_len < sz)) {
1210 sz = buf_len;
1211 m->msg_flags |= MSG_TRUNC;
1212 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001213 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1214 m->msg_iov, sz);
1215 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001217 res = sz;
1218 } else {
1219 if ((sock->state == SS_READY) ||
1220 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1221 res = 0;
1222 else
1223 res = -ECONNRESET;
1224 }
1225
1226 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001227 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001228 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001229 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1230 tipc_acknowledge(port->ref, tsk->rcv_unacked);
1231 tsk->rcv_unacked = 0;
1232 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001233 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001234 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001236 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 return res;
1238}
1239
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001240/**
Ying Xue247f0f32014-02-18 16:06:46 +08001241 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001242 * @iocb: (unused)
1243 * @m: descriptor for message info
1244 * @buf_len: total size of user buffer area
1245 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001246 *
1247 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001248 * will optionally wait for more; never truncates data.
1249 *
1250 * Returns size of returned message data, errno otherwise
1251 */
Ying Xue247f0f32014-02-18 16:06:46 +08001252static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1253 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001254{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001255 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001256 struct tipc_sock *tsk = tipc_sk(sk);
1257 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001258 struct sk_buff *buf;
1259 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001260 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001262 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001263 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001265 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001266
1267 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268 if (unlikely(!buf_len))
1269 return -EINVAL;
1270
Allan Stephens0c3141e2008-04-15 00:22:02 -07001271 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001273 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001274 res = -ENOTCONN;
1275 goto exit;
1276 }
1277
Florian Westphal3720d402010-08-17 11:00:04 +00001278 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001279 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001280
Allan Stephens0c3141e2008-04-15 00:22:02 -07001281restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001282 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001283 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001284 if (res)
1285 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001286
1287 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001288 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001289 msg = buf_msg(buf);
1290 sz = msg_data_sz(msg);
1291 err = msg_errcode(msg);
1292
1293 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001294 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001295 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001296 goto restart;
1297 }
1298
1299 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001300 if (sz_copied == 0) {
1301 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001302 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001303 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001304 goto exit;
1305 }
1306
1307 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001308 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001309 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310
Allan Stephens0232fd02011-02-21 09:45:40 -05001311 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001312 needed = (buf_len - sz_copied);
1313 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001314
1315 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1316 m->msg_iov, sz_to_copy);
1317 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001318 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001319
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 sz_copied += sz_to_copy;
1321
1322 if (sz_to_copy < sz) {
1323 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001324 TIPC_SKB_CB(buf)->handle =
1325 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 goto exit;
1327 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328 } else {
1329 if (sz_copied != 0)
1330 goto exit; /* can't add error msg to valid data */
1331
1332 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1333 res = 0;
1334 else
1335 res = -ECONNRESET;
1336 }
1337
1338 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001340 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1341 tipc_acknowledge(port->ref, tsk->rcv_unacked);
1342 tsk->rcv_unacked = 0;
1343 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001344 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001345 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346
1347 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001348 if ((sz_copied < buf_len) && /* didn't get all requested data */
1349 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001350 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001351 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1352 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353 goto restart;
1354
1355exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001356 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001357 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358}
1359
1360/**
Ying Xuef288bef2012-08-21 11:16:57 +08001361 * tipc_write_space - wake up thread if port congestion is released
1362 * @sk: socket
1363 */
1364static void tipc_write_space(struct sock *sk)
1365{
1366 struct socket_wq *wq;
1367
1368 rcu_read_lock();
1369 wq = rcu_dereference(sk->sk_wq);
1370 if (wq_has_sleeper(wq))
1371 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1372 POLLWRNORM | POLLWRBAND);
1373 rcu_read_unlock();
1374}
1375
1376/**
1377 * tipc_data_ready - wake up threads to indicate messages have been received
1378 * @sk: socket
1379 * @len: the length of messages
1380 */
David S. Miller676d2362014-04-11 16:15:36 -04001381static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001382{
1383 struct socket_wq *wq;
1384
1385 rcu_read_lock();
1386 wq = rcu_dereference(sk->sk_wq);
1387 if (wq_has_sleeper(wq))
1388 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1389 POLLRDNORM | POLLRDBAND);
1390 rcu_read_unlock();
1391}
1392
1393/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001394 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001395 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001396 * @msg: message
1397 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001398 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001399 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001400static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001401{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001402 struct sock *sk = &tsk->sk;
1403 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001404 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001405 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001406
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001407 int retval = -TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001408 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001409
1410 if (msg_mcast(msg))
1411 return retval;
1412
1413 switch ((int)sock->state) {
1414 case SS_CONNECTED:
1415 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001416 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001417 if (unlikely(msg_errcode(msg))) {
1418 sock->state = SS_DISCONNECTING;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001419 __tipc_port_disconnect(port);
Ying Xue7e6c1312012-11-29 18:39:14 -05001420 }
1421 retval = TIPC_OK;
1422 }
1423 break;
1424 case SS_CONNECTING:
1425 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001426 if (unlikely(msg_errcode(msg))) {
1427 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001428 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001429 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001430 break;
1431 }
1432
1433 if (unlikely(!msg_connected(msg)))
1434 break;
1435
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001436 res = auto_connect(tsk, msg);
Ying Xue584d24b2012-11-29 18:51:19 -05001437 if (res) {
1438 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001439 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001440 retval = TIPC_OK;
1441 break;
1442 }
1443
1444 /* If an incoming message is an 'ACK-', it should be
1445 * discarded here because it doesn't contain useful
1446 * data. In addition, we should try to wake up
1447 * connect() routine if sleeping.
1448 */
1449 if (msg_data_sz(msg) == 0) {
1450 kfree_skb(*buf);
1451 *buf = NULL;
1452 if (waitqueue_active(sk_sleep(sk)))
1453 wake_up_interruptible(sk_sleep(sk));
1454 }
1455 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001456 break;
1457 case SS_LISTENING:
1458 case SS_UNCONNECTED:
1459 /* Accept only SYN message */
1460 if (!msg_connected(msg) && !(msg_errcode(msg)))
1461 retval = TIPC_OK;
1462 break;
1463 case SS_DISCONNECTING:
1464 break;
1465 default:
1466 pr_err("Unknown socket state %u\n", sock->state);
1467 }
1468 return retval;
1469}
1470
1471/**
Ying Xueaba79f32013-01-20 23:30:09 +01001472 * rcvbuf_limit - get proper overload limit of socket receive queue
1473 * @sk: socket
1474 * @buf: message
1475 *
1476 * For all connection oriented messages, irrespective of importance,
1477 * the default overload value (i.e. 67MB) is set as limit.
1478 *
1479 * For all connectionless messages, by default new queue limits are
1480 * as belows:
1481 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001482 * TIPC_LOW_IMPORTANCE (4 MB)
1483 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1484 * TIPC_HIGH_IMPORTANCE (16 MB)
1485 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001486 *
1487 * Returns overload limit according to corresponding message importance
1488 */
1489static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1490{
1491 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001492
1493 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001494 return sysctl_tipc_rmem[2];
1495
1496 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1497 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001498}
1499
1500/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001501 * filter_rcv - validate incoming message
1502 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001503 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001504 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001505 * Enqueues message on receive queue if acceptable; optionally handles
1506 * disconnect indication for a connected socket.
1507 *
1508 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001509 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001510 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001511 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001512 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001513static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001514{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001515 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001516 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001517 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001518 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001519 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001520 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001521
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001522 if (unlikely(msg_user(msg) == CONN_MANAGER))
1523 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001524
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001525 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1526 kfree_skb(buf);
1527 tsk->link_cong = 0;
1528 sk->sk_write_space(sk);
1529 return TIPC_OK;
1530 }
1531
Per Lidenb97bf3f2006-01-02 19:04:38 +01001532 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001533 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001534 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001535
Per Lidenb97bf3f2006-01-02 19:04:38 +01001536 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001537 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001538 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001539 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001540 rc = filter_connect(tsk, &buf);
1541 if (rc != TIPC_OK || buf == NULL)
1542 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001543 }
1544
1545 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001546 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001547 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001548
Ying Xueaba79f32013-01-20 23:30:09 +01001549 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001550 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001551 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001552 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001553
David S. Miller676d2362014-04-11 16:15:36 -04001554 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001555 return TIPC_OK;
1556}
1557
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001558/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001559 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001560 * @sk: socket
1561 * @buf: message
1562 *
1563 * Caller must hold socket lock, but not port lock.
1564 *
1565 * Returns 0
1566 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001567static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001568{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001569 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001570 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001571 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001572 uint truesize = buf->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001573
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001574 rc = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001575
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001576 if (likely(!rc)) {
1577 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1578 atomic_add(truesize, &tsk->dupl_rcvcnt);
1579 return 0;
1580 }
1581
1582 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1583 return 0;
1584
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001585 tipc_link_xmit(buf, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001586
Allan Stephens0c3141e2008-04-15 00:22:02 -07001587 return 0;
1588}
1589
1590/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001591 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001592 * @buf: buffer containing arriving message
1593 * Consumes buffer
1594 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001595 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001596int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001597{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001598 struct tipc_sock *tsk;
1599 struct tipc_port *port;
1600 struct sock *sk;
1601 u32 dport = msg_destport(buf_msg(buf));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001602 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001603 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001604 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001605
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001606 /* Validate destination and message */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001607 port = tipc_port_lock(dport);
1608 if (unlikely(!port)) {
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001609 rc = tipc_msg_eval(buf, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001610 goto exit;
1611 }
1612
1613 tsk = tipc_port_to_sock(port);
1614 sk = &tsk->sk;
1615
1616 /* Queue message */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001617 bh_lock_sock(sk);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001618
Allan Stephens0c3141e2008-04-15 00:22:02 -07001619 if (!sock_owned_by_user(sk)) {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001620 rc = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001621 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001622 if (sk->sk_backlog.len == 0)
1623 atomic_set(&tsk->dupl_rcvcnt, 0);
1624 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1625 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001626 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001627 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001628 bh_unlock_sock(sk);
1629 tipc_port_unlock(port);
1630
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001631 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001632 return 0;
1633exit:
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001634 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001635 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001636
Jon Paul Maloy9fbfb8b2014-07-16 20:41:03 -04001637 tipc_link_xmit(buf, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001638 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001639}
1640
Ying Xue78eb3a52014-01-17 09:50:03 +08001641static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1642{
1643 struct sock *sk = sock->sk;
1644 DEFINE_WAIT(wait);
1645 int done;
1646
1647 do {
1648 int err = sock_error(sk);
1649 if (err)
1650 return err;
1651 if (!*timeo_p)
1652 return -ETIMEDOUT;
1653 if (signal_pending(current))
1654 return sock_intr_errno(*timeo_p);
1655
1656 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1657 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1658 finish_wait(sk_sleep(sk), &wait);
1659 } while (!done);
1660 return 0;
1661}
1662
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663/**
Ying Xue247f0f32014-02-18 16:06:46 +08001664 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665 * @sock: socket structure
1666 * @dest: socket address for destination port
1667 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001668 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669 *
1670 * Returns 0 on success, errno otherwise
1671 */
Ying Xue247f0f32014-02-18 16:06:46 +08001672static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1673 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001675 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001676 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1677 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001678 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1679 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001680 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001681
Allan Stephens0c3141e2008-04-15 00:22:02 -07001682 lock_sock(sk);
1683
Allan Stephensb89741a2008-04-15 00:20:37 -07001684 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001685 if (sock->state == SS_READY) {
1686 res = -EOPNOTSUPP;
1687 goto exit;
1688 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001689
Allan Stephensb89741a2008-04-15 00:20:37 -07001690 /*
1691 * Reject connection attempt using multicast address
1692 *
1693 * Note: send_msg() validates the rest of the address fields,
1694 * so there's no need to do it here
1695 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001696 if (dst->addrtype == TIPC_ADDR_MCAST) {
1697 res = -EINVAL;
1698 goto exit;
1699 }
1700
Ying Xue78eb3a52014-01-17 09:50:03 +08001701 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001702 switch (sock->state) {
1703 case SS_UNCONNECTED:
1704 /* Send a 'SYN-' to destination */
1705 m.msg_name = dest;
1706 m.msg_namelen = destlen;
1707
1708 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1709 * indicate send_msg() is never blocked.
1710 */
1711 if (!timeout)
1712 m.msg_flags = MSG_DONTWAIT;
1713
Ying Xue247f0f32014-02-18 16:06:46 +08001714 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001715 if ((res < 0) && (res != -EWOULDBLOCK))
1716 goto exit;
1717
1718 /* Just entered SS_CONNECTING state; the only
1719 * difference is that return value in non-blocking
1720 * case is EINPROGRESS, rather than EALREADY.
1721 */
1722 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001723 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001724 if (previous == SS_CONNECTING)
1725 res = -EALREADY;
1726 if (!timeout)
1727 goto exit;
1728 timeout = msecs_to_jiffies(timeout);
1729 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1730 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001731 break;
1732 case SS_CONNECTED:
1733 res = -EISCONN;
1734 break;
1735 default:
1736 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001737 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001738 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001739exit:
1740 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001741 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001742}
1743
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001744/**
Ying Xue247f0f32014-02-18 16:06:46 +08001745 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746 * @sock: socket structure
1747 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001748 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749 * Returns 0 on success, errno otherwise
1750 */
Ying Xue247f0f32014-02-18 16:06:46 +08001751static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001752{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001753 struct sock *sk = sock->sk;
1754 int res;
1755
1756 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001757
Ying Xue245f3d32011-07-06 06:01:13 -04001758 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001759 res = -EINVAL;
1760 else {
1761 sock->state = SS_LISTENING;
1762 res = 0;
1763 }
1764
1765 release_sock(sk);
1766 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001767}
1768
Ying Xue6398e232014-01-17 09:50:04 +08001769static int tipc_wait_for_accept(struct socket *sock, long timeo)
1770{
1771 struct sock *sk = sock->sk;
1772 DEFINE_WAIT(wait);
1773 int err;
1774
1775 /* True wake-one mechanism for incoming connections: only
1776 * one process gets woken up, not the 'whole herd'.
1777 * Since we do not 'race & poll' for established sockets
1778 * anymore, the common case will execute the loop only once.
1779 */
1780 for (;;) {
1781 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1782 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001783 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001784 release_sock(sk);
1785 timeo = schedule_timeout(timeo);
1786 lock_sock(sk);
1787 }
1788 err = 0;
1789 if (!skb_queue_empty(&sk->sk_receive_queue))
1790 break;
1791 err = -EINVAL;
1792 if (sock->state != SS_LISTENING)
1793 break;
1794 err = sock_intr_errno(timeo);
1795 if (signal_pending(current))
1796 break;
1797 err = -EAGAIN;
1798 if (!timeo)
1799 break;
1800 }
1801 finish_wait(sk_sleep(sk), &wait);
1802 return err;
1803}
1804
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001805/**
Ying Xue247f0f32014-02-18 16:06:46 +08001806 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001807 * @sock: listening socket
1808 * @newsock: new socket that is to be connected
1809 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001810 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001811 * Returns 0 on success, errno otherwise
1812 */
Ying Xue247f0f32014-02-18 16:06:46 +08001813static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001815 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001817 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001818 struct tipc_msg *msg;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001819 struct tipc_portid peer;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001820 u32 new_ref;
Ying Xue6398e232014-01-17 09:50:04 +08001821 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001822 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001823
Allan Stephens0c3141e2008-04-15 00:22:02 -07001824 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001825
Allan Stephens0c3141e2008-04-15 00:22:02 -07001826 if (sock->state != SS_LISTENING) {
1827 res = -EINVAL;
1828 goto exit;
1829 }
Ying Xue6398e232014-01-17 09:50:04 +08001830 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1831 res = tipc_wait_for_accept(sock, timeo);
1832 if (res)
1833 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001834
1835 buf = skb_peek(&sk->sk_receive_queue);
1836
Ying Xuec5fa7b32013-06-17 10:54:39 -04001837 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001838 if (res)
1839 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001840
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001841 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001842 new_port = &tipc_sk(new_sk)->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001843 new_ref = new_port->ref;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001844 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001845
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001846 /* we lock on new_sk; but lockdep sees the lock on sk */
1847 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001848
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001849 /*
1850 * Reject any stray messages received by new socket
1851 * before the socket lock was taken (very, very unlikely)
1852 */
1853 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001854
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001855 /* Connect new socket to it's peer */
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001856 peer.ref = msg_origport(msg);
1857 peer.node = msg_orignode(msg);
1858 tipc_port_connect(new_ref, &peer);
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 Maloy58ed9442014-03-12 11:31:12 -04001922 tipc_port_disconnect(port->ref);
Jon Paul Maloy80e44c22014-08-22 18:09:10 -04001923 if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN))
1924 tipc_link_xmit(buf, 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);
1933 __tipc_port_disconnect(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001934 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001935
1936 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001937
1938 /* fall through */
1939
1940 case SS_DISCONNECTING:
1941
Ying Xue75031152012-10-29 09:38:15 -04001942 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001943 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001944
1945 /* Wake up anyone sleeping in poll */
1946 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001947 res = 0;
1948 break;
1949
1950 default:
1951 res = -ENOTCONN;
1952 }
1953
Allan Stephens0c3141e2008-04-15 00:22:02 -07001954 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001955 return res;
1956}
1957
Jon Paul Maloy57289012014-08-22 18:09:09 -04001958static void tipc_sk_timeout(unsigned long ref)
1959{
1960 struct tipc_port *port = tipc_port_lock(ref);
1961 struct tipc_sock *tsk;
1962 struct sock *sk;
1963 struct sk_buff *buf = NULL;
1964 struct tipc_msg *msg = NULL;
1965 u32 peer_port, peer_node;
1966
1967 if (!port)
1968 return;
1969
1970 if (!port->connected) {
1971 tipc_port_unlock(port);
1972 return;
1973 }
1974 tsk = tipc_port_to_sock(port);
1975 sk = &tsk->sk;
1976 bh_lock_sock(sk);
1977 peer_port = tipc_port_peerport(port);
1978 peer_node = tipc_port_peernode(port);
1979
1980 if (port->probing_state == TIPC_CONN_PROBING) {
1981 /* Previous probe not answered -> self abort */
1982 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
1983 SHORT_H_SIZE, 0, tipc_own_addr,
1984 peer_node, ref, peer_port,
1985 TIPC_ERR_NO_PORT);
1986 } else {
1987 buf = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
1988 0, peer_node, tipc_own_addr,
1989 peer_port, ref, TIPC_OK);
1990 port->probing_state = TIPC_CONN_PROBING;
1991 k_start_timer(&port->timer, port->probing_interval);
1992 }
1993 bh_unlock_sock(sk);
1994 tipc_port_unlock(port);
1995 if (!buf)
1996 return;
1997
1998 msg = buf_msg(buf);
1999 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
2000}
2001
Per Lidenb97bf3f2006-01-02 19:04:38 +01002002/**
Ying Xue247f0f32014-02-18 16:06:46 +08002003 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002004 * @sock: socket structure
2005 * @lvl: option level
2006 * @opt: option identifier
2007 * @ov: pointer to new option value
2008 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002009 *
2010 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002011 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002012 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002013 * Returns 0 on success, errno otherwise
2014 */
Ying Xue247f0f32014-02-18 16:06:46 +08002015static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2016 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002017{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002018 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002019 struct tipc_sock *tsk = tipc_sk(sk);
2020 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002021 u32 value;
2022 int res;
2023
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002024 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2025 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002026 if (lvl != SOL_TIPC)
2027 return -ENOPROTOOPT;
2028 if (ol < sizeof(value))
2029 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00002030 res = get_user(value, (u32 __user *)ov);
2031 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002032 return res;
2033
Allan Stephens0c3141e2008-04-15 00:22:02 -07002034 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002035
Per Lidenb97bf3f2006-01-02 19:04:38 +01002036 switch (opt) {
2037 case TIPC_IMPORTANCE:
Erik Hugneac32c7f2014-08-15 16:44:35 +02002038 res = tipc_port_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002039 break;
2040 case TIPC_SRC_DROPPABLE:
2041 if (sock->type != SOCK_STREAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002042 tipc_port_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002043 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002044 res = -ENOPROTOOPT;
2045 break;
2046 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002047 tipc_port_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002048 break;
2049 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002050 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002051 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002052 break;
2053 default:
2054 res = -EINVAL;
2055 }
2056
Allan Stephens0c3141e2008-04-15 00:22:02 -07002057 release_sock(sk);
2058
Per Lidenb97bf3f2006-01-02 19:04:38 +01002059 return res;
2060}
2061
2062/**
Ying Xue247f0f32014-02-18 16:06:46 +08002063 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002064 * @sock: socket structure
2065 * @lvl: option level
2066 * @opt: option identifier
2067 * @ov: receptacle for option value
2068 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002069 *
2070 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002071 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002072 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002073 * Returns 0 on success, errno otherwise
2074 */
Ying Xue247f0f32014-02-18 16:06:46 +08002075static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2076 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002077{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002078 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002079 struct tipc_sock *tsk = tipc_sk(sk);
2080 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002081 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002082 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002083 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002084
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002085 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2086 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002087 if (lvl != SOL_TIPC)
2088 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002089 res = get_user(len, ol);
2090 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002091 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002092
Allan Stephens0c3141e2008-04-15 00:22:02 -07002093 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002094
2095 switch (opt) {
2096 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002097 value = tipc_port_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002098 break;
2099 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002100 value = tipc_port_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002101 break;
2102 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002103 value = tipc_port_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002104 break;
2105 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002106 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002107 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002108 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002109 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002110 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002111 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002112 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002113 value = skb_queue_len(&sk->sk_receive_queue);
2114 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002115 default:
2116 res = -EINVAL;
2117 }
2118
Allan Stephens0c3141e2008-04-15 00:22:02 -07002119 release_sock(sk);
2120
Paul Gortmaker25860c32010-12-31 18:59:31 +00002121 if (res)
2122 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002123
Paul Gortmaker25860c32010-12-31 18:59:31 +00002124 if (len < sizeof(value))
2125 return -EINVAL;
2126
2127 if (copy_to_user(ov, &value, sizeof(value)))
2128 return -EFAULT;
2129
2130 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002131}
2132
Wei Yongjun52f50ce2014-07-20 13:14:28 +08002133static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002134{
2135 struct tipc_sioc_ln_req lnr;
2136 void __user *argp = (void __user *)arg;
2137
2138 switch (cmd) {
2139 case SIOCGETLINKNAME:
2140 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2141 return -EFAULT;
2142 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
2143 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2144 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2145 return -EFAULT;
2146 return 0;
2147 }
2148 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002149 default:
2150 return -ENOIOCTLCMD;
2151 }
2152}
2153
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002154/* Protocol switches for the various types of TIPC sockets */
2155
Florian Westphalbca65ea2008-02-07 18:18:01 -08002156static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002157 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002158 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002159 .release = tipc_release,
2160 .bind = tipc_bind,
2161 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002162 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002163 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002164 .getname = tipc_getname,
2165 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002166 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002167 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002168 .shutdown = tipc_shutdown,
2169 .setsockopt = tipc_setsockopt,
2170 .getsockopt = tipc_getsockopt,
2171 .sendmsg = tipc_sendmsg,
2172 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002173 .mmap = sock_no_mmap,
2174 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002175};
2176
Florian Westphalbca65ea2008-02-07 18:18:01 -08002177static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002178 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002179 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002180 .release = tipc_release,
2181 .bind = tipc_bind,
2182 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002183 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002184 .accept = tipc_accept,
2185 .getname = tipc_getname,
2186 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002187 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002188 .listen = tipc_listen,
2189 .shutdown = tipc_shutdown,
2190 .setsockopt = tipc_setsockopt,
2191 .getsockopt = tipc_getsockopt,
2192 .sendmsg = tipc_send_packet,
2193 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002194 .mmap = sock_no_mmap,
2195 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002196};
2197
Florian Westphalbca65ea2008-02-07 18:18:01 -08002198static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002199 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002200 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002201 .release = tipc_release,
2202 .bind = tipc_bind,
2203 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002204 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002205 .accept = tipc_accept,
2206 .getname = tipc_getname,
2207 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002208 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002209 .listen = tipc_listen,
2210 .shutdown = tipc_shutdown,
2211 .setsockopt = tipc_setsockopt,
2212 .getsockopt = tipc_getsockopt,
2213 .sendmsg = tipc_send_stream,
2214 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002215 .mmap = sock_no_mmap,
2216 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002217};
2218
Florian Westphalbca65ea2008-02-07 18:18:01 -08002219static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002220 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002221 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002222 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002223};
2224
2225static struct proto tipc_proto = {
2226 .name = "TIPC",
2227 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002228 .obj_size = sizeof(struct tipc_sock),
2229 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002230};
2231
Ying Xuec5fa7b32013-06-17 10:54:39 -04002232static struct proto tipc_proto_kern = {
2233 .name = "TIPC",
2234 .obj_size = sizeof(struct tipc_sock),
2235 .sysctl_rmem = sysctl_tipc_rmem
2236};
2237
Per Lidenb97bf3f2006-01-02 19:04:38 +01002238/**
Per Liden4323add2006-01-18 00:38:21 +01002239 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002240 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002241 * Returns 0 on success, errno otherwise
2242 */
Per Liden4323add2006-01-18 00:38:21 +01002243int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002244{
2245 int res;
2246
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002247 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002248 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002249 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002250 goto out;
2251 }
2252
2253 res = sock_register(&tipc_family_ops);
2254 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002255 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002256 proto_unregister(&tipc_proto);
2257 goto out;
2258 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002259 out:
2260 return res;
2261}
2262
2263/**
Per Liden4323add2006-01-18 00:38:21 +01002264 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002265 */
Per Liden4323add2006-01-18 00:38:21 +01002266void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002267{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002268 sock_unregister(tipc_family_ops.family);
2269 proto_unregister(&tipc_proto);
2270}