blob: f553fdecc1d2019ef48e5efcb03a31b16d6777ec [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Maloye643df12012-11-27 06:15:29 -05004 * Copyright (c) 2001-2007, 2012 Ericsson AB
Ying Xue9da3d472012-11-27 06:15:27 -05005 * Copyright (c) 2004-2008, 2010-2012, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000038#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039
Erik Hugne2cf8aa12012-06-29 00:16:37 -040040#include <linux/export.h>
41#include <net/sock.h>
42
Per Lidenb97bf3f2006-01-02 19:04:38 +010043#define SS_LISTENING -1 /* socket is listening */
44#define SS_READY -2 /* socket is connectionless */
45
Jon Maloye643df12012-11-27 06:15:29 -050046#define OVERLOAD_LIMIT_BASE 10000
Allan Stephens3654ea02008-04-13 21:35:11 -070047#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010048
49struct tipc_sock {
50 struct sock sk;
51 struct tipc_port *p;
Allan Stephens2da59912008-07-14 22:43:32 -070052 struct tipc_portid peer_name;
Allan Stephensa0f40f02011-05-26 13:44:34 -040053 unsigned int conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +010054};
55
Allan Stephens0c3141e2008-04-15 00:22:02 -070056#define tipc_sk(sk) ((struct tipc_sock *)(sk))
Joe Perchese3192692012-06-03 17:41:40 +000057#define tipc_sk_port(sk) (tipc_sk(sk)->p)
Per Lidenb97bf3f2006-01-02 19:04:38 +010058
Allan Stephens71092ea2011-02-23 14:52:14 -050059#define tipc_rx_ready(sock) (!skb_queue_empty(&sock->sk->sk_receive_queue) || \
60 (sock->state == SS_DISCONNECTING))
61
Allan Stephens0c3141e2008-04-15 00:22:02 -070062static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +010063static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
64static void wakeupdispatch(struct tipc_port *tport);
Ying Xuef288bef2012-08-21 11:16:57 +080065static void tipc_data_ready(struct sock *sk, int len);
66static void tipc_write_space(struct sock *sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +010067
Florian Westphalbca65ea2008-02-07 18:18:01 -080068static const struct proto_ops packet_ops;
69static const struct proto_ops stream_ops;
70static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010071
72static struct proto tipc_proto;
73
Allan Stephense3ec9c72010-12-31 18:59:34 +000074static int sockets_enabled;
Per Lidenb97bf3f2006-01-02 19:04:38 +010075
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090076/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070077 * Revised TIPC socket locking policy:
78 *
79 * Most socket operations take the standard socket lock when they start
80 * and hold it until they finish (or until they need to sleep). Acquiring
81 * this lock grants the owner exclusive access to the fields of the socket
82 * data structures, with the exception of the backlog queue. A few socket
83 * operations can be done without taking the socket lock because they only
84 * read socket information that never changes during the life of the socket.
85 *
86 * Socket operations may acquire the lock for the associated TIPC port if they
87 * need to perform an operation on the port. If any routine needs to acquire
88 * both the socket lock and the port lock it must take the socket lock first
89 * to avoid the risk of deadlock.
90 *
91 * The dispatcher handling incoming messages cannot grab the socket lock in
92 * the standard fashion, since invoked it runs at the BH level and cannot block.
93 * Instead, it checks to see if the socket lock is currently owned by someone,
94 * and either handles the message itself or adds it to the socket's backlog
95 * queue; in the latter case the queued message is processed once the process
96 * owning the socket lock releases it.
97 *
98 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
99 * the problem of a blocked socket operation preventing any other operations
100 * from occurring. However, applications must be careful if they have
101 * multiple threads trying to send (or receive) on the same socket, as these
102 * operations might interfere with each other. For example, doing a connect
103 * and a receive at the same time might allow the receive to consume the
104 * ACK message meant for the connect. While additional work could be done
105 * to try and overcome this, it doesn't seem to be worthwhile at the present.
106 *
107 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
108 * that another operation that must be performed in a non-blocking manner is
109 * not delayed for very long because the lock has already been taken.
110 *
111 * NOTE: This code assumes that certain fields of a port/socket pair are
112 * constant over its lifetime; such fields can be examined without taking
113 * the socket lock and/or port lock, and do not need to be re-read even
114 * after resuming processing after waiting. These fields include:
115 * - socket type
116 * - pointer to socket sk structure (aka tipc_sock structure)
117 * - pointer to port structure
118 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120
121/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700122 * advance_rx_queue - discard first buffer in socket receive queue
123 *
124 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700126static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400128 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129}
130
131/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700132 * discard_rx_queue - discard all buffers in socket receive queue
133 *
134 * Caller must hold socket lock
135 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700136static void discard_rx_queue(struct sock *sk)
137{
138 struct sk_buff *buf;
139
Ying Xue9da3d472012-11-27 06:15:27 -0500140 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens5f6d9122011-11-04 13:24:29 -0400141 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700142}
143
144/**
145 * reject_rx_queue - reject all buffers in socket receive queue
146 *
147 * Caller must hold socket lock
148 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700149static void reject_rx_queue(struct sock *sk)
150{
151 struct sk_buff *buf;
152
Ying Xue9da3d472012-11-27 06:15:27 -0500153 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700154 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700155}
156
157/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158 * tipc_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700159 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 * @sock: pre-allocated socket structure
161 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800162 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900163 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700164 * This routine creates additional data structures used by the TIPC socket,
165 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 *
167 * Returns 0 on success, errno otherwise
168 */
Eric Paris3f378b62009-11-05 22:18:14 -0800169static int tipc_create(struct net *net, struct socket *sock, int protocol,
170 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700172 const struct proto_ops *ops;
173 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700175 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700176
177 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 if (unlikely(protocol != 0))
179 return -EPROTONOSUPPORT;
180
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 switch (sock->type) {
182 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700183 ops = &stream_ops;
184 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185 break;
186 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700187 ops = &packet_ops;
188 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100189 break;
190 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700192 ops = &msg_ops;
193 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194 break;
Allan Stephens49978652006-06-25 23:47:18 -0700195 default:
Allan Stephens49978652006-06-25 23:47:18 -0700196 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197 }
198
Allan Stephens0c3141e2008-04-15 00:22:02 -0700199 /* Allocate socket's protocol area */
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700200 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700201 if (sk == NULL)
202 return -ENOMEM;
203
204 /* Allocate TIPC port for socket to use */
Allan Stephens0ea52242008-07-14 22:42:19 -0700205 tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
206 TIPC_LOW_IMPORTANCE);
207 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700208 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209 return -ENOMEM;
210 }
211
Allan Stephens0c3141e2008-04-15 00:22:02 -0700212 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700213 sock->ops = ops;
214 sock->state = state;
215
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700217 sk->sk_backlog_rcv = backlog_rcv;
Erik Hugnee57edf62012-10-04 05:00:43 +0000218 sk->sk_rcvbuf = TIPC_FLOW_CONTROL_WIN * 2 * TIPC_MAX_USER_MSG_SIZE * 2;
Ying Xuef288bef2012-08-21 11:16:57 +0800219 sk->sk_data_ready = tipc_data_ready;
220 sk->sk_write_space = tipc_write_space;
Allan Stephens0ea52242008-07-14 22:42:19 -0700221 tipc_sk(sk)->p = tp_ptr;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400222 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700224 spin_unlock_bh(tp_ptr->lock);
225
Allan Stephens0c3141e2008-04-15 00:22:02 -0700226 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700227 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700228 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700229 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700230 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 return 0;
233}
234
235/**
236 * release - destroy a TIPC socket
237 * @sock: socket to destroy
238 *
239 * This routine cleans up any messages that are still queued on the socket.
240 * For DGRAM and RDM socket types, all queued messages are rejected.
241 * For SEQPACKET and STREAM socket types, the first message is rejected
242 * and any others are discarded. (If the first message on a STREAM socket
243 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900244 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245 * NOTE: Rejected messages are not necessarily returned to the sender! They
246 * are returned or discarded according to the "destination droppable" setting
247 * specified for the message by the sender.
248 *
249 * Returns 0 on success, errno otherwise
250 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251static int release(struct socket *sock)
252{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700254 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700256 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257
Allan Stephens0c3141e2008-04-15 00:22:02 -0700258 /*
259 * Exit if socket isn't fully initialized (occurs when a failed accept()
260 * releases a pre-allocated child socket that was never used)
261 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700262 if (sk == NULL)
263 return 0;
264
265 tport = tipc_sk_port(sk);
266 lock_sock(sk);
267
268 /*
269 * Reject all unreceived messages, except on an active connection
270 * (which disconnects locally & sends a 'FIN+' to peer)
271 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700273 buf = __skb_dequeue(&sk->sk_receive_queue);
274 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275 break;
Allan Stephens0232fd02011-02-21 09:45:40 -0500276 if (TIPC_SKB_CB(buf)->handle != 0)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400277 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700278 else {
279 if ((sock->state == SS_CONNECTING) ||
280 (sock->state == SS_CONNECTED)) {
281 sock->state = SS_DISCONNECTING;
282 tipc_disconnect(tport->ref);
283 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700285 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286 }
287
Allan Stephens0c3141e2008-04-15 00:22:02 -0700288 /*
289 * Delete TIPC port; this ensures no more messages are queued
290 * (also disconnects an active connection & sends a 'FIN-' to peer)
291 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700292 res = tipc_deleteport(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293
Allan Stephens0c3141e2008-04-15 00:22:02 -0700294 /* Discard any remaining (connection-based) messages in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700295 discard_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296
Allan Stephens0c3141e2008-04-15 00:22:02 -0700297 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700298 sock->state = SS_DISCONNECTING;
299 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300
301 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700302 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 return res;
305}
306
307/**
308 * bind - associate or disassocate TIPC name(s) with a socket
309 * @sock: socket structure
310 * @uaddr: socket address describing name(s) and desired operation
311 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900312 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313 * Name and name sequence binding is indicated using a positive scope value;
314 * a negative scope value unbinds the specified name. Specifying no name
315 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900316 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700318 *
319 * NOTE: This routine doesn't need to take the socket lock since it doesn't
320 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
323{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700325 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326
Allan Stephens0c3141e2008-04-15 00:22:02 -0700327 if (unlikely(!uaddr_len))
328 return tipc_withdraw(portref, 0, NULL);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900329
Allan Stephens0c3141e2008-04-15 00:22:02 -0700330 if (uaddr_len < sizeof(struct sockaddr_tipc))
331 return -EINVAL;
332 if (addr->family != AF_TIPC)
333 return -EAFNOSUPPORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335 if (addr->addrtype == TIPC_ADDR_NAME)
336 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700337 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
338 return -EAFNOSUPPORT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900339
Allan Stephensc422f1b2011-11-02 15:49:40 -0400340 if (addr->addr.nameseq.type < TIPC_RESERVED_TYPES)
341 return -EACCES;
342
Allan Stephens0c3141e2008-04-15 00:22:02 -0700343 return (addr->scope > 0) ?
344 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
345 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346}
347
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900348/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 * get_name - get port ID of socket or peer socket
350 * @sock: socket structure
351 * @uaddr: area for returned socket address
352 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700353 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900354 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700356 *
Allan Stephens2da59912008-07-14 22:43:32 -0700357 * NOTE: This routine doesn't need to take the socket lock since it only
358 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000359 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900361static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362 int *uaddr_len, int peer)
363{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700365 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000367 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700368 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700369 if ((sock->state != SS_CONNECTED) &&
370 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
371 return -ENOTCONN;
372 addr->addr.id.ref = tsock->peer_name.ref;
373 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700374 } else {
Allan Stephensb924dcf2010-11-30 12:01:03 +0000375 addr->addr.id.ref = tsock->p->ref;
376 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700377 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378
379 *uaddr_len = sizeof(*addr);
380 addr->addrtype = TIPC_ADDR_ID;
381 addr->family = AF_TIPC;
382 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 addr->addr.name.domain = 0;
384
Allan Stephens0c3141e2008-04-15 00:22:02 -0700385 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386}
387
388/**
389 * poll - read and possibly block on pollmask
390 * @file: file structure associated with the socket
391 * @sock: socket for which to calculate the poll bits
392 * @wait: ???
393 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700394 * Returns pollmask value
395 *
396 * COMMENTARY:
397 * It appears that the usual socket locking mechanisms are not useful here
398 * since the pollmask info is potentially out-of-date the moment this routine
399 * exits. TCP and other protocols seem to rely on higher level poll routines
400 * to handle any preventable race conditions, so TIPC will do the same ...
401 *
402 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700403 *
Allan Stephensf662c072010-08-17 11:00:06 +0000404 * socket state flags set
405 * ------------ ---------
406 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200407 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000408 *
409 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
410 * no write flags
411 *
412 * connected POLLIN/POLLRDNORM if data in rx queue
413 * POLLOUT if port is not congested
414 *
415 * disconnecting POLLIN/POLLRDNORM/POLLHUP
416 * no write flags
417 *
418 * listening POLLIN if SYN in rx queue
419 * no write flags
420 *
421 * ready POLLIN/POLLRDNORM if data in rx queue
422 * [connectionless] POLLOUT (since port cannot be congested)
423 *
424 * IMPORTANT: The fact that a read or write operation is indicated does NOT
425 * imply that the operation will succeed, merely that it should be performed
426 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900428static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429 poll_table *wait)
430{
Allan Stephens9b674e82008-03-26 16:48:21 -0700431 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000432 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700433
Ying Xuef288bef2012-08-21 11:16:57 +0800434 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700435
Allan Stephensf662c072010-08-17 11:00:06 +0000436 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200437 case SS_UNCONNECTED:
438 if (!tipc_sk_port(sk)->congested)
439 mask |= POLLOUT;
440 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000441 case SS_READY:
442 case SS_CONNECTED:
443 if (!tipc_sk_port(sk)->congested)
444 mask |= POLLOUT;
445 /* fall thru' */
446 case SS_CONNECTING:
447 case SS_LISTENING:
448 if (!skb_queue_empty(&sk->sk_receive_queue))
449 mask |= (POLLIN | POLLRDNORM);
450 break;
451 case SS_DISCONNECTING:
452 mask = (POLLIN | POLLRDNORM | POLLHUP);
453 break;
454 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700455
456 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457}
458
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900459/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 * dest_name_check - verify user is permitted to send to specified port name
461 * @dest: destination address
462 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900463 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 * Prevents restricted configuration commands from being issued by
465 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900466 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467 * Returns 0 if permission is granted, otherwise errno
468 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800469static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470{
471 struct tipc_cfg_msg_hdr hdr;
472
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900473 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
474 return 0;
475 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
476 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900477 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
478 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479
Allan Stephens3f8dd942011-01-18 13:09:29 -0500480 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
481 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900482 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700484 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900486
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 return 0;
488}
489
490/**
491 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700492 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493 * @sock: socket structure
494 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700495 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900496 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900498 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
500 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900501 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502 * Returns the number of bytes sent on success, or errno otherwise
503 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504static int send_msg(struct kiocb *iocb, struct socket *sock,
505 struct msghdr *m, size_t total_len)
506{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700507 struct sock *sk = sock->sk;
508 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900509 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100510 int needs_conn;
Ying Xue1d835872011-07-06 05:53:15 -0400511 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512 int res = -EINVAL;
513
514 if (unlikely(!dest))
515 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700516 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
517 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518 return -EINVAL;
Allan Stephensc29c3f72010-04-20 17:58:24 -0400519 if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
Eric Dumazet95c96172012-04-15 05:58:06 +0000520 (m->msg_iovlen > (unsigned int)INT_MAX))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400521 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522
Allan Stephens0c3141e2008-04-15 00:22:02 -0700523 if (iocb)
524 lock_sock(sk);
525
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526 needs_conn = (sock->state != SS_READY);
527 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700528 if (sock->state == SS_LISTENING) {
529 res = -EPIPE;
530 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700531 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700532 if (sock->state != SS_UNCONNECTED) {
533 res = -EISCONN;
534 goto exit;
535 }
536 if ((tport->published) ||
537 ((sock->type == SOCK_STREAM) && (total_len != 0))) {
538 res = -EOPNOTSUPP;
539 goto exit;
540 }
541 if (dest->addrtype == TIPC_ADDR_NAME) {
542 tport->conn_type = dest->addr.name.name.type;
543 tport->conn_instance = dest->addr.name.name.instance;
544 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545
546 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700547 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548 }
549
Ying Xue1d835872011-07-06 05:53:15 -0400550 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
551
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900552 do {
553 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000554 res = dest_name_check(dest, m);
555 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700556 break;
557 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900558 &dest->addr.name.name,
559 dest->addr.name.domain,
560 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500561 m->msg_iov,
562 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000563 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700564 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900565 &dest->addr.id,
566 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500567 m->msg_iov,
568 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000569 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570 if (needs_conn) {
571 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700572 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573 }
Allan Stephens2db99832010-12-31 18:59:33 +0000574 res = dest_name_check(dest, m);
575 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700576 break;
577 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900578 &dest->addr.nameseq,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900579 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500580 m->msg_iov,
581 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900582 }
583 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000584 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700585 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700586 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900587 }
Ying Xue1d835872011-07-06 05:53:15 -0400588 if (timeout_val <= 0L) {
589 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700590 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700592 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400593 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
594 !tport->congested, timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700595 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900596 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700597
598exit:
599 if (iocb)
600 release_sock(sk);
601 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602}
603
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900604/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700606 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 * @sock: socket structure
608 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700609 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900610 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900612 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 * Returns the number of bytes sent on success, or errno otherwise
614 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615static int send_packet(struct kiocb *iocb, struct socket *sock,
616 struct msghdr *m, size_t total_len)
617{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700618 struct sock *sk = sock->sk;
619 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900620 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Ying Xue1d835872011-07-06 05:53:15 -0400621 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622 int res;
623
624 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 if (unlikely(dest))
626 return send_msg(iocb, sock, m, total_len);
627
Allan Stephensc29c3f72010-04-20 17:58:24 -0400628 if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
Eric Dumazet95c96172012-04-15 05:58:06 +0000629 (m->msg_iovlen > (unsigned int)INT_MAX))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400630 return -EMSGSIZE;
631
Allan Stephens0c3141e2008-04-15 00:22:02 -0700632 if (iocb)
633 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634
Ying Xue1d835872011-07-06 05:53:15 -0400635 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
636
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900637 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700638 if (unlikely(sock->state != SS_CONNECTED)) {
639 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900640 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700641 else
642 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700643 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700644 }
645
Allan Stephens26896902011-04-21 10:42:07 -0500646 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov,
647 total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000648 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700649 break;
Ying Xue1d835872011-07-06 05:53:15 -0400650 if (timeout_val <= 0L) {
651 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700652 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700654 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400655 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
656 (!tport->congested || !tport->connected), timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700657 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900658 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700659
660 if (iocb)
661 release_sock(sk);
662 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663}
664
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900665/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 * send_stream - send stream-oriented data
667 * @iocb: (unused)
668 * @sock: socket structure
669 * @m: data to send
670 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900671 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900673 *
674 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700675 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100677static int send_stream(struct kiocb *iocb, struct socket *sock,
678 struct msghdr *m, size_t total_len)
679{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700680 struct sock *sk = sock->sk;
681 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100682 struct msghdr my_msg;
683 struct iovec my_iov;
684 struct iovec *curr_iov;
685 int curr_iovlen;
686 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700687 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100688 int curr_left;
689 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700690 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100691 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900692
Allan Stephens0c3141e2008-04-15 00:22:02 -0700693 lock_sock(sk);
694
Allan Stephens05646c92007-06-10 17:25:24 -0700695 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900696 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700697 if (sock->state == SS_UNCONNECTED) {
698 res = send_packet(NULL, sock, m, total_len);
699 goto exit;
700 } else if (sock->state == SS_DISCONNECTING) {
701 res = -EPIPE;
702 goto exit;
703 } else {
704 res = -ENOTCONN;
705 goto exit;
706 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900707 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708
Allan Stephens0c3141e2008-04-15 00:22:02 -0700709 if (unlikely(m->msg_name)) {
710 res = -EISCONN;
711 goto exit;
712 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700713
Eric Dumazet95c96172012-04-15 05:58:06 +0000714 if ((total_len > (unsigned int)INT_MAX) ||
715 (m->msg_iovlen > (unsigned int)INT_MAX)) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400716 res = -EMSGSIZE;
717 goto exit;
718 }
719
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900720 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100721 * Send each iovec entry using one or more messages
722 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900723 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724 * (i.e. one large iovec entry), but could be improved to pass sets
725 * of small iovec entries into send_packet().
726 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700727 curr_iov = m->msg_iov;
728 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 my_msg.msg_iov = &my_iov;
730 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700731 my_msg.msg_flags = m->msg_flags;
732 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700733 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734
Allan Stephens05646c92007-06-10 17:25:24 -0700735 hdr_size = msg_hdr_sz(&tport->phdr);
736
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737 while (curr_iovlen--) {
738 curr_start = curr_iov->iov_base;
739 curr_left = curr_iov->iov_len;
740
741 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700742 bytes_to_send = tport->max_pkt - hdr_size;
743 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
744 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
745 if (curr_left < bytes_to_send)
746 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747 my_iov.iov_base = curr_start;
748 my_iov.iov_len = bytes_to_send;
Allan Stephens26896902011-04-21 10:42:07 -0500749 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000750 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700751 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700752 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700753 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700754 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 curr_left -= bytes_to_send;
756 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700757 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100758 }
759
760 curr_iov++;
761 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700762 res = bytes_sent;
763exit:
764 release_sock(sk);
765 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766}
767
768/**
769 * auto_connect - complete connection setup to a remote port
770 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900772 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 * Returns 0 on success, errno otherwise
774 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700775static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776{
Allan Stephens2da59912008-07-14 22:43:32 -0700777 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778
779 if (msg_errcode(msg)) {
780 sock->state = SS_DISCONNECTING;
781 return -ECONNREFUSED;
782 }
783
Allan Stephens2da59912008-07-14 22:43:32 -0700784 tsock->peer_name.ref = msg_origport(msg);
785 tsock->peer_name.node = msg_orignode(msg);
786 tipc_connect2port(tsock->p->ref, &tsock->peer_name);
787 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788 sock->state = SS_CONNECTED;
789 return 0;
790}
791
792/**
793 * set_orig_addr - capture sender's address for received message
794 * @m: descriptor for message info
795 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900796 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100797 * Note: Address is not captured if not requested by receiver.
798 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800799static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100800{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900801 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900803 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804 addr->family = AF_TIPC;
805 addr->addrtype = TIPC_ADDR_ID;
806 addr->addr.id.ref = msg_origport(msg);
807 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000808 addr->addr.name.domain = 0; /* could leave uninitialized */
809 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810 m->msg_namelen = sizeof(struct sockaddr_tipc);
811 }
812}
813
814/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900815 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100816 * @m: descriptor for message info
817 * @msg: received message header
818 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900819 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100820 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900821 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822 * Returns 0 if successful, otherwise errno
823 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800824static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825 struct tipc_port *tport)
826{
827 u32 anc_data[3];
828 u32 err;
829 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700830 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100831 int res;
832
833 if (likely(m->msg_controllen == 0))
834 return 0;
835
836 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100837 err = msg ? msg_errcode(msg) : 0;
838 if (unlikely(err)) {
839 anc_data[0] = err;
840 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000841 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
842 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000844 if (anc_data[1]) {
845 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
846 msg_data(msg));
847 if (res)
848 return res;
849 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100850 }
851
852 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100853 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
854 switch (dest_type) {
855 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700856 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857 anc_data[0] = msg_nametype(msg);
858 anc_data[1] = msg_namelower(msg);
859 anc_data[2] = msg_namelower(msg);
860 break;
861 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700862 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100863 anc_data[0] = msg_nametype(msg);
864 anc_data[1] = msg_namelower(msg);
865 anc_data[2] = msg_nameupper(msg);
866 break;
867 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700868 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100869 anc_data[0] = tport->conn_type;
870 anc_data[1] = tport->conn_instance;
871 anc_data[2] = tport->conn_instance;
872 break;
873 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700874 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100875 }
Allan Stephens2db99832010-12-31 18:59:33 +0000876 if (has_name) {
877 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
878 if (res)
879 return res;
880 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881
882 return 0;
883}
884
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900885/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886 * recv_msg - receive packet-oriented message
887 * @iocb: (unused)
888 * @m: descriptor for message info
889 * @buf_len: total size of user buffer area
890 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900891 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
893 * If the complete message doesn't fit in user area, truncate it.
894 *
895 * Returns size of returned message data, errno otherwise
896 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897static int recv_msg(struct kiocb *iocb, struct socket *sock,
898 struct msghdr *m, size_t buf_len, int flags)
899{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700900 struct sock *sk = sock->sk;
901 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902 struct sk_buff *buf;
903 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500904 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905 unsigned int sz;
906 u32 err;
907 int res;
908
Allan Stephens0c3141e2008-04-15 00:22:02 -0700909 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910 if (unlikely(!buf_len))
911 return -EINVAL;
912
Allan Stephens0c3141e2008-04-15 00:22:02 -0700913 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914
Allan Stephens0c3141e2008-04-15 00:22:02 -0700915 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916 res = -ENOTCONN;
917 goto exit;
918 }
919
Allan Stephens71092ea2011-02-23 14:52:14 -0500920 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700921restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100922
Allan Stephens0c3141e2008-04-15 00:22:02 -0700923 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700924 while (skb_queue_empty(&sk->sk_receive_queue)) {
925 if (sock->state == SS_DISCONNECTING) {
926 res = -ENOTCONN;
927 goto exit;
928 }
Allan Stephens71092ea2011-02-23 14:52:14 -0500929 if (timeout <= 0L) {
930 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700931 goto exit;
932 }
933 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -0500934 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
935 tipc_rx_ready(sock),
936 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700937 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700938 }
939
940 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700941 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100942 msg = buf_msg(buf);
943 sz = msg_data_sz(msg);
944 err = msg_errcode(msg);
945
946 /* Complete connection setup for an implied connect */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947 if (unlikely(sock->state == SS_CONNECTING)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700948 res = auto_connect(sock, msg);
949 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100950 goto exit;
951 }
952
953 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100954 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700955 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100956 goto restart;
957 }
958
959 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100960 set_orig_addr(m, msg);
961
962 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700963 res = anc_data_recv(m, msg, tport);
964 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100965 goto exit;
966
967 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100968 if (!err) {
969 if (unlikely(buf_len < sz)) {
970 sz = buf_len;
971 m->msg_flags |= MSG_TRUNC;
972 }
Allan Stephens0232fd02011-02-21 09:45:40 -0500973 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
974 m->msg_iov, sz);
975 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100977 res = sz;
978 } else {
979 if ((sock->state == SS_READY) ||
980 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
981 res = 0;
982 else
983 res = -ECONNRESET;
984 }
985
986 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100987 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -0700988 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -0700989 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
990 tipc_acknowledge(tport->ref, tport->conn_unacked);
991 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900992 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700994 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995 return res;
996}
997
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900998/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100999 * recv_stream - receive stream-oriented data
1000 * @iocb: (unused)
1001 * @m: descriptor for message info
1002 * @buf_len: total size of user buffer area
1003 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001004 *
1005 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006 * will optionally wait for more; never truncates data.
1007 *
1008 * Returns size of returned message data, errno otherwise
1009 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010static int recv_stream(struct kiocb *iocb, struct socket *sock,
1011 struct msghdr *m, size_t buf_len, int flags)
1012{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001013 struct sock *sk = sock->sk;
1014 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001015 struct sk_buff *buf;
1016 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -05001017 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001019 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001021 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001022 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023
1024 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 if (unlikely(!buf_len))
1026 return -EINVAL;
1027
Allan Stephens0c3141e2008-04-15 00:22:02 -07001028 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029
Allan Stephens0c3141e2008-04-15 00:22:02 -07001030 if (unlikely((sock->state == SS_UNCONNECTED) ||
1031 (sock->state == SS_CONNECTING))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032 res = -ENOTCONN;
1033 goto exit;
1034 }
1035
Florian Westphal3720d402010-08-17 11:00:04 +00001036 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Allan Stephens71092ea2011-02-23 14:52:14 -05001037 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001038
Allan Stephens0c3141e2008-04-15 00:22:02 -07001039restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001040 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001041 while (skb_queue_empty(&sk->sk_receive_queue)) {
1042 if (sock->state == SS_DISCONNECTING) {
1043 res = -ENOTCONN;
1044 goto exit;
1045 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001046 if (timeout <= 0L) {
1047 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001048 goto exit;
1049 }
1050 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001051 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1052 tipc_rx_ready(sock),
1053 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001054 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001055 }
1056
1057 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001058 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059 msg = buf_msg(buf);
1060 sz = msg_data_sz(msg);
1061 err = msg_errcode(msg);
1062
1063 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001065 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 goto restart;
1067 }
1068
1069 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001070 if (sz_copied == 0) {
1071 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001072 res = anc_data_recv(m, msg, tport);
1073 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 goto exit;
1075 }
1076
1077 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001079 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001080
Allan Stephens0232fd02011-02-21 09:45:40 -05001081 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 needed = (buf_len - sz_copied);
1083 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001084
1085 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1086 m->msg_iov, sz_to_copy);
1087 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001088 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001089
Per Lidenb97bf3f2006-01-02 19:04:38 +01001090 sz_copied += sz_to_copy;
1091
1092 if (sz_to_copy < sz) {
1093 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001094 TIPC_SKB_CB(buf)->handle =
1095 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 goto exit;
1097 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001098 } else {
1099 if (sz_copied != 0)
1100 goto exit; /* can't add error msg to valid data */
1101
1102 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1103 res = 0;
1104 else
1105 res = -ECONNRESET;
1106 }
1107
1108 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001109 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001110 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1111 tipc_acknowledge(tport->ref, tport->conn_unacked);
1112 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001113 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114
1115 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001116 if ((sz_copied < buf_len) && /* didn't get all requested data */
1117 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001118 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001119 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1120 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121 goto restart;
1122
1123exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001124 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001125 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001126}
1127
1128/**
Ying Xuef288bef2012-08-21 11:16:57 +08001129 * tipc_write_space - wake up thread if port congestion is released
1130 * @sk: socket
1131 */
1132static void tipc_write_space(struct sock *sk)
1133{
1134 struct socket_wq *wq;
1135
1136 rcu_read_lock();
1137 wq = rcu_dereference(sk->sk_wq);
1138 if (wq_has_sleeper(wq))
1139 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1140 POLLWRNORM | POLLWRBAND);
1141 rcu_read_unlock();
1142}
1143
1144/**
1145 * tipc_data_ready - wake up threads to indicate messages have been received
1146 * @sk: socket
1147 * @len: the length of messages
1148 */
1149static void tipc_data_ready(struct sock *sk, int len)
1150{
1151 struct socket_wq *wq;
1152
1153 rcu_read_lock();
1154 wq = rcu_dereference(sk->sk_wq);
1155 if (wq_has_sleeper(wq))
1156 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1157 POLLRDNORM | POLLRDBAND);
1158 rcu_read_unlock();
1159}
1160
1161/**
Allan Stephens1819b832008-04-15 00:15:50 -07001162 * rx_queue_full - determine if receive queue can accept another message
1163 * @msg: message to be added to queue
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164 * @queue_size: current size of queue
1165 * @base: nominal maximum size of queue
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001166 *
Allan Stephens1819b832008-04-15 00:15:50 -07001167 * Returns 1 if queue is unable to accept message, 0 otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 */
Allan Stephens1819b832008-04-15 00:15:50 -07001169static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170{
1171 u32 threshold;
1172 u32 imp = msg_importance(msg);
1173
1174 if (imp == TIPC_LOW_IMPORTANCE)
1175 threshold = base;
1176 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1177 threshold = base * 2;
1178 else if (imp == TIPC_HIGH_IMPORTANCE)
1179 threshold = base * 100;
1180 else
1181 return 0;
1182
1183 if (msg_connected(msg))
1184 threshold *= 4;
1185
Eric Dumazeta02cec22010-09-22 20:43:57 +00001186 return queue_size >= threshold;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001187}
1188
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001189/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001190 * filter_rcv - validate incoming message
1191 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001192 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001193 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001194 * Enqueues message on receive queue if acceptable; optionally handles
1195 * disconnect indication for a connected socket.
1196 *
1197 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001198 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1200 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001201static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001203 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001204 struct tipc_msg *msg = buf_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001205 u32 recv_q_len;
1206
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001208 if (msg_type(msg) > TIPC_DIRECT_MSG)
1209 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001210
Per Lidenb97bf3f2006-01-02 19:04:38 +01001211 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001212 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001213 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214 } else {
Allan Stephensb29f1422010-12-31 18:59:25 +00001215 if (msg_mcast(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001217 if (sock->state == SS_CONNECTED) {
Allan Stephensf0712e862012-04-17 18:42:28 -04001218 if (!msg_connected(msg) ||
1219 !tipc_port_peer_msg(tipc_sk_port(sk), msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 return TIPC_ERR_NO_PORT;
Allan Stephensb29f1422010-12-31 18:59:25 +00001221 } else if (sock->state == SS_CONNECTING) {
1222 if (!msg_connected(msg) && (msg_errcode(msg) == 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223 return TIPC_ERR_NO_PORT;
Allan Stephensb29f1422010-12-31 18:59:25 +00001224 } else if (sock->state == SS_LISTENING) {
1225 if (msg_connected(msg) || msg_errcode(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226 return TIPC_ERR_NO_PORT;
Allan Stephensb29f1422010-12-31 18:59:25 +00001227 } else if (sock->state == SS_DISCONNECTING) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 return TIPC_ERR_NO_PORT;
Allan Stephensb29f1422010-12-31 18:59:25 +00001229 } else /* (sock->state == SS_UNCONNECTED) */ {
1230 if (msg_connected(msg) || msg_errcode(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232 }
1233 }
1234
1235 /* Reject message if there isn't room to queue it */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001236 recv_q_len = skb_queue_len(&sk->sk_receive_queue);
Allan Stephens1819b832008-04-15 00:15:50 -07001237 if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
1238 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001239 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001240 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001241
Allan Stephens0c3141e2008-04-15 00:22:02 -07001242 /* Enqueue message (finally!) */
Allan Stephens0232fd02011-02-21 09:45:40 -05001243 TIPC_SKB_CB(buf)->handle = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001244 __skb_queue_tail(&sk->sk_receive_queue, buf);
1245
Per Lidenb97bf3f2006-01-02 19:04:38 +01001246 /* Initiate connection termination for an incoming 'FIN' */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001247 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1248 sock->state = SS_DISCONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001249 tipc_disconnect_port(tipc_sk_port(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001250 }
1251
Ying Xuef288bef2012-08-21 11:16:57 +08001252 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001253 return TIPC_OK;
1254}
1255
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001256/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001257 * backlog_rcv - handle incoming message from backlog queue
1258 * @sk: socket
1259 * @buf: message
1260 *
1261 * Caller must hold socket lock, but not port lock.
1262 *
1263 * Returns 0
1264 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001265static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1266{
1267 u32 res;
1268
1269 res = filter_rcv(sk, buf);
1270 if (res)
1271 tipc_reject_msg(buf, res);
1272 return 0;
1273}
1274
1275/**
1276 * dispatch - handle incoming message
1277 * @tport: TIPC port that received message
1278 * @buf: message
1279 *
1280 * Called with port lock already taken.
1281 *
1282 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1283 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001284static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1285{
1286 struct sock *sk = (struct sock *)tport->usr_handle;
1287 u32 res;
1288
1289 /*
1290 * Process message if socket is unlocked; otherwise add to backlog queue
1291 *
1292 * This code is based on sk_receive_skb(), but must be distinct from it
1293 * since a TIPC-specific filter/reject mechanism is utilized
1294 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001295 bh_lock_sock(sk);
1296 if (!sock_owned_by_user(sk)) {
1297 res = filter_rcv(sk, buf);
1298 } else {
Eric Dumazetf545a382012-04-22 23:34:26 +00001299 if (sk_add_backlog(sk, buf, sk->sk_rcvbuf))
Zhu Yi53eecb12010-03-04 18:01:45 +00001300 res = TIPC_ERR_OVERLOAD;
1301 else
1302 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001303 }
1304 bh_unlock_sock(sk);
1305
1306 return res;
1307}
1308
1309/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310 * wakeupdispatch - wake up port after congestion
1311 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001312 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001313 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315static void wakeupdispatch(struct tipc_port *tport)
1316{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001317 struct sock *sk = (struct sock *)tport->usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001318
Ying Xuef288bef2012-08-21 11:16:57 +08001319 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320}
1321
1322/**
1323 * connect - establish a connection to another TIPC port
1324 * @sock: socket structure
1325 * @dest: socket address for destination port
1326 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001327 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328 *
1329 * Returns 0 on success, errno otherwise
1330 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001331static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001332 int flags)
1333{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001334 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001335 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1336 struct msghdr m = {NULL,};
1337 struct sk_buff *buf;
1338 struct tipc_msg *msg;
Allan Stephensa0f40f02011-05-26 13:44:34 -04001339 unsigned int timeout;
Allan Stephensb89741a2008-04-15 00:20:37 -07001340 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341
Allan Stephens0c3141e2008-04-15 00:22:02 -07001342 lock_sock(sk);
1343
Allan Stephensb89741a2008-04-15 00:20:37 -07001344 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001345 if (sock->state == SS_READY) {
1346 res = -EOPNOTSUPP;
1347 goto exit;
1348 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349
Allan Stephensb89741a2008-04-15 00:20:37 -07001350 /* For now, TIPC does not support the non-blocking form of connect() */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001351 if (flags & O_NONBLOCK) {
Allan Stephens35997e32010-08-17 11:00:05 +00001352 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001353 goto exit;
1354 }
Allan Stephens4934c692008-04-15 00:16:19 -07001355
Allan Stephensb89741a2008-04-15 00:20:37 -07001356 /* Issue Posix-compliant error code if socket is in the wrong state */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001357 if (sock->state == SS_LISTENING) {
1358 res = -EOPNOTSUPP;
1359 goto exit;
1360 }
1361 if (sock->state == SS_CONNECTING) {
1362 res = -EALREADY;
1363 goto exit;
1364 }
1365 if (sock->state != SS_UNCONNECTED) {
1366 res = -EISCONN;
1367 goto exit;
1368 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001369
Allan Stephensb89741a2008-04-15 00:20:37 -07001370 /*
1371 * Reject connection attempt using multicast address
1372 *
1373 * Note: send_msg() validates the rest of the address fields,
1374 * so there's no need to do it here
1375 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001376 if (dst->addrtype == TIPC_ADDR_MCAST) {
1377 res = -EINVAL;
1378 goto exit;
1379 }
1380
1381 /* Reject any messages already in receive queue (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001382 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383
Allan Stephensb89741a2008-04-15 00:20:37 -07001384 /* Send a 'SYN-' to destination */
Allan Stephensb89741a2008-04-15 00:20:37 -07001385 m.msg_name = dest;
1386 m.msg_namelen = destlen;
1387 res = send_msg(NULL, sock, &m, 0);
Allan Stephensa0168922010-12-31 18:59:35 +00001388 if (res < 0)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001389 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390
Allan Stephens0c3141e2008-04-15 00:22:02 -07001391 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
Allan Stephens564e83b2010-08-17 11:00:15 +00001392 timeout = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001393 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001394 res = wait_event_interruptible_timeout(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001395 (!skb_queue_empty(&sk->sk_receive_queue) ||
1396 (sock->state != SS_CONNECTING)),
Allan Stephensa0f40f02011-05-26 13:44:34 -04001397 timeout ? (long)msecs_to_jiffies(timeout)
1398 : MAX_SCHEDULE_TIMEOUT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001399 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001400
Allan Stephensb89741a2008-04-15 00:20:37 -07001401 if (res > 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001402 buf = skb_peek(&sk->sk_receive_queue);
1403 if (buf != NULL) {
1404 msg = buf_msg(buf);
1405 res = auto_connect(sock, msg);
1406 if (!res) {
1407 if (!msg_data_sz(msg))
1408 advance_rx_queue(sk);
1409 }
1410 } else {
Allan Stephensa0168922010-12-31 18:59:35 +00001411 if (sock->state == SS_CONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001412 res = -EISCONN;
Allan Stephensa0168922010-12-31 18:59:35 +00001413 else
Allan Stephens0c3141e2008-04-15 00:22:02 -07001414 res = -ECONNREFUSED;
Allan Stephensb89741a2008-04-15 00:20:37 -07001415 }
1416 } else {
1417 if (res == 0)
1418 res = -ETIMEDOUT;
1419 else
1420 ; /* leave "res" unchanged */
1421 sock->state = SS_DISCONNECTING;
1422 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001423
Allan Stephens0c3141e2008-04-15 00:22:02 -07001424exit:
1425 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001426 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001427}
1428
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001429/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001430 * listen - allow socket to listen for incoming connections
1431 * @sock: socket structure
1432 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001433 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001434 * Returns 0 on success, errno otherwise
1435 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001436static int listen(struct socket *sock, int len)
1437{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001438 struct sock *sk = sock->sk;
1439 int res;
1440
1441 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001442
Ying Xue245f3d32011-07-06 06:01:13 -04001443 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001444 res = -EINVAL;
1445 else {
1446 sock->state = SS_LISTENING;
1447 res = 0;
1448 }
1449
1450 release_sock(sk);
1451 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001452}
1453
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001454/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001455 * accept - wait for connection request
1456 * @sock: listening socket
1457 * @newsock: new socket that is to be connected
1458 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001459 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001460 * Returns 0 on success, errno otherwise
1461 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001462static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001464 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001465 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001466 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467
Allan Stephens0c3141e2008-04-15 00:22:02 -07001468 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001469
Allan Stephens0c3141e2008-04-15 00:22:02 -07001470 if (sock->state != SS_LISTENING) {
1471 res = -EINVAL;
1472 goto exit;
1473 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001474
Allan Stephens0c3141e2008-04-15 00:22:02 -07001475 while (skb_queue_empty(&sk->sk_receive_queue)) {
1476 if (flags & O_NONBLOCK) {
1477 res = -EWOULDBLOCK;
1478 goto exit;
1479 }
1480 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001481 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001482 (!skb_queue_empty(&sk->sk_receive_queue)));
1483 lock_sock(sk);
1484 if (res)
1485 goto exit;
1486 }
1487
1488 buf = skb_peek(&sk->sk_receive_queue);
1489
Eric Paris3f378b62009-11-05 22:18:14 -08001490 res = tipc_create(sock_net(sock->sk), new_sock, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001491 if (!res) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001492 struct sock *new_sk = new_sock->sk;
Allan Stephens2da59912008-07-14 22:43:32 -07001493 struct tipc_sock *new_tsock = tipc_sk(new_sk);
1494 struct tipc_port *new_tport = new_tsock->p;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001495 u32 new_ref = new_tport->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001496 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001497
1498 lock_sock(new_sk);
1499
1500 /*
1501 * Reject any stray messages received by new socket
1502 * before the socket lock was taken (very, very unlikely)
1503 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001504 reject_rx_queue(new_sk);
1505
1506 /* Connect new socket to it's peer */
Allan Stephens2da59912008-07-14 22:43:32 -07001507 new_tsock->peer_name.ref = msg_origport(msg);
1508 new_tsock->peer_name.node = msg_orignode(msg);
1509 tipc_connect2port(new_ref, &new_tsock->peer_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001510 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511
1512 tipc_set_portimportance(new_ref, msg_importance(msg));
1513 if (msg_named(msg)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001514 new_tport->conn_type = msg_nametype(msg);
1515 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001516 }
1517
Allan Stephens0c3141e2008-04-15 00:22:02 -07001518 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001519 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1520 * Respond to 'SYN+' by queuing it on new socket.
1521 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001522 if (!msg_data_sz(msg)) {
1523 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001524
Allan Stephens0c3141e2008-04-15 00:22:02 -07001525 advance_rx_queue(sk);
1526 send_packet(NULL, new_sock, &m, 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001527 } else {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001528 __skb_dequeue(&sk->sk_receive_queue);
1529 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001530 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001531 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001532 }
1533exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001534 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001535 return res;
1536}
1537
1538/**
1539 * shutdown - shutdown socket connection
1540 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001541 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001542 *
1543 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001544 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001545 * Returns 0 on success, errno otherwise
1546 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001547static int shutdown(struct socket *sock, int how)
1548{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001549 struct sock *sk = sock->sk;
1550 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001551 struct sk_buff *buf;
1552 int res;
1553
Allan Stephense247a8f2008-03-06 15:05:38 -08001554 if (how != SHUT_RDWR)
1555 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001556
Allan Stephens0c3141e2008-04-15 00:22:02 -07001557 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001558
1559 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001560 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001561 case SS_CONNECTED:
1562
Per Lidenb97bf3f2006-01-02 19:04:38 +01001563restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001564 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001565 buf = __skb_dequeue(&sk->sk_receive_queue);
1566 if (buf) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001567 if (TIPC_SKB_CB(buf)->handle != 0) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001568 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001569 goto restart;
1570 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001571 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001572 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001573 } else {
1574 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001575 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001576
1577 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001578
1579 /* fall through */
1580
1581 case SS_DISCONNECTING:
1582
Ying Xue75031152012-10-29 09:38:15 -04001583 /* Discard any unreceived messages */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001584 discard_rx_queue(sk);
Ying Xue75031152012-10-29 09:38:15 -04001585
1586 /* Wake up anyone sleeping in poll */
1587 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001588 res = 0;
1589 break;
1590
1591 default:
1592 res = -ENOTCONN;
1593 }
1594
Allan Stephens0c3141e2008-04-15 00:22:02 -07001595 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001596 return res;
1597}
1598
1599/**
1600 * setsockopt - set socket option
1601 * @sock: socket structure
1602 * @lvl: option level
1603 * @opt: option identifier
1604 * @ov: pointer to new option value
1605 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001606 *
1607 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001608 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001609 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001610 * Returns 0 on success, errno otherwise
1611 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001612static int setsockopt(struct socket *sock,
David S. Millerb7058842009-09-30 16:12:20 -07001613 int lvl, int opt, char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001614{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001615 struct sock *sk = sock->sk;
1616 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001617 u32 value;
1618 int res;
1619
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001620 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1621 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001622 if (lvl != SOL_TIPC)
1623 return -ENOPROTOOPT;
1624 if (ol < sizeof(value))
1625 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001626 res = get_user(value, (u32 __user *)ov);
1627 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001628 return res;
1629
Allan Stephens0c3141e2008-04-15 00:22:02 -07001630 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001631
Per Lidenb97bf3f2006-01-02 19:04:38 +01001632 switch (opt) {
1633 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001634 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001635 break;
1636 case TIPC_SRC_DROPPABLE:
1637 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001638 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001639 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001640 res = -ENOPROTOOPT;
1641 break;
1642 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001643 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001644 break;
1645 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001646 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001647 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001648 break;
1649 default:
1650 res = -EINVAL;
1651 }
1652
Allan Stephens0c3141e2008-04-15 00:22:02 -07001653 release_sock(sk);
1654
Per Lidenb97bf3f2006-01-02 19:04:38 +01001655 return res;
1656}
1657
1658/**
1659 * getsockopt - get socket option
1660 * @sock: socket structure
1661 * @lvl: option level
1662 * @opt: option identifier
1663 * @ov: receptacle for option value
1664 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001665 *
1666 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001667 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001668 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669 * Returns 0 on success, errno otherwise
1670 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001671static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001672 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001673{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001674 struct sock *sk = sock->sk;
1675 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001676 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001677 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001678 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001680 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1681 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001682 if (lvl != SOL_TIPC)
1683 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001684 res = get_user(len, ol);
1685 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001686 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001687
Allan Stephens0c3141e2008-04-15 00:22:02 -07001688 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001689
1690 switch (opt) {
1691 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001692 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001693 break;
1694 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001695 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696 break;
1697 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001698 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699 break;
1700 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001701 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001702 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001703 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001704 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001705 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001706 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001707 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001708 value = skb_queue_len(&sk->sk_receive_queue);
1709 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001710 default:
1711 res = -EINVAL;
1712 }
1713
Allan Stephens0c3141e2008-04-15 00:22:02 -07001714 release_sock(sk);
1715
Paul Gortmaker25860c32010-12-31 18:59:31 +00001716 if (res)
1717 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001718
Paul Gortmaker25860c32010-12-31 18:59:31 +00001719 if (len < sizeof(value))
1720 return -EINVAL;
1721
1722 if (copy_to_user(ov, &value, sizeof(value)))
1723 return -EFAULT;
1724
1725 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726}
1727
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001728/* Protocol switches for the various types of TIPC sockets */
1729
Florian Westphalbca65ea2008-02-07 18:18:01 -08001730static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001731 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001732 .family = AF_TIPC,
1733 .release = release,
1734 .bind = bind,
1735 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001736 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001737 .accept = sock_no_accept,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001738 .getname = get_name,
1739 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001740 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001741 .listen = sock_no_listen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001742 .shutdown = shutdown,
1743 .setsockopt = setsockopt,
1744 .getsockopt = getsockopt,
1745 .sendmsg = send_msg,
1746 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001747 .mmap = sock_no_mmap,
1748 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749};
1750
Florian Westphalbca65ea2008-02-07 18:18:01 -08001751static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001752 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001753 .family = AF_TIPC,
1754 .release = release,
1755 .bind = bind,
1756 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001757 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001758 .accept = accept,
1759 .getname = get_name,
1760 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001761 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001762 .listen = listen,
1763 .shutdown = shutdown,
1764 .setsockopt = setsockopt,
1765 .getsockopt = getsockopt,
1766 .sendmsg = send_packet,
1767 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001768 .mmap = sock_no_mmap,
1769 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001770};
1771
Florian Westphalbca65ea2008-02-07 18:18:01 -08001772static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001773 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774 .family = AF_TIPC,
1775 .release = release,
1776 .bind = bind,
1777 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001778 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001779 .accept = accept,
1780 .getname = get_name,
1781 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001782 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001783 .listen = listen,
1784 .shutdown = shutdown,
1785 .setsockopt = setsockopt,
1786 .getsockopt = getsockopt,
1787 .sendmsg = send_stream,
1788 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001789 .mmap = sock_no_mmap,
1790 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001791};
1792
Florian Westphalbca65ea2008-02-07 18:18:01 -08001793static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001794 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001795 .family = AF_TIPC,
1796 .create = tipc_create
1797};
1798
1799static struct proto tipc_proto = {
1800 .name = "TIPC",
1801 .owner = THIS_MODULE,
1802 .obj_size = sizeof(struct tipc_sock)
1803};
1804
1805/**
Per Liden4323add2006-01-18 00:38:21 +01001806 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001807 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001808 * Returns 0 on success, errno otherwise
1809 */
Per Liden4323add2006-01-18 00:38:21 +01001810int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001811{
1812 int res;
1813
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001814 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001815 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001816 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001817 goto out;
1818 }
1819
1820 res = sock_register(&tipc_family_ops);
1821 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001822 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001823 proto_unregister(&tipc_proto);
1824 goto out;
1825 }
1826
1827 sockets_enabled = 1;
1828 out:
1829 return res;
1830}
1831
1832/**
Per Liden4323add2006-01-18 00:38:21 +01001833 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001834 */
Per Liden4323add2006-01-18 00:38:21 +01001835void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001836{
1837 if (!sockets_enabled)
1838 return;
1839
1840 sockets_enabled = 0;
1841 sock_unregister(tipc_family_ops.family);
1842 proto_unregister(&tipc_proto);
1843}