blob: cbe2f6ecf07ac960c120e49aa5a45844792af559 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Maloye643df12012-11-27 06:15:29 -05004 * Copyright (c) 2001-2007, 2012 Ericsson AB
Ying Xue9da3d472012-11-27 06:15:27 -05005 * Copyright (c) 2004-2008, 2010-2012, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000038#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039
Erik Hugne2cf8aa12012-06-29 00:16:37 -040040#include <linux/export.h>
41#include <net/sock.h>
42
Per Lidenb97bf3f2006-01-02 19:04:38 +010043#define SS_LISTENING -1 /* socket is listening */
44#define SS_READY -2 /* socket is connectionless */
45
Ying Xueaba79f32013-01-20 23:30:09 +010046#define CONN_OVERLOAD_LIMIT ((TIPC_FLOW_CONTROL_WIN * 2 + 1) * \
47 SKB_TRUESIZE(TIPC_MAX_USER_MSG_SIZE))
Allan Stephens3654ea02008-04-13 21:35:11 -070048#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010049
50struct tipc_sock {
51 struct sock sk;
52 struct tipc_port *p;
Allan Stephens2da59912008-07-14 22:43:32 -070053 struct tipc_portid peer_name;
Allan Stephensa0f40f02011-05-26 13:44:34 -040054 unsigned int conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +010055};
56
Allan Stephens0c3141e2008-04-15 00:22:02 -070057#define tipc_sk(sk) ((struct tipc_sock *)(sk))
Joe Perchese3192692012-06-03 17:41:40 +000058#define tipc_sk_port(sk) (tipc_sk(sk)->p)
Per Lidenb97bf3f2006-01-02 19:04:38 +010059
Allan Stephens71092ea2011-02-23 14:52:14 -050060#define tipc_rx_ready(sock) (!skb_queue_empty(&sock->sk->sk_receive_queue) || \
61 (sock->state == SS_DISCONNECTING))
62
Allan Stephens0c3141e2008-04-15 00:22:02 -070063static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +010064static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
65static void wakeupdispatch(struct tipc_port *tport);
Ying Xuef288bef2012-08-21 11:16:57 +080066static void tipc_data_ready(struct sock *sk, int len);
67static void tipc_write_space(struct sock *sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +010068
Florian Westphalbca65ea2008-02-07 18:18:01 -080069static const struct proto_ops packet_ops;
70static const struct proto_ops stream_ops;
71static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010072
73static struct proto tipc_proto;
74
Allan Stephense3ec9c72010-12-31 18:59:34 +000075static int sockets_enabled;
Per Lidenb97bf3f2006-01-02 19:04:38 +010076
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090077/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070078 * Revised TIPC socket locking policy:
79 *
80 * Most socket operations take the standard socket lock when they start
81 * and hold it until they finish (or until they need to sleep). Acquiring
82 * this lock grants the owner exclusive access to the fields of the socket
83 * data structures, with the exception of the backlog queue. A few socket
84 * operations can be done without taking the socket lock because they only
85 * read socket information that never changes during the life of the socket.
86 *
87 * Socket operations may acquire the lock for the associated TIPC port if they
88 * need to perform an operation on the port. If any routine needs to acquire
89 * both the socket lock and the port lock it must take the socket lock first
90 * to avoid the risk of deadlock.
91 *
92 * The dispatcher handling incoming messages cannot grab the socket lock in
93 * the standard fashion, since invoked it runs at the BH level and cannot block.
94 * Instead, it checks to see if the socket lock is currently owned by someone,
95 * and either handles the message itself or adds it to the socket's backlog
96 * queue; in the latter case the queued message is processed once the process
97 * owning the socket lock releases it.
98 *
99 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
100 * the problem of a blocked socket operation preventing any other operations
101 * from occurring. However, applications must be careful if they have
102 * multiple threads trying to send (or receive) on the same socket, as these
103 * operations might interfere with each other. For example, doing a connect
104 * and a receive at the same time might allow the receive to consume the
105 * ACK message meant for the connect. While additional work could be done
106 * to try and overcome this, it doesn't seem to be worthwhile at the present.
107 *
108 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
109 * that another operation that must be performed in a non-blocking manner is
110 * not delayed for very long because the lock has already been taken.
111 *
112 * NOTE: This code assumes that certain fields of a port/socket pair are
113 * constant over its lifetime; such fields can be examined without taking
114 * the socket lock and/or port lock, and do not need to be re-read even
115 * after resuming processing after waiting. These fields include:
116 * - socket type
117 * - pointer to socket sk structure (aka tipc_sock structure)
118 * - pointer to port structure
119 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121
122/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700123 * advance_rx_queue - discard first buffer in socket receive queue
124 *
125 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100126 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700127static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400129 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130}
131
132/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700133 * reject_rx_queue - reject all buffers in socket receive queue
134 *
135 * Caller must hold socket lock
136 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700137static void reject_rx_queue(struct sock *sk)
138{
139 struct sk_buff *buf;
140
Ying Xue9da3d472012-11-27 06:15:27 -0500141 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700142 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700143}
144
145/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146 * tipc_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700147 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148 * @sock: pre-allocated socket structure
149 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800150 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900151 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700152 * This routine creates additional data structures used by the TIPC socket,
153 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154 *
155 * Returns 0 on success, errno otherwise
156 */
Eric Paris3f378b62009-11-05 22:18:14 -0800157static int tipc_create(struct net *net, struct socket *sock, int protocol,
158 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700160 const struct proto_ops *ops;
161 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700163 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700164
165 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 if (unlikely(protocol != 0))
167 return -EPROTONOSUPPORT;
168
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 switch (sock->type) {
170 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700171 ops = &stream_ops;
172 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 break;
174 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700175 ops = &packet_ops;
176 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 break;
178 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700180 ops = &msg_ops;
181 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182 break;
Allan Stephens49978652006-06-25 23:47:18 -0700183 default:
Allan Stephens49978652006-06-25 23:47:18 -0700184 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185 }
186
Allan Stephens0c3141e2008-04-15 00:22:02 -0700187 /* Allocate socket's protocol area */
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700188 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700189 if (sk == NULL)
190 return -ENOMEM;
191
192 /* Allocate TIPC port for socket to use */
Allan Stephens0ea52242008-07-14 22:42:19 -0700193 tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
194 TIPC_LOW_IMPORTANCE);
195 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700196 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197 return -ENOMEM;
198 }
199
Allan Stephens0c3141e2008-04-15 00:22:02 -0700200 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700201 sock->ops = ops;
202 sock->state = state;
203
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700205 sk->sk_backlog_rcv = backlog_rcv;
Ying Xuef288bef2012-08-21 11:16:57 +0800206 sk->sk_data_ready = tipc_data_ready;
207 sk->sk_write_space = tipc_write_space;
Allan Stephens0ea52242008-07-14 22:42:19 -0700208 tipc_sk(sk)->p = tp_ptr;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400209 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700211 spin_unlock_bh(tp_ptr->lock);
212
Allan Stephens0c3141e2008-04-15 00:22:02 -0700213 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700214 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700215 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700216 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700217 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219 return 0;
220}
221
222/**
223 * release - destroy a TIPC socket
224 * @sock: socket to destroy
225 *
226 * This routine cleans up any messages that are still queued on the socket.
227 * For DGRAM and RDM socket types, all queued messages are rejected.
228 * For SEQPACKET and STREAM socket types, the first message is rejected
229 * and any others are discarded. (If the first message on a STREAM socket
230 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900231 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 * NOTE: Rejected messages are not necessarily returned to the sender! They
233 * are returned or discarded according to the "destination droppable" setting
234 * specified for the message by the sender.
235 *
236 * Returns 0 on success, errno otherwise
237 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238static int release(struct socket *sock)
239{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700241 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700243 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244
Allan Stephens0c3141e2008-04-15 00:22:02 -0700245 /*
246 * Exit if socket isn't fully initialized (occurs when a failed accept()
247 * releases a pre-allocated child socket that was never used)
248 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700249 if (sk == NULL)
250 return 0;
251
252 tport = tipc_sk_port(sk);
253 lock_sock(sk);
254
255 /*
256 * Reject all unreceived messages, except on an active connection
257 * (which disconnects locally & sends a 'FIN+' to peer)
258 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700260 buf = __skb_dequeue(&sk->sk_receive_queue);
261 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262 break;
Allan Stephens0232fd02011-02-21 09:45:40 -0500263 if (TIPC_SKB_CB(buf)->handle != 0)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400264 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700265 else {
266 if ((sock->state == SS_CONNECTING) ||
267 (sock->state == SS_CONNECTED)) {
268 sock->state = SS_DISCONNECTING;
269 tipc_disconnect(tport->ref);
270 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700272 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273 }
274
Allan Stephens0c3141e2008-04-15 00:22:02 -0700275 /*
276 * Delete TIPC port; this ensures no more messages are queued
277 * (also disconnects an active connection & sends a 'FIN-' to peer)
278 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700279 res = tipc_deleteport(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280
Allan Stephens0c3141e2008-04-15 00:22:02 -0700281 /* Discard any remaining (connection-based) messages in receive queue */
Ying Xue57467e52013-01-20 23:30:08 +0100282 __skb_queue_purge(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283
Allan Stephens0c3141e2008-04-15 00:22:02 -0700284 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700285 sock->state = SS_DISCONNECTING;
286 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287
288 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700289 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290
Per Lidenb97bf3f2006-01-02 19:04:38 +0100291 return res;
292}
293
294/**
295 * bind - associate or disassocate TIPC name(s) with a socket
296 * @sock: socket structure
297 * @uaddr: socket address describing name(s) and desired operation
298 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900299 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 * Name and name sequence binding is indicated using a positive scope value;
301 * a negative scope value unbinds the specified name. Specifying no name
302 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900303 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700305 *
306 * NOTE: This routine doesn't need to take the socket lock since it doesn't
307 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100309static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
310{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700312 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313
Allan Stephens0c3141e2008-04-15 00:22:02 -0700314 if (unlikely(!uaddr_len))
315 return tipc_withdraw(portref, 0, NULL);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900316
Allan Stephens0c3141e2008-04-15 00:22:02 -0700317 if (uaddr_len < sizeof(struct sockaddr_tipc))
318 return -EINVAL;
319 if (addr->family != AF_TIPC)
320 return -EAFNOSUPPORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 if (addr->addrtype == TIPC_ADDR_NAME)
323 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700324 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
325 return -EAFNOSUPPORT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900326
Allan Stephensc422f1b2011-11-02 15:49:40 -0400327 if (addr->addr.nameseq.type < TIPC_RESERVED_TYPES)
328 return -EACCES;
329
Allan Stephens0c3141e2008-04-15 00:22:02 -0700330 return (addr->scope > 0) ?
331 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
332 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333}
334
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900335/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336 * get_name - get port ID of socket or peer socket
337 * @sock: socket structure
338 * @uaddr: area for returned socket address
339 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700340 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900341 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700343 *
Allan Stephens2da59912008-07-14 22:43:32 -0700344 * NOTE: This routine doesn't need to take the socket lock since it only
345 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000346 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900348static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 int *uaddr_len, int peer)
350{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700352 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000354 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700355 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700356 if ((sock->state != SS_CONNECTED) &&
357 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
358 return -ENOTCONN;
359 addr->addr.id.ref = tsock->peer_name.ref;
360 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700361 } else {
Allan Stephensb924dcf2010-11-30 12:01:03 +0000362 addr->addr.id.ref = tsock->p->ref;
363 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700364 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365
366 *uaddr_len = sizeof(*addr);
367 addr->addrtype = TIPC_ADDR_ID;
368 addr->family = AF_TIPC;
369 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100370 addr->addr.name.domain = 0;
371
Allan Stephens0c3141e2008-04-15 00:22:02 -0700372 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373}
374
375/**
376 * poll - read and possibly block on pollmask
377 * @file: file structure associated with the socket
378 * @sock: socket for which to calculate the poll bits
379 * @wait: ???
380 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700381 * Returns pollmask value
382 *
383 * COMMENTARY:
384 * It appears that the usual socket locking mechanisms are not useful here
385 * since the pollmask info is potentially out-of-date the moment this routine
386 * exits. TCP and other protocols seem to rely on higher level poll routines
387 * to handle any preventable race conditions, so TIPC will do the same ...
388 *
389 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700390 *
Allan Stephensf662c072010-08-17 11:00:06 +0000391 * socket state flags set
392 * ------------ ---------
393 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200394 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000395 *
396 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
397 * no write flags
398 *
399 * connected POLLIN/POLLRDNORM if data in rx queue
400 * POLLOUT if port is not congested
401 *
402 * disconnecting POLLIN/POLLRDNORM/POLLHUP
403 * no write flags
404 *
405 * listening POLLIN if SYN in rx queue
406 * no write flags
407 *
408 * ready POLLIN/POLLRDNORM if data in rx queue
409 * [connectionless] POLLOUT (since port cannot be congested)
410 *
411 * IMPORTANT: The fact that a read or write operation is indicated does NOT
412 * imply that the operation will succeed, merely that it should be performed
413 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900415static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416 poll_table *wait)
417{
Allan Stephens9b674e82008-03-26 16:48:21 -0700418 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000419 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700420
Ying Xuef288bef2012-08-21 11:16:57 +0800421 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700422
Allan Stephensf662c072010-08-17 11:00:06 +0000423 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200424 case SS_UNCONNECTED:
425 if (!tipc_sk_port(sk)->congested)
426 mask |= POLLOUT;
427 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000428 case SS_READY:
429 case SS_CONNECTED:
430 if (!tipc_sk_port(sk)->congested)
431 mask |= POLLOUT;
432 /* fall thru' */
433 case SS_CONNECTING:
434 case SS_LISTENING:
435 if (!skb_queue_empty(&sk->sk_receive_queue))
436 mask |= (POLLIN | POLLRDNORM);
437 break;
438 case SS_DISCONNECTING:
439 mask = (POLLIN | POLLRDNORM | POLLHUP);
440 break;
441 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700442
443 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444}
445
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900446/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100447 * dest_name_check - verify user is permitted to send to specified port name
448 * @dest: destination address
449 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900450 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451 * Prevents restricted configuration commands from being issued by
452 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900453 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454 * Returns 0 if permission is granted, otherwise errno
455 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800456static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457{
458 struct tipc_cfg_msg_hdr hdr;
459
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900460 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
461 return 0;
462 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
463 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900464 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
465 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466
Allan Stephens3f8dd942011-01-18 13:09:29 -0500467 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
468 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900469 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700471 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900473
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474 return 0;
475}
476
477/**
478 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700479 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480 * @sock: socket structure
481 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700482 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900483 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900485 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
487 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900488 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489 * Returns the number of bytes sent on success, or errno otherwise
490 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491static int send_msg(struct kiocb *iocb, struct socket *sock,
492 struct msghdr *m, size_t total_len)
493{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700494 struct sock *sk = sock->sk;
495 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900496 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 int needs_conn;
Ying Xue1d835872011-07-06 05:53:15 -0400498 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 int res = -EINVAL;
500
501 if (unlikely(!dest))
502 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700503 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
504 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 return -EINVAL;
Allan Stephensc29c3f72010-04-20 17:58:24 -0400506 if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
Eric Dumazet95c96172012-04-15 05:58:06 +0000507 (m->msg_iovlen > (unsigned int)INT_MAX))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400508 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509
Allan Stephens0c3141e2008-04-15 00:22:02 -0700510 if (iocb)
511 lock_sock(sk);
512
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 needs_conn = (sock->state != SS_READY);
514 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700515 if (sock->state == SS_LISTENING) {
516 res = -EPIPE;
517 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700518 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700519 if (sock->state != SS_UNCONNECTED) {
520 res = -EISCONN;
521 goto exit;
522 }
523 if ((tport->published) ||
524 ((sock->type == SOCK_STREAM) && (total_len != 0))) {
525 res = -EOPNOTSUPP;
526 goto exit;
527 }
528 if (dest->addrtype == TIPC_ADDR_NAME) {
529 tport->conn_type = dest->addr.name.name.type;
530 tport->conn_instance = dest->addr.name.name.instance;
531 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100532
533 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700534 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 }
536
Ying Xue1d835872011-07-06 05:53:15 -0400537 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
538
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900539 do {
540 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000541 res = dest_name_check(dest, m);
542 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700543 break;
544 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900545 &dest->addr.name.name,
546 dest->addr.name.domain,
547 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500548 m->msg_iov,
549 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000550 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700551 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900552 &dest->addr.id,
553 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500554 m->msg_iov,
555 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000556 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 if (needs_conn) {
558 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700559 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560 }
Allan Stephens2db99832010-12-31 18:59:33 +0000561 res = dest_name_check(dest, m);
562 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700563 break;
564 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900565 &dest->addr.nameseq,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900566 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500567 m->msg_iov,
568 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900569 }
570 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000571 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700572 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700573 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900574 }
Ying Xue1d835872011-07-06 05:53:15 -0400575 if (timeout_val <= 0L) {
576 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700577 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700579 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400580 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
581 !tport->congested, timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700582 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900583 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700584
585exit:
586 if (iocb)
587 release_sock(sk);
588 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589}
590
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900591/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700593 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 * @sock: socket structure
595 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700596 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900597 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900599 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 * Returns the number of bytes sent on success, or errno otherwise
601 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602static int send_packet(struct kiocb *iocb, struct socket *sock,
603 struct msghdr *m, size_t total_len)
604{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700605 struct sock *sk = sock->sk;
606 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900607 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Ying Xue1d835872011-07-06 05:53:15 -0400608 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 int res;
610
611 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 if (unlikely(dest))
613 return send_msg(iocb, sock, m, total_len);
614
Allan Stephensc29c3f72010-04-20 17:58:24 -0400615 if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
Eric Dumazet95c96172012-04-15 05:58:06 +0000616 (m->msg_iovlen > (unsigned int)INT_MAX))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400617 return -EMSGSIZE;
618
Allan Stephens0c3141e2008-04-15 00:22:02 -0700619 if (iocb)
620 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621
Ying Xue1d835872011-07-06 05:53:15 -0400622 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
623
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900624 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700625 if (unlikely(sock->state != SS_CONNECTED)) {
626 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900627 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700628 else
629 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700630 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700631 }
632
Allan Stephens26896902011-04-21 10:42:07 -0500633 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov,
634 total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000635 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700636 break;
Ying Xue1d835872011-07-06 05:53:15 -0400637 if (timeout_val <= 0L) {
638 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700639 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700641 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400642 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
643 (!tport->congested || !tport->connected), timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700644 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900645 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700646
647 if (iocb)
648 release_sock(sk);
649 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650}
651
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900652/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 * send_stream - send stream-oriented data
654 * @iocb: (unused)
655 * @sock: socket structure
656 * @m: data to send
657 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900658 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900660 *
661 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700662 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664static int send_stream(struct kiocb *iocb, struct socket *sock,
665 struct msghdr *m, size_t total_len)
666{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700667 struct sock *sk = sock->sk;
668 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669 struct msghdr my_msg;
670 struct iovec my_iov;
671 struct iovec *curr_iov;
672 int curr_iovlen;
673 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700674 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675 int curr_left;
676 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700677 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900679
Allan Stephens0c3141e2008-04-15 00:22:02 -0700680 lock_sock(sk);
681
Allan Stephens05646c92007-06-10 17:25:24 -0700682 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900683 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700684 if (sock->state == SS_UNCONNECTED) {
685 res = send_packet(NULL, sock, m, total_len);
686 goto exit;
687 } else if (sock->state == SS_DISCONNECTING) {
688 res = -EPIPE;
689 goto exit;
690 } else {
691 res = -ENOTCONN;
692 goto exit;
693 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900694 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100695
Allan Stephens0c3141e2008-04-15 00:22:02 -0700696 if (unlikely(m->msg_name)) {
697 res = -EISCONN;
698 goto exit;
699 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700700
Eric Dumazet95c96172012-04-15 05:58:06 +0000701 if ((total_len > (unsigned int)INT_MAX) ||
702 (m->msg_iovlen > (unsigned int)INT_MAX)) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400703 res = -EMSGSIZE;
704 goto exit;
705 }
706
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900707 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708 * Send each iovec entry using one or more messages
709 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900710 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711 * (i.e. one large iovec entry), but could be improved to pass sets
712 * of small iovec entries into send_packet().
713 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700714 curr_iov = m->msg_iov;
715 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716 my_msg.msg_iov = &my_iov;
717 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700718 my_msg.msg_flags = m->msg_flags;
719 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700720 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100721
Allan Stephens05646c92007-06-10 17:25:24 -0700722 hdr_size = msg_hdr_sz(&tport->phdr);
723
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724 while (curr_iovlen--) {
725 curr_start = curr_iov->iov_base;
726 curr_left = curr_iov->iov_len;
727
728 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700729 bytes_to_send = tport->max_pkt - hdr_size;
730 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
731 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
732 if (curr_left < bytes_to_send)
733 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734 my_iov.iov_base = curr_start;
735 my_iov.iov_len = bytes_to_send;
Allan Stephens26896902011-04-21 10:42:07 -0500736 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000737 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700738 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700739 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700740 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700741 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742 curr_left -= bytes_to_send;
743 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700744 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 }
746
747 curr_iov++;
748 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700749 res = bytes_sent;
750exit:
751 release_sock(sk);
752 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100753}
754
755/**
756 * auto_connect - complete connection setup to a remote port
757 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100758 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900759 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 * Returns 0 on success, errno otherwise
761 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700762static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763{
Allan Stephens2da59912008-07-14 22:43:32 -0700764 struct tipc_sock *tsock = tipc_sk(sock->sk);
Ying Xue584d24b2012-11-29 18:51:19 -0500765 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766
Allan Stephens2da59912008-07-14 22:43:32 -0700767 tsock->peer_name.ref = msg_origport(msg);
768 tsock->peer_name.node = msg_orignode(msg);
Ying Xue584d24b2012-11-29 18:51:19 -0500769 p_ptr = tipc_port_deref(tsock->p->ref);
770 if (!p_ptr)
771 return -EINVAL;
772
773 __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
774
775 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
776 return -EINVAL;
777 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 sock->state = SS_CONNECTED;
779 return 0;
780}
781
782/**
783 * set_orig_addr - capture sender's address for received message
784 * @m: descriptor for message info
785 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900786 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100787 * Note: Address is not captured if not requested by receiver.
788 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800789static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100790{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900791 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900793 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794 addr->family = AF_TIPC;
795 addr->addrtype = TIPC_ADDR_ID;
796 addr->addr.id.ref = msg_origport(msg);
797 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000798 addr->addr.name.domain = 0; /* could leave uninitialized */
799 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100800 m->msg_namelen = sizeof(struct sockaddr_tipc);
801 }
802}
803
804/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900805 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100806 * @m: descriptor for message info
807 * @msg: received message header
808 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900809 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900811 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812 * Returns 0 if successful, otherwise errno
813 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800814static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100815 struct tipc_port *tport)
816{
817 u32 anc_data[3];
818 u32 err;
819 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700820 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100821 int res;
822
823 if (likely(m->msg_controllen == 0))
824 return 0;
825
826 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100827 err = msg ? msg_errcode(msg) : 0;
828 if (unlikely(err)) {
829 anc_data[0] = err;
830 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000831 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
832 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100833 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000834 if (anc_data[1]) {
835 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
836 msg_data(msg));
837 if (res)
838 return res;
839 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840 }
841
842 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
844 switch (dest_type) {
845 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700846 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 anc_data[0] = msg_nametype(msg);
848 anc_data[1] = msg_namelower(msg);
849 anc_data[2] = msg_namelower(msg);
850 break;
851 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700852 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100853 anc_data[0] = msg_nametype(msg);
854 anc_data[1] = msg_namelower(msg);
855 anc_data[2] = msg_nameupper(msg);
856 break;
857 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700858 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859 anc_data[0] = tport->conn_type;
860 anc_data[1] = tport->conn_instance;
861 anc_data[2] = tport->conn_instance;
862 break;
863 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700864 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865 }
Allan Stephens2db99832010-12-31 18:59:33 +0000866 if (has_name) {
867 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
868 if (res)
869 return res;
870 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871
872 return 0;
873}
874
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900875/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100876 * recv_msg - receive packet-oriented message
877 * @iocb: (unused)
878 * @m: descriptor for message info
879 * @buf_len: total size of user buffer area
880 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900881 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100882 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
883 * If the complete message doesn't fit in user area, truncate it.
884 *
885 * Returns size of returned message data, errno otherwise
886 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100887static int recv_msg(struct kiocb *iocb, struct socket *sock,
888 struct msghdr *m, size_t buf_len, int flags)
889{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700890 struct sock *sk = sock->sk;
891 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892 struct sk_buff *buf;
893 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500894 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100895 unsigned int sz;
896 u32 err;
897 int res;
898
Allan Stephens0c3141e2008-04-15 00:22:02 -0700899 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900 if (unlikely(!buf_len))
901 return -EINVAL;
902
Allan Stephens0c3141e2008-04-15 00:22:02 -0700903 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904
Allan Stephens0c3141e2008-04-15 00:22:02 -0700905 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906 res = -ENOTCONN;
907 goto exit;
908 }
909
Allan Stephens71092ea2011-02-23 14:52:14 -0500910 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700911restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912
Allan Stephens0c3141e2008-04-15 00:22:02 -0700913 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700914 while (skb_queue_empty(&sk->sk_receive_queue)) {
915 if (sock->state == SS_DISCONNECTING) {
916 res = -ENOTCONN;
917 goto exit;
918 }
Allan Stephens71092ea2011-02-23 14:52:14 -0500919 if (timeout <= 0L) {
920 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700921 goto exit;
922 }
923 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -0500924 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
925 tipc_rx_ready(sock),
926 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700927 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700928 }
929
930 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700931 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100932 msg = buf_msg(buf);
933 sz = msg_data_sz(msg);
934 err = msg_errcode(msg);
935
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100937 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700938 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100939 goto restart;
940 }
941
942 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100943 set_orig_addr(m, msg);
944
945 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700946 res = anc_data_recv(m, msg, tport);
947 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100948 goto exit;
949
950 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951 if (!err) {
952 if (unlikely(buf_len < sz)) {
953 sz = buf_len;
954 m->msg_flags |= MSG_TRUNC;
955 }
Allan Stephens0232fd02011-02-21 09:45:40 -0500956 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
957 m->msg_iov, sz);
958 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100960 res = sz;
961 } else {
962 if ((sock->state == SS_READY) ||
963 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
964 res = 0;
965 else
966 res = -ECONNRESET;
967 }
968
969 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -0700971 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -0700972 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
973 tipc_acknowledge(tport->ref, tport->conn_unacked);
974 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900975 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700977 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100978 return res;
979}
980
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900981/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100982 * recv_stream - receive stream-oriented data
983 * @iocb: (unused)
984 * @m: descriptor for message info
985 * @buf_len: total size of user buffer area
986 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900987 *
988 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989 * will optionally wait for more; never truncates data.
990 *
991 * Returns size of returned message data, errno otherwise
992 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100993static int recv_stream(struct kiocb *iocb, struct socket *sock,
994 struct msghdr *m, size_t buf_len, int flags)
995{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700996 struct sock *sk = sock->sk;
997 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100998 struct sk_buff *buf;
999 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -05001000 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001001 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001002 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001003 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001004 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001005 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006
1007 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001008 if (unlikely(!buf_len))
1009 return -EINVAL;
1010
Allan Stephens0c3141e2008-04-15 00:22:02 -07001011 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001012
Allan Stephens0c3141e2008-04-15 00:22:02 -07001013 if (unlikely((sock->state == SS_UNCONNECTED) ||
1014 (sock->state == SS_CONNECTING))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001015 res = -ENOTCONN;
1016 goto exit;
1017 }
1018
Florian Westphal3720d402010-08-17 11:00:04 +00001019 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Allan Stephens71092ea2011-02-23 14:52:14 -05001020 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001021
Allan Stephens0c3141e2008-04-15 00:22:02 -07001022restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001023 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001024 while (skb_queue_empty(&sk->sk_receive_queue)) {
1025 if (sock->state == SS_DISCONNECTING) {
1026 res = -ENOTCONN;
1027 goto exit;
1028 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001029 if (timeout <= 0L) {
1030 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001031 goto exit;
1032 }
1033 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001034 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1035 tipc_rx_ready(sock),
1036 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001037 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001038 }
1039
1040 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001041 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001042 msg = buf_msg(buf);
1043 sz = msg_data_sz(msg);
1044 err = msg_errcode(msg);
1045
1046 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001047 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001048 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001049 goto restart;
1050 }
1051
1052 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001053 if (sz_copied == 0) {
1054 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001055 res = anc_data_recv(m, msg, tport);
1056 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001057 goto exit;
1058 }
1059
1060 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001062 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001063
Allan Stephens0232fd02011-02-21 09:45:40 -05001064 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065 needed = (buf_len - sz_copied);
1066 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001067
1068 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1069 m->msg_iov, sz_to_copy);
1070 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001071 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001072
Per Lidenb97bf3f2006-01-02 19:04:38 +01001073 sz_copied += sz_to_copy;
1074
1075 if (sz_to_copy < sz) {
1076 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001077 TIPC_SKB_CB(buf)->handle =
1078 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001079 goto exit;
1080 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001081 } else {
1082 if (sz_copied != 0)
1083 goto exit; /* can't add error msg to valid data */
1084
1085 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1086 res = 0;
1087 else
1088 res = -ECONNRESET;
1089 }
1090
1091 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001093 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1094 tipc_acknowledge(tport->ref, tport->conn_unacked);
1095 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001096 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001097
1098 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001099 if ((sz_copied < buf_len) && /* didn't get all requested data */
1100 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001101 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001102 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1103 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001104 goto restart;
1105
1106exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001107 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001108 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001109}
1110
1111/**
Ying Xuef288bef2012-08-21 11:16:57 +08001112 * tipc_write_space - wake up thread if port congestion is released
1113 * @sk: socket
1114 */
1115static void tipc_write_space(struct sock *sk)
1116{
1117 struct socket_wq *wq;
1118
1119 rcu_read_lock();
1120 wq = rcu_dereference(sk->sk_wq);
1121 if (wq_has_sleeper(wq))
1122 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1123 POLLWRNORM | POLLWRBAND);
1124 rcu_read_unlock();
1125}
1126
1127/**
1128 * tipc_data_ready - wake up threads to indicate messages have been received
1129 * @sk: socket
1130 * @len: the length of messages
1131 */
1132static void tipc_data_ready(struct sock *sk, int len)
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, POLLIN |
1140 POLLRDNORM | POLLRDBAND);
1141 rcu_read_unlock();
1142}
1143
1144/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001145 * filter_connect - Handle all incoming messages for a connection-based socket
1146 * @tsock: TIPC socket
1147 * @msg: message
1148 *
1149 * Returns TIPC error status code and socket error status code
1150 * once it encounters some errors
1151 */
1152static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
1153{
1154 struct socket *sock = tsock->sk.sk_socket;
1155 struct tipc_msg *msg = buf_msg(*buf);
Ying Xue584d24b2012-11-29 18:51:19 -05001156 struct sock *sk = &tsock->sk;
Ying Xue7e6c1312012-11-29 18:39:14 -05001157 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001158 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001159
1160 if (msg_mcast(msg))
1161 return retval;
1162
1163 switch ((int)sock->state) {
1164 case SS_CONNECTED:
1165 /* Accept only connection-based messages sent by peer */
1166 if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
1167 if (unlikely(msg_errcode(msg))) {
1168 sock->state = SS_DISCONNECTING;
1169 __tipc_disconnect(tsock->p);
1170 }
1171 retval = TIPC_OK;
1172 }
1173 break;
1174 case SS_CONNECTING:
1175 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001176 if (unlikely(msg_errcode(msg))) {
1177 sock->state = SS_DISCONNECTING;
1178 sk->sk_err = -ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001179 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001180 break;
1181 }
1182
1183 if (unlikely(!msg_connected(msg)))
1184 break;
1185
1186 res = auto_connect(sock, msg);
1187 if (res) {
1188 sock->state = SS_DISCONNECTING;
1189 sk->sk_err = res;
1190 retval = TIPC_OK;
1191 break;
1192 }
1193
1194 /* If an incoming message is an 'ACK-', it should be
1195 * discarded here because it doesn't contain useful
1196 * data. In addition, we should try to wake up
1197 * connect() routine if sleeping.
1198 */
1199 if (msg_data_sz(msg) == 0) {
1200 kfree_skb(*buf);
1201 *buf = NULL;
1202 if (waitqueue_active(sk_sleep(sk)))
1203 wake_up_interruptible(sk_sleep(sk));
1204 }
1205 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001206 break;
1207 case SS_LISTENING:
1208 case SS_UNCONNECTED:
1209 /* Accept only SYN message */
1210 if (!msg_connected(msg) && !(msg_errcode(msg)))
1211 retval = TIPC_OK;
1212 break;
1213 case SS_DISCONNECTING:
1214 break;
1215 default:
1216 pr_err("Unknown socket state %u\n", sock->state);
1217 }
1218 return retval;
1219}
1220
1221/**
Ying Xueaba79f32013-01-20 23:30:09 +01001222 * rcvbuf_limit - get proper overload limit of socket receive queue
1223 * @sk: socket
1224 * @buf: message
1225 *
1226 * For all connection oriented messages, irrespective of importance,
1227 * the default overload value (i.e. 67MB) is set as limit.
1228 *
1229 * For all connectionless messages, by default new queue limits are
1230 * as belows:
1231 *
1232 * TIPC_LOW_IMPORTANCE (5MB)
1233 * TIPC_MEDIUM_IMPORTANCE (10MB)
1234 * TIPC_HIGH_IMPORTANCE (20MB)
1235 * TIPC_CRITICAL_IMPORTANCE (40MB)
1236 *
1237 * Returns overload limit according to corresponding message importance
1238 */
1239static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1240{
1241 struct tipc_msg *msg = buf_msg(buf);
1242 unsigned int limit;
1243
1244 if (msg_connected(msg))
1245 limit = CONN_OVERLOAD_LIMIT;
1246 else
1247 limit = sk->sk_rcvbuf << (msg_importance(msg) + 5);
1248 return limit;
1249}
1250
1251/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001252 * filter_rcv - validate incoming message
1253 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001254 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001255 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001256 * Enqueues message on receive queue if acceptable; optionally handles
1257 * disconnect indication for a connected socket.
1258 *
1259 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001260 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1262 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001263static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001265 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001266 struct tipc_msg *msg = buf_msg(buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001267 unsigned int limit = rcvbuf_limit(sk, buf);
Ying Xue7e6c1312012-11-29 18:39:14 -05001268 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001269
Per Lidenb97bf3f2006-01-02 19:04:38 +01001270 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001271 if (msg_type(msg) > TIPC_DIRECT_MSG)
1272 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001273
Per Lidenb97bf3f2006-01-02 19:04:38 +01001274 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001275 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001276 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001277 } else {
Ying Xue7e6c1312012-11-29 18:39:14 -05001278 res = filter_connect(tipc_sk(sk), &buf);
1279 if (res != TIPC_OK || buf == NULL)
1280 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001281 }
1282
1283 /* Reject message if there isn't room to queue it */
Ying Xueaba79f32013-01-20 23:30:09 +01001284 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1285 return TIPC_ERR_OVERLOAD;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001286
Ying Xueaba79f32013-01-20 23:30:09 +01001287 /* Enqueue message */
Allan Stephens0232fd02011-02-21 09:45:40 -05001288 TIPC_SKB_CB(buf)->handle = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001289 __skb_queue_tail(&sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001290 skb_set_owner_r(buf, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001291
Ying Xuef288bef2012-08-21 11:16:57 +08001292 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293 return TIPC_OK;
1294}
1295
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001296/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001297 * backlog_rcv - handle incoming message from backlog queue
1298 * @sk: socket
1299 * @buf: message
1300 *
1301 * Caller must hold socket lock, but not port lock.
1302 *
1303 * Returns 0
1304 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001305static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1306{
1307 u32 res;
1308
1309 res = filter_rcv(sk, buf);
1310 if (res)
1311 tipc_reject_msg(buf, res);
1312 return 0;
1313}
1314
1315/**
1316 * dispatch - handle incoming message
1317 * @tport: TIPC port that received message
1318 * @buf: message
1319 *
1320 * Called with port lock already taken.
1321 *
1322 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1323 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001324static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1325{
1326 struct sock *sk = (struct sock *)tport->usr_handle;
1327 u32 res;
1328
1329 /*
1330 * Process message if socket is unlocked; otherwise add to backlog queue
1331 *
1332 * This code is based on sk_receive_skb(), but must be distinct from it
1333 * since a TIPC-specific filter/reject mechanism is utilized
1334 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001335 bh_lock_sock(sk);
1336 if (!sock_owned_by_user(sk)) {
1337 res = filter_rcv(sk, buf);
1338 } else {
Ying Xueaba79f32013-01-20 23:30:09 +01001339 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
Zhu Yi53eecb12010-03-04 18:01:45 +00001340 res = TIPC_ERR_OVERLOAD;
1341 else
1342 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001343 }
1344 bh_unlock_sock(sk);
1345
1346 return res;
1347}
1348
1349/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001350 * wakeupdispatch - wake up port after congestion
1351 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001352 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001353 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001354 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001355static void wakeupdispatch(struct tipc_port *tport)
1356{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001357 struct sock *sk = (struct sock *)tport->usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001358
Ying Xuef288bef2012-08-21 11:16:57 +08001359 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001360}
1361
1362/**
1363 * connect - establish a connection to another TIPC port
1364 * @sock: socket structure
1365 * @dest: socket address for destination port
1366 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001367 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001368 *
1369 * Returns 0 on success, errno otherwise
1370 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001371static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372 int flags)
1373{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001374 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001375 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1376 struct msghdr m = {NULL,};
Allan Stephensa0f40f02011-05-26 13:44:34 -04001377 unsigned int timeout;
Allan Stephensb89741a2008-04-15 00:20:37 -07001378 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001379
Allan Stephens0c3141e2008-04-15 00:22:02 -07001380 lock_sock(sk);
1381
Allan Stephensb89741a2008-04-15 00:20:37 -07001382 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001383 if (sock->state == SS_READY) {
1384 res = -EOPNOTSUPP;
1385 goto exit;
1386 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387
Allan Stephensb89741a2008-04-15 00:20:37 -07001388 /*
1389 * Reject connection attempt using multicast address
1390 *
1391 * Note: send_msg() validates the rest of the address fields,
1392 * so there's no need to do it here
1393 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001394 if (dst->addrtype == TIPC_ADDR_MCAST) {
1395 res = -EINVAL;
1396 goto exit;
1397 }
1398
Ying Xue584d24b2012-11-29 18:51:19 -05001399 timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001400
Ying Xue584d24b2012-11-29 18:51:19 -05001401 switch (sock->state) {
1402 case SS_UNCONNECTED:
1403 /* Send a 'SYN-' to destination */
1404 m.msg_name = dest;
1405 m.msg_namelen = destlen;
1406
1407 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1408 * indicate send_msg() is never blocked.
1409 */
1410 if (!timeout)
1411 m.msg_flags = MSG_DONTWAIT;
1412
1413 res = send_msg(NULL, sock, &m, 0);
1414 if ((res < 0) && (res != -EWOULDBLOCK))
1415 goto exit;
1416
1417 /* Just entered SS_CONNECTING state; the only
1418 * difference is that return value in non-blocking
1419 * case is EINPROGRESS, rather than EALREADY.
1420 */
1421 res = -EINPROGRESS;
1422 break;
1423 case SS_CONNECTING:
1424 res = -EALREADY;
1425 break;
1426 case SS_CONNECTED:
1427 res = -EISCONN;
1428 break;
1429 default:
1430 res = -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001431 goto exit;
Allan Stephensb89741a2008-04-15 00:20:37 -07001432 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433
Ying Xue584d24b2012-11-29 18:51:19 -05001434 if (sock->state == SS_CONNECTING) {
1435 if (!timeout)
1436 goto exit;
1437
1438 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1439 release_sock(sk);
1440 res = wait_event_interruptible_timeout(*sk_sleep(sk),
1441 sock->state != SS_CONNECTING,
1442 timeout ? (long)msecs_to_jiffies(timeout)
1443 : MAX_SCHEDULE_TIMEOUT);
1444 lock_sock(sk);
1445 if (res <= 0) {
1446 if (res == 0)
1447 res = -ETIMEDOUT;
1448 else
1449 ; /* leave "res" unchanged */
1450 goto exit;
1451 }
1452 }
1453
1454 if (unlikely(sock->state == SS_DISCONNECTING))
1455 res = sock_error(sk);
1456 else
1457 res = 0;
1458
Allan Stephens0c3141e2008-04-15 00:22:02 -07001459exit:
1460 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001461 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 * listen - allow socket to listen for incoming connections
1466 * @sock: socket structure
1467 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001468 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001469 * Returns 0 on success, errno otherwise
1470 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001471static int listen(struct socket *sock, int len)
1472{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001473 struct sock *sk = sock->sk;
1474 int res;
1475
1476 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001477
Ying Xue245f3d32011-07-06 06:01:13 -04001478 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001479 res = -EINVAL;
1480 else {
1481 sock->state = SS_LISTENING;
1482 res = 0;
1483 }
1484
1485 release_sock(sk);
1486 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001487}
1488
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001489/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001490 * accept - wait for connection request
1491 * @sock: listening socket
1492 * @newsock: new socket that is to be connected
1493 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001494 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001495 * Returns 0 on success, errno otherwise
1496 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001497static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001498{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001499 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001500 struct sk_buff *buf;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001501 struct tipc_sock *new_tsock;
1502 struct tipc_port *new_tport;
1503 struct tipc_msg *msg;
1504 u32 new_ref;
1505
Allan Stephens0c3141e2008-04-15 00:22:02 -07001506 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001507
Allan Stephens0c3141e2008-04-15 00:22:02 -07001508 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001509
Allan Stephens0c3141e2008-04-15 00:22:02 -07001510 if (sock->state != SS_LISTENING) {
1511 res = -EINVAL;
1512 goto exit;
1513 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001514
Allan Stephens0c3141e2008-04-15 00:22:02 -07001515 while (skb_queue_empty(&sk->sk_receive_queue)) {
1516 if (flags & O_NONBLOCK) {
1517 res = -EWOULDBLOCK;
1518 goto exit;
1519 }
1520 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001521 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001522 (!skb_queue_empty(&sk->sk_receive_queue)));
1523 lock_sock(sk);
1524 if (res)
1525 goto exit;
1526 }
1527
1528 buf = skb_peek(&sk->sk_receive_queue);
1529
Eric Paris3f378b62009-11-05 22:18:14 -08001530 res = tipc_create(sock_net(sock->sk), new_sock, 0, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001531 if (res)
1532 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001533
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001534 new_sk = new_sock->sk;
1535 new_tsock = tipc_sk(new_sk);
1536 new_tport = new_tsock->p;
1537 new_ref = new_tport->ref;
1538 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001539
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001540 /* we lock on new_sk; but lockdep sees the lock on sk */
1541 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001542
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001543 /*
1544 * Reject any stray messages received by new socket
1545 * before the socket lock was taken (very, very unlikely)
1546 */
1547 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001548
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001549 /* Connect new socket to it's peer */
1550 new_tsock->peer_name.ref = msg_origport(msg);
1551 new_tsock->peer_name.node = msg_orignode(msg);
1552 tipc_connect(new_ref, &new_tsock->peer_name);
1553 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001554
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001555 tipc_set_portimportance(new_ref, msg_importance(msg));
1556 if (msg_named(msg)) {
1557 new_tport->conn_type = msg_nametype(msg);
1558 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001559 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001560
1561 /*
1562 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1563 * Respond to 'SYN+' by queuing it on new socket.
1564 */
1565 if (!msg_data_sz(msg)) {
1566 struct msghdr m = {NULL,};
1567
1568 advance_rx_queue(sk);
1569 send_packet(NULL, new_sock, &m, 0);
1570 } else {
1571 __skb_dequeue(&sk->sk_receive_queue);
1572 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01001573 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001574 }
1575 release_sock(new_sk);
1576
Per Lidenb97bf3f2006-01-02 19:04:38 +01001577exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001578 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001579 return res;
1580}
1581
1582/**
1583 * shutdown - shutdown socket connection
1584 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001585 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001586 *
1587 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001588 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001589 * Returns 0 on success, errno otherwise
1590 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001591static int shutdown(struct socket *sock, int how)
1592{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001593 struct sock *sk = sock->sk;
1594 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001595 struct sk_buff *buf;
1596 int res;
1597
Allan Stephense247a8f2008-03-06 15:05:38 -08001598 if (how != SHUT_RDWR)
1599 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600
Allan Stephens0c3141e2008-04-15 00:22:02 -07001601 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001602
1603 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001604 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001605 case SS_CONNECTED:
1606
Per Lidenb97bf3f2006-01-02 19:04:38 +01001607restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001608 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001609 buf = __skb_dequeue(&sk->sk_receive_queue);
1610 if (buf) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001611 if (TIPC_SKB_CB(buf)->handle != 0) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001612 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001613 goto restart;
1614 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001615 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001616 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001617 } else {
1618 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001619 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001620
1621 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001622
1623 /* fall through */
1624
1625 case SS_DISCONNECTING:
1626
Ying Xue75031152012-10-29 09:38:15 -04001627 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01001628 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04001629
1630 /* Wake up anyone sleeping in poll */
1631 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001632 res = 0;
1633 break;
1634
1635 default:
1636 res = -ENOTCONN;
1637 }
1638
Allan Stephens0c3141e2008-04-15 00:22:02 -07001639 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001640 return res;
1641}
1642
1643/**
1644 * setsockopt - set socket option
1645 * @sock: socket structure
1646 * @lvl: option level
1647 * @opt: option identifier
1648 * @ov: pointer to new option value
1649 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001650 *
1651 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001653 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001654 * Returns 0 on success, errno otherwise
1655 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001656static int setsockopt(struct socket *sock,
David S. Millerb7058842009-09-30 16:12:20 -07001657 int lvl, int opt, char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001658{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001659 struct sock *sk = sock->sk;
1660 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661 u32 value;
1662 int res;
1663
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001664 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1665 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666 if (lvl != SOL_TIPC)
1667 return -ENOPROTOOPT;
1668 if (ol < sizeof(value))
1669 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001670 res = get_user(value, (u32 __user *)ov);
1671 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001672 return res;
1673
Allan Stephens0c3141e2008-04-15 00:22:02 -07001674 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001675
Per Lidenb97bf3f2006-01-02 19:04:38 +01001676 switch (opt) {
1677 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001678 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679 break;
1680 case TIPC_SRC_DROPPABLE:
1681 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001682 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001683 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001684 res = -ENOPROTOOPT;
1685 break;
1686 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001687 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001688 break;
1689 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001690 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001691 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001692 break;
1693 default:
1694 res = -EINVAL;
1695 }
1696
Allan Stephens0c3141e2008-04-15 00:22:02 -07001697 release_sock(sk);
1698
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699 return res;
1700}
1701
1702/**
1703 * getsockopt - get socket option
1704 * @sock: socket structure
1705 * @lvl: option level
1706 * @opt: option identifier
1707 * @ov: receptacle for option value
1708 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001709 *
1710 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001711 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001712 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001713 * Returns 0 on success, errno otherwise
1714 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001715static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001716 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001717{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001718 struct sock *sk = sock->sk;
1719 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001720 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001721 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001722 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001723
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001724 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1725 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726 if (lvl != SOL_TIPC)
1727 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001728 res = get_user(len, ol);
1729 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001730 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001731
Allan Stephens0c3141e2008-04-15 00:22:02 -07001732 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001733
1734 switch (opt) {
1735 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001736 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001737 break;
1738 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001739 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001740 break;
1741 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001742 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001743 break;
1744 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001745 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001746 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001747 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001748 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001749 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001750 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001751 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001752 value = skb_queue_len(&sk->sk_receive_queue);
1753 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001754 default:
1755 res = -EINVAL;
1756 }
1757
Allan Stephens0c3141e2008-04-15 00:22:02 -07001758 release_sock(sk);
1759
Paul Gortmaker25860c32010-12-31 18:59:31 +00001760 if (res)
1761 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001762
Paul Gortmaker25860c32010-12-31 18:59:31 +00001763 if (len < sizeof(value))
1764 return -EINVAL;
1765
1766 if (copy_to_user(ov, &value, sizeof(value)))
1767 return -EFAULT;
1768
1769 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001770}
1771
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001772/* Protocol switches for the various types of TIPC sockets */
1773
Florian Westphalbca65ea2008-02-07 18:18:01 -08001774static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001775 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001776 .family = AF_TIPC,
1777 .release = release,
1778 .bind = bind,
1779 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001780 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001781 .accept = sock_no_accept,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001782 .getname = get_name,
1783 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001784 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001785 .listen = sock_no_listen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001786 .shutdown = shutdown,
1787 .setsockopt = setsockopt,
1788 .getsockopt = getsockopt,
1789 .sendmsg = send_msg,
1790 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001791 .mmap = sock_no_mmap,
1792 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793};
1794
Florian Westphalbca65ea2008-02-07 18:18:01 -08001795static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001796 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001797 .family = AF_TIPC,
1798 .release = release,
1799 .bind = bind,
1800 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001801 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001802 .accept = accept,
1803 .getname = get_name,
1804 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001805 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001806 .listen = listen,
1807 .shutdown = shutdown,
1808 .setsockopt = setsockopt,
1809 .getsockopt = getsockopt,
1810 .sendmsg = send_packet,
1811 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001812 .mmap = sock_no_mmap,
1813 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814};
1815
Florian Westphalbca65ea2008-02-07 18:18:01 -08001816static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001817 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001818 .family = AF_TIPC,
1819 .release = release,
1820 .bind = bind,
1821 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001822 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001823 .accept = accept,
1824 .getname = get_name,
1825 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001826 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001827 .listen = listen,
1828 .shutdown = shutdown,
1829 .setsockopt = setsockopt,
1830 .getsockopt = getsockopt,
1831 .sendmsg = send_stream,
1832 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001833 .mmap = sock_no_mmap,
1834 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001835};
1836
Florian Westphalbca65ea2008-02-07 18:18:01 -08001837static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001838 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001839 .family = AF_TIPC,
1840 .create = tipc_create
1841};
1842
1843static struct proto tipc_proto = {
1844 .name = "TIPC",
1845 .owner = THIS_MODULE,
1846 .obj_size = sizeof(struct tipc_sock)
1847};
1848
1849/**
Per Liden4323add2006-01-18 00:38:21 +01001850 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001851 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001852 * Returns 0 on success, errno otherwise
1853 */
Per Liden4323add2006-01-18 00:38:21 +01001854int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855{
1856 int res;
1857
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001858 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001859 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001860 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001861 goto out;
1862 }
1863
1864 res = sock_register(&tipc_family_ops);
1865 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001866 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001867 proto_unregister(&tipc_proto);
1868 goto out;
1869 }
1870
1871 sockets_enabled = 1;
1872 out:
1873 return res;
1874}
1875
1876/**
Per Liden4323add2006-01-18 00:38:21 +01001877 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001878 */
Per Liden4323add2006-01-18 00:38:21 +01001879void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001880{
1881 if (!sockets_enabled)
1882 return;
1883
1884 sockets_enabled = 0;
1885 sock_unregister(tipc_family_ops.family);
1886 proto_unregister(&tipc_proto);
1887}