blob: 844bf349bb819f1531986aabc073dca747485606 [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 res = -ENOTCONN;
755
756 if (sock->state == SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700757 res = send_packet(NULL, sock, m, total_len);
wangweidong3b8401f2013-12-12 09:36:40 +0800758 else if (sock->state == SS_DISCONNECTING)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700759 res = -EPIPE;
wangweidong3b8401f2013-12-12 09:36:40 +0800760
761 goto exit;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900762 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763
Allan Stephens0c3141e2008-04-15 00:22:02 -0700764 if (unlikely(m->msg_name)) {
765 res = -EISCONN;
766 goto exit;
767 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700768
Ying Xue97f8b872013-01-31 21:51:47 +0100769 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400770 res = -EMSGSIZE;
771 goto exit;
772 }
773
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900774 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775 * Send each iovec entry using one or more messages
776 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900777 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 * (i.e. one large iovec entry), but could be improved to pass sets
779 * of small iovec entries into send_packet().
780 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700781 curr_iov = m->msg_iov;
782 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100783 my_msg.msg_iov = &my_iov;
784 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700785 my_msg.msg_flags = m->msg_flags;
786 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700787 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788
Allan Stephens05646c92007-06-10 17:25:24 -0700789 hdr_size = msg_hdr_sz(&tport->phdr);
790
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791 while (curr_iovlen--) {
792 curr_start = curr_iov->iov_base;
793 curr_left = curr_iov->iov_len;
794
795 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700796 bytes_to_send = tport->max_pkt - hdr_size;
797 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
798 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
799 if (curr_left < bytes_to_send)
800 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100801 my_iov.iov_base = curr_start;
802 my_iov.iov_len = bytes_to_send;
Allan Stephens26896902011-04-21 10:42:07 -0500803 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000804 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700805 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700806 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700807 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700808 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100809 curr_left -= bytes_to_send;
810 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700811 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812 }
813
814 curr_iov++;
815 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700816 res = bytes_sent;
817exit:
818 release_sock(sk);
819 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100820}
821
822/**
823 * auto_connect - complete connection setup to a remote port
824 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900826 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100827 * Returns 0 on success, errno otherwise
828 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700829static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830{
Allan Stephens2da59912008-07-14 22:43:32 -0700831 struct tipc_sock *tsock = tipc_sk(sock->sk);
Ying Xue584d24b2012-11-29 18:51:19 -0500832 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100833
Allan Stephens2da59912008-07-14 22:43:32 -0700834 tsock->peer_name.ref = msg_origport(msg);
835 tsock->peer_name.node = msg_orignode(msg);
Ying Xue584d24b2012-11-29 18:51:19 -0500836 p_ptr = tipc_port_deref(tsock->p->ref);
837 if (!p_ptr)
838 return -EINVAL;
839
840 __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
841
842 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
843 return -EINVAL;
844 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100845 sock->state = SS_CONNECTED;
846 return 0;
847}
848
849/**
850 * set_orig_addr - capture sender's address for received message
851 * @m: descriptor for message info
852 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900853 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854 * Note: Address is not captured if not requested by receiver.
855 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800856static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900858 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900860 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861 addr->family = AF_TIPC;
862 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000863 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100864 addr->addr.id.ref = msg_origport(msg);
865 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000866 addr->addr.name.domain = 0; /* could leave uninitialized */
867 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100868 m->msg_namelen = sizeof(struct sockaddr_tipc);
869 }
870}
871
872/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900873 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100874 * @m: descriptor for message info
875 * @msg: received message header
876 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900877 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900879 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 * Returns 0 if successful, otherwise errno
881 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800882static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Paul Gortmakerae8509c2013-06-17 10:54:47 -0400883 struct tipc_port *tport)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100884{
885 u32 anc_data[3];
886 u32 err;
887 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700888 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889 int res;
890
891 if (likely(m->msg_controllen == 0))
892 return 0;
893
894 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100895 err = msg ? msg_errcode(msg) : 0;
896 if (unlikely(err)) {
897 anc_data[0] = err;
898 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000899 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
900 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000902 if (anc_data[1]) {
903 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
904 msg_data(msg));
905 if (res)
906 return res;
907 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908 }
909
910 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100911 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
912 switch (dest_type) {
913 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700914 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100915 anc_data[0] = msg_nametype(msg);
916 anc_data[1] = msg_namelower(msg);
917 anc_data[2] = msg_namelower(msg);
918 break;
919 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700920 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921 anc_data[0] = msg_nametype(msg);
922 anc_data[1] = msg_namelower(msg);
923 anc_data[2] = msg_nameupper(msg);
924 break;
925 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700926 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927 anc_data[0] = tport->conn_type;
928 anc_data[1] = tport->conn_instance;
929 anc_data[2] = tport->conn_instance;
930 break;
931 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700932 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100933 }
Allan Stephens2db99832010-12-31 18:59:33 +0000934 if (has_name) {
935 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
936 if (res)
937 return res;
938 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100939
940 return 0;
941}
942
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900943/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100944 * recv_msg - receive packet-oriented message
945 * @iocb: (unused)
946 * @m: descriptor for message info
947 * @buf_len: total size of user buffer area
948 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900949 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100950 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
951 * If the complete message doesn't fit in user area, truncate it.
952 *
953 * Returns size of returned message data, errno otherwise
954 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955static int recv_msg(struct kiocb *iocb, struct socket *sock,
956 struct msghdr *m, size_t buf_len, int flags)
957{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700958 struct sock *sk = sock->sk;
959 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100960 struct sk_buff *buf;
961 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500962 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100963 unsigned int sz;
964 u32 err;
965 int res;
966
Allan Stephens0c3141e2008-04-15 00:22:02 -0700967 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968 if (unlikely(!buf_len))
969 return -EINVAL;
970
Allan Stephens0c3141e2008-04-15 00:22:02 -0700971 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100972
Allan Stephens0c3141e2008-04-15 00:22:02 -0700973 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974 res = -ENOTCONN;
975 goto exit;
976 }
977
Allan Stephens71092ea2011-02-23 14:52:14 -0500978 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700979restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980
Allan Stephens0c3141e2008-04-15 00:22:02 -0700981 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700982 while (skb_queue_empty(&sk->sk_receive_queue)) {
983 if (sock->state == SS_DISCONNECTING) {
984 res = -ENOTCONN;
985 goto exit;
986 }
Allan Stephens71092ea2011-02-23 14:52:14 -0500987 if (timeout <= 0L) {
988 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700989 goto exit;
990 }
991 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -0500992 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
993 tipc_rx_ready(sock),
994 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700995 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700996 }
997
998 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700999 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001000 msg = buf_msg(buf);
1001 sz = msg_data_sz(msg);
1002 err = msg_errcode(msg);
1003
Per Lidenb97bf3f2006-01-02 19:04:38 +01001004 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001005 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001006 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 goto restart;
1008 }
1009
1010 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011 set_orig_addr(m, msg);
1012
1013 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001014 res = anc_data_recv(m, msg, tport);
1015 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016 goto exit;
1017
1018 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001019 if (!err) {
1020 if (unlikely(buf_len < sz)) {
1021 sz = buf_len;
1022 m->msg_flags |= MSG_TRUNC;
1023 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001024 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1025 m->msg_iov, sz);
1026 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 res = sz;
1029 } else {
1030 if ((sock->state == SS_READY) ||
1031 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1032 res = 0;
1033 else
1034 res = -ECONNRESET;
1035 }
1036
1037 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001038 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001039 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -07001040 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1041 tipc_acknowledge(tport->ref, tport->conn_unacked);
1042 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001043 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001044exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001045 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046 return res;
1047}
1048
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001049/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 * recv_stream - receive stream-oriented data
1051 * @iocb: (unused)
1052 * @m: descriptor for message info
1053 * @buf_len: total size of user buffer area
1054 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001055 *
1056 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001057 * will optionally wait for more; never truncates data.
1058 *
1059 * Returns size of returned message data, errno otherwise
1060 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061static int recv_stream(struct kiocb *iocb, struct socket *sock,
1062 struct msghdr *m, size_t buf_len, int flags)
1063{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001064 struct sock *sk = sock->sk;
1065 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 struct sk_buff *buf;
1067 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -05001068 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001070 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001073 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074
1075 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076 if (unlikely(!buf_len))
1077 return -EINVAL;
1078
Allan Stephens0c3141e2008-04-15 00:22:02 -07001079 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001080
Erik Hugne5d21cb72013-06-17 10:54:38 -04001081 if (unlikely((sock->state == SS_UNCONNECTED))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 res = -ENOTCONN;
1083 goto exit;
1084 }
1085
Florian Westphal3720d402010-08-17 11:00:04 +00001086 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Allan Stephens71092ea2011-02-23 14:52:14 -05001087 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001088
Allan Stephens0c3141e2008-04-15 00:22:02 -07001089restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001090 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001091 while (skb_queue_empty(&sk->sk_receive_queue)) {
1092 if (sock->state == SS_DISCONNECTING) {
1093 res = -ENOTCONN;
1094 goto exit;
1095 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001096 if (timeout <= 0L) {
1097 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001098 goto exit;
1099 }
1100 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001101 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1102 tipc_rx_ready(sock),
1103 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001104 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001105 }
1106
1107 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001108 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001109 msg = buf_msg(buf);
1110 sz = msg_data_sz(msg);
1111 err = msg_errcode(msg);
1112
1113 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001115 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116 goto restart;
1117 }
1118
1119 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001120 if (sz_copied == 0) {
1121 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001122 res = anc_data_recv(m, msg, tport);
1123 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124 goto exit;
1125 }
1126
1127 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001128 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001129 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130
Allan Stephens0232fd02011-02-21 09:45:40 -05001131 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132 needed = (buf_len - sz_copied);
1133 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001134
1135 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1136 m->msg_iov, sz_to_copy);
1137 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001138 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001139
Per Lidenb97bf3f2006-01-02 19:04:38 +01001140 sz_copied += sz_to_copy;
1141
1142 if (sz_to_copy < sz) {
1143 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001144 TIPC_SKB_CB(buf)->handle =
1145 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146 goto exit;
1147 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 } else {
1149 if (sz_copied != 0)
1150 goto exit; /* can't add error msg to valid data */
1151
1152 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1153 res = 0;
1154 else
1155 res = -ECONNRESET;
1156 }
1157
1158 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001160 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1161 tipc_acknowledge(tport->ref, tport->conn_unacked);
1162 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001163 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164
1165 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001166 if ((sz_copied < buf_len) && /* didn't get all requested data */
1167 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001168 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001169 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1170 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171 goto restart;
1172
1173exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001174 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001175 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176}
1177
1178/**
Ying Xuef288bef2012-08-21 11:16:57 +08001179 * tipc_write_space - wake up thread if port congestion is released
1180 * @sk: socket
1181 */
1182static void tipc_write_space(struct sock *sk)
1183{
1184 struct socket_wq *wq;
1185
1186 rcu_read_lock();
1187 wq = rcu_dereference(sk->sk_wq);
1188 if (wq_has_sleeper(wq))
1189 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1190 POLLWRNORM | POLLWRBAND);
1191 rcu_read_unlock();
1192}
1193
1194/**
1195 * tipc_data_ready - wake up threads to indicate messages have been received
1196 * @sk: socket
1197 * @len: the length of messages
1198 */
1199static void tipc_data_ready(struct sock *sk, int len)
1200{
1201 struct socket_wq *wq;
1202
1203 rcu_read_lock();
1204 wq = rcu_dereference(sk->sk_wq);
1205 if (wq_has_sleeper(wq))
1206 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1207 POLLRDNORM | POLLRDBAND);
1208 rcu_read_unlock();
1209}
1210
1211/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001212 * filter_connect - Handle all incoming messages for a connection-based socket
1213 * @tsock: TIPC socket
1214 * @msg: message
1215 *
1216 * Returns TIPC error status code and socket error status code
1217 * once it encounters some errors
1218 */
1219static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
1220{
1221 struct socket *sock = tsock->sk.sk_socket;
1222 struct tipc_msg *msg = buf_msg(*buf);
Ying Xue584d24b2012-11-29 18:51:19 -05001223 struct sock *sk = &tsock->sk;
Ying Xue7e6c1312012-11-29 18:39:14 -05001224 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001225 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001226
1227 if (msg_mcast(msg))
1228 return retval;
1229
1230 switch ((int)sock->state) {
1231 case SS_CONNECTED:
1232 /* Accept only connection-based messages sent by peer */
1233 if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
1234 if (unlikely(msg_errcode(msg))) {
1235 sock->state = SS_DISCONNECTING;
1236 __tipc_disconnect(tsock->p);
1237 }
1238 retval = TIPC_OK;
1239 }
1240 break;
1241 case SS_CONNECTING:
1242 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001243 if (unlikely(msg_errcode(msg))) {
1244 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001245 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001246 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001247 break;
1248 }
1249
1250 if (unlikely(!msg_connected(msg)))
1251 break;
1252
1253 res = auto_connect(sock, msg);
1254 if (res) {
1255 sock->state = SS_DISCONNECTING;
Erik Hugne2c8d8512013-08-28 09:29:58 +02001256 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001257 retval = TIPC_OK;
1258 break;
1259 }
1260
1261 /* If an incoming message is an 'ACK-', it should be
1262 * discarded here because it doesn't contain useful
1263 * data. In addition, we should try to wake up
1264 * connect() routine if sleeping.
1265 */
1266 if (msg_data_sz(msg) == 0) {
1267 kfree_skb(*buf);
1268 *buf = NULL;
1269 if (waitqueue_active(sk_sleep(sk)))
1270 wake_up_interruptible(sk_sleep(sk));
1271 }
1272 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001273 break;
1274 case SS_LISTENING:
1275 case SS_UNCONNECTED:
1276 /* Accept only SYN message */
1277 if (!msg_connected(msg) && !(msg_errcode(msg)))
1278 retval = TIPC_OK;
1279 break;
1280 case SS_DISCONNECTING:
1281 break;
1282 default:
1283 pr_err("Unknown socket state %u\n", sock->state);
1284 }
1285 return retval;
1286}
1287
1288/**
Ying Xueaba79f32013-01-20 23:30:09 +01001289 * rcvbuf_limit - get proper overload limit of socket receive queue
1290 * @sk: socket
1291 * @buf: message
1292 *
1293 * For all connection oriented messages, irrespective of importance,
1294 * the default overload value (i.e. 67MB) is set as limit.
1295 *
1296 * For all connectionless messages, by default new queue limits are
1297 * as belows:
1298 *
Ying Xuecc79dd12013-06-17 10:54:37 -04001299 * TIPC_LOW_IMPORTANCE (4 MB)
1300 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1301 * TIPC_HIGH_IMPORTANCE (16 MB)
1302 * TIPC_CRITICAL_IMPORTANCE (32 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001303 *
1304 * Returns overload limit according to corresponding message importance
1305 */
1306static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1307{
1308 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001309
1310 if (msg_connected(msg))
wangweidong0cee6bb2013-12-12 09:36:39 +08001311 return sysctl_tipc_rmem[2];
1312
1313 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1314 msg_importance(msg);
Ying Xueaba79f32013-01-20 23:30:09 +01001315}
1316
1317/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001318 * filter_rcv - validate incoming message
1319 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001321 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001322 * Enqueues message on receive queue if acceptable; optionally handles
1323 * disconnect indication for a connected socket.
1324 *
1325 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001326 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001327 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1328 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001329static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001330{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001331 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001333 unsigned int limit = rcvbuf_limit(sk, buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001334 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335
Per Lidenb97bf3f2006-01-02 19:04:38 +01001336 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001337 if (msg_type(msg) > TIPC_DIRECT_MSG)
1338 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001339
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001341 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343 } else {
Ying Xue7e6c1312012-11-29 18:39:14 -05001344 res = filter_connect(tipc_sk(sk), &buf);
1345 if (res != TIPC_OK || buf == NULL)
1346 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347 }
1348
1349 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001350 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1351 return TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352
Ying Xueaba79f32013-01-20 23:30:09 +01001353 /* Enqueue message */
Ying Xue40682432013-10-18 07:23:16 +02001354 TIPC_SKB_CB(buf)->handle = NULL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001355 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001356 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001357
Ying Xuef288bef2012-08-21 11:16:57 +08001358 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359 return TIPC_OK;
1360}
1361
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001362/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001363 * backlog_rcv - handle incoming message from backlog queue
1364 * @sk: socket
1365 * @buf: message
1366 *
1367 * Caller must hold socket lock, but not port lock.
1368 *
1369 * Returns 0
1370 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001371static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1372{
1373 u32 res;
1374
1375 res = filter_rcv(sk, buf);
1376 if (res)
1377 tipc_reject_msg(buf, res);
1378 return 0;
1379}
1380
1381/**
1382 * dispatch - handle incoming message
1383 * @tport: TIPC port that received message
1384 * @buf: message
1385 *
1386 * Called with port lock already taken.
1387 *
1388 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1389 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001390static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1391{
Ying Xuec0fee8a2013-06-17 10:54:46 -04001392 struct sock *sk = tport->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001393 u32 res;
1394
1395 /*
1396 * Process message if socket is unlocked; otherwise add to backlog queue
1397 *
1398 * This code is based on sk_receive_skb(), but must be distinct from it
1399 * since a TIPC-specific filter/reject mechanism is utilized
1400 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001401 bh_lock_sock(sk);
1402 if (!sock_owned_by_user(sk)) {
1403 res = filter_rcv(sk, buf);
1404 } else {
Ying Xueaba79f32013-01-20 23:30:09 +01001405 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
Zhu Yi53eecb12010-03-04 18:01:45 +00001406 res = TIPC_ERR_OVERLOAD;
1407 else
1408 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001409 }
1410 bh_unlock_sock(sk);
1411
1412 return res;
1413}
1414
1415/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001416 * wakeupdispatch - wake up port after congestion
1417 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001418 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001419 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001420 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421static void wakeupdispatch(struct tipc_port *tport)
1422{
Ying Xuec0fee8a2013-06-17 10:54:46 -04001423 struct sock *sk = tport->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424
Ying Xuef288bef2012-08-21 11:16:57 +08001425 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426}
1427
1428/**
1429 * connect - establish a connection to another TIPC port
1430 * @sock: socket structure
1431 * @dest: socket address for destination port
1432 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001433 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001434 *
1435 * Returns 0 on success, errno otherwise
1436 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001437static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001438 int flags)
1439{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001440 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001441 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1442 struct msghdr m = {NULL,};
Allan Stephensa0f40f02011-05-26 13:44:34 -04001443 unsigned int timeout;
Allan Stephensb89741a2008-04-15 00:20:37 -07001444 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001445
Allan Stephens0c3141e2008-04-15 00:22:02 -07001446 lock_sock(sk);
1447
Allan Stephensb89741a2008-04-15 00:20:37 -07001448 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001449 if (sock->state == SS_READY) {
1450 res = -EOPNOTSUPP;
1451 goto exit;
1452 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001453
Allan Stephensb89741a2008-04-15 00:20:37 -07001454 /*
1455 * Reject connection attempt using multicast address
1456 *
1457 * Note: send_msg() validates the rest of the address fields,
1458 * so there's no need to do it here
1459 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001460 if (dst->addrtype == TIPC_ADDR_MCAST) {
1461 res = -EINVAL;
1462 goto exit;
1463 }
1464
Ying Xue584d24b2012-11-29 18:51:19 -05001465 timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001466
Ying Xue584d24b2012-11-29 18:51:19 -05001467 switch (sock->state) {
1468 case SS_UNCONNECTED:
1469 /* Send a 'SYN-' to destination */
1470 m.msg_name = dest;
1471 m.msg_namelen = destlen;
1472
1473 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1474 * indicate send_msg() is never blocked.
1475 */
1476 if (!timeout)
1477 m.msg_flags = MSG_DONTWAIT;
1478
1479 res = send_msg(NULL, sock, &m, 0);
1480 if ((res < 0) && (res != -EWOULDBLOCK))
1481 goto exit;
1482
1483 /* Just entered SS_CONNECTING state; the only
1484 * difference is that return value in non-blocking
1485 * case is EINPROGRESS, rather than EALREADY.
1486 */
1487 res = -EINPROGRESS;
1488 break;
1489 case SS_CONNECTING:
1490 res = -EALREADY;
1491 break;
1492 case SS_CONNECTED:
1493 res = -EISCONN;
1494 break;
1495 default:
1496 res = -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001497 goto exit;
Allan Stephensb89741a2008-04-15 00:20:37 -07001498 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001499
Ying Xue584d24b2012-11-29 18:51:19 -05001500 if (sock->state == SS_CONNECTING) {
1501 if (!timeout)
1502 goto exit;
1503
1504 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1505 release_sock(sk);
1506 res = wait_event_interruptible_timeout(*sk_sleep(sk),
1507 sock->state != SS_CONNECTING,
1508 timeout ? (long)msecs_to_jiffies(timeout)
1509 : MAX_SCHEDULE_TIMEOUT);
1510 lock_sock(sk);
1511 if (res <= 0) {
1512 if (res == 0)
1513 res = -ETIMEDOUT;
1514 else
1515 ; /* leave "res" unchanged */
1516 goto exit;
1517 }
1518 }
1519
1520 if (unlikely(sock->state == SS_DISCONNECTING))
1521 res = sock_error(sk);
1522 else
1523 res = 0;
1524
Allan Stephens0c3141e2008-04-15 00:22:02 -07001525exit:
1526 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001527 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001528}
1529
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001530/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001531 * listen - allow socket to listen for incoming connections
1532 * @sock: socket structure
1533 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001534 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001535 * Returns 0 on success, errno otherwise
1536 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001537static int listen(struct socket *sock, int len)
1538{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001539 struct sock *sk = sock->sk;
1540 int res;
1541
1542 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001543
Ying Xue245f3d32011-07-06 06:01:13 -04001544 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001545 res = -EINVAL;
1546 else {
1547 sock->state = SS_LISTENING;
1548 res = 0;
1549 }
1550
1551 release_sock(sk);
1552 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001553}
1554
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001555/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001556 * accept - wait for connection request
1557 * @sock: listening socket
1558 * @newsock: new socket that is to be connected
1559 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001560 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001561 * Returns 0 on success, errno otherwise
1562 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001563static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001564{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001565 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001566 struct sk_buff *buf;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001567 struct tipc_sock *new_tsock;
1568 struct tipc_port *new_tport;
1569 struct tipc_msg *msg;
1570 u32 new_ref;
1571
Allan Stephens0c3141e2008-04-15 00:22:02 -07001572 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001573
Allan Stephens0c3141e2008-04-15 00:22:02 -07001574 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001575
Allan Stephens0c3141e2008-04-15 00:22:02 -07001576 if (sock->state != SS_LISTENING) {
1577 res = -EINVAL;
1578 goto exit;
1579 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001580
Allan Stephens0c3141e2008-04-15 00:22:02 -07001581 while (skb_queue_empty(&sk->sk_receive_queue)) {
1582 if (flags & O_NONBLOCK) {
1583 res = -EWOULDBLOCK;
1584 goto exit;
1585 }
1586 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001587 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001588 (!skb_queue_empty(&sk->sk_receive_queue)));
1589 lock_sock(sk);
1590 if (res)
1591 goto exit;
1592 }
1593
1594 buf = skb_peek(&sk->sk_receive_queue);
1595
Ying Xuec5fa7b32013-06-17 10:54:39 -04001596 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001597 if (res)
1598 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001599
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001600 new_sk = new_sock->sk;
1601 new_tsock = tipc_sk(new_sk);
1602 new_tport = new_tsock->p;
1603 new_ref = new_tport->ref;
1604 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001605
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001606 /* we lock on new_sk; but lockdep sees the lock on sk */
1607 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001608
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001609 /*
1610 * Reject any stray messages received by new socket
1611 * before the socket lock was taken (very, very unlikely)
1612 */
1613 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001614
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001615 /* Connect new socket to it's peer */
1616 new_tsock->peer_name.ref = msg_origport(msg);
1617 new_tsock->peer_name.node = msg_orignode(msg);
1618 tipc_connect(new_ref, &new_tsock->peer_name);
1619 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001620
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001621 tipc_set_portimportance(new_ref, msg_importance(msg));
1622 if (msg_named(msg)) {
1623 new_tport->conn_type = msg_nametype(msg);
1624 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001625 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001626
1627 /*
1628 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1629 * Respond to 'SYN+' by queuing it on new socket.
1630 */
1631 if (!msg_data_sz(msg)) {
1632 struct msghdr m = {NULL,};
1633
1634 advance_rx_queue(sk);
1635 send_packet(NULL, new_sock, &m, 0);
1636 } else {
1637 __skb_dequeue(&sk->sk_receive_queue);
1638 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001639 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001640 }
1641 release_sock(new_sk);
1642
Per Lidenb97bf3f2006-01-02 19:04:38 +01001643exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001644 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001645 return res;
1646}
1647
1648/**
1649 * shutdown - shutdown socket connection
1650 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001651 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652 *
1653 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001654 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001655 * Returns 0 on success, errno otherwise
1656 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001657static int shutdown(struct socket *sock, int how)
1658{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001659 struct sock *sk = sock->sk;
1660 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661 struct sk_buff *buf;
1662 int res;
1663
Allan Stephense247a8f2008-03-06 15:05:38 -08001664 if (how != SHUT_RDWR)
1665 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666
Allan Stephens0c3141e2008-04-15 00:22:02 -07001667 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001668
1669 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001670 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001671 case SS_CONNECTED:
1672
Per Lidenb97bf3f2006-01-02 19:04:38 +01001673restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001674 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001675 buf = __skb_dequeue(&sk->sk_receive_queue);
1676 if (buf) {
Ying Xue40682432013-10-18 07:23:16 +02001677 if (TIPC_SKB_CB(buf)->handle != NULL) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001678 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679 goto restart;
1680 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001681 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001682 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001683 } else {
1684 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001685 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001686
1687 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001688
1689 /* fall through */
1690
1691 case SS_DISCONNECTING:
1692
Ying Xue75031152012-10-29 09:38:15 -04001693 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001694 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001695
1696 /* Wake up anyone sleeping in poll */
1697 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001698 res = 0;
1699 break;
1700
1701 default:
1702 res = -ENOTCONN;
1703 }
1704
Allan Stephens0c3141e2008-04-15 00:22:02 -07001705 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001706 return res;
1707}
1708
1709/**
1710 * setsockopt - set socket option
1711 * @sock: socket structure
1712 * @lvl: option level
1713 * @opt: option identifier
1714 * @ov: pointer to new option value
1715 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001716 *
1717 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001718 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001719 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001720 * Returns 0 on success, errno otherwise
1721 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001722static int setsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
1723 unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001724{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001725 struct sock *sk = sock->sk;
1726 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001727 u32 value;
1728 int res;
1729
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001730 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1731 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001732 if (lvl != SOL_TIPC)
1733 return -ENOPROTOOPT;
1734 if (ol < sizeof(value))
1735 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001736 res = get_user(value, (u32 __user *)ov);
1737 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001738 return res;
1739
Allan Stephens0c3141e2008-04-15 00:22:02 -07001740 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001741
Per Lidenb97bf3f2006-01-02 19:04:38 +01001742 switch (opt) {
1743 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001744 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001745 break;
1746 case TIPC_SRC_DROPPABLE:
1747 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001748 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001749 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001750 res = -ENOPROTOOPT;
1751 break;
1752 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001753 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001754 break;
1755 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001756 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001757 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001758 break;
1759 default:
1760 res = -EINVAL;
1761 }
1762
Allan Stephens0c3141e2008-04-15 00:22:02 -07001763 release_sock(sk);
1764
Per Lidenb97bf3f2006-01-02 19:04:38 +01001765 return res;
1766}
1767
1768/**
1769 * getsockopt - get socket option
1770 * @sock: socket structure
1771 * @lvl: option level
1772 * @opt: option identifier
1773 * @ov: receptacle for option value
1774 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001775 *
1776 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001777 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001778 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001779 * Returns 0 on success, errno otherwise
1780 */
Paul Gortmakerae8509c2013-06-17 10:54:47 -04001781static int getsockopt(struct socket *sock, int lvl, int opt, char __user *ov,
1782 int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001783{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001784 struct sock *sk = sock->sk;
1785 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001786 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001788 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001789
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001790 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1791 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001792 if (lvl != SOL_TIPC)
1793 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001794 res = get_user(len, ol);
1795 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001796 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001797
Allan Stephens0c3141e2008-04-15 00:22:02 -07001798 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001799
1800 switch (opt) {
1801 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001802 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001803 break;
1804 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001805 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001806 break;
1807 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001808 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001809 break;
1810 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001811 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001812 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001813 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001814 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001815 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001816 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001817 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001818 value = skb_queue_len(&sk->sk_receive_queue);
1819 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001820 default:
1821 res = -EINVAL;
1822 }
1823
Allan Stephens0c3141e2008-04-15 00:22:02 -07001824 release_sock(sk);
1825
Paul Gortmaker25860c32010-12-31 18:59:31 +00001826 if (res)
1827 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001828
Paul Gortmaker25860c32010-12-31 18:59:31 +00001829 if (len < sizeof(value))
1830 return -EINVAL;
1831
1832 if (copy_to_user(ov, &value, sizeof(value)))
1833 return -EFAULT;
1834
1835 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001836}
1837
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001838/* Protocol switches for the various types of TIPC sockets */
1839
Florian Westphalbca65ea2008-02-07 18:18:01 -08001840static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001841 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001842 .family = AF_TIPC,
1843 .release = release,
1844 .bind = bind,
1845 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001846 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001847 .accept = sock_no_accept,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001848 .getname = get_name,
1849 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001850 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001851 .listen = sock_no_listen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001852 .shutdown = shutdown,
1853 .setsockopt = setsockopt,
1854 .getsockopt = getsockopt,
1855 .sendmsg = send_msg,
1856 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001857 .mmap = sock_no_mmap,
1858 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001859};
1860
Florian Westphalbca65ea2008-02-07 18:18:01 -08001861static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001862 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001863 .family = AF_TIPC,
1864 .release = release,
1865 .bind = bind,
1866 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001867 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001868 .accept = accept,
1869 .getname = get_name,
1870 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001871 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001872 .listen = listen,
1873 .shutdown = shutdown,
1874 .setsockopt = setsockopt,
1875 .getsockopt = getsockopt,
1876 .sendmsg = send_packet,
1877 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001878 .mmap = sock_no_mmap,
1879 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001880};
1881
Florian Westphalbca65ea2008-02-07 18:18:01 -08001882static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001883 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001884 .family = AF_TIPC,
1885 .release = release,
1886 .bind = bind,
1887 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001888 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001889 .accept = accept,
1890 .getname = get_name,
1891 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001892 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001893 .listen = listen,
1894 .shutdown = shutdown,
1895 .setsockopt = setsockopt,
1896 .getsockopt = getsockopt,
1897 .sendmsg = send_stream,
1898 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001899 .mmap = sock_no_mmap,
1900 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001901};
1902
Florian Westphalbca65ea2008-02-07 18:18:01 -08001903static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001904 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001905 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04001906 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907};
1908
1909static struct proto tipc_proto = {
1910 .name = "TIPC",
1911 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04001912 .obj_size = sizeof(struct tipc_sock),
1913 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01001914};
1915
Ying Xuec5fa7b32013-06-17 10:54:39 -04001916static struct proto tipc_proto_kern = {
1917 .name = "TIPC",
1918 .obj_size = sizeof(struct tipc_sock),
1919 .sysctl_rmem = sysctl_tipc_rmem
1920};
1921
Per Lidenb97bf3f2006-01-02 19:04:38 +01001922/**
Per Liden4323add2006-01-18 00:38:21 +01001923 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001924 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001925 * Returns 0 on success, errno otherwise
1926 */
Per Liden4323add2006-01-18 00:38:21 +01001927int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001928{
1929 int res;
1930
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001931 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001932 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001933 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001934 goto out;
1935 }
1936
1937 res = sock_register(&tipc_family_ops);
1938 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001939 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001940 proto_unregister(&tipc_proto);
1941 goto out;
1942 }
1943
1944 sockets_enabled = 1;
1945 out:
1946 return res;
1947}
1948
1949/**
Per Liden4323add2006-01-18 00:38:21 +01001950 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001951 */
Per Liden4323add2006-01-18 00:38:21 +01001952void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001953{
1954 if (!sockets_enabled)
1955 return;
1956
1957 sockets_enabled = 0;
1958 sock_unregister(tipc_family_ops.family);
1959 proto_unregister(&tipc_proto);
1960}