blob: 5efdeef06f9d51453652e77f46f91867265d943c [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Maloye643df12012-11-27 06:15:29 -05004 * Copyright (c) 2001-2007, 2012 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"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039
Erik Hugne2cf8aa12012-06-29 00:16:37 -040040#include <linux/export.h>
41#include <net/sock.h>
42
Per Lidenb97bf3f2006-01-02 19:04:38 +010043#define SS_LISTENING -1 /* socket is listening */
44#define SS_READY -2 /* socket is connectionless */
45
Allan Stephens3654ea02008-04-13 21:35:11 -070046#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010047
48struct tipc_sock {
49 struct sock sk;
50 struct tipc_port *p;
Allan Stephens2da59912008-07-14 22:43:32 -070051 struct tipc_portid peer_name;
Allan Stephensa0f40f02011-05-26 13:44:34 -040052 unsigned int conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +010053};
54
Allan Stephens0c3141e2008-04-15 00:22:02 -070055#define tipc_sk(sk) ((struct tipc_sock *)(sk))
Joe Perchese3192692012-06-03 17:41:40 +000056#define tipc_sk_port(sk) (tipc_sk(sk)->p)
Per Lidenb97bf3f2006-01-02 19:04:38 +010057
Allan Stephens71092ea2011-02-23 14:52:14 -050058#define tipc_rx_ready(sock) (!skb_queue_empty(&sock->sk->sk_receive_queue) || \
59 (sock->state == SS_DISCONNECTING))
60
Allan Stephens0c3141e2008-04-15 00:22:02 -070061static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +010062static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
63static void wakeupdispatch(struct tipc_port *tport);
Ying Xuef288bef2012-08-21 11:16:57 +080064static void tipc_data_ready(struct sock *sk, int len);
65static void tipc_write_space(struct sock *sk);
Ying Xuec5fa7b32013-06-17 10:54:39 -040066static int release(struct socket *sock);
67static int accept(struct socket *sock, struct socket *new_sock, int flags);
Per Lidenb97bf3f2006-01-02 19:04:38 +010068
Florian Westphalbca65ea2008-02-07 18:18:01 -080069static const struct proto_ops packet_ops;
70static const struct proto_ops stream_ops;
71static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010072
73static struct proto tipc_proto;
Ying Xuec5fa7b32013-06-17 10:54:39 -040074static struct proto tipc_proto_kern;
Per Lidenb97bf3f2006-01-02 19:04:38 +010075
Allan Stephense3ec9c72010-12-31 18:59:34 +000076static int sockets_enabled;
Per Lidenb97bf3f2006-01-02 19:04:38 +010077
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090078/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070079 * Revised TIPC socket locking policy:
80 *
81 * Most socket operations take the standard socket lock when they start
82 * and hold it until they finish (or until they need to sleep). Acquiring
83 * this lock grants the owner exclusive access to the fields of the socket
84 * data structures, with the exception of the backlog queue. A few socket
85 * operations can be done without taking the socket lock because they only
86 * read socket information that never changes during the life of the socket.
87 *
88 * Socket operations may acquire the lock for the associated TIPC port if they
89 * need to perform an operation on the port. If any routine needs to acquire
90 * both the socket lock and the port lock it must take the socket lock first
91 * to avoid the risk of deadlock.
92 *
93 * The dispatcher handling incoming messages cannot grab the socket lock in
94 * the standard fashion, since invoked it runs at the BH level and cannot block.
95 * Instead, it checks to see if the socket lock is currently owned by someone,
96 * and either handles the message itself or adds it to the socket's backlog
97 * queue; in the latter case the queued message is processed once the process
98 * owning the socket lock releases it.
99 *
100 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
101 * the problem of a blocked socket operation preventing any other operations
102 * from occurring. However, applications must be careful if they have
103 * multiple threads trying to send (or receive) on the same socket, as these
104 * operations might interfere with each other. For example, doing a connect
105 * and a receive at the same time might allow the receive to consume the
106 * ACK message meant for the connect. While additional work could be done
107 * to try and overcome this, it doesn't seem to be worthwhile at the present.
108 *
109 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
110 * that another operation that must be performed in a non-blocking manner is
111 * not delayed for very long because the lock has already been taken.
112 *
113 * NOTE: This code assumes that certain fields of a port/socket pair are
114 * constant over its lifetime; such fields can be examined without taking
115 * the socket lock and/or port lock, and do not need to be re-read even
116 * after resuming processing after waiting. These fields include:
117 * - socket type
118 * - pointer to socket sk structure (aka tipc_sock structure)
119 * - pointer to port structure
120 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122
123/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700124 * advance_rx_queue - discard first buffer in socket receive queue
125 *
126 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700128static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400130 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131}
132
133/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700134 * reject_rx_queue - reject all buffers in socket receive queue
135 *
136 * Caller must hold socket lock
137 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700138static void reject_rx_queue(struct sock *sk)
139{
140 struct sk_buff *buf;
141
Ying Xue9da3d472012-11-27 06:15:27 -0500142 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700143 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700144}
145
146/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400147 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700148 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100149 * @sock: pre-allocated socket structure
150 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800151 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900152 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700153 * This routine creates additional data structures used by the TIPC socket,
154 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155 *
156 * Returns 0 on success, errno otherwise
157 */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400158static int tipc_sk_create(struct net *net, struct socket *sock, int protocol,
159 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700161 const struct proto_ops *ops;
162 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700164 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700165
166 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 if (unlikely(protocol != 0))
168 return -EPROTONOSUPPORT;
169
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 switch (sock->type) {
171 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700172 ops = &stream_ops;
173 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 break;
175 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700176 ops = &packet_ops;
177 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 break;
179 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700181 ops = &msg_ops;
182 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 break;
Allan Stephens49978652006-06-25 23:47:18 -0700184 default:
Allan Stephens49978652006-06-25 23:47:18 -0700185 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100186 }
187
Allan Stephens0c3141e2008-04-15 00:22:02 -0700188 /* Allocate socket's protocol area */
Ying Xuec5fa7b32013-06-17 10:54:39 -0400189 if (!kern)
190 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
191 else
192 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
193
Allan Stephens0c3141e2008-04-15 00:22:02 -0700194 if (sk == NULL)
195 return -ENOMEM;
196
197 /* Allocate TIPC port for socket to use */
Ying Xue3c5db8e2013-06-17 10:54:44 -0400198 tp_ptr = tipc_createport(sk, &dispatch, &wakeupdispatch,
199 TIPC_LOW_IMPORTANCE);
Allan Stephens0ea52242008-07-14 22:42:19 -0700200 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700201 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202 return -ENOMEM;
203 }
204
Allan Stephens0c3141e2008-04-15 00:22:02 -0700205 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700206 sock->ops = ops;
207 sock->state = state;
208
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700210 sk->sk_backlog_rcv = backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400211 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800212 sk->sk_data_ready = tipc_data_ready;
213 sk->sk_write_space = tipc_write_space;
Allan Stephens0ea52242008-07-14 22:42:19 -0700214 tipc_sk(sk)->p = tp_ptr;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400215 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700217 spin_unlock_bh(tp_ptr->lock);
218
Allan Stephens0c3141e2008-04-15 00:22:02 -0700219 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700220 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700221 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700222 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700223 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224
Per Lidenb97bf3f2006-01-02 19:04:38 +0100225 return 0;
226}
227
228/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400229 * tipc_sock_create_local - create TIPC socket from inside TIPC module
230 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
231 *
232 * We cannot use sock_creat_kern here because it bumps module user count.
233 * Since socket owner and creator is the same module we must make sure
234 * that module count remains zero for module local sockets, otherwise
235 * we cannot do rmmod.
236 *
237 * Returns 0 on success, errno otherwise
238 */
239int tipc_sock_create_local(int type, struct socket **res)
240{
241 int rc;
Ying Xuec5fa7b32013-06-17 10:54:39 -0400242
243 rc = sock_create_lite(AF_TIPC, type, 0, res);
244 if (rc < 0) {
245 pr_err("Failed to create kernel socket\n");
246 return rc;
247 }
248 tipc_sk_create(&init_net, *res, 0, 1);
249
Ying Xuec5fa7b32013-06-17 10:54:39 -0400250 return 0;
251}
252
253/**
254 * tipc_sock_release_local - release socket created by tipc_sock_create_local
255 * @sock: the socket to be released.
256 *
257 * Module reference count is not incremented when such sockets are created,
258 * so we must keep it from being decremented when they are released.
259 */
260void tipc_sock_release_local(struct socket *sock)
261{
262 release(sock);
263 sock->ops = NULL;
264 sock_release(sock);
265}
266
267/**
268 * tipc_sock_accept_local - accept a connection on a socket created
269 * with tipc_sock_create_local. Use this function to avoid that
270 * module reference count is inadvertently incremented.
271 *
272 * @sock: the accepting socket
273 * @newsock: reference to the new socket to be created
274 * @flags: socket flags
275 */
276
277int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400278 int flags)
Ying Xuec5fa7b32013-06-17 10:54:39 -0400279{
280 struct sock *sk = sock->sk;
281 int ret;
282
283 ret = sock_create_lite(sk->sk_family, sk->sk_type,
284 sk->sk_protocol, newsock);
285 if (ret < 0)
286 return ret;
287
288 ret = accept(sock, *newsock, flags);
289 if (ret < 0) {
290 sock_release(*newsock);
291 return ret;
292 }
293 (*newsock)->ops = sock->ops;
294 return ret;
295}
296
297/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298 * release - destroy a TIPC socket
299 * @sock: socket to destroy
300 *
301 * This routine cleans up any messages that are still queued on the socket.
302 * For DGRAM and RDM socket types, all queued messages are rejected.
303 * For SEQPACKET and STREAM socket types, the first message is rejected
304 * and any others are discarded. (If the first message on a STREAM socket
305 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900306 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 * NOTE: Rejected messages are not necessarily returned to the sender! They
308 * are returned or discarded according to the "destination droppable" setting
309 * specified for the message by the sender.
310 *
311 * Returns 0 on success, errno otherwise
312 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313static int release(struct socket *sock)
314{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700316 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700318 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100319
Allan Stephens0c3141e2008-04-15 00:22:02 -0700320 /*
321 * Exit if socket isn't fully initialized (occurs when a failed accept()
322 * releases a pre-allocated child socket that was never used)
323 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700324 if (sk == NULL)
325 return 0;
326
327 tport = tipc_sk_port(sk);
328 lock_sock(sk);
329
330 /*
331 * Reject all unreceived messages, except on an active connection
332 * (which disconnects locally & sends a 'FIN+' to peer)
333 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700335 buf = __skb_dequeue(&sk->sk_receive_queue);
336 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 break;
Ying Xue40682432013-10-18 07:23:16 +0200338 if (TIPC_SKB_CB(buf)->handle != NULL)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400339 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700340 else {
341 if ((sock->state == SS_CONNECTING) ||
342 (sock->state == SS_CONNECTED)) {
343 sock->state = SS_DISCONNECTING;
344 tipc_disconnect(tport->ref);
345 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700347 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348 }
349
Allan Stephens0c3141e2008-04-15 00:22:02 -0700350 /*
351 * Delete TIPC port; this ensures no more messages are queued
352 * (also disconnects an active connection & sends a 'FIN-' to peer)
353 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700354 res = tipc_deleteport(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355
Allan Stephens0c3141e2008-04-15 00:22:02 -0700356 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100357 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358
Allan Stephens0c3141e2008-04-15 00:22:02 -0700359 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700360 sock->state = SS_DISCONNECTING;
361 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362
363 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700364 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 return res;
367}
368
369/**
370 * bind - associate or disassocate TIPC name(s) with a socket
371 * @sock: socket structure
372 * @uaddr: socket address describing name(s) and desired operation
373 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900374 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100375 * Name and name sequence binding is indicated using a positive scope value;
376 * a negative scope value unbinds the specified name. Specifying no name
377 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900378 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700380 *
381 * NOTE: This routine doesn't need to take the socket lock since it doesn't
382 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
385{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700387 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388
Allan Stephens0c3141e2008-04-15 00:22:02 -0700389 if (unlikely(!uaddr_len))
390 return tipc_withdraw(portref, 0, NULL);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900391
Allan Stephens0c3141e2008-04-15 00:22:02 -0700392 if (uaddr_len < sizeof(struct sockaddr_tipc))
393 return -EINVAL;
394 if (addr->family != AF_TIPC)
395 return -EAFNOSUPPORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397 if (addr->addrtype == TIPC_ADDR_NAME)
398 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700399 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
400 return -EAFNOSUPPORT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900401
Ying Xue13a2e892013-06-17 10:54:40 -0400402 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400403 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
404 (addr->addr.nameseq.type != TIPC_CFG_SRV))
Allan Stephensc422f1b2011-11-02 15:49:40 -0400405 return -EACCES;
406
Allan Stephens0c3141e2008-04-15 00:22:02 -0700407 return (addr->scope > 0) ?
408 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
409 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410}
411
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900412/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 * get_name - get port ID of socket or peer socket
414 * @sock: socket structure
415 * @uaddr: area for returned socket address
416 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700417 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900418 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700420 *
Allan Stephens2da59912008-07-14 22:43:32 -0700421 * NOTE: This routine doesn't need to take the socket lock since it only
422 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000423 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900425static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 int *uaddr_len, int peer)
427{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700429 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000431 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700432 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700433 if ((sock->state != SS_CONNECTED) &&
434 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
435 return -ENOTCONN;
436 addr->addr.id.ref = tsock->peer_name.ref;
437 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700438 } else {
Allan Stephensb924dcf2010-11-30 12:01:03 +0000439 addr->addr.id.ref = tsock->p->ref;
440 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700441 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442
443 *uaddr_len = sizeof(*addr);
444 addr->addrtype = TIPC_ADDR_ID;
445 addr->family = AF_TIPC;
446 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447 addr->addr.name.domain = 0;
448
Allan Stephens0c3141e2008-04-15 00:22:02 -0700449 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450}
451
452/**
453 * poll - read and possibly block on pollmask
454 * @file: file structure associated with the socket
455 * @sock: socket for which to calculate the poll bits
456 * @wait: ???
457 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700458 * Returns pollmask value
459 *
460 * COMMENTARY:
461 * It appears that the usual socket locking mechanisms are not useful here
462 * since the pollmask info is potentially out-of-date the moment this routine
463 * exits. TCP and other protocols seem to rely on higher level poll routines
464 * to handle any preventable race conditions, so TIPC will do the same ...
465 *
466 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700467 *
Allan Stephensf662c072010-08-17 11:00:06 +0000468 * socket state flags set
469 * ------------ ---------
470 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200471 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000472 *
473 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
474 * no write flags
475 *
476 * connected POLLIN/POLLRDNORM if data in rx queue
477 * POLLOUT if port is not congested
478 *
479 * disconnecting POLLIN/POLLRDNORM/POLLHUP
480 * no write flags
481 *
482 * listening POLLIN if SYN in rx queue
483 * no write flags
484 *
485 * ready POLLIN/POLLRDNORM if data in rx queue
486 * [connectionless] POLLOUT (since port cannot be congested)
487 *
488 * IMPORTANT: The fact that a read or write operation is indicated does NOT
489 * imply that the operation will succeed, merely that it should be performed
490 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900492static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493 poll_table *wait)
494{
Allan Stephens9b674e82008-03-26 16:48:21 -0700495 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000496 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700497
Ying Xuef288bef2012-08-21 11:16:57 +0800498 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700499
Allan Stephensf662c072010-08-17 11:00:06 +0000500 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200501 case SS_UNCONNECTED:
502 if (!tipc_sk_port(sk)->congested)
503 mask |= POLLOUT;
504 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000505 case SS_READY:
506 case SS_CONNECTED:
507 if (!tipc_sk_port(sk)->congested)
508 mask |= POLLOUT;
509 /* fall thru' */
510 case SS_CONNECTING:
511 case SS_LISTENING:
512 if (!skb_queue_empty(&sk->sk_receive_queue))
513 mask |= (POLLIN | POLLRDNORM);
514 break;
515 case SS_DISCONNECTING:
516 mask = (POLLIN | POLLRDNORM | POLLHUP);
517 break;
518 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700519
520 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521}
522
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900523/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524 * dest_name_check - verify user is permitted to send to specified port name
525 * @dest: destination address
526 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900527 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528 * Prevents restricted configuration commands from being issued by
529 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900530 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531 * Returns 0 if permission is granted, otherwise errno
532 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800533static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534{
535 struct tipc_cfg_msg_hdr hdr;
536
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900537 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
538 return 0;
539 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
540 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900541 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
542 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543
Allan Stephens3f8dd942011-01-18 13:09:29 -0500544 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
545 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900546 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700548 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900550
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 return 0;
552}
553
554/**
555 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700556 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 * @sock: socket structure
558 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700559 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900560 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100561 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900562 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
564 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900565 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 * Returns the number of bytes sent on success, or errno otherwise
567 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568static int send_msg(struct kiocb *iocb, struct socket *sock,
569 struct msghdr *m, size_t total_len)
570{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700571 struct sock *sk = sock->sk;
572 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900573 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574 int needs_conn;
Ying Xue1d835872011-07-06 05:53:15 -0400575 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 int res = -EINVAL;
577
578 if (unlikely(!dest))
579 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700580 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
581 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 return -EINVAL;
Ying Xue97f8b872013-01-31 21:51:47 +0100583 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400584 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585
Allan Stephens0c3141e2008-04-15 00:22:02 -0700586 if (iocb)
587 lock_sock(sk);
588
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589 needs_conn = (sock->state != SS_READY);
590 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700591 if (sock->state == SS_LISTENING) {
592 res = -EPIPE;
593 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700594 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700595 if (sock->state != SS_UNCONNECTED) {
596 res = -EISCONN;
597 goto exit;
598 }
Erik Hugne5d21cb72013-06-17 10:54:38 -0400599 if (tport->published) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700600 res = -EOPNOTSUPP;
601 goto exit;
602 }
603 if (dest->addrtype == TIPC_ADDR_NAME) {
604 tport->conn_type = dest->addr.name.name.type;
605 tport->conn_instance = dest->addr.name.name.instance;
606 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607
608 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700609 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 }
611
Ying Xue1d835872011-07-06 05:53:15 -0400612 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
613
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900614 do {
615 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000616 res = dest_name_check(dest, m);
617 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700618 break;
619 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900620 &dest->addr.name.name,
621 dest->addr.name.domain,
Allan Stephens26896902011-04-21 10:42:07 -0500622 m->msg_iov,
623 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000624 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700625 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900626 &dest->addr.id,
Allan Stephens26896902011-04-21 10:42:07 -0500627 m->msg_iov,
628 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000629 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 if (needs_conn) {
631 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700632 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 }
Allan Stephens2db99832010-12-31 18:59:33 +0000634 res = dest_name_check(dest, m);
635 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700636 break;
637 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900638 &dest->addr.nameseq,
Allan Stephens26896902011-04-21 10:42:07 -0500639 m->msg_iov,
640 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900641 }
642 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000643 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700644 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700645 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900646 }
Ying Xue1d835872011-07-06 05:53:15 -0400647 if (timeout_val <= 0L) {
648 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700649 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700651 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400652 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
653 !tport->congested, timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700654 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900655 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700656
657exit:
658 if (iocb)
659 release_sock(sk);
660 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661}
662
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900663/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700665 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 * @sock: socket structure
667 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700668 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900669 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900671 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 * Returns the number of bytes sent on success, or errno otherwise
673 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674static int send_packet(struct kiocb *iocb, struct socket *sock,
675 struct msghdr *m, size_t total_len)
676{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700677 struct sock *sk = sock->sk;
678 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900679 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Ying Xue1d835872011-07-06 05:53:15 -0400680 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681 int res;
682
683 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100684 if (unlikely(dest))
685 return send_msg(iocb, sock, m, total_len);
686
Ying Xue97f8b872013-01-31 21:51:47 +0100687 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400688 return -EMSGSIZE;
689
Allan Stephens0c3141e2008-04-15 00:22:02 -0700690 if (iocb)
691 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100692
Ying Xue1d835872011-07-06 05:53:15 -0400693 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
694
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900695 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700696 if (unlikely(sock->state != SS_CONNECTED)) {
697 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900698 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700699 else
700 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700701 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700702 }
703
Ying Xue9446b872013-10-18 07:23:15 +0200704 res = tipc_send(tport->ref, m->msg_iov, total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000705 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700706 break;
Ying Xue1d835872011-07-06 05:53:15 -0400707 if (timeout_val <= 0L) {
708 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700709 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700711 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400712 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
713 (!tport->congested || !tport->connected), timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700714 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900715 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700716
717 if (iocb)
718 release_sock(sk);
719 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100720}
721
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900722/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100723 * send_stream - send stream-oriented data
724 * @iocb: (unused)
725 * @sock: socket structure
726 * @m: data to send
727 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900728 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900730 *
731 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700732 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734static int send_stream(struct kiocb *iocb, struct socket *sock,
735 struct msghdr *m, size_t total_len)
736{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700737 struct sock *sk = sock->sk;
738 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100739 struct msghdr my_msg;
740 struct iovec my_iov;
741 struct iovec *curr_iov;
742 int curr_iovlen;
743 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700744 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 int curr_left;
746 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700747 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900749
Allan Stephens0c3141e2008-04-15 00:22:02 -0700750 lock_sock(sk);
751
Allan Stephens05646c92007-06-10 17:25:24 -0700752 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900753 if (unlikely(sock->state != SS_CONNECTED)) {
wangweidong3b8401f2013-12-12 09:36:40 +0800754 if (sock->state == SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700755 res = send_packet(NULL, sock, m, total_len);
wangweidongb0555972013-12-27 10:09:39 +0800756 else
757 res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
wangweidong3b8401f2013-12-12 09:36:40 +0800758 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900759 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760
Allan Stephens0c3141e2008-04-15 00:22:02 -0700761 if (unlikely(m->msg_name)) {
762 res = -EISCONN;
763 goto exit;
764 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700765
Ying Xue97f8b872013-01-31 21:51:47 +0100766 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400767 res = -EMSGSIZE;
768 goto exit;
769 }
770
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900771 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100772 * Send each iovec entry using one or more messages
773 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900774 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775 * (i.e. one large iovec entry), but could be improved to pass sets
776 * of small iovec entries into send_packet().
777 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700778 curr_iov = m->msg_iov;
779 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780 my_msg.msg_iov = &my_iov;
781 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700782 my_msg.msg_flags = m->msg_flags;
783 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700784 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785
Allan Stephens05646c92007-06-10 17:25:24 -0700786 hdr_size = msg_hdr_sz(&tport->phdr);
787
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788 while (curr_iovlen--) {
789 curr_start = curr_iov->iov_base;
790 curr_left = curr_iov->iov_len;
791
792 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700793 bytes_to_send = tport->max_pkt - hdr_size;
794 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
795 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
796 if (curr_left < bytes_to_send)
797 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798 my_iov.iov_base = curr_start;
799 my_iov.iov_len = bytes_to_send;
Allan Stephens26896902011-04-21 10:42:07 -0500800 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000801 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700802 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700803 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700804 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700805 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100806 curr_left -= bytes_to_send;
807 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700808 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100809 }
810
811 curr_iov++;
812 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700813 res = bytes_sent;
814exit:
815 release_sock(sk);
816 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100817}
818
819/**
820 * auto_connect - complete connection setup to a remote port
821 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900823 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824 * Returns 0 on success, errno otherwise
825 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700826static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100827{
Allan Stephens2da59912008-07-14 22:43:32 -0700828 struct tipc_sock *tsock = tipc_sk(sock->sk);
Ying Xue584d24b2012-11-29 18:51:19 -0500829 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830
Allan Stephens2da59912008-07-14 22:43:32 -0700831 tsock->peer_name.ref = msg_origport(msg);
832 tsock->peer_name.node = msg_orignode(msg);
Ying Xue584d24b2012-11-29 18:51:19 -0500833 p_ptr = tipc_port_deref(tsock->p->ref);
834 if (!p_ptr)
835 return -EINVAL;
836
837 __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
838
839 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
840 return -EINVAL;
841 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100842 sock->state = SS_CONNECTED;
843 return 0;
844}
845
846/**
847 * set_orig_addr - capture sender's address for received message
848 * @m: descriptor for message info
849 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900850 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 * Note: Address is not captured if not requested by receiver.
852 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800853static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900855 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100856
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900857 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100858 addr->family = AF_TIPC;
859 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000860 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861 addr->addr.id.ref = msg_origport(msg);
862 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000863 addr->addr.name.domain = 0; /* could leave uninitialized */
864 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865 m->msg_namelen = sizeof(struct sockaddr_tipc);
866 }
867}
868
869/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900870 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871 * @m: descriptor for message info
872 * @msg: received message header
873 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900874 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100875 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900876 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100877 * Returns 0 if successful, otherwise errno
878 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800879static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400880 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881{
882 u32 anc_data[3];
883 u32 err;
884 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700885 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886 int res;
887
888 if (likely(m->msg_controllen == 0))
889 return 0;
890
891 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892 err = msg ? msg_errcode(msg) : 0;
893 if (unlikely(err)) {
894 anc_data[0] = err;
895 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000896 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
897 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000899 if (anc_data[1]) {
900 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
901 msg_data(msg));
902 if (res)
903 return res;
904 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905 }
906
907 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
909 switch (dest_type) {
910 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700911 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912 anc_data[0] = msg_nametype(msg);
913 anc_data[1] = msg_namelower(msg);
914 anc_data[2] = msg_namelower(msg);
915 break;
916 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700917 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100918 anc_data[0] = msg_nametype(msg);
919 anc_data[1] = msg_namelower(msg);
920 anc_data[2] = msg_nameupper(msg);
921 break;
922 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700923 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924 anc_data[0] = tport->conn_type;
925 anc_data[1] = tport->conn_instance;
926 anc_data[2] = tport->conn_instance;
927 break;
928 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700929 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100930 }
Allan Stephens2db99832010-12-31 18:59:33 +0000931 if (has_name) {
932 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
933 if (res)
934 return res;
935 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936
937 return 0;
938}
939
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900940/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941 * recv_msg - receive packet-oriented message
942 * @iocb: (unused)
943 * @m: descriptor for message info
944 * @buf_len: total size of user buffer area
945 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900946 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
948 * If the complete message doesn't fit in user area, truncate it.
949 *
950 * Returns size of returned message data, errno otherwise
951 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100952static int recv_msg(struct kiocb *iocb, struct socket *sock,
953 struct msghdr *m, size_t buf_len, int flags)
954{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700955 struct sock *sk = sock->sk;
956 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100957 struct sk_buff *buf;
958 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500959 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100960 unsigned int sz;
961 u32 err;
962 int res;
963
Allan Stephens0c3141e2008-04-15 00:22:02 -0700964 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100965 if (unlikely(!buf_len))
966 return -EINVAL;
967
Allan Stephens0c3141e2008-04-15 00:22:02 -0700968 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969
Allan Stephens0c3141e2008-04-15 00:22:02 -0700970 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100971 res = -ENOTCONN;
972 goto exit;
973 }
974
Allan Stephens71092ea2011-02-23 14:52:14 -0500975 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700976restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100977
Allan Stephens0c3141e2008-04-15 00:22:02 -0700978 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700979 while (skb_queue_empty(&sk->sk_receive_queue)) {
980 if (sock->state == SS_DISCONNECTING) {
981 res = -ENOTCONN;
982 goto exit;
983 }
Allan Stephens71092ea2011-02-23 14:52:14 -0500984 if (timeout <= 0L) {
985 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700986 goto exit;
987 }
988 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -0500989 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
990 tipc_rx_ready(sock),
991 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700992 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700993 }
994
995 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700996 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100997 msg = buf_msg(buf);
998 sz = msg_data_sz(msg);
999 err = msg_errcode(msg);
1000
Per Lidenb97bf3f2006-01-02 19:04:38 +01001001 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001003 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001004 goto restart;
1005 }
1006
1007 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008 set_orig_addr(m, msg);
1009
1010 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001011 res = anc_data_recv(m, msg, tport);
1012 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 goto exit;
1014
1015 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016 if (!err) {
1017 if (unlikely(buf_len < sz)) {
1018 sz = buf_len;
1019 m->msg_flags |= MSG_TRUNC;
1020 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001021 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1022 m->msg_iov, sz);
1023 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 res = sz;
1026 } else {
1027 if ((sock->state == SS_READY) ||
1028 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1029 res = 0;
1030 else
1031 res = -ECONNRESET;
1032 }
1033
1034 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001036 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -07001037 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1038 tipc_acknowledge(tport->ref, tport->conn_unacked);
1039 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001040 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001042 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001043 return res;
1044}
1045
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001046/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001047 * recv_stream - receive stream-oriented data
1048 * @iocb: (unused)
1049 * @m: descriptor for message info
1050 * @buf_len: total size of user buffer area
1051 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001052 *
1053 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054 * will optionally wait for more; never truncates data.
1055 *
1056 * Returns size of returned message data, errno otherwise
1057 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001058static int recv_stream(struct kiocb *iocb, struct socket *sock,
1059 struct msghdr *m, size_t buf_len, int flags)
1060{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001061 struct sock *sk = sock->sk;
1062 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001063 struct sk_buff *buf;
1064 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -05001065 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001067 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001070 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071
1072 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 if (unlikely(!buf_len))
1074 return -EINVAL;
1075
Allan Stephens0c3141e2008-04-15 00:22:02 -07001076 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077
Erik Hugne5d21cb72013-06-17 10:54:38 -04001078 if (unlikely((sock->state == SS_UNCONNECTED))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001079 res = -ENOTCONN;
1080 goto exit;
1081 }
1082
Florian Westphal3720d402010-08-17 11:00:04 +00001083 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Allan Stephens71092ea2011-02-23 14:52:14 -05001084 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001085
Allan Stephens0c3141e2008-04-15 00:22:02 -07001086restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001087 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001088 while (skb_queue_empty(&sk->sk_receive_queue)) {
1089 if (sock->state == SS_DISCONNECTING) {
1090 res = -ENOTCONN;
1091 goto exit;
1092 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001093 if (timeout <= 0L) {
1094 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001095 goto exit;
1096 }
1097 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001098 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1099 tipc_rx_ready(sock),
1100 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001101 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001102 }
1103
1104 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001105 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001106 msg = buf_msg(buf);
1107 sz = msg_data_sz(msg);
1108 err = msg_errcode(msg);
1109
1110 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001111 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001112 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001113 goto restart;
1114 }
1115
1116 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117 if (sz_copied == 0) {
1118 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001119 res = anc_data_recv(m, msg, tport);
1120 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121 goto exit;
1122 }
1123
1124 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001125 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001126 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127
Allan Stephens0232fd02011-02-21 09:45:40 -05001128 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129 needed = (buf_len - sz_copied);
1130 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001131
1132 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1133 m->msg_iov, sz_to_copy);
1134 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001136
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 sz_copied += sz_to_copy;
1138
1139 if (sz_to_copy < sz) {
1140 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001141 TIPC_SKB_CB(buf)->handle =
1142 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143 goto exit;
1144 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 } else {
1146 if (sz_copied != 0)
1147 goto exit; /* can't add error msg to valid data */
1148
1149 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1150 res = 0;
1151 else
1152 res = -ECONNRESET;
1153 }
1154
1155 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001157 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1158 tipc_acknowledge(tport->ref, tport->conn_unacked);
1159 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001160 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161
1162 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001163 if ((sz_copied < buf_len) && /* didn't get all requested data */
1164 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001165 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001166 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1167 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 goto restart;
1169
1170exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001171 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001172 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173}
1174
1175/**
Ying Xuef288bef2012-08-21 11:16:57 +08001176 * tipc_write_space - wake up thread if port congestion is released
1177 * @sk: socket
1178 */
1179static void tipc_write_space(struct sock *sk)
1180{
1181 struct socket_wq *wq;
1182
1183 rcu_read_lock();
1184 wq = rcu_dereference(sk->sk_wq);
1185 if (wq_has_sleeper(wq))
1186 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1187 POLLWRNORM | POLLWRBAND);
1188 rcu_read_unlock();
1189}
1190
1191/**
1192 * tipc_data_ready - wake up threads to indicate messages have been received
1193 * @sk: socket
1194 * @len: the length of messages
1195 */
1196static void tipc_data_ready(struct sock *sk, int len)
1197{
1198 struct socket_wq *wq;
1199
1200 rcu_read_lock();
1201 wq = rcu_dereference(sk->sk_wq);
1202 if (wq_has_sleeper(wq))
1203 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1204 POLLRDNORM | POLLRDBAND);
1205 rcu_read_unlock();
1206}
1207
1208/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001209 * filter_connect - Handle all incoming messages for a connection-based socket
1210 * @tsock: TIPC socket
1211 * @msg: message
1212 *
1213 * Returns TIPC error status code and socket error status code
1214 * once it encounters some errors
1215 */
1216static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
1217{
1218 struct socket *sock = tsock->sk.sk_socket;
1219 struct tipc_msg *msg = buf_msg(*buf);
Ying Xue584d24b2012-11-29 18:51:19 -05001220 struct sock *sk = &tsock->sk;
Ying Xue7e6c1312012-11-29 18:39:14 -05001221 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001222 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001223
1224 if (msg_mcast(msg))
1225 return retval;
1226
1227 switch ((int)sock->state) {
1228 case SS_CONNECTED:
1229 /* Accept only connection-based messages sent by peer */
1230 if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
1231 if (unlikely(msg_errcode(msg))) {
1232 sock->state = SS_DISCONNECTING;
1233 __tipc_disconnect(tsock->p);
1234 }
1235 retval = TIPC_OK;
1236 }
1237 break;
1238 case SS_CONNECTING:
1239 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001240 if (unlikely(msg_errcode(msg))) {
1241 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001242 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001243 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001244 break;
1245 }
1246
1247 if (unlikely(!msg_connected(msg)))
1248 break;
1249
1250 res = auto_connect(sock, msg);
1251 if (res) {
1252 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001253 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001254 retval = TIPC_OK;
1255 break;
1256 }
1257
1258 /* If an incoming message is an 'ACK-', it should be
1259 * discarded here because it doesn't contain useful
1260 * data. In addition, we should try to wake up
1261 * connect() routine if sleeping.
1262 */
1263 if (msg_data_sz(msg) == 0) {
1264 kfree_skb(*buf);
1265 *buf = NULL;
1266 if (waitqueue_active(sk_sleep(sk)))
1267 wake_up_interruptible(sk_sleep(sk));
1268 }
1269 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001270 break;
1271 case SS_LISTENING:
1272 case SS_UNCONNECTED:
1273 /* Accept only SYN message */
1274 if (!msg_connected(msg) && !(msg_errcode(msg)))
1275 retval = TIPC_OK;
1276 break;
1277 case SS_DISCONNECTING:
1278 break;
1279 default:
1280 pr_err("Unknown socket state %u\n", sock->state);
1281 }
1282 return retval;
1283}
1284
1285/**
Ying Xueaba79f32013-01-20 23:30:09 +01001286 * rcvbuf_limit - get proper overload limit of socket receive queue
1287 * @sk: socket
1288 * @buf: message
1289 *
1290 * For all connection oriented messages, irrespective of importance,
1291 * the default overload value (i.e. 67MB) is set as limit.
1292 *
1293 * For all connectionless messages, by default new queue limits are
1294 * as belows:
1295 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001296 * TIPC_LOW_IMPORTANCE (4 MB)
1297 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1298 * TIPC_HIGH_IMPORTANCE (16 MB)
1299 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001300 *
1301 * Returns overload limit according to corresponding message importance
1302 */
1303static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1304{
1305 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001306
1307 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001308 return sysctl_tipc_rmem[2];
1309
1310 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1311 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001312}
1313
1314/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001315 * filter_rcv - validate incoming message
1316 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001317 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001318 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001319 * Enqueues message on receive queue if acceptable; optionally handles
1320 * disconnect indication for a connected socket.
1321 *
1322 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001323 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1325 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001326static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001327{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001328 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001329 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001330 unsigned int limit = rcvbuf_limit(sk, buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001331 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001334 if (msg_type(msg) > TIPC_DIRECT_MSG)
1335 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001336
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001338 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 } else {
Ying Xue7e6c1312012-11-29 18:39:14 -05001341 res = filter_connect(tipc_sk(sk), &buf);
1342 if (res != TIPC_OK || buf == NULL)
1343 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 }
1345
1346 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001347 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1348 return TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349
Ying Xueaba79f32013-01-20 23:30:09 +01001350 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001351 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001352 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001353 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001354
Ying Xuef288bef2012-08-21 11:16:57 +08001355 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356 return TIPC_OK;
1357}
1358
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001359/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001360 * backlog_rcv - handle incoming message from backlog queue
1361 * @sk: socket
1362 * @buf: message
1363 *
1364 * Caller must hold socket lock, but not port lock.
1365 *
1366 * Returns 0
1367 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001368static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1369{
1370 u32 res;
1371
1372 res = filter_rcv(sk, buf);
1373 if (res)
1374 tipc_reject_msg(buf, res);
1375 return 0;
1376}
1377
1378/**
1379 * dispatch - handle incoming message
1380 * @tport: TIPC port that received message
1381 * @buf: message
1382 *
1383 * Called with port lock already taken.
1384 *
1385 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1386 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001387static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1388{
Ying Xuec0fee8a2013-06-17 10:54:46 -04001389 struct sock *sk = tport->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001390 u32 res;
1391
1392 /*
1393 * Process message if socket is unlocked; otherwise add to backlog queue
1394 *
1395 * This code is based on sk_receive_skb(), but must be distinct from it
1396 * since a TIPC-specific filter/reject mechanism is utilized
1397 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001398 bh_lock_sock(sk);
1399 if (!sock_owned_by_user(sk)) {
1400 res = filter_rcv(sk, buf);
1401 } else {
Ying Xueaba79f32013-01-20 23:30:09 +01001402 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
Zhu Yi53eecb12010-03-04 18:01:45 +00001403 res = TIPC_ERR_OVERLOAD;
1404 else
1405 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001406 }
1407 bh_unlock_sock(sk);
1408
1409 return res;
1410}
1411
1412/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001413 * wakeupdispatch - wake up port after congestion
1414 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001415 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001416 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418static void wakeupdispatch(struct tipc_port *tport)
1419{
Ying Xuec0fee8a2013-06-17 10:54:46 -04001420 struct sock *sk = tport->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421
Ying Xuef288bef2012-08-21 11:16:57 +08001422 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001423}
1424
1425/**
1426 * connect - establish a connection to another TIPC port
1427 * @sock: socket structure
1428 * @dest: socket address for destination port
1429 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001430 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001431 *
1432 * Returns 0 on success, errno otherwise
1433 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001434static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001435 int flags)
1436{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001437 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001438 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1439 struct msghdr m = {NULL,};
Allan Stephensa0f40f02011-05-26 13:44:34 -04001440 unsigned int timeout;
Allan Stephensb89741a2008-04-15 00:20:37 -07001441 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001442
Allan Stephens0c3141e2008-04-15 00:22:02 -07001443 lock_sock(sk);
1444
Allan Stephensb89741a2008-04-15 00:20:37 -07001445 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001446 if (sock->state == SS_READY) {
1447 res = -EOPNOTSUPP;
1448 goto exit;
1449 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001450
Allan Stephensb89741a2008-04-15 00:20:37 -07001451 /*
1452 * Reject connection attempt using multicast address
1453 *
1454 * Note: send_msg() validates the rest of the address fields,
1455 * so there's no need to do it here
1456 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001457 if (dst->addrtype == TIPC_ADDR_MCAST) {
1458 res = -EINVAL;
1459 goto exit;
1460 }
1461
Ying Xue584d24b2012-11-29 18:51:19 -05001462 timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463
Ying Xue584d24b2012-11-29 18:51:19 -05001464 switch (sock->state) {
1465 case SS_UNCONNECTED:
1466 /* Send a 'SYN-' to destination */
1467 m.msg_name = dest;
1468 m.msg_namelen = destlen;
1469
1470 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1471 * indicate send_msg() is never blocked.
1472 */
1473 if (!timeout)
1474 m.msg_flags = MSG_DONTWAIT;
1475
1476 res = send_msg(NULL, sock, &m, 0);
1477 if ((res < 0) && (res != -EWOULDBLOCK))
1478 goto exit;
1479
1480 /* Just entered SS_CONNECTING state; the only
1481 * difference is that return value in non-blocking
1482 * case is EINPROGRESS, rather than EALREADY.
1483 */
1484 res = -EINPROGRESS;
1485 break;
1486 case SS_CONNECTING:
1487 res = -EALREADY;
1488 break;
1489 case SS_CONNECTED:
1490 res = -EISCONN;
1491 break;
1492 default:
1493 res = -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001494 goto exit;
Allan Stephensb89741a2008-04-15 00:20:37 -07001495 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001496
Ying Xue584d24b2012-11-29 18:51:19 -05001497 if (sock->state == SS_CONNECTING) {
1498 if (!timeout)
1499 goto exit;
1500
1501 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1502 release_sock(sk);
1503 res = wait_event_interruptible_timeout(*sk_sleep(sk),
1504 sock->state != SS_CONNECTING,
1505 timeout ? (long)msecs_to_jiffies(timeout)
1506 : MAX_SCHEDULE_TIMEOUT);
Ying Xue584d24b2012-11-29 18:51:19 -05001507 if (res <= 0) {
1508 if (res == 0)
1509 res = -ETIMEDOUT;
wangweidongd3fbccf2013-12-12 09:36:42 +08001510 return res;
Ying Xue584d24b2012-11-29 18:51:19 -05001511 }
wangweidongd3fbccf2013-12-12 09:36:42 +08001512 lock_sock(sk);
Ying Xue584d24b2012-11-29 18:51:19 -05001513 }
1514
1515 if (unlikely(sock->state == SS_DISCONNECTING))
1516 res = sock_error(sk);
1517 else
1518 res = 0;
1519
Allan Stephens0c3141e2008-04-15 00:22:02 -07001520exit:
1521 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001522 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001523}
1524
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001525/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001526 * listen - allow socket to listen for incoming connections
1527 * @sock: socket structure
1528 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001529 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001530 * Returns 0 on success, errno otherwise
1531 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001532static int listen(struct socket *sock, int len)
1533{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001534 struct sock *sk = sock->sk;
1535 int res;
1536
1537 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001538
Ying Xue245f3d32011-07-06 06:01:13 -04001539 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001540 res = -EINVAL;
1541 else {
1542 sock->state = SS_LISTENING;
1543 res = 0;
1544 }
1545
1546 release_sock(sk);
1547 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001548}
1549
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001550/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001551 * accept - wait for connection request
1552 * @sock: listening socket
1553 * @newsock: new socket that is to be connected
1554 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001555 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001556 * Returns 0 on success, errno otherwise
1557 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001558static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001559{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001560 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001561 struct sk_buff *buf;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001562 struct tipc_sock *new_tsock;
1563 struct tipc_port *new_tport;
1564 struct tipc_msg *msg;
1565 u32 new_ref;
1566
Allan Stephens0c3141e2008-04-15 00:22:02 -07001567 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001568
Allan Stephens0c3141e2008-04-15 00:22:02 -07001569 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001570
Allan Stephens0c3141e2008-04-15 00:22:02 -07001571 if (sock->state != SS_LISTENING) {
1572 res = -EINVAL;
1573 goto exit;
1574 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001575
Allan Stephens0c3141e2008-04-15 00:22:02 -07001576 while (skb_queue_empty(&sk->sk_receive_queue)) {
1577 if (flags & O_NONBLOCK) {
1578 res = -EWOULDBLOCK;
1579 goto exit;
1580 }
1581 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001582 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001583 (!skb_queue_empty(&sk->sk_receive_queue)));
1584 lock_sock(sk);
1585 if (res)
1586 goto exit;
1587 }
1588
1589 buf = skb_peek(&sk->sk_receive_queue);
1590
Ying Xuec5fa7b32013-06-17 10:54:39 -04001591 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001592 if (res)
1593 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001594
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001595 new_sk = new_sock->sk;
1596 new_tsock = tipc_sk(new_sk);
1597 new_tport = new_tsock->p;
1598 new_ref = new_tport->ref;
1599 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001600
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001601 /* we lock on new_sk; but lockdep sees the lock on sk */
1602 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001603
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001604 /*
1605 * Reject any stray messages received by new socket
1606 * before the socket lock was taken (very, very unlikely)
1607 */
1608 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001609
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001610 /* Connect new socket to it's peer */
1611 new_tsock->peer_name.ref = msg_origport(msg);
1612 new_tsock->peer_name.node = msg_orignode(msg);
1613 tipc_connect(new_ref, &new_tsock->peer_name);
1614 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001615
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001616 tipc_set_portimportance(new_ref, msg_importance(msg));
1617 if (msg_named(msg)) {
1618 new_tport->conn_type = msg_nametype(msg);
1619 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001620 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001621
1622 /*
1623 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1624 * Respond to 'SYN+' by queuing it on new socket.
1625 */
1626 if (!msg_data_sz(msg)) {
1627 struct msghdr m = {NULL,};
1628
1629 advance_rx_queue(sk);
1630 send_packet(NULL, new_sock, &m, 0);
1631 } else {
1632 __skb_dequeue(&sk->sk_receive_queue);
1633 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001634 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001635 }
1636 release_sock(new_sk);
1637
Per Lidenb97bf3f2006-01-02 19:04:38 +01001638exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001639 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001640 return res;
1641}
1642
1643/**
1644 * shutdown - shutdown socket connection
1645 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001646 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001647 *
1648 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001649 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001650 * Returns 0 on success, errno otherwise
1651 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652static int shutdown(struct socket *sock, int how)
1653{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001654 struct sock *sk = sock->sk;
1655 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656 struct sk_buff *buf;
1657 int res;
1658
Allan Stephense247a8f2008-03-06 15:05:38 -08001659 if (how != SHUT_RDWR)
1660 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661
Allan Stephens0c3141e2008-04-15 00:22:02 -07001662 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663
1664 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001665 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666 case SS_CONNECTED:
1667
Per Lidenb97bf3f2006-01-02 19:04:38 +01001668restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001669 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001670 buf = __skb_dequeue(&sk->sk_receive_queue);
1671 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001672 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001673 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674 goto restart;
1675 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001676 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001677 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001678 } else {
1679 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001680 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001681
1682 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683
1684 /* fall through */
1685
1686 case SS_DISCONNECTING:
1687
Ying Xue75031152012-10-29 09:38:15 -04001688 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001689 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001690
1691 /* Wake up anyone sleeping in poll */
1692 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001693 res = 0;
1694 break;
1695
1696 default:
1697 res = -ENOTCONN;
1698 }
1699
Allan Stephens0c3141e2008-04-15 00:22:02 -07001700 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701 return res;
1702}
1703
1704/**
1705 * setsockopt - set socket option
1706 * @sock: socket structure
1707 * @lvl: option level
1708 * @opt: option identifier
1709 * @ov: pointer to new option value
1710 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001711 *
1712 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001713 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001714 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001715 * Returns 0 on success, errno otherwise
1716 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001717static int setsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
1718 unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001720 struct sock *sk = sock->sk;
1721 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001722 u32 value;
1723 int res;
1724
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001725 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1726 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001727 if (lvl != SOL_TIPC)
1728 return -ENOPROTOOPT;
1729 if (ol < sizeof(value))
1730 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001731 res = get_user(value, (u32 __user *)ov);
1732 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001733 return res;
1734
Allan Stephens0c3141e2008-04-15 00:22:02 -07001735 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001736
Per Lidenb97bf3f2006-01-02 19:04:38 +01001737 switch (opt) {
1738 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001739 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001740 break;
1741 case TIPC_SRC_DROPPABLE:
1742 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001743 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001744 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001745 res = -ENOPROTOOPT;
1746 break;
1747 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001748 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749 break;
1750 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001751 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001752 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001753 break;
1754 default:
1755 res = -EINVAL;
1756 }
1757
Allan Stephens0c3141e2008-04-15 00:22:02 -07001758 release_sock(sk);
1759
Per Lidenb97bf3f2006-01-02 19:04:38 +01001760 return res;
1761}
1762
1763/**
1764 * getsockopt - get socket option
1765 * @sock: socket structure
1766 * @lvl: option level
1767 * @opt: option identifier
1768 * @ov: receptacle for option value
1769 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001770 *
1771 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001772 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001773 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774 * Returns 0 on success, errno otherwise
1775 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001776static int getsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
1777 int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001778{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001779 struct sock *sk = sock->sk;
1780 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001781 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001782 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001783 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001784
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001785 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1786 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787 if (lvl != SOL_TIPC)
1788 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001789 res = get_user(len, ol);
1790 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001791 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001792
Allan Stephens0c3141e2008-04-15 00:22:02 -07001793 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001794
1795 switch (opt) {
1796 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001797 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001798 break;
1799 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001800 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001801 break;
1802 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001803 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001804 break;
1805 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001806 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001807 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001808 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001809 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001810 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001811 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001812 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001813 value = skb_queue_len(&sk->sk_receive_queue);
1814 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001815 default:
1816 res = -EINVAL;
1817 }
1818
Allan Stephens0c3141e2008-04-15 00:22:02 -07001819 release_sock(sk);
1820
Paul Gortmaker25860c32010-12-31 18:59:31 +00001821 if (res)
1822 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001823
Paul Gortmaker25860c32010-12-31 18:59:31 +00001824 if (len < sizeof(value))
1825 return -EINVAL;
1826
1827 if (copy_to_user(ov, &value, sizeof(value)))
1828 return -EFAULT;
1829
1830 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001831}
1832
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001833/* Protocol switches for the various types of TIPC sockets */
1834
Florian Westphalbca65ea2008-02-07 18:18:01 -08001835static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001836 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001837 .family = AF_TIPC,
1838 .release = release,
1839 .bind = bind,
1840 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001841 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001842 .accept = sock_no_accept,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001843 .getname = get_name,
1844 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001845 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001846 .listen = sock_no_listen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001847 .shutdown = shutdown,
1848 .setsockopt = setsockopt,
1849 .getsockopt = getsockopt,
1850 .sendmsg = send_msg,
1851 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001852 .mmap = sock_no_mmap,
1853 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001854};
1855
Florian Westphalbca65ea2008-02-07 18:18:01 -08001856static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001857 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001858 .family = AF_TIPC,
1859 .release = release,
1860 .bind = bind,
1861 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001862 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001863 .accept = accept,
1864 .getname = get_name,
1865 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001866 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001867 .listen = listen,
1868 .shutdown = shutdown,
1869 .setsockopt = setsockopt,
1870 .getsockopt = getsockopt,
1871 .sendmsg = send_packet,
1872 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001873 .mmap = sock_no_mmap,
1874 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001875};
1876
Florian Westphalbca65ea2008-02-07 18:18:01 -08001877static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001878 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001879 .family = AF_TIPC,
1880 .release = release,
1881 .bind = bind,
1882 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001883 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001884 .accept = accept,
1885 .getname = get_name,
1886 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001887 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001888 .listen = listen,
1889 .shutdown = shutdown,
1890 .setsockopt = setsockopt,
1891 .getsockopt = getsockopt,
1892 .sendmsg = send_stream,
1893 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001894 .mmap = sock_no_mmap,
1895 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001896};
1897
Florian Westphalbca65ea2008-02-07 18:18:01 -08001898static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001899 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001900 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04001901 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01001902};
1903
1904static struct proto tipc_proto = {
1905 .name = "TIPC",
1906 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04001907 .obj_size = sizeof(struct tipc_sock),
1908 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01001909};
1910
Ying Xuec5fa7b32013-06-17 10:54:39 -04001911static struct proto tipc_proto_kern = {
1912 .name = "TIPC",
1913 .obj_size = sizeof(struct tipc_sock),
1914 .sysctl_rmem = sysctl_tipc_rmem
1915};
1916
Per Lidenb97bf3f2006-01-02 19:04:38 +01001917/**
Per Liden4323add2006-01-18 00:38:21 +01001918 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001919 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001920 * Returns 0 on success, errno otherwise
1921 */
Per Liden4323add2006-01-18 00:38:21 +01001922int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001923{
1924 int res;
1925
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001926 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001927 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001928 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001929 goto out;
1930 }
1931
1932 res = sock_register(&tipc_family_ops);
1933 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001934 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001935 proto_unregister(&tipc_proto);
1936 goto out;
1937 }
1938
1939 sockets_enabled = 1;
1940 out:
1941 return res;
1942}
1943
1944/**
Per Liden4323add2006-01-18 00:38:21 +01001945 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001946 */
Per Liden4323add2006-01-18 00:38:21 +01001947void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001948{
1949 if (!sockets_enabled)
1950 return;
1951
1952 sockets_enabled = 0;
1953 sock_unregister(tipc_family_ops.family);
1954 proto_unregister(&tipc_proto);
1955}