blob: dbce2745f0a8ab52bbc037eba771054778287096 [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
Jon Maloye643df12012-11-27 06:15:29 -050046#define OVERLOAD_LIMIT_BASE 10000
Allan Stephens3654ea02008-04-13 21:35:11 -070047#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010048
49struct tipc_sock {
50 struct sock sk;
51 struct tipc_port *p;
Allan Stephens2da59912008-07-14 22:43:32 -070052 struct tipc_portid peer_name;
Allan Stephensa0f40f02011-05-26 13:44:34 -040053 unsigned int conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +010054};
55
Allan Stephens0c3141e2008-04-15 00:22:02 -070056#define tipc_sk(sk) ((struct tipc_sock *)(sk))
Joe Perchese3192692012-06-03 17:41:40 +000057#define tipc_sk_port(sk) (tipc_sk(sk)->p)
Per Lidenb97bf3f2006-01-02 19:04:38 +010058
Allan Stephens71092ea2011-02-23 14:52:14 -050059#define tipc_rx_ready(sock) (!skb_queue_empty(&sock->sk->sk_receive_queue) || \
60 (sock->state == SS_DISCONNECTING))
61
Allan Stephens0c3141e2008-04-15 00:22:02 -070062static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +010063static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
64static void wakeupdispatch(struct tipc_port *tport);
Ying Xuef288bef2012-08-21 11:16:57 +080065static void tipc_data_ready(struct sock *sk, int len);
66static void tipc_write_space(struct sock *sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +010067
Florian Westphalbca65ea2008-02-07 18:18:01 -080068static const struct proto_ops packet_ops;
69static const struct proto_ops stream_ops;
70static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010071
72static struct proto tipc_proto;
73
Allan Stephense3ec9c72010-12-31 18:59:34 +000074static int sockets_enabled;
Per Lidenb97bf3f2006-01-02 19:04:38 +010075
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090076/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070077 * Revised TIPC socket locking policy:
78 *
79 * Most socket operations take the standard socket lock when they start
80 * and hold it until they finish (or until they need to sleep). Acquiring
81 * this lock grants the owner exclusive access to the fields of the socket
82 * data structures, with the exception of the backlog queue. A few socket
83 * operations can be done without taking the socket lock because they only
84 * read socket information that never changes during the life of the socket.
85 *
86 * Socket operations may acquire the lock for the associated TIPC port if they
87 * need to perform an operation on the port. If any routine needs to acquire
88 * both the socket lock and the port lock it must take the socket lock first
89 * to avoid the risk of deadlock.
90 *
91 * The dispatcher handling incoming messages cannot grab the socket lock in
92 * the standard fashion, since invoked it runs at the BH level and cannot block.
93 * Instead, it checks to see if the socket lock is currently owned by someone,
94 * and either handles the message itself or adds it to the socket's backlog
95 * queue; in the latter case the queued message is processed once the process
96 * owning the socket lock releases it.
97 *
98 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
99 * the problem of a blocked socket operation preventing any other operations
100 * from occurring. However, applications must be careful if they have
101 * multiple threads trying to send (or receive) on the same socket, as these
102 * operations might interfere with each other. For example, doing a connect
103 * and a receive at the same time might allow the receive to consume the
104 * ACK message meant for the connect. While additional work could be done
105 * to try and overcome this, it doesn't seem to be worthwhile at the present.
106 *
107 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
108 * that another operation that must be performed in a non-blocking manner is
109 * not delayed for very long because the lock has already been taken.
110 *
111 * NOTE: This code assumes that certain fields of a port/socket pair are
112 * constant over its lifetime; such fields can be examined without taking
113 * the socket lock and/or port lock, and do not need to be re-read even
114 * after resuming processing after waiting. These fields include:
115 * - socket type
116 * - pointer to socket sk structure (aka tipc_sock structure)
117 * - pointer to port structure
118 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120
121/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700122 * advance_rx_queue - discard first buffer in socket receive queue
123 *
124 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700126static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400128 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129}
130
131/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700132 * discard_rx_queue - discard all buffers in socket receive queue
133 *
134 * Caller must hold socket lock
135 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700136static void discard_rx_queue(struct sock *sk)
137{
138 struct sk_buff *buf;
139
Ying Xue9da3d472012-11-27 06:15:27 -0500140 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens5f6d9122011-11-04 13:24:29 -0400141 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700142}
143
144/**
145 * reject_rx_queue - reject all buffers in socket receive queue
146 *
147 * Caller must hold socket lock
148 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700149static void reject_rx_queue(struct sock *sk)
150{
151 struct sk_buff *buf;
152
Ying Xue9da3d472012-11-27 06:15:27 -0500153 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700154 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700155}
156
157/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158 * tipc_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700159 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 * @sock: pre-allocated socket structure
161 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800162 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900163 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700164 * This routine creates additional data structures used by the TIPC socket,
165 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 *
167 * Returns 0 on success, errno otherwise
168 */
Eric Paris3f378b62009-11-05 22:18:14 -0800169static int tipc_create(struct net *net, struct socket *sock, int protocol,
170 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700172 const struct proto_ops *ops;
173 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700175 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700176
177 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 if (unlikely(protocol != 0))
179 return -EPROTONOSUPPORT;
180
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 switch (sock->type) {
182 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700183 ops = &stream_ops;
184 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185 break;
186 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700187 ops = &packet_ops;
188 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100189 break;
190 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700192 ops = &msg_ops;
193 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194 break;
Allan Stephens49978652006-06-25 23:47:18 -0700195 default:
Allan Stephens49978652006-06-25 23:47:18 -0700196 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197 }
198
Allan Stephens0c3141e2008-04-15 00:22:02 -0700199 /* Allocate socket's protocol area */
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700200 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700201 if (sk == NULL)
202 return -ENOMEM;
203
204 /* Allocate TIPC port for socket to use */
Allan Stephens0ea52242008-07-14 22:42:19 -0700205 tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
206 TIPC_LOW_IMPORTANCE);
207 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700208 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209 return -ENOMEM;
210 }
211
Allan Stephens0c3141e2008-04-15 00:22:02 -0700212 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700213 sock->ops = ops;
214 sock->state = state;
215
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700217 sk->sk_backlog_rcv = backlog_rcv;
Erik Hugnee57edf62012-10-04 05:00:43 +0000218 sk->sk_rcvbuf = TIPC_FLOW_CONTROL_WIN * 2 * TIPC_MAX_USER_MSG_SIZE * 2;
Ying Xuef288bef2012-08-21 11:16:57 +0800219 sk->sk_data_ready = tipc_data_ready;
220 sk->sk_write_space = tipc_write_space;
Allan Stephens0ea52242008-07-14 22:42:19 -0700221 tipc_sk(sk)->p = tp_ptr;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400222 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700224 spin_unlock_bh(tp_ptr->lock);
225
Allan Stephens0c3141e2008-04-15 00:22:02 -0700226 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700227 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700228 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700229 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700230 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 return 0;
233}
234
235/**
236 * release - destroy a TIPC socket
237 * @sock: socket to destroy
238 *
239 * This routine cleans up any messages that are still queued on the socket.
240 * For DGRAM and RDM socket types, all queued messages are rejected.
241 * For SEQPACKET and STREAM socket types, the first message is rejected
242 * and any others are discarded. (If the first message on a STREAM socket
243 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900244 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245 * NOTE: Rejected messages are not necessarily returned to the sender! They
246 * are returned or discarded according to the "destination droppable" setting
247 * specified for the message by the sender.
248 *
249 * Returns 0 on success, errno otherwise
250 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251static int release(struct socket *sock)
252{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700254 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700256 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257
Allan Stephens0c3141e2008-04-15 00:22:02 -0700258 /*
259 * Exit if socket isn't fully initialized (occurs when a failed accept()
260 * releases a pre-allocated child socket that was never used)
261 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700262 if (sk == NULL)
263 return 0;
264
265 tport = tipc_sk_port(sk);
266 lock_sock(sk);
267
268 /*
269 * Reject all unreceived messages, except on an active connection
270 * (which disconnects locally & sends a 'FIN+' to peer)
271 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700273 buf = __skb_dequeue(&sk->sk_receive_queue);
274 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275 break;
Allan Stephens0232fd02011-02-21 09:45:40 -0500276 if (TIPC_SKB_CB(buf)->handle != 0)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400277 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700278 else {
279 if ((sock->state == SS_CONNECTING) ||
280 (sock->state == SS_CONNECTED)) {
281 sock->state = SS_DISCONNECTING;
282 tipc_disconnect(tport->ref);
283 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700285 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286 }
287
Allan Stephens0c3141e2008-04-15 00:22:02 -0700288 /*
289 * Delete TIPC port; this ensures no more messages are queued
290 * (also disconnects an active connection & sends a 'FIN-' to peer)
291 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700292 res = tipc_deleteport(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293
Allan Stephens0c3141e2008-04-15 00:22:02 -0700294 /* Discard any remaining (connection-based) messages in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700295 discard_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296
Allan Stephens0c3141e2008-04-15 00:22:02 -0700297 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700298 sock->state = SS_DISCONNECTING;
299 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300
301 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700302 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 return res;
305}
306
307/**
308 * bind - associate or disassocate TIPC name(s) with a socket
309 * @sock: socket structure
310 * @uaddr: socket address describing name(s) and desired operation
311 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900312 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313 * Name and name sequence binding is indicated using a positive scope value;
314 * a negative scope value unbinds the specified name. Specifying no name
315 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900316 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700318 *
319 * NOTE: This routine doesn't need to take the socket lock since it doesn't
320 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
323{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700325 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326
Allan Stephens0c3141e2008-04-15 00:22:02 -0700327 if (unlikely(!uaddr_len))
328 return tipc_withdraw(portref, 0, NULL);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900329
Allan Stephens0c3141e2008-04-15 00:22:02 -0700330 if (uaddr_len < sizeof(struct sockaddr_tipc))
331 return -EINVAL;
332 if (addr->family != AF_TIPC)
333 return -EAFNOSUPPORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335 if (addr->addrtype == TIPC_ADDR_NAME)
336 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700337 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
338 return -EAFNOSUPPORT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900339
Allan Stephensc422f1b2011-11-02 15:49:40 -0400340 if (addr->addr.nameseq.type < TIPC_RESERVED_TYPES)
341 return -EACCES;
342
Allan Stephens0c3141e2008-04-15 00:22:02 -0700343 return (addr->scope > 0) ?
344 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
345 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346}
347
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900348/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 * get_name - get port ID of socket or peer socket
350 * @sock: socket structure
351 * @uaddr: area for returned socket address
352 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700353 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900354 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700356 *
Allan Stephens2da59912008-07-14 22:43:32 -0700357 * NOTE: This routine doesn't need to take the socket lock since it only
358 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000359 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900361static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362 int *uaddr_len, int peer)
363{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700365 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000367 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700368 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700369 if ((sock->state != SS_CONNECTED) &&
370 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
371 return -ENOTCONN;
372 addr->addr.id.ref = tsock->peer_name.ref;
373 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700374 } else {
Allan Stephensb924dcf2010-11-30 12:01:03 +0000375 addr->addr.id.ref = tsock->p->ref;
376 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700377 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378
379 *uaddr_len = sizeof(*addr);
380 addr->addrtype = TIPC_ADDR_ID;
381 addr->family = AF_TIPC;
382 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 addr->addr.name.domain = 0;
384
Allan Stephens0c3141e2008-04-15 00:22:02 -0700385 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386}
387
388/**
389 * poll - read and possibly block on pollmask
390 * @file: file structure associated with the socket
391 * @sock: socket for which to calculate the poll bits
392 * @wait: ???
393 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700394 * Returns pollmask value
395 *
396 * COMMENTARY:
397 * It appears that the usual socket locking mechanisms are not useful here
398 * since the pollmask info is potentially out-of-date the moment this routine
399 * exits. TCP and other protocols seem to rely on higher level poll routines
400 * to handle any preventable race conditions, so TIPC will do the same ...
401 *
402 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700403 *
Allan Stephensf662c072010-08-17 11:00:06 +0000404 * socket state flags set
405 * ------------ ---------
406 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200407 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000408 *
409 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
410 * no write flags
411 *
412 * connected POLLIN/POLLRDNORM if data in rx queue
413 * POLLOUT if port is not congested
414 *
415 * disconnecting POLLIN/POLLRDNORM/POLLHUP
416 * no write flags
417 *
418 * listening POLLIN if SYN in rx queue
419 * no write flags
420 *
421 * ready POLLIN/POLLRDNORM if data in rx queue
422 * [connectionless] POLLOUT (since port cannot be congested)
423 *
424 * IMPORTANT: The fact that a read or write operation is indicated does NOT
425 * imply that the operation will succeed, merely that it should be performed
426 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900428static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429 poll_table *wait)
430{
Allan Stephens9b674e82008-03-26 16:48:21 -0700431 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000432 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700433
Ying Xuef288bef2012-08-21 11:16:57 +0800434 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700435
Allan Stephensf662c072010-08-17 11:00:06 +0000436 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200437 case SS_UNCONNECTED:
438 if (!tipc_sk_port(sk)->congested)
439 mask |= POLLOUT;
440 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000441 case SS_READY:
442 case SS_CONNECTED:
443 if (!tipc_sk_port(sk)->congested)
444 mask |= POLLOUT;
445 /* fall thru' */
446 case SS_CONNECTING:
447 case SS_LISTENING:
448 if (!skb_queue_empty(&sk->sk_receive_queue))
449 mask |= (POLLIN | POLLRDNORM);
450 break;
451 case SS_DISCONNECTING:
452 mask = (POLLIN | POLLRDNORM | POLLHUP);
453 break;
454 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700455
456 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457}
458
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900459/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 * dest_name_check - verify user is permitted to send to specified port name
461 * @dest: destination address
462 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900463 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 * Prevents restricted configuration commands from being issued by
465 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900466 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467 * Returns 0 if permission is granted, otherwise errno
468 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800469static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470{
471 struct tipc_cfg_msg_hdr hdr;
472
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900473 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
474 return 0;
475 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
476 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900477 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
478 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479
Allan Stephens3f8dd942011-01-18 13:09:29 -0500480 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
481 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900482 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700484 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900486
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 return 0;
488}
489
490/**
491 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700492 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493 * @sock: socket structure
494 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700495 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900496 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900498 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
500 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900501 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502 * Returns the number of bytes sent on success, or errno otherwise
503 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504static int send_msg(struct kiocb *iocb, struct socket *sock,
505 struct msghdr *m, size_t total_len)
506{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700507 struct sock *sk = sock->sk;
508 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900509 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100510 int needs_conn;
Ying Xue1d835872011-07-06 05:53:15 -0400511 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512 int res = -EINVAL;
513
514 if (unlikely(!dest))
515 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700516 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
517 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518 return -EINVAL;
Allan Stephensc29c3f72010-04-20 17:58:24 -0400519 if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
Eric Dumazet95c96172012-04-15 05:58:06 +0000520 (m->msg_iovlen > (unsigned int)INT_MAX))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400521 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522
Allan Stephens0c3141e2008-04-15 00:22:02 -0700523 if (iocb)
524 lock_sock(sk);
525
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526 needs_conn = (sock->state != SS_READY);
527 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700528 if (sock->state == SS_LISTENING) {
529 res = -EPIPE;
530 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700531 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700532 if (sock->state != SS_UNCONNECTED) {
533 res = -EISCONN;
534 goto exit;
535 }
536 if ((tport->published) ||
537 ((sock->type == SOCK_STREAM) && (total_len != 0))) {
538 res = -EOPNOTSUPP;
539 goto exit;
540 }
541 if (dest->addrtype == TIPC_ADDR_NAME) {
542 tport->conn_type = dest->addr.name.name.type;
543 tport->conn_instance = dest->addr.name.name.instance;
544 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545
546 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700547 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548 }
549
Ying Xue1d835872011-07-06 05:53:15 -0400550 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
551
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900552 do {
553 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000554 res = dest_name_check(dest, m);
555 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700556 break;
557 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900558 &dest->addr.name.name,
559 dest->addr.name.domain,
560 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500561 m->msg_iov,
562 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000563 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700564 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900565 &dest->addr.id,
566 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500567 m->msg_iov,
568 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000569 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570 if (needs_conn) {
571 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700572 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573 }
Allan Stephens2db99832010-12-31 18:59:33 +0000574 res = dest_name_check(dest, m);
575 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700576 break;
577 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900578 &dest->addr.nameseq,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900579 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500580 m->msg_iov,
581 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900582 }
583 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000584 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700585 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700586 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900587 }
Ying Xue1d835872011-07-06 05:53:15 -0400588 if (timeout_val <= 0L) {
589 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700590 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700592 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400593 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
594 !tport->congested, timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700595 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900596 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700597
598exit:
599 if (iocb)
600 release_sock(sk);
601 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602}
603
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900604/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700606 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 * @sock: socket structure
608 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700609 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900610 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900612 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 * Returns the number of bytes sent on success, or errno otherwise
614 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615static int send_packet(struct kiocb *iocb, struct socket *sock,
616 struct msghdr *m, size_t total_len)
617{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700618 struct sock *sk = sock->sk;
619 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900620 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Ying Xue1d835872011-07-06 05:53:15 -0400621 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622 int res;
623
624 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 if (unlikely(dest))
626 return send_msg(iocb, sock, m, total_len);
627
Allan Stephensc29c3f72010-04-20 17:58:24 -0400628 if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
Eric Dumazet95c96172012-04-15 05:58:06 +0000629 (m->msg_iovlen > (unsigned int)INT_MAX))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400630 return -EMSGSIZE;
631
Allan Stephens0c3141e2008-04-15 00:22:02 -0700632 if (iocb)
633 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634
Ying Xue1d835872011-07-06 05:53:15 -0400635 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
636
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900637 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700638 if (unlikely(sock->state != SS_CONNECTED)) {
639 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900640 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700641 else
642 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700643 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700644 }
645
Allan Stephens26896902011-04-21 10:42:07 -0500646 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov,
647 total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000648 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700649 break;
Ying Xue1d835872011-07-06 05:53:15 -0400650 if (timeout_val <= 0L) {
651 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700652 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700654 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400655 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
656 (!tport->congested || !tport->connected), timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700657 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900658 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700659
660 if (iocb)
661 release_sock(sk);
662 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663}
664
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900665/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 * send_stream - send stream-oriented data
667 * @iocb: (unused)
668 * @sock: socket structure
669 * @m: data to send
670 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900671 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900673 *
674 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700675 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100677static int send_stream(struct kiocb *iocb, struct socket *sock,
678 struct msghdr *m, size_t total_len)
679{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700680 struct sock *sk = sock->sk;
681 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100682 struct msghdr my_msg;
683 struct iovec my_iov;
684 struct iovec *curr_iov;
685 int curr_iovlen;
686 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700687 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100688 int curr_left;
689 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700690 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100691 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900692
Allan Stephens0c3141e2008-04-15 00:22:02 -0700693 lock_sock(sk);
694
Allan Stephens05646c92007-06-10 17:25:24 -0700695 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900696 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700697 if (sock->state == SS_UNCONNECTED) {
698 res = send_packet(NULL, sock, m, total_len);
699 goto exit;
700 } else if (sock->state == SS_DISCONNECTING) {
701 res = -EPIPE;
702 goto exit;
703 } else {
704 res = -ENOTCONN;
705 goto exit;
706 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900707 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708
Allan Stephens0c3141e2008-04-15 00:22:02 -0700709 if (unlikely(m->msg_name)) {
710 res = -EISCONN;
711 goto exit;
712 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700713
Eric Dumazet95c96172012-04-15 05:58:06 +0000714 if ((total_len > (unsigned int)INT_MAX) ||
715 (m->msg_iovlen > (unsigned int)INT_MAX)) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400716 res = -EMSGSIZE;
717 goto exit;
718 }
719
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900720 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100721 * Send each iovec entry using one or more messages
722 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900723 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724 * (i.e. one large iovec entry), but could be improved to pass sets
725 * of small iovec entries into send_packet().
726 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700727 curr_iov = m->msg_iov;
728 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 my_msg.msg_iov = &my_iov;
730 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700731 my_msg.msg_flags = m->msg_flags;
732 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700733 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734
Allan Stephens05646c92007-06-10 17:25:24 -0700735 hdr_size = msg_hdr_sz(&tport->phdr);
736
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737 while (curr_iovlen--) {
738 curr_start = curr_iov->iov_base;
739 curr_left = curr_iov->iov_len;
740
741 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700742 bytes_to_send = tport->max_pkt - hdr_size;
743 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
744 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
745 if (curr_left < bytes_to_send)
746 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747 my_iov.iov_base = curr_start;
748 my_iov.iov_len = bytes_to_send;
Allan Stephens26896902011-04-21 10:42:07 -0500749 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000750 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700751 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700752 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700753 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700754 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 curr_left -= bytes_to_send;
756 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700757 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100758 }
759
760 curr_iov++;
761 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700762 res = bytes_sent;
763exit:
764 release_sock(sk);
765 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766}
767
768/**
769 * auto_connect - complete connection setup to a remote port
770 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900772 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 * Returns 0 on success, errno otherwise
774 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700775static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776{
Allan Stephens2da59912008-07-14 22:43:32 -0700777 struct tipc_sock *tsock = tipc_sk(sock->sk);
Ying Xue584d24b2012-11-29 18:51:19 -0500778 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779
Allan Stephens2da59912008-07-14 22:43:32 -0700780 tsock->peer_name.ref = msg_origport(msg);
781 tsock->peer_name.node = msg_orignode(msg);
Ying Xue584d24b2012-11-29 18:51:19 -0500782 p_ptr = tipc_port_deref(tsock->p->ref);
783 if (!p_ptr)
784 return -EINVAL;
785
786 __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
787
788 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
789 return -EINVAL;
790 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791 sock->state = SS_CONNECTED;
792 return 0;
793}
794
795/**
796 * set_orig_addr - capture sender's address for received message
797 * @m: descriptor for message info
798 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900799 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100800 * Note: Address is not captured if not requested by receiver.
801 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800802static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100803{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900804 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900806 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100807 addr->family = AF_TIPC;
808 addr->addrtype = TIPC_ADDR_ID;
809 addr->addr.id.ref = msg_origport(msg);
810 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000811 addr->addr.name.domain = 0; /* could leave uninitialized */
812 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813 m->msg_namelen = sizeof(struct sockaddr_tipc);
814 }
815}
816
817/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900818 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100819 * @m: descriptor for message info
820 * @msg: received message header
821 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900822 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100823 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900824 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825 * Returns 0 if successful, otherwise errno
826 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800827static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828 struct tipc_port *tport)
829{
830 u32 anc_data[3];
831 u32 err;
832 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700833 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834 int res;
835
836 if (likely(m->msg_controllen == 0))
837 return 0;
838
839 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840 err = msg ? msg_errcode(msg) : 0;
841 if (unlikely(err)) {
842 anc_data[0] = err;
843 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000844 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
845 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000847 if (anc_data[1]) {
848 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
849 msg_data(msg));
850 if (res)
851 return res;
852 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100853 }
854
855 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100856 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
857 switch (dest_type) {
858 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700859 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100860 anc_data[0] = msg_nametype(msg);
861 anc_data[1] = msg_namelower(msg);
862 anc_data[2] = msg_namelower(msg);
863 break;
864 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700865 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866 anc_data[0] = msg_nametype(msg);
867 anc_data[1] = msg_namelower(msg);
868 anc_data[2] = msg_nameupper(msg);
869 break;
870 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700871 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872 anc_data[0] = tport->conn_type;
873 anc_data[1] = tport->conn_instance;
874 anc_data[2] = tport->conn_instance;
875 break;
876 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700877 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878 }
Allan Stephens2db99832010-12-31 18:59:33 +0000879 if (has_name) {
880 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
881 if (res)
882 return res;
883 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100884
885 return 0;
886}
887
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900888/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889 * recv_msg - receive packet-oriented message
890 * @iocb: (unused)
891 * @m: descriptor for message info
892 * @buf_len: total size of user buffer area
893 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900894 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100895 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
896 * If the complete message doesn't fit in user area, truncate it.
897 *
898 * Returns size of returned message data, errno otherwise
899 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900static int recv_msg(struct kiocb *iocb, struct socket *sock,
901 struct msghdr *m, size_t buf_len, int flags)
902{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700903 struct sock *sk = sock->sk;
904 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905 struct sk_buff *buf;
906 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500907 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908 unsigned int sz;
909 u32 err;
910 int res;
911
Allan Stephens0c3141e2008-04-15 00:22:02 -0700912 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913 if (unlikely(!buf_len))
914 return -EINVAL;
915
Allan Stephens0c3141e2008-04-15 00:22:02 -0700916 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100917
Allan Stephens0c3141e2008-04-15 00:22:02 -0700918 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100919 res = -ENOTCONN;
920 goto exit;
921 }
922
Allan Stephens71092ea2011-02-23 14:52:14 -0500923 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700924restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100925
Allan Stephens0c3141e2008-04-15 00:22:02 -0700926 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700927 while (skb_queue_empty(&sk->sk_receive_queue)) {
928 if (sock->state == SS_DISCONNECTING) {
929 res = -ENOTCONN;
930 goto exit;
931 }
Allan Stephens71092ea2011-02-23 14:52:14 -0500932 if (timeout <= 0L) {
933 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700934 goto exit;
935 }
936 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -0500937 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
938 tipc_rx_ready(sock),
939 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700940 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700941 }
942
943 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700944 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100945 msg = buf_msg(buf);
946 sz = msg_data_sz(msg);
947 err = msg_errcode(msg);
948
949 /* Complete connection setup for an implied connect */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100950 if (unlikely(sock->state == SS_CONNECTING)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700951 res = auto_connect(sock, msg);
952 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100953 goto exit;
954 }
955
956 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100957 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700958 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959 goto restart;
960 }
961
962 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100963 set_orig_addr(m, msg);
964
965 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700966 res = anc_data_recv(m, msg, tport);
967 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968 goto exit;
969
970 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100971 if (!err) {
972 if (unlikely(buf_len < sz)) {
973 sz = buf_len;
974 m->msg_flags |= MSG_TRUNC;
975 }
Allan Stephens0232fd02011-02-21 09:45:40 -0500976 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
977 m->msg_iov, sz);
978 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100979 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980 res = sz;
981 } else {
982 if ((sock->state == SS_READY) ||
983 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
984 res = 0;
985 else
986 res = -ECONNRESET;
987 }
988
989 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -0700991 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -0700992 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
993 tipc_acknowledge(tport->ref, tport->conn_unacked);
994 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900995 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700997 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100998 return res;
999}
1000
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001001/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 * recv_stream - receive stream-oriented data
1003 * @iocb: (unused)
1004 * @m: descriptor for message info
1005 * @buf_len: total size of user buffer area
1006 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001007 *
1008 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001009 * will optionally wait for more; never truncates data.
1010 *
1011 * Returns size of returned message data, errno otherwise
1012 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013static int recv_stream(struct kiocb *iocb, struct socket *sock,
1014 struct msghdr *m, size_t buf_len, int flags)
1015{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001016 struct sock *sk = sock->sk;
1017 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 struct sk_buff *buf;
1019 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -05001020 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001021 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001022 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001025 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026
1027 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 if (unlikely(!buf_len))
1029 return -EINVAL;
1030
Allan Stephens0c3141e2008-04-15 00:22:02 -07001031 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032
Allan Stephens0c3141e2008-04-15 00:22:02 -07001033 if (unlikely((sock->state == SS_UNCONNECTED) ||
1034 (sock->state == SS_CONNECTING))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035 res = -ENOTCONN;
1036 goto exit;
1037 }
1038
Florian Westphal3720d402010-08-17 11:00:04 +00001039 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Allan Stephens71092ea2011-02-23 14:52:14 -05001040 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001041
Allan Stephens0c3141e2008-04-15 00:22:02 -07001042restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001043 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001044 while (skb_queue_empty(&sk->sk_receive_queue)) {
1045 if (sock->state == SS_DISCONNECTING) {
1046 res = -ENOTCONN;
1047 goto exit;
1048 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001049 if (timeout <= 0L) {
1050 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001051 goto exit;
1052 }
1053 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001054 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1055 tipc_rx_ready(sock),
1056 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001057 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001058 }
1059
1060 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001061 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 msg = buf_msg(buf);
1063 sz = msg_data_sz(msg);
1064 err = msg_errcode(msg);
1065
1066 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001068 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069 goto restart;
1070 }
1071
1072 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 if (sz_copied == 0) {
1074 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001075 res = anc_data_recv(m, msg, tport);
1076 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001077 goto exit;
1078 }
1079
1080 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001081 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001082 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001083
Allan Stephens0232fd02011-02-21 09:45:40 -05001084 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001085 needed = (buf_len - sz_copied);
1086 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001087
1088 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1089 m->msg_iov, sz_to_copy);
1090 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001091 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001092
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093 sz_copied += sz_to_copy;
1094
1095 if (sz_to_copy < sz) {
1096 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001097 TIPC_SKB_CB(buf)->handle =
1098 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001099 goto exit;
1100 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101 } else {
1102 if (sz_copied != 0)
1103 goto exit; /* can't add error msg to valid data */
1104
1105 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1106 res = 0;
1107 else
1108 res = -ECONNRESET;
1109 }
1110
1111 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001112 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001113 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1114 tipc_acknowledge(tport->ref, tport->conn_unacked);
1115 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001116 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117
1118 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001119 if ((sz_copied < buf_len) && /* didn't get all requested data */
1120 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001121 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001122 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1123 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124 goto restart;
1125
1126exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001127 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001128 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129}
1130
1131/**
Ying Xuef288bef2012-08-21 11:16:57 +08001132 * tipc_write_space - wake up thread if port congestion is released
1133 * @sk: socket
1134 */
1135static void tipc_write_space(struct sock *sk)
1136{
1137 struct socket_wq *wq;
1138
1139 rcu_read_lock();
1140 wq = rcu_dereference(sk->sk_wq);
1141 if (wq_has_sleeper(wq))
1142 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1143 POLLWRNORM | POLLWRBAND);
1144 rcu_read_unlock();
1145}
1146
1147/**
1148 * tipc_data_ready - wake up threads to indicate messages have been received
1149 * @sk: socket
1150 * @len: the length of messages
1151 */
1152static void tipc_data_ready(struct sock *sk, int len)
1153{
1154 struct socket_wq *wq;
1155
1156 rcu_read_lock();
1157 wq = rcu_dereference(sk->sk_wq);
1158 if (wq_has_sleeper(wq))
1159 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1160 POLLRDNORM | POLLRDBAND);
1161 rcu_read_unlock();
1162}
1163
1164/**
Allan Stephens1819b832008-04-15 00:15:50 -07001165 * rx_queue_full - determine if receive queue can accept another message
1166 * @msg: message to be added to queue
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 * @queue_size: current size of queue
1168 * @base: nominal maximum size of queue
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001169 *
Allan Stephens1819b832008-04-15 00:15:50 -07001170 * Returns 1 if queue is unable to accept message, 0 otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171 */
Allan Stephens1819b832008-04-15 00:15:50 -07001172static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173{
1174 u32 threshold;
1175 u32 imp = msg_importance(msg);
1176
1177 if (imp == TIPC_LOW_IMPORTANCE)
1178 threshold = base;
1179 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1180 threshold = base * 2;
1181 else if (imp == TIPC_HIGH_IMPORTANCE)
1182 threshold = base * 100;
1183 else
1184 return 0;
1185
1186 if (msg_connected(msg))
1187 threshold *= 4;
1188
Eric Dumazeta02cec22010-09-22 20:43:57 +00001189 return queue_size >= threshold;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190}
1191
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001192/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001193 * filter_connect - Handle all incoming messages for a connection-based socket
1194 * @tsock: TIPC socket
1195 * @msg: message
1196 *
1197 * Returns TIPC error status code and socket error status code
1198 * once it encounters some errors
1199 */
1200static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
1201{
1202 struct socket *sock = tsock->sk.sk_socket;
1203 struct tipc_msg *msg = buf_msg(*buf);
Ying Xue584d24b2012-11-29 18:51:19 -05001204 struct sock *sk = &tsock->sk;
Ying Xue7e6c1312012-11-29 18:39:14 -05001205 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001206 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001207
1208 if (msg_mcast(msg))
1209 return retval;
1210
1211 switch ((int)sock->state) {
1212 case SS_CONNECTED:
1213 /* Accept only connection-based messages sent by peer */
1214 if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
1215 if (unlikely(msg_errcode(msg))) {
1216 sock->state = SS_DISCONNECTING;
1217 __tipc_disconnect(tsock->p);
1218 }
1219 retval = TIPC_OK;
1220 }
1221 break;
1222 case SS_CONNECTING:
1223 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001224 if (unlikely(msg_errcode(msg))) {
1225 sock->state = SS_DISCONNECTING;
1226 sk->sk_err = -ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001227 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001228 break;
1229 }
1230
1231 if (unlikely(!msg_connected(msg)))
1232 break;
1233
1234 res = auto_connect(sock, msg);
1235 if (res) {
1236 sock->state = SS_DISCONNECTING;
1237 sk->sk_err = res;
1238 retval = TIPC_OK;
1239 break;
1240 }
1241
1242 /* If an incoming message is an 'ACK-', it should be
1243 * discarded here because it doesn't contain useful
1244 * data. In addition, we should try to wake up
1245 * connect() routine if sleeping.
1246 */
1247 if (msg_data_sz(msg) == 0) {
1248 kfree_skb(*buf);
1249 *buf = NULL;
1250 if (waitqueue_active(sk_sleep(sk)))
1251 wake_up_interruptible(sk_sleep(sk));
1252 }
1253 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001254 break;
1255 case SS_LISTENING:
1256 case SS_UNCONNECTED:
1257 /* Accept only SYN message */
1258 if (!msg_connected(msg) && !(msg_errcode(msg)))
1259 retval = TIPC_OK;
1260 break;
1261 case SS_DISCONNECTING:
1262 break;
1263 default:
1264 pr_err("Unknown socket state %u\n", sock->state);
1265 }
1266 return retval;
1267}
1268
1269/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001270 * filter_rcv - validate incoming message
1271 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001273 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001274 * Enqueues message on receive queue if acceptable; optionally handles
1275 * disconnect indication for a connected socket.
1276 *
1277 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001278 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001279 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1280 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001281static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001282{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001283 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001284 struct tipc_msg *msg = buf_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285 u32 recv_q_len;
Ying Xue7e6c1312012-11-29 18:39:14 -05001286 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001287
Per Lidenb97bf3f2006-01-02 19:04:38 +01001288 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001289 if (msg_type(msg) > TIPC_DIRECT_MSG)
1290 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001291
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001293 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001294 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295 } else {
Ying Xue7e6c1312012-11-29 18:39:14 -05001296 res = filter_connect(tipc_sk(sk), &buf);
1297 if (res != TIPC_OK || buf == NULL)
1298 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001299 }
1300
1301 /* Reject message if there isn't room to queue it */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001302 recv_q_len = skb_queue_len(&sk->sk_receive_queue);
Allan Stephens1819b832008-04-15 00:15:50 -07001303 if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
1304 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001305 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001306 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001307
Allan Stephens0c3141e2008-04-15 00:22:02 -07001308 /* Enqueue message (finally!) */
Allan Stephens0232fd02011-02-21 09:45:40 -05001309 TIPC_SKB_CB(buf)->handle = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001310 __skb_queue_tail(&sk->sk_receive_queue, buf);
1311
Ying Xuef288bef2012-08-21 11:16:57 +08001312 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001313 return TIPC_OK;
1314}
1315
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001316/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001317 * backlog_rcv - handle incoming message from backlog queue
1318 * @sk: socket
1319 * @buf: message
1320 *
1321 * Caller must hold socket lock, but not port lock.
1322 *
1323 * Returns 0
1324 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001325static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1326{
1327 u32 res;
1328
1329 res = filter_rcv(sk, buf);
1330 if (res)
1331 tipc_reject_msg(buf, res);
1332 return 0;
1333}
1334
1335/**
1336 * dispatch - handle incoming message
1337 * @tport: TIPC port that received message
1338 * @buf: message
1339 *
1340 * Called with port lock already taken.
1341 *
1342 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1343 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001344static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1345{
1346 struct sock *sk = (struct sock *)tport->usr_handle;
1347 u32 res;
1348
1349 /*
1350 * Process message if socket is unlocked; otherwise add to backlog queue
1351 *
1352 * This code is based on sk_receive_skb(), but must be distinct from it
1353 * since a TIPC-specific filter/reject mechanism is utilized
1354 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001355 bh_lock_sock(sk);
1356 if (!sock_owned_by_user(sk)) {
1357 res = filter_rcv(sk, buf);
1358 } else {
Eric Dumazetf545a382012-04-22 23:34:26 +00001359 if (sk_add_backlog(sk, buf, sk->sk_rcvbuf))
Zhu Yi53eecb12010-03-04 18:01:45 +00001360 res = TIPC_ERR_OVERLOAD;
1361 else
1362 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001363 }
1364 bh_unlock_sock(sk);
1365
1366 return res;
1367}
1368
1369/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370 * wakeupdispatch - wake up port after congestion
1371 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001372 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001373 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375static void wakeupdispatch(struct tipc_port *tport)
1376{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001377 struct sock *sk = (struct sock *)tport->usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001378
Ying Xuef288bef2012-08-21 11:16:57 +08001379 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380}
1381
1382/**
1383 * connect - establish a connection to another TIPC port
1384 * @sock: socket structure
1385 * @dest: socket address for destination port
1386 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001387 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001388 *
1389 * Returns 0 on success, errno otherwise
1390 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001391static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 int flags)
1393{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001394 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001395 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1396 struct msghdr m = {NULL,};
Allan Stephensa0f40f02011-05-26 13:44:34 -04001397 unsigned int timeout;
Allan Stephensb89741a2008-04-15 00:20:37 -07001398 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001399
Allan Stephens0c3141e2008-04-15 00:22:02 -07001400 lock_sock(sk);
1401
Allan Stephensb89741a2008-04-15 00:20:37 -07001402 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001403 if (sock->state == SS_READY) {
1404 res = -EOPNOTSUPP;
1405 goto exit;
1406 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001407
Allan Stephensb89741a2008-04-15 00:20:37 -07001408 /*
1409 * Reject connection attempt using multicast address
1410 *
1411 * Note: send_msg() validates the rest of the address fields,
1412 * so there's no need to do it here
1413 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001414 if (dst->addrtype == TIPC_ADDR_MCAST) {
1415 res = -EINVAL;
1416 goto exit;
1417 }
1418
Ying Xue584d24b2012-11-29 18:51:19 -05001419 timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001420
Ying Xue584d24b2012-11-29 18:51:19 -05001421 switch (sock->state) {
1422 case SS_UNCONNECTED:
1423 /* Send a 'SYN-' to destination */
1424 m.msg_name = dest;
1425 m.msg_namelen = destlen;
1426
1427 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1428 * indicate send_msg() is never blocked.
1429 */
1430 if (!timeout)
1431 m.msg_flags = MSG_DONTWAIT;
1432
1433 res = send_msg(NULL, sock, &m, 0);
1434 if ((res < 0) && (res != -EWOULDBLOCK))
1435 goto exit;
1436
1437 /* Just entered SS_CONNECTING state; the only
1438 * difference is that return value in non-blocking
1439 * case is EINPROGRESS, rather than EALREADY.
1440 */
1441 res = -EINPROGRESS;
1442 break;
1443 case SS_CONNECTING:
1444 res = -EALREADY;
1445 break;
1446 case SS_CONNECTED:
1447 res = -EISCONN;
1448 break;
1449 default:
1450 res = -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001451 goto exit;
Allan Stephensb89741a2008-04-15 00:20:37 -07001452 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001453
Ying Xue584d24b2012-11-29 18:51:19 -05001454 if (sock->state == SS_CONNECTING) {
1455 if (!timeout)
1456 goto exit;
1457
1458 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1459 release_sock(sk);
1460 res = wait_event_interruptible_timeout(*sk_sleep(sk),
1461 sock->state != SS_CONNECTING,
1462 timeout ? (long)msecs_to_jiffies(timeout)
1463 : MAX_SCHEDULE_TIMEOUT);
1464 lock_sock(sk);
1465 if (res <= 0) {
1466 if (res == 0)
1467 res = -ETIMEDOUT;
1468 else
1469 ; /* leave "res" unchanged */
1470 goto exit;
1471 }
1472 }
1473
1474 if (unlikely(sock->state == SS_DISCONNECTING))
1475 res = sock_error(sk);
1476 else
1477 res = 0;
1478
Allan Stephens0c3141e2008-04-15 00:22:02 -07001479exit:
1480 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001481 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001482}
1483
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001484/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001485 * listen - allow socket to listen for incoming connections
1486 * @sock: socket structure
1487 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001488 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001489 * Returns 0 on success, errno otherwise
1490 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001491static int listen(struct socket *sock, int len)
1492{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001493 struct sock *sk = sock->sk;
1494 int res;
1495
1496 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001497
Ying Xue245f3d32011-07-06 06:01:13 -04001498 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001499 res = -EINVAL;
1500 else {
1501 sock->state = SS_LISTENING;
1502 res = 0;
1503 }
1504
1505 release_sock(sk);
1506 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001507}
1508
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001509/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001510 * accept - wait for connection request
1511 * @sock: listening socket
1512 * @newsock: new socket that is to be connected
1513 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001514 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001515 * Returns 0 on success, errno otherwise
1516 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001517static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001518{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001519 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001520 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001521 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001522
Allan Stephens0c3141e2008-04-15 00:22:02 -07001523 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001524
Allan Stephens0c3141e2008-04-15 00:22:02 -07001525 if (sock->state != SS_LISTENING) {
1526 res = -EINVAL;
1527 goto exit;
1528 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001529
Allan Stephens0c3141e2008-04-15 00:22:02 -07001530 while (skb_queue_empty(&sk->sk_receive_queue)) {
1531 if (flags & O_NONBLOCK) {
1532 res = -EWOULDBLOCK;
1533 goto exit;
1534 }
1535 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001536 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001537 (!skb_queue_empty(&sk->sk_receive_queue)));
1538 lock_sock(sk);
1539 if (res)
1540 goto exit;
1541 }
1542
1543 buf = skb_peek(&sk->sk_receive_queue);
1544
Eric Paris3f378b62009-11-05 22:18:14 -08001545 res = tipc_create(sock_net(sock->sk), new_sock, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001546 if (!res) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001547 struct sock *new_sk = new_sock->sk;
Allan Stephens2da59912008-07-14 22:43:32 -07001548 struct tipc_sock *new_tsock = tipc_sk(new_sk);
1549 struct tipc_port *new_tport = new_tsock->p;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001550 u32 new_ref = new_tport->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001551 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001552
1553 lock_sock(new_sk);
1554
1555 /*
1556 * Reject any stray messages received by new socket
1557 * before the socket lock was taken (very, very unlikely)
1558 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001559 reject_rx_queue(new_sk);
1560
1561 /* Connect new socket to it's peer */
Allan Stephens2da59912008-07-14 22:43:32 -07001562 new_tsock->peer_name.ref = msg_origport(msg);
1563 new_tsock->peer_name.node = msg_orignode(msg);
Paul Gortmakerbc879112012-11-29 13:48:40 -05001564 tipc_connect(new_ref, &new_tsock->peer_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001565 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001566
1567 tipc_set_portimportance(new_ref, msg_importance(msg));
1568 if (msg_named(msg)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001569 new_tport->conn_type = msg_nametype(msg);
1570 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001571 }
1572
Allan Stephens0c3141e2008-04-15 00:22:02 -07001573 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1575 * Respond to 'SYN+' by queuing it on new socket.
1576 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001577 if (!msg_data_sz(msg)) {
1578 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001579
Allan Stephens0c3141e2008-04-15 00:22:02 -07001580 advance_rx_queue(sk);
1581 send_packet(NULL, new_sock, &m, 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001582 } else {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001583 __skb_dequeue(&sk->sk_receive_queue);
1584 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001585 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001586 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001587 }
1588exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001589 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001590 return res;
1591}
1592
1593/**
1594 * shutdown - shutdown socket connection
1595 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001596 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001597 *
1598 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001599 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600 * Returns 0 on success, errno otherwise
1601 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001602static int shutdown(struct socket *sock, int how)
1603{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001604 struct sock *sk = sock->sk;
1605 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606 struct sk_buff *buf;
1607 int res;
1608
Allan Stephense247a8f2008-03-06 15:05:38 -08001609 if (how != SHUT_RDWR)
1610 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001611
Allan Stephens0c3141e2008-04-15 00:22:02 -07001612 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001613
1614 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001615 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001616 case SS_CONNECTED:
1617
Per Lidenb97bf3f2006-01-02 19:04:38 +01001618restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001619 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001620 buf = __skb_dequeue(&sk->sk_receive_queue);
1621 if (buf) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001622 if (TIPC_SKB_CB(buf)->handle != 0) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001623 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001624 goto restart;
1625 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001626 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001627 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001628 } else {
1629 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001631
1632 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001633
1634 /* fall through */
1635
1636 case SS_DISCONNECTING:
1637
Ying Xue75031152012-10-29 09:38:15 -04001638 /* Discard any unreceived messages */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001639 discard_rx_queue(sk);
Ying Xue75031152012-10-29 09:38:15 -04001640
1641 /* Wake up anyone sleeping in poll */
1642 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001643 res = 0;
1644 break;
1645
1646 default:
1647 res = -ENOTCONN;
1648 }
1649
Allan Stephens0c3141e2008-04-15 00:22:02 -07001650 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001651 return res;
1652}
1653
1654/**
1655 * setsockopt - set socket option
1656 * @sock: socket structure
1657 * @lvl: option level
1658 * @opt: option identifier
1659 * @ov: pointer to new option value
1660 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001661 *
1662 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001664 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665 * Returns 0 on success, errno otherwise
1666 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001667static int setsockopt(struct socket *sock,
David S. Millerb7058842009-09-30 16:12:20 -07001668 int lvl, int opt, char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001670 struct sock *sk = sock->sk;
1671 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001672 u32 value;
1673 int res;
1674
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001675 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1676 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001677 if (lvl != SOL_TIPC)
1678 return -ENOPROTOOPT;
1679 if (ol < sizeof(value))
1680 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001681 res = get_user(value, (u32 __user *)ov);
1682 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683 return res;
1684
Allan Stephens0c3141e2008-04-15 00:22:02 -07001685 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001686
Per Lidenb97bf3f2006-01-02 19:04:38 +01001687 switch (opt) {
1688 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001689 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690 break;
1691 case TIPC_SRC_DROPPABLE:
1692 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001693 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001694 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001695 res = -ENOPROTOOPT;
1696 break;
1697 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001698 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699 break;
1700 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001701 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001702 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001703 break;
1704 default:
1705 res = -EINVAL;
1706 }
1707
Allan Stephens0c3141e2008-04-15 00:22:02 -07001708 release_sock(sk);
1709
Per Lidenb97bf3f2006-01-02 19:04:38 +01001710 return res;
1711}
1712
1713/**
1714 * getsockopt - get socket option
1715 * @sock: socket structure
1716 * @lvl: option level
1717 * @opt: option identifier
1718 * @ov: receptacle for option value
1719 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001720 *
1721 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001722 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001723 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001724 * Returns 0 on success, errno otherwise
1725 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001726static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001727 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001728{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001729 struct sock *sk = sock->sk;
1730 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001731 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001732 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001733 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001734
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001735 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1736 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001737 if (lvl != SOL_TIPC)
1738 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001739 res = get_user(len, ol);
1740 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001741 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001742
Allan Stephens0c3141e2008-04-15 00:22:02 -07001743 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001744
1745 switch (opt) {
1746 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001747 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001748 break;
1749 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001750 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001751 break;
1752 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001753 res = tipc_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 value = tipc_sk(sk)->conn_timeout;
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;
Allan Stephens0e659672010-12-31 18:59:32 +00001759 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001760 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001761 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001762 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001763 value = skb_queue_len(&sk->sk_receive_queue);
1764 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001765 default:
1766 res = -EINVAL;
1767 }
1768
Allan Stephens0c3141e2008-04-15 00:22:02 -07001769 release_sock(sk);
1770
Paul Gortmaker25860c32010-12-31 18:59:31 +00001771 if (res)
1772 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001773
Paul Gortmaker25860c32010-12-31 18:59:31 +00001774 if (len < sizeof(value))
1775 return -EINVAL;
1776
1777 if (copy_to_user(ov, &value, sizeof(value)))
1778 return -EFAULT;
1779
1780 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001781}
1782
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001783/* Protocol switches for the various types of TIPC sockets */
1784
Florian Westphalbca65ea2008-02-07 18:18:01 -08001785static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001786 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787 .family = AF_TIPC,
1788 .release = release,
1789 .bind = bind,
1790 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001791 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001792 .accept = sock_no_accept,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793 .getname = get_name,
1794 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001795 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001796 .listen = sock_no_listen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001797 .shutdown = shutdown,
1798 .setsockopt = setsockopt,
1799 .getsockopt = getsockopt,
1800 .sendmsg = send_msg,
1801 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001802 .mmap = sock_no_mmap,
1803 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001804};
1805
Florian Westphalbca65ea2008-02-07 18:18:01 -08001806static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001807 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001808 .family = AF_TIPC,
1809 .release = release,
1810 .bind = bind,
1811 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001812 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001813 .accept = accept,
1814 .getname = get_name,
1815 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001816 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001817 .listen = listen,
1818 .shutdown = shutdown,
1819 .setsockopt = setsockopt,
1820 .getsockopt = getsockopt,
1821 .sendmsg = send_packet,
1822 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001823 .mmap = sock_no_mmap,
1824 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001825};
1826
Florian Westphalbca65ea2008-02-07 18:18:01 -08001827static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001828 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829 .family = AF_TIPC,
1830 .release = release,
1831 .bind = bind,
1832 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001833 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834 .accept = accept,
1835 .getname = get_name,
1836 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001837 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001838 .listen = listen,
1839 .shutdown = shutdown,
1840 .setsockopt = setsockopt,
1841 .getsockopt = getsockopt,
1842 .sendmsg = send_stream,
1843 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001844 .mmap = sock_no_mmap,
1845 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001846};
1847
Florian Westphalbca65ea2008-02-07 18:18:01 -08001848static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001849 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001850 .family = AF_TIPC,
1851 .create = tipc_create
1852};
1853
1854static struct proto tipc_proto = {
1855 .name = "TIPC",
1856 .owner = THIS_MODULE,
1857 .obj_size = sizeof(struct tipc_sock)
1858};
1859
1860/**
Per Liden4323add2006-01-18 00:38:21 +01001861 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001862 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001863 * Returns 0 on success, errno otherwise
1864 */
Per Liden4323add2006-01-18 00:38:21 +01001865int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001866{
1867 int res;
1868
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001869 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001870 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001871 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001872 goto out;
1873 }
1874
1875 res = sock_register(&tipc_family_ops);
1876 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001877 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001878 proto_unregister(&tipc_proto);
1879 goto out;
1880 }
1881
1882 sockets_enabled = 1;
1883 out:
1884 return res;
1885}
1886
1887/**
Per Liden4323add2006-01-18 00:38:21 +01001888 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001889 */
Per Liden4323add2006-01-18 00:38:21 +01001890void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001891{
1892 if (!sockets_enabled)
1893 return;
1894
1895 sockets_enabled = 0;
1896 sock_unregister(tipc_family_ops.family);
1897 proto_unregister(&tipc_proto);
1898}