blob: b89c7b1a0d09c4866ad0f1b741c254a834b1d64c [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 Stephens0ea52242008-07-14 22:42:19 -07005 * Copyright (c) 2004-2008, 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
37#include <linux/module.h>
38#include <linux/types.h>
39#include <linux/net.h>
40#include <linux/socket.h>
41#include <linux/errno.h>
42#include <linux/mm.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010043#include <linux/poll.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010044#include <linux/fcntl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/gfp.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010046#include <asm/string.h>
47#include <asm/atomic.h>
48#include <net/sock.h>
49
50#include <linux/tipc.h>
Per Lidenea714cc2006-01-11 12:28:47 +010051#include <linux/tipc_config.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010052#include <net/tipc/tipc_msg.h>
53#include <net/tipc/tipc_port.h>
54
55#include "core.h"
56
57#define SS_LISTENING -1 /* socket is listening */
58#define SS_READY -2 /* socket is connectionless */
59
Allan Stephens3654ea02008-04-13 21:35:11 -070060#define OVERLOAD_LIMIT_BASE 5000
61#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010062
63struct tipc_sock {
64 struct sock sk;
65 struct tipc_port *p;
Allan Stephens2da59912008-07-14 22:43:32 -070066 struct tipc_portid peer_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +010067};
68
Allan Stephens0c3141e2008-04-15 00:22:02 -070069#define tipc_sk(sk) ((struct tipc_sock *)(sk))
70#define tipc_sk_port(sk) ((struct tipc_port *)(tipc_sk(sk)->p))
Per Lidenb97bf3f2006-01-02 19:04:38 +010071
Allan Stephens0c3141e2008-04-15 00:22:02 -070072static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +010073static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
74static void wakeupdispatch(struct tipc_port *tport);
75
Florian Westphalbca65ea2008-02-07 18:18:01 -080076static const struct proto_ops packet_ops;
77static const struct proto_ops stream_ops;
78static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +010079
80static struct proto tipc_proto;
81
82static int sockets_enabled = 0;
83
84static atomic_t tipc_queue_size = ATOMIC_INIT(0);
85
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090086/*
Allan Stephens0c3141e2008-04-15 00:22:02 -070087 * Revised TIPC socket locking policy:
88 *
89 * Most socket operations take the standard socket lock when they start
90 * and hold it until they finish (or until they need to sleep). Acquiring
91 * this lock grants the owner exclusive access to the fields of the socket
92 * data structures, with the exception of the backlog queue. A few socket
93 * operations can be done without taking the socket lock because they only
94 * read socket information that never changes during the life of the socket.
95 *
96 * Socket operations may acquire the lock for the associated TIPC port if they
97 * need to perform an operation on the port. If any routine needs to acquire
98 * both the socket lock and the port lock it must take the socket lock first
99 * to avoid the risk of deadlock.
100 *
101 * The dispatcher handling incoming messages cannot grab the socket lock in
102 * the standard fashion, since invoked it runs at the BH level and cannot block.
103 * Instead, it checks to see if the socket lock is currently owned by someone,
104 * and either handles the message itself or adds it to the socket's backlog
105 * queue; in the latter case the queued message is processed once the process
106 * owning the socket lock releases it.
107 *
108 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
109 * the problem of a blocked socket operation preventing any other operations
110 * from occurring. However, applications must be careful if they have
111 * multiple threads trying to send (or receive) on the same socket, as these
112 * operations might interfere with each other. For example, doing a connect
113 * and a receive at the same time might allow the receive to consume the
114 * ACK message meant for the connect. While additional work could be done
115 * to try and overcome this, it doesn't seem to be worthwhile at the present.
116 *
117 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
118 * that another operation that must be performed in a non-blocking manner is
119 * not delayed for very long because the lock has already been taken.
120 *
121 * NOTE: This code assumes that certain fields of a port/socket pair are
122 * constant over its lifetime; such fields can be examined without taking
123 * the socket lock and/or port lock, and do not need to be re-read even
124 * after resuming processing after waiting. These fields include:
125 * - socket type
126 * - pointer to socket sk structure (aka tipc_sock structure)
127 * - pointer to port structure
128 * - port reference
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130
131/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700132 * advance_rx_queue - discard first buffer in socket receive queue
133 *
134 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135 */
136
Allan Stephens0c3141e2008-04-15 00:22:02 -0700137static void advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100138{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700139 buf_discard(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140 atomic_dec(&tipc_queue_size);
141}
142
143/**
Allan Stephens0c3141e2008-04-15 00:22:02 -0700144 * discard_rx_queue - discard all buffers in socket receive queue
145 *
146 * Caller must hold socket lock
147 */
148
149static void discard_rx_queue(struct sock *sk)
150{
151 struct sk_buff *buf;
152
153 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
154 atomic_dec(&tipc_queue_size);
155 buf_discard(buf);
156 }
157}
158
159/**
160 * reject_rx_queue - reject all buffers in socket receive queue
161 *
162 * Caller must hold socket lock
163 */
164
165static void reject_rx_queue(struct sock *sk)
166{
167 struct sk_buff *buf;
168
169 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
170 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
171 atomic_dec(&tipc_queue_size);
172 }
173}
174
175/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176 * tipc_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700177 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100178 * @sock: pre-allocated socket structure
179 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800180 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900181 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700182 * This routine creates additional data structures used by the TIPC socket,
183 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184 *
185 * Returns 0 on success, errno otherwise
186 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700187
Eric Paris3f378b62009-11-05 22:18:14 -0800188static int tipc_create(struct net *net, struct socket *sock, int protocol,
189 int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100190{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700191 const struct proto_ops *ops;
192 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100193 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700194 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700195
196 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100197
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800198 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700199 return -EAFNOSUPPORT;
200
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 if (unlikely(protocol != 0))
202 return -EPROTONOSUPPORT;
203
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204 switch (sock->type) {
205 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700206 ops = &stream_ops;
207 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100208 break;
209 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700210 ops = &packet_ops;
211 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 break;
213 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700215 ops = &msg_ops;
216 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217 break;
Allan Stephens49978652006-06-25 23:47:18 -0700218 default:
Allan Stephens49978652006-06-25 23:47:18 -0700219 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 }
221
Allan Stephens0c3141e2008-04-15 00:22:02 -0700222 /* Allocate socket's protocol area */
223
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700224 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700225 if (sk == NULL)
226 return -ENOMEM;
227
228 /* Allocate TIPC port for socket to use */
229
Allan Stephens0ea52242008-07-14 22:42:19 -0700230 tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
231 TIPC_LOW_IMPORTANCE);
232 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700233 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234 return -ENOMEM;
235 }
236
Allan Stephens0c3141e2008-04-15 00:22:02 -0700237 /* Finish initializing socket data structures */
238
239 sock->ops = ops;
240 sock->state = state;
241
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 sock_init_data(sock, sk);
Allan Stephens3654ea02008-04-13 21:35:11 -0700243 sk->sk_rcvtimeo = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700244 sk->sk_backlog_rcv = backlog_rcv;
Allan Stephens0ea52242008-07-14 22:42:19 -0700245 tipc_sk(sk)->p = tp_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700247 spin_unlock_bh(tp_ptr->lock);
248
Allan Stephens0c3141e2008-04-15 00:22:02 -0700249 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700250 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700251 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700252 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700253 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254
255 atomic_inc(&tipc_user_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256 return 0;
257}
258
259/**
260 * release - destroy a TIPC socket
261 * @sock: socket to destroy
262 *
263 * This routine cleans up any messages that are still queued on the socket.
264 * For DGRAM and RDM socket types, all queued messages are rejected.
265 * For SEQPACKET and STREAM socket types, the first message is rejected
266 * and any others are discarded. (If the first message on a STREAM socket
267 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900268 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269 * NOTE: Rejected messages are not necessarily returned to the sender! They
270 * are returned or discarded according to the "destination droppable" setting
271 * specified for the message by the sender.
272 *
273 * Returns 0 on success, errno otherwise
274 */
275
276static int release(struct socket *sock)
277{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700279 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700281 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282
Allan Stephens0c3141e2008-04-15 00:22:02 -0700283 /*
284 * Exit if socket isn't fully initialized (occurs when a failed accept()
285 * releases a pre-allocated child socket that was never used)
286 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900287
Allan Stephens0c3141e2008-04-15 00:22:02 -0700288 if (sk == NULL)
289 return 0;
290
291 tport = tipc_sk_port(sk);
292 lock_sock(sk);
293
294 /*
295 * Reject all unreceived messages, except on an active connection
296 * (which disconnects locally & sends a 'FIN+' to peer)
297 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298
299 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700300 buf = __skb_dequeue(&sk->sk_receive_queue);
301 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 break;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700303 atomic_dec(&tipc_queue_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
305 buf_discard(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700306 else {
307 if ((sock->state == SS_CONNECTING) ||
308 (sock->state == SS_CONNECTED)) {
309 sock->state = SS_DISCONNECTING;
310 tipc_disconnect(tport->ref);
311 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700313 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314 }
315
Allan Stephens0c3141e2008-04-15 00:22:02 -0700316 /*
317 * Delete TIPC port; this ensures no more messages are queued
318 * (also disconnects an active connection & sends a 'FIN-' to peer)
319 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320
Allan Stephens0c3141e2008-04-15 00:22:02 -0700321 res = tipc_deleteport(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322
Allan Stephens0c3141e2008-04-15 00:22:02 -0700323 /* Discard any remaining (connection-based) messages in receive queue */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324
Allan Stephens0c3141e2008-04-15 00:22:02 -0700325 discard_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100326
Allan Stephens0c3141e2008-04-15 00:22:02 -0700327 /* Reject any messages that accumulated in backlog queue */
328
329 sock->state = SS_DISCONNECTING;
330 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331
332 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700333 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900335 atomic_dec(&tipc_user_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336 return res;
337}
338
339/**
340 * bind - associate or disassocate TIPC name(s) with a socket
341 * @sock: socket structure
342 * @uaddr: socket address describing name(s) and desired operation
343 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900344 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 * Name and name sequence binding is indicated using a positive scope value;
346 * a negative scope value unbinds the specified name. Specifying no name
347 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900348 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700350 *
351 * NOTE: This routine doesn't need to take the socket lock since it doesn't
352 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100353 */
354
355static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
356{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700358 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359
Allan Stephens0c3141e2008-04-15 00:22:02 -0700360 if (unlikely(!uaddr_len))
361 return tipc_withdraw(portref, 0, NULL);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900362
Allan Stephens0c3141e2008-04-15 00:22:02 -0700363 if (uaddr_len < sizeof(struct sockaddr_tipc))
364 return -EINVAL;
365 if (addr->family != AF_TIPC)
366 return -EAFNOSUPPORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368 if (addr->addrtype == TIPC_ADDR_NAME)
369 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700370 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
371 return -EAFNOSUPPORT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900372
Allan Stephens0c3141e2008-04-15 00:22:02 -0700373 return (addr->scope > 0) ?
374 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
375 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376}
377
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900378/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 * get_name - get port ID of socket or peer socket
380 * @sock: socket structure
381 * @uaddr: area for returned socket address
382 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700383 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900384 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700386 *
Allan Stephens2da59912008-07-14 22:43:32 -0700387 * NOTE: This routine doesn't need to take the socket lock since it only
388 * accesses socket information that is unchanging (or which changes in
389 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100390 */
391
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900392static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 int *uaddr_len, int peer)
394{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700396 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397
Allan Stephens0c3141e2008-04-15 00:22:02 -0700398 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700399 if ((sock->state != SS_CONNECTED) &&
400 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
401 return -ENOTCONN;
402 addr->addr.id.ref = tsock->peer_name.ref;
403 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700404 } else {
Allan Stephens2da59912008-07-14 22:43:32 -0700405 tipc_ownidentity(tsock->p->ref, &addr->addr.id);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700406 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407
408 *uaddr_len = sizeof(*addr);
409 addr->addrtype = TIPC_ADDR_ID;
410 addr->family = AF_TIPC;
411 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412 addr->addr.name.domain = 0;
413
Allan Stephens0c3141e2008-04-15 00:22:02 -0700414 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415}
416
417/**
418 * poll - read and possibly block on pollmask
419 * @file: file structure associated with the socket
420 * @sock: socket for which to calculate the poll bits
421 * @wait: ???
422 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700423 * Returns pollmask value
424 *
425 * COMMENTARY:
426 * It appears that the usual socket locking mechanisms are not useful here
427 * since the pollmask info is potentially out-of-date the moment this routine
428 * exits. TCP and other protocols seem to rely on higher level poll routines
429 * to handle any preventable race conditions, so TIPC will do the same ...
430 *
431 * TIPC sets the returned events as follows:
432 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
433 * or if a connection-oriented socket is does not have an active connection
434 * (i.e. a read operation will not block).
435 * b) POLLOUT is set except when a socket's connection has been terminated
436 * (i.e. a write operation will not block).
437 * c) POLLHUP is set when a socket's connection has been terminated.
438 *
439 * IMPORTANT: The fact that a read or write operation will not block does NOT
440 * imply that the operation will succeed!
Per Lidenb97bf3f2006-01-02 19:04:38 +0100441 */
442
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900443static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444 poll_table *wait)
445{
Allan Stephens9b674e82008-03-26 16:48:21 -0700446 struct sock *sk = sock->sk;
447 u32 mask;
448
Eric Dumazetaa395142010-04-20 13:03:51 +0000449 poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700450
451 if (!skb_queue_empty(&sk->sk_receive_queue) ||
452 (sock->state == SS_UNCONNECTED) ||
453 (sock->state == SS_DISCONNECTING))
454 mask = (POLLRDNORM | POLLIN);
455 else
456 mask = 0;
457
458 if (sock->state == SS_DISCONNECTING)
459 mask |= POLLHUP;
460 else
461 mask |= POLLOUT;
462
463 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464}
465
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900466/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467 * dest_name_check - verify user is permitted to send to specified port name
468 * @dest: destination address
469 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900470 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471 * Prevents restricted configuration commands from being issued by
472 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900473 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474 * Returns 0 if permission is granted, otherwise errno
475 */
476
Sam Ravnborg05790c62006-03-20 22:37:04 -0800477static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478{
479 struct tipc_cfg_msg_hdr hdr;
480
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900481 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
482 return 0;
483 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
484 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900485 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
486 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900488 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700490 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900492
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493 return 0;
494}
495
496/**
497 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700498 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 * @sock: socket structure
500 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700501 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900502 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900504 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
506 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900507 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508 * Returns the number of bytes sent on success, or errno otherwise
509 */
510
511static int send_msg(struct kiocb *iocb, struct socket *sock,
512 struct msghdr *m, size_t total_len)
513{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700514 struct sock *sk = sock->sk;
515 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900516 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517 int needs_conn;
518 int res = -EINVAL;
519
520 if (unlikely(!dest))
521 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700522 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
523 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100524 return -EINVAL;
525
Allan Stephens0c3141e2008-04-15 00:22:02 -0700526 if (iocb)
527 lock_sock(sk);
528
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529 needs_conn = (sock->state != SS_READY);
530 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700531 if (sock->state == SS_LISTENING) {
532 res = -EPIPE;
533 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700534 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700535 if (sock->state != SS_UNCONNECTED) {
536 res = -EISCONN;
537 goto exit;
538 }
539 if ((tport->published) ||
540 ((sock->type == SOCK_STREAM) && (total_len != 0))) {
541 res = -EOPNOTSUPP;
542 goto exit;
543 }
544 if (dest->addrtype == TIPC_ADDR_NAME) {
545 tport->conn_type = dest->addr.name.name.type;
546 tport->conn_instance = dest->addr.name.name.instance;
547 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100548
549 /* Abort any pending connection attempts (very unlikely) */
550
Allan Stephens0c3141e2008-04-15 00:22:02 -0700551 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 }
553
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900554 do {
555 if (dest->addrtype == TIPC_ADDR_NAME) {
556 if ((res = dest_name_check(dest, m)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700557 break;
558 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900559 &dest->addr.name.name,
560 dest->addr.name.domain,
561 m->msg_iovlen,
562 m->msg_iov);
563 }
564 else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700565 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900566 &dest->addr.id,
567 m->msg_iovlen,
568 m->msg_iov);
569 }
570 else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100571 if (needs_conn) {
572 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700573 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900575 if ((res = dest_name_check(dest, m)))
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,
579 0,
580 m->msg_iovlen,
581 m->msg_iov);
582 }
583 if (likely(res != -ELINKCONG)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700584 if (needs_conn && (res >= 0)) {
585 sock->state = SS_CONNECTING;
586 }
587 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900588 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589 if (m->msg_flags & MSG_DONTWAIT) {
590 res = -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700591 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700593 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +0000594 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -0700595 !tport->congested);
596 lock_sock(sk);
597 if (res)
598 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900599 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700600
601exit:
602 if (iocb)
603 release_sock(sk);
604 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100605}
606
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900607/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700609 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 * @sock: socket structure
611 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700612 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900613 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900615 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616 * Returns the number of bytes sent on success, or errno otherwise
617 */
618
619static int send_packet(struct kiocb *iocb, struct socket *sock,
620 struct msghdr *m, size_t total_len)
621{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700622 struct sock *sk = sock->sk;
623 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900624 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 int res;
626
627 /* Handle implied connection establishment */
628
629 if (unlikely(dest))
630 return send_msg(iocb, sock, m, total_len);
631
Allan Stephens0c3141e2008-04-15 00:22:02 -0700632 if (iocb)
633 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900635 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700636 if (unlikely(sock->state != SS_CONNECTED)) {
637 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900638 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700639 else
640 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700641 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700642 }
643
Allan Stephens0c3141e2008-04-15 00:22:02 -0700644 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900645 if (likely(res != -ELINKCONG)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700646 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900647 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 if (m->msg_flags & MSG_DONTWAIT) {
649 res = -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700650 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700652 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +0000653 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -0700654 (!tport->congested || !tport->connected));
655 lock_sock(sk);
656 if (res)
657 break;
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 */
677
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678static int send_stream(struct kiocb *iocb, struct socket *sock,
679 struct msghdr *m, size_t total_len)
680{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700681 struct sock *sk = sock->sk;
682 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683 struct msghdr my_msg;
684 struct iovec my_iov;
685 struct iovec *curr_iov;
686 int curr_iovlen;
687 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700688 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689 int curr_left;
690 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700691 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100692 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900693
Allan Stephens0c3141e2008-04-15 00:22:02 -0700694 lock_sock(sk);
695
Allan Stephens05646c92007-06-10 17:25:24 -0700696 /* Handle special cases where there is no connection */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100697
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900698 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700699 if (sock->state == SS_UNCONNECTED) {
700 res = send_packet(NULL, sock, m, total_len);
701 goto exit;
702 } else if (sock->state == SS_DISCONNECTING) {
703 res = -EPIPE;
704 goto exit;
705 } else {
706 res = -ENOTCONN;
707 goto exit;
708 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900709 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710
Allan Stephens0c3141e2008-04-15 00:22:02 -0700711 if (unlikely(m->msg_name)) {
712 res = -EISCONN;
713 goto exit;
714 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700715
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900716 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100717 * Send each iovec entry using one or more messages
718 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900719 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100720 * (i.e. one large iovec entry), but could be improved to pass sets
721 * of small iovec entries into send_packet().
722 */
723
Allan Stephens1303e8f2006-06-25 23:46:50 -0700724 curr_iov = m->msg_iov;
725 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726 my_msg.msg_iov = &my_iov;
727 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700728 my_msg.msg_flags = m->msg_flags;
729 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700730 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731
Allan Stephens05646c92007-06-10 17:25:24 -0700732 hdr_size = msg_hdr_sz(&tport->phdr);
733
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734 while (curr_iovlen--) {
735 curr_start = curr_iov->iov_base;
736 curr_left = curr_iov->iov_len;
737
738 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700739 bytes_to_send = tport->max_pkt - hdr_size;
740 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
741 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
742 if (curr_left < bytes_to_send)
743 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100744 my_iov.iov_base = curr_start;
745 my_iov.iov_len = bytes_to_send;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700746 if ((res = send_packet(NULL, sock, &my_msg, 0)) < 0) {
747 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700748 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700749 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700750 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751 curr_left -= bytes_to_send;
752 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700753 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100754 }
755
756 curr_iov++;
757 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700758 res = bytes_sent;
759exit:
760 release_sock(sk);
761 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100762}
763
764/**
765 * auto_connect - complete connection setup to a remote port
766 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100767 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900768 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100769 * Returns 0 on success, errno otherwise
770 */
771
Allan Stephens0c3141e2008-04-15 00:22:02 -0700772static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773{
Allan Stephens2da59912008-07-14 22:43:32 -0700774 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775
776 if (msg_errcode(msg)) {
777 sock->state = SS_DISCONNECTING;
778 return -ECONNREFUSED;
779 }
780
Allan Stephens2da59912008-07-14 22:43:32 -0700781 tsock->peer_name.ref = msg_origport(msg);
782 tsock->peer_name.node = msg_orignode(msg);
783 tipc_connect2port(tsock->p->ref, &tsock->peer_name);
784 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100785 sock->state = SS_CONNECTED;
786 return 0;
787}
788
789/**
790 * set_orig_addr - capture sender's address for received message
791 * @m: descriptor for message info
792 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900793 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100794 * Note: Address is not captured if not requested by receiver.
795 */
796
Sam Ravnborg05790c62006-03-20 22:37:04 -0800797static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900799 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100800
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900801 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802 addr->family = AF_TIPC;
803 addr->addrtype = TIPC_ADDR_ID;
804 addr->addr.id.ref = msg_origport(msg);
805 addr->addr.id.node = msg_orignode(msg);
806 addr->addr.name.domain = 0; /* could leave uninitialized */
807 addr->scope = 0; /* could leave uninitialized */
808 m->msg_namelen = sizeof(struct sockaddr_tipc);
809 }
810}
811
812/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900813 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100814 * @m: descriptor for message info
815 * @msg: received message header
816 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900817 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100818 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900819 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100820 * Returns 0 if successful, otherwise errno
821 */
822
Sam Ravnborg05790c62006-03-20 22:37:04 -0800823static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100824 struct tipc_port *tport)
825{
826 u32 anc_data[3];
827 u32 err;
828 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700829 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830 int res;
831
832 if (likely(m->msg_controllen == 0))
833 return 0;
834
835 /* Optionally capture errored message object(s) */
836
837 err = msg ? msg_errcode(msg) : 0;
838 if (unlikely(err)) {
839 anc_data[0] = err;
840 anc_data[1] = msg_data_sz(msg);
Allan Stephens4b087b22006-06-25 23:47:44 -0700841 if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100842 return res;
843 if (anc_data[1] &&
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900844 (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
Per Lidenb97bf3f2006-01-02 19:04:38 +0100845 msg_data(msg))))
846 return res;
847 }
848
849 /* Optionally capture message destination object */
850
851 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
852 switch (dest_type) {
853 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700854 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100855 anc_data[0] = msg_nametype(msg);
856 anc_data[1] = msg_namelower(msg);
857 anc_data[2] = msg_namelower(msg);
858 break;
859 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700860 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100861 anc_data[0] = msg_nametype(msg);
862 anc_data[1] = msg_namelower(msg);
863 anc_data[2] = msg_nameupper(msg);
864 break;
865 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700866 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100867 anc_data[0] = tport->conn_type;
868 anc_data[1] = tport->conn_instance;
869 anc_data[2] = tport->conn_instance;
870 break;
871 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700872 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100873 }
Allan Stephens3546c752006-06-25 23:45:24 -0700874 if (has_name &&
Allan Stephens4b087b22006-06-25 23:47:44 -0700875 (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100876 return res;
877
878 return 0;
879}
880
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900881/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100882 * recv_msg - receive packet-oriented message
883 * @iocb: (unused)
884 * @m: descriptor for message info
885 * @buf_len: total size of user buffer area
886 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900887 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100888 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
889 * If the complete message doesn't fit in user area, truncate it.
890 *
891 * Returns size of returned message data, errno otherwise
892 */
893
894static int recv_msg(struct kiocb *iocb, struct socket *sock,
895 struct msghdr *m, size_t buf_len, int flags)
896{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700897 struct sock *sk = sock->sk;
898 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899 struct sk_buff *buf;
900 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 unsigned int sz;
902 u32 err;
903 int res;
904
Allan Stephens0c3141e2008-04-15 00:22:02 -0700905 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906
907 if (m->msg_iovlen != 1)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700908 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100909
910 if (unlikely(!buf_len))
911 return -EINVAL;
912
Allan Stephens0c3141e2008-04-15 00:22:02 -0700913 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914
Allan Stephens0c3141e2008-04-15 00:22:02 -0700915 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916 res = -ENOTCONN;
917 goto exit;
918 }
919
Allan Stephens0c3141e2008-04-15 00:22:02 -0700920restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921
Allan Stephens0c3141e2008-04-15 00:22:02 -0700922 /* Look for a message in receive queue; wait if necessary */
923
924 while (skb_queue_empty(&sk->sk_receive_queue)) {
925 if (sock->state == SS_DISCONNECTING) {
926 res = -ENOTCONN;
927 goto exit;
928 }
929 if (flags & MSG_DONTWAIT) {
930 res = -EWOULDBLOCK;
931 goto exit;
932 }
933 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +0000934 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -0700935 (!skb_queue_empty(&sk->sk_receive_queue) ||
936 (sock->state == SS_DISCONNECTING)));
937 lock_sock(sk);
938 if (res)
939 goto exit;
940 }
941
942 /* Look at first message in receive queue */
943
944 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
949 /* Complete connection setup for an implied connect */
950
951 if (unlikely(sock->state == SS_CONNECTING)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700952 res = auto_connect(sock, msg);
953 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100954 goto exit;
955 }
956
957 /* Discard an empty non-errored message & try again */
958
959 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700960 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100961 goto restart;
962 }
963
964 /* Capture sender's address (optional) */
965
966 set_orig_addr(m, msg);
967
968 /* Capture ancillary data (optional) */
969
Allan Stephens0c3141e2008-04-15 00:22:02 -0700970 res = anc_data_recv(m, msg, tport);
971 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100972 goto exit;
973
974 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900975
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976 if (!err) {
977 if (unlikely(buf_len < sz)) {
978 sz = buf_len;
979 m->msg_flags |= MSG_TRUNC;
980 }
981 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
982 sz))) {
983 res = -EFAULT;
984 goto exit;
985 }
986 res = sz;
987 } else {
988 if ((sock->state == SS_READY) ||
989 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
990 res = 0;
991 else
992 res = -ECONNRESET;
993 }
994
995 /* Consume received message (optional) */
996
997 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -0700998 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -0700999 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1000 tipc_acknowledge(tport->ref, tport->conn_unacked);
1001 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001002 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001003exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001004 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001005 return res;
1006}
1007
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001008/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001009 * recv_stream - receive stream-oriented data
1010 * @iocb: (unused)
1011 * @m: descriptor for message info
1012 * @buf_len: total size of user buffer area
1013 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001014 *
1015 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016 * will optionally wait for more; never truncates data.
1017 *
1018 * Returns size of returned message data, errno otherwise
1019 */
1020
1021static int recv_stream(struct kiocb *iocb, struct socket *sock,
1022 struct msghdr *m, size_t buf_len, int flags)
1023{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001024 struct sock *sk = sock->sk;
1025 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 struct sk_buff *buf;
1027 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001029 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001030 int sz_copied = 0;
Al Viro28c4dad2006-10-10 22:45:57 +01001031 char __user *crs = m->msg_iov->iov_base;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032 unsigned char *buf_crs;
1033 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001034 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001035
1036 /* Catch invalid receive attempts */
1037
Allan Stephens0c3141e2008-04-15 00:22:02 -07001038 if (m->msg_iovlen != 1)
1039 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
1040
Per Lidenb97bf3f2006-01-02 19:04:38 +01001041 if (unlikely(!buf_len))
1042 return -EINVAL;
1043
Allan Stephens0c3141e2008-04-15 00:22:02 -07001044 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001045
Allan Stephens0c3141e2008-04-15 00:22:02 -07001046 if (unlikely((sock->state == SS_UNCONNECTED) ||
1047 (sock->state == SS_CONNECTING))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 res = -ENOTCONN;
1049 goto exit;
1050 }
1051
Florian Westphal3720d402010-08-17 11:00:04 +00001052 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
1053
Allan Stephens0c3141e2008-04-15 00:22:02 -07001054restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055
Allan Stephens0c3141e2008-04-15 00:22:02 -07001056 /* Look for a message in receive queue; wait if necessary */
1057
1058 while (skb_queue_empty(&sk->sk_receive_queue)) {
1059 if (sock->state == SS_DISCONNECTING) {
1060 res = -ENOTCONN;
1061 goto exit;
1062 }
1063 if (flags & MSG_DONTWAIT) {
1064 res = -EWOULDBLOCK;
1065 goto exit;
1066 }
1067 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001068 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001069 (!skb_queue_empty(&sk->sk_receive_queue) ||
1070 (sock->state == SS_DISCONNECTING)));
1071 lock_sock(sk);
1072 if (res)
1073 goto exit;
1074 }
1075
1076 /* Look at first message in receive queue */
1077
1078 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001079 msg = buf_msg(buf);
1080 sz = msg_data_sz(msg);
1081 err = msg_errcode(msg);
1082
1083 /* Discard an empty non-errored message & try again */
1084
1085 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001086 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001087 goto restart;
1088 }
1089
1090 /* Optionally capture sender's address & ancillary data of first msg */
1091
1092 if (sz_copied == 0) {
1093 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001094 res = anc_data_recv(m, msg, tport);
1095 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001096 goto exit;
1097 }
1098
1099 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001100
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101 if (!err) {
1102 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
Allan Stephens7a8036c2008-04-15 00:15:15 -07001103 sz = (unsigned char *)msg + msg_size(msg) - buf_crs;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001104
1105 needed = (buf_len - sz_copied);
1106 sz_to_copy = (sz <= needed) ? sz : needed;
1107 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1108 res = -EFAULT;
1109 goto exit;
1110 }
1111 sz_copied += sz_to_copy;
1112
1113 if (sz_to_copy < sz) {
1114 if (!(flags & MSG_PEEK))
1115 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1116 goto exit;
1117 }
1118
1119 crs += sz_to_copy;
1120 } else {
1121 if (sz_copied != 0)
1122 goto exit; /* can't add error msg to valid data */
1123
1124 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1125 res = 0;
1126 else
1127 res = -ECONNRESET;
1128 }
1129
1130 /* Consume received message (optional) */
1131
1132 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001133 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1134 tipc_acknowledge(tport->ref, tport->conn_unacked);
1135 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001136 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137
1138 /* Loop around if more data is required */
1139
Joe Perchesf64f9e72009-11-29 16:55:45 -08001140 if ((sz_copied < buf_len) && /* didn't get all requested data */
1141 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001142 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001143 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1144 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145 goto restart;
1146
1147exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001148 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001149 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150}
1151
1152/**
Allan Stephens1819b832008-04-15 00:15:50 -07001153 * rx_queue_full - determine if receive queue can accept another message
1154 * @msg: message to be added to queue
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155 * @queue_size: current size of queue
1156 * @base: nominal maximum size of queue
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001157 *
Allan Stephens1819b832008-04-15 00:15:50 -07001158 * Returns 1 if queue is unable to accept message, 0 otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 */
1160
Allan Stephens1819b832008-04-15 00:15:50 -07001161static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001162{
1163 u32 threshold;
1164 u32 imp = msg_importance(msg);
1165
1166 if (imp == TIPC_LOW_IMPORTANCE)
1167 threshold = base;
1168 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1169 threshold = base * 2;
1170 else if (imp == TIPC_HIGH_IMPORTANCE)
1171 threshold = base * 100;
1172 else
1173 return 0;
1174
1175 if (msg_connected(msg))
1176 threshold *= 4;
1177
Allan Stephens1819b832008-04-15 00:15:50 -07001178 return (queue_size >= threshold);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179}
1180
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001181/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001182 * filter_rcv - validate incoming message
1183 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001185 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001186 * Enqueues message on receive queue if acceptable; optionally handles
1187 * disconnect indication for a connected socket.
1188 *
1189 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001190 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1192 */
1193
Allan Stephens0c3141e2008-04-15 00:22:02 -07001194static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001196 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197 struct tipc_msg *msg = buf_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 u32 recv_q_len;
1199
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200 /* Reject message if it is wrong sort of message for socket */
1201
1202 /*
1203 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1204 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1205 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1206 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001207
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 if (sock->state == SS_READY) {
1209 if (msg_connected(msg)) {
1210 msg_dbg(msg, "dispatch filter 1\n");
1211 return TIPC_ERR_NO_PORT;
1212 }
1213 } else {
1214 if (msg_mcast(msg)) {
1215 msg_dbg(msg, "dispatch filter 2\n");
1216 return TIPC_ERR_NO_PORT;
1217 }
1218 if (sock->state == SS_CONNECTED) {
1219 if (!msg_connected(msg)) {
1220 msg_dbg(msg, "dispatch filter 3\n");
1221 return TIPC_ERR_NO_PORT;
1222 }
1223 }
1224 else if (sock->state == SS_CONNECTING) {
1225 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1226 msg_dbg(msg, "dispatch filter 4\n");
1227 return TIPC_ERR_NO_PORT;
1228 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001229 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001230 else if (sock->state == SS_LISTENING) {
1231 if (msg_connected(msg) || msg_errcode(msg)) {
1232 msg_dbg(msg, "dispatch filter 5\n");
1233 return TIPC_ERR_NO_PORT;
1234 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001235 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 else if (sock->state == SS_DISCONNECTING) {
1237 msg_dbg(msg, "dispatch filter 6\n");
1238 return TIPC_ERR_NO_PORT;
1239 }
1240 else /* (sock->state == SS_UNCONNECTED) */ {
1241 if (msg_connected(msg) || msg_errcode(msg)) {
1242 msg_dbg(msg, "dispatch filter 7\n");
1243 return TIPC_ERR_NO_PORT;
1244 }
1245 }
1246 }
1247
1248 /* Reject message if there isn't room to queue it */
1249
Allan Stephens1819b832008-04-15 00:15:50 -07001250 recv_q_len = (u32)atomic_read(&tipc_queue_size);
1251 if (unlikely(recv_q_len >= OVERLOAD_LIMIT_BASE)) {
1252 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001253 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001254 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001255 recv_q_len = skb_queue_len(&sk->sk_receive_queue);
Allan Stephens1819b832008-04-15 00:15:50 -07001256 if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
1257 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001258 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001259 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001260
Allan Stephens0c3141e2008-04-15 00:22:02 -07001261 /* Enqueue message (finally!) */
1262
1263 msg_dbg(msg, "<DISP<: ");
1264 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1265 atomic_inc(&tipc_queue_size);
1266 __skb_queue_tail(&sk->sk_receive_queue, buf);
1267
Per Lidenb97bf3f2006-01-02 19:04:38 +01001268 /* Initiate connection termination for an incoming 'FIN' */
1269
1270 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1271 sock->state = SS_DISCONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001272 tipc_disconnect_port(tipc_sk_port(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001273 }
1274
Eric Dumazetaa395142010-04-20 13:03:51 +00001275 if (waitqueue_active(sk_sleep(sk)))
1276 wake_up_interruptible(sk_sleep(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001277 return TIPC_OK;
1278}
1279
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001280/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001281 * backlog_rcv - handle incoming message from backlog queue
1282 * @sk: socket
1283 * @buf: message
1284 *
1285 * Caller must hold socket lock, but not port lock.
1286 *
1287 * Returns 0
1288 */
1289
1290static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1291{
1292 u32 res;
1293
1294 res = filter_rcv(sk, buf);
1295 if (res)
1296 tipc_reject_msg(buf, res);
1297 return 0;
1298}
1299
1300/**
1301 * dispatch - handle incoming message
1302 * @tport: TIPC port that received message
1303 * @buf: message
1304 *
1305 * Called with port lock already taken.
1306 *
1307 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1308 */
1309
1310static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1311{
1312 struct sock *sk = (struct sock *)tport->usr_handle;
1313 u32 res;
1314
1315 /*
1316 * Process message if socket is unlocked; otherwise add to backlog queue
1317 *
1318 * This code is based on sk_receive_skb(), but must be distinct from it
1319 * since a TIPC-specific filter/reject mechanism is utilized
1320 */
1321
1322 bh_lock_sock(sk);
1323 if (!sock_owned_by_user(sk)) {
1324 res = filter_rcv(sk, buf);
1325 } else {
Zhu Yia3a858f2010-03-04 18:01:47 +00001326 if (sk_add_backlog(sk, buf))
Zhu Yi53eecb12010-03-04 18:01:45 +00001327 res = TIPC_ERR_OVERLOAD;
1328 else
1329 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001330 }
1331 bh_unlock_sock(sk);
1332
1333 return res;
1334}
1335
1336/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337 * wakeupdispatch - wake up port after congestion
1338 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001339 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001340 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001341 */
1342
1343static void wakeupdispatch(struct tipc_port *tport)
1344{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001345 struct sock *sk = (struct sock *)tport->usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346
Eric Dumazetaa395142010-04-20 13:03:51 +00001347 if (waitqueue_active(sk_sleep(sk)))
1348 wake_up_interruptible(sk_sleep(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001349}
1350
1351/**
1352 * connect - establish a connection to another TIPC port
1353 * @sock: socket structure
1354 * @dest: socket address for destination port
1355 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001356 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357 *
1358 * Returns 0 on success, errno otherwise
1359 */
1360
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001361static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362 int flags)
1363{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001364 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001365 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1366 struct msghdr m = {NULL,};
1367 struct sk_buff *buf;
1368 struct tipc_msg *msg;
1369 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370
Allan Stephens0c3141e2008-04-15 00:22:02 -07001371 lock_sock(sk);
1372
Allan Stephensb89741a2008-04-15 00:20:37 -07001373 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374
Allan Stephens0c3141e2008-04-15 00:22:02 -07001375 if (sock->state == SS_READY) {
1376 res = -EOPNOTSUPP;
1377 goto exit;
1378 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001379
Allan Stephensb89741a2008-04-15 00:20:37 -07001380 /* For now, TIPC does not support the non-blocking form of connect() */
Allan Stephens4934c692008-04-15 00:16:19 -07001381
Allan Stephens0c3141e2008-04-15 00:22:02 -07001382 if (flags & O_NONBLOCK) {
Allan Stephens35997e32010-08-17 11:00:05 +00001383 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001384 goto exit;
1385 }
Allan Stephens4934c692008-04-15 00:16:19 -07001386
Allan Stephensb89741a2008-04-15 00:20:37 -07001387 /* Issue Posix-compliant error code if socket is in the wrong state */
Allan Stephens51f9cc12006-06-25 23:49:06 -07001388
Allan Stephens0c3141e2008-04-15 00:22:02 -07001389 if (sock->state == SS_LISTENING) {
1390 res = -EOPNOTSUPP;
1391 goto exit;
1392 }
1393 if (sock->state == SS_CONNECTING) {
1394 res = -EALREADY;
1395 goto exit;
1396 }
1397 if (sock->state != SS_UNCONNECTED) {
1398 res = -EISCONN;
1399 goto exit;
1400 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001401
Allan Stephensb89741a2008-04-15 00:20:37 -07001402 /*
1403 * Reject connection attempt using multicast address
1404 *
1405 * Note: send_msg() validates the rest of the address fields,
1406 * so there's no need to do it here
1407 */
Allan Stephens51f9cc12006-06-25 23:49:06 -07001408
Allan Stephens0c3141e2008-04-15 00:22:02 -07001409 if (dst->addrtype == TIPC_ADDR_MCAST) {
1410 res = -EINVAL;
1411 goto exit;
1412 }
1413
1414 /* Reject any messages already in receive queue (very unlikely) */
1415
1416 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417
Allan Stephensb89741a2008-04-15 00:20:37 -07001418 /* Send a 'SYN-' to destination */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001419
Allan Stephensb89741a2008-04-15 00:20:37 -07001420 m.msg_name = dest;
1421 m.msg_namelen = destlen;
1422 res = send_msg(NULL, sock, &m, 0);
1423 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001424 goto exit;
Allan Stephensb89741a2008-04-15 00:20:37 -07001425 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426
Allan Stephens0c3141e2008-04-15 00:22:02 -07001427 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001428
Allan Stephens0c3141e2008-04-15 00:22:02 -07001429 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001430 res = wait_event_interruptible_timeout(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001431 (!skb_queue_empty(&sk->sk_receive_queue) ||
1432 (sock->state != SS_CONNECTING)),
1433 sk->sk_rcvtimeo);
1434 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001435
Allan Stephensb89741a2008-04-15 00:20:37 -07001436 if (res > 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001437 buf = skb_peek(&sk->sk_receive_queue);
1438 if (buf != NULL) {
1439 msg = buf_msg(buf);
1440 res = auto_connect(sock, msg);
1441 if (!res) {
1442 if (!msg_data_sz(msg))
1443 advance_rx_queue(sk);
1444 }
1445 } else {
1446 if (sock->state == SS_CONNECTED) {
1447 res = -EISCONN;
1448 } else {
1449 res = -ECONNREFUSED;
1450 }
Allan Stephensb89741a2008-04-15 00:20:37 -07001451 }
1452 } else {
1453 if (res == 0)
1454 res = -ETIMEDOUT;
1455 else
1456 ; /* leave "res" unchanged */
1457 sock->state = SS_DISCONNECTING;
1458 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001459
Allan Stephens0c3141e2008-04-15 00:22:02 -07001460exit:
1461 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001462 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463}
1464
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001465/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001466 * listen - allow socket to listen for incoming connections
1467 * @sock: socket structure
1468 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001469 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470 * Returns 0 on success, errno otherwise
1471 */
1472
1473static int listen(struct socket *sock, int len)
1474{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001475 struct sock *sk = sock->sk;
1476 int res;
1477
1478 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479
1480 if (sock->state == SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001481 res = -EOPNOTSUPP;
1482 else if (sock->state != SS_UNCONNECTED)
1483 res = -EINVAL;
1484 else {
1485 sock->state = SS_LISTENING;
1486 res = 0;
1487 }
1488
1489 release_sock(sk);
1490 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001491}
1492
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001493/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001494 * accept - wait for connection request
1495 * @sock: listening socket
1496 * @newsock: new socket that is to be connected
1497 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001498 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001499 * Returns 0 on success, errno otherwise
1500 */
1501
Allan Stephens0c3141e2008-04-15 00:22:02 -07001502static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001503{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001504 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001505 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001506 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001507
Allan Stephens0c3141e2008-04-15 00:22:02 -07001508 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001509
Allan Stephens0c3141e2008-04-15 00:22:02 -07001510 if (sock->state == SS_READY) {
1511 res = -EOPNOTSUPP;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001512 goto exit;
1513 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001514 if (sock->state != SS_LISTENING) {
1515 res = -EINVAL;
1516 goto exit;
1517 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001518
Allan Stephens0c3141e2008-04-15 00:22:02 -07001519 while (skb_queue_empty(&sk->sk_receive_queue)) {
1520 if (flags & O_NONBLOCK) {
1521 res = -EWOULDBLOCK;
1522 goto exit;
1523 }
1524 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001525 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001526 (!skb_queue_empty(&sk->sk_receive_queue)));
1527 lock_sock(sk);
1528 if (res)
1529 goto exit;
1530 }
1531
1532 buf = skb_peek(&sk->sk_receive_queue);
1533
Eric Paris3f378b62009-11-05 22:18:14 -08001534 res = tipc_create(sock_net(sock->sk), new_sock, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001535 if (!res) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001536 struct sock *new_sk = new_sock->sk;
Allan Stephens2da59912008-07-14 22:43:32 -07001537 struct tipc_sock *new_tsock = tipc_sk(new_sk);
1538 struct tipc_port *new_tport = new_tsock->p;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001539 u32 new_ref = new_tport->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001540 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001541
1542 lock_sock(new_sk);
1543
1544 /*
1545 * Reject any stray messages received by new socket
1546 * before the socket lock was taken (very, very unlikely)
1547 */
1548
1549 reject_rx_queue(new_sk);
1550
1551 /* Connect new socket to it's peer */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001552
Allan Stephens2da59912008-07-14 22:43:32 -07001553 new_tsock->peer_name.ref = msg_origport(msg);
1554 new_tsock->peer_name.node = msg_orignode(msg);
1555 tipc_connect2port(new_ref, &new_tsock->peer_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001556 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001557
1558 tipc_set_portimportance(new_ref, msg_importance(msg));
1559 if (msg_named(msg)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001560 new_tport->conn_type = msg_nametype(msg);
1561 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001562 }
1563
Allan Stephens0c3141e2008-04-15 00:22:02 -07001564 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001565 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1566 * Respond to 'SYN+' by queuing it on new socket.
1567 */
1568
1569 msg_dbg(msg,"<ACC<: ");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001570 if (!msg_data_sz(msg)) {
1571 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001572
Allan Stephens0c3141e2008-04-15 00:22:02 -07001573 advance_rx_queue(sk);
1574 send_packet(NULL, new_sock, &m, 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001575 } else {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001576 __skb_dequeue(&sk->sk_receive_queue);
1577 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001578 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001579 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001580 }
1581exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001582 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001583 return res;
1584}
1585
1586/**
1587 * shutdown - shutdown socket connection
1588 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001589 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001590 *
1591 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001592 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001593 * Returns 0 on success, errno otherwise
1594 */
1595
1596static int shutdown(struct socket *sock, int how)
1597{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001598 struct sock *sk = sock->sk;
1599 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600 struct sk_buff *buf;
1601 int res;
1602
Allan Stephense247a8f2008-03-06 15:05:38 -08001603 if (how != SHUT_RDWR)
1604 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001605
Allan Stephens0c3141e2008-04-15 00:22:02 -07001606 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001607
1608 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001609 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001610 case SS_CONNECTED:
1611
Allan Stephens0c3141e2008-04-15 00:22:02 -07001612 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001613restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001614 buf = __skb_dequeue(&sk->sk_receive_queue);
1615 if (buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001616 atomic_dec(&tipc_queue_size);
1617 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1618 buf_discard(buf);
1619 goto restart;
1620 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001621 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001622 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001623 } else {
1624 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001625 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001626
1627 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001628
1629 /* fall through */
1630
1631 case SS_DISCONNECTING:
1632
Allan Stephens0c3141e2008-04-15 00:22:02 -07001633 /* Discard any unreceived messages; wake up sleeping tasks */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001634
Allan Stephens0c3141e2008-04-15 00:22:02 -07001635 discard_rx_queue(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001636 if (waitqueue_active(sk_sleep(sk)))
1637 wake_up_interruptible(sk_sleep(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001638 res = 0;
1639 break;
1640
1641 default:
1642 res = -ENOTCONN;
1643 }
1644
Allan Stephens0c3141e2008-04-15 00:22:02 -07001645 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001646 return res;
1647}
1648
1649/**
1650 * setsockopt - set socket option
1651 * @sock: socket structure
1652 * @lvl: option level
1653 * @opt: option identifier
1654 * @ov: pointer to new option value
1655 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001656 *
1657 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001658 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001659 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001660 * Returns 0 on success, errno otherwise
1661 */
1662
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001663static int setsockopt(struct socket *sock,
David S. Millerb7058842009-09-30 16:12:20 -07001664 int lvl, int opt, char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001666 struct sock *sk = sock->sk;
1667 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001668 u32 value;
1669 int res;
1670
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001671 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1672 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001673 if (lvl != SOL_TIPC)
1674 return -ENOPROTOOPT;
1675 if (ol < sizeof(value))
1676 return -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001677 if ((res = get_user(value, (u32 __user *)ov)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001678 return res;
1679
Allan Stephens0c3141e2008-04-15 00:22:02 -07001680 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001681
Per Lidenb97bf3f2006-01-02 19:04:38 +01001682 switch (opt) {
1683 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001684 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001685 break;
1686 case TIPC_SRC_DROPPABLE:
1687 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001688 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001689 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690 res = -ENOPROTOOPT;
1691 break;
1692 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001693 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001694 break;
1695 case TIPC_CONN_TIMEOUT:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001696 sk->sk_rcvtimeo = msecs_to_jiffies(value);
1697 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001698 break;
1699 default:
1700 res = -EINVAL;
1701 }
1702
Allan Stephens0c3141e2008-04-15 00:22:02 -07001703 release_sock(sk);
1704
Per Lidenb97bf3f2006-01-02 19:04:38 +01001705 return res;
1706}
1707
1708/**
1709 * getsockopt - get socket option
1710 * @sock: socket structure
1711 * @lvl: option level
1712 * @opt: option identifier
1713 * @ov: receptacle for option value
1714 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001715 *
1716 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001717 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001718 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719 * Returns 0 on success, errno otherwise
1720 */
1721
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001722static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001723 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001724{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001725 struct sock *sk = sock->sk;
1726 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001727 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001728 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001729 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001730
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001731 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1732 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001733 if (lvl != SOL_TIPC)
1734 return -ENOPROTOOPT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001735 if ((res = get_user(len, ol)))
1736 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001737
Allan Stephens0c3141e2008-04-15 00:22:02 -07001738 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739
1740 switch (opt) {
1741 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001742 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001743 break;
1744 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001745 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746 break;
1747 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001748 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749 break;
1750 case TIPC_CONN_TIMEOUT:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001751 value = jiffies_to_msecs(sk->sk_rcvtimeo);
1752 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001753 break;
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001754 case TIPC_NODE_RECVQ_DEPTH:
1755 value = (u32)atomic_read(&tipc_queue_size);
1756 break;
1757 case TIPC_SOCK_RECVQ_DEPTH:
1758 value = skb_queue_len(&sk->sk_receive_queue);
1759 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001760 default:
1761 res = -EINVAL;
1762 }
1763
Allan Stephens0c3141e2008-04-15 00:22:02 -07001764 release_sock(sk);
1765
Per Lidenb97bf3f2006-01-02 19:04:38 +01001766 if (res) {
1767 /* "get" failed */
1768 }
1769 else if (len < sizeof(value)) {
1770 res = -EINVAL;
1771 }
Pavel Emelyanov653252c2008-04-25 01:49:48 -07001772 else if (copy_to_user(ov, &value, sizeof(value))) {
1773 res = -EFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774 }
1775 else {
1776 res = put_user(sizeof(value), ol);
1777 }
1778
Per Lidenb97bf3f2006-01-02 19:04:38 +01001779 return res;
1780}
1781
1782/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001783 * Protocol switches for the various types of TIPC sockets
1784 */
1785
Florian Westphalbca65ea2008-02-07 18:18:01 -08001786static const struct proto_ops msg_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787 .owner = THIS_MODULE,
1788 .family = AF_TIPC,
1789 .release = release,
1790 .bind = bind,
1791 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001792 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001793 .accept = accept,
1794 .getname = get_name,
1795 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001796 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001797 .listen = listen,
1798 .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 = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001808 .owner = THIS_MODULE,
1809 .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 = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001829 .owner = THIS_MODULE,
1830 .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 = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001850 .owner = THIS_MODULE,
1851 .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) {
Per Lidend0a14a92006-01-11 13:52:51 +01001872 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) {
Per Lidend0a14a92006-01-11 13:52:51 +01001878 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 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001891
Per Liden4323add2006-01-18 00:38:21 +01001892void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001893{
1894 if (!sockets_enabled)
1895 return;
1896
1897 sockets_enabled = 0;
1898 sock_unregister(tipc_family_ops.family);
1899 proto_unregister(&tipc_proto);
1900}
1901