blob: 2f90beba282b193d0878336718e4457ae714b7d9 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Allan Stephens5eee6a62007-06-10 17:24:55 -07004 * Copyright (c) 2001-2007, Ericsson AB
Allan Stephens4132fac2011-01-07 13:12:12 -05005 * Copyright (c) 2004-2008, 2010-2011, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include <net/sock.h>
38
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000040#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041
42#define SS_LISTENING -1 /* socket is listening */
43#define SS_READY -2 /* socket is connectionless */
44
Allan Stephens3654ea02008-04-13 21:35:11 -070045#define OVERLOAD_LIMIT_BASE 5000
46#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010047
48struct tipc_sock {
49 struct sock sk;
50 struct tipc_port *p;
Allan Stephens2da59912008-07-14 22:43:32 -070051 struct tipc_portid peer_name;
Allan Stephensa0f40f02011-05-26 13:44:34 -040052 unsigned int conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +010053};
54
Allan Stephens0c3141e2008-04-15 00:22:02 -070055#define tipc_sk(sk) ((struct tipc_sock *)(sk))
56#define tipc_sk_port(sk) ((struct tipc_port *)(tipc_sk(sk)->p))
Per Lidenb97bf3f2006-01-02 19:04:38 +010057
Allan Stephens71092ea2011-02-23 14:52:14 -050058#define tipc_rx_ready(sock) (!skb_queue_empty(&sock->sk->sk_receive_queue) || \
59 (sock->state == SS_DISCONNECTING))
60
Allan Stephens0c3141e2008-04-15 00:22:02 -070061static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +010062static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
63static void wakeupdispatch(struct tipc_port *tport);
64
Florian Westphalbca65ea2008-02-07 18:18:01 -080065static const struct proto_ops packet_ops;
66static const struct proto_ops stream_ops;
67static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010068
69static struct proto tipc_proto;
70
Allan Stephense3ec9c72010-12-31 18:59:34 +000071static int sockets_enabled;
Per Lidenb97bf3f2006-01-02 19:04:38 +010072
73static atomic_t tipc_queue_size = ATOMIC_INIT(0);
74
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090075/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070076 * Revised TIPC socket locking policy:
77 *
78 * Most socket operations take the standard socket lock when they start
79 * and hold it until they finish (or until they need to sleep). Acquiring
80 * this lock grants the owner exclusive access to the fields of the socket
81 * data structures, with the exception of the backlog queue. A few socket
82 * operations can be done without taking the socket lock because they only
83 * read socket information that never changes during the life of the socket.
84 *
85 * Socket operations may acquire the lock for the associated TIPC port if they
86 * need to perform an operation on the port. If any routine needs to acquire
87 * both the socket lock and the port lock it must take the socket lock first
88 * to avoid the risk of deadlock.
89 *
90 * The dispatcher handling incoming messages cannot grab the socket lock in
91 * the standard fashion, since invoked it runs at the BH level and cannot block.
92 * Instead, it checks to see if the socket lock is currently owned by someone,
93 * and either handles the message itself or adds it to the socket's backlog
94 * queue; in the latter case the queued message is processed once the process
95 * owning the socket lock releases it.
96 *
97 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
98 * the problem of a blocked socket operation preventing any other operations
99 * from occurring. However, applications must be careful if they have
100 * multiple threads trying to send (or receive) on the same socket, as these
101 * operations might interfere with each other. For example, doing a connect
102 * and a receive at the same time might allow the receive to consume the
103 * ACK message meant for the connect. While additional work could be done
104 * to try and overcome this, it doesn't seem to be worthwhile at the present.
105 *
106 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
107 * that another operation that must be performed in a non-blocking manner is
108 * not delayed for very long because the lock has already been taken.
109 *
110 * NOTE: This code assumes that certain fields of a port/socket pair are
111 * constant over its lifetime; such fields can be examined without taking
112 * the socket lock and/or port lock, and do not need to be re-read even
113 * after resuming processing after waiting. These fields include:
114 * - socket type
115 * - pointer to socket sk structure (aka tipc_sock structure)
116 * - pointer to port structure
117 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119
120/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700121 * advance_rx_queue - discard first buffer in socket receive queue
122 *
123 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100124 */
125
Allan Stephens0c3141e2008-04-15 00:22:02 -0700126static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700128 buf_discard(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129 atomic_dec(&tipc_queue_size);
130}
131
132/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700133 * discard_rx_queue - discard all buffers in socket receive queue
134 *
135 * Caller must hold socket lock
136 */
137
138static void discard_rx_queue(struct sock *sk)
139{
140 struct sk_buff *buf;
141
142 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
143 atomic_dec(&tipc_queue_size);
144 buf_discard(buf);
145 }
146}
147
148/**
149 * reject_rx_queue - reject all buffers in socket receive queue
150 *
151 * Caller must hold socket lock
152 */
153
154static void reject_rx_queue(struct sock *sk)
155{
156 struct sk_buff *buf;
157
158 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
159 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
160 atomic_dec(&tipc_queue_size);
161 }
162}
163
164/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 * tipc_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700166 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 * @sock: pre-allocated socket structure
168 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800169 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900170 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700171 * This routine creates additional data structures used by the TIPC socket,
172 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 *
174 * Returns 0 on success, errno otherwise
175 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700176
Eric Paris3f378b62009-11-05 22:18:14 -0800177static int tipc_create(struct net *net, struct socket *sock, int protocol,
178 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700180 const struct proto_ops *ops;
181 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700183 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700184
185 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100186
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800187 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700188 return -EAFNOSUPPORT;
189
Per Lidenb97bf3f2006-01-02 19:04:38 +0100190 if (unlikely(protocol != 0))
191 return -EPROTONOSUPPORT;
192
Per Lidenb97bf3f2006-01-02 19:04:38 +0100193 switch (sock->type) {
194 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700195 ops = &stream_ops;
196 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197 break;
198 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700199 ops = &packet_ops;
200 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 break;
202 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100203 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700204 ops = &msg_ops;
205 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206 break;
Allan Stephens49978652006-06-25 23:47:18 -0700207 default:
Allan Stephens49978652006-06-25 23:47:18 -0700208 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209 }
210
Allan Stephens0c3141e2008-04-15 00:22:02 -0700211 /* Allocate socket's protocol area */
212
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700213 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700214 if (sk == NULL)
215 return -ENOMEM;
216
217 /* Allocate TIPC port for socket to use */
218
Allan Stephens0ea52242008-07-14 22:42:19 -0700219 tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
220 TIPC_LOW_IMPORTANCE);
221 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700222 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223 return -ENOMEM;
224 }
225
Allan Stephens0c3141e2008-04-15 00:22:02 -0700226 /* Finish initializing socket data structures */
227
228 sock->ops = ops;
229 sock->state = state;
230
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700232 sk->sk_backlog_rcv = backlog_rcv;
Allan Stephens0ea52242008-07-14 22:42:19 -0700233 tipc_sk(sk)->p = tp_ptr;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400234 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700236 spin_unlock_bh(tp_ptr->lock);
237
Allan Stephens0c3141e2008-04-15 00:22:02 -0700238 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700239 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700240 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700241 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700242 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244 return 0;
245}
246
247/**
248 * release - destroy a TIPC socket
249 * @sock: socket to destroy
250 *
251 * This routine cleans up any messages that are still queued on the socket.
252 * For DGRAM and RDM socket types, all queued messages are rejected.
253 * For SEQPACKET and STREAM socket types, the first message is rejected
254 * and any others are discarded. (If the first message on a STREAM socket
255 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900256 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257 * NOTE: Rejected messages are not necessarily returned to the sender! They
258 * are returned or discarded according to the "destination droppable" setting
259 * specified for the message by the sender.
260 *
261 * Returns 0 on success, errno otherwise
262 */
263
264static int release(struct socket *sock)
265{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700267 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700269 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270
Allan Stephens0c3141e2008-04-15 00:22:02 -0700271 /*
272 * Exit if socket isn't fully initialized (occurs when a failed accept()
273 * releases a pre-allocated child socket that was never used)
274 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900275
Allan Stephens0c3141e2008-04-15 00:22:02 -0700276 if (sk == NULL)
277 return 0;
278
279 tport = tipc_sk_port(sk);
280 lock_sock(sk);
281
282 /*
283 * Reject all unreceived messages, except on an active connection
284 * (which disconnects locally & sends a 'FIN+' to peer)
285 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286
287 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700288 buf = __skb_dequeue(&sk->sk_receive_queue);
289 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290 break;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700291 atomic_dec(&tipc_queue_size);
Allan Stephens0232fd02011-02-21 09:45:40 -0500292 if (TIPC_SKB_CB(buf)->handle != 0)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293 buf_discard(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700294 else {
295 if ((sock->state == SS_CONNECTING) ||
296 (sock->state == SS_CONNECTED)) {
297 sock->state = SS_DISCONNECTING;
298 tipc_disconnect(tport->ref);
299 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700301 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 }
303
Allan Stephens0c3141e2008-04-15 00:22:02 -0700304 /*
305 * Delete TIPC port; this ensures no more messages are queued
306 * (also disconnects an active connection & sends a 'FIN-' to peer)
307 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100308
Allan Stephens0c3141e2008-04-15 00:22:02 -0700309 res = tipc_deleteport(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310
Allan Stephens0c3141e2008-04-15 00:22:02 -0700311 /* Discard any remaining (connection-based) messages in receive queue */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312
Allan Stephens0c3141e2008-04-15 00:22:02 -0700313 discard_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314
Allan Stephens0c3141e2008-04-15 00:22:02 -0700315 /* Reject any messages that accumulated in backlog queue */
316
317 sock->state = SS_DISCONNECTING;
318 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100319
320 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700321 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322
Per Lidenb97bf3f2006-01-02 19:04:38 +0100323 return res;
324}
325
326/**
327 * bind - associate or disassocate TIPC name(s) with a socket
328 * @sock: socket structure
329 * @uaddr: socket address describing name(s) and desired operation
330 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900331 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332 * Name and name sequence binding is indicated using a positive scope value;
333 * a negative scope value unbinds the specified name. Specifying no name
334 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900335 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700337 *
338 * NOTE: This routine doesn't need to take the socket lock since it doesn't
339 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340 */
341
342static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
343{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700345 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346
Allan Stephens0c3141e2008-04-15 00:22:02 -0700347 if (unlikely(!uaddr_len))
348 return tipc_withdraw(portref, 0, NULL);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900349
Allan Stephens0c3141e2008-04-15 00:22:02 -0700350 if (uaddr_len < sizeof(struct sockaddr_tipc))
351 return -EINVAL;
352 if (addr->family != AF_TIPC)
353 return -EAFNOSUPPORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355 if (addr->addrtype == TIPC_ADDR_NAME)
356 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700357 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
358 return -EAFNOSUPPORT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900359
Allan Stephens0c3141e2008-04-15 00:22:02 -0700360 return (addr->scope > 0) ?
361 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
362 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363}
364
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900365/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 * get_name - get port ID of socket or peer socket
367 * @sock: socket structure
368 * @uaddr: area for returned socket address
369 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700370 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900371 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700373 *
Allan Stephens2da59912008-07-14 22:43:32 -0700374 * NOTE: This routine doesn't need to take the socket lock since it only
375 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000376 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377 */
378
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900379static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100380 int *uaddr_len, int peer)
381{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700383 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000385 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700386 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700387 if ((sock->state != SS_CONNECTED) &&
388 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
389 return -ENOTCONN;
390 addr->addr.id.ref = tsock->peer_name.ref;
391 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700392 } else {
Allan Stephensb924dcf2010-11-30 12:01:03 +0000393 addr->addr.id.ref = tsock->p->ref;
394 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700395 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396
397 *uaddr_len = sizeof(*addr);
398 addr->addrtype = TIPC_ADDR_ID;
399 addr->family = AF_TIPC;
400 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 addr->addr.name.domain = 0;
402
Allan Stephens0c3141e2008-04-15 00:22:02 -0700403 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100404}
405
406/**
407 * poll - read and possibly block on pollmask
408 * @file: file structure associated with the socket
409 * @sock: socket for which to calculate the poll bits
410 * @wait: ???
411 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700412 * Returns pollmask value
413 *
414 * COMMENTARY:
415 * It appears that the usual socket locking mechanisms are not useful here
416 * since the pollmask info is potentially out-of-date the moment this routine
417 * exits. TCP and other protocols seem to rely on higher level poll routines
418 * to handle any preventable race conditions, so TIPC will do the same ...
419 *
420 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700421 *
Allan Stephensf662c072010-08-17 11:00:06 +0000422 * socket state flags set
423 * ------------ ---------
424 * unconnected no read flags
425 * no write flags
426 *
427 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
428 * no write flags
429 *
430 * connected POLLIN/POLLRDNORM if data in rx queue
431 * POLLOUT if port is not congested
432 *
433 * disconnecting POLLIN/POLLRDNORM/POLLHUP
434 * no write flags
435 *
436 * listening POLLIN if SYN in rx queue
437 * no write flags
438 *
439 * ready POLLIN/POLLRDNORM if data in rx queue
440 * [connectionless] POLLOUT (since port cannot be congested)
441 *
442 * IMPORTANT: The fact that a read or write operation is indicated does NOT
443 * imply that the operation will succeed, merely that it should be performed
444 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445 */
446
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900447static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448 poll_table *wait)
449{
Allan Stephens9b674e82008-03-26 16:48:21 -0700450 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000451 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700452
Eric Dumazetaa395142010-04-20 13:03:51 +0000453 poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700454
Allan Stephensf662c072010-08-17 11:00:06 +0000455 switch ((int)sock->state) {
456 case SS_READY:
457 case SS_CONNECTED:
458 if (!tipc_sk_port(sk)->congested)
459 mask |= POLLOUT;
460 /* fall thru' */
461 case SS_CONNECTING:
462 case SS_LISTENING:
463 if (!skb_queue_empty(&sk->sk_receive_queue))
464 mask |= (POLLIN | POLLRDNORM);
465 break;
466 case SS_DISCONNECTING:
467 mask = (POLLIN | POLLRDNORM | POLLHUP);
468 break;
469 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700470
471 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472}
473
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900474/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475 * dest_name_check - verify user is permitted to send to specified port name
476 * @dest: destination address
477 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900478 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479 * Prevents restricted configuration commands from being issued by
480 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900481 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482 * Returns 0 if permission is granted, otherwise errno
483 */
484
Sam Ravnborg05790c62006-03-20 22:37:04 -0800485static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486{
487 struct tipc_cfg_msg_hdr hdr;
488
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900489 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
490 return 0;
491 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
492 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900493 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
494 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495
Allan Stephens3f8dd942011-01-18 13:09:29 -0500496 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
497 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900498 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700500 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900502
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503 return 0;
504}
505
506/**
507 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700508 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 * @sock: socket structure
510 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700511 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900512 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900514 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
516 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900517 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518 * Returns the number of bytes sent on success, or errno otherwise
519 */
520
521static int send_msg(struct kiocb *iocb, struct socket *sock,
522 struct msghdr *m, size_t total_len)
523{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700524 struct sock *sk = sock->sk;
525 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900526 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527 int needs_conn;
Ying Xue1d835872011-07-06 05:53:15 -0400528 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529 int res = -EINVAL;
530
531 if (unlikely(!dest))
532 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700533 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
534 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 return -EINVAL;
Allan Stephensc29c3f72010-04-20 17:58:24 -0400536 if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
537 (m->msg_iovlen > (unsigned)INT_MAX))
538 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539
Allan Stephens0c3141e2008-04-15 00:22:02 -0700540 if (iocb)
541 lock_sock(sk);
542
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543 needs_conn = (sock->state != SS_READY);
544 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700545 if (sock->state == SS_LISTENING) {
546 res = -EPIPE;
547 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700548 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700549 if (sock->state != SS_UNCONNECTED) {
550 res = -EISCONN;
551 goto exit;
552 }
553 if ((tport->published) ||
554 ((sock->type == SOCK_STREAM) && (total_len != 0))) {
555 res = -EOPNOTSUPP;
556 goto exit;
557 }
558 if (dest->addrtype == TIPC_ADDR_NAME) {
559 tport->conn_type = dest->addr.name.name.type;
560 tport->conn_instance = dest->addr.name.name.instance;
561 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100562
563 /* Abort any pending connection attempts (very unlikely) */
564
Allan Stephens0c3141e2008-04-15 00:22:02 -0700565 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100566 }
567
Ying Xue1d835872011-07-06 05:53:15 -0400568 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
569
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900570 do {
571 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000572 res = dest_name_check(dest, m);
573 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700574 break;
575 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900576 &dest->addr.name.name,
577 dest->addr.name.domain,
578 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500579 m->msg_iov,
580 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000581 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700582 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900583 &dest->addr.id,
584 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500585 m->msg_iov,
586 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000587 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588 if (needs_conn) {
589 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700590 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 }
Allan Stephens2db99832010-12-31 18:59:33 +0000592 res = dest_name_check(dest, m);
593 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700594 break;
595 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900596 &dest->addr.nameseq,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900597 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500598 m->msg_iov,
599 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900600 }
601 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000602 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700603 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700604 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900605 }
Ying Xue1d835872011-07-06 05:53:15 -0400606 if (timeout_val <= 0L) {
607 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700608 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700610 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400611 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
612 !tport->congested, timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700613 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900614 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700615
616exit:
617 if (iocb)
618 release_sock(sk);
619 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620}
621
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900622/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700624 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 * @sock: socket structure
626 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700627 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900628 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900630 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100631 * Returns the number of bytes sent on success, or errno otherwise
632 */
633
634static int send_packet(struct kiocb *iocb, struct socket *sock,
635 struct msghdr *m, size_t total_len)
636{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700637 struct sock *sk = sock->sk;
638 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900639 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Ying Xue1d835872011-07-06 05:53:15 -0400640 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 int res;
642
643 /* Handle implied connection establishment */
644
645 if (unlikely(dest))
646 return send_msg(iocb, sock, m, total_len);
647
Allan Stephensc29c3f72010-04-20 17:58:24 -0400648 if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
649 (m->msg_iovlen > (unsigned)INT_MAX))
650 return -EMSGSIZE;
651
Allan Stephens0c3141e2008-04-15 00:22:02 -0700652 if (iocb)
653 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654
Ying Xue1d835872011-07-06 05:53:15 -0400655 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
656
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900657 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700658 if (unlikely(sock->state != SS_CONNECTED)) {
659 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900660 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700661 else
662 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700663 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700664 }
665
Allan Stephens26896902011-04-21 10:42:07 -0500666 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov,
667 total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000668 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700669 break;
Ying Xue1d835872011-07-06 05:53:15 -0400670 if (timeout_val <= 0L) {
671 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700672 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700674 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400675 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
676 (!tport->congested || !tport->connected), timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700677 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900678 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700679
680 if (iocb)
681 release_sock(sk);
682 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683}
684
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900685/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686 * send_stream - send stream-oriented data
687 * @iocb: (unused)
688 * @sock: socket structure
689 * @m: data to send
690 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900691 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100692 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900693 *
694 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700695 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100696 */
697
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698static int send_stream(struct kiocb *iocb, struct socket *sock,
699 struct msghdr *m, size_t total_len)
700{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700701 struct sock *sk = sock->sk;
702 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100703 struct msghdr my_msg;
704 struct iovec my_iov;
705 struct iovec *curr_iov;
706 int curr_iovlen;
707 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700708 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100709 int curr_left;
710 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700711 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900713
Allan Stephens0c3141e2008-04-15 00:22:02 -0700714 lock_sock(sk);
715
Allan Stephens05646c92007-06-10 17:25:24 -0700716 /* Handle special cases where there is no connection */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100717
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900718 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700719 if (sock->state == SS_UNCONNECTED) {
720 res = send_packet(NULL, sock, m, total_len);
721 goto exit;
722 } else if (sock->state == SS_DISCONNECTING) {
723 res = -EPIPE;
724 goto exit;
725 } else {
726 res = -ENOTCONN;
727 goto exit;
728 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900729 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730
Allan Stephens0c3141e2008-04-15 00:22:02 -0700731 if (unlikely(m->msg_name)) {
732 res = -EISCONN;
733 goto exit;
734 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700735
Allan Stephensc29c3f72010-04-20 17:58:24 -0400736 if ((total_len > (unsigned)INT_MAX) ||
737 (m->msg_iovlen > (unsigned)INT_MAX)) {
738 res = -EMSGSIZE;
739 goto exit;
740 }
741
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900742 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743 * Send each iovec entry using one or more messages
744 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900745 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100746 * (i.e. one large iovec entry), but could be improved to pass sets
747 * of small iovec entries into send_packet().
748 */
749
Allan Stephens1303e8f2006-06-25 23:46:50 -0700750 curr_iov = m->msg_iov;
751 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752 my_msg.msg_iov = &my_iov;
753 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700754 my_msg.msg_flags = m->msg_flags;
755 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700756 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100757
Allan Stephens05646c92007-06-10 17:25:24 -0700758 hdr_size = msg_hdr_sz(&tport->phdr);
759
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760 while (curr_iovlen--) {
761 curr_start = curr_iov->iov_base;
762 curr_left = curr_iov->iov_len;
763
764 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700765 bytes_to_send = tport->max_pkt - hdr_size;
766 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
767 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
768 if (curr_left < bytes_to_send)
769 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100770 my_iov.iov_base = curr_start;
771 my_iov.iov_len = bytes_to_send;
Allan Stephens26896902011-04-21 10:42:07 -0500772 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000773 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700774 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700775 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700776 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700777 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 curr_left -= bytes_to_send;
779 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700780 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100781 }
782
783 curr_iov++;
784 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700785 res = bytes_sent;
786exit:
787 release_sock(sk);
788 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789}
790
791/**
792 * auto_connect - complete connection setup to a remote port
793 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900795 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796 * Returns 0 on success, errno otherwise
797 */
798
Allan Stephens0c3141e2008-04-15 00:22:02 -0700799static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100800{
Allan Stephens2da59912008-07-14 22:43:32 -0700801 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802
803 if (msg_errcode(msg)) {
804 sock->state = SS_DISCONNECTING;
805 return -ECONNREFUSED;
806 }
807
Allan Stephens2da59912008-07-14 22:43:32 -0700808 tsock->peer_name.ref = msg_origport(msg);
809 tsock->peer_name.node = msg_orignode(msg);
810 tipc_connect2port(tsock->p->ref, &tsock->peer_name);
811 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812 sock->state = SS_CONNECTED;
813 return 0;
814}
815
816/**
817 * set_orig_addr - capture sender's address for received message
818 * @m: descriptor for message info
819 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900820 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100821 * Note: Address is not captured if not requested by receiver.
822 */
823
Sam Ravnborg05790c62006-03-20 22:37:04 -0800824static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900826 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100827
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900828 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829 addr->family = AF_TIPC;
830 addr->addrtype = TIPC_ADDR_ID;
831 addr->addr.id.ref = msg_origport(msg);
832 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000833 addr->addr.name.domain = 0; /* could leave uninitialized */
834 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100835 m->msg_namelen = sizeof(struct sockaddr_tipc);
836 }
837}
838
839/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900840 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100841 * @m: descriptor for message info
842 * @msg: received message header
843 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900844 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100845 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900846 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100847 * Returns 0 if successful, otherwise errno
848 */
849
Sam Ravnborg05790c62006-03-20 22:37:04 -0800850static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100851 struct tipc_port *tport)
852{
853 u32 anc_data[3];
854 u32 err;
855 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700856 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100857 int res;
858
859 if (likely(m->msg_controllen == 0))
860 return 0;
861
862 /* Optionally capture errored message object(s) */
863
864 err = msg ? msg_errcode(msg) : 0;
865 if (unlikely(err)) {
866 anc_data[0] = err;
867 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000868 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
869 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000871 if (anc_data[1]) {
872 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
873 msg_data(msg));
874 if (res)
875 return res;
876 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100877 }
878
879 /* Optionally capture message destination object */
880
881 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
882 switch (dest_type) {
883 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700884 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100885 anc_data[0] = msg_nametype(msg);
886 anc_data[1] = msg_namelower(msg);
887 anc_data[2] = msg_namelower(msg);
888 break;
889 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700890 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 anc_data[0] = msg_nametype(msg);
892 anc_data[1] = msg_namelower(msg);
893 anc_data[2] = msg_nameupper(msg);
894 break;
895 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700896 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897 anc_data[0] = tport->conn_type;
898 anc_data[1] = tport->conn_instance;
899 anc_data[2] = tport->conn_instance;
900 break;
901 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700902 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903 }
Allan Stephens2db99832010-12-31 18:59:33 +0000904 if (has_name) {
905 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
906 if (res)
907 return res;
908 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100909
910 return 0;
911}
912
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900913/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914 * recv_msg - receive packet-oriented message
915 * @iocb: (unused)
916 * @m: descriptor for message info
917 * @buf_len: total size of user buffer area
918 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900919 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100920 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
921 * If the complete message doesn't fit in user area, truncate it.
922 *
923 * Returns size of returned message data, errno otherwise
924 */
925
926static int recv_msg(struct kiocb *iocb, struct socket *sock,
927 struct msghdr *m, size_t buf_len, int flags)
928{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700929 struct sock *sk = sock->sk;
930 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100931 struct sk_buff *buf;
932 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500933 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934 unsigned int sz;
935 u32 err;
936 int res;
937
Allan Stephens0c3141e2008-04-15 00:22:02 -0700938 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100939
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940 if (unlikely(!buf_len))
941 return -EINVAL;
942
Allan Stephens0c3141e2008-04-15 00:22:02 -0700943 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100944
Allan Stephens0c3141e2008-04-15 00:22:02 -0700945 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946 res = -ENOTCONN;
947 goto exit;
948 }
949
Allan Stephens71092ea2011-02-23 14:52:14 -0500950 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700951restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100952
Allan Stephens0c3141e2008-04-15 00:22:02 -0700953 /* Look for a message in receive queue; wait if necessary */
954
955 while (skb_queue_empty(&sk->sk_receive_queue)) {
956 if (sock->state == SS_DISCONNECTING) {
957 res = -ENOTCONN;
958 goto exit;
959 }
Allan Stephens71092ea2011-02-23 14:52:14 -0500960 if (timeout <= 0L) {
961 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700962 goto exit;
963 }
964 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -0500965 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
966 tipc_rx_ready(sock),
967 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700968 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700969 }
970
971 /* Look at first message in receive queue */
972
973 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974 msg = buf_msg(buf);
975 sz = msg_data_sz(msg);
976 err = msg_errcode(msg);
977
978 /* Complete connection setup for an implied connect */
979
980 if (unlikely(sock->state == SS_CONNECTING)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700981 res = auto_connect(sock, msg);
982 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100983 goto exit;
984 }
985
986 /* Discard an empty non-errored message & try again */
987
988 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700989 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100990 goto restart;
991 }
992
993 /* Capture sender's address (optional) */
994
995 set_orig_addr(m, msg);
996
997 /* Capture ancillary data (optional) */
998
Allan Stephens0c3141e2008-04-15 00:22:02 -0700999 res = anc_data_recv(m, msg, tport);
1000 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001001 goto exit;
1002
1003 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001004
Per Lidenb97bf3f2006-01-02 19:04:38 +01001005 if (!err) {
1006 if (unlikely(buf_len < sz)) {
1007 sz = buf_len;
1008 m->msg_flags |= MSG_TRUNC;
1009 }
Allan Stephens0232fd02011-02-21 09:45:40 -05001010 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1011 m->msg_iov, sz);
1012 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001013 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014 res = sz;
1015 } else {
1016 if ((sock->state == SS_READY) ||
1017 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1018 res = 0;
1019 else
1020 res = -ECONNRESET;
1021 }
1022
1023 /* Consume received message (optional) */
1024
1025 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001026 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -07001027 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1028 tipc_acknowledge(tport->ref, tport->conn_unacked);
1029 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001030 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001032 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001033 return res;
1034}
1035
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001036/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001037 * recv_stream - receive stream-oriented data
1038 * @iocb: (unused)
1039 * @m: descriptor for message info
1040 * @buf_len: total size of user buffer area
1041 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001042 *
1043 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001044 * will optionally wait for more; never truncates data.
1045 *
1046 * Returns size of returned message data, errno otherwise
1047 */
1048
1049static int recv_stream(struct kiocb *iocb, struct socket *sock,
1050 struct msghdr *m, size_t buf_len, int flags)
1051{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001052 struct sock *sk = sock->sk;
1053 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001054 struct sk_buff *buf;
1055 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -05001056 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001057 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001058 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001059 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001061 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062
1063 /* Catch invalid receive attempts */
1064
1065 if (unlikely(!buf_len))
1066 return -EINVAL;
1067
Allan Stephens0c3141e2008-04-15 00:22:02 -07001068 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001069
Allan Stephens0c3141e2008-04-15 00:22:02 -07001070 if (unlikely((sock->state == SS_UNCONNECTED) ||
1071 (sock->state == SS_CONNECTING))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001072 res = -ENOTCONN;
1073 goto exit;
1074 }
1075
Florian Westphal3720d402010-08-17 11:00:04 +00001076 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Allan Stephens71092ea2011-02-23 14:52:14 -05001077 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001078restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001079
Allan Stephens0c3141e2008-04-15 00:22:02 -07001080 /* Look for a message in receive queue; wait if necessary */
1081
1082 while (skb_queue_empty(&sk->sk_receive_queue)) {
1083 if (sock->state == SS_DISCONNECTING) {
1084 res = -ENOTCONN;
1085 goto exit;
1086 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001087 if (timeout <= 0L) {
1088 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001089 goto exit;
1090 }
1091 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001092 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1093 tipc_rx_ready(sock),
1094 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001095 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001096 }
1097
1098 /* Look at first message in receive queue */
1099
1100 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101 msg = buf_msg(buf);
1102 sz = msg_data_sz(msg);
1103 err = msg_errcode(msg);
1104
1105 /* Discard an empty non-errored message & try again */
1106
1107 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001108 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001109 goto restart;
1110 }
1111
1112 /* Optionally capture sender's address & ancillary data of first msg */
1113
1114 if (sz_copied == 0) {
1115 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001116 res = anc_data_recv(m, msg, tport);
1117 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118 goto exit;
1119 }
1120
1121 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001122
Per Lidenb97bf3f2006-01-02 19:04:38 +01001123 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001124 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001125
Allan Stephens0232fd02011-02-21 09:45:40 -05001126 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001127 needed = (buf_len - sz_copied);
1128 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001129
1130 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1131 m->msg_iov, sz_to_copy);
1132 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001133 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001134
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135 sz_copied += sz_to_copy;
1136
1137 if (sz_to_copy < sz) {
1138 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001139 TIPC_SKB_CB(buf)->handle =
1140 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 goto exit;
1142 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001143 } else {
1144 if (sz_copied != 0)
1145 goto exit; /* can't add error msg to valid data */
1146
1147 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1148 res = 0;
1149 else
1150 res = -ECONNRESET;
1151 }
1152
1153 /* Consume received message (optional) */
1154
1155 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001156 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1157 tipc_acknowledge(tport->ref, tport->conn_unacked);
1158 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001159 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160
1161 /* Loop around if more data is required */
1162
Joe Perchesf64f9e72009-11-29 16:55:45 -08001163 if ((sz_copied < buf_len) && /* didn't get all requested data */
1164 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001165 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001166 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1167 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001168 goto restart;
1169
1170exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001171 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001172 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173}
1174
1175/**
Allan Stephens1819b832008-04-15 00:15:50 -07001176 * rx_queue_full - determine if receive queue can accept another message
1177 * @msg: message to be added to queue
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178 * @queue_size: current size of queue
1179 * @base: nominal maximum size of queue
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001180 *
Allan Stephens1819b832008-04-15 00:15:50 -07001181 * Returns 1 if queue is unable to accept message, 0 otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 */
1183
Allan Stephens1819b832008-04-15 00:15:50 -07001184static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001185{
1186 u32 threshold;
1187 u32 imp = msg_importance(msg);
1188
1189 if (imp == TIPC_LOW_IMPORTANCE)
1190 threshold = base;
1191 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1192 threshold = base * 2;
1193 else if (imp == TIPC_HIGH_IMPORTANCE)
1194 threshold = base * 100;
1195 else
1196 return 0;
1197
1198 if (msg_connected(msg))
1199 threshold *= 4;
1200
Eric Dumazeta02cec22010-09-22 20:43:57 +00001201 return queue_size >= threshold;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202}
1203
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001204/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001205 * filter_rcv - validate incoming message
1206 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001208 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001209 * Enqueues message on receive queue if acceptable; optionally handles
1210 * disconnect indication for a connected socket.
1211 *
1212 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001213 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1215 */
1216
Allan Stephens0c3141e2008-04-15 00:22:02 -07001217static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001219 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 struct tipc_msg *msg = buf_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001221 u32 recv_q_len;
1222
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223 /* Reject message if it is wrong sort of message for socket */
1224
1225 /*
1226 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1227 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1228 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1229 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001230
Per Lidenb97bf3f2006-01-02 19:04:38 +01001231 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001232 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001233 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 } else {
Allan Stephensb29f1422010-12-31 18:59:25 +00001235 if (msg_mcast(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 if (sock->state == SS_CONNECTED) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001238 if (!msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001239 return TIPC_ERR_NO_PORT;
Allan Stephensb29f1422010-12-31 18:59:25 +00001240 } else if (sock->state == SS_CONNECTING) {
1241 if (!msg_connected(msg) && (msg_errcode(msg) == 0))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001242 return TIPC_ERR_NO_PORT;
Allan Stephensb29f1422010-12-31 18:59:25 +00001243 } else if (sock->state == SS_LISTENING) {
1244 if (msg_connected(msg) || msg_errcode(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001245 return TIPC_ERR_NO_PORT;
Allan Stephensb29f1422010-12-31 18:59:25 +00001246 } else if (sock->state == SS_DISCONNECTING) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001247 return TIPC_ERR_NO_PORT;
Allan Stephensb29f1422010-12-31 18:59:25 +00001248 } else /* (sock->state == SS_UNCONNECTED) */ {
1249 if (msg_connected(msg) || msg_errcode(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001250 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001251 }
1252 }
1253
1254 /* Reject message if there isn't room to queue it */
1255
Allan Stephens1819b832008-04-15 00:15:50 -07001256 recv_q_len = (u32)atomic_read(&tipc_queue_size);
1257 if (unlikely(recv_q_len >= OVERLOAD_LIMIT_BASE)) {
1258 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001260 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001261 recv_q_len = skb_queue_len(&sk->sk_receive_queue);
Allan Stephens1819b832008-04-15 00:15:50 -07001262 if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
1263 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001265 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001266
Allan Stephens0c3141e2008-04-15 00:22:02 -07001267 /* Enqueue message (finally!) */
1268
Allan Stephens0232fd02011-02-21 09:45:40 -05001269 TIPC_SKB_CB(buf)->handle = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001270 atomic_inc(&tipc_queue_size);
1271 __skb_queue_tail(&sk->sk_receive_queue, buf);
1272
Per Lidenb97bf3f2006-01-02 19:04:38 +01001273 /* Initiate connection termination for an incoming 'FIN' */
1274
1275 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1276 sock->state = SS_DISCONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001277 tipc_disconnect_port(tipc_sk_port(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278 }
1279
Eric Dumazetaa395142010-04-20 13:03:51 +00001280 if (waitqueue_active(sk_sleep(sk)))
1281 wake_up_interruptible(sk_sleep(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001282 return TIPC_OK;
1283}
1284
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001285/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001286 * backlog_rcv - handle incoming message from backlog queue
1287 * @sk: socket
1288 * @buf: message
1289 *
1290 * Caller must hold socket lock, but not port lock.
1291 *
1292 * Returns 0
1293 */
1294
1295static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1296{
1297 u32 res;
1298
1299 res = filter_rcv(sk, buf);
1300 if (res)
1301 tipc_reject_msg(buf, res);
1302 return 0;
1303}
1304
1305/**
1306 * dispatch - handle incoming message
1307 * @tport: TIPC port that received message
1308 * @buf: message
1309 *
1310 * Called with port lock already taken.
1311 *
1312 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1313 */
1314
1315static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1316{
1317 struct sock *sk = (struct sock *)tport->usr_handle;
1318 u32 res;
1319
1320 /*
1321 * Process message if socket is unlocked; otherwise add to backlog queue
1322 *
1323 * This code is based on sk_receive_skb(), but must be distinct from it
1324 * since a TIPC-specific filter/reject mechanism is utilized
1325 */
1326
1327 bh_lock_sock(sk);
1328 if (!sock_owned_by_user(sk)) {
1329 res = filter_rcv(sk, buf);
1330 } else {
Zhu Yia3a858f2010-03-04 18:01:47 +00001331 if (sk_add_backlog(sk, buf))
Zhu Yi53eecb12010-03-04 18:01:45 +00001332 res = TIPC_ERR_OVERLOAD;
1333 else
1334 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001335 }
1336 bh_unlock_sock(sk);
1337
1338 return res;
1339}
1340
1341/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342 * wakeupdispatch - wake up port after congestion
1343 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001344 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001345 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346 */
1347
1348static void wakeupdispatch(struct tipc_port *tport)
1349{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001350 struct sock *sk = (struct sock *)tport->usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351
Eric Dumazetaa395142010-04-20 13:03:51 +00001352 if (waitqueue_active(sk_sleep(sk)))
1353 wake_up_interruptible(sk_sleep(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001354}
1355
1356/**
1357 * connect - establish a connection to another TIPC port
1358 * @sock: socket structure
1359 * @dest: socket address for destination port
1360 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001361 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362 *
1363 * Returns 0 on success, errno otherwise
1364 */
1365
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001366static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 int flags)
1368{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001369 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001370 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1371 struct msghdr m = {NULL,};
1372 struct sk_buff *buf;
1373 struct tipc_msg *msg;
Allan Stephensa0f40f02011-05-26 13:44:34 -04001374 unsigned int timeout;
Allan Stephensb89741a2008-04-15 00:20:37 -07001375 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376
Allan Stephens0c3141e2008-04-15 00:22:02 -07001377 lock_sock(sk);
1378
Allan Stephensb89741a2008-04-15 00:20:37 -07001379 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380
Allan Stephens0c3141e2008-04-15 00:22:02 -07001381 if (sock->state == SS_READY) {
1382 res = -EOPNOTSUPP;
1383 goto exit;
1384 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385
Allan Stephensb89741a2008-04-15 00:20:37 -07001386 /* For now, TIPC does not support the non-blocking form of connect() */
Allan Stephens4934c692008-04-15 00:16:19 -07001387
Allan Stephens0c3141e2008-04-15 00:22:02 -07001388 if (flags & O_NONBLOCK) {
Allan Stephens35997e32010-08-17 11:00:05 +00001389 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001390 goto exit;
1391 }
Allan Stephens4934c692008-04-15 00:16:19 -07001392
Allan Stephensb89741a2008-04-15 00:20:37 -07001393 /* Issue Posix-compliant error code if socket is in the wrong state */
Allan Stephens51f9cc12006-06-25 23:49:06 -07001394
Allan Stephens0c3141e2008-04-15 00:22:02 -07001395 if (sock->state == SS_LISTENING) {
1396 res = -EOPNOTSUPP;
1397 goto exit;
1398 }
1399 if (sock->state == SS_CONNECTING) {
1400 res = -EALREADY;
1401 goto exit;
1402 }
1403 if (sock->state != SS_UNCONNECTED) {
1404 res = -EISCONN;
1405 goto exit;
1406 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001407
Allan Stephensb89741a2008-04-15 00:20:37 -07001408 /*
1409 * Reject connection attempt using multicast address
1410 *
1411 * Note: send_msg() validates the rest of the address fields,
1412 * so there's no need to do it here
1413 */
Allan Stephens51f9cc12006-06-25 23:49:06 -07001414
Allan Stephens0c3141e2008-04-15 00:22:02 -07001415 if (dst->addrtype == TIPC_ADDR_MCAST) {
1416 res = -EINVAL;
1417 goto exit;
1418 }
1419
1420 /* Reject any messages already in receive queue (very unlikely) */
1421
1422 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001423
Allan Stephensb89741a2008-04-15 00:20:37 -07001424 /* Send a 'SYN-' to destination */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001425
Allan Stephensb89741a2008-04-15 00:20:37 -07001426 m.msg_name = dest;
1427 m.msg_namelen = destlen;
1428 res = send_msg(NULL, sock, &m, 0);
Allan Stephensa0168922010-12-31 18:59:35 +00001429 if (res < 0)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001430 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001431
Allan Stephens0c3141e2008-04-15 00:22:02 -07001432 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001433
Allan Stephens564e83b2010-08-17 11:00:15 +00001434 timeout = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001435 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001436 res = wait_event_interruptible_timeout(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001437 (!skb_queue_empty(&sk->sk_receive_queue) ||
1438 (sock->state != SS_CONNECTING)),
Allan Stephensa0f40f02011-05-26 13:44:34 -04001439 timeout ? (long)msecs_to_jiffies(timeout)
1440 : MAX_SCHEDULE_TIMEOUT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001441 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001442
Allan Stephensb89741a2008-04-15 00:20:37 -07001443 if (res > 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001444 buf = skb_peek(&sk->sk_receive_queue);
1445 if (buf != NULL) {
1446 msg = buf_msg(buf);
1447 res = auto_connect(sock, msg);
1448 if (!res) {
1449 if (!msg_data_sz(msg))
1450 advance_rx_queue(sk);
1451 }
1452 } else {
Allan Stephensa0168922010-12-31 18:59:35 +00001453 if (sock->state == SS_CONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001454 res = -EISCONN;
Allan Stephensa0168922010-12-31 18:59:35 +00001455 else
Allan Stephens0c3141e2008-04-15 00:22:02 -07001456 res = -ECONNREFUSED;
Allan Stephensb89741a2008-04-15 00:20:37 -07001457 }
1458 } else {
1459 if (res == 0)
1460 res = -ETIMEDOUT;
1461 else
1462 ; /* leave "res" unchanged */
1463 sock->state = SS_DISCONNECTING;
1464 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001465
Allan Stephens0c3141e2008-04-15 00:22:02 -07001466exit:
1467 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001468 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001469}
1470
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001471/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001472 * listen - allow socket to listen for incoming connections
1473 * @sock: socket structure
1474 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001475 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001476 * Returns 0 on success, errno otherwise
1477 */
1478
1479static int listen(struct socket *sock, int len)
1480{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001481 struct sock *sk = sock->sk;
1482 int res;
1483
1484 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001485
1486 if (sock->state == SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001487 res = -EOPNOTSUPP;
1488 else if (sock->state != SS_UNCONNECTED)
1489 res = -EINVAL;
1490 else {
1491 sock->state = SS_LISTENING;
1492 res = 0;
1493 }
1494
1495 release_sock(sk);
1496 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001497}
1498
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001499/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001500 * accept - wait for connection request
1501 * @sock: listening socket
1502 * @newsock: new socket that is to be connected
1503 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001504 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001505 * Returns 0 on success, errno otherwise
1506 */
1507
Allan Stephens0c3141e2008-04-15 00:22:02 -07001508static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001509{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001510 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001512 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001513
Allan Stephens0c3141e2008-04-15 00:22:02 -07001514 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001515
Allan Stephens0c3141e2008-04-15 00:22:02 -07001516 if (sock->state == SS_READY) {
1517 res = -EOPNOTSUPP;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001518 goto exit;
1519 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001520 if (sock->state != SS_LISTENING) {
1521 res = -EINVAL;
1522 goto exit;
1523 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001524
Allan Stephens0c3141e2008-04-15 00:22:02 -07001525 while (skb_queue_empty(&sk->sk_receive_queue)) {
1526 if (flags & O_NONBLOCK) {
1527 res = -EWOULDBLOCK;
1528 goto exit;
1529 }
1530 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001531 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001532 (!skb_queue_empty(&sk->sk_receive_queue)));
1533 lock_sock(sk);
1534 if (res)
1535 goto exit;
1536 }
1537
1538 buf = skb_peek(&sk->sk_receive_queue);
1539
Eric Paris3f378b62009-11-05 22:18:14 -08001540 res = tipc_create(sock_net(sock->sk), new_sock, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001541 if (!res) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001542 struct sock *new_sk = new_sock->sk;
Allan Stephens2da59912008-07-14 22:43:32 -07001543 struct tipc_sock *new_tsock = tipc_sk(new_sk);
1544 struct tipc_port *new_tport = new_tsock->p;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001545 u32 new_ref = new_tport->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001546 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001547
1548 lock_sock(new_sk);
1549
1550 /*
1551 * Reject any stray messages received by new socket
1552 * before the socket lock was taken (very, very unlikely)
1553 */
1554
1555 reject_rx_queue(new_sk);
1556
1557 /* Connect new socket to it's peer */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001558
Allan Stephens2da59912008-07-14 22:43:32 -07001559 new_tsock->peer_name.ref = msg_origport(msg);
1560 new_tsock->peer_name.node = msg_orignode(msg);
1561 tipc_connect2port(new_ref, &new_tsock->peer_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001562 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001563
1564 tipc_set_portimportance(new_ref, msg_importance(msg));
1565 if (msg_named(msg)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001566 new_tport->conn_type = msg_nametype(msg);
1567 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001568 }
1569
Allan Stephens0c3141e2008-04-15 00:22:02 -07001570 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001571 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1572 * Respond to 'SYN+' by queuing it on new socket.
1573 */
1574
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001575 if (!msg_data_sz(msg)) {
1576 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001577
Allan Stephens0c3141e2008-04-15 00:22:02 -07001578 advance_rx_queue(sk);
1579 send_packet(NULL, new_sock, &m, 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001580 } else {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001581 __skb_dequeue(&sk->sk_receive_queue);
1582 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001583 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001584 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001585 }
1586exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001587 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001588 return res;
1589}
1590
1591/**
1592 * shutdown - shutdown socket connection
1593 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001594 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001595 *
1596 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001597 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001598 * Returns 0 on success, errno otherwise
1599 */
1600
1601static int shutdown(struct socket *sock, int how)
1602{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001603 struct sock *sk = sock->sk;
1604 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001605 struct sk_buff *buf;
1606 int res;
1607
Allan Stephense247a8f2008-03-06 15:05:38 -08001608 if (how != SHUT_RDWR)
1609 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001610
Allan Stephens0c3141e2008-04-15 00:22:02 -07001611 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001612
1613 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001614 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001615 case SS_CONNECTED:
1616
Allan Stephens0c3141e2008-04-15 00:22:02 -07001617 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001618restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001619 buf = __skb_dequeue(&sk->sk_receive_queue);
1620 if (buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001621 atomic_dec(&tipc_queue_size);
Allan Stephens0232fd02011-02-21 09:45:40 -05001622 if (TIPC_SKB_CB(buf)->handle != 0) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001623 buf_discard(buf);
1624 goto restart;
1625 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001626 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001627 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001628 } else {
1629 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001631
1632 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001633
1634 /* fall through */
1635
1636 case SS_DISCONNECTING:
1637
Allan Stephens0c3141e2008-04-15 00:22:02 -07001638 /* Discard any unreceived messages; wake up sleeping tasks */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001639
Allan Stephens0c3141e2008-04-15 00:22:02 -07001640 discard_rx_queue(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001641 if (waitqueue_active(sk_sleep(sk)))
1642 wake_up_interruptible(sk_sleep(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001643 res = 0;
1644 break;
1645
1646 default:
1647 res = -ENOTCONN;
1648 }
1649
Allan Stephens0c3141e2008-04-15 00:22:02 -07001650 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001651 return res;
1652}
1653
1654/**
1655 * setsockopt - set socket option
1656 * @sock: socket structure
1657 * @lvl: option level
1658 * @opt: option identifier
1659 * @ov: pointer to new option value
1660 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001661 *
1662 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001664 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665 * Returns 0 on success, errno otherwise
1666 */
1667
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001668static int setsockopt(struct socket *sock,
David S. Millerb7058842009-09-30 16:12:20 -07001669 int lvl, int opt, char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001671 struct sock *sk = sock->sk;
1672 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001673 u32 value;
1674 int res;
1675
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001676 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1677 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678 if (lvl != SOL_TIPC)
1679 return -ENOPROTOOPT;
1680 if (ol < sizeof(value))
1681 return -EINVAL;
Allan Stephens2db99832010-12-31 18:59:33 +00001682 res = get_user(value, (u32 __user *)ov);
1683 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001684 return res;
1685
Allan Stephens0c3141e2008-04-15 00:22:02 -07001686 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001687
Per Lidenb97bf3f2006-01-02 19:04:38 +01001688 switch (opt) {
1689 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001690 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001691 break;
1692 case TIPC_SRC_DROPPABLE:
1693 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001694 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001695 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696 res = -ENOPROTOOPT;
1697 break;
1698 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001699 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001700 break;
1701 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001702 tipc_sk(sk)->conn_timeout = value;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001703 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001704 break;
1705 default:
1706 res = -EINVAL;
1707 }
1708
Allan Stephens0c3141e2008-04-15 00:22:02 -07001709 release_sock(sk);
1710
Per Lidenb97bf3f2006-01-02 19:04:38 +01001711 return res;
1712}
1713
1714/**
1715 * getsockopt - get socket option
1716 * @sock: socket structure
1717 * @lvl: option level
1718 * @opt: option identifier
1719 * @ov: receptacle for option value
1720 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001721 *
1722 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001723 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001724 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001725 * Returns 0 on success, errno otherwise
1726 */
1727
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001728static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001729 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001730{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001731 struct sock *sk = sock->sk;
1732 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001733 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001734 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001735 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001736
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001737 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1738 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739 if (lvl != SOL_TIPC)
1740 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001741 res = get_user(len, ol);
1742 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001743 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001744
Allan Stephens0c3141e2008-04-15 00:22:02 -07001745 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746
1747 switch (opt) {
1748 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001749 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001750 break;
1751 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001752 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001753 break;
1754 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001755 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001756 break;
1757 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001758 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001759 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001760 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001761 case TIPC_NODE_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001762 value = (u32)atomic_read(&tipc_queue_size);
1763 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001764 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001765 value = skb_queue_len(&sk->sk_receive_queue);
1766 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001767 default:
1768 res = -EINVAL;
1769 }
1770
Allan Stephens0c3141e2008-04-15 00:22:02 -07001771 release_sock(sk);
1772
Paul Gortmaker25860c32010-12-31 18:59:31 +00001773 if (res)
1774 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001775
Paul Gortmaker25860c32010-12-31 18:59:31 +00001776 if (len < sizeof(value))
1777 return -EINVAL;
1778
1779 if (copy_to_user(ov, &value, sizeof(value)))
1780 return -EFAULT;
1781
1782 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001783}
1784
1785/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001786 * Protocol switches for the various types of TIPC sockets
1787 */
1788
Florian Westphalbca65ea2008-02-07 18:18:01 -08001789static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001790 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001791 .family = AF_TIPC,
1792 .release = release,
1793 .bind = bind,
1794 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001795 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001796 .accept = accept,
1797 .getname = get_name,
1798 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001799 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001800 .listen = listen,
1801 .shutdown = shutdown,
1802 .setsockopt = setsockopt,
1803 .getsockopt = getsockopt,
1804 .sendmsg = send_msg,
1805 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001806 .mmap = sock_no_mmap,
1807 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001808};
1809
Florian Westphalbca65ea2008-02-07 18:18:01 -08001810static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001811 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001812 .family = AF_TIPC,
1813 .release = release,
1814 .bind = bind,
1815 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001816 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001817 .accept = accept,
1818 .getname = get_name,
1819 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001820 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001821 .listen = listen,
1822 .shutdown = shutdown,
1823 .setsockopt = setsockopt,
1824 .getsockopt = getsockopt,
1825 .sendmsg = send_packet,
1826 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001827 .mmap = sock_no_mmap,
1828 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829};
1830
Florian Westphalbca65ea2008-02-07 18:18:01 -08001831static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001832 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001833 .family = AF_TIPC,
1834 .release = release,
1835 .bind = bind,
1836 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001837 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001838 .accept = accept,
1839 .getname = get_name,
1840 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001841 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001842 .listen = listen,
1843 .shutdown = shutdown,
1844 .setsockopt = setsockopt,
1845 .getsockopt = getsockopt,
1846 .sendmsg = send_stream,
1847 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001848 .mmap = sock_no_mmap,
1849 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001850};
1851
Florian Westphalbca65ea2008-02-07 18:18:01 -08001852static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001853 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001854 .family = AF_TIPC,
1855 .create = tipc_create
1856};
1857
1858static struct proto tipc_proto = {
1859 .name = "TIPC",
1860 .owner = THIS_MODULE,
1861 .obj_size = sizeof(struct tipc_sock)
1862};
1863
1864/**
Per Liden4323add2006-01-18 00:38:21 +01001865 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001866 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001867 * Returns 0 on success, errno otherwise
1868 */
Per Liden4323add2006-01-18 00:38:21 +01001869int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001870{
1871 int res;
1872
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001873 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001874 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001875 err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001876 goto out;
1877 }
1878
1879 res = sock_register(&tipc_family_ops);
1880 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001881 err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001882 proto_unregister(&tipc_proto);
1883 goto out;
1884 }
1885
1886 sockets_enabled = 1;
1887 out:
1888 return res;
1889}
1890
1891/**
Per Liden4323add2006-01-18 00:38:21 +01001892 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001893 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001894
Per Liden4323add2006-01-18 00:38:21 +01001895void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001896{
1897 if (!sockets_enabled)
1898 return;
1899
1900 sockets_enabled = 0;
1901 sock_unregister(tipc_family_ops.family);
1902 proto_unregister(&tipc_proto);
1903}
1904