blob: 9b4e4833a484f959a891d5022ff468c671f38239 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Maloye643df12012-11-27 06:15:29 -05004 * Copyright (c) 2001-2007, 2012 Ericsson AB
Ying Xue9da3d472012-11-27 06:15:27 -05005 * Copyright (c) 2004-2008, 2010-2012, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Per Lidenb97bf3f2006-01-02 19:04:38 +010037#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000038#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039
Erik Hugne2cf8aa12012-06-29 00:16:37 -040040#include <linux/export.h>
41#include <net/sock.h>
42
Per Lidenb97bf3f2006-01-02 19:04:38 +010043#define SS_LISTENING -1 /* socket is listening */
44#define SS_READY -2 /* socket is connectionless */
45
Jon Maloye643df12012-11-27 06:15:29 -050046#define OVERLOAD_LIMIT_BASE 10000
Allan Stephens3654ea02008-04-13 21:35:11 -070047#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010048
49struct tipc_sock {
50 struct sock sk;
51 struct tipc_port *p;
Allan Stephens2da59912008-07-14 22:43:32 -070052 struct tipc_portid peer_name;
Allan Stephensa0f40f02011-05-26 13:44:34 -040053 unsigned int conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +010054};
55
Allan Stephens0c3141e2008-04-15 00:22:02 -070056#define tipc_sk(sk) ((struct tipc_sock *)(sk))
Joe Perchese3192692012-06-03 17:41:40 +000057#define tipc_sk_port(sk) (tipc_sk(sk)->p)
Per Lidenb97bf3f2006-01-02 19:04:38 +010058
Allan Stephens71092ea2011-02-23 14:52:14 -050059#define tipc_rx_ready(sock) (!skb_queue_empty(&sock->sk->sk_receive_queue) || \
60 (sock->state == SS_DISCONNECTING))
61
Allan Stephens0c3141e2008-04-15 00:22:02 -070062static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +010063static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
64static void wakeupdispatch(struct tipc_port *tport);
Ying Xuef288bef2012-08-21 11:16:57 +080065static void tipc_data_ready(struct sock *sk, int len);
66static void tipc_write_space(struct sock *sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +010067
Florian Westphalbca65ea2008-02-07 18:18:01 -080068static const struct proto_ops packet_ops;
69static const struct proto_ops stream_ops;
70static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010071
72static struct proto tipc_proto;
73
Allan Stephense3ec9c72010-12-31 18:59:34 +000074static int sockets_enabled;
Per Lidenb97bf3f2006-01-02 19:04:38 +010075
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090076/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070077 * Revised TIPC socket locking policy:
78 *
79 * Most socket operations take the standard socket lock when they start
80 * and hold it until they finish (or until they need to sleep). Acquiring
81 * this lock grants the owner exclusive access to the fields of the socket
82 * data structures, with the exception of the backlog queue. A few socket
83 * operations can be done without taking the socket lock because they only
84 * read socket information that never changes during the life of the socket.
85 *
86 * Socket operations may acquire the lock for the associated TIPC port if they
87 * need to perform an operation on the port. If any routine needs to acquire
88 * both the socket lock and the port lock it must take the socket lock first
89 * to avoid the risk of deadlock.
90 *
91 * The dispatcher handling incoming messages cannot grab the socket lock in
92 * the standard fashion, since invoked it runs at the BH level and cannot block.
93 * Instead, it checks to see if the socket lock is currently owned by someone,
94 * and either handles the message itself or adds it to the socket's backlog
95 * queue; in the latter case the queued message is processed once the process
96 * owning the socket lock releases it.
97 *
98 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
99 * the problem of a blocked socket operation preventing any other operations
100 * from occurring. However, applications must be careful if they have
101 * multiple threads trying to send (or receive) on the same socket, as these
102 * operations might interfere with each other. For example, doing a connect
103 * and a receive at the same time might allow the receive to consume the
104 * ACK message meant for the connect. While additional work could be done
105 * to try and overcome this, it doesn't seem to be worthwhile at the present.
106 *
107 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
108 * that another operation that must be performed in a non-blocking manner is
109 * not delayed for very long because the lock has already been taken.
110 *
111 * NOTE: This code assumes that certain fields of a port/socket pair are
112 * constant over its lifetime; such fields can be examined without taking
113 * the socket lock and/or port lock, and do not need to be re-read even
114 * after resuming processing after waiting. These fields include:
115 * - socket type
116 * - pointer to socket sk structure (aka tipc_sock structure)
117 * - pointer to port structure
118 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100119 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120
121/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700122 * advance_rx_queue - discard first buffer in socket receive queue
123 *
124 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700126static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400128 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129}
130
131/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700132 * discard_rx_queue - discard all buffers in socket receive queue
133 *
134 * Caller must hold socket lock
135 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700136static void discard_rx_queue(struct sock *sk)
137{
138 struct sk_buff *buf;
139
Ying Xue9da3d472012-11-27 06:15:27 -0500140 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens5f6d9122011-11-04 13:24:29 -0400141 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700142}
143
144/**
145 * reject_rx_queue - reject all buffers in socket receive queue
146 *
147 * Caller must hold socket lock
148 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700149static void reject_rx_queue(struct sock *sk)
150{
151 struct sk_buff *buf;
152
Ying Xue9da3d472012-11-27 06:15:27 -0500153 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700154 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700155}
156
157/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158 * tipc_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700159 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 * @sock: pre-allocated socket structure
161 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800162 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900163 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700164 * This routine creates additional data structures used by the TIPC socket,
165 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 *
167 * Returns 0 on success, errno otherwise
168 */
Eric Paris3f378b62009-11-05 22:18:14 -0800169static int tipc_create(struct net *net, struct socket *sock, int protocol,
170 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700172 const struct proto_ops *ops;
173 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700175 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700176
177 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 if (unlikely(protocol != 0))
179 return -EPROTONOSUPPORT;
180
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 switch (sock->type) {
182 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700183 ops = &stream_ops;
184 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185 break;
186 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700187 ops = &packet_ops;
188 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100189 break;
190 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700192 ops = &msg_ops;
193 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194 break;
Allan Stephens49978652006-06-25 23:47:18 -0700195 default:
Allan Stephens49978652006-06-25 23:47:18 -0700196 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197 }
198
Allan Stephens0c3141e2008-04-15 00:22:02 -0700199 /* Allocate socket's protocol area */
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700200 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700201 if (sk == NULL)
202 return -ENOMEM;
203
204 /* Allocate TIPC port for socket to use */
Allan Stephens0ea52242008-07-14 22:42:19 -0700205 tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
206 TIPC_LOW_IMPORTANCE);
207 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700208 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100209 return -ENOMEM;
210 }
211
Allan Stephens0c3141e2008-04-15 00:22:02 -0700212 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700213 sock->ops = ops;
214 sock->state = state;
215
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 sock_init_data(sock, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700217 sk->sk_backlog_rcv = backlog_rcv;
Erik Hugnee57edf62012-10-04 05:00:43 +0000218 sk->sk_rcvbuf = TIPC_FLOW_CONTROL_WIN * 2 * TIPC_MAX_USER_MSG_SIZE * 2;
Ying Xuef288bef2012-08-21 11:16:57 +0800219 sk->sk_data_ready = tipc_data_ready;
220 sk->sk_write_space = tipc_write_space;
Allan Stephens0ea52242008-07-14 22:42:19 -0700221 tipc_sk(sk)->p = tp_ptr;
Allan Stephensa0f40f02011-05-26 13:44:34 -0400222 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700224 spin_unlock_bh(tp_ptr->lock);
225
Allan Stephens0c3141e2008-04-15 00:22:02 -0700226 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700227 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700228 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700229 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700230 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 return 0;
233}
234
235/**
236 * release - destroy a TIPC socket
237 * @sock: socket to destroy
238 *
239 * This routine cleans up any messages that are still queued on the socket.
240 * For DGRAM and RDM socket types, all queued messages are rejected.
241 * For SEQPACKET and STREAM socket types, the first message is rejected
242 * and any others are discarded. (If the first message on a STREAM socket
243 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900244 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245 * NOTE: Rejected messages are not necessarily returned to the sender! They
246 * are returned or discarded according to the "destination droppable" setting
247 * specified for the message by the sender.
248 *
249 * Returns 0 on success, errno otherwise
250 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251static int release(struct socket *sock)
252{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700254 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700256 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100257
Allan Stephens0c3141e2008-04-15 00:22:02 -0700258 /*
259 * Exit if socket isn't fully initialized (occurs when a failed accept()
260 * releases a pre-allocated child socket that was never used)
261 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700262 if (sk == NULL)
263 return 0;
264
265 tport = tipc_sk_port(sk);
266 lock_sock(sk);
267
268 /*
269 * Reject all unreceived messages, except on an active connection
270 * (which disconnects locally & sends a 'FIN+' to peer)
271 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700273 buf = __skb_dequeue(&sk->sk_receive_queue);
274 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275 break;
Allan Stephens0232fd02011-02-21 09:45:40 -0500276 if (TIPC_SKB_CB(buf)->handle != 0)
Allan Stephens5f6d9122011-11-04 13:24:29 -0400277 kfree_skb(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700278 else {
279 if ((sock->state == SS_CONNECTING) ||
280 (sock->state == SS_CONNECTED)) {
281 sock->state = SS_DISCONNECTING;
282 tipc_disconnect(tport->ref);
283 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100284 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700285 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286 }
287
Allan Stephens0c3141e2008-04-15 00:22:02 -0700288 /*
289 * Delete TIPC port; this ensures no more messages are queued
290 * (also disconnects an active connection & sends a 'FIN-' to peer)
291 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700292 res = tipc_deleteport(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293
Allan Stephens0c3141e2008-04-15 00:22:02 -0700294 /* Discard any remaining (connection-based) messages in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700295 discard_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296
Allan Stephens0c3141e2008-04-15 00:22:02 -0700297 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700298 sock->state = SS_DISCONNECTING;
299 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300
301 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700302 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 return res;
305}
306
307/**
308 * bind - associate or disassocate TIPC name(s) with a socket
309 * @sock: socket structure
310 * @uaddr: socket address describing name(s) and desired operation
311 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900312 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313 * Name and name sequence binding is indicated using a positive scope value;
314 * a negative scope value unbinds the specified name. Specifying no name
315 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900316 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700318 *
319 * NOTE: This routine doesn't need to take the socket lock since it doesn't
320 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
323{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700325 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326
Allan Stephens0c3141e2008-04-15 00:22:02 -0700327 if (unlikely(!uaddr_len))
328 return tipc_withdraw(portref, 0, NULL);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900329
Allan Stephens0c3141e2008-04-15 00:22:02 -0700330 if (uaddr_len < sizeof(struct sockaddr_tipc))
331 return -EINVAL;
332 if (addr->family != AF_TIPC)
333 return -EAFNOSUPPORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335 if (addr->addrtype == TIPC_ADDR_NAME)
336 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700337 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
338 return -EAFNOSUPPORT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900339
Allan Stephensc422f1b2011-11-02 15:49:40 -0400340 if (addr->addr.nameseq.type < TIPC_RESERVED_TYPES)
341 return -EACCES;
342
Allan Stephens0c3141e2008-04-15 00:22:02 -0700343 return (addr->scope > 0) ?
344 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
345 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346}
347
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900348/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 * get_name - get port ID of socket or peer socket
350 * @sock: socket structure
351 * @uaddr: area for returned socket address
352 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700353 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900354 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700356 *
Allan Stephens2da59912008-07-14 22:43:32 -0700357 * NOTE: This routine doesn't need to take the socket lock since it only
358 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000359 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900361static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362 int *uaddr_len, int peer)
363{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700365 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000367 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700368 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700369 if ((sock->state != SS_CONNECTED) &&
370 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
371 return -ENOTCONN;
372 addr->addr.id.ref = tsock->peer_name.ref;
373 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700374 } else {
Allan Stephensb924dcf2010-11-30 12:01:03 +0000375 addr->addr.id.ref = tsock->p->ref;
376 addr->addr.id.node = tipc_own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700377 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378
379 *uaddr_len = sizeof(*addr);
380 addr->addrtype = TIPC_ADDR_ID;
381 addr->family = AF_TIPC;
382 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 addr->addr.name.domain = 0;
384
Allan Stephens0c3141e2008-04-15 00:22:02 -0700385 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386}
387
388/**
389 * poll - read and possibly block on pollmask
390 * @file: file structure associated with the socket
391 * @sock: socket for which to calculate the poll bits
392 * @wait: ???
393 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700394 * Returns pollmask value
395 *
396 * COMMENTARY:
397 * It appears that the usual socket locking mechanisms are not useful here
398 * since the pollmask info is potentially out-of-date the moment this routine
399 * exits. TCP and other protocols seem to rely on higher level poll routines
400 * to handle any preventable race conditions, so TIPC will do the same ...
401 *
402 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700403 *
Allan Stephensf662c072010-08-17 11:00:06 +0000404 * socket state flags set
405 * ------------ ---------
406 * unconnected no read flags
Erik Hugnec4fc2982012-10-16 16:47:06 +0200407 * POLLOUT if port is not congested
Allan Stephensf662c072010-08-17 11:00:06 +0000408 *
409 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
410 * no write flags
411 *
412 * connected POLLIN/POLLRDNORM if data in rx queue
413 * POLLOUT if port is not congested
414 *
415 * disconnecting POLLIN/POLLRDNORM/POLLHUP
416 * no write flags
417 *
418 * listening POLLIN if SYN in rx queue
419 * no write flags
420 *
421 * ready POLLIN/POLLRDNORM if data in rx queue
422 * [connectionless] POLLOUT (since port cannot be congested)
423 *
424 * IMPORTANT: The fact that a read or write operation is indicated does NOT
425 * imply that the operation will succeed, merely that it should be performed
426 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900428static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100429 poll_table *wait)
430{
Allan Stephens9b674e82008-03-26 16:48:21 -0700431 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000432 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700433
Ying Xuef288bef2012-08-21 11:16:57 +0800434 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700435
Allan Stephensf662c072010-08-17 11:00:06 +0000436 switch ((int)sock->state) {
Erik Hugnec4fc2982012-10-16 16:47:06 +0200437 case SS_UNCONNECTED:
438 if (!tipc_sk_port(sk)->congested)
439 mask |= POLLOUT;
440 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000441 case SS_READY:
442 case SS_CONNECTED:
443 if (!tipc_sk_port(sk)->congested)
444 mask |= POLLOUT;
445 /* fall thru' */
446 case SS_CONNECTING:
447 case SS_LISTENING:
448 if (!skb_queue_empty(&sk->sk_receive_queue))
449 mask |= (POLLIN | POLLRDNORM);
450 break;
451 case SS_DISCONNECTING:
452 mask = (POLLIN | POLLRDNORM | POLLHUP);
453 break;
454 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700455
456 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457}
458
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900459/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 * dest_name_check - verify user is permitted to send to specified port name
461 * @dest: destination address
462 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900463 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 * Prevents restricted configuration commands from being issued by
465 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900466 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467 * Returns 0 if permission is granted, otherwise errno
468 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800469static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470{
471 struct tipc_cfg_msg_hdr hdr;
472
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900473 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
474 return 0;
475 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
476 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900477 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
478 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479
Allan Stephens3f8dd942011-01-18 13:09:29 -0500480 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
481 return -EMSGSIZE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900482 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700484 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900486
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 return 0;
488}
489
490/**
491 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700492 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493 * @sock: socket structure
494 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700495 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900496 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900498 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
500 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900501 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502 * Returns the number of bytes sent on success, or errno otherwise
503 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100504static int send_msg(struct kiocb *iocb, struct socket *sock,
505 struct msghdr *m, size_t total_len)
506{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700507 struct sock *sk = sock->sk;
508 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900509 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100510 int needs_conn;
Ying Xue1d835872011-07-06 05:53:15 -0400511 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100512 int res = -EINVAL;
513
514 if (unlikely(!dest))
515 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700516 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
517 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518 return -EINVAL;
Allan Stephensc29c3f72010-04-20 17:58:24 -0400519 if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
Eric Dumazet95c96172012-04-15 05:58:06 +0000520 (m->msg_iovlen > (unsigned int)INT_MAX))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400521 return -EMSGSIZE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522
Allan Stephens0c3141e2008-04-15 00:22:02 -0700523 if (iocb)
524 lock_sock(sk);
525
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526 needs_conn = (sock->state != SS_READY);
527 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700528 if (sock->state == SS_LISTENING) {
529 res = -EPIPE;
530 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700531 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700532 if (sock->state != SS_UNCONNECTED) {
533 res = -EISCONN;
534 goto exit;
535 }
536 if ((tport->published) ||
537 ((sock->type == SOCK_STREAM) && (total_len != 0))) {
538 res = -EOPNOTSUPP;
539 goto exit;
540 }
541 if (dest->addrtype == TIPC_ADDR_NAME) {
542 tport->conn_type = dest->addr.name.name.type;
543 tport->conn_instance = dest->addr.name.name.instance;
544 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545
546 /* Abort any pending connection attempts (very unlikely) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700547 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548 }
549
Ying Xue1d835872011-07-06 05:53:15 -0400550 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
551
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900552 do {
553 if (dest->addrtype == TIPC_ADDR_NAME) {
Allan Stephens2db99832010-12-31 18:59:33 +0000554 res = dest_name_check(dest, m);
555 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700556 break;
557 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900558 &dest->addr.name.name,
559 dest->addr.name.domain,
560 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500561 m->msg_iov,
562 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000563 } else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700564 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900565 &dest->addr.id,
566 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500567 m->msg_iov,
568 total_len);
Allan Stephens0e659672010-12-31 18:59:32 +0000569 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570 if (needs_conn) {
571 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700572 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573 }
Allan Stephens2db99832010-12-31 18:59:33 +0000574 res = dest_name_check(dest, m);
575 if (res)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700576 break;
577 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900578 &dest->addr.nameseq,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900579 m->msg_iovlen,
Allan Stephens26896902011-04-21 10:42:07 -0500580 m->msg_iov,
581 total_len);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900582 }
583 if (likely(res != -ELINKCONG)) {
Allan Stephensa0168922010-12-31 18:59:35 +0000584 if (needs_conn && (res >= 0))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700585 sock->state = SS_CONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700586 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900587 }
Ying Xue1d835872011-07-06 05:53:15 -0400588 if (timeout_val <= 0L) {
589 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700590 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700592 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400593 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
594 !tport->congested, timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700595 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900596 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700597
598exit:
599 if (iocb)
600 release_sock(sk);
601 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602}
603
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900604/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700606 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100607 * @sock: socket structure
608 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700609 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900610 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900612 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 * Returns the number of bytes sent on success, or errno otherwise
614 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615static int send_packet(struct kiocb *iocb, struct socket *sock,
616 struct msghdr *m, size_t total_len)
617{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700618 struct sock *sk = sock->sk;
619 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900620 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Ying Xue1d835872011-07-06 05:53:15 -0400621 long timeout_val;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100622 int res;
623
624 /* Handle implied connection establishment */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 if (unlikely(dest))
626 return send_msg(iocb, sock, m, total_len);
627
Allan Stephensc29c3f72010-04-20 17:58:24 -0400628 if ((total_len > TIPC_MAX_USER_MSG_SIZE) ||
Eric Dumazet95c96172012-04-15 05:58:06 +0000629 (m->msg_iovlen > (unsigned int)INT_MAX))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400630 return -EMSGSIZE;
631
Allan Stephens0c3141e2008-04-15 00:22:02 -0700632 if (iocb)
633 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634
Ying Xue1d835872011-07-06 05:53:15 -0400635 timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
636
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900637 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700638 if (unlikely(sock->state != SS_CONNECTED)) {
639 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900640 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700641 else
642 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700643 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700644 }
645
Allan Stephens26896902011-04-21 10:42:07 -0500646 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov,
647 total_len);
Allan Stephensa0168922010-12-31 18:59:35 +0000648 if (likely(res != -ELINKCONG))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700649 break;
Ying Xue1d835872011-07-06 05:53:15 -0400650 if (timeout_val <= 0L) {
651 res = timeout_val ? timeout_val : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700652 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700654 release_sock(sk);
Ying Xue1d835872011-07-06 05:53:15 -0400655 timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk),
656 (!tport->congested || !tport->connected), timeout_val);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700657 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900658 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700659
660 if (iocb)
661 release_sock(sk);
662 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663}
664
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900665/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 * send_stream - send stream-oriented data
667 * @iocb: (unused)
668 * @sock: socket structure
669 * @m: data to send
670 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900671 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900673 *
674 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700675 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100677static int send_stream(struct kiocb *iocb, struct socket *sock,
678 struct msghdr *m, size_t total_len)
679{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700680 struct sock *sk = sock->sk;
681 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100682 struct msghdr my_msg;
683 struct iovec my_iov;
684 struct iovec *curr_iov;
685 int curr_iovlen;
686 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700687 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100688 int curr_left;
689 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700690 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100691 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900692
Allan Stephens0c3141e2008-04-15 00:22:02 -0700693 lock_sock(sk);
694
Allan Stephens05646c92007-06-10 17:25:24 -0700695 /* Handle special cases where there is no connection */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900696 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700697 if (sock->state == SS_UNCONNECTED) {
698 res = send_packet(NULL, sock, m, total_len);
699 goto exit;
700 } else if (sock->state == SS_DISCONNECTING) {
701 res = -EPIPE;
702 goto exit;
703 } else {
704 res = -ENOTCONN;
705 goto exit;
706 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900707 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708
Allan Stephens0c3141e2008-04-15 00:22:02 -0700709 if (unlikely(m->msg_name)) {
710 res = -EISCONN;
711 goto exit;
712 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700713
Eric Dumazet95c96172012-04-15 05:58:06 +0000714 if ((total_len > (unsigned int)INT_MAX) ||
715 (m->msg_iovlen > (unsigned int)INT_MAX)) {
Allan Stephensc29c3f72010-04-20 17:58:24 -0400716 res = -EMSGSIZE;
717 goto exit;
718 }
719
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900720 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100721 * Send each iovec entry using one or more messages
722 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900723 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724 * (i.e. one large iovec entry), but could be improved to pass sets
725 * of small iovec entries into send_packet().
726 */
Allan Stephens1303e8f2006-06-25 23:46:50 -0700727 curr_iov = m->msg_iov;
728 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729 my_msg.msg_iov = &my_iov;
730 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700731 my_msg.msg_flags = m->msg_flags;
732 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700733 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734
Allan Stephens05646c92007-06-10 17:25:24 -0700735 hdr_size = msg_hdr_sz(&tport->phdr);
736
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737 while (curr_iovlen--) {
738 curr_start = curr_iov->iov_base;
739 curr_left = curr_iov->iov_len;
740
741 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700742 bytes_to_send = tport->max_pkt - hdr_size;
743 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
744 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
745 if (curr_left < bytes_to_send)
746 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747 my_iov.iov_base = curr_start;
748 my_iov.iov_len = bytes_to_send;
Allan Stephens26896902011-04-21 10:42:07 -0500749 res = send_packet(NULL, sock, &my_msg, bytes_to_send);
Allan Stephens2db99832010-12-31 18:59:33 +0000750 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700751 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700752 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700753 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700754 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 curr_left -= bytes_to_send;
756 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700757 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100758 }
759
760 curr_iov++;
761 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700762 res = bytes_sent;
763exit:
764 release_sock(sk);
765 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766}
767
768/**
769 * auto_connect - complete connection setup to a remote port
770 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900772 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 * Returns 0 on success, errno otherwise
774 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700775static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776{
Allan Stephens2da59912008-07-14 22:43:32 -0700777 struct tipc_sock *tsock = tipc_sk(sock->sk);
Ying Xue584d24b2012-11-29 18:51:19 -0500778 struct tipc_port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100779
Allan Stephens2da59912008-07-14 22:43:32 -0700780 tsock->peer_name.ref = msg_origport(msg);
781 tsock->peer_name.node = msg_orignode(msg);
Ying Xue584d24b2012-11-29 18:51:19 -0500782 p_ptr = tipc_port_deref(tsock->p->ref);
783 if (!p_ptr)
784 return -EINVAL;
785
786 __tipc_connect(tsock->p->ref, p_ptr, &tsock->peer_name);
787
788 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
789 return -EINVAL;
790 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791 sock->state = SS_CONNECTED;
792 return 0;
793}
794
795/**
796 * set_orig_addr - capture sender's address for received message
797 * @m: descriptor for message info
798 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900799 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100800 * Note: Address is not captured if not requested by receiver.
801 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800802static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100803{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900804 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900806 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100807 addr->family = AF_TIPC;
808 addr->addrtype = TIPC_ADDR_ID;
809 addr->addr.id.ref = msg_origport(msg);
810 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +0000811 addr->addr.name.domain = 0; /* could leave uninitialized */
812 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813 m->msg_namelen = sizeof(struct sockaddr_tipc);
814 }
815}
816
817/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900818 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100819 * @m: descriptor for message info
820 * @msg: received message header
821 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900822 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100823 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900824 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825 * Returns 0 if successful, otherwise errno
826 */
Sam Ravnborg05790c62006-03-20 22:37:04 -0800827static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828 struct tipc_port *tport)
829{
830 u32 anc_data[3];
831 u32 err;
832 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700833 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834 int res;
835
836 if (likely(m->msg_controllen == 0))
837 return 0;
838
839 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840 err = msg ? msg_errcode(msg) : 0;
841 if (unlikely(err)) {
842 anc_data[0] = err;
843 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +0000844 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
845 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100846 return res;
Allan Stephens2db99832010-12-31 18:59:33 +0000847 if (anc_data[1]) {
848 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
849 msg_data(msg));
850 if (res)
851 return res;
852 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100853 }
854
855 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100856 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
857 switch (dest_type) {
858 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700859 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100860 anc_data[0] = msg_nametype(msg);
861 anc_data[1] = msg_namelower(msg);
862 anc_data[2] = msg_namelower(msg);
863 break;
864 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700865 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100866 anc_data[0] = msg_nametype(msg);
867 anc_data[1] = msg_namelower(msg);
868 anc_data[2] = msg_nameupper(msg);
869 break;
870 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700871 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100872 anc_data[0] = tport->conn_type;
873 anc_data[1] = tport->conn_instance;
874 anc_data[2] = tport->conn_instance;
875 break;
876 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700877 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100878 }
Allan Stephens2db99832010-12-31 18:59:33 +0000879 if (has_name) {
880 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
881 if (res)
882 return res;
883 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100884
885 return 0;
886}
887
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900888/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889 * recv_msg - receive packet-oriented message
890 * @iocb: (unused)
891 * @m: descriptor for message info
892 * @buf_len: total size of user buffer area
893 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900894 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100895 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
896 * If the complete message doesn't fit in user area, truncate it.
897 *
898 * Returns size of returned message data, errno otherwise
899 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900static int recv_msg(struct kiocb *iocb, struct socket *sock,
901 struct msghdr *m, size_t buf_len, int flags)
902{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700903 struct sock *sk = sock->sk;
904 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905 struct sk_buff *buf;
906 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -0500907 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908 unsigned int sz;
909 u32 err;
910 int res;
911
Allan Stephens0c3141e2008-04-15 00:22:02 -0700912 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100913 if (unlikely(!buf_len))
914 return -EINVAL;
915
Allan Stephens0c3141e2008-04-15 00:22:02 -0700916 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100917
Allan Stephens0c3141e2008-04-15 00:22:02 -0700918 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100919 res = -ENOTCONN;
920 goto exit;
921 }
922
Allan Stephens71092ea2011-02-23 14:52:14 -0500923 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700924restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100925
Allan Stephens0c3141e2008-04-15 00:22:02 -0700926 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700927 while (skb_queue_empty(&sk->sk_receive_queue)) {
928 if (sock->state == SS_DISCONNECTING) {
929 res = -ENOTCONN;
930 goto exit;
931 }
Allan Stephens71092ea2011-02-23 14:52:14 -0500932 if (timeout <= 0L) {
933 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700934 goto exit;
935 }
936 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -0500937 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
938 tipc_rx_ready(sock),
939 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700940 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700941 }
942
943 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700944 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100945 msg = buf_msg(buf);
946 sz = msg_data_sz(msg);
947 err = msg_errcode(msg);
948
Per Lidenb97bf3f2006-01-02 19:04:38 +0100949 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100950 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700951 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100952 goto restart;
953 }
954
955 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100956 set_orig_addr(m, msg);
957
958 /* Capture ancillary data (optional) */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700959 res = anc_data_recv(m, msg, tport);
960 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100961 goto exit;
962
963 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100964 if (!err) {
965 if (unlikely(buf_len < sz)) {
966 sz = buf_len;
967 m->msg_flags |= MSG_TRUNC;
968 }
Allan Stephens0232fd02011-02-21 09:45:40 -0500969 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
970 m->msg_iov, sz);
971 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100972 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100973 res = sz;
974 } else {
975 if ((sock->state == SS_READY) ||
976 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
977 res = 0;
978 else
979 res = -ECONNRESET;
980 }
981
982 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100983 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -0700984 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -0700985 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
986 tipc_acknowledge(tport->ref, tport->conn_unacked);
987 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900988 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700990 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100991 return res;
992}
993
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900994/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100995 * recv_stream - receive stream-oriented data
996 * @iocb: (unused)
997 * @m: descriptor for message info
998 * @buf_len: total size of user buffer area
999 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001000 *
1001 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001002 * will optionally wait for more; never truncates data.
1003 *
1004 * Returns size of returned message data, errno otherwise
1005 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001006static int recv_stream(struct kiocb *iocb, struct socket *sock,
1007 struct msghdr *m, size_t buf_len, int flags)
1008{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001009 struct sock *sk = sock->sk;
1010 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001011 struct sk_buff *buf;
1012 struct tipc_msg *msg;
Allan Stephens71092ea2011-02-23 14:52:14 -05001013 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001015 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001017 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001018 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001019
1020 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001021 if (unlikely(!buf_len))
1022 return -EINVAL;
1023
Allan Stephens0c3141e2008-04-15 00:22:02 -07001024 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025
Allan Stephens0c3141e2008-04-15 00:22:02 -07001026 if (unlikely((sock->state == SS_UNCONNECTED) ||
1027 (sock->state == SS_CONNECTING))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 res = -ENOTCONN;
1029 goto exit;
1030 }
1031
Florian Westphal3720d402010-08-17 11:00:04 +00001032 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Allan Stephens71092ea2011-02-23 14:52:14 -05001033 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001034
Allan Stephens0c3141e2008-04-15 00:22:02 -07001035restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001036 /* Look for a message in receive queue; wait if necessary */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001037 while (skb_queue_empty(&sk->sk_receive_queue)) {
1038 if (sock->state == SS_DISCONNECTING) {
1039 res = -ENOTCONN;
1040 goto exit;
1041 }
Allan Stephens71092ea2011-02-23 14:52:14 -05001042 if (timeout <= 0L) {
1043 res = timeout ? timeout : -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001044 goto exit;
1045 }
1046 release_sock(sk);
Allan Stephens71092ea2011-02-23 14:52:14 -05001047 timeout = wait_event_interruptible_timeout(*sk_sleep(sk),
1048 tipc_rx_ready(sock),
1049 timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001050 lock_sock(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001051 }
1052
1053 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001054 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 msg = buf_msg(buf);
1056 sz = msg_data_sz(msg);
1057 err = msg_errcode(msg);
1058
1059 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001061 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 goto restart;
1063 }
1064
1065 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001066 if (sz_copied == 0) {
1067 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001068 res = anc_data_recv(m, msg, tport);
1069 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001070 goto exit;
1071 }
1072
1073 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074 if (!err) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001075 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076
Allan Stephens0232fd02011-02-21 09:45:40 -05001077 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001078 needed = (buf_len - sz_copied);
1079 sz_to_copy = (sz <= needed) ? sz : needed;
Allan Stephens0232fd02011-02-21 09:45:40 -05001080
1081 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1082 m->msg_iov, sz_to_copy);
1083 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001085
Per Lidenb97bf3f2006-01-02 19:04:38 +01001086 sz_copied += sz_to_copy;
1087
1088 if (sz_to_copy < sz) {
1089 if (!(flags & MSG_PEEK))
Allan Stephens0232fd02011-02-21 09:45:40 -05001090 TIPC_SKB_CB(buf)->handle =
1091 (void *)(unsigned long)(offset + sz_to_copy);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092 goto exit;
1093 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001094 } else {
1095 if (sz_copied != 0)
1096 goto exit; /* can't add error msg to valid data */
1097
1098 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1099 res = 0;
1100 else
1101 res = -ECONNRESET;
1102 }
1103
1104 /* Consume received message (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001105 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001106 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1107 tipc_acknowledge(tport->ref, tport->conn_unacked);
1108 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001109 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001110
1111 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001112 if ((sz_copied < buf_len) && /* didn't get all requested data */
1113 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001114 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001115 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1116 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117 goto restart;
1118
1119exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001120 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001121 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001122}
1123
1124/**
Ying Xuef288bef2012-08-21 11:16:57 +08001125 * tipc_write_space - wake up thread if port congestion is released
1126 * @sk: socket
1127 */
1128static void tipc_write_space(struct sock *sk)
1129{
1130 struct socket_wq *wq;
1131
1132 rcu_read_lock();
1133 wq = rcu_dereference(sk->sk_wq);
1134 if (wq_has_sleeper(wq))
1135 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1136 POLLWRNORM | POLLWRBAND);
1137 rcu_read_unlock();
1138}
1139
1140/**
1141 * tipc_data_ready - wake up threads to indicate messages have been received
1142 * @sk: socket
1143 * @len: the length of messages
1144 */
1145static void tipc_data_ready(struct sock *sk, int len)
1146{
1147 struct socket_wq *wq;
1148
1149 rcu_read_lock();
1150 wq = rcu_dereference(sk->sk_wq);
1151 if (wq_has_sleeper(wq))
1152 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1153 POLLRDNORM | POLLRDBAND);
1154 rcu_read_unlock();
1155}
1156
1157/**
Allan Stephens1819b832008-04-15 00:15:50 -07001158 * rx_queue_full - determine if receive queue can accept another message
1159 * @msg: message to be added to queue
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160 * @queue_size: current size of queue
1161 * @base: nominal maximum size of queue
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001162 *
Allan Stephens1819b832008-04-15 00:15:50 -07001163 * Returns 1 if queue is unable to accept message, 0 otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001164 */
Allan Stephens1819b832008-04-15 00:15:50 -07001165static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001166{
1167 u32 threshold;
1168 u32 imp = msg_importance(msg);
1169
1170 if (imp == TIPC_LOW_IMPORTANCE)
1171 threshold = base;
1172 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1173 threshold = base * 2;
1174 else if (imp == TIPC_HIGH_IMPORTANCE)
1175 threshold = base * 100;
1176 else
1177 return 0;
1178
1179 if (msg_connected(msg))
1180 threshold *= 4;
1181
Eric Dumazeta02cec22010-09-22 20:43:57 +00001182 return queue_size >= threshold;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183}
1184
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001185/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001186 * filter_connect - Handle all incoming messages for a connection-based socket
1187 * @tsock: TIPC socket
1188 * @msg: message
1189 *
1190 * Returns TIPC error status code and socket error status code
1191 * once it encounters some errors
1192 */
1193static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
1194{
1195 struct socket *sock = tsock->sk.sk_socket;
1196 struct tipc_msg *msg = buf_msg(*buf);
Ying Xue584d24b2012-11-29 18:51:19 -05001197 struct sock *sk = &tsock->sk;
Ying Xue7e6c1312012-11-29 18:39:14 -05001198 u32 retval = TIPC_ERR_NO_PORT;
Ying Xue584d24b2012-11-29 18:51:19 -05001199 int res;
Ying Xue7e6c1312012-11-29 18:39:14 -05001200
1201 if (msg_mcast(msg))
1202 return retval;
1203
1204 switch ((int)sock->state) {
1205 case SS_CONNECTED:
1206 /* Accept only connection-based messages sent by peer */
1207 if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
1208 if (unlikely(msg_errcode(msg))) {
1209 sock->state = SS_DISCONNECTING;
1210 __tipc_disconnect(tsock->p);
1211 }
1212 retval = TIPC_OK;
1213 }
1214 break;
1215 case SS_CONNECTING:
1216 /* Accept only ACK or NACK message */
Ying Xue584d24b2012-11-29 18:51:19 -05001217 if (unlikely(msg_errcode(msg))) {
1218 sock->state = SS_DISCONNECTING;
1219 sk->sk_err = -ECONNREFUSED;
Ying Xue7e6c1312012-11-29 18:39:14 -05001220 retval = TIPC_OK;
Ying Xue584d24b2012-11-29 18:51:19 -05001221 break;
1222 }
1223
1224 if (unlikely(!msg_connected(msg)))
1225 break;
1226
1227 res = auto_connect(sock, msg);
1228 if (res) {
1229 sock->state = SS_DISCONNECTING;
1230 sk->sk_err = res;
1231 retval = TIPC_OK;
1232 break;
1233 }
1234
1235 /* If an incoming message is an 'ACK-', it should be
1236 * discarded here because it doesn't contain useful
1237 * data. In addition, we should try to wake up
1238 * connect() routine if sleeping.
1239 */
1240 if (msg_data_sz(msg) == 0) {
1241 kfree_skb(*buf);
1242 *buf = NULL;
1243 if (waitqueue_active(sk_sleep(sk)))
1244 wake_up_interruptible(sk_sleep(sk));
1245 }
1246 retval = TIPC_OK;
Ying Xue7e6c1312012-11-29 18:39:14 -05001247 break;
1248 case SS_LISTENING:
1249 case SS_UNCONNECTED:
1250 /* Accept only SYN message */
1251 if (!msg_connected(msg) && !(msg_errcode(msg)))
1252 retval = TIPC_OK;
1253 break;
1254 case SS_DISCONNECTING:
1255 break;
1256 default:
1257 pr_err("Unknown socket state %u\n", sock->state);
1258 }
1259 return retval;
1260}
1261
1262/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001263 * filter_rcv - validate incoming message
1264 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001265 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001266 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001267 * Enqueues message on receive queue if acceptable; optionally handles
1268 * disconnect indication for a connected socket.
1269 *
1270 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001271 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1273 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001274static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001275{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001276 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001277 struct tipc_msg *msg = buf_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278 u32 recv_q_len;
Ying Xue7e6c1312012-11-29 18:39:14 -05001279 u32 res = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001280
Per Lidenb97bf3f2006-01-02 19:04:38 +01001281 /* Reject message if it is wrong sort of message for socket */
Allan Stephensaad58542012-04-26 18:13:08 -04001282 if (msg_type(msg) > TIPC_DIRECT_MSG)
1283 return TIPC_ERR_NO_PORT;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001284
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285 if (sock->state == SS_READY) {
Allan Stephensb29f1422010-12-31 18:59:25 +00001286 if (msg_connected(msg))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001287 return TIPC_ERR_NO_PORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001288 } else {
Ying Xue7e6c1312012-11-29 18:39:14 -05001289 res = filter_connect(tipc_sk(sk), &buf);
1290 if (res != TIPC_OK || buf == NULL)
1291 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001292 }
1293
1294 /* Reject message if there isn't room to queue it */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001295 recv_q_len = skb_queue_len(&sk->sk_receive_queue);
Allan Stephens1819b832008-04-15 00:15:50 -07001296 if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
1297 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001298 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001299 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001300
Allan Stephens0c3141e2008-04-15 00:22:02 -07001301 /* Enqueue message (finally!) */
Allan Stephens0232fd02011-02-21 09:45:40 -05001302 TIPC_SKB_CB(buf)->handle = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001303 __skb_queue_tail(&sk->sk_receive_queue, buf);
1304
Ying Xuef288bef2012-08-21 11:16:57 +08001305 sk->sk_data_ready(sk, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001306 return TIPC_OK;
1307}
1308
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001309/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001310 * backlog_rcv - handle incoming message from backlog queue
1311 * @sk: socket
1312 * @buf: message
1313 *
1314 * Caller must hold socket lock, but not port lock.
1315 *
1316 * Returns 0
1317 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001318static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1319{
1320 u32 res;
1321
1322 res = filter_rcv(sk, buf);
1323 if (res)
1324 tipc_reject_msg(buf, res);
1325 return 0;
1326}
1327
1328/**
1329 * dispatch - handle incoming message
1330 * @tport: TIPC port that received message
1331 * @buf: message
1332 *
1333 * Called with port lock already taken.
1334 *
1335 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1336 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001337static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1338{
1339 struct sock *sk = (struct sock *)tport->usr_handle;
1340 u32 res;
1341
1342 /*
1343 * Process message if socket is unlocked; otherwise add to backlog queue
1344 *
1345 * This code is based on sk_receive_skb(), but must be distinct from it
1346 * since a TIPC-specific filter/reject mechanism is utilized
1347 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001348 bh_lock_sock(sk);
1349 if (!sock_owned_by_user(sk)) {
1350 res = filter_rcv(sk, buf);
1351 } else {
Eric Dumazetf545a382012-04-22 23:34:26 +00001352 if (sk_add_backlog(sk, buf, sk->sk_rcvbuf))
Zhu Yi53eecb12010-03-04 18:01:45 +00001353 res = TIPC_ERR_OVERLOAD;
1354 else
1355 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001356 }
1357 bh_unlock_sock(sk);
1358
1359 return res;
1360}
1361
1362/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001363 * wakeupdispatch - wake up port after congestion
1364 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001365 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001366 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001368static void wakeupdispatch(struct tipc_port *tport)
1369{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001370 struct sock *sk = (struct sock *)tport->usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371
Ying Xuef288bef2012-08-21 11:16:57 +08001372 sk->sk_write_space(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001373}
1374
1375/**
1376 * connect - establish a connection to another TIPC port
1377 * @sock: socket structure
1378 * @dest: socket address for destination port
1379 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001380 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001381 *
1382 * Returns 0 on success, errno otherwise
1383 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001384static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001385 int flags)
1386{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001387 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001388 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1389 struct msghdr m = {NULL,};
Allan Stephensa0f40f02011-05-26 13:44:34 -04001390 unsigned int timeout;
Allan Stephensb89741a2008-04-15 00:20:37 -07001391 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392
Allan Stephens0c3141e2008-04-15 00:22:02 -07001393 lock_sock(sk);
1394
Allan Stephensb89741a2008-04-15 00:20:37 -07001395 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001396 if (sock->state == SS_READY) {
1397 res = -EOPNOTSUPP;
1398 goto exit;
1399 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001400
Allan Stephensb89741a2008-04-15 00:20:37 -07001401 /*
1402 * Reject connection attempt using multicast address
1403 *
1404 * Note: send_msg() validates the rest of the address fields,
1405 * so there's no need to do it here
1406 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001407 if (dst->addrtype == TIPC_ADDR_MCAST) {
1408 res = -EINVAL;
1409 goto exit;
1410 }
1411
Ying Xue584d24b2012-11-29 18:51:19 -05001412 timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001413
Ying Xue584d24b2012-11-29 18:51:19 -05001414 switch (sock->state) {
1415 case SS_UNCONNECTED:
1416 /* Send a 'SYN-' to destination */
1417 m.msg_name = dest;
1418 m.msg_namelen = destlen;
1419
1420 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1421 * indicate send_msg() is never blocked.
1422 */
1423 if (!timeout)
1424 m.msg_flags = MSG_DONTWAIT;
1425
1426 res = send_msg(NULL, sock, &m, 0);
1427 if ((res < 0) && (res != -EWOULDBLOCK))
1428 goto exit;
1429
1430 /* Just entered SS_CONNECTING state; the only
1431 * difference is that return value in non-blocking
1432 * case is EINPROGRESS, rather than EALREADY.
1433 */
1434 res = -EINPROGRESS;
1435 break;
1436 case SS_CONNECTING:
1437 res = -EALREADY;
1438 break;
1439 case SS_CONNECTED:
1440 res = -EISCONN;
1441 break;
1442 default:
1443 res = -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001444 goto exit;
Allan Stephensb89741a2008-04-15 00:20:37 -07001445 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001446
Ying Xue584d24b2012-11-29 18:51:19 -05001447 if (sock->state == SS_CONNECTING) {
1448 if (!timeout)
1449 goto exit;
1450
1451 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1452 release_sock(sk);
1453 res = wait_event_interruptible_timeout(*sk_sleep(sk),
1454 sock->state != SS_CONNECTING,
1455 timeout ? (long)msecs_to_jiffies(timeout)
1456 : MAX_SCHEDULE_TIMEOUT);
1457 lock_sock(sk);
1458 if (res <= 0) {
1459 if (res == 0)
1460 res = -ETIMEDOUT;
1461 else
1462 ; /* leave "res" unchanged */
1463 goto exit;
1464 }
1465 }
1466
1467 if (unlikely(sock->state == SS_DISCONNECTING))
1468 res = sock_error(sk);
1469 else
1470 res = 0;
1471
Allan Stephens0c3141e2008-04-15 00:22:02 -07001472exit:
1473 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001474 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001475}
1476
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001477/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001478 * listen - allow socket to listen for incoming connections
1479 * @sock: socket structure
1480 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001481 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001482 * Returns 0 on success, errno otherwise
1483 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001484static int listen(struct socket *sock, int len)
1485{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001486 struct sock *sk = sock->sk;
1487 int res;
1488
1489 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001490
Ying Xue245f3d32011-07-06 06:01:13 -04001491 if (sock->state != SS_UNCONNECTED)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001492 res = -EINVAL;
1493 else {
1494 sock->state = SS_LISTENING;
1495 res = 0;
1496 }
1497
1498 release_sock(sk);
1499 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001500}
1501
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001502/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001503 * accept - wait for connection request
1504 * @sock: listening socket
1505 * @newsock: new socket that is to be connected
1506 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001507 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001508 * Returns 0 on success, errno otherwise
1509 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001510static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001512 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001513 struct sk_buff *buf;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001514 struct tipc_sock *new_tsock;
1515 struct tipc_port *new_tport;
1516 struct tipc_msg *msg;
1517 u32 new_ref;
1518
Allan Stephens0c3141e2008-04-15 00:22:02 -07001519 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001520
Allan Stephens0c3141e2008-04-15 00:22:02 -07001521 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001522
Allan Stephens0c3141e2008-04-15 00:22:02 -07001523 if (sock->state != SS_LISTENING) {
1524 res = -EINVAL;
1525 goto exit;
1526 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001527
Allan Stephens0c3141e2008-04-15 00:22:02 -07001528 while (skb_queue_empty(&sk->sk_receive_queue)) {
1529 if (flags & O_NONBLOCK) {
1530 res = -EWOULDBLOCK;
1531 goto exit;
1532 }
1533 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001534 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001535 (!skb_queue_empty(&sk->sk_receive_queue)));
1536 lock_sock(sk);
1537 if (res)
1538 goto exit;
1539 }
1540
1541 buf = skb_peek(&sk->sk_receive_queue);
1542
Eric Paris3f378b62009-11-05 22:18:14 -08001543 res = tipc_create(sock_net(sock->sk), new_sock, 0, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001544 if (res)
1545 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001546
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001547 new_sk = new_sock->sk;
1548 new_tsock = tipc_sk(new_sk);
1549 new_tport = new_tsock->p;
1550 new_ref = new_tport->ref;
1551 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001552
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001553 /* we lock on new_sk; but lockdep sees the lock on sk */
1554 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001555
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001556 /*
1557 * Reject any stray messages received by new socket
1558 * before the socket lock was taken (very, very unlikely)
1559 */
1560 reject_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001561
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001562 /* Connect new socket to it's peer */
1563 new_tsock->peer_name.ref = msg_origport(msg);
1564 new_tsock->peer_name.node = msg_orignode(msg);
1565 tipc_connect(new_ref, &new_tsock->peer_name);
1566 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001567
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001568 tipc_set_portimportance(new_ref, msg_importance(msg));
1569 if (msg_named(msg)) {
1570 new_tport->conn_type = msg_nametype(msg);
1571 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001572 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05001573
1574 /*
1575 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1576 * Respond to 'SYN+' by queuing it on new socket.
1577 */
1578 if (!msg_data_sz(msg)) {
1579 struct msghdr m = {NULL,};
1580
1581 advance_rx_queue(sk);
1582 send_packet(NULL, new_sock, &m, 0);
1583 } else {
1584 __skb_dequeue(&sk->sk_receive_queue);
1585 __skb_queue_head(&new_sk->sk_receive_queue, buf);
1586 }
1587 release_sock(new_sk);
1588
Per Lidenb97bf3f2006-01-02 19:04:38 +01001589exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001590 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001591 return res;
1592}
1593
1594/**
1595 * shutdown - shutdown socket connection
1596 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001597 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001598 *
1599 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001600 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001601 * Returns 0 on success, errno otherwise
1602 */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001603static int shutdown(struct socket *sock, int how)
1604{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001605 struct sock *sk = sock->sk;
1606 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001607 struct sk_buff *buf;
1608 int res;
1609
Allan Stephense247a8f2008-03-06 15:05:38 -08001610 if (how != SHUT_RDWR)
1611 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001612
Allan Stephens0c3141e2008-04-15 00:22:02 -07001613 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001614
1615 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001616 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001617 case SS_CONNECTED:
1618
Per Lidenb97bf3f2006-01-02 19:04:38 +01001619restart:
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001620 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001621 buf = __skb_dequeue(&sk->sk_receive_queue);
1622 if (buf) {
Allan Stephens0232fd02011-02-21 09:45:40 -05001623 if (TIPC_SKB_CB(buf)->handle != 0) {
Allan Stephens5f6d9122011-11-04 13:24:29 -04001624 kfree_skb(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001625 goto restart;
1626 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001627 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001628 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001629 } else {
1630 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001631 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001632
1633 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001634
1635 /* fall through */
1636
1637 case SS_DISCONNECTING:
1638
Ying Xue75031152012-10-29 09:38:15 -04001639 /* Discard any unreceived messages */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001640 discard_rx_queue(sk);
Ying Xue75031152012-10-29 09:38:15 -04001641
1642 /* Wake up anyone sleeping in poll */
1643 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001644 res = 0;
1645 break;
1646
1647 default:
1648 res = -ENOTCONN;
1649 }
1650
Allan Stephens0c3141e2008-04-15 00:22:02 -07001651 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001652 return res;
1653}
1654
1655/**
1656 * setsockopt - set socket option
1657 * @sock: socket structure
1658 * @lvl: option level
1659 * @opt: option identifier
1660 * @ov: pointer to new option value
1661 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001662 *
1663 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001664 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001665 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666 * Returns 0 on success, errno otherwise
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 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001727static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001728 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001729{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001730 struct sock *sk = sock->sk;
1731 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001732 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001733 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001734 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001735
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001736 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1737 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001738 if (lvl != SOL_TIPC)
1739 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00001740 res = get_user(len, ol);
1741 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001742 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001743
Allan Stephens0c3141e2008-04-15 00:22:02 -07001744 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001745
1746 switch (opt) {
1747 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001748 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749 break;
1750 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001751 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001752 break;
1753 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001754 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001755 break;
1756 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04001757 value = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001758 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001759 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001760 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05001761 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001762 break;
Allan Stephens0e659672010-12-31 18:59:32 +00001763 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001764 value = skb_queue_len(&sk->sk_receive_queue);
1765 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001766 default:
1767 res = -EINVAL;
1768 }
1769
Allan Stephens0c3141e2008-04-15 00:22:02 -07001770 release_sock(sk);
1771
Paul Gortmaker25860c32010-12-31 18:59:31 +00001772 if (res)
1773 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774
Paul Gortmaker25860c32010-12-31 18:59:31 +00001775 if (len < sizeof(value))
1776 return -EINVAL;
1777
1778 if (copy_to_user(ov, &value, sizeof(value)))
1779 return -EFAULT;
1780
1781 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001782}
1783
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00001784/* Protocol switches for the various types of TIPC sockets */
1785
Florian Westphalbca65ea2008-02-07 18:18:01 -08001786static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001787 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001788 .family = AF_TIPC,
1789 .release = release,
1790 .bind = bind,
1791 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001792 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04001793 .accept = sock_no_accept,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001794 .getname = get_name,
1795 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001796 .ioctl = sock_no_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04001797 .listen = sock_no_listen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001798 .shutdown = shutdown,
1799 .setsockopt = setsockopt,
1800 .getsockopt = getsockopt,
1801 .sendmsg = send_msg,
1802 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001803 .mmap = sock_no_mmap,
1804 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001805};
1806
Florian Westphalbca65ea2008-02-07 18:18:01 -08001807static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001808 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001809 .family = AF_TIPC,
1810 .release = release,
1811 .bind = bind,
1812 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001813 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001814 .accept = accept,
1815 .getname = get_name,
1816 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001817 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001818 .listen = listen,
1819 .shutdown = shutdown,
1820 .setsockopt = setsockopt,
1821 .getsockopt = getsockopt,
1822 .sendmsg = send_packet,
1823 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001824 .mmap = sock_no_mmap,
1825 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826};
1827
Florian Westphalbca65ea2008-02-07 18:18:01 -08001828static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001829 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001830 .family = AF_TIPC,
1831 .release = release,
1832 .bind = bind,
1833 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001834 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001835 .accept = accept,
1836 .getname = get_name,
1837 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001838 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001839 .listen = listen,
1840 .shutdown = shutdown,
1841 .setsockopt = setsockopt,
1842 .getsockopt = getsockopt,
1843 .sendmsg = send_stream,
1844 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001845 .mmap = sock_no_mmap,
1846 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001847};
1848
Florian Westphalbca65ea2008-02-07 18:18:01 -08001849static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00001850 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001851 .family = AF_TIPC,
1852 .create = tipc_create
1853};
1854
1855static struct proto tipc_proto = {
1856 .name = "TIPC",
1857 .owner = THIS_MODULE,
1858 .obj_size = sizeof(struct tipc_sock)
1859};
1860
1861/**
Per Liden4323add2006-01-18 00:38:21 +01001862 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001863 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001864 * Returns 0 on success, errno otherwise
1865 */
Per Liden4323add2006-01-18 00:38:21 +01001866int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001867{
1868 int res;
1869
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001870 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001871 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001872 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001873 goto out;
1874 }
1875
1876 res = sock_register(&tipc_family_ops);
1877 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04001878 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001879 proto_unregister(&tipc_proto);
1880 goto out;
1881 }
1882
1883 sockets_enabled = 1;
1884 out:
1885 return res;
1886}
1887
1888/**
Per Liden4323add2006-01-18 00:38:21 +01001889 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001890 */
Per Liden4323add2006-01-18 00:38:21 +01001891void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001892{
1893 if (!sockets_enabled)
1894 return;
1895
1896 sockets_enabled = 0;
1897 sock_unregister(tipc_family_ops.family);
1898 proto_unregister(&tipc_proto);
1899}