blob: 59adc76905e0d022211f68056175041fde326c4d [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 *
Allan Stephens5eee6a62007-06-10 17:24:55 -07004 * Copyright (c) 2001-2007, Ericsson AB
Allan Stephens4132fac2011-01-07 13:12:12 -05005 * Copyright (c) 2004-2008, 2010-2011, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000038#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039
Erik Hugne2cf8aa12012-06-29 00:16:37 -040040#include <linux/export.h>
41#include <net/sock.h>
42
Per Lidenb97bf3f2006-01-02 19:04:38 +010043#define SS_LISTENING -1 /* socket is listening */
44#define SS_READY -2 /* socket is connectionless */
45
Allan Stephens3654ea02008-04-13 21:35:11 -070046#define OVERLOAD_LIMIT_BASE 5000
47#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
76static atomic_t tipc_queue_size = ATOMIC_INIT(0);
77
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090078/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070079 * Revised TIPC socket locking policy:
80 *
81 * Most socket operations take the standard socket lock when they start
82 * and hold it until they finish (or until they need to sleep). Acquiring
83 * this lock grants the owner exclusive access to the fields of the socket
84 * data structures, with the exception of the backlog queue. A few socket
85 * operations can be done without taking the socket lock because they only
86 * read socket information that never changes during the life of the socket.
87 *
88 * Socket operations may acquire the lock for the associated TIPC port if they
89 * need to perform an operation on the port. If any routine needs to acquire
90 * both the socket lock and the port lock it must take the socket lock first
91 * to avoid the risk of deadlock.
92 *
93 * The dispatcher handling incoming messages cannot grab the socket lock in
94 * the standard fashion, since invoked it runs at the BH level and cannot block.
95 * Instead, it checks to see if the socket lock is currently owned by someone,
96 * and either handles the message itself or adds it to the socket's backlog
97 * queue; in the latter case the queued message is processed once the process
98 * owning the socket lock releases it.
99 *
100 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
101 * the problem of a blocked socket operation preventing any other operations
102 * from occurring. However, applications must be careful if they have
103 * multiple threads trying to send (or receive) on the same socket, as these
104 * operations might interfere with each other. For example, doing a connect
105 * and a receive at the same time might allow the receive to consume the
106 * ACK message meant for the connect. While additional work could be done
107 * to try and overcome this, it doesn't seem to be worthwhile at the present.
108 *
109 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
110 * that another operation that must be performed in a non-blocking manner is
111 * not delayed for very long because the lock has already been taken.
112 *
113 * NOTE: This code assumes that certain fields of a port/socket pair are
114 * constant over its lifetime; such fields can be examined without taking
115 * the socket lock and/or port lock, and do not need to be re-read even
116 * after resuming processing after waiting. These fields include:
117 * - socket type
118 * - pointer to socket sk structure (aka tipc_sock structure)
119 * - pointer to port structure
120 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122
123/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700124 * advance_rx_queue - discard first buffer in socket receive queue
125 *
126 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700128static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400130 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131 atomic_dec(&tipc_queue_size);
132}
133
134/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700135 * discard_rx_queue - discard all buffers in socket receive queue
136 *
137 * Caller must hold socket lock
138 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700139static void discard_rx_queue(struct sock *sk)
140{
141 struct sk_buff *buf;
142
143 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
144 atomic_dec(&tipc_queue_size);
Allan Stephens5f6d9122011-11-04 13:24:29 -0400145 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700146 }
147}
148
149/**
150 * reject_rx_queue - reject all buffers in socket receive queue
151 *
152 * Caller must hold socket lock
153 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700154static void reject_rx_queue(struct sock *sk)
155{
156 struct sk_buff *buf;
157
158 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
159 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
160 atomic_dec(&tipc_queue_size);
161 }
162}
163
164/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 * tipc_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700166 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 * @sock: pre-allocated socket structure
168 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800169 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900170 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700171 * This routine creates additional data structures used by the TIPC socket,
172 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 *
174 * Returns 0 on success, errno otherwise
175 */
Eric Paris3f378b62009-11-05 22:18:14 -0800176static int tipc_create(struct net *net, struct socket *sock, int protocol,
177 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700179 const struct proto_ops *ops;
180 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700182 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700183
184 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185 if (unlikely(protocol != 0))
186 return -EPROTONOSUPPORT;
187
Per Lidenb97bf3f2006-01-02 19:04:38 +0100188 switch (sock->type) {
189 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700190 ops = &stream_ops;
191 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100192 break;
193 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700194 ops = &packet_ops;
195 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196 break;
197 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700199 ops = &msg_ops;
200 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 break;
Allan Stephens49978652006-06-25 23:47:18 -0700202 default:
Allan Stephens49978652006-06-25 23:47:18 -0700203 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204 }
205
Allan Stephens0c3141e2008-04-15 00:22:02 -0700206 /* Allocate socket's protocol area */
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700207 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700208 if (sk == NULL)
209 return -ENOMEM;
210
211 /* Allocate TIPC port for socket to use */
Allan Stephens0ea52242008-07-14 22:42:19 -0700212 tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
213 TIPC_LOW_IMPORTANCE);
214 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700215 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 return -ENOMEM;
217 }
218
Allan Stephens0c3141e2008-04-15 00:22:02 -0700219 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700220 sock->ops = ops;
221 sock->state = state;
222
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700224 sk->sk_backlog_rcv = backlog_rcv;
Erik Hugnee57edf62012-10-04 05:00:43 +0000225 sk->sk_rcvbuf = TIPC_FLOW_CONTROL_WIN * 2 * TIPC_MAX_USER_MSG_SIZE * 2;
Ying Xuef288bef2012-08-21 11:16:57 +0800226 sk->sk_data_ready = tipc_data_ready;
227 sk->sk_write_space = tipc_write_space;
Allan Stephens0ea52242008-07-14 22:42:19 -0700228 tipc_sk(sk)->p = tp_ptr;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400229 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700231 spin_unlock_bh(tp_ptr->lock);
232
Allan Stephens0c3141e2008-04-15 00:22:02 -0700233 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700234 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700235 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700236 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700237 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 return 0;
240}
241
242/**
243 * release - destroy a TIPC socket
244 * @sock: socket to destroy
245 *
246 * This routine cleans up any messages that are still queued on the socket.
247 * For DGRAM and RDM socket types, all queued messages are rejected.
248 * For SEQPACKET and STREAM socket types, the first message is rejected
249 * and any others are discarded. (If the first message on a STREAM socket
250 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900251 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252 * NOTE: Rejected messages are not necessarily returned to the sender! They
253 * are returned or discarded according to the "destination droppable" setting
254 * specified for the message by the sender.
255 *
256 * Returns 0 on success, errno otherwise
257 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258static int release(struct socket *sock)
259{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700261 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700263 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100264
Allan Stephens0c3141e2008-04-15 00:22:02 -0700265 /*
266 * Exit if socket isn't fully initialized (occurs when a failed accept()
267 * releases a pre-allocated child socket that was never used)
268 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700269 if (sk == NULL)
270 return 0;
271
272 tport = tipc_sk_port(sk);
273 lock_sock(sk);
274
275 /*
276 * Reject all unreceived messages, except on an active connection
277 * (which disconnects locally & sends a 'FIN+' to peer)
278 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700280 buf = __skb_dequeue(&sk->sk_receive_queue);
281 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282 break;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700283 atomic_dec(&tipc_queue_size);
Allan Stephens0232fd02011-02-21 09:45:40 -0500284 if (TIPC_SKB_CB(buf)->handle != 0)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400285 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700286 else {
287 if ((sock->state == SS_CONNECTING) ||
288 (sock->state == SS_CONNECTED)) {
289 sock->state = SS_DISCONNECTING;
290 tipc_disconnect(tport->ref);
291 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100292 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700293 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 }
295
Allan Stephens0c3141e2008-04-15 00:22:02 -0700296 /*
297 * Delete TIPC port; this ensures no more messages are queued
298 * (also disconnects an active connection & sends a 'FIN-' to peer)
299 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700300 res = tipc_deleteport(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301
Allan Stephens0c3141e2008-04-15 00:22:02 -0700302 /* Discard any remaining (connection-based) messages in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700303 discard_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304
Allan Stephens0c3141e2008-04-15 00:22:02 -0700305 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700306 sock->state = SS_DISCONNECTING;
307 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308
309 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700310 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 return res;
313}
314
315/**
316 * bind - associate or disassocate TIPC name(s) with a socket
317 * @sock: socket structure
318 * @uaddr: socket address describing name(s) and desired operation
319 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900320 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 * Name and name sequence binding is indicated using a positive scope value;
322 * a negative scope value unbinds the specified name. Specifying no name
323 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900324 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700326 *
327 * NOTE: This routine doesn't need to take the socket lock since it doesn't
328 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
331{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700333 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334
Allan Stephens0c3141e2008-04-15 00:22:02 -0700335 if (unlikely(!uaddr_len))
336 return tipc_withdraw(portref, 0, NULL);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900337
Allan Stephens0c3141e2008-04-15 00:22:02 -0700338 if (uaddr_len < sizeof(struct sockaddr_tipc))
339 return -EINVAL;
340 if (addr->family != AF_TIPC)
341 return -EAFNOSUPPORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 if (addr->addrtype == TIPC_ADDR_NAME)
344 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700345 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
346 return -EAFNOSUPPORT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900347
Allan Stephensc422f1b2011-11-02 15:49:40 -0400348 if (addr->addr.nameseq.type < TIPC_RESERVED_TYPES)
349 return -EACCES;
350
Allan Stephens0c3141e2008-04-15 00:22:02 -0700351 return (addr->scope > 0) ?
352 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
353 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354}
355
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900356/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357 * get_name - get port ID of socket or peer socket
358 * @sock: socket structure
359 * @uaddr: area for returned socket address
360 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700361 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900362 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700364 *
Allan Stephens2da59912008-07-14 22:43:32 -0700365 * NOTE: This routine doesn't need to take the socket lock since it only
366 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000367 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900369static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100370 int *uaddr_len, int peer)
371{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700373 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000375 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700376 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700377 if ((sock->state != SS_CONNECTED) &&
378 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
379 return -ENOTCONN;
380 addr->addr.id.ref = tsock->peer_name.ref;
381 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700382 } else {
Allan Stephensb924dcf2010-11-30 12:01:03 +0000383 addr->addr.id.ref = tsock->p->ref;
384 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700385 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386
387 *uaddr_len = sizeof(*addr);
388 addr->addrtype = TIPC_ADDR_ID;
389 addr->family = AF_TIPC;
390 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 addr->addr.name.domain = 0;
392
Allan Stephens0c3141e2008-04-15 00:22:02 -0700393 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394}
395
396/**
397 * poll - read and possibly block on pollmask
398 * @file: file structure associated with the socket
399 * @sock: socket for which to calculate the poll bits
400 * @wait: ???
401 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700402 * Returns pollmask value
403 *
404 * COMMENTARY:
405 * It appears that the usual socket locking mechanisms are not useful here
406 * since the pollmask info is potentially out-of-date the moment this routine
407 * exits. TCP and other protocols seem to rely on higher level poll routines
408 * to handle any preventable race conditions, so TIPC will do the same ...
409 *
410 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700411 *
Allan Stephensf662c072010-08-17 11:00:06 +0000412 * socket state flags set
413 * ------------ ---------
414 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200415 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000416 *
417 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
418 * no write flags
419 *
420 * connected POLLIN/POLLRDNORM if data in rx queue
421 * POLLOUT if port is not congested
422 *
423 * disconnecting POLLIN/POLLRDNORM/POLLHUP
424 * no write flags
425 *
426 * listening POLLIN if SYN in rx queue
427 * no write flags
428 *
429 * ready POLLIN/POLLRDNORM if data in rx queue
430 * [connectionless] POLLOUT (since port cannot be congested)
431 *
432 * IMPORTANT: The fact that a read or write operation is indicated does NOT
433 * imply that the operation will succeed, merely that it should be performed
434 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900436static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437 poll_table *wait)
438{
Allan Stephens9b674e82008-03-26 16:48:21 -0700439 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000440 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700441
Ying Xuef288bef2012-08-21 11:16:57 +0800442 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700443
Allan Stephensf662c072010-08-17 11:00:06 +0000444 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200445 case SS_UNCONNECTED:
446 if (!tipc_sk_port(sk)->congested)
447 mask |= POLLOUT;
448 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000449 case SS_READY:
450 case SS_CONNECTED:
451 if (!tipc_sk_port(sk)->congested)
452 mask |= POLLOUT;
453 /* fall thru' */
454 case SS_CONNECTING:
455 case SS_LISTENING:
456 if (!skb_queue_empty(&sk->sk_receive_queue))
457 mask |= (POLLIN | POLLRDNORM);
458 break;
459 case SS_DISCONNECTING:
460 mask = (POLLIN | POLLRDNORM | POLLHUP);
461 break;
462 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700463
464 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465}
466
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900467/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468 * dest_name_check - verify user is permitted to send to specified port name
469 * @dest: destination address
470 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900471 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472 * Prevents restricted configuration commands from being issued by
473 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900474 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475 * Returns 0 if permission is granted, otherwise errno
476 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800477static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478{
479 struct tipc_cfg_msg_hdr hdr;
480
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900481 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
482 return 0;
483 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
484 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900485 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
486 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487
Allan Stephens3f8dd942011-01-18 13:09:29 -0500488 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
489 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900490 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700492 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900494
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495 return 0;
496}
497
498/**
499 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700500 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 * @sock: socket structure
502 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700503 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900504 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900506 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
508 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900509 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100510 * Returns the number of bytes sent on success, or errno otherwise
511 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512static int send_msg(struct kiocb *iocb, struct socket *sock,
513 struct msghdr *m, size_t total_len)
514{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700515 struct sock *sk = sock->sk;
516 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900517 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518 int needs_conn;
Ying Xue1d835872011-07-06 05:53:15 -0400519 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520 int res = -EINVAL;
521
522 if (unlikely(!dest))
523 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700524 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
525 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526 return -EINVAL;
Allan Stephensc29c3f72010-04-20 17:58:24 -0400527 if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
Eric Dumazet95c96172012-04-15 05:58:06 +0000528 (m->msg_iovlen > (unsigned int)INT_MAX))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400529 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530
Allan Stephens0c3141e2008-04-15 00:22:02 -0700531 if (iocb)
532 lock_sock(sk);
533
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534 needs_conn = (sock->state != SS_READY);
535 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700536 if (sock->state == SS_LISTENING) {
537 res = -EPIPE;
538 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700539 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700540 if (sock->state != SS_UNCONNECTED) {
541 res = -EISCONN;
542 goto exit;
543 }
544 if ((tport->published) ||
545 ((sock->type == SOCK_STREAM) && (total_len != 0))) {
546 res = -EOPNOTSUPP;
547 goto exit;
548 }
549 if (dest->addrtype == TIPC_ADDR_NAME) {
550 tport->conn_type = dest->addr.name.name.type;
551 tport->conn_instance = dest->addr.name.name.instance;
552 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553
554 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700555 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556 }
557
Ying Xue1d835872011-07-06 05:53:15 -0400558 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
559
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900560 do {
561 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000562 res = dest_name_check(dest, m);
563 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700564 break;
565 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900566 &dest->addr.name.name,
567 dest->addr.name.domain,
568 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500569 m->msg_iov,
570 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000571 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700572 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900573 &dest->addr.id,
574 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500575 m->msg_iov,
576 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000577 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 if (needs_conn) {
579 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700580 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 }
Allan Stephens2db99832010-12-31 18:59:33 +0000582 res = dest_name_check(dest, m);
583 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700584 break;
585 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900586 &dest->addr.nameseq,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900587 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500588 m->msg_iov,
589 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900590 }
591 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000592 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700593 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700594 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900595 }
Ying Xue1d835872011-07-06 05:53:15 -0400596 if (timeout_val <= 0L) {
597 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700598 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700600 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400601 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
602 !tport->congested, timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700603 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900604 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700605
606exit:
607 if (iocb)
608 release_sock(sk);
609 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610}
611
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900612/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700614 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615 * @sock: socket structure
616 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700617 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900618 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900620 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621 * Returns the number of bytes sent on success, or errno otherwise
622 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623static int send_packet(struct kiocb *iocb, struct socket *sock,
624 struct msghdr *m, size_t total_len)
625{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700626 struct sock *sk = sock->sk;
627 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900628 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Ying Xue1d835872011-07-06 05:53:15 -0400629 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 int res;
631
632 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100633 if (unlikely(dest))
634 return send_msg(iocb, sock, m, total_len);
635
Allan Stephensc29c3f72010-04-20 17:58:24 -0400636 if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
Eric Dumazet95c96172012-04-15 05:58:06 +0000637 (m->msg_iovlen > (unsigned int)INT_MAX))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400638 return -EMSGSIZE;
639
Allan Stephens0c3141e2008-04-15 00:22:02 -0700640 if (iocb)
641 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642
Ying Xue1d835872011-07-06 05:53:15 -0400643 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
644
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900645 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700646 if (unlikely(sock->state != SS_CONNECTED)) {
647 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900648 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700649 else
650 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700651 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700652 }
653
Allan Stephens26896902011-04-21 10:42:07 -0500654 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov,
655 total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000656 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700657 break;
Ying Xue1d835872011-07-06 05:53:15 -0400658 if (timeout_val <= 0L) {
659 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700660 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700662 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400663 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
664 (!tport->congested || !tport->connected), timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700665 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900666 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700667
668 if (iocb)
669 release_sock(sk);
670 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671}
672
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900673/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674 * send_stream - send stream-oriented data
675 * @iocb: (unused)
676 * @sock: socket structure
677 * @m: data to send
678 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900679 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900681 *
682 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700683 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100684 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100685static int send_stream(struct kiocb *iocb, struct socket *sock,
686 struct msghdr *m, size_t total_len)
687{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700688 struct sock *sk = sock->sk;
689 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690 struct msghdr my_msg;
691 struct iovec my_iov;
692 struct iovec *curr_iov;
693 int curr_iovlen;
694 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700695 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100696 int curr_left;
697 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700698 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100699 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900700
Allan Stephens0c3141e2008-04-15 00:22:02 -0700701 lock_sock(sk);
702
Allan Stephens05646c92007-06-10 17:25:24 -0700703 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900704 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700705 if (sock->state == SS_UNCONNECTED) {
706 res = send_packet(NULL, sock, m, total_len);
707 goto exit;
708 } else if (sock->state == SS_DISCONNECTING) {
709 res = -EPIPE;
710 goto exit;
711 } else {
712 res = -ENOTCONN;
713 goto exit;
714 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900715 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716
Allan Stephens0c3141e2008-04-15 00:22:02 -0700717 if (unlikely(m->msg_name)) {
718 res = -EISCONN;
719 goto exit;
720 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700721
Eric Dumazet95c96172012-04-15 05:58:06 +0000722 if ((total_len > (unsigned int)INT_MAX) ||
723 (m->msg_iovlen > (unsigned int)INT_MAX)) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400724 res = -EMSGSIZE;
725 goto exit;
726 }
727
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900728 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 * Send each iovec entry using one or more messages
730 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900731 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732 * (i.e. one large iovec entry), but could be improved to pass sets
733 * of small iovec entries into send_packet().
734 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700735 curr_iov = m->msg_iov;
736 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737 my_msg.msg_iov = &my_iov;
738 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700739 my_msg.msg_flags = m->msg_flags;
740 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700741 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742
Allan Stephens05646c92007-06-10 17:25:24 -0700743 hdr_size = msg_hdr_sz(&tport->phdr);
744
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 while (curr_iovlen--) {
746 curr_start = curr_iov->iov_base;
747 curr_left = curr_iov->iov_len;
748
749 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700750 bytes_to_send = tport->max_pkt - hdr_size;
751 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
752 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
753 if (curr_left < bytes_to_send)
754 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 my_iov.iov_base = curr_start;
756 my_iov.iov_len = bytes_to_send;
Allan Stephens26896902011-04-21 10:42:07 -0500757 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000758 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700759 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700760 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700761 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700762 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763 curr_left -= bytes_to_send;
764 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700765 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766 }
767
768 curr_iov++;
769 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700770 res = bytes_sent;
771exit:
772 release_sock(sk);
773 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774}
775
776/**
777 * auto_connect - complete connection setup to a remote port
778 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900780 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100781 * Returns 0 on success, errno otherwise
782 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700783static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100784{
Allan Stephens2da59912008-07-14 22:43:32 -0700785 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100786
787 if (msg_errcode(msg)) {
788 sock->state = SS_DISCONNECTING;
789 return -ECONNREFUSED;
790 }
791
Allan Stephens2da59912008-07-14 22:43:32 -0700792 tsock->peer_name.ref = msg_origport(msg);
793 tsock->peer_name.node = msg_orignode(msg);
794 tipc_connect2port(tsock->p->ref, &tsock->peer_name);
795 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 sock->state = SS_CONNECTED;
797 return 0;
798}
799
800/**
801 * set_orig_addr - capture sender's address for received message
802 * @m: descriptor for message info
803 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900804 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805 * Note: Address is not captured if not requested by receiver.
806 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800807static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900809 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900811 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812 addr->family = AF_TIPC;
813 addr->addrtype = TIPC_ADDR_ID;
814 addr->addr.id.ref = msg_origport(msg);
815 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000816 addr->addr.name.domain = 0; /* could leave uninitialized */
817 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100818 m->msg_namelen = sizeof(struct sockaddr_tipc);
819 }
820}
821
822/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900823 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824 * @m: descriptor for message info
825 * @msg: received message header
826 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900827 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900829 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830 * Returns 0 if successful, otherwise errno
831 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800832static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100833 struct tipc_port *tport)
834{
835 u32 anc_data[3];
836 u32 err;
837 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700838 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100839 int res;
840
841 if (likely(m->msg_controllen == 0))
842 return 0;
843
844 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100845 err = msg ? msg_errcode(msg) : 0;
846 if (unlikely(err)) {
847 anc_data[0] = err;
848 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000849 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
850 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000852 if (anc_data[1]) {
853 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
854 msg_data(msg));
855 if (res)
856 return res;
857 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100858 }
859
860 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
862 switch (dest_type) {
863 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700864 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865 anc_data[0] = msg_nametype(msg);
866 anc_data[1] = msg_namelower(msg);
867 anc_data[2] = msg_namelower(msg);
868 break;
869 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700870 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871 anc_data[0] = msg_nametype(msg);
872 anc_data[1] = msg_namelower(msg);
873 anc_data[2] = msg_nameupper(msg);
874 break;
875 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700876 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100877 anc_data[0] = tport->conn_type;
878 anc_data[1] = tport->conn_instance;
879 anc_data[2] = tport->conn_instance;
880 break;
881 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700882 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100883 }
Allan Stephens2db99832010-12-31 18:59:33 +0000884 if (has_name) {
885 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
886 if (res)
887 return res;
888 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889
890 return 0;
891}
892
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900893/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100894 * recv_msg - receive packet-oriented message
895 * @iocb: (unused)
896 * @m: descriptor for message info
897 * @buf_len: total size of user buffer area
898 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900899 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
901 * If the complete message doesn't fit in user area, truncate it.
902 *
903 * Returns size of returned message data, errno otherwise
904 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905static int recv_msg(struct kiocb *iocb, struct socket *sock,
906 struct msghdr *m, size_t buf_len, int flags)
907{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700908 struct sock *sk = sock->sk;
909 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910 struct sk_buff *buf;
911 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500912 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913 unsigned int sz;
914 u32 err;
915 int res;
916
Allan Stephens0c3141e2008-04-15 00:22:02 -0700917 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100918 if (unlikely(!buf_len))
919 return -EINVAL;
920
Allan Stephens0c3141e2008-04-15 00:22:02 -0700921 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922
Allan Stephens0c3141e2008-04-15 00:22:02 -0700923 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100924 res = -ENOTCONN;
925 goto exit;
926 }
927
Allan Stephens71092ea2011-02-23 14:52:14 -0500928 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700929restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100930
Allan Stephens0c3141e2008-04-15 00:22:02 -0700931 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700932 while (skb_queue_empty(&sk->sk_receive_queue)) {
933 if (sock->state == SS_DISCONNECTING) {
934 res = -ENOTCONN;
935 goto exit;
936 }
Allan Stephens71092ea2011-02-23 14:52:14 -0500937 if (timeout <= 0L) {
938 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700939 goto exit;
940 }
941 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -0500942 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
943 tipc_rx_ready(sock),
944 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700945 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700946 }
947
948 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700949 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100950 msg = buf_msg(buf);
951 sz = msg_data_sz(msg);
952 err = msg_errcode(msg);
953
954 /* Complete connection setup for an implied connect */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955 if (unlikely(sock->state == SS_CONNECTING)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700956 res = auto_connect(sock, msg);
957 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958 goto exit;
959 }
960
961 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100962 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700963 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964 goto restart;
965 }
966
967 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968 set_orig_addr(m, msg);
969
970 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700971 res = anc_data_recv(m, msg, tport);
972 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100973 goto exit;
974
975 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976 if (!err) {
977 if (unlikely(buf_len < sz)) {
978 sz = buf_len;
979 m->msg_flags |= MSG_TRUNC;
980 }
Allan Stephens0232fd02011-02-21 09:45:40 -0500981 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
982 m->msg_iov, sz);
983 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985 res = sz;
986 } else {
987 if ((sock->state == SS_READY) ||
988 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
989 res = 0;
990 else
991 res = -ECONNRESET;
992 }
993
994 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens990098062008-04-15 00:06:12 -0700996 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -0700997 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
998 tipc_acknowledge(tport->ref, tport->conn_unacked);
999 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001000 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001001exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001002 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001003 return res;
1004}
1005
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001006/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 * recv_stream - receive stream-oriented data
1008 * @iocb: (unused)
1009 * @m: descriptor for message info
1010 * @buf_len: total size of user buffer area
1011 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001012 *
1013 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014 * will optionally wait for more; never truncates data.
1015 *
1016 * Returns size of returned message data, errno otherwise
1017 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018static int recv_stream(struct kiocb *iocb, struct socket *sock,
1019 struct msghdr *m, size_t buf_len, int flags)
1020{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001021 struct sock *sk = sock->sk;
1022 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023 struct sk_buff *buf;
1024 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -05001025 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001027 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001030 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031
1032 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001033 if (unlikely(!buf_len))
1034 return -EINVAL;
1035
Allan Stephens0c3141e2008-04-15 00:22:02 -07001036 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037
Allan Stephens0c3141e2008-04-15 00:22:02 -07001038 if (unlikely((sock->state == SS_UNCONNECTED) ||
1039 (sock->state == SS_CONNECTING))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 res = -ENOTCONN;
1041 goto exit;
1042 }
1043
Florian Westphal3720d402010-08-17 11:00:04 +00001044 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Allan Stephens71092ea2011-02-23 14:52:14 -05001045 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001046
Allan Stephens0c3141e2008-04-15 00:22:02 -07001047restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001048 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001049 while (skb_queue_empty(&sk->sk_receive_queue)) {
1050 if (sock->state == SS_DISCONNECTING) {
1051 res = -ENOTCONN;
1052 goto exit;
1053 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001054 if (timeout <= 0L) {
1055 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001056 goto exit;
1057 }
1058 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001059 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1060 tipc_rx_ready(sock),
1061 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001062 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001063 }
1064
1065 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001066 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001067 msg = buf_msg(buf);
1068 sz = msg_data_sz(msg);
1069 err = msg_errcode(msg);
1070
1071 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001073 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 goto restart;
1075 }
1076
1077 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 if (sz_copied == 0) {
1079 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001080 res = anc_data_recv(m, msg, tport);
1081 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 goto exit;
1083 }
1084
1085 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001086 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001087 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001088
Allan Stephens0232fd02011-02-21 09:45:40 -05001089 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090 needed = (buf_len - sz_copied);
1091 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001092
1093 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1094 m->msg_iov, sz_to_copy);
1095 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001097
Per Lidenb97bf3f2006-01-02 19:04:38 +01001098 sz_copied += sz_to_copy;
1099
1100 if (sz_to_copy < sz) {
1101 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001102 TIPC_SKB_CB(buf)->handle =
1103 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001104 goto exit;
1105 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001106 } else {
1107 if (sz_copied != 0)
1108 goto exit; /* can't add error msg to valid data */
1109
1110 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1111 res = 0;
1112 else
1113 res = -ECONNRESET;
1114 }
1115
1116 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001118 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1119 tipc_acknowledge(tport->ref, tport->conn_unacked);
1120 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001121 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122
1123 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001124 if ((sz_copied < buf_len) && /* didn't get all requested data */
1125 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001126 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001127 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1128 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001129 goto restart;
1130
1131exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001132 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001133 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134}
1135
1136/**
Ying Xuef288bef2012-08-21 11:16:57 +08001137 * tipc_write_space - wake up thread if port congestion is released
1138 * @sk: socket
1139 */
1140static void tipc_write_space(struct sock *sk)
1141{
1142 struct socket_wq *wq;
1143
1144 rcu_read_lock();
1145 wq = rcu_dereference(sk->sk_wq);
1146 if (wq_has_sleeper(wq))
1147 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1148 POLLWRNORM | POLLWRBAND);
1149 rcu_read_unlock();
1150}
1151
1152/**
1153 * tipc_data_ready - wake up threads to indicate messages have been received
1154 * @sk: socket
1155 * @len: the length of messages
1156 */
1157static void tipc_data_ready(struct sock *sk, int len)
1158{
1159 struct socket_wq *wq;
1160
1161 rcu_read_lock();
1162 wq = rcu_dereference(sk->sk_wq);
1163 if (wq_has_sleeper(wq))
1164 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1165 POLLRDNORM | POLLRDBAND);
1166 rcu_read_unlock();
1167}
1168
1169/**
Allan Stephens1819b832008-04-15 00:15:50 -07001170 * rx_queue_full - determine if receive queue can accept another message
1171 * @msg: message to be added to queue
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172 * @queue_size: current size of queue
1173 * @base: nominal maximum size of queue
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001174 *
Allan Stephens1819b832008-04-15 00:15:50 -07001175 * Returns 1 if queue is unable to accept message, 0 otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176 */
Allan Stephens1819b832008-04-15 00:15:50 -07001177static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178{
1179 u32 threshold;
1180 u32 imp = msg_importance(msg);
1181
1182 if (imp == TIPC_LOW_IMPORTANCE)
1183 threshold = base;
1184 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1185 threshold = base * 2;
1186 else if (imp == TIPC_HIGH_IMPORTANCE)
1187 threshold = base * 100;
1188 else
1189 return 0;
1190
1191 if (msg_connected(msg))
1192 threshold *= 4;
1193
Eric Dumazeta02cec22010-09-22 20:43:57 +00001194 return queue_size >= threshold;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195}
1196
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001197/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001198 * filter_rcv - validate incoming message
1199 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001201 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001202 * Enqueues message on receive queue if acceptable; optionally handles
1203 * disconnect indication for a connected socket.
1204 *
1205 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001206 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1208 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001209static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001210{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001211 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001212 struct tipc_msg *msg = buf_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213 u32 recv_q_len;
1214
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001216 if (msg_type(msg) > TIPC_DIRECT_MSG)
1217 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001218
Per Lidenb97bf3f2006-01-02 19:04:38 +01001219 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001220 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001221 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222 } else {
Allan Stephensb29f1422010-12-31 18:59:25 +00001223 if (msg_mcast(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001224 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225 if (sock->state == SS_CONNECTED) {
Allan Stephensf0712e862012-04-17 18:42:28 -04001226 if (!msg_connected(msg) ||
1227 !tipc_port_peer_msg(tipc_sk_port(sk), msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 return TIPC_ERR_NO_PORT;
Allan Stephensb29f1422010-12-31 18:59:25 +00001229 } else if (sock->state == SS_CONNECTING) {
1230 if (!msg_connected(msg) && (msg_errcode(msg) == 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 return TIPC_ERR_NO_PORT;
Allan Stephensb29f1422010-12-31 18:59:25 +00001232 } else if (sock->state == SS_LISTENING) {
1233 if (msg_connected(msg) || msg_errcode(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 return TIPC_ERR_NO_PORT;
Allan Stephensb29f1422010-12-31 18:59:25 +00001235 } else if (sock->state == SS_DISCONNECTING) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 return TIPC_ERR_NO_PORT;
Allan Stephensb29f1422010-12-31 18:59:25 +00001237 } else /* (sock->state == SS_UNCONNECTED) */ {
1238 if (msg_connected(msg) || msg_errcode(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001239 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001240 }
1241 }
1242
1243 /* Reject message if there isn't room to queue it */
Allan Stephens1819b832008-04-15 00:15:50 -07001244 recv_q_len = (u32)atomic_read(&tipc_queue_size);
1245 if (unlikely(recv_q_len >= OVERLOAD_LIMIT_BASE)) {
1246 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001247 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001248 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001249 recv_q_len = skb_queue_len(&sk->sk_receive_queue);
Allan Stephens1819b832008-04-15 00:15:50 -07001250 if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
1251 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001253 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001254
Allan Stephens0c3141e2008-04-15 00:22:02 -07001255 /* Enqueue message (finally!) */
Allan Stephens0232fd02011-02-21 09:45:40 -05001256 TIPC_SKB_CB(buf)->handle = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001257 atomic_inc(&tipc_queue_size);
1258 __skb_queue_tail(&sk->sk_receive_queue, buf);
1259
Per Lidenb97bf3f2006-01-02 19:04:38 +01001260 /* Initiate connection termination for an incoming 'FIN' */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1262 sock->state = SS_DISCONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001263 tipc_disconnect_port(tipc_sk_port(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264 }
1265
Ying Xuef288bef2012-08-21 11:16:57 +08001266 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001267 return TIPC_OK;
1268}
1269
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001270/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001271 * backlog_rcv - handle incoming message from backlog queue
1272 * @sk: socket
1273 * @buf: message
1274 *
1275 * Caller must hold socket lock, but not port lock.
1276 *
1277 * Returns 0
1278 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001279static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1280{
1281 u32 res;
1282
1283 res = filter_rcv(sk, buf);
1284 if (res)
1285 tipc_reject_msg(buf, res);
1286 return 0;
1287}
1288
1289/**
1290 * dispatch - handle incoming message
1291 * @tport: TIPC port that received message
1292 * @buf: message
1293 *
1294 * Called with port lock already taken.
1295 *
1296 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1297 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001298static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1299{
1300 struct sock *sk = (struct sock *)tport->usr_handle;
1301 u32 res;
1302
1303 /*
1304 * Process message if socket is unlocked; otherwise add to backlog queue
1305 *
1306 * This code is based on sk_receive_skb(), but must be distinct from it
1307 * since a TIPC-specific filter/reject mechanism is utilized
1308 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001309 bh_lock_sock(sk);
1310 if (!sock_owned_by_user(sk)) {
1311 res = filter_rcv(sk, buf);
1312 } else {
Eric Dumazetf545a382012-04-22 23:34:26 +00001313 if (sk_add_backlog(sk, buf, sk->sk_rcvbuf))
Zhu Yi53eecb12010-03-04 18:01:45 +00001314 res = TIPC_ERR_OVERLOAD;
1315 else
1316 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001317 }
1318 bh_unlock_sock(sk);
1319
1320 return res;
1321}
1322
1323/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324 * wakeupdispatch - wake up port after congestion
1325 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001326 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001327 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001329static void wakeupdispatch(struct tipc_port *tport)
1330{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001331 struct sock *sk = (struct sock *)tport->usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332
Ying Xuef288bef2012-08-21 11:16:57 +08001333 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334}
1335
1336/**
1337 * connect - establish a connection to another TIPC port
1338 * @sock: socket structure
1339 * @dest: socket address for destination port
1340 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001341 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342 *
1343 * Returns 0 on success, errno otherwise
1344 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001345static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346 int flags)
1347{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001348 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001349 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1350 struct msghdr m = {NULL,};
1351 struct sk_buff *buf;
1352 struct tipc_msg *msg;
Allan Stephensa0f40f02011-05-26 13:44:34 -04001353 unsigned int timeout;
Allan Stephensb89741a2008-04-15 00:20:37 -07001354 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355
Allan Stephens0c3141e2008-04-15 00:22:02 -07001356 lock_sock(sk);
1357
Allan Stephensb89741a2008-04-15 00:20:37 -07001358 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001359 if (sock->state == SS_READY) {
1360 res = -EOPNOTSUPP;
1361 goto exit;
1362 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363
Allan Stephensb89741a2008-04-15 00:20:37 -07001364 /* For now, TIPC does not support the non-blocking form of connect() */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001365 if (flags & O_NONBLOCK) {
Allan Stephens35997e32010-08-17 11:00:05 +00001366 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001367 goto exit;
1368 }
Allan Stephens4934c692008-04-15 00:16:19 -07001369
Allan Stephensb89741a2008-04-15 00:20:37 -07001370 /* Issue Posix-compliant error code if socket is in the wrong state */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001371 if (sock->state == SS_LISTENING) {
1372 res = -EOPNOTSUPP;
1373 goto exit;
1374 }
1375 if (sock->state == SS_CONNECTING) {
1376 res = -EALREADY;
1377 goto exit;
1378 }
1379 if (sock->state != SS_UNCONNECTED) {
1380 res = -EISCONN;
1381 goto exit;
1382 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383
Allan Stephensb89741a2008-04-15 00:20:37 -07001384 /*
1385 * Reject connection attempt using multicast address
1386 *
1387 * Note: send_msg() validates the rest of the address fields,
1388 * so there's no need to do it here
1389 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001390 if (dst->addrtype == TIPC_ADDR_MCAST) {
1391 res = -EINVAL;
1392 goto exit;
1393 }
1394
1395 /* Reject any messages already in receive queue (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001396 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001397
Allan Stephensb89741a2008-04-15 00:20:37 -07001398 /* Send a 'SYN-' to destination */
Allan Stephensb89741a2008-04-15 00:20:37 -07001399 m.msg_name = dest;
1400 m.msg_namelen = destlen;
1401 res = send_msg(NULL, sock, &m, 0);
Allan Stephensa0168922010-12-31 18:59:35 +00001402 if (res < 0)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001403 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001404
Allan Stephens0c3141e2008-04-15 00:22:02 -07001405 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
Allan Stephens564e83b2010-08-17 11:00:15 +00001406 timeout = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001407 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001408 res = wait_event_interruptible_timeout(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001409 (!skb_queue_empty(&sk->sk_receive_queue) ||
1410 (sock->state != SS_CONNECTING)),
Allan Stephensa0f40f02011-05-26 13:44:34 -04001411 timeout ? (long)msecs_to_jiffies(timeout)
1412 : MAX_SCHEDULE_TIMEOUT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001413 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001414
Allan Stephensb89741a2008-04-15 00:20:37 -07001415 if (res > 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001416 buf = skb_peek(&sk->sk_receive_queue);
1417 if (buf != NULL) {
1418 msg = buf_msg(buf);
1419 res = auto_connect(sock, msg);
1420 if (!res) {
1421 if (!msg_data_sz(msg))
1422 advance_rx_queue(sk);
1423 }
1424 } else {
Allan Stephensa0168922010-12-31 18:59:35 +00001425 if (sock->state == SS_CONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001426 res = -EISCONN;
Allan Stephensa0168922010-12-31 18:59:35 +00001427 else
Allan Stephens0c3141e2008-04-15 00:22:02 -07001428 res = -ECONNREFUSED;
Allan Stephensb89741a2008-04-15 00:20:37 -07001429 }
1430 } else {
1431 if (res == 0)
1432 res = -ETIMEDOUT;
1433 else
1434 ; /* leave "res" unchanged */
1435 sock->state = SS_DISCONNECTING;
1436 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001437
Allan Stephens0c3141e2008-04-15 00:22:02 -07001438exit:
1439 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001440 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001441}
1442
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001443/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444 * listen - allow socket to listen for incoming connections
1445 * @sock: socket structure
1446 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001447 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448 * Returns 0 on success, errno otherwise
1449 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001450static int listen(struct socket *sock, int len)
1451{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001452 struct sock *sk = sock->sk;
1453 int res;
1454
1455 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001456
Ying Xue245f3d32011-07-06 06:01:13 -04001457 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001458 res = -EINVAL;
1459 else {
1460 sock->state = SS_LISTENING;
1461 res = 0;
1462 }
1463
1464 release_sock(sk);
1465 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001466}
1467
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001468/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001469 * accept - wait for connection request
1470 * @sock: listening socket
1471 * @newsock: new socket that is to be connected
1472 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001473 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001474 * Returns 0 on success, errno otherwise
1475 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001476static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001477{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001478 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001480 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001481
Allan Stephens0c3141e2008-04-15 00:22:02 -07001482 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001483
Allan Stephens0c3141e2008-04-15 00:22:02 -07001484 if (sock->state != SS_LISTENING) {
1485 res = -EINVAL;
1486 goto exit;
1487 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488
Allan Stephens0c3141e2008-04-15 00:22:02 -07001489 while (skb_queue_empty(&sk->sk_receive_queue)) {
1490 if (flags & O_NONBLOCK) {
1491 res = -EWOULDBLOCK;
1492 goto exit;
1493 }
1494 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001495 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001496 (!skb_queue_empty(&sk->sk_receive_queue)));
1497 lock_sock(sk);
1498 if (res)
1499 goto exit;
1500 }
1501
1502 buf = skb_peek(&sk->sk_receive_queue);
1503
Eric Paris3f378b62009-11-05 22:18:14 -08001504 res = tipc_create(sock_net(sock->sk), new_sock, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001505 if (!res) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001506 struct sock *new_sk = new_sock->sk;
Allan Stephens2da59912008-07-14 22:43:32 -07001507 struct tipc_sock *new_tsock = tipc_sk(new_sk);
1508 struct tipc_port *new_tport = new_tsock->p;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001509 u32 new_ref = new_tport->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001510 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001511
1512 lock_sock(new_sk);
1513
1514 /*
1515 * Reject any stray messages received by new socket
1516 * before the socket lock was taken (very, very unlikely)
1517 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001518 reject_rx_queue(new_sk);
1519
1520 /* Connect new socket to it's peer */
Allan Stephens2da59912008-07-14 22:43:32 -07001521 new_tsock->peer_name.ref = msg_origport(msg);
1522 new_tsock->peer_name.node = msg_orignode(msg);
1523 tipc_connect2port(new_ref, &new_tsock->peer_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001524 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001525
1526 tipc_set_portimportance(new_ref, msg_importance(msg));
1527 if (msg_named(msg)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001528 new_tport->conn_type = msg_nametype(msg);
1529 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001530 }
1531
Allan Stephens0c3141e2008-04-15 00:22:02 -07001532 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001533 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1534 * Respond to 'SYN+' by queuing it on new socket.
1535 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001536 if (!msg_data_sz(msg)) {
1537 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001538
Allan Stephens0c3141e2008-04-15 00:22:02 -07001539 advance_rx_queue(sk);
1540 send_packet(NULL, new_sock, &m, 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001541 } else {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001542 __skb_dequeue(&sk->sk_receive_queue);
1543 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001544 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001545 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001546 }
1547exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001548 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001549 return res;
1550}
1551
1552/**
1553 * shutdown - shutdown socket connection
1554 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001555 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001556 *
1557 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001558 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001559 * Returns 0 on success, errno otherwise
1560 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001561static int shutdown(struct socket *sock, int how)
1562{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001563 struct sock *sk = sock->sk;
1564 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001565 struct sk_buff *buf;
1566 int res;
1567
Allan Stephense247a8f2008-03-06 15:05:38 -08001568 if (how != SHUT_RDWR)
1569 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001570
Allan Stephens0c3141e2008-04-15 00:22:02 -07001571 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001572
1573 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001574 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001575 case SS_CONNECTED:
1576
Per Lidenb97bf3f2006-01-02 19:04:38 +01001577restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001578 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001579 buf = __skb_dequeue(&sk->sk_receive_queue);
1580 if (buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001581 atomic_dec(&tipc_queue_size);
Allan Stephens0232fd02011-02-21 09:45:40 -05001582 if (TIPC_SKB_CB(buf)->handle != 0) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001583 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001584 goto restart;
1585 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001586 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001587 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001588 } else {
1589 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001590 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001591
1592 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001593
1594 /* fall through */
1595
1596 case SS_DISCONNECTING:
1597
Allan Stephens0c3141e2008-04-15 00:22:02 -07001598 /* Discard any unreceived messages; wake up sleeping tasks */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001599 discard_rx_queue(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001600 if (waitqueue_active(sk_sleep(sk)))
1601 wake_up_interruptible(sk_sleep(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001602 res = 0;
1603 break;
1604
1605 default:
1606 res = -ENOTCONN;
1607 }
1608
Allan Stephens0c3141e2008-04-15 00:22:02 -07001609 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001610 return res;
1611}
1612
1613/**
1614 * setsockopt - set socket option
1615 * @sock: socket structure
1616 * @lvl: option level
1617 * @opt: option identifier
1618 * @ov: pointer to new option value
1619 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001620 *
1621 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001622 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001623 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001624 * Returns 0 on success, errno otherwise
1625 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001626static int setsockopt(struct socket *sock,
David S. Millerb7058842009-09-30 16:12:20 -07001627 int lvl, int opt, char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001628{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001629 struct sock *sk = sock->sk;
1630 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001631 u32 value;
1632 int res;
1633
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001634 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1635 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001636 if (lvl != SOL_TIPC)
1637 return -ENOPROTOOPT;
1638 if (ol < sizeof(value))
1639 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001640 res = get_user(value, (u32 __user *)ov);
1641 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001642 return res;
1643
Allan Stephens0c3141e2008-04-15 00:22:02 -07001644 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001645
Per Lidenb97bf3f2006-01-02 19:04:38 +01001646 switch (opt) {
1647 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001648 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001649 break;
1650 case TIPC_SRC_DROPPABLE:
1651 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001652 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001653 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001654 res = -ENOPROTOOPT;
1655 break;
1656 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001657 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001658 break;
1659 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001660 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001661 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001662 break;
1663 default:
1664 res = -EINVAL;
1665 }
1666
Allan Stephens0c3141e2008-04-15 00:22:02 -07001667 release_sock(sk);
1668
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669 return res;
1670}
1671
1672/**
1673 * getsockopt - get socket option
1674 * @sock: socket structure
1675 * @lvl: option level
1676 * @opt: option identifier
1677 * @ov: receptacle for option value
1678 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001679 *
1680 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001681 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001682 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683 * Returns 0 on success, errno otherwise
1684 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001685static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001686 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001687{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001688 struct sock *sk = sock->sk;
1689 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001690 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001691 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001692 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001693
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001694 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1695 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696 if (lvl != SOL_TIPC)
1697 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001698 res = get_user(len, ol);
1699 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001700 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701
Allan Stephens0c3141e2008-04-15 00:22:02 -07001702 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001703
1704 switch (opt) {
1705 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001706 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001707 break;
1708 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001709 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001710 break;
1711 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001712 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001713 break;
1714 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001715 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001716 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001717 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001718 case TIPC_NODE_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001719 value = (u32)atomic_read(&tipc_queue_size);
1720 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001721 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001722 value = skb_queue_len(&sk->sk_receive_queue);
1723 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001724 default:
1725 res = -EINVAL;
1726 }
1727
Allan Stephens0c3141e2008-04-15 00:22:02 -07001728 release_sock(sk);
1729
Paul Gortmaker25860c32010-12-31 18:59:31 +00001730 if (res)
1731 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001732
Paul Gortmaker25860c32010-12-31 18:59:31 +00001733 if (len < sizeof(value))
1734 return -EINVAL;
1735
1736 if (copy_to_user(ov, &value, sizeof(value)))
1737 return -EFAULT;
1738
1739 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001740}
1741
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001742/* Protocol switches for the various types of TIPC sockets */
1743
Florian Westphalbca65ea2008-02-07 18:18:01 -08001744static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001745 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746 .family = AF_TIPC,
1747 .release = release,
1748 .bind = bind,
1749 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001750 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001751 .accept = sock_no_accept,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001752 .getname = get_name,
1753 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001754 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001755 .listen = sock_no_listen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001756 .shutdown = shutdown,
1757 .setsockopt = setsockopt,
1758 .getsockopt = getsockopt,
1759 .sendmsg = send_msg,
1760 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001761 .mmap = sock_no_mmap,
1762 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001763};
1764
Florian Westphalbca65ea2008-02-07 18:18:01 -08001765static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001766 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001767 .family = AF_TIPC,
1768 .release = release,
1769 .bind = bind,
1770 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001771 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001772 .accept = accept,
1773 .getname = get_name,
1774 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001775 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001776 .listen = listen,
1777 .shutdown = shutdown,
1778 .setsockopt = setsockopt,
1779 .getsockopt = getsockopt,
1780 .sendmsg = send_packet,
1781 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001782 .mmap = sock_no_mmap,
1783 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001784};
1785
Florian Westphalbca65ea2008-02-07 18:18:01 -08001786static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001787 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001788 .family = AF_TIPC,
1789 .release = release,
1790 .bind = bind,
1791 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001792 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793 .accept = accept,
1794 .getname = get_name,
1795 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001796 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001797 .listen = listen,
1798 .shutdown = shutdown,
1799 .setsockopt = setsockopt,
1800 .getsockopt = getsockopt,
1801 .sendmsg = send_stream,
1802 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001803 .mmap = sock_no_mmap,
1804 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001805};
1806
Florian Westphalbca65ea2008-02-07 18:18:01 -08001807static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001808 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001809 .family = AF_TIPC,
1810 .create = tipc_create
1811};
1812
1813static struct proto tipc_proto = {
1814 .name = "TIPC",
1815 .owner = THIS_MODULE,
1816 .obj_size = sizeof(struct tipc_sock)
1817};
1818
1819/**
Per Liden4323add2006-01-18 00:38:21 +01001820 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001821 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001822 * Returns 0 on success, errno otherwise
1823 */
Per Liden4323add2006-01-18 00:38:21 +01001824int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001825{
1826 int res;
1827
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001828 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001830 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001831 goto out;
1832 }
1833
1834 res = sock_register(&tipc_family_ops);
1835 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001836 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001837 proto_unregister(&tipc_proto);
1838 goto out;
1839 }
1840
1841 sockets_enabled = 1;
1842 out:
1843 return res;
1844}
1845
1846/**
Per Liden4323add2006-01-18 00:38:21 +01001847 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001848 */
Per Liden4323add2006-01-18 00:38:21 +01001849void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001850{
1851 if (!sockets_enabled)
1852 return;
1853
1854 sockets_enabled = 0;
1855 sock_unregister(tipc_family_ops.family);
1856 proto_unregister(&tipc_proto);
1857}