blob: b28eea50c7fcc901bb0deab3576034e8317788a8 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04004 * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000038#include "port.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050039#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020040#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050041#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040042#include <linux/export.h>
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -050043#include "link.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040044
Per Lidenb97bf3f2006-01-02 19:04:38 +010045#define SS_LISTENING -1 /* socket is listening */
46#define SS_READY -2 /* socket is connectionless */
47
Allan Stephens3654ea02008-04-13 21:35:11 -070048#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Jon Paul Maloyac0074e2014-06-25 20:41:41 -050049#define TIPC_FWD_MSG 1
Per Lidenb97bf3f2006-01-02 19:04:38 +010050
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -040051static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -040052static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +080053static void tipc_write_space(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +080054static int tipc_release(struct socket *sock);
55static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -040056static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
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))
134 tipc_link_xmit2(buf, dnode, 0);
135 }
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 Maloy4f4482d2014-05-14 05:39:09 -0400206 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400207 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800208 sk->sk_data_ready = tipc_data_ready;
209 sk->sk_write_space = tipc_write_space;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400210 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500211 tsk->sent_unacked = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400212 atomic_set(&tsk->dupl_rcvcnt, 0);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400213 tipc_port_unlock(port);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700214
Allan Stephens0c3141e2008-04-15 00:22:02 -0700215 if (sock->state == SS_READY) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400216 tipc_port_set_unreturnable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700217 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400218 tipc_port_set_unreliable(port, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700219 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 return 0;
221}
222
223/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400224 * tipc_sock_create_local - create TIPC socket from inside TIPC module
225 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
226 *
227 * We cannot use sock_creat_kern here because it bumps module user count.
228 * Since socket owner and creator is the same module we must make sure
229 * that module count remains zero for module local sockets, otherwise
230 * we cannot do rmmod.
231 *
232 * Returns 0 on success, errno otherwise
233 */
234int tipc_sock_create_local(int type, struct socket **res)
235{
236 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400237
238 rc = sock_create_lite(AF_TIPC, type, 0, res);
239 if (rc < 0) {
240 pr_err("Failed to create kernel socket\n");
241 return rc;
242 }
243 tipc_sk_create(&init_net, *res, 0, 1);
244
Ying Xuec5fa7b32013-06-17 10:54:39 -0400245 return 0;
246}
247
248/**
249 * tipc_sock_release_local - release socket created by tipc_sock_create_local
250 * @sock: the socket to be released.
251 *
252 * Module reference count is not incremented when such sockets are created,
253 * so we must keep it from being decremented when they are released.
254 */
255void tipc_sock_release_local(struct socket *sock)
256{
Ying Xue247f0f32014-02-18 16:06:46 +0800257 tipc_release(sock);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400258 sock->ops = NULL;
259 sock_release(sock);
260}
261
262/**
263 * tipc_sock_accept_local - accept a connection on a socket created
264 * with tipc_sock_create_local. Use this function to avoid that
265 * module reference count is inadvertently incremented.
266 *
267 * @sock: the accepting socket
268 * @newsock: reference to the new socket to be created
269 * @flags: socket flags
270 */
271
272int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400273 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400274{
275 struct sock *sk = sock->sk;
276 int ret;
277
278 ret = sock_create_lite(sk->sk_family, sk->sk_type,
279 sk->sk_protocol, newsock);
280 if (ret < 0)
281 return ret;
282
Ying Xue247f0f32014-02-18 16:06:46 +0800283 ret = tipc_accept(sock, *newsock, flags);
Ying Xuec5fa7b32013-06-17 10:54:39 -0400284 if (ret < 0) {
285 sock_release(*newsock);
286 return ret;
287 }
288 (*newsock)->ops = sock->ops;
289 return ret;
290}
291
292/**
Ying Xue247f0f32014-02-18 16:06:46 +0800293 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 * @sock: socket to destroy
295 *
296 * This routine cleans up any messages that are still queued on the socket.
297 * For DGRAM and RDM socket types, all queued messages are rejected.
298 * For SEQPACKET and STREAM socket types, the first message is rejected
299 * and any others are discarded. (If the first message on a STREAM socket
300 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900301 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 * NOTE: Rejected messages are not necessarily returned to the sender! They
303 * are returned or discarded according to the "destination droppable" setting
304 * specified for the message by the sender.
305 *
306 * Returns 0 on success, errno otherwise
307 */
Ying Xue247f0f32014-02-18 16:06:46 +0800308static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400311 struct tipc_sock *tsk;
312 struct tipc_port *port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500314 u32 dnode;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315
Allan Stephens0c3141e2008-04-15 00:22:02 -0700316 /*
317 * Exit if socket isn't fully initialized (occurs when a failed accept()
318 * releases a pre-allocated child socket that was never used)
319 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700320 if (sk == NULL)
321 return 0;
322
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400323 tsk = tipc_sk(sk);
324 port = &tsk->port;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700325 lock_sock(sk);
326
327 /*
328 * Reject all unreceived messages, except on an active connection
329 * (which disconnects locally & sends a 'FIN+' to peer)
330 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700332 buf = __skb_dequeue(&sk->sk_receive_queue);
333 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334 break;
Ying Xue40682432013-10-18 07:23:16 +0200335 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400336 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700337 else {
338 if ((sock->state == SS_CONNECTING) ||
339 (sock->state == SS_CONNECTED)) {
340 sock->state = SS_DISCONNECTING;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400341 tipc_port_disconnect(port->ref);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700342 }
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -0500343 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
344 tipc_link_xmit2(buf, dnode, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700345 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346 }
347
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400348 /* Destroy TIPC port; also disconnects an active connection and
349 * sends a 'FIN-' to peer.
Allan Stephens0c3141e2008-04-15 00:22:02 -0700350 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400351 tipc_port_destroy(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352
Allan Stephens0c3141e2008-04-15 00:22:02 -0700353 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100354 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355
Allan Stephens0c3141e2008-04-15 00:22:02 -0700356 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700357 sock->state = SS_DISCONNECTING;
358 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359
360 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700361 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200363 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364}
365
366/**
Ying Xue247f0f32014-02-18 16:06:46 +0800367 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368 * @sock: socket structure
369 * @uaddr: socket address describing name(s) and desired operation
370 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900371 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372 * Name and name sequence binding is indicated using a positive scope value;
373 * a negative scope value unbinds the specified name. Specifying no name
374 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900375 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700377 *
378 * NOTE: This routine doesn't need to take the socket lock since it doesn't
379 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380 */
Ying Xue247f0f32014-02-18 16:06:46 +0800381static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
382 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383{
Ying Xue84602762013-12-27 10:18:28 +0800384 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400386 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800387 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388
Ying Xue84602762013-12-27 10:18:28 +0800389 lock_sock(sk);
390 if (unlikely(!uaddr_len)) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400391 res = tipc_withdraw(&tsk->port, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800392 goto exit;
393 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900394
Ying Xue84602762013-12-27 10:18:28 +0800395 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
396 res = -EINVAL;
397 goto exit;
398 }
399 if (addr->family != AF_TIPC) {
400 res = -EAFNOSUPPORT;
401 goto exit;
402 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404 if (addr->addrtype == TIPC_ADDR_NAME)
405 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800406 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
407 res = -EAFNOSUPPORT;
408 goto exit;
409 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900410
Ying Xue13a2e892013-06-17 10:54:40 -0400411 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400412 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800413 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
414 res = -EACCES;
415 goto exit;
416 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400417
Ying Xue84602762013-12-27 10:18:28 +0800418 res = (addr->scope > 0) ?
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400419 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
420 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800421exit:
422 release_sock(sk);
423 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424}
425
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900426/**
Ying Xue247f0f32014-02-18 16:06:46 +0800427 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428 * @sock: socket structure
429 * @uaddr: area for returned socket address
430 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700431 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900432 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100433 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700434 *
Allan Stephens2da59912008-07-14 22:43:32 -0700435 * NOTE: This routine doesn't need to take the socket lock since it only
436 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000437 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438 */
Ying Xue247f0f32014-02-18 16:06:46 +0800439static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
440 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400443 struct tipc_sock *tsk = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000445 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700446 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700447 if ((sock->state != SS_CONNECTED) &&
448 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
449 return -ENOTCONN;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400450 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
451 addr->addr.id.node = tipc_port_peernode(&tsk->port);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700452 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400453 addr->addr.id.ref = tsk->port.ref;
Allan Stephensb924dcf2010-11-30 12:01:03 +0000454 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700455 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456
457 *uaddr_len = sizeof(*addr);
458 addr->addrtype = TIPC_ADDR_ID;
459 addr->family = AF_TIPC;
460 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461 addr->addr.name.domain = 0;
462
Allan Stephens0c3141e2008-04-15 00:22:02 -0700463 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464}
465
466/**
Ying Xue247f0f32014-02-18 16:06:46 +0800467 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468 * @file: file structure associated with the socket
469 * @sock: socket for which to calculate the poll bits
470 * @wait: ???
471 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700472 * Returns pollmask value
473 *
474 * COMMENTARY:
475 * It appears that the usual socket locking mechanisms are not useful here
476 * since the pollmask info is potentially out-of-date the moment this routine
477 * exits. TCP and other protocols seem to rely on higher level poll routines
478 * to handle any preventable race conditions, so TIPC will do the same ...
479 *
480 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700481 *
Allan Stephensf662c072010-08-17 11:00:06 +0000482 * socket state flags set
483 * ------------ ---------
484 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200485 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000486 *
487 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
488 * no write flags
489 *
490 * connected POLLIN/POLLRDNORM if data in rx queue
491 * POLLOUT if port is not congested
492 *
493 * disconnecting POLLIN/POLLRDNORM/POLLHUP
494 * no write flags
495 *
496 * listening POLLIN if SYN in rx queue
497 * no write flags
498 *
499 * ready POLLIN/POLLRDNORM if data in rx queue
500 * [connectionless] POLLOUT (since port cannot be congested)
501 *
502 * IMPORTANT: The fact that a read or write operation is indicated does NOT
503 * imply that the operation will succeed, merely that it should be performed
504 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 */
Ying Xue247f0f32014-02-18 16:06:46 +0800506static unsigned int tipc_poll(struct file *file, struct socket *sock,
507 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508{
Allan Stephens9b674e82008-03-26 16:48:21 -0700509 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400510 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000511 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700512
Ying Xuef288bef2012-08-21 11:16:57 +0800513 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700514
Allan Stephensf662c072010-08-17 11:00:06 +0000515 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200516 case SS_UNCONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500517 if (!tsk->link_cong)
Erik Hugnec4fc2982012-10-16 16:47:06 +0200518 mask |= POLLOUT;
519 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000520 case SS_READY:
521 case SS_CONNECTED:
Jon Paul Maloy60120522014-06-25 20:41:42 -0500522 if (!tsk->link_cong && !tipc_sk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000523 mask |= POLLOUT;
524 /* fall thru' */
525 case SS_CONNECTING:
526 case SS_LISTENING:
527 if (!skb_queue_empty(&sk->sk_receive_queue))
528 mask |= (POLLIN | POLLRDNORM);
529 break;
530 case SS_DISCONNECTING:
531 mask = (POLLIN | POLLRDNORM | POLLHUP);
532 break;
533 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700534
535 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100536}
537
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400538/**
539 * tipc_sendmcast - send multicast message
540 * @sock: socket structure
541 * @seq: destination address
542 * @iov: message data to send
543 * @dsz: total length of message data
544 * @timeo: timeout to wait for wakeup
545 *
546 * Called from function tipc_sendmsg(), which has done all sanity checks
547 * Returns the number of bytes sent on success, or errno
548 */
549static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
550 struct iovec *iov, size_t dsz, long timeo)
551{
552 struct sock *sk = sock->sk;
553 struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr;
554 struct sk_buff *buf;
555 uint mtu;
556 int rc;
557
558 msg_set_type(mhdr, TIPC_MCAST_MSG);
559 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
560 msg_set_destport(mhdr, 0);
561 msg_set_destnode(mhdr, 0);
562 msg_set_nametype(mhdr, seq->type);
563 msg_set_namelower(mhdr, seq->lower);
564 msg_set_nameupper(mhdr, seq->upper);
565 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
566
567new_mtu:
568 mtu = tipc_bclink_get_mtu();
569 rc = tipc_msg_build2(mhdr, iov, 0, dsz, mtu, &buf);
570 if (unlikely(rc < 0))
571 return rc;
572
573 do {
574 rc = tipc_bclink_xmit(buf);
575 if (likely(rc >= 0)) {
576 rc = dsz;
577 break;
578 }
579 if (rc == -EMSGSIZE)
580 goto new_mtu;
581 if (rc != -ELINKCONG)
582 break;
583 rc = tipc_wait_for_sndmsg(sock, &timeo);
584 if (rc)
585 kfree_skb_list(buf);
586 } while (!rc);
587 return rc;
588}
589
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400590/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
591 */
592void tipc_sk_mcast_rcv(struct sk_buff *buf)
593{
594 struct tipc_msg *msg = buf_msg(buf);
595 struct tipc_port_list dports = {0, NULL, };
596 struct tipc_port_list *item;
597 struct sk_buff *b;
598 uint i, last, dst = 0;
599 u32 scope = TIPC_CLUSTER_SCOPE;
600
601 if (in_own_node(msg_orignode(msg)))
602 scope = TIPC_NODE_SCOPE;
603
604 /* Create destination port list: */
605 tipc_nametbl_mc_translate(msg_nametype(msg),
606 msg_namelower(msg),
607 msg_nameupper(msg),
608 scope,
609 &dports);
610 last = dports.count;
611 if (!last) {
612 kfree_skb(buf);
613 return;
614 }
615
616 for (item = &dports; item; item = item->next) {
617 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
618 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
619 if (!b) {
620 pr_warn("Failed do clone mcast rcv buffer\n");
621 continue;
622 }
623 msg_set_destport(msg, item->ports[i]);
624 tipc_sk_rcv(b);
625 }
626 }
627 tipc_port_list_free(&dports);
628}
629
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900630/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500631 * tipc_sk_proto_rcv - receive a connection mng protocol message
632 * @tsk: receiving socket
633 * @dnode: node to send response message to, if any
634 * @buf: buffer containing protocol message
635 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
636 * (CONN_PROBE_REPLY) message should be forwarded.
637 */
638int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode, struct sk_buff *buf)
639{
640 struct tipc_msg *msg = buf_msg(buf);
641 struct tipc_port *port = &tsk->port;
Jon Paul Maloy60120522014-06-25 20:41:42 -0500642 int conn_cong;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500643
644 /* Ignore if connection cannot be validated: */
645 if (!port->connected || !tipc_port_peer_msg(port, msg))
646 goto exit;
647
648 port->probing_state = TIPC_CONN_OK;
649
650 if (msg_type(msg) == CONN_ACK) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500651 conn_cong = tipc_sk_conn_cong(tsk);
652 tsk->sent_unacked -= msg_msgcnt(msg);
653 if (conn_cong)
654 tipc_sock_wakeup(tsk);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500655 } else if (msg_type(msg) == CONN_PROBE) {
656 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
657 return TIPC_OK;
658 msg_set_type(msg, CONN_PROBE_REPLY);
659 return TIPC_FWD_MSG;
660 }
661 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
662exit:
663 kfree_skb(buf);
664 return TIPC_OK;
665}
666
667/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668 * dest_name_check - verify user is permitted to send to specified port name
669 * @dest: destination address
670 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900671 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 * Prevents restricted configuration commands from being issued by
673 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900674 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675 * Returns 0 if permission is granted, otherwise errno
676 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800677static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678{
679 struct tipc_cfg_msg_hdr hdr;
680
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500681 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
682 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900683 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
684 return 0;
685 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
686 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900687 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
688 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689
Allan Stephens3f8dd942011-01-18 13:09:29 -0500690 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
691 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900692 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700694 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100695 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900696
Per Lidenb97bf3f2006-01-02 19:04:38 +0100697 return 0;
698}
699
Ying Xue3f405042014-01-17 09:50:05 +0800700static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
701{
702 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400703 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue3f405042014-01-17 09:50:05 +0800704 DEFINE_WAIT(wait);
705 int done;
706
707 do {
708 int err = sock_error(sk);
709 if (err)
710 return err;
711 if (sock->state == SS_DISCONNECTING)
712 return -EPIPE;
713 if (!*timeo_p)
714 return -EAGAIN;
715 if (signal_pending(current))
716 return sock_intr_errno(*timeo_p);
717
718 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500719 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
Ying Xue3f405042014-01-17 09:50:05 +0800720 finish_wait(sk_sleep(sk), &wait);
721 } while (!done);
722 return 0;
723}
724
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500725/**
Ying Xue247f0f32014-02-18 16:06:46 +0800726 * tipc_sendmsg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700727 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100728 * @sock: socket structure
729 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500730 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900731 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900733 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
735 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900736 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737 * Returns the number of bytes sent on success, or errno otherwise
738 */
Ying Xue247f0f32014-02-18 16:06:46 +0800739static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500740 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741{
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500742 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700743 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400744 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy5c311422014-03-12 11:31:13 -0400745 struct tipc_port *port = &tsk->port;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500746 struct tipc_msg *mhdr = &port->phdr;
747 struct iovec *iov = m->msg_iov;
748 u32 dnode, dport;
749 struct sk_buff *buf;
750 struct tipc_name_seq *seq = &dest->addr.nameseq;
751 u32 mtu;
Ying Xue3f405042014-01-17 09:50:05 +0800752 long timeo;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500753 int rc = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100754
755 if (unlikely(!dest))
756 return -EDESTADDRREQ;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500757
Allan Stephens51f9cc12006-06-25 23:49:06 -0700758 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
759 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500761
762 if (dsz > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400763 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764
Allan Stephens0c3141e2008-04-15 00:22:02 -0700765 if (iocb)
766 lock_sock(sk);
767
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500768 if (unlikely(sock->state != SS_READY)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700769 if (sock->state == SS_LISTENING) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500770 rc = -EPIPE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700771 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700772 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700773 if (sock->state != SS_UNCONNECTED) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500774 rc = -EISCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700775 goto exit;
776 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400777 if (tsk->port.published) {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500778 rc = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700779 goto exit;
780 }
781 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400782 tsk->port.conn_type = dest->addr.name.name.type;
783 tsk->port.conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700784 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500786 rc = dest_name_check(dest, m);
787 if (rc)
788 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789
Ying Xue3f405042014-01-17 09:50:05 +0800790 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500791
792 if (dest->addrtype == TIPC_ADDR_MCAST) {
793 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
794 goto exit;
795 } else if (dest->addrtype == TIPC_ADDR_NAME) {
796 u32 type = dest->addr.name.name.type;
797 u32 inst = dest->addr.name.name.instance;
798 u32 domain = dest->addr.name.domain;
799
800 dnode = domain;
801 msg_set_type(mhdr, TIPC_NAMED_MSG);
802 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
803 msg_set_nametype(mhdr, type);
804 msg_set_nameinst(mhdr, inst);
805 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
806 dport = tipc_nametbl_translate(type, inst, &dnode);
807 msg_set_destnode(mhdr, dnode);
808 msg_set_destport(mhdr, dport);
809 if (unlikely(!dport && !dnode)) {
810 rc = -EHOSTUNREACH;
811 goto exit;
812 }
813 } else if (dest->addrtype == TIPC_ADDR_ID) {
814 dnode = dest->addr.id.node;
815 msg_set_type(mhdr, TIPC_DIRECT_MSG);
816 msg_set_lookup_scope(mhdr, 0);
817 msg_set_destnode(mhdr, dnode);
818 msg_set_destport(mhdr, dest->addr.id.ref);
819 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
820 }
821
822new_mtu:
823 mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
824 rc = tipc_msg_build2(mhdr, iov, 0, dsz, mtu, &buf);
825 if (rc < 0)
826 goto exit;
827
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900828 do {
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500829 rc = tipc_link_xmit2(buf, dnode, tsk->port.ref);
830 if (likely(rc >= 0)) {
831 if (sock->state != SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700832 sock->state = SS_CONNECTING;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500833 rc = dsz;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700834 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900835 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500836 if (rc == -EMSGSIZE)
837 goto new_mtu;
838
839 if (rc != -ELINKCONG)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700840 break;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500841
842 rc = tipc_wait_for_sndmsg(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400843 if (rc)
844 kfree_skb_list(buf);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500845 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700846exit:
847 if (iocb)
848 release_sock(sk);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500849
850 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851}
852
Ying Xue391a6dd2014-01-17 09:50:06 +0800853static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
854{
855 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400856 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue391a6dd2014-01-17 09:50:06 +0800857 DEFINE_WAIT(wait);
858 int done;
859
860 do {
861 int err = sock_error(sk);
862 if (err)
863 return err;
864 if (sock->state == SS_DISCONNECTING)
865 return -EPIPE;
866 else if (sock->state != SS_CONNECTED)
867 return -ENOTCONN;
868 if (!*timeo_p)
869 return -EAGAIN;
870 if (signal_pending(current))
871 return sock_intr_errno(*timeo_p);
872
873 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
874 done = sk_wait_event(sk, timeo_p,
Jon Paul Maloy60120522014-06-25 20:41:42 -0500875 (!tsk->link_cong &&
876 !tipc_sk_conn_cong(tsk)) ||
877 !tsk->port.connected);
Ying Xue391a6dd2014-01-17 09:50:06 +0800878 finish_wait(sk_sleep(sk), &wait);
879 } while (!done);
880 return 0;
881}
882
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900883/**
Ying Xue247f0f32014-02-18 16:06:46 +0800884 * tipc_send_stream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885 * @iocb: (unused)
886 * @sock: socket structure
887 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500888 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900889 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100890 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900891 *
892 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700893 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100894 */
Ying Xue247f0f32014-02-18 16:06:46 +0800895static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500896 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700898 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400899 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500900 struct tipc_port *port = &tsk->port;
901 struct tipc_msg *mhdr = &port->phdr;
902 struct sk_buff *buf;
903 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
904 u32 ref = port->ref;
905 int rc = -EINVAL;
906 long timeo;
907 u32 dnode;
908 uint mtu, send, sent = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900909
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500910 /* Handle implied connection establishment */
911 if (unlikely(dest)) {
912 rc = tipc_sendmsg(iocb, sock, m, dsz);
913 if (dsz && (dsz == rc))
Jon Paul Maloy60120522014-06-25 20:41:42 -0500914 tsk->sent_unacked = 1;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500915 return rc;
916 }
917 if (dsz > (uint)INT_MAX)
918 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700919
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500920 if (iocb)
921 lock_sock(sk);
922
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900923 if (unlikely(sock->state != SS_CONNECTED)) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500924 if (sock->state == SS_DISCONNECTING)
925 rc = -EPIPE;
wangweidongb0555972013-12-27 10:09:39 +0800926 else
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500927 rc = -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800928 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900929 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100930
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500931 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
932 dnode = tipc_port_peernode(port);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500933
934next:
935 mtu = port->max_pkt;
936 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
937 rc = tipc_msg_build2(mhdr, m->msg_iov, sent, send, mtu, &buf);
938 if (unlikely(rc < 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700939 goto exit;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500940 do {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500941 if (likely(!tipc_sk_conn_cong(tsk))) {
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500942 rc = tipc_link_xmit2(buf, dnode, ref);
943 if (likely(!rc)) {
Jon Paul Maloy60120522014-06-25 20:41:42 -0500944 tsk->sent_unacked++;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500945 sent += send;
946 if (sent == dsz)
947 break;
948 goto next;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700949 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500950 if (rc == -EMSGSIZE) {
951 port->max_pkt = tipc_node_get_mtu(dnode, ref);
952 goto next;
953 }
954 if (rc != -ELINKCONG)
955 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100956 }
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500957 rc = tipc_wait_for_sndpkt(sock, &timeo);
Erik Hugne70452dc2014-07-06 20:38:50 -0400958 if (rc)
959 kfree_skb_list(buf);
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500960 } while (!rc);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700961exit:
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -0500962 if (iocb)
963 release_sock(sk);
964 return sent ? sent : rc;
965}
966
967/**
968 * tipc_send_packet - send a connection-oriented message
969 * @iocb: if NULL, indicates that socket lock is already held
970 * @sock: socket structure
971 * @m: message to send
972 * @dsz: length of data to be transmitted
973 *
974 * Used for SOCK_SEQPACKET messages.
975 *
976 * Returns the number of bytes sent on success, or errno otherwise
977 */
978static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
979 struct msghdr *m, size_t dsz)
980{
981 if (dsz > TIPC_MAX_USER_MSG_SIZE)
982 return -EMSGSIZE;
983
984 return tipc_send_stream(iocb, sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985}
986
987/**
988 * auto_connect - complete connection setup to a remote port
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400989 * @tsk: tipc socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900991 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100992 * Returns 0 on success, errno otherwise
993 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400994static int auto_connect(struct tipc_sock *tsk, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400996 struct tipc_port *port = &tsk->port;
997 struct socket *sock = tsk->sk.sk_socket;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -0400998 struct tipc_portid peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100999
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001000 peer.ref = msg_origport(msg);
1001 peer.node = msg_orignode(msg);
1002
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001003 __tipc_port_connect(port->ref, port, &peer);
Ying Xue584d24b2012-11-29 18:51:19 -05001004
1005 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
1006 return -EINVAL;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001007 msg_set_importance(&port->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008 sock->state = SS_CONNECTED;
1009 return 0;
1010}
1011
1012/**
1013 * set_orig_addr - capture sender's address for received message
1014 * @m: descriptor for message info
1015 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001016 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001017 * Note: Address is not captured if not requested by receiver.
1018 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001019static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001021 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001022
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001023 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 addr->family = AF_TIPC;
1025 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001026 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027 addr->addr.id.ref = msg_origport(msg);
1028 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001029 addr->addr.name.domain = 0; /* could leave uninitialized */
1030 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031 m->msg_namelen = sizeof(struct sockaddr_tipc);
1032 }
1033}
1034
1035/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001036 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037 * @m: descriptor for message info
1038 * @msg: received message header
1039 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001040 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001042 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043 * Returns 0 if successful, otherwise errno
1044 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001045static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001046 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001047{
1048 u32 anc_data[3];
1049 u32 err;
1050 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001051 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052 int res;
1053
1054 if (likely(m->msg_controllen == 0))
1055 return 0;
1056
1057 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001058 err = msg ? msg_errcode(msg) : 0;
1059 if (unlikely(err)) {
1060 anc_data[0] = err;
1061 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001062 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1063 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001065 if (anc_data[1]) {
1066 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1067 msg_data(msg));
1068 if (res)
1069 return res;
1070 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 }
1072
1073 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1075 switch (dest_type) {
1076 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001077 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 anc_data[0] = msg_nametype(msg);
1079 anc_data[1] = msg_namelower(msg);
1080 anc_data[2] = msg_namelower(msg);
1081 break;
1082 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001083 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084 anc_data[0] = msg_nametype(msg);
1085 anc_data[1] = msg_namelower(msg);
1086 anc_data[2] = msg_nameupper(msg);
1087 break;
1088 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001089 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090 anc_data[0] = tport->conn_type;
1091 anc_data[1] = tport->conn_instance;
1092 anc_data[2] = tport->conn_instance;
1093 break;
1094 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001095 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 }
Allan Stephens2db99832010-12-31 18:59:33 +00001097 if (has_name) {
1098 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1099 if (res)
1100 return res;
1101 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001102
1103 return 0;
1104}
1105
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001106static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001107{
1108 struct sock *sk = sock->sk;
1109 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001110 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001111 int err;
1112
1113 for (;;) {
1114 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001115 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001116 if (sock->state == SS_DISCONNECTING) {
1117 err = -ENOTCONN;
1118 break;
1119 }
1120 release_sock(sk);
1121 timeo = schedule_timeout(timeo);
1122 lock_sock(sk);
1123 }
1124 err = 0;
1125 if (!skb_queue_empty(&sk->sk_receive_queue))
1126 break;
1127 err = sock_intr_errno(timeo);
1128 if (signal_pending(current))
1129 break;
1130 err = -EAGAIN;
1131 if (!timeo)
1132 break;
1133 }
1134 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001135 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001136 return err;
1137}
1138
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001139/**
Ying Xue247f0f32014-02-18 16:06:46 +08001140 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 * @iocb: (unused)
1142 * @m: descriptor for message info
1143 * @buf_len: total size of user buffer area
1144 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001145 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1147 * If the complete message doesn't fit in user area, truncate it.
1148 *
1149 * Returns size of returned message data, errno otherwise
1150 */
Ying Xue247f0f32014-02-18 16:06:46 +08001151static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1152 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001154 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001155 struct tipc_sock *tsk = tipc_sk(sk);
1156 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157 struct sk_buff *buf;
1158 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001159 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160 unsigned int sz;
1161 u32 err;
1162 int res;
1163
Allan Stephens0c3141e2008-04-15 00:22:02 -07001164 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001165 if (unlikely(!buf_len))
1166 return -EINVAL;
1167
Allan Stephens0c3141e2008-04-15 00:22:02 -07001168 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169
Allan Stephens0c3141e2008-04-15 00:22:02 -07001170 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171 res = -ENOTCONN;
1172 goto exit;
1173 }
1174
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001175 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001176restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177
Allan Stephens0c3141e2008-04-15 00:22:02 -07001178 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001179 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001180 if (res)
1181 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001182
1183 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001184 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185 msg = buf_msg(buf);
1186 sz = msg_data_sz(msg);
1187 err = msg_errcode(msg);
1188
Per Lidenb97bf3f2006-01-02 19:04:38 +01001189 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001191 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192 goto restart;
1193 }
1194
1195 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 set_orig_addr(m, msg);
1197
1198 /* Capture ancillary data (optional) */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001199 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001200 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201 goto exit;
1202
1203 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001204 if (!err) {
1205 if (unlikely(buf_len < sz)) {
1206 sz = buf_len;
1207 m->msg_flags |= MSG_TRUNC;
1208 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001209 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1210 m->msg_iov, sz);
1211 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001212 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213 res = sz;
1214 } else {
1215 if ((sock->state == SS_READY) ||
1216 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1217 res = 0;
1218 else
1219 res = -ECONNRESET;
1220 }
1221
1222 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001224 if ((sock->state != SS_READY) &&
Jon Paul Maloy60120522014-06-25 20:41:42 -05001225 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1226 tipc_acknowledge(port->ref, tsk->rcv_unacked);
1227 tsk->rcv_unacked = 0;
1228 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001229 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001230 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001232 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233 return res;
1234}
1235
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001236/**
Ying Xue247f0f32014-02-18 16:06:46 +08001237 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238 * @iocb: (unused)
1239 * @m: descriptor for message info
1240 * @buf_len: total size of user buffer area
1241 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001242 *
1243 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001244 * will optionally wait for more; never truncates data.
1245 *
1246 * Returns size of returned message data, errno otherwise
1247 */
Ying Xue247f0f32014-02-18 16:06:46 +08001248static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1249 struct msghdr *m, size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001250{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001251 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001252 struct tipc_sock *tsk = tipc_sk(sk);
1253 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001254 struct sk_buff *buf;
1255 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001256 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001257 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001258 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001260 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001261 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262
1263 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264 if (unlikely(!buf_len))
1265 return -EINVAL;
1266
Allan Stephens0c3141e2008-04-15 00:22:02 -07001267 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001269 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001270 res = -ENOTCONN;
1271 goto exit;
1272 }
1273
Florian Westphal3720d402010-08-17 11:00:04 +00001274 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001275 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001276
Allan Stephens0c3141e2008-04-15 00:22:02 -07001277restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001278 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001279 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001280 if (res)
1281 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001282
1283 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001284 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285 msg = buf_msg(buf);
1286 sz = msg_data_sz(msg);
1287 err = msg_errcode(msg);
1288
1289 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001290 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001291 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 goto restart;
1293 }
1294
1295 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001296 if (sz_copied == 0) {
1297 set_orig_addr(m, msg);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001298 res = anc_data_recv(m, msg, port);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001299 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001300 goto exit;
1301 }
1302
1303 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001304 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001305 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306
Allan Stephens0232fd02011-02-21 09:45:40 -05001307 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001308 needed = (buf_len - sz_copied);
1309 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001310
1311 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1312 m->msg_iov, sz_to_copy);
1313 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001315
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316 sz_copied += sz_to_copy;
1317
1318 if (sz_to_copy < sz) {
1319 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001320 TIPC_SKB_CB(buf)->handle =
1321 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001322 goto exit;
1323 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324 } else {
1325 if (sz_copied != 0)
1326 goto exit; /* can't add error msg to valid data */
1327
1328 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1329 res = 0;
1330 else
1331 res = -ECONNRESET;
1332 }
1333
1334 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 if (likely(!(flags & MSG_PEEK))) {
Jon Paul Maloy60120522014-06-25 20:41:42 -05001336 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1337 tipc_acknowledge(port->ref, tsk->rcv_unacked);
1338 tsk->rcv_unacked = 0;
1339 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001340 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001341 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342
1343 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001344 if ((sz_copied < buf_len) && /* didn't get all requested data */
1345 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001346 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001347 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1348 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349 goto restart;
1350
1351exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001352 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001353 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001354}
1355
1356/**
Ying Xuef288bef2012-08-21 11:16:57 +08001357 * tipc_write_space - wake up thread if port congestion is released
1358 * @sk: socket
1359 */
1360static void tipc_write_space(struct sock *sk)
1361{
1362 struct socket_wq *wq;
1363
1364 rcu_read_lock();
1365 wq = rcu_dereference(sk->sk_wq);
1366 if (wq_has_sleeper(wq))
1367 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1368 POLLWRNORM | POLLWRBAND);
1369 rcu_read_unlock();
1370}
1371
1372/**
1373 * tipc_data_ready - wake up threads to indicate messages have been received
1374 * @sk: socket
1375 * @len: the length of messages
1376 */
David S. Miller676d2362014-04-11 16:15:36 -04001377static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001378{
1379 struct socket_wq *wq;
1380
1381 rcu_read_lock();
1382 wq = rcu_dereference(sk->sk_wq);
1383 if (wq_has_sleeper(wq))
1384 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1385 POLLRDNORM | POLLRDBAND);
1386 rcu_read_unlock();
1387}
1388
1389/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001390 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001391 * @tsk: TIPC socket
Ying Xue7e6c1312012-11-29 18:39:14 -05001392 * @msg: message
1393 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001394 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001395 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001396static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
Ying Xue7e6c1312012-11-29 18:39:14 -05001397{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001398 struct sock *sk = &tsk->sk;
1399 struct tipc_port *port = &tsk->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001400 struct socket *sock = sk->sk_socket;
Ying Xue7e6c1312012-11-29 18:39:14 -05001401 struct tipc_msg *msg = buf_msg(*buf);
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001402
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001403 int retval = -TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001404 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001405
1406 if (msg_mcast(msg))
1407 return retval;
1408
1409 switch ((int)sock->state) {
1410 case SS_CONNECTED:
1411 /* Accept only connection-based messages sent by peer */
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001412 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
Ying Xue7e6c1312012-11-29 18:39:14 -05001413 if (unlikely(msg_errcode(msg))) {
1414 sock->state = SS_DISCONNECTING;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001415 __tipc_port_disconnect(port);
Ying Xue7e6c1312012-11-29 18:39:14 -05001416 }
1417 retval = TIPC_OK;
1418 }
1419 break;
1420 case SS_CONNECTING:
1421 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001422 if (unlikely(msg_errcode(msg))) {
1423 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001424 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001425 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001426 break;
1427 }
1428
1429 if (unlikely(!msg_connected(msg)))
1430 break;
1431
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001432 res = auto_connect(tsk, msg);
Ying Xue584d24b2012-11-29 18:51:19 -05001433 if (res) {
1434 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001435 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001436 retval = TIPC_OK;
1437 break;
1438 }
1439
1440 /* If an incoming message is an 'ACK-', it should be
1441 * discarded here because it doesn't contain useful
1442 * data. In addition, we should try to wake up
1443 * connect() routine if sleeping.
1444 */
1445 if (msg_data_sz(msg) == 0) {
1446 kfree_skb(*buf);
1447 *buf = NULL;
1448 if (waitqueue_active(sk_sleep(sk)))
1449 wake_up_interruptible(sk_sleep(sk));
1450 }
1451 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001452 break;
1453 case SS_LISTENING:
1454 case SS_UNCONNECTED:
1455 /* Accept only SYN message */
1456 if (!msg_connected(msg) && !(msg_errcode(msg)))
1457 retval = TIPC_OK;
1458 break;
1459 case SS_DISCONNECTING:
1460 break;
1461 default:
1462 pr_err("Unknown socket state %u\n", sock->state);
1463 }
1464 return retval;
1465}
1466
1467/**
Ying Xueaba79f32013-01-20 23:30:09 +01001468 * rcvbuf_limit - get proper overload limit of socket receive queue
1469 * @sk: socket
1470 * @buf: message
1471 *
1472 * For all connection oriented messages, irrespective of importance,
1473 * the default overload value (i.e. 67MB) is set as limit.
1474 *
1475 * For all connectionless messages, by default new queue limits are
1476 * as belows:
1477 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001478 * TIPC_LOW_IMPORTANCE (4 MB)
1479 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1480 * TIPC_HIGH_IMPORTANCE (16 MB)
1481 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001482 *
1483 * Returns overload limit according to corresponding message importance
1484 */
1485static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1486{
1487 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001488
1489 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001490 return sysctl_tipc_rmem[2];
1491
1492 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1493 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001494}
1495
1496/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001497 * filter_rcv - validate incoming message
1498 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001499 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001500 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001501 * Enqueues message on receive queue if acceptable; optionally handles
1502 * disconnect indication for a connected socket.
1503 *
1504 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001505 *
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001506 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001507 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
Per Lidenb97bf3f2006-01-02 19:04:38 +01001508 */
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001509static int filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001510{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001511 struct socket *sock = sk->sk_socket;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001512 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001513 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001514 unsigned int limit = rcvbuf_limit(sk, buf);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001515 u32 onode;
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001516 int rc = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001517
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001518 if (unlikely(msg_user(msg) == CONN_MANAGER))
1519 return tipc_sk_proto_rcv(tsk, &onode, buf);
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001520
Per Lidenb97bf3f2006-01-02 19:04:38 +01001521 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001522 if (msg_type(msg) > TIPC_DIRECT_MSG)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001523 return -TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001524
Per Lidenb97bf3f2006-01-02 19:04:38 +01001525 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001526 if (msg_connected(msg))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001527 return -TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001528 } else {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001529 rc = filter_connect(tsk, &buf);
1530 if (rc != TIPC_OK || buf == NULL)
1531 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001532 }
1533
1534 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001535 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001536 return -TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001537
Ying Xueaba79f32013-01-20 23:30:09 +01001538 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001539 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001540 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001541 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001542
David S. Miller676d2362014-04-11 16:15:36 -04001543 sk->sk_data_ready(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001544 return TIPC_OK;
1545}
1546
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001547/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001548 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001549 * @sk: socket
1550 * @buf: message
1551 *
1552 * Caller must hold socket lock, but not port lock.
1553 *
1554 * Returns 0
1555 */
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001556static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001557{
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001558 int rc;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001559 u32 onode;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001560 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05001561 uint truesize = buf->truesize;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001562
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001563 rc = filter_rcv(sk, buf);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001564
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001565 if (likely(!rc)) {
1566 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1567 atomic_add(truesize, &tsk->dupl_rcvcnt);
1568 return 0;
1569 }
1570
1571 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1572 return 0;
1573
1574 tipc_link_xmit2(buf, onode, 0);
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001575
Allan Stephens0c3141e2008-04-15 00:22:02 -07001576 return 0;
1577}
1578
1579/**
Jon Paul Maloy24be34b2014-03-12 11:31:10 -04001580 * tipc_sk_rcv - handle incoming message
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001581 * @buf: buffer containing arriving message
1582 * Consumes buffer
1583 * Returns 0 if success, or errno: -EHOSTUNREACH
Allan Stephens0c3141e2008-04-15 00:22:02 -07001584 */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001585int tipc_sk_rcv(struct sk_buff *buf)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001586{
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001587 struct tipc_sock *tsk;
1588 struct tipc_port *port;
1589 struct sock *sk;
1590 u32 dport = msg_destport(buf_msg(buf));
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001591 int rc = TIPC_OK;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001592 uint limit;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001593 u32 dnode;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001594
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001595 /* Validate destination and message */
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001596 port = tipc_port_lock(dport);
1597 if (unlikely(!port)) {
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001598 rc = tipc_msg_eval(buf, &dnode);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001599 goto exit;
1600 }
1601
1602 tsk = tipc_port_to_sock(port);
1603 sk = &tsk->sk;
1604
1605 /* Queue message */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001606 bh_lock_sock(sk);
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001607
Allan Stephens0c3141e2008-04-15 00:22:02 -07001608 if (!sock_owned_by_user(sk)) {
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001609 rc = filter_rcv(sk, buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001610 } else {
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001611 if (sk->sk_backlog.len == 0)
1612 atomic_set(&tsk->dupl_rcvcnt, 0);
1613 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1614 if (sk_add_backlog(sk, buf, limit))
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001615 rc = -TIPC_ERR_OVERLOAD;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001616 }
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001617 bh_unlock_sock(sk);
1618 tipc_port_unlock(port);
1619
Jon Paul Maloye4de5fa2014-06-25 20:41:31 -05001620 if (likely(!rc))
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001621 return 0;
1622exit:
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001623 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001624 return -EHOSTUNREACH;
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001625
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001626 tipc_link_xmit2(buf, dnode, 0);
Jon Paul Maloy5a379072014-06-25 20:41:36 -05001627 return (rc < 0) ? -EHOSTUNREACH : 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001628}
1629
Ying Xue78eb3a52014-01-17 09:50:03 +08001630static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1631{
1632 struct sock *sk = sock->sk;
1633 DEFINE_WAIT(wait);
1634 int done;
1635
1636 do {
1637 int err = sock_error(sk);
1638 if (err)
1639 return err;
1640 if (!*timeo_p)
1641 return -ETIMEDOUT;
1642 if (signal_pending(current))
1643 return sock_intr_errno(*timeo_p);
1644
1645 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1646 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1647 finish_wait(sk_sleep(sk), &wait);
1648 } while (!done);
1649 return 0;
1650}
1651
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652/**
Ying Xue247f0f32014-02-18 16:06:46 +08001653 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001654 * @sock: socket structure
1655 * @dest: socket address for destination port
1656 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001657 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001658 *
1659 * Returns 0 on success, errno otherwise
1660 */
Ying Xue247f0f32014-02-18 16:06:46 +08001661static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1662 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001664 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001665 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1666 struct msghdr m = {NULL,};
Ying Xue78eb3a52014-01-17 09:50:03 +08001667 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1668 socket_state previous;
Allan Stephensb89741a2008-04-15 00:20:37 -07001669 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670
Allan Stephens0c3141e2008-04-15 00:22:02 -07001671 lock_sock(sk);
1672
Allan Stephensb89741a2008-04-15 00:20:37 -07001673 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001674 if (sock->state == SS_READY) {
1675 res = -EOPNOTSUPP;
1676 goto exit;
1677 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678
Allan Stephensb89741a2008-04-15 00:20:37 -07001679 /*
1680 * Reject connection attempt using multicast address
1681 *
1682 * Note: send_msg() validates the rest of the address fields,
1683 * so there's no need to do it here
1684 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001685 if (dst->addrtype == TIPC_ADDR_MCAST) {
1686 res = -EINVAL;
1687 goto exit;
1688 }
1689
Ying Xue78eb3a52014-01-17 09:50:03 +08001690 previous = sock->state;
Ying Xue584d24b2012-11-29 18:51:19 -05001691 switch (sock->state) {
1692 case SS_UNCONNECTED:
1693 /* Send a 'SYN-' to destination */
1694 m.msg_name = dest;
1695 m.msg_namelen = destlen;
1696
1697 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1698 * indicate send_msg() is never blocked.
1699 */
1700 if (!timeout)
1701 m.msg_flags = MSG_DONTWAIT;
1702
Ying Xue247f0f32014-02-18 16:06:46 +08001703 res = tipc_sendmsg(NULL, sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001704 if ((res < 0) && (res != -EWOULDBLOCK))
1705 goto exit;
1706
1707 /* Just entered SS_CONNECTING state; the only
1708 * difference is that return value in non-blocking
1709 * case is EINPROGRESS, rather than EALREADY.
1710 */
1711 res = -EINPROGRESS;
Ying Xue584d24b2012-11-29 18:51:19 -05001712 case SS_CONNECTING:
Ying Xue78eb3a52014-01-17 09:50:03 +08001713 if (previous == SS_CONNECTING)
1714 res = -EALREADY;
1715 if (!timeout)
1716 goto exit;
1717 timeout = msecs_to_jiffies(timeout);
1718 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1719 res = tipc_wait_for_connect(sock, &timeout);
Ying Xue584d24b2012-11-29 18:51:19 -05001720 break;
1721 case SS_CONNECTED:
1722 res = -EISCONN;
1723 break;
1724 default:
1725 res = -EINVAL;
Ying Xue78eb3a52014-01-17 09:50:03 +08001726 break;
Allan Stephensb89741a2008-04-15 00:20:37 -07001727 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001728exit:
1729 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001730 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001731}
1732
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001733/**
Ying Xue247f0f32014-02-18 16:06:46 +08001734 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001735 * @sock: socket structure
1736 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001737 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001738 * Returns 0 on success, errno otherwise
1739 */
Ying Xue247f0f32014-02-18 16:06:46 +08001740static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001742 struct sock *sk = sock->sk;
1743 int res;
1744
1745 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746
Ying Xue245f3d32011-07-06 06:01:13 -04001747 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001748 res = -EINVAL;
1749 else {
1750 sock->state = SS_LISTENING;
1751 res = 0;
1752 }
1753
1754 release_sock(sk);
1755 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001756}
1757
Ying Xue6398e232014-01-17 09:50:04 +08001758static int tipc_wait_for_accept(struct socket *sock, long timeo)
1759{
1760 struct sock *sk = sock->sk;
1761 DEFINE_WAIT(wait);
1762 int err;
1763
1764 /* True wake-one mechanism for incoming connections: only
1765 * one process gets woken up, not the 'whole herd'.
1766 * Since we do not 'race & poll' for established sockets
1767 * anymore, the common case will execute the loop only once.
1768 */
1769 for (;;) {
1770 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1771 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001772 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001773 release_sock(sk);
1774 timeo = schedule_timeout(timeo);
1775 lock_sock(sk);
1776 }
1777 err = 0;
1778 if (!skb_queue_empty(&sk->sk_receive_queue))
1779 break;
1780 err = -EINVAL;
1781 if (sock->state != SS_LISTENING)
1782 break;
1783 err = sock_intr_errno(timeo);
1784 if (signal_pending(current))
1785 break;
1786 err = -EAGAIN;
1787 if (!timeo)
1788 break;
1789 }
1790 finish_wait(sk_sleep(sk), &wait);
1791 return err;
1792}
1793
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001794/**
Ying Xue247f0f32014-02-18 16:06:46 +08001795 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01001796 * @sock: listening socket
1797 * @newsock: new socket that is to be connected
1798 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001799 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800 * Returns 0 on success, errno otherwise
1801 */
Ying Xue247f0f32014-02-18 16:06:46 +08001802static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001803{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001804 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001805 struct sk_buff *buf;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001806 struct tipc_port *new_port;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001807 struct tipc_msg *msg;
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001808 struct tipc_portid peer;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001809 u32 new_ref;
Ying Xue6398e232014-01-17 09:50:04 +08001810 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001811 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001812
Allan Stephens0c3141e2008-04-15 00:22:02 -07001813 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001814
Allan Stephens0c3141e2008-04-15 00:22:02 -07001815 if (sock->state != SS_LISTENING) {
1816 res = -EINVAL;
1817 goto exit;
1818 }
Ying Xue6398e232014-01-17 09:50:04 +08001819 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1820 res = tipc_wait_for_accept(sock, timeo);
1821 if (res)
1822 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001823
1824 buf = skb_peek(&sk->sk_receive_queue);
1825
Ying Xuec5fa7b32013-06-17 10:54:39 -04001826 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001827 if (res)
1828 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001829
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001830 new_sk = new_sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001831 new_port = &tipc_sk(new_sk)->port;
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001832 new_ref = new_port->ref;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001833 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001834
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001835 /* we lock on new_sk; but lockdep sees the lock on sk */
1836 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001837
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001838 /*
1839 * Reject any stray messages received by new socket
1840 * before the socket lock was taken (very, very unlikely)
1841 */
1842 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001843
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001844 /* Connect new socket to it's peer */
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001845 peer.ref = msg_origport(msg);
1846 peer.node = msg_orignode(msg);
1847 tipc_port_connect(new_ref, &peer);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001848 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001849
Jon Paul Maloy3b4f3022014-03-12 11:31:11 -04001850 tipc_port_set_importance(new_port, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001851 if (msg_named(msg)) {
Jon Paul Maloy8826cde2014-03-12 11:31:09 -04001852 new_port->conn_type = msg_nametype(msg);
1853 new_port->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001854 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001855
1856 /*
1857 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1858 * Respond to 'SYN+' by queuing it on new socket.
1859 */
1860 if (!msg_data_sz(msg)) {
1861 struct msghdr m = {NULL,};
1862
1863 advance_rx_queue(sk);
Ying Xue247f0f32014-02-18 16:06:46 +08001864 tipc_send_packet(NULL, new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001865 } else {
1866 __skb_dequeue(&sk->sk_receive_queue);
1867 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001868 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001869 }
1870 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001871exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001872 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001873 return res;
1874}
1875
1876/**
Ying Xue247f0f32014-02-18 16:06:46 +08001877 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001878 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001879 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001880 *
1881 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001882 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001883 * Returns 0 on success, errno otherwise
1884 */
Ying Xue247f0f32014-02-18 16:06:46 +08001885static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001887 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001888 struct tipc_sock *tsk = tipc_sk(sk);
1889 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001890 struct sk_buff *buf;
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001891 u32 peer;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001892 int res;
1893
Allan Stephense247a8f2008-03-06 15:05:38 -08001894 if (how != SHUT_RDWR)
1895 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001896
Allan Stephens0c3141e2008-04-15 00:22:02 -07001897 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001898
1899 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001900 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001901 case SS_CONNECTED:
1902
Per Lidenb97bf3f2006-01-02 19:04:38 +01001903restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001904 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001905 buf = __skb_dequeue(&sk->sk_receive_queue);
1906 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001907 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001908 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001909 goto restart;
1910 }
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001911 tipc_port_disconnect(port->ref);
Jon Paul Maloy8db1bae2014-06-25 20:41:35 -05001912 if (tipc_msg_reverse(buf, &peer, TIPC_CONN_SHUTDOWN))
1913 tipc_link_xmit2(buf, peer, 0);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001914 } else {
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001915 tipc_port_shutdown(port->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001916 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001917
1918 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001919
1920 /* fall through */
1921
1922 case SS_DISCONNECTING:
1923
Ying Xue75031152012-10-29 09:38:15 -04001924 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001925 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001926
1927 /* Wake up anyone sleeping in poll */
1928 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001929 res = 0;
1930 break;
1931
1932 default:
1933 res = -ENOTCONN;
1934 }
1935
Allan Stephens0c3141e2008-04-15 00:22:02 -07001936 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001937 return res;
1938}
1939
1940/**
Ying Xue247f0f32014-02-18 16:06:46 +08001941 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01001942 * @sock: socket structure
1943 * @lvl: option level
1944 * @opt: option identifier
1945 * @ov: pointer to new option value
1946 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001947 *
1948 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001949 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001950 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001951 * Returns 0 on success, errno otherwise
1952 */
Ying Xue247f0f32014-02-18 16:06:46 +08001953static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1954 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001955{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001956 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001957 struct tipc_sock *tsk = tipc_sk(sk);
1958 struct tipc_port *port = &tsk->port;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001959 u32 value;
1960 int res;
1961
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001962 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1963 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001964 if (lvl != SOL_TIPC)
1965 return -ENOPROTOOPT;
1966 if (ol < sizeof(value))
1967 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001968 res = get_user(value, (u32 __user *)ov);
1969 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001970 return res;
1971
Allan Stephens0c3141e2008-04-15 00:22:02 -07001972 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001973
Per Lidenb97bf3f2006-01-02 19:04:38 +01001974 switch (opt) {
1975 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001976 tipc_port_set_importance(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001977 break;
1978 case TIPC_SRC_DROPPABLE:
1979 if (sock->type != SOCK_STREAM)
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001980 tipc_port_set_unreliable(port, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001981 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001982 res = -ENOPROTOOPT;
1983 break;
1984 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001985 tipc_port_set_unreturnable(port, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001986 break;
1987 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001988 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001989 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001990 break;
1991 default:
1992 res = -EINVAL;
1993 }
1994
Allan Stephens0c3141e2008-04-15 00:22:02 -07001995 release_sock(sk);
1996
Per Lidenb97bf3f2006-01-02 19:04:38 +01001997 return res;
1998}
1999
2000/**
Ying Xue247f0f32014-02-18 16:06:46 +08002001 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002002 * @sock: socket structure
2003 * @lvl: option level
2004 * @opt: option identifier
2005 * @ov: receptacle for option value
2006 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002007 *
2008 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002009 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002010 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002011 * Returns 0 on success, errno otherwise
2012 */
Ying Xue247f0f32014-02-18 16:06:46 +08002013static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2014 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002015{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002016 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002017 struct tipc_sock *tsk = tipc_sk(sk);
2018 struct tipc_port *port = &tsk->port;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002019 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002020 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002021 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002022
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002023 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2024 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002025 if (lvl != SOL_TIPC)
2026 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002027 res = get_user(len, ol);
2028 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002029 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002030
Allan Stephens0c3141e2008-04-15 00:22:02 -07002031 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002032
2033 switch (opt) {
2034 case TIPC_IMPORTANCE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002035 value = tipc_port_importance(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002036 break;
2037 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002038 value = tipc_port_unreliable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002039 break;
2040 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002041 value = tipc_port_unreturnable(port);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002042 break;
2043 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002044 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002045 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002046 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002047 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002048 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002049 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002050 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002051 value = skb_queue_len(&sk->sk_receive_queue);
2052 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002053 default:
2054 res = -EINVAL;
2055 }
2056
Allan Stephens0c3141e2008-04-15 00:22:02 -07002057 release_sock(sk);
2058
Paul Gortmaker25860c32010-12-31 18:59:31 +00002059 if (res)
2060 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002061
Paul Gortmaker25860c32010-12-31 18:59:31 +00002062 if (len < sizeof(value))
2063 return -EINVAL;
2064
2065 if (copy_to_user(ov, &value, sizeof(value)))
2066 return -EFAULT;
2067
2068 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002069}
2070
Erik Hugne78acb1f2014-04-24 16:26:47 +02002071int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
2072{
2073 struct tipc_sioc_ln_req lnr;
2074 void __user *argp = (void __user *)arg;
2075
2076 switch (cmd) {
2077 case SIOCGETLINKNAME:
2078 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2079 return -EFAULT;
2080 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
2081 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2082 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2083 return -EFAULT;
2084 return 0;
2085 }
2086 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002087 default:
2088 return -ENOIOCTLCMD;
2089 }
2090}
2091
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002092/* Protocol switches for the various types of TIPC sockets */
2093
Florian Westphalbca65ea2008-02-07 18:18:01 -08002094static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002095 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002096 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002097 .release = tipc_release,
2098 .bind = tipc_bind,
2099 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002100 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002101 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002102 .getname = tipc_getname,
2103 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002104 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002105 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002106 .shutdown = tipc_shutdown,
2107 .setsockopt = tipc_setsockopt,
2108 .getsockopt = tipc_getsockopt,
2109 .sendmsg = tipc_sendmsg,
2110 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002111 .mmap = sock_no_mmap,
2112 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002113};
2114
Florian Westphalbca65ea2008-02-07 18:18:01 -08002115static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002116 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002117 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002118 .release = tipc_release,
2119 .bind = tipc_bind,
2120 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002121 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002122 .accept = tipc_accept,
2123 .getname = tipc_getname,
2124 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002125 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002126 .listen = tipc_listen,
2127 .shutdown = tipc_shutdown,
2128 .setsockopt = tipc_setsockopt,
2129 .getsockopt = tipc_getsockopt,
2130 .sendmsg = tipc_send_packet,
2131 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002132 .mmap = sock_no_mmap,
2133 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002134};
2135
Florian Westphalbca65ea2008-02-07 18:18:01 -08002136static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002137 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002138 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002139 .release = tipc_release,
2140 .bind = tipc_bind,
2141 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002142 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002143 .accept = tipc_accept,
2144 .getname = tipc_getname,
2145 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002146 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002147 .listen = tipc_listen,
2148 .shutdown = tipc_shutdown,
2149 .setsockopt = tipc_setsockopt,
2150 .getsockopt = tipc_getsockopt,
2151 .sendmsg = tipc_send_stream,
2152 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002153 .mmap = sock_no_mmap,
2154 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002155};
2156
Florian Westphalbca65ea2008-02-07 18:18:01 -08002157static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002158 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002159 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002160 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002161};
2162
2163static struct proto tipc_proto = {
2164 .name = "TIPC",
2165 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002166 .obj_size = sizeof(struct tipc_sock),
2167 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002168};
2169
Ying Xuec5fa7b32013-06-17 10:54:39 -04002170static struct proto tipc_proto_kern = {
2171 .name = "TIPC",
2172 .obj_size = sizeof(struct tipc_sock),
2173 .sysctl_rmem = sysctl_tipc_rmem
2174};
2175
Per Lidenb97bf3f2006-01-02 19:04:38 +01002176/**
Per Liden4323add2006-01-18 00:38:21 +01002177 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002178 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002179 * Returns 0 on success, errno otherwise
2180 */
Per Liden4323add2006-01-18 00:38:21 +01002181int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002182{
2183 int res;
2184
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002185 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002186 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002187 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002188 goto out;
2189 }
2190
2191 res = sock_register(&tipc_family_ops);
2192 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002193 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002194 proto_unregister(&tipc_proto);
2195 goto out;
2196 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002197 out:
2198 return res;
2199}
2200
2201/**
Per Liden4323add2006-01-18 00:38:21 +01002202 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002203 */
Per Liden4323add2006-01-18 00:38:21 +01002204void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002205{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002206 sock_unregister(tipc_family_ops.family);
2207 proto_unregister(&tipc_proto);
2208}