blob: b5fc8ed1d1fec0dbccc524ed2645f91d9fbeed55 [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
415 * no write flags
416 *
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) {
445 case SS_READY:
446 case SS_CONNECTED:
447 if (!tipc_sk_port(sk)->congested)
448 mask |= POLLOUT;
449 /* fall thru' */
450 case SS_CONNECTING:
451 case SS_LISTENING:
452 if (!skb_queue_empty(&sk->sk_receive_queue))
453 mask |= (POLLIN | POLLRDNORM);
454 break;
455 case SS_DISCONNECTING:
456 mask = (POLLIN | POLLRDNORM | POLLHUP);
457 break;
458 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700459
460 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461}
462
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900463/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 * dest_name_check - verify user is permitted to send to specified port name
465 * @dest: destination address
466 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900467 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468 * Prevents restricted configuration commands from being issued by
469 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900470 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471 * Returns 0 if permission is granted, otherwise errno
472 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800473static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474{
475 struct tipc_cfg_msg_hdr hdr;
476
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900477 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
478 return 0;
479 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
480 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900481 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
482 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483
Allan Stephens3f8dd942011-01-18 13:09:29 -0500484 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
485 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900486 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700488 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900490
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 return 0;
492}
493
494/**
495 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700496 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 * @sock: socket structure
498 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700499 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900500 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900502 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
504 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900505 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 * Returns the number of bytes sent on success, or errno otherwise
507 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508static int send_msg(struct kiocb *iocb, struct socket *sock,
509 struct msghdr *m, size_t total_len)
510{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700511 struct sock *sk = sock->sk;
512 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900513 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514 int needs_conn;
Ying Xue1d835872011-07-06 05:53:15 -0400515 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516 int res = -EINVAL;
517
518 if (unlikely(!dest))
519 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700520 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
521 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522 return -EINVAL;
Allan Stephensc29c3f72010-04-20 17:58:24 -0400523 if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
Eric Dumazet95c96172012-04-15 05:58:06 +0000524 (m->msg_iovlen > (unsigned int)INT_MAX))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400525 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526
Allan Stephens0c3141e2008-04-15 00:22:02 -0700527 if (iocb)
528 lock_sock(sk);
529
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530 needs_conn = (sock->state != SS_READY);
531 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700532 if (sock->state == SS_LISTENING) {
533 res = -EPIPE;
534 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700535 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700536 if (sock->state != SS_UNCONNECTED) {
537 res = -EISCONN;
538 goto exit;
539 }
540 if ((tport->published) ||
541 ((sock->type == SOCK_STREAM) && (total_len != 0))) {
542 res = -EOPNOTSUPP;
543 goto exit;
544 }
545 if (dest->addrtype == TIPC_ADDR_NAME) {
546 tport->conn_type = dest->addr.name.name.type;
547 tport->conn_instance = dest->addr.name.name.instance;
548 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549
550 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700551 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 }
553
Ying Xue1d835872011-07-06 05:53:15 -0400554 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
555
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900556 do {
557 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000558 res = dest_name_check(dest, m);
559 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700560 break;
561 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900562 &dest->addr.name.name,
563 dest->addr.name.domain,
564 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500565 m->msg_iov,
566 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000567 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700568 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900569 &dest->addr.id,
570 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500571 m->msg_iov,
572 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000573 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574 if (needs_conn) {
575 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700576 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577 }
Allan Stephens2db99832010-12-31 18:59:33 +0000578 res = dest_name_check(dest, m);
579 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700580 break;
581 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900582 &dest->addr.nameseq,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900583 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500584 m->msg_iov,
585 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900586 }
587 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000588 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700589 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700590 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900591 }
Ying Xue1d835872011-07-06 05:53:15 -0400592 if (timeout_val <= 0L) {
593 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700594 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700596 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400597 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
598 !tport->congested, timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700599 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900600 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700601
602exit:
603 if (iocb)
604 release_sock(sk);
605 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606}
607
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900608/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700610 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 * @sock: socket structure
612 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700613 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900614 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900616 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 * Returns the number of bytes sent on success, or errno otherwise
618 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619static int send_packet(struct kiocb *iocb, struct socket *sock,
620 struct msghdr *m, size_t total_len)
621{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700622 struct sock *sk = sock->sk;
623 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900624 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Ying Xue1d835872011-07-06 05:53:15 -0400625 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 int res;
627
628 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 if (unlikely(dest))
630 return send_msg(iocb, sock, m, total_len);
631
Allan Stephensc29c3f72010-04-20 17:58:24 -0400632 if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
Eric Dumazet95c96172012-04-15 05:58:06 +0000633 (m->msg_iovlen > (unsigned int)INT_MAX))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400634 return -EMSGSIZE;
635
Allan Stephens0c3141e2008-04-15 00:22:02 -0700636 if (iocb)
637 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638
Ying Xue1d835872011-07-06 05:53:15 -0400639 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
640
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900641 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700642 if (unlikely(sock->state != SS_CONNECTED)) {
643 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900644 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700645 else
646 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700647 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700648 }
649
Allan Stephens26896902011-04-21 10:42:07 -0500650 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov,
651 total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000652 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700653 break;
Ying Xue1d835872011-07-06 05:53:15 -0400654 if (timeout_val <= 0L) {
655 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700656 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700658 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400659 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
660 (!tport->congested || !tport->connected), timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700661 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900662 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700663
664 if (iocb)
665 release_sock(sk);
666 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667}
668
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900669/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 * send_stream - send stream-oriented data
671 * @iocb: (unused)
672 * @sock: socket structure
673 * @m: data to send
674 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900675 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900677 *
678 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700679 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681static int send_stream(struct kiocb *iocb, struct socket *sock,
682 struct msghdr *m, size_t total_len)
683{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700684 struct sock *sk = sock->sk;
685 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686 struct msghdr my_msg;
687 struct iovec my_iov;
688 struct iovec *curr_iov;
689 int curr_iovlen;
690 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700691 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100692 int curr_left;
693 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700694 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100695 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900696
Allan Stephens0c3141e2008-04-15 00:22:02 -0700697 lock_sock(sk);
698
Allan Stephens05646c92007-06-10 17:25:24 -0700699 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900700 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700701 if (sock->state == SS_UNCONNECTED) {
702 res = send_packet(NULL, sock, m, total_len);
703 goto exit;
704 } else if (sock->state == SS_DISCONNECTING) {
705 res = -EPIPE;
706 goto exit;
707 } else {
708 res = -ENOTCONN;
709 goto exit;
710 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900711 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712
Allan Stephens0c3141e2008-04-15 00:22:02 -0700713 if (unlikely(m->msg_name)) {
714 res = -EISCONN;
715 goto exit;
716 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700717
Eric Dumazet95c96172012-04-15 05:58:06 +0000718 if ((total_len > (unsigned int)INT_MAX) ||
719 (m->msg_iovlen > (unsigned int)INT_MAX)) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400720 res = -EMSGSIZE;
721 goto exit;
722 }
723
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900724 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100725 * Send each iovec entry using one or more messages
726 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900727 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100728 * (i.e. one large iovec entry), but could be improved to pass sets
729 * of small iovec entries into send_packet().
730 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700731 curr_iov = m->msg_iov;
732 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733 my_msg.msg_iov = &my_iov;
734 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700735 my_msg.msg_flags = m->msg_flags;
736 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700737 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100738
Allan Stephens05646c92007-06-10 17:25:24 -0700739 hdr_size = msg_hdr_sz(&tport->phdr);
740
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741 while (curr_iovlen--) {
742 curr_start = curr_iov->iov_base;
743 curr_left = curr_iov->iov_len;
744
745 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700746 bytes_to_send = tport->max_pkt - hdr_size;
747 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
748 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
749 if (curr_left < bytes_to_send)
750 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751 my_iov.iov_base = curr_start;
752 my_iov.iov_len = bytes_to_send;
Allan Stephens26896902011-04-21 10:42:07 -0500753 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000754 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700755 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700756 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700757 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700758 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100759 curr_left -= bytes_to_send;
760 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700761 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100762 }
763
764 curr_iov++;
765 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700766 res = bytes_sent;
767exit:
768 release_sock(sk);
769 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100770}
771
772/**
773 * auto_connect - complete connection setup to a remote port
774 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900776 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100777 * Returns 0 on success, errno otherwise
778 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700779static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780{
Allan Stephens2da59912008-07-14 22:43:32 -0700781 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782
783 if (msg_errcode(msg)) {
784 sock->state = SS_DISCONNECTING;
785 return -ECONNREFUSED;
786 }
787
Allan Stephens2da59912008-07-14 22:43:32 -0700788 tsock->peer_name.ref = msg_origport(msg);
789 tsock->peer_name.node = msg_orignode(msg);
790 tipc_connect2port(tsock->p->ref, &tsock->peer_name);
791 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792 sock->state = SS_CONNECTED;
793 return 0;
794}
795
796/**
797 * set_orig_addr - capture sender's address for received message
798 * @m: descriptor for message info
799 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900800 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100801 * Note: Address is not captured if not requested by receiver.
802 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800803static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900805 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100806
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900807 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100808 addr->family = AF_TIPC;
809 addr->addrtype = TIPC_ADDR_ID;
810 addr->addr.id.ref = msg_origport(msg);
811 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000812 addr->addr.name.domain = 0; /* could leave uninitialized */
813 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100814 m->msg_namelen = sizeof(struct sockaddr_tipc);
815 }
816}
817
818/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900819 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100820 * @m: descriptor for message info
821 * @msg: received message header
822 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900823 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900825 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100826 * Returns 0 if successful, otherwise errno
827 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800828static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829 struct tipc_port *tport)
830{
831 u32 anc_data[3];
832 u32 err;
833 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700834 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100835 int res;
836
837 if (likely(m->msg_controllen == 0))
838 return 0;
839
840 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100841 err = msg ? msg_errcode(msg) : 0;
842 if (unlikely(err)) {
843 anc_data[0] = err;
844 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000845 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
846 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000848 if (anc_data[1]) {
849 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
850 msg_data(msg));
851 if (res)
852 return res;
853 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854 }
855
856 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
858 switch (dest_type) {
859 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700860 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861 anc_data[0] = msg_nametype(msg);
862 anc_data[1] = msg_namelower(msg);
863 anc_data[2] = msg_namelower(msg);
864 break;
865 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700866 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867 anc_data[0] = msg_nametype(msg);
868 anc_data[1] = msg_namelower(msg);
869 anc_data[2] = msg_nameupper(msg);
870 break;
871 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700872 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873 anc_data[0] = tport->conn_type;
874 anc_data[1] = tport->conn_instance;
875 anc_data[2] = tport->conn_instance;
876 break;
877 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700878 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879 }
Allan Stephens2db99832010-12-31 18:59:33 +0000880 if (has_name) {
881 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
882 if (res)
883 return res;
884 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885
886 return 0;
887}
888
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900889/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100890 * recv_msg - receive packet-oriented message
891 * @iocb: (unused)
892 * @m: descriptor for message info
893 * @buf_len: total size of user buffer area
894 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900895 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100896 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
897 * If the complete message doesn't fit in user area, truncate it.
898 *
899 * Returns size of returned message data, errno otherwise
900 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901static int recv_msg(struct kiocb *iocb, struct socket *sock,
902 struct msghdr *m, size_t buf_len, int flags)
903{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700904 struct sock *sk = sock->sk;
905 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906 struct sk_buff *buf;
907 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500908 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100909 unsigned int sz;
910 u32 err;
911 int res;
912
Allan Stephens0c3141e2008-04-15 00:22:02 -0700913 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914 if (unlikely(!buf_len))
915 return -EINVAL;
916
Allan Stephens0c3141e2008-04-15 00:22:02 -0700917 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100918
Allan Stephens0c3141e2008-04-15 00:22:02 -0700919 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100920 res = -ENOTCONN;
921 goto exit;
922 }
923
Allan Stephens71092ea2011-02-23 14:52:14 -0500924 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700925restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926
Allan Stephens0c3141e2008-04-15 00:22:02 -0700927 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700928 while (skb_queue_empty(&sk->sk_receive_queue)) {
929 if (sock->state == SS_DISCONNECTING) {
930 res = -ENOTCONN;
931 goto exit;
932 }
Allan Stephens71092ea2011-02-23 14:52:14 -0500933 if (timeout <= 0L) {
934 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700935 goto exit;
936 }
937 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -0500938 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
939 tipc_rx_ready(sock),
940 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700941 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700942 }
943
944 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700945 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946 msg = buf_msg(buf);
947 sz = msg_data_sz(msg);
948 err = msg_errcode(msg);
949
950 /* Complete connection setup for an implied connect */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951 if (unlikely(sock->state == SS_CONNECTING)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700952 res = auto_connect(sock, msg);
953 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100954 goto exit;
955 }
956
957 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100958 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700959 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100960 goto restart;
961 }
962
963 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964 set_orig_addr(m, msg);
965
966 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700967 res = anc_data_recv(m, msg, tport);
968 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100969 goto exit;
970
971 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100972 if (!err) {
973 if (unlikely(buf_len < sz)) {
974 sz = buf_len;
975 m->msg_flags |= MSG_TRUNC;
976 }
Allan Stephens0232fd02011-02-21 09:45:40 -0500977 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
978 m->msg_iov, sz);
979 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100981 res = sz;
982 } else {
983 if ((sock->state == SS_READY) ||
984 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
985 res = 0;
986 else
987 res = -ECONNRESET;
988 }
989
990 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -0700992 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -0700993 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
994 tipc_acknowledge(tport->ref, tport->conn_unacked);
995 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900996 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100997exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700998 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100999 return res;
1000}
1001
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001002/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001003 * recv_stream - receive stream-oriented data
1004 * @iocb: (unused)
1005 * @m: descriptor for message info
1006 * @buf_len: total size of user buffer area
1007 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001008 *
1009 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010 * will optionally wait for more; never truncates data.
1011 *
1012 * Returns size of returned message data, errno otherwise
1013 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014static int recv_stream(struct kiocb *iocb, struct socket *sock,
1015 struct msghdr *m, size_t buf_len, int flags)
1016{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001017 struct sock *sk = sock->sk;
1018 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001019 struct sk_buff *buf;
1020 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -05001021 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001022 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001023 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001026 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001027
1028 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 if (unlikely(!buf_len))
1030 return -EINVAL;
1031
Allan Stephens0c3141e2008-04-15 00:22:02 -07001032 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001033
Allan Stephens0c3141e2008-04-15 00:22:02 -07001034 if (unlikely((sock->state == SS_UNCONNECTED) ||
1035 (sock->state == SS_CONNECTING))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036 res = -ENOTCONN;
1037 goto exit;
1038 }
1039
Florian Westphal3720d402010-08-17 11:00:04 +00001040 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Allan Stephens71092ea2011-02-23 14:52:14 -05001041 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001042
Allan Stephens0c3141e2008-04-15 00:22:02 -07001043restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001044 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001045 while (skb_queue_empty(&sk->sk_receive_queue)) {
1046 if (sock->state == SS_DISCONNECTING) {
1047 res = -ENOTCONN;
1048 goto exit;
1049 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001050 if (timeout <= 0L) {
1051 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001052 goto exit;
1053 }
1054 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001055 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1056 tipc_rx_ready(sock),
1057 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001058 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001059 }
1060
1061 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001062 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001063 msg = buf_msg(buf);
1064 sz = msg_data_sz(msg);
1065 err = msg_errcode(msg);
1066
1067 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001069 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001070 goto restart;
1071 }
1072
1073 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 if (sz_copied == 0) {
1075 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001076 res = anc_data_recv(m, msg, tport);
1077 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 goto exit;
1079 }
1080
1081 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001083 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084
Allan Stephens0232fd02011-02-21 09:45:40 -05001085 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001086 needed = (buf_len - sz_copied);
1087 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001088
1089 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1090 m->msg_iov, sz_to_copy);
1091 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001093
Per Lidenb97bf3f2006-01-02 19:04:38 +01001094 sz_copied += sz_to_copy;
1095
1096 if (sz_to_copy < sz) {
1097 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001098 TIPC_SKB_CB(buf)->handle =
1099 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001100 goto exit;
1101 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001102 } else {
1103 if (sz_copied != 0)
1104 goto exit; /* can't add error msg to valid data */
1105
1106 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1107 res = 0;
1108 else
1109 res = -ECONNRESET;
1110 }
1111
1112 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001113 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001114 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1115 tipc_acknowledge(tport->ref, tport->conn_unacked);
1116 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001117 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118
1119 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001120 if ((sz_copied < buf_len) && /* didn't get all requested data */
1121 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001122 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001123 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1124 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001125 goto restart;
1126
1127exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001128 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001129 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001130}
1131
1132/**
Ying Xuef288bef2012-08-21 11:16:57 +08001133 * tipc_write_space - wake up thread if port congestion is released
1134 * @sk: socket
1135 */
1136static void tipc_write_space(struct sock *sk)
1137{
1138 struct socket_wq *wq;
1139
1140 rcu_read_lock();
1141 wq = rcu_dereference(sk->sk_wq);
1142 if (wq_has_sleeper(wq))
1143 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1144 POLLWRNORM | POLLWRBAND);
1145 rcu_read_unlock();
1146}
1147
1148/**
1149 * tipc_data_ready - wake up threads to indicate messages have been received
1150 * @sk: socket
1151 * @len: the length of messages
1152 */
1153static void tipc_data_ready(struct sock *sk, int len)
1154{
1155 struct socket_wq *wq;
1156
1157 rcu_read_lock();
1158 wq = rcu_dereference(sk->sk_wq);
1159 if (wq_has_sleeper(wq))
1160 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1161 POLLRDNORM | POLLRDBAND);
1162 rcu_read_unlock();
1163}
1164
1165/**
Allan Stephens1819b832008-04-15 00:15:50 -07001166 * rx_queue_full - determine if receive queue can accept another message
1167 * @msg: message to be added to queue
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 * @queue_size: current size of queue
1169 * @base: nominal maximum size of queue
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001170 *
Allan Stephens1819b832008-04-15 00:15:50 -07001171 * Returns 1 if queue is unable to accept message, 0 otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172 */
Allan Stephens1819b832008-04-15 00:15:50 -07001173static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174{
1175 u32 threshold;
1176 u32 imp = msg_importance(msg);
1177
1178 if (imp == TIPC_LOW_IMPORTANCE)
1179 threshold = base;
1180 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1181 threshold = base * 2;
1182 else if (imp == TIPC_HIGH_IMPORTANCE)
1183 threshold = base * 100;
1184 else
1185 return 0;
1186
1187 if (msg_connected(msg))
1188 threshold *= 4;
1189
Eric Dumazeta02cec22010-09-22 20:43:57 +00001190 return queue_size >= threshold;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191}
1192
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001193/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001194 * filter_rcv - validate incoming message
1195 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001197 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001198 * Enqueues message on receive queue if acceptable; optionally handles
1199 * disconnect indication for a connected socket.
1200 *
1201 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001202 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1204 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001205static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001207 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 struct tipc_msg *msg = buf_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001209 u32 recv_q_len;
1210
Per Lidenb97bf3f2006-01-02 19:04:38 +01001211 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001212 if (msg_type(msg) > TIPC_DIRECT_MSG)
1213 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001214
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001216 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001217 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218 } else {
Allan Stephensb29f1422010-12-31 18:59:25 +00001219 if (msg_mcast(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001221 if (sock->state == SS_CONNECTED) {
Allan Stephensf0712e82012-04-17 18:42:28 -04001222 if (!msg_connected(msg) ||
1223 !tipc_port_peer_msg(tipc_sk_port(sk), msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001224 return TIPC_ERR_NO_PORT;
Allan Stephensb29f1422010-12-31 18:59:25 +00001225 } else if (sock->state == SS_CONNECTING) {
1226 if (!msg_connected(msg) && (msg_errcode(msg) == 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001227 return TIPC_ERR_NO_PORT;
Allan Stephensb29f1422010-12-31 18:59:25 +00001228 } else if (sock->state == SS_LISTENING) {
1229 if (msg_connected(msg) || msg_errcode(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230 return TIPC_ERR_NO_PORT;
Allan Stephensb29f1422010-12-31 18:59:25 +00001231 } else if (sock->state == SS_DISCONNECTING) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232 return TIPC_ERR_NO_PORT;
Allan Stephensb29f1422010-12-31 18:59:25 +00001233 } else /* (sock->state == SS_UNCONNECTED) */ {
1234 if (msg_connected(msg) || msg_errcode(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 }
1237 }
1238
1239 /* Reject message if there isn't room to queue it */
Allan Stephens1819b832008-04-15 00:15:50 -07001240 recv_q_len = (u32)atomic_read(&tipc_queue_size);
1241 if (unlikely(recv_q_len >= OVERLOAD_LIMIT_BASE)) {
1242 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001243 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001244 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001245 recv_q_len = skb_queue_len(&sk->sk_receive_queue);
Allan Stephens1819b832008-04-15 00:15:50 -07001246 if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
1247 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001248 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001249 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001250
Allan Stephens0c3141e2008-04-15 00:22:02 -07001251 /* Enqueue message (finally!) */
Allan Stephens0232fd02011-02-21 09:45:40 -05001252 TIPC_SKB_CB(buf)->handle = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001253 atomic_inc(&tipc_queue_size);
1254 __skb_queue_tail(&sk->sk_receive_queue, buf);
1255
Per Lidenb97bf3f2006-01-02 19:04:38 +01001256 /* Initiate connection termination for an incoming 'FIN' */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001257 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1258 sock->state = SS_DISCONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001259 tipc_disconnect_port(tipc_sk_port(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001260 }
1261
Ying Xuef288bef2012-08-21 11:16:57 +08001262 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001263 return TIPC_OK;
1264}
1265
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001266/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001267 * backlog_rcv - handle incoming message from backlog queue
1268 * @sk: socket
1269 * @buf: message
1270 *
1271 * Caller must hold socket lock, but not port lock.
1272 *
1273 * Returns 0
1274 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001275static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1276{
1277 u32 res;
1278
1279 res = filter_rcv(sk, buf);
1280 if (res)
1281 tipc_reject_msg(buf, res);
1282 return 0;
1283}
1284
1285/**
1286 * dispatch - handle incoming message
1287 * @tport: TIPC port that received message
1288 * @buf: message
1289 *
1290 * Called with port lock already taken.
1291 *
1292 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1293 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001294static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1295{
1296 struct sock *sk = (struct sock *)tport->usr_handle;
1297 u32 res;
1298
1299 /*
1300 * Process message if socket is unlocked; otherwise add to backlog queue
1301 *
1302 * This code is based on sk_receive_skb(), but must be distinct from it
1303 * since a TIPC-specific filter/reject mechanism is utilized
1304 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001305 bh_lock_sock(sk);
1306 if (!sock_owned_by_user(sk)) {
1307 res = filter_rcv(sk, buf);
1308 } else {
Eric Dumazetf545a382012-04-22 23:34:26 +00001309 if (sk_add_backlog(sk, buf, sk->sk_rcvbuf))
Zhu Yi53eecb12010-03-04 18:01:45 +00001310 res = TIPC_ERR_OVERLOAD;
1311 else
1312 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001313 }
1314 bh_unlock_sock(sk);
1315
1316 return res;
1317}
1318
1319/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 * wakeupdispatch - wake up port after congestion
1321 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001322 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001323 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001324 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001325static void wakeupdispatch(struct tipc_port *tport)
1326{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001327 struct sock *sk = (struct sock *)tport->usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328
Ying Xuef288bef2012-08-21 11:16:57 +08001329 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001330}
1331
1332/**
1333 * connect - establish a connection to another TIPC port
1334 * @sock: socket structure
1335 * @dest: socket address for destination port
1336 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001337 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 *
1339 * Returns 0 on success, errno otherwise
1340 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001341static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342 int flags)
1343{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001344 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001345 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1346 struct msghdr m = {NULL,};
1347 struct sk_buff *buf;
1348 struct tipc_msg *msg;
Allan Stephensa0f40f02011-05-26 13:44:34 -04001349 unsigned int timeout;
Allan Stephensb89741a2008-04-15 00:20:37 -07001350 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351
Allan Stephens0c3141e2008-04-15 00:22:02 -07001352 lock_sock(sk);
1353
Allan Stephensb89741a2008-04-15 00:20:37 -07001354 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001355 if (sock->state == SS_READY) {
1356 res = -EOPNOTSUPP;
1357 goto exit;
1358 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359
Allan Stephensb89741a2008-04-15 00:20:37 -07001360 /* For now, TIPC does not support the non-blocking form of connect() */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001361 if (flags & O_NONBLOCK) {
Allan Stephens35997e32010-08-17 11:00:05 +00001362 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001363 goto exit;
1364 }
Allan Stephens4934c692008-04-15 00:16:19 -07001365
Allan Stephensb89741a2008-04-15 00:20:37 -07001366 /* Issue Posix-compliant error code if socket is in the wrong state */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001367 if (sock->state == SS_LISTENING) {
1368 res = -EOPNOTSUPP;
1369 goto exit;
1370 }
1371 if (sock->state == SS_CONNECTING) {
1372 res = -EALREADY;
1373 goto exit;
1374 }
1375 if (sock->state != SS_UNCONNECTED) {
1376 res = -EISCONN;
1377 goto exit;
1378 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001379
Allan Stephensb89741a2008-04-15 00:20:37 -07001380 /*
1381 * Reject connection attempt using multicast address
1382 *
1383 * Note: send_msg() validates the rest of the address fields,
1384 * so there's no need to do it here
1385 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001386 if (dst->addrtype == TIPC_ADDR_MCAST) {
1387 res = -EINVAL;
1388 goto exit;
1389 }
1390
1391 /* Reject any messages already in receive queue (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001392 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001393
Allan Stephensb89741a2008-04-15 00:20:37 -07001394 /* Send a 'SYN-' to destination */
Allan Stephensb89741a2008-04-15 00:20:37 -07001395 m.msg_name = dest;
1396 m.msg_namelen = destlen;
1397 res = send_msg(NULL, sock, &m, 0);
Allan Stephensa0168922010-12-31 18:59:35 +00001398 if (res < 0)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001399 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001400
Allan Stephens0c3141e2008-04-15 00:22:02 -07001401 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
Allan Stephens564e83b2010-08-17 11:00:15 +00001402 timeout = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001403 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001404 res = wait_event_interruptible_timeout(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001405 (!skb_queue_empty(&sk->sk_receive_queue) ||
1406 (sock->state != SS_CONNECTING)),
Allan Stephensa0f40f02011-05-26 13:44:34 -04001407 timeout ? (long)msecs_to_jiffies(timeout)
1408 : MAX_SCHEDULE_TIMEOUT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001409 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001410
Allan Stephensb89741a2008-04-15 00:20:37 -07001411 if (res > 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001412 buf = skb_peek(&sk->sk_receive_queue);
1413 if (buf != NULL) {
1414 msg = buf_msg(buf);
1415 res = auto_connect(sock, msg);
1416 if (!res) {
1417 if (!msg_data_sz(msg))
1418 advance_rx_queue(sk);
1419 }
1420 } else {
Allan Stephensa0168922010-12-31 18:59:35 +00001421 if (sock->state == SS_CONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001422 res = -EISCONN;
Allan Stephensa0168922010-12-31 18:59:35 +00001423 else
Allan Stephens0c3141e2008-04-15 00:22:02 -07001424 res = -ECONNREFUSED;
Allan Stephensb89741a2008-04-15 00:20:37 -07001425 }
1426 } else {
1427 if (res == 0)
1428 res = -ETIMEDOUT;
1429 else
1430 ; /* leave "res" unchanged */
1431 sock->state = SS_DISCONNECTING;
1432 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433
Allan Stephens0c3141e2008-04-15 00:22:02 -07001434exit:
1435 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001436 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001437}
1438
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001439/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001440 * listen - allow socket to listen for incoming connections
1441 * @sock: socket structure
1442 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001443 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444 * Returns 0 on success, errno otherwise
1445 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001446static int listen(struct socket *sock, int len)
1447{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001448 struct sock *sk = sock->sk;
1449 int res;
1450
1451 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001452
Ying Xue245f3d32011-07-06 06:01:13 -04001453 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001454 res = -EINVAL;
1455 else {
1456 sock->state = SS_LISTENING;
1457 res = 0;
1458 }
1459
1460 release_sock(sk);
1461 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001462}
1463
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001464/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001465 * accept - wait for connection request
1466 * @sock: listening socket
1467 * @newsock: new socket that is to be connected
1468 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001469 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470 * Returns 0 on success, errno otherwise
1471 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001472static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001474 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001476 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001477
Allan Stephens0c3141e2008-04-15 00:22:02 -07001478 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001479
Allan Stephens0c3141e2008-04-15 00:22:02 -07001480 if (sock->state != SS_LISTENING) {
1481 res = -EINVAL;
1482 goto exit;
1483 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484
Allan Stephens0c3141e2008-04-15 00:22:02 -07001485 while (skb_queue_empty(&sk->sk_receive_queue)) {
1486 if (flags & O_NONBLOCK) {
1487 res = -EWOULDBLOCK;
1488 goto exit;
1489 }
1490 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001491 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001492 (!skb_queue_empty(&sk->sk_receive_queue)));
1493 lock_sock(sk);
1494 if (res)
1495 goto exit;
1496 }
1497
1498 buf = skb_peek(&sk->sk_receive_queue);
1499
Eric Paris3f378b62009-11-05 22:18:14 -08001500 res = tipc_create(sock_net(sock->sk), new_sock, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001501 if (!res) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001502 struct sock *new_sk = new_sock->sk;
Allan Stephens2da59912008-07-14 22:43:32 -07001503 struct tipc_sock *new_tsock = tipc_sk(new_sk);
1504 struct tipc_port *new_tport = new_tsock->p;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001505 u32 new_ref = new_tport->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001506 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001507
1508 lock_sock(new_sk);
1509
1510 /*
1511 * Reject any stray messages received by new socket
1512 * before the socket lock was taken (very, very unlikely)
1513 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001514 reject_rx_queue(new_sk);
1515
1516 /* Connect new socket to it's peer */
Allan Stephens2da59912008-07-14 22:43:32 -07001517 new_tsock->peer_name.ref = msg_origport(msg);
1518 new_tsock->peer_name.node = msg_orignode(msg);
1519 tipc_connect2port(new_ref, &new_tsock->peer_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001520 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001521
1522 tipc_set_portimportance(new_ref, msg_importance(msg));
1523 if (msg_named(msg)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001524 new_tport->conn_type = msg_nametype(msg);
1525 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001526 }
1527
Allan Stephens0c3141e2008-04-15 00:22:02 -07001528 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001529 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1530 * Respond to 'SYN+' by queuing it on new socket.
1531 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001532 if (!msg_data_sz(msg)) {
1533 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001534
Allan Stephens0c3141e2008-04-15 00:22:02 -07001535 advance_rx_queue(sk);
1536 send_packet(NULL, new_sock, &m, 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001537 } else {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001538 __skb_dequeue(&sk->sk_receive_queue);
1539 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001540 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001541 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001542 }
1543exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001544 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001545 return res;
1546}
1547
1548/**
1549 * shutdown - shutdown socket connection
1550 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001551 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001552 *
1553 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001554 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001555 * Returns 0 on success, errno otherwise
1556 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001557static int shutdown(struct socket *sock, int how)
1558{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001559 struct sock *sk = sock->sk;
1560 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001561 struct sk_buff *buf;
1562 int res;
1563
Allan Stephense247a8f2008-03-06 15:05:38 -08001564 if (how != SHUT_RDWR)
1565 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001566
Allan Stephens0c3141e2008-04-15 00:22:02 -07001567 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001568
1569 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001570 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001571 case SS_CONNECTED:
1572
Per Lidenb97bf3f2006-01-02 19:04:38 +01001573restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001574 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001575 buf = __skb_dequeue(&sk->sk_receive_queue);
1576 if (buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001577 atomic_dec(&tipc_queue_size);
Allan Stephens0232fd02011-02-21 09:45:40 -05001578 if (TIPC_SKB_CB(buf)->handle != 0) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001579 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001580 goto restart;
1581 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001582 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001583 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001584 } else {
1585 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001586 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001587
1588 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001589
1590 /* fall through */
1591
1592 case SS_DISCONNECTING:
1593
Allan Stephens0c3141e2008-04-15 00:22:02 -07001594 /* Discard any unreceived messages; wake up sleeping tasks */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001595 discard_rx_queue(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001596 if (waitqueue_active(sk_sleep(sk)))
1597 wake_up_interruptible(sk_sleep(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001598 res = 0;
1599 break;
1600
1601 default:
1602 res = -ENOTCONN;
1603 }
1604
Allan Stephens0c3141e2008-04-15 00:22:02 -07001605 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001606 return res;
1607}
1608
1609/**
1610 * setsockopt - set socket option
1611 * @sock: socket structure
1612 * @lvl: option level
1613 * @opt: option identifier
1614 * @ov: pointer to new option value
1615 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001616 *
1617 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001618 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001619 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001620 * Returns 0 on success, errno otherwise
1621 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001622static int setsockopt(struct socket *sock,
David S. Millerb7058842009-09-30 16:12:20 -07001623 int lvl, int opt, char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001624{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001625 struct sock *sk = sock->sk;
1626 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001627 u32 value;
1628 int res;
1629
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001630 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1631 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001632 if (lvl != SOL_TIPC)
1633 return -ENOPROTOOPT;
1634 if (ol < sizeof(value))
1635 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001636 res = get_user(value, (u32 __user *)ov);
1637 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001638 return res;
1639
Allan Stephens0c3141e2008-04-15 00:22:02 -07001640 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001641
Per Lidenb97bf3f2006-01-02 19:04:38 +01001642 switch (opt) {
1643 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001644 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001645 break;
1646 case TIPC_SRC_DROPPABLE:
1647 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001648 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001649 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001650 res = -ENOPROTOOPT;
1651 break;
1652 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001653 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001654 break;
1655 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001656 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001657 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001658 break;
1659 default:
1660 res = -EINVAL;
1661 }
1662
Allan Stephens0c3141e2008-04-15 00:22:02 -07001663 release_sock(sk);
1664
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665 return res;
1666}
1667
1668/**
1669 * getsockopt - get socket option
1670 * @sock: socket structure
1671 * @lvl: option level
1672 * @opt: option identifier
1673 * @ov: receptacle for option value
1674 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001675 *
1676 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001677 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001678 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679 * Returns 0 on success, errno otherwise
1680 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001681static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001682 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001684 struct sock *sk = sock->sk;
1685 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001686 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001687 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001688 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001689
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001690 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1691 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001692 if (lvl != SOL_TIPC)
1693 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001694 res = get_user(len, ol);
1695 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001696 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001697
Allan Stephens0c3141e2008-04-15 00:22:02 -07001698 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699
1700 switch (opt) {
1701 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001702 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001703 break;
1704 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001705 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001706 break;
1707 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001708 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001709 break;
1710 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001711 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001712 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001713 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001714 case TIPC_NODE_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001715 value = (u32)atomic_read(&tipc_queue_size);
1716 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001717 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001718 value = skb_queue_len(&sk->sk_receive_queue);
1719 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001720 default:
1721 res = -EINVAL;
1722 }
1723
Allan Stephens0c3141e2008-04-15 00:22:02 -07001724 release_sock(sk);
1725
Paul Gortmaker25860c32010-12-31 18:59:31 +00001726 if (res)
1727 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001728
Paul Gortmaker25860c32010-12-31 18:59:31 +00001729 if (len < sizeof(value))
1730 return -EINVAL;
1731
1732 if (copy_to_user(ov, &value, sizeof(value)))
1733 return -EFAULT;
1734
1735 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001736}
1737
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001738/* Protocol switches for the various types of TIPC sockets */
1739
Florian Westphalbca65ea2008-02-07 18:18:01 -08001740static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001741 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001742 .family = AF_TIPC,
1743 .release = release,
1744 .bind = bind,
1745 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001746 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001747 .accept = sock_no_accept,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001748 .getname = get_name,
1749 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001750 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001751 .listen = sock_no_listen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001752 .shutdown = shutdown,
1753 .setsockopt = setsockopt,
1754 .getsockopt = getsockopt,
1755 .sendmsg = send_msg,
1756 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001757 .mmap = sock_no_mmap,
1758 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001759};
1760
Florian Westphalbca65ea2008-02-07 18:18:01 -08001761static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001762 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001763 .family = AF_TIPC,
1764 .release = release,
1765 .bind = bind,
1766 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001767 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001768 .accept = accept,
1769 .getname = get_name,
1770 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001771 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001772 .listen = listen,
1773 .shutdown = shutdown,
1774 .setsockopt = setsockopt,
1775 .getsockopt = getsockopt,
1776 .sendmsg = send_packet,
1777 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001778 .mmap = sock_no_mmap,
1779 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001780};
1781
Florian Westphalbca65ea2008-02-07 18:18:01 -08001782static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001783 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001784 .family = AF_TIPC,
1785 .release = release,
1786 .bind = bind,
1787 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001788 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001789 .accept = accept,
1790 .getname = get_name,
1791 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001792 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793 .listen = listen,
1794 .shutdown = shutdown,
1795 .setsockopt = setsockopt,
1796 .getsockopt = getsockopt,
1797 .sendmsg = send_stream,
1798 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001799 .mmap = sock_no_mmap,
1800 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001801};
1802
Florian Westphalbca65ea2008-02-07 18:18:01 -08001803static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001804 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001805 .family = AF_TIPC,
1806 .create = tipc_create
1807};
1808
1809static struct proto tipc_proto = {
1810 .name = "TIPC",
1811 .owner = THIS_MODULE,
1812 .obj_size = sizeof(struct tipc_sock)
1813};
1814
1815/**
Per Liden4323add2006-01-18 00:38:21 +01001816 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001817 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001818 * Returns 0 on success, errno otherwise
1819 */
Per Liden4323add2006-01-18 00:38:21 +01001820int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001821{
1822 int res;
1823
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001824 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001825 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001826 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001827 goto out;
1828 }
1829
1830 res = sock_register(&tipc_family_ops);
1831 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001832 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001833 proto_unregister(&tipc_proto);
1834 goto out;
1835 }
1836
1837 sockets_enabled = 1;
1838 out:
1839 return res;
1840}
1841
1842/**
Per Liden4323add2006-01-18 00:38:21 +01001843 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001844 */
Per Liden4323add2006-01-18 00:38:21 +01001845void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001846{
1847 if (!sockets_enabled)
1848 return;
1849
1850 sockets_enabled = 0;
1851 sock_unregister(tipc_family_ops.family);
1852 proto_unregister(&tipc_proto);
1853}