blob: 2b1d7c2d677d3f45bf6a547b8c777e5f2277f2a8 [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 Xue9da3d472012-11-27 06:15:27 -05005 * Copyright (c) 2004-2008, 2010-2012, 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
Ying Xueaba79f32013-01-20 23:30:09 +010046#define CONN_OVERLOAD_LIMIT ((TIPC_FLOW_CONTROL_WIN * 2 + 1) * \
47 SKB_TRUESIZE(TIPC_MAX_USER_MSG_SIZE))
Allan Stephens3654ea02008-04-13 21:35:11 -070048#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010049
50struct tipc_sock {
51 struct sock sk;
52 struct tipc_port *p;
Allan Stephens2da59912008-07-14 22:43:32 -070053 struct tipc_portid peer_name;
Allan Stephensa0f40f02011-05-26 13:44:34 -040054 unsigned int conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +010055};
56
Allan Stephens0c3141e2008-04-15 00:22:02 -070057#define tipc_sk(sk) ((struct tipc_sock *)(sk))
Joe Perchese3192692012-06-03 17:41:40 +000058#define tipc_sk_port(sk) (tipc_sk(sk)->p)
Per Lidenb97bf3f2006-01-02 19:04:38 +010059
Allan Stephens71092ea2011-02-23 14:52:14 -050060#define tipc_rx_ready(sock) (!skb_queue_empty(&sock->sk->sk_receive_queue) || \
61 (sock->state == SS_DISCONNECTING))
62
Allan Stephens0c3141e2008-04-15 00:22:02 -070063static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +010064static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
65static void wakeupdispatch(struct tipc_port *tport);
Ying Xuef288bef2012-08-21 11:16:57 +080066static void tipc_data_ready(struct sock *sk, int len);
67static void tipc_write_space(struct sock *sk);
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;
74
Allan Stephense3ec9c72010-12-31 18:59:34 +000075static int sockets_enabled;
Per Lidenb97bf3f2006-01-02 19:04:38 +010076
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090077/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070078 * Revised TIPC socket locking policy:
79 *
80 * Most socket operations take the standard socket lock when they start
81 * and hold it until they finish (or until they need to sleep). Acquiring
82 * this lock grants the owner exclusive access to the fields of the socket
83 * data structures, with the exception of the backlog queue. A few socket
84 * operations can be done without taking the socket lock because they only
85 * read socket information that never changes during the life of the socket.
86 *
87 * Socket operations may acquire the lock for the associated TIPC port if they
88 * need to perform an operation on the port. If any routine needs to acquire
89 * both the socket lock and the port lock it must take the socket lock first
90 * to avoid the risk of deadlock.
91 *
92 * The dispatcher handling incoming messages cannot grab the socket lock in
93 * the standard fashion, since invoked it runs at the BH level and cannot block.
94 * Instead, it checks to see if the socket lock is currently owned by someone,
95 * and either handles the message itself or adds it to the socket's backlog
96 * queue; in the latter case the queued message is processed once the process
97 * owning the socket lock releases it.
98 *
99 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
100 * the problem of a blocked socket operation preventing any other operations
101 * from occurring. However, applications must be careful if they have
102 * multiple threads trying to send (or receive) on the same socket, as these
103 * operations might interfere with each other. For example, doing a connect
104 * and a receive at the same time might allow the receive to consume the
105 * ACK message meant for the connect. While additional work could be done
106 * to try and overcome this, it doesn't seem to be worthwhile at the present.
107 *
108 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
109 * that another operation that must be performed in a non-blocking manner is
110 * not delayed for very long because the lock has already been taken.
111 *
112 * NOTE: This code assumes that certain fields of a port/socket pair are
113 * constant over its lifetime; such fields can be examined without taking
114 * the socket lock and/or port lock, and do not need to be re-read even
115 * after resuming processing after waiting. These fields include:
116 * - socket type
117 * - pointer to socket sk structure (aka tipc_sock structure)
118 * - pointer to port structure
119 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121
122/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700123 * advance_rx_queue - discard first buffer in socket receive queue
124 *
125 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700127static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400129 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130}
131
132/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700133 * reject_rx_queue - reject all buffers in socket receive queue
134 *
135 * Caller must hold socket lock
136 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700137static void reject_rx_queue(struct sock *sk)
138{
139 struct sk_buff *buf;
140
Ying Xue9da3d472012-11-27 06:15:27 -0500141 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700142 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700143}
144
145/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146 * tipc_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700147 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148 * @sock: pre-allocated socket structure
149 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800150 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900151 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700152 * This routine creates additional data structures used by the TIPC socket,
153 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154 *
155 * Returns 0 on success, errno otherwise
156 */
Eric Paris3f378b62009-11-05 22:18:14 -0800157static int tipc_create(struct net *net, struct socket *sock, int protocol,
158 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700160 const struct proto_ops *ops;
161 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700163 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700164
165 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 if (unlikely(protocol != 0))
167 return -EPROTONOSUPPORT;
168
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 switch (sock->type) {
170 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700171 ops = &stream_ops;
172 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 break;
174 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700175 ops = &packet_ops;
176 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 break;
178 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700180 ops = &msg_ops;
181 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182 break;
Allan Stephens49978652006-06-25 23:47:18 -0700183 default:
Allan Stephens49978652006-06-25 23:47:18 -0700184 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185 }
186
Allan Stephens0c3141e2008-04-15 00:22:02 -0700187 /* Allocate socket's protocol area */
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700188 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700189 if (sk == NULL)
190 return -ENOMEM;
191
192 /* Allocate TIPC port for socket to use */
Allan Stephens0ea52242008-07-14 22:42:19 -0700193 tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
194 TIPC_LOW_IMPORTANCE);
195 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700196 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197 return -ENOMEM;
198 }
199
Allan Stephens0c3141e2008-04-15 00:22:02 -0700200 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700201 sock->ops = ops;
202 sock->state = state;
203
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700205 sk->sk_backlog_rcv = backlog_rcv;
Ying Xuef288bef2012-08-21 11:16:57 +0800206 sk->sk_data_ready = tipc_data_ready;
207 sk->sk_write_space = tipc_write_space;
Allan Stephens0ea52242008-07-14 22:42:19 -0700208 tipc_sk(sk)->p = tp_ptr;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400209 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700211 spin_unlock_bh(tp_ptr->lock);
212
Allan Stephens0c3141e2008-04-15 00:22:02 -0700213 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700214 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700215 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700216 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700217 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219 return 0;
220}
221
222/**
223 * release - destroy a TIPC socket
224 * @sock: socket to destroy
225 *
226 * This routine cleans up any messages that are still queued on the socket.
227 * For DGRAM and RDM socket types, all queued messages are rejected.
228 * For SEQPACKET and STREAM socket types, the first message is rejected
229 * and any others are discarded. (If the first message on a STREAM socket
230 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900231 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 * NOTE: Rejected messages are not necessarily returned to the sender! They
233 * are returned or discarded according to the "destination droppable" setting
234 * specified for the message by the sender.
235 *
236 * Returns 0 on success, errno otherwise
237 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238static int release(struct socket *sock)
239{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700241 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700243 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244
Allan Stephens0c3141e2008-04-15 00:22:02 -0700245 /*
246 * Exit if socket isn't fully initialized (occurs when a failed accept()
247 * releases a pre-allocated child socket that was never used)
248 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700249 if (sk == NULL)
250 return 0;
251
252 tport = tipc_sk_port(sk);
253 lock_sock(sk);
254
255 /*
256 * Reject all unreceived messages, except on an active connection
257 * (which disconnects locally & sends a 'FIN+' to peer)
258 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700260 buf = __skb_dequeue(&sk->sk_receive_queue);
261 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262 break;
Allan Stephens0232fd02011-02-21 09:45:40 -0500263 if (TIPC_SKB_CB(buf)->handle != 0)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400264 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700265 else {
266 if ((sock->state == SS_CONNECTING) ||
267 (sock->state == SS_CONNECTED)) {
268 sock->state = SS_DISCONNECTING;
269 tipc_disconnect(tport->ref);
270 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700272 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273 }
274
Allan Stephens0c3141e2008-04-15 00:22:02 -0700275 /*
276 * Delete TIPC port; this ensures no more messages are queued
277 * (also disconnects an active connection & sends a 'FIN-' to peer)
278 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700279 res = tipc_deleteport(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280
Allan Stephens0c3141e2008-04-15 00:22:02 -0700281 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100282 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283
Allan Stephens0c3141e2008-04-15 00:22:02 -0700284 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700285 sock->state = SS_DISCONNECTING;
286 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287
288 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700289 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 return res;
292}
293
294/**
295 * bind - associate or disassocate TIPC name(s) with a socket
296 * @sock: socket structure
297 * @uaddr: socket address describing name(s) and desired operation
298 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900299 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 * Name and name sequence binding is indicated using a positive scope value;
301 * a negative scope value unbinds the specified name. Specifying no name
302 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900303 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700305 *
306 * NOTE: This routine doesn't need to take the socket lock since it doesn't
307 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
310{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700312 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313
Allan Stephens0c3141e2008-04-15 00:22:02 -0700314 if (unlikely(!uaddr_len))
315 return tipc_withdraw(portref, 0, NULL);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900316
Allan Stephens0c3141e2008-04-15 00:22:02 -0700317 if (uaddr_len < sizeof(struct sockaddr_tipc))
318 return -EINVAL;
319 if (addr->family != AF_TIPC)
320 return -EAFNOSUPPORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 if (addr->addrtype == TIPC_ADDR_NAME)
323 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700324 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
325 return -EAFNOSUPPORT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900326
Allan Stephensc422f1b2011-11-02 15:49:40 -0400327 if (addr->addr.nameseq.type < TIPC_RESERVED_TYPES)
328 return -EACCES;
329
Allan Stephens0c3141e2008-04-15 00:22:02 -0700330 return (addr->scope > 0) ?
331 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
332 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333}
334
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900335/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336 * get_name - get port ID of socket or peer socket
337 * @sock: socket structure
338 * @uaddr: area for returned socket address
339 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700340 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900341 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700343 *
Allan Stephens2da59912008-07-14 22:43:32 -0700344 * NOTE: This routine doesn't need to take the socket lock since it only
345 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000346 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900348static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 int *uaddr_len, int peer)
350{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700352 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000354 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700355 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700356 if ((sock->state != SS_CONNECTED) &&
357 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
358 return -ENOTCONN;
359 addr->addr.id.ref = tsock->peer_name.ref;
360 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700361 } else {
Allan Stephensb924dcf2010-11-30 12:01:03 +0000362 addr->addr.id.ref = tsock->p->ref;
363 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700364 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365
366 *uaddr_len = sizeof(*addr);
367 addr->addrtype = TIPC_ADDR_ID;
368 addr->family = AF_TIPC;
369 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100370 addr->addr.name.domain = 0;
371
Allan Stephens0c3141e2008-04-15 00:22:02 -0700372 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373}
374
375/**
376 * poll - read and possibly block on pollmask
377 * @file: file structure associated with the socket
378 * @sock: socket for which to calculate the poll bits
379 * @wait: ???
380 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700381 * Returns pollmask value
382 *
383 * COMMENTARY:
384 * It appears that the usual socket locking mechanisms are not useful here
385 * since the pollmask info is potentially out-of-date the moment this routine
386 * exits. TCP and other protocols seem to rely on higher level poll routines
387 * to handle any preventable race conditions, so TIPC will do the same ...
388 *
389 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700390 *
Allan Stephensf662c072010-08-17 11:00:06 +0000391 * socket state flags set
392 * ------------ ---------
393 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200394 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000395 *
396 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
397 * no write flags
398 *
399 * connected POLLIN/POLLRDNORM if data in rx queue
400 * POLLOUT if port is not congested
401 *
402 * disconnecting POLLIN/POLLRDNORM/POLLHUP
403 * no write flags
404 *
405 * listening POLLIN if SYN in rx queue
406 * no write flags
407 *
408 * ready POLLIN/POLLRDNORM if data in rx queue
409 * [connectionless] POLLOUT (since port cannot be congested)
410 *
411 * IMPORTANT: The fact that a read or write operation is indicated does NOT
412 * imply that the operation will succeed, merely that it should be performed
413 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900415static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 poll_table *wait)
417{
Allan Stephens9b674e82008-03-26 16:48:21 -0700418 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000419 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700420
Ying Xuef288bef2012-08-21 11:16:57 +0800421 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700422
Allan Stephensf662c072010-08-17 11:00:06 +0000423 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200424 case SS_UNCONNECTED:
425 if (!tipc_sk_port(sk)->congested)
426 mask |= POLLOUT;
427 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000428 case SS_READY:
429 case SS_CONNECTED:
430 if (!tipc_sk_port(sk)->congested)
431 mask |= POLLOUT;
432 /* fall thru' */
433 case SS_CONNECTING:
434 case SS_LISTENING:
435 if (!skb_queue_empty(&sk->sk_receive_queue))
436 mask |= (POLLIN | POLLRDNORM);
437 break;
438 case SS_DISCONNECTING:
439 mask = (POLLIN | POLLRDNORM | POLLHUP);
440 break;
441 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700442
443 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444}
445
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900446/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447 * dest_name_check - verify user is permitted to send to specified port name
448 * @dest: destination address
449 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900450 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451 * Prevents restricted configuration commands from being issued by
452 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900453 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454 * Returns 0 if permission is granted, otherwise errno
455 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800456static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457{
458 struct tipc_cfg_msg_hdr hdr;
459
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900460 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
461 return 0;
462 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
463 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900464 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
465 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466
Allan Stephens3f8dd942011-01-18 13:09:29 -0500467 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
468 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900469 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700471 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900473
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474 return 0;
475}
476
477/**
478 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700479 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480 * @sock: socket structure
481 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700482 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900483 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900485 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
487 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900488 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489 * Returns the number of bytes sent on success, or errno otherwise
490 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491static int send_msg(struct kiocb *iocb, struct socket *sock,
492 struct msghdr *m, size_t total_len)
493{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700494 struct sock *sk = sock->sk;
495 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900496 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 int needs_conn;
Ying Xue1d835872011-07-06 05:53:15 -0400498 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 int res = -EINVAL;
500
501 if (unlikely(!dest))
502 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700503 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
504 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 return -EINVAL;
Ying Xue97f8b872013-01-31 21:51:47 +0100506 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400507 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508
Allan Stephens0c3141e2008-04-15 00:22:02 -0700509 if (iocb)
510 lock_sock(sk);
511
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512 needs_conn = (sock->state != SS_READY);
513 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700514 if (sock->state == SS_LISTENING) {
515 res = -EPIPE;
516 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700517 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700518 if (sock->state != SS_UNCONNECTED) {
519 res = -EISCONN;
520 goto exit;
521 }
522 if ((tport->published) ||
523 ((sock->type == SOCK_STREAM) && (total_len != 0))) {
524 res = -EOPNOTSUPP;
525 goto exit;
526 }
527 if (dest->addrtype == TIPC_ADDR_NAME) {
528 tport->conn_type = dest->addr.name.name.type;
529 tport->conn_instance = dest->addr.name.name.instance;
530 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531
532 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700533 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534 }
535
Ying Xue1d835872011-07-06 05:53:15 -0400536 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
537
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900538 do {
539 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000540 res = dest_name_check(dest, m);
541 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700542 break;
543 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900544 &dest->addr.name.name,
545 dest->addr.name.domain,
546 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500547 m->msg_iov,
548 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000549 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700550 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900551 &dest->addr.id,
552 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500553 m->msg_iov,
554 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000555 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556 if (needs_conn) {
557 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700558 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 }
Allan Stephens2db99832010-12-31 18:59:33 +0000560 res = dest_name_check(dest, m);
561 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700562 break;
563 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900564 &dest->addr.nameseq,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900565 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500566 m->msg_iov,
567 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900568 }
569 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000570 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700571 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700572 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900573 }
Ying Xue1d835872011-07-06 05:53:15 -0400574 if (timeout_val <= 0L) {
575 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700576 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700578 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400579 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
580 !tport->congested, timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700581 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900582 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700583
584exit:
585 if (iocb)
586 release_sock(sk);
587 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588}
589
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900590/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700592 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 * @sock: socket structure
594 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700595 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900596 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900598 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599 * Returns the number of bytes sent on success, or errno otherwise
600 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100601static int send_packet(struct kiocb *iocb, struct socket *sock,
602 struct msghdr *m, size_t total_len)
603{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700604 struct sock *sk = sock->sk;
605 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900606 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Ying Xue1d835872011-07-06 05:53:15 -0400607 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 int res;
609
610 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 if (unlikely(dest))
612 return send_msg(iocb, sock, m, total_len);
613
Ying Xue97f8b872013-01-31 21:51:47 +0100614 if (total_len > TIPC_MAX_USER_MSG_SIZE)
Allan Stephensc29c3f72010-04-20 17:58:24 -0400615 return -EMSGSIZE;
616
Allan Stephens0c3141e2008-04-15 00:22:02 -0700617 if (iocb)
618 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619
Ying Xue1d835872011-07-06 05:53:15 -0400620 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
621
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900622 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700623 if (unlikely(sock->state != SS_CONNECTED)) {
624 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900625 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700626 else
627 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700628 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700629 }
630
Allan Stephens26896902011-04-21 10:42:07 -0500631 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov,
632 total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000633 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700634 break;
Ying Xue1d835872011-07-06 05:53:15 -0400635 if (timeout_val <= 0L) {
636 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700637 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700639 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400640 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
641 (!tport->congested || !tport->connected), timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700642 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900643 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700644
645 if (iocb)
646 release_sock(sk);
647 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648}
649
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900650/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 * send_stream - send stream-oriented data
652 * @iocb: (unused)
653 * @sock: socket structure
654 * @m: data to send
655 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900656 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900658 *
659 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700660 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100662static int send_stream(struct kiocb *iocb, struct socket *sock,
663 struct msghdr *m, size_t total_len)
664{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700665 struct sock *sk = sock->sk;
666 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667 struct msghdr my_msg;
668 struct iovec my_iov;
669 struct iovec *curr_iov;
670 int curr_iovlen;
671 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700672 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673 int curr_left;
674 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700675 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900677
Allan Stephens0c3141e2008-04-15 00:22:02 -0700678 lock_sock(sk);
679
Allan Stephens05646c92007-06-10 17:25:24 -0700680 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900681 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700682 if (sock->state == SS_UNCONNECTED) {
683 res = send_packet(NULL, sock, m, total_len);
684 goto exit;
685 } else if (sock->state == SS_DISCONNECTING) {
686 res = -EPIPE;
687 goto exit;
688 } else {
689 res = -ENOTCONN;
690 goto exit;
691 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900692 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693
Allan Stephens0c3141e2008-04-15 00:22:02 -0700694 if (unlikely(m->msg_name)) {
695 res = -EISCONN;
696 goto exit;
697 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700698
Ying Xue97f8b872013-01-31 21:51:47 +0100699 if (total_len > (unsigned int)INT_MAX) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400700 res = -EMSGSIZE;
701 goto exit;
702 }
703
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900704 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100705 * Send each iovec entry using one or more messages
706 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900707 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708 * (i.e. one large iovec entry), but could be improved to pass sets
709 * of small iovec entries into send_packet().
710 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700711 curr_iov = m->msg_iov;
712 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100713 my_msg.msg_iov = &my_iov;
714 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700715 my_msg.msg_flags = m->msg_flags;
716 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700717 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100718
Allan Stephens05646c92007-06-10 17:25:24 -0700719 hdr_size = msg_hdr_sz(&tport->phdr);
720
Per Lidenb97bf3f2006-01-02 19:04:38 +0100721 while (curr_iovlen--) {
722 curr_start = curr_iov->iov_base;
723 curr_left = curr_iov->iov_len;
724
725 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700726 bytes_to_send = tport->max_pkt - hdr_size;
727 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
728 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
729 if (curr_left < bytes_to_send)
730 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731 my_iov.iov_base = curr_start;
732 my_iov.iov_len = bytes_to_send;
Allan Stephens26896902011-04-21 10:42:07 -0500733 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000734 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700735 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700736 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700737 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700738 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100739 curr_left -= bytes_to_send;
740 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700741 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742 }
743
744 curr_iov++;
745 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700746 res = bytes_sent;
747exit:
748 release_sock(sk);
749 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750}
751
752/**
753 * auto_connect - complete connection setup to a remote port
754 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900756 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100757 * Returns 0 on success, errno otherwise
758 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700759static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760{
Allan Stephens2da59912008-07-14 22:43:32 -0700761 struct tipc_sock *tsock = tipc_sk(sock->sk);
Ying Xue584d24b2012-11-29 18:51:19 -0500762 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763
Allan Stephens2da59912008-07-14 22:43:32 -0700764 tsock->peer_name.ref = msg_origport(msg);
765 tsock->peer_name.node = msg_orignode(msg);
Ying Xue584d24b2012-11-29 18:51:19 -0500766 p_ptr = tipc_port_deref(tsock->p->ref);
767 if (!p_ptr)
768 return -EINVAL;
769
770 __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
771
772 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
773 return -EINVAL;
774 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775 sock->state = SS_CONNECTED;
776 return 0;
777}
778
779/**
780 * set_orig_addr - capture sender's address for received message
781 * @m: descriptor for message info
782 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900783 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100784 * Note: Address is not captured if not requested by receiver.
785 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800786static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100787{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900788 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900790 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791 addr->family = AF_TIPC;
792 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +0000793 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794 addr->addr.id.ref = msg_origport(msg);
795 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000796 addr->addr.name.domain = 0; /* could leave uninitialized */
797 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798 m->msg_namelen = sizeof(struct sockaddr_tipc);
799 }
800}
801
802/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900803 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804 * @m: descriptor for message info
805 * @msg: received message header
806 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900807 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900809 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810 * Returns 0 if successful, otherwise errno
811 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800812static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813 struct tipc_port *tport)
814{
815 u32 anc_data[3];
816 u32 err;
817 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700818 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100819 int res;
820
821 if (likely(m->msg_controllen == 0))
822 return 0;
823
824 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825 err = msg ? msg_errcode(msg) : 0;
826 if (unlikely(err)) {
827 anc_data[0] = err;
828 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000829 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
830 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100831 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000832 if (anc_data[1]) {
833 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
834 msg_data(msg));
835 if (res)
836 return res;
837 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100838 }
839
840 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100841 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
842 switch (dest_type) {
843 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700844 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100845 anc_data[0] = msg_nametype(msg);
846 anc_data[1] = msg_namelower(msg);
847 anc_data[2] = msg_namelower(msg);
848 break;
849 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700850 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 anc_data[0] = msg_nametype(msg);
852 anc_data[1] = msg_namelower(msg);
853 anc_data[2] = msg_nameupper(msg);
854 break;
855 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700856 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857 anc_data[0] = tport->conn_type;
858 anc_data[1] = tport->conn_instance;
859 anc_data[2] = tport->conn_instance;
860 break;
861 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700862 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863 }
Allan Stephens2db99832010-12-31 18:59:33 +0000864 if (has_name) {
865 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
866 if (res)
867 return res;
868 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100869
870 return 0;
871}
872
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900873/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100874 * recv_msg - receive packet-oriented message
875 * @iocb: (unused)
876 * @m: descriptor for message info
877 * @buf_len: total size of user buffer area
878 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900879 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
881 * If the complete message doesn't fit in user area, truncate it.
882 *
883 * Returns size of returned message data, errno otherwise
884 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885static int recv_msg(struct kiocb *iocb, struct socket *sock,
886 struct msghdr *m, size_t buf_len, int flags)
887{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700888 struct sock *sk = sock->sk;
889 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100890 struct sk_buff *buf;
891 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500892 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893 unsigned int sz;
894 u32 err;
895 int res;
896
Allan Stephens0c3141e2008-04-15 00:22:02 -0700897 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100898 if (unlikely(!buf_len))
899 return -EINVAL;
900
Allan Stephens0c3141e2008-04-15 00:22:02 -0700901 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902
Allan Stephens0c3141e2008-04-15 00:22:02 -0700903 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904 res = -ENOTCONN;
905 goto exit;
906 }
907
Allan Stephens71092ea2011-02-23 14:52:14 -0500908 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700909restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910
Allan Stephens0c3141e2008-04-15 00:22:02 -0700911 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700912 while (skb_queue_empty(&sk->sk_receive_queue)) {
913 if (sock->state == SS_DISCONNECTING) {
914 res = -ENOTCONN;
915 goto exit;
916 }
Allan Stephens71092ea2011-02-23 14:52:14 -0500917 if (timeout <= 0L) {
918 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700919 goto exit;
920 }
921 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -0500922 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
923 tipc_rx_ready(sock),
924 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700925 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700926 }
927
928 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700929 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100930 msg = buf_msg(buf);
931 sz = msg_data_sz(msg);
932 err = msg_errcode(msg);
933
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100935 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700936 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100937 goto restart;
938 }
939
940 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941 set_orig_addr(m, msg);
942
943 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700944 res = anc_data_recv(m, msg, tport);
945 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946 goto exit;
947
948 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100949 if (!err) {
950 if (unlikely(buf_len < sz)) {
951 sz = buf_len;
952 m->msg_flags |= MSG_TRUNC;
953 }
Allan Stephens0232fd02011-02-21 09:45:40 -0500954 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
955 m->msg_iov, sz);
956 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100957 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958 res = sz;
959 } else {
960 if ((sock->state == SS_READY) ||
961 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
962 res = 0;
963 else
964 res = -ECONNRESET;
965 }
966
967 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -0700969 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -0700970 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
971 tipc_acknowledge(tport->ref, tport->conn_unacked);
972 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900973 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700975 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976 return res;
977}
978
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900979/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980 * recv_stream - receive stream-oriented data
981 * @iocb: (unused)
982 * @m: descriptor for message info
983 * @buf_len: total size of user buffer area
984 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900985 *
986 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +0100987 * will optionally wait for more; never truncates data.
988 *
989 * Returns size of returned message data, errno otherwise
990 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991static int recv_stream(struct kiocb *iocb, struct socket *sock,
992 struct msghdr *m, size_t buf_len, int flags)
993{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700994 struct sock *sk = sock->sk;
995 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996 struct sk_buff *buf;
997 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500998 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100999 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001000 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001001 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001003 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001004
1005 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006 if (unlikely(!buf_len))
1007 return -EINVAL;
1008
Allan Stephens0c3141e2008-04-15 00:22:02 -07001009 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010
Allan Stephens0c3141e2008-04-15 00:22:02 -07001011 if (unlikely((sock->state == SS_UNCONNECTED) ||
1012 (sock->state == SS_CONNECTING))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 res = -ENOTCONN;
1014 goto exit;
1015 }
1016
Florian Westphal3720d402010-08-17 11:00:04 +00001017 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Allan Stephens71092ea2011-02-23 14:52:14 -05001018 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001019
Allan Stephens0c3141e2008-04-15 00:22:02 -07001020restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001021 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001022 while (skb_queue_empty(&sk->sk_receive_queue)) {
1023 if (sock->state == SS_DISCONNECTING) {
1024 res = -ENOTCONN;
1025 goto exit;
1026 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001027 if (timeout <= 0L) {
1028 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001029 goto exit;
1030 }
1031 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001032 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1033 tipc_rx_ready(sock),
1034 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001035 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001036 }
1037
1038 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001039 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 msg = buf_msg(buf);
1041 sz = msg_data_sz(msg);
1042 err = msg_errcode(msg);
1043
1044 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001046 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001047 goto restart;
1048 }
1049
1050 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001051 if (sz_copied == 0) {
1052 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001053 res = anc_data_recv(m, msg, tport);
1054 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 goto exit;
1056 }
1057
1058 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001060 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061
Allan Stephens0232fd02011-02-21 09:45:40 -05001062 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001063 needed = (buf_len - sz_copied);
1064 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001065
1066 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1067 m->msg_iov, sz_to_copy);
1068 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001070
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 sz_copied += sz_to_copy;
1072
1073 if (sz_to_copy < sz) {
1074 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001075 TIPC_SKB_CB(buf)->handle =
1076 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077 goto exit;
1078 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001079 } else {
1080 if (sz_copied != 0)
1081 goto exit; /* can't add error msg to valid data */
1082
1083 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1084 res = 0;
1085 else
1086 res = -ECONNRESET;
1087 }
1088
1089 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001091 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1092 tipc_acknowledge(tport->ref, tport->conn_unacked);
1093 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001094 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001095
1096 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001097 if ((sz_copied < buf_len) && /* didn't get all requested data */
1098 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001099 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001100 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1101 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001102 goto restart;
1103
1104exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001105 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001106 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001107}
1108
1109/**
Ying Xuef288bef2012-08-21 11:16:57 +08001110 * tipc_write_space - wake up thread if port congestion is released
1111 * @sk: socket
1112 */
1113static void tipc_write_space(struct sock *sk)
1114{
1115 struct socket_wq *wq;
1116
1117 rcu_read_lock();
1118 wq = rcu_dereference(sk->sk_wq);
1119 if (wq_has_sleeper(wq))
1120 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1121 POLLWRNORM | POLLWRBAND);
1122 rcu_read_unlock();
1123}
1124
1125/**
1126 * tipc_data_ready - wake up threads to indicate messages have been received
1127 * @sk: socket
1128 * @len: the length of messages
1129 */
1130static void tipc_data_ready(struct sock *sk, int len)
1131{
1132 struct socket_wq *wq;
1133
1134 rcu_read_lock();
1135 wq = rcu_dereference(sk->sk_wq);
1136 if (wq_has_sleeper(wq))
1137 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1138 POLLRDNORM | POLLRDBAND);
1139 rcu_read_unlock();
1140}
1141
1142/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001143 * filter_connect - Handle all incoming messages for a connection-based socket
1144 * @tsock: TIPC socket
1145 * @msg: message
1146 *
1147 * Returns TIPC error status code and socket error status code
1148 * once it encounters some errors
1149 */
1150static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
1151{
1152 struct socket *sock = tsock->sk.sk_socket;
1153 struct tipc_msg *msg = buf_msg(*buf);
Ying Xue584d24b2012-11-29 18:51:19 -05001154 struct sock *sk = &tsock->sk;
Ying Xue7e6c1312012-11-29 18:39:14 -05001155 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001156 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001157
1158 if (msg_mcast(msg))
1159 return retval;
1160
1161 switch ((int)sock->state) {
1162 case SS_CONNECTED:
1163 /* Accept only connection-based messages sent by peer */
1164 if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
1165 if (unlikely(msg_errcode(msg))) {
1166 sock->state = SS_DISCONNECTING;
1167 __tipc_disconnect(tsock->p);
1168 }
1169 retval = TIPC_OK;
1170 }
1171 break;
1172 case SS_CONNECTING:
1173 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001174 if (unlikely(msg_errcode(msg))) {
1175 sock->state = SS_DISCONNECTING;
Erik Hugne8db07b82013-08-28 09:29:58 +02001176 sk->sk_err = ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001177 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001178 break;
1179 }
1180
1181 if (unlikely(!msg_connected(msg)))
1182 break;
1183
1184 res = auto_connect(sock, msg);
1185 if (res) {
1186 sock->state = SS_DISCONNECTING;
Erik Hugne8db07b82013-08-28 09:29:58 +02001187 sk->sk_err = -res;
Ying Xue584d24b2012-11-29 18:51:19 -05001188 retval = TIPC_OK;
1189 break;
1190 }
1191
1192 /* If an incoming message is an 'ACK-', it should be
1193 * discarded here because it doesn't contain useful
1194 * data. In addition, we should try to wake up
1195 * connect() routine if sleeping.
1196 */
1197 if (msg_data_sz(msg) == 0) {
1198 kfree_skb(*buf);
1199 *buf = NULL;
1200 if (waitqueue_active(sk_sleep(sk)))
1201 wake_up_interruptible(sk_sleep(sk));
1202 }
1203 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001204 break;
1205 case SS_LISTENING:
1206 case SS_UNCONNECTED:
1207 /* Accept only SYN message */
1208 if (!msg_connected(msg) && !(msg_errcode(msg)))
1209 retval = TIPC_OK;
1210 break;
1211 case SS_DISCONNECTING:
1212 break;
1213 default:
1214 pr_err("Unknown socket state %u\n", sock->state);
1215 }
1216 return retval;
1217}
1218
1219/**
Ying Xueaba79f32013-01-20 23:30:09 +01001220 * rcvbuf_limit - get proper overload limit of socket receive queue
1221 * @sk: socket
1222 * @buf: message
1223 *
1224 * For all connection oriented messages, irrespective of importance,
1225 * the default overload value (i.e. 67MB) is set as limit.
1226 *
1227 * For all connectionless messages, by default new queue limits are
1228 * as belows:
1229 *
1230 * TIPC_LOW_IMPORTANCE (5MB)
1231 * TIPC_MEDIUM_IMPORTANCE (10MB)
1232 * TIPC_HIGH_IMPORTANCE (20MB)
1233 * TIPC_CRITICAL_IMPORTANCE (40MB)
1234 *
1235 * Returns overload limit according to corresponding message importance
1236 */
1237static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1238{
1239 struct tipc_msg *msg = buf_msg(buf);
1240 unsigned int limit;
1241
1242 if (msg_connected(msg))
1243 limit = CONN_OVERLOAD_LIMIT;
1244 else
1245 limit = sk->sk_rcvbuf << (msg_importance(msg) + 5);
1246 return limit;
1247}
1248
1249/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001250 * filter_rcv - validate incoming message
1251 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001253 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001254 * Enqueues message on receive queue if acceptable; optionally handles
1255 * disconnect indication for a connected socket.
1256 *
1257 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001258 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1260 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001261static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001263 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001265 unsigned int limit = rcvbuf_limit(sk, buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001266 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001267
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001269 if (msg_type(msg) > TIPC_DIRECT_MSG)
1270 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001271
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001273 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001274 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001275 } else {
Ying Xue7e6c1312012-11-29 18:39:14 -05001276 res = filter_connect(tipc_sk(sk), &buf);
1277 if (res != TIPC_OK || buf == NULL)
1278 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279 }
1280
1281 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001282 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1283 return TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001284
Ying Xueaba79f32013-01-20 23:30:09 +01001285 /* Enqueue message */
Allan Stephens0232fd02011-02-21 09:45:40 -05001286 TIPC_SKB_CB(buf)->handle = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001287 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001288 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001289
Ying Xuef288bef2012-08-21 11:16:57 +08001290 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001291 return TIPC_OK;
1292}
1293
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001294/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001295 * backlog_rcv - handle incoming message from backlog queue
1296 * @sk: socket
1297 * @buf: message
1298 *
1299 * Caller must hold socket lock, but not port lock.
1300 *
1301 * Returns 0
1302 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001303static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1304{
1305 u32 res;
1306
1307 res = filter_rcv(sk, buf);
1308 if (res)
1309 tipc_reject_msg(buf, res);
1310 return 0;
1311}
1312
1313/**
1314 * dispatch - handle incoming message
1315 * @tport: TIPC port that received message
1316 * @buf: message
1317 *
1318 * Called with port lock already taken.
1319 *
1320 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1321 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001322static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1323{
1324 struct sock *sk = (struct sock *)tport->usr_handle;
1325 u32 res;
1326
1327 /*
1328 * Process message if socket is unlocked; otherwise add to backlog queue
1329 *
1330 * This code is based on sk_receive_skb(), but must be distinct from it
1331 * since a TIPC-specific filter/reject mechanism is utilized
1332 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001333 bh_lock_sock(sk);
1334 if (!sock_owned_by_user(sk)) {
1335 res = filter_rcv(sk, buf);
1336 } else {
Ying Xueaba79f32013-01-20 23:30:09 +01001337 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
Zhu Yi53eecb12010-03-04 18:01:45 +00001338 res = TIPC_ERR_OVERLOAD;
1339 else
1340 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001341 }
1342 bh_unlock_sock(sk);
1343
1344 return res;
1345}
1346
1347/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001348 * wakeupdispatch - wake up port after congestion
1349 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001350 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001351 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001352 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353static void wakeupdispatch(struct tipc_port *tport)
1354{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001355 struct sock *sk = (struct sock *)tport->usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001356
Ying Xuef288bef2012-08-21 11:16:57 +08001357 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358}
1359
1360/**
1361 * connect - establish a connection to another TIPC port
1362 * @sock: socket structure
1363 * @dest: socket address for destination port
1364 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001365 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366 *
1367 * Returns 0 on success, errno otherwise
1368 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001369static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370 int flags)
1371{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001372 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001373 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1374 struct msghdr m = {NULL,};
Allan Stephensa0f40f02011-05-26 13:44:34 -04001375 unsigned int timeout;
Allan Stephensb89741a2008-04-15 00:20:37 -07001376 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377
Allan Stephens0c3141e2008-04-15 00:22:02 -07001378 lock_sock(sk);
1379
Allan Stephensb89741a2008-04-15 00:20:37 -07001380 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001381 if (sock->state == SS_READY) {
1382 res = -EOPNOTSUPP;
1383 goto exit;
1384 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385
Allan Stephensb89741a2008-04-15 00:20:37 -07001386 /*
1387 * Reject connection attempt using multicast address
1388 *
1389 * Note: send_msg() validates the rest of the address fields,
1390 * so there's no need to do it here
1391 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001392 if (dst->addrtype == TIPC_ADDR_MCAST) {
1393 res = -EINVAL;
1394 goto exit;
1395 }
1396
Ying Xue584d24b2012-11-29 18:51:19 -05001397 timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398
Ying Xue584d24b2012-11-29 18:51:19 -05001399 switch (sock->state) {
1400 case SS_UNCONNECTED:
1401 /* Send a 'SYN-' to destination */
1402 m.msg_name = dest;
1403 m.msg_namelen = destlen;
1404
1405 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1406 * indicate send_msg() is never blocked.
1407 */
1408 if (!timeout)
1409 m.msg_flags = MSG_DONTWAIT;
1410
1411 res = send_msg(NULL, sock, &m, 0);
1412 if ((res < 0) && (res != -EWOULDBLOCK))
1413 goto exit;
1414
1415 /* Just entered SS_CONNECTING state; the only
1416 * difference is that return value in non-blocking
1417 * case is EINPROGRESS, rather than EALREADY.
1418 */
1419 res = -EINPROGRESS;
1420 break;
1421 case SS_CONNECTING:
1422 res = -EALREADY;
1423 break;
1424 case SS_CONNECTED:
1425 res = -EISCONN;
1426 break;
1427 default:
1428 res = -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001429 goto exit;
Allan Stephensb89741a2008-04-15 00:20:37 -07001430 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001431
Ying Xue584d24b2012-11-29 18:51:19 -05001432 if (sock->state == SS_CONNECTING) {
1433 if (!timeout)
1434 goto exit;
1435
1436 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1437 release_sock(sk);
1438 res = wait_event_interruptible_timeout(*sk_sleep(sk),
1439 sock->state != SS_CONNECTING,
1440 timeout ? (long)msecs_to_jiffies(timeout)
1441 : MAX_SCHEDULE_TIMEOUT);
1442 lock_sock(sk);
1443 if (res <= 0) {
1444 if (res == 0)
1445 res = -ETIMEDOUT;
1446 else
1447 ; /* leave "res" unchanged */
1448 goto exit;
1449 }
1450 }
1451
1452 if (unlikely(sock->state == SS_DISCONNECTING))
1453 res = sock_error(sk);
1454 else
1455 res = 0;
1456
Allan Stephens0c3141e2008-04-15 00:22:02 -07001457exit:
1458 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001459 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001460}
1461
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001462/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463 * listen - allow socket to listen for incoming connections
1464 * @sock: socket structure
1465 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001466 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467 * Returns 0 on success, errno otherwise
1468 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001469static int listen(struct socket *sock, int len)
1470{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001471 struct sock *sk = sock->sk;
1472 int res;
1473
1474 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475
Ying Xue245f3d32011-07-06 06:01:13 -04001476 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001477 res = -EINVAL;
1478 else {
1479 sock->state = SS_LISTENING;
1480 res = 0;
1481 }
1482
1483 release_sock(sk);
1484 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001485}
1486
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001487/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488 * accept - wait for connection request
1489 * @sock: listening socket
1490 * @newsock: new socket that is to be connected
1491 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001492 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001493 * Returns 0 on success, errno otherwise
1494 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001495static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001496{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001497 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001498 struct sk_buff *buf;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001499 struct tipc_sock *new_tsock;
1500 struct tipc_port *new_tport;
1501 struct tipc_msg *msg;
1502 u32 new_ref;
1503
Allan Stephens0c3141e2008-04-15 00:22:02 -07001504 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001505
Allan Stephens0c3141e2008-04-15 00:22:02 -07001506 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001507
Allan Stephens0c3141e2008-04-15 00:22:02 -07001508 if (sock->state != SS_LISTENING) {
1509 res = -EINVAL;
1510 goto exit;
1511 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001512
Allan Stephens0c3141e2008-04-15 00:22:02 -07001513 while (skb_queue_empty(&sk->sk_receive_queue)) {
1514 if (flags & O_NONBLOCK) {
1515 res = -EWOULDBLOCK;
1516 goto exit;
1517 }
1518 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001519 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001520 (!skb_queue_empty(&sk->sk_receive_queue)));
1521 lock_sock(sk);
1522 if (res)
1523 goto exit;
1524 }
1525
1526 buf = skb_peek(&sk->sk_receive_queue);
1527
Eric Paris3f378b62009-11-05 22:18:14 -08001528 res = tipc_create(sock_net(sock->sk), new_sock, 0, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001529 if (res)
1530 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001531
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001532 new_sk = new_sock->sk;
1533 new_tsock = tipc_sk(new_sk);
1534 new_tport = new_tsock->p;
1535 new_ref = new_tport->ref;
1536 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001537
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001538 /* we lock on new_sk; but lockdep sees the lock on sk */
1539 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001540
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001541 /*
1542 * Reject any stray messages received by new socket
1543 * before the socket lock was taken (very, very unlikely)
1544 */
1545 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001546
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001547 /* Connect new socket to it's peer */
1548 new_tsock->peer_name.ref = msg_origport(msg);
1549 new_tsock->peer_name.node = msg_orignode(msg);
1550 tipc_connect(new_ref, &new_tsock->peer_name);
1551 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001552
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001553 tipc_set_portimportance(new_ref, msg_importance(msg));
1554 if (msg_named(msg)) {
1555 new_tport->conn_type = msg_nametype(msg);
1556 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001557 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001558
1559 /*
1560 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1561 * Respond to 'SYN+' by queuing it on new socket.
1562 */
1563 if (!msg_data_sz(msg)) {
1564 struct msghdr m = {NULL,};
1565
1566 advance_rx_queue(sk);
1567 send_packet(NULL, new_sock, &m, 0);
1568 } else {
1569 __skb_dequeue(&sk->sk_receive_queue);
1570 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001571 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001572 }
1573 release_sock(new_sk);
1574
Per Lidenb97bf3f2006-01-02 19:04:38 +01001575exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001576 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001577 return res;
1578}
1579
1580/**
1581 * shutdown - shutdown socket connection
1582 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001583 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001584 *
1585 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001586 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001587 * Returns 0 on success, errno otherwise
1588 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001589static int shutdown(struct socket *sock, int how)
1590{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001591 struct sock *sk = sock->sk;
1592 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001593 struct sk_buff *buf;
1594 int res;
1595
Allan Stephense247a8f2008-03-06 15:05:38 -08001596 if (how != SHUT_RDWR)
1597 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001598
Allan Stephens0c3141e2008-04-15 00:22:02 -07001599 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600
1601 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001602 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001603 case SS_CONNECTED:
1604
Per Lidenb97bf3f2006-01-02 19:04:38 +01001605restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001606 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001607 buf = __skb_dequeue(&sk->sk_receive_queue);
1608 if (buf) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001609 if (TIPC_SKB_CB(buf)->handle != 0) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001610 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001611 goto restart;
1612 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001613 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001614 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001615 } else {
1616 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001617 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001618
1619 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001620
1621 /* fall through */
1622
1623 case SS_DISCONNECTING:
1624
Ying Xue75031152012-10-29 09:38:15 -04001625 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001626 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001627
1628 /* Wake up anyone sleeping in poll */
1629 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630 res = 0;
1631 break;
1632
1633 default:
1634 res = -ENOTCONN;
1635 }
1636
Allan Stephens0c3141e2008-04-15 00:22:02 -07001637 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001638 return res;
1639}
1640
1641/**
1642 * setsockopt - set socket option
1643 * @sock: socket structure
1644 * @lvl: option level
1645 * @opt: option identifier
1646 * @ov: pointer to new option value
1647 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001648 *
1649 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001650 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001651 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652 * Returns 0 on success, errno otherwise
1653 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001654static int setsockopt(struct socket *sock,
David S. Millerb7058842009-09-30 16:12:20 -07001655 int lvl, int opt, char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001657 struct sock *sk = sock->sk;
1658 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001659 u32 value;
1660 int res;
1661
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001662 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1663 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001664 if (lvl != SOL_TIPC)
1665 return -ENOPROTOOPT;
1666 if (ol < sizeof(value))
1667 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001668 res = get_user(value, (u32 __user *)ov);
1669 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670 return res;
1671
Allan Stephens0c3141e2008-04-15 00:22:02 -07001672 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001673
Per Lidenb97bf3f2006-01-02 19:04:38 +01001674 switch (opt) {
1675 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001676 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001677 break;
1678 case TIPC_SRC_DROPPABLE:
1679 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001680 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001681 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001682 res = -ENOPROTOOPT;
1683 break;
1684 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001685 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001686 break;
1687 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001688 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001689 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690 break;
1691 default:
1692 res = -EINVAL;
1693 }
1694
Allan Stephens0c3141e2008-04-15 00:22:02 -07001695 release_sock(sk);
1696
Per Lidenb97bf3f2006-01-02 19:04:38 +01001697 return res;
1698}
1699
1700/**
1701 * getsockopt - get socket option
1702 * @sock: socket structure
1703 * @lvl: option level
1704 * @opt: option identifier
1705 * @ov: receptacle for option value
1706 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001707 *
1708 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001709 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001710 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001711 * Returns 0 on success, errno otherwise
1712 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001713static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001714 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001715{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001716 struct sock *sk = sock->sk;
1717 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001718 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001720 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001721
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001722 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1723 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001724 if (lvl != SOL_TIPC)
1725 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001726 res = get_user(len, ol);
1727 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001728 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001729
Allan Stephens0c3141e2008-04-15 00:22:02 -07001730 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001731
1732 switch (opt) {
1733 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001734 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001735 break;
1736 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001737 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001738 break;
1739 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001740 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741 break;
1742 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001743 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001744 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001745 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001746 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001747 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001748 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001749 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001750 value = skb_queue_len(&sk->sk_receive_queue);
1751 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001752 default:
1753 res = -EINVAL;
1754 }
1755
Allan Stephens0c3141e2008-04-15 00:22:02 -07001756 release_sock(sk);
1757
Paul Gortmaker25860c32010-12-31 18:59:31 +00001758 if (res)
1759 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001760
Paul Gortmaker25860c32010-12-31 18:59:31 +00001761 if (len < sizeof(value))
1762 return -EINVAL;
1763
1764 if (copy_to_user(ov, &value, sizeof(value)))
1765 return -EFAULT;
1766
1767 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001768}
1769
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001770/* Protocol switches for the various types of TIPC sockets */
1771
Florian Westphalbca65ea2008-02-07 18:18:01 -08001772static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001773 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774 .family = AF_TIPC,
1775 .release = release,
1776 .bind = bind,
1777 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001778 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001779 .accept = sock_no_accept,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001780 .getname = get_name,
1781 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001782 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001783 .listen = sock_no_listen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001784 .shutdown = shutdown,
1785 .setsockopt = setsockopt,
1786 .getsockopt = getsockopt,
1787 .sendmsg = send_msg,
1788 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001789 .mmap = sock_no_mmap,
1790 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001791};
1792
Florian Westphalbca65ea2008-02-07 18:18:01 -08001793static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001794 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001795 .family = AF_TIPC,
1796 .release = release,
1797 .bind = bind,
1798 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001799 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800 .accept = accept,
1801 .getname = get_name,
1802 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001803 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001804 .listen = listen,
1805 .shutdown = shutdown,
1806 .setsockopt = setsockopt,
1807 .getsockopt = getsockopt,
1808 .sendmsg = send_packet,
1809 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001810 .mmap = sock_no_mmap,
1811 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001812};
1813
Florian Westphalbca65ea2008-02-07 18:18:01 -08001814static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001815 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001816 .family = AF_TIPC,
1817 .release = release,
1818 .bind = bind,
1819 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001820 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001821 .accept = accept,
1822 .getname = get_name,
1823 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001824 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001825 .listen = listen,
1826 .shutdown = shutdown,
1827 .setsockopt = setsockopt,
1828 .getsockopt = getsockopt,
1829 .sendmsg = send_stream,
1830 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001831 .mmap = sock_no_mmap,
1832 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001833};
1834
Florian Westphalbca65ea2008-02-07 18:18:01 -08001835static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001836 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001837 .family = AF_TIPC,
1838 .create = tipc_create
1839};
1840
1841static struct proto tipc_proto = {
1842 .name = "TIPC",
1843 .owner = THIS_MODULE,
1844 .obj_size = sizeof(struct tipc_sock)
1845};
1846
1847/**
Per Liden4323add2006-01-18 00:38:21 +01001848 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001849 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001850 * Returns 0 on success, errno otherwise
1851 */
Per Liden4323add2006-01-18 00:38:21 +01001852int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001853{
1854 int res;
1855
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001856 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001857 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001858 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001859 goto out;
1860 }
1861
1862 res = sock_register(&tipc_family_ops);
1863 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001864 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001865 proto_unregister(&tipc_proto);
1866 goto out;
1867 }
1868
1869 sockets_enabled = 1;
1870 out:
1871 return res;
1872}
1873
1874/**
Per Liden4323add2006-01-18 00:38:21 +01001875 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001876 */
Per Liden4323add2006-01-18 00:38:21 +01001877void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001878{
1879 if (!sockets_enabled)
1880 return;
1881
1882 sockets_enabled = 0;
1883 sock_unregister(tipc_family_ops.family);
1884 proto_unregister(&tipc_proto);
1885}