blob: 23a12e44347ff5e7a93d42815dc48c1ed121312d [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
53#include "core.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000054#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010055
56#define SS_LISTENING -1 /* socket is listening */
57#define SS_READY -2 /* socket is connectionless */
58
Allan Stephens3654ea02008-04-13 21:35:11 -070059#define OVERLOAD_LIMIT_BASE 5000
60#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Per Lidenb97bf3f2006-01-02 19:04:38 +010061
62struct tipc_sock {
63 struct sock sk;
64 struct tipc_port *p;
Allan Stephens2da59912008-07-14 22:43:32 -070065 struct tipc_portid peer_name;
Allan Stephens564e83b2010-08-17 11:00:15 +000066 long conn_timeout;
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 Stephens0c3141e2008-04-15 00:22:02 -0700243 sk->sk_backlog_rcv = backlog_rcv;
Allan Stephens0ea52242008-07-14 22:42:19 -0700244 tipc_sk(sk)->p = tp_ptr;
Allan Stephens564e83b2010-08-17 11:00:15 +0000245 tipc_sk(sk)->conn_timeout = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
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
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000398 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700399 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700400 if ((sock->state != SS_CONNECTED) &&
401 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
402 return -ENOTCONN;
403 addr->addr.id.ref = tsock->peer_name.ref;
404 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700405 } else {
Allan Stephens2da59912008-07-14 22:43:32 -0700406 tipc_ownidentity(tsock->p->ref, &addr->addr.id);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700407 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408
409 *uaddr_len = sizeof(*addr);
410 addr->addrtype = TIPC_ADDR_ID;
411 addr->family = AF_TIPC;
412 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413 addr->addr.name.domain = 0;
414
Allan Stephens0c3141e2008-04-15 00:22:02 -0700415 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416}
417
418/**
419 * poll - read and possibly block on pollmask
420 * @file: file structure associated with the socket
421 * @sock: socket for which to calculate the poll bits
422 * @wait: ???
423 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700424 * Returns pollmask value
425 *
426 * COMMENTARY:
427 * It appears that the usual socket locking mechanisms are not useful here
428 * since the pollmask info is potentially out-of-date the moment this routine
429 * exits. TCP and other protocols seem to rely on higher level poll routines
430 * to handle any preventable race conditions, so TIPC will do the same ...
431 *
432 * TIPC sets the returned events as follows:
Allan Stephens9b674e82008-03-26 16:48:21 -0700433 *
Allan Stephensf662c072010-08-17 11:00:06 +0000434 * socket state flags set
435 * ------------ ---------
436 * unconnected no read flags
437 * no write flags
438 *
439 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
440 * no write flags
441 *
442 * connected POLLIN/POLLRDNORM if data in rx queue
443 * POLLOUT if port is not congested
444 *
445 * disconnecting POLLIN/POLLRDNORM/POLLHUP
446 * no write flags
447 *
448 * listening POLLIN if SYN in rx queue
449 * no write flags
450 *
451 * ready POLLIN/POLLRDNORM if data in rx queue
452 * [connectionless] POLLOUT (since port cannot be congested)
453 *
454 * IMPORTANT: The fact that a read or write operation is indicated does NOT
455 * imply that the operation will succeed, merely that it should be performed
456 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 */
458
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900459static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 poll_table *wait)
461{
Allan Stephens9b674e82008-03-26 16:48:21 -0700462 struct sock *sk = sock->sk;
Allan Stephensf662c072010-08-17 11:00:06 +0000463 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700464
Eric Dumazetaa395142010-04-20 13:03:51 +0000465 poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700466
Allan Stephensf662c072010-08-17 11:00:06 +0000467 switch ((int)sock->state) {
468 case SS_READY:
469 case SS_CONNECTED:
470 if (!tipc_sk_port(sk)->congested)
471 mask |= POLLOUT;
472 /* fall thru' */
473 case SS_CONNECTING:
474 case SS_LISTENING:
475 if (!skb_queue_empty(&sk->sk_receive_queue))
476 mask |= (POLLIN | POLLRDNORM);
477 break;
478 case SS_DISCONNECTING:
479 mask = (POLLIN | POLLRDNORM | POLLHUP);
480 break;
481 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700482
483 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484}
485
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900486/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 * dest_name_check - verify user is permitted to send to specified port name
488 * @dest: destination address
489 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900490 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 * Prevents restricted configuration commands from being issued by
492 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900493 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494 * Returns 0 if permission is granted, otherwise errno
495 */
496
Sam Ravnborg05790c62006-03-20 22:37:04 -0800497static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498{
499 struct tipc_cfg_msg_hdr hdr;
500
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900501 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
502 return 0;
503 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
504 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900505 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
506 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900508 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700510 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900512
Per Lidenb97bf3f2006-01-02 19:04:38 +0100513 return 0;
514}
515
516/**
517 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700518 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100519 * @sock: socket structure
520 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700521 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900522 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100523 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900524 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
526 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900527 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528 * Returns the number of bytes sent on success, or errno otherwise
529 */
530
531static int send_msg(struct kiocb *iocb, struct socket *sock,
532 struct msghdr *m, size_t total_len)
533{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700534 struct sock *sk = sock->sk;
535 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900536 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 int needs_conn;
538 int res = -EINVAL;
539
540 if (unlikely(!dest))
541 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700542 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
543 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544 return -EINVAL;
545
Allan Stephens0c3141e2008-04-15 00:22:02 -0700546 if (iocb)
547 lock_sock(sk);
548
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549 needs_conn = (sock->state != SS_READY);
550 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700551 if (sock->state == SS_LISTENING) {
552 res = -EPIPE;
553 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700554 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700555 if (sock->state != SS_UNCONNECTED) {
556 res = -EISCONN;
557 goto exit;
558 }
559 if ((tport->published) ||
560 ((sock->type == SOCK_STREAM) && (total_len != 0))) {
561 res = -EOPNOTSUPP;
562 goto exit;
563 }
564 if (dest->addrtype == TIPC_ADDR_NAME) {
565 tport->conn_type = dest->addr.name.name.type;
566 tport->conn_instance = dest->addr.name.name.instance;
567 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100568
569 /* Abort any pending connection attempts (very unlikely) */
570
Allan Stephens0c3141e2008-04-15 00:22:02 -0700571 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572 }
573
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900574 do {
575 if (dest->addrtype == TIPC_ADDR_NAME) {
576 if ((res = dest_name_check(dest, m)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700577 break;
578 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900579 &dest->addr.name.name,
580 dest->addr.name.domain,
581 m->msg_iovlen,
582 m->msg_iov);
583 }
584 else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700585 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900586 &dest->addr.id,
587 m->msg_iovlen,
588 m->msg_iov);
589 }
590 else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591 if (needs_conn) {
592 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700593 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900595 if ((res = dest_name_check(dest, m)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700596 break;
597 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900598 &dest->addr.nameseq,
599 0,
600 m->msg_iovlen,
601 m->msg_iov);
602 }
603 if (likely(res != -ELINKCONG)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700604 if (needs_conn && (res >= 0)) {
605 sock->state = SS_CONNECTING;
606 }
607 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900608 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 if (m->msg_flags & MSG_DONTWAIT) {
610 res = -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700611 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700613 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +0000614 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -0700615 !tport->congested);
616 lock_sock(sk);
617 if (res)
618 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900619 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700620
621exit:
622 if (iocb)
623 release_sock(sk);
624 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625}
626
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900627/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700629 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630 * @sock: socket structure
631 * @m: message to send
Allan Stephense9024f0f2006-06-25 23:43:57 -0700632 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900633 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900635 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 * Returns the number of bytes sent on success, or errno otherwise
637 */
638
639static int send_packet(struct kiocb *iocb, struct socket *sock,
640 struct msghdr *m, size_t total_len)
641{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700642 struct sock *sk = sock->sk;
643 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900644 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645 int res;
646
647 /* Handle implied connection establishment */
648
649 if (unlikely(dest))
650 return send_msg(iocb, sock, m, total_len);
651
Allan Stephens0c3141e2008-04-15 00:22:02 -0700652 if (iocb)
653 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900655 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700656 if (unlikely(sock->state != SS_CONNECTED)) {
657 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900658 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700659 else
660 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700661 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700662 }
663
Allan Stephens0c3141e2008-04-15 00:22:02 -0700664 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900665 if (likely(res != -ELINKCONG)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700666 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900667 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668 if (m->msg_flags & MSG_DONTWAIT) {
669 res = -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700670 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700672 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +0000673 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -0700674 (!tport->congested || !tport->connected));
675 lock_sock(sk);
676 if (res)
677 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900678 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700679
680 if (iocb)
681 release_sock(sk);
682 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683}
684
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900685/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100686 * send_stream - send stream-oriented data
687 * @iocb: (unused)
688 * @sock: socket structure
689 * @m: data to send
690 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900691 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100692 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900693 *
694 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700695 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100696 */
697
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698static int send_stream(struct kiocb *iocb, struct socket *sock,
699 struct msghdr *m, size_t total_len)
700{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700701 struct sock *sk = sock->sk;
702 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100703 struct msghdr my_msg;
704 struct iovec my_iov;
705 struct iovec *curr_iov;
706 int curr_iovlen;
707 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700708 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100709 int curr_left;
710 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700711 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900713
Allan Stephens0c3141e2008-04-15 00:22:02 -0700714 lock_sock(sk);
715
Allan Stephens05646c92007-06-10 17:25:24 -0700716 /* Handle special cases where there is no connection */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100717
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900718 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700719 if (sock->state == SS_UNCONNECTED) {
720 res = send_packet(NULL, sock, m, total_len);
721 goto exit;
722 } else if (sock->state == SS_DISCONNECTING) {
723 res = -EPIPE;
724 goto exit;
725 } else {
726 res = -ENOTCONN;
727 goto exit;
728 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900729 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730
Allan Stephens0c3141e2008-04-15 00:22:02 -0700731 if (unlikely(m->msg_name)) {
732 res = -EISCONN;
733 goto exit;
734 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700735
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900736 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100737 * Send each iovec entry using one or more messages
738 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900739 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100740 * (i.e. one large iovec entry), but could be improved to pass sets
741 * of small iovec entries into send_packet().
742 */
743
Allan Stephens1303e8f2006-06-25 23:46:50 -0700744 curr_iov = m->msg_iov;
745 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100746 my_msg.msg_iov = &my_iov;
747 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700748 my_msg.msg_flags = m->msg_flags;
749 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700750 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751
Allan Stephens05646c92007-06-10 17:25:24 -0700752 hdr_size = msg_hdr_sz(&tport->phdr);
753
Per Lidenb97bf3f2006-01-02 19:04:38 +0100754 while (curr_iovlen--) {
755 curr_start = curr_iov->iov_base;
756 curr_left = curr_iov->iov_len;
757
758 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700759 bytes_to_send = tport->max_pkt - hdr_size;
760 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
761 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
762 if (curr_left < bytes_to_send)
763 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764 my_iov.iov_base = curr_start;
765 my_iov.iov_len = bytes_to_send;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700766 if ((res = send_packet(NULL, sock, &my_msg, 0)) < 0) {
767 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700768 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700769 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700770 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771 curr_left -= bytes_to_send;
772 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700773 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774 }
775
776 curr_iov++;
777 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700778 res = bytes_sent;
779exit:
780 release_sock(sk);
781 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100782}
783
784/**
785 * auto_connect - complete connection setup to a remote port
786 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100787 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900788 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100789 * Returns 0 on success, errno otherwise
790 */
791
Allan Stephens0c3141e2008-04-15 00:22:02 -0700792static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793{
Allan Stephens2da59912008-07-14 22:43:32 -0700794 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100795
796 if (msg_errcode(msg)) {
797 sock->state = SS_DISCONNECTING;
798 return -ECONNREFUSED;
799 }
800
Allan Stephens2da59912008-07-14 22:43:32 -0700801 tsock->peer_name.ref = msg_origport(msg);
802 tsock->peer_name.node = msg_orignode(msg);
803 tipc_connect2port(tsock->p->ref, &tsock->peer_name);
804 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100805 sock->state = SS_CONNECTED;
806 return 0;
807}
808
809/**
810 * set_orig_addr - capture sender's address for received message
811 * @m: descriptor for message info
812 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900813 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100814 * Note: Address is not captured if not requested by receiver.
815 */
816
Sam Ravnborg05790c62006-03-20 22:37:04 -0800817static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100818{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900819 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100820
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900821 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822 addr->family = AF_TIPC;
823 addr->addrtype = TIPC_ADDR_ID;
824 addr->addr.id.ref = msg_origport(msg);
825 addr->addr.id.node = msg_orignode(msg);
826 addr->addr.name.domain = 0; /* could leave uninitialized */
827 addr->scope = 0; /* could leave uninitialized */
828 m->msg_namelen = sizeof(struct sockaddr_tipc);
829 }
830}
831
832/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900833 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834 * @m: descriptor for message info
835 * @msg: received message header
836 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900837 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100838 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900839 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840 * Returns 0 if successful, otherwise errno
841 */
842
Sam Ravnborg05790c62006-03-20 22:37:04 -0800843static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100844 struct tipc_port *tport)
845{
846 u32 anc_data[3];
847 u32 err;
848 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700849 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100850 int res;
851
852 if (likely(m->msg_controllen == 0))
853 return 0;
854
855 /* Optionally capture errored message object(s) */
856
857 err = msg ? msg_errcode(msg) : 0;
858 if (unlikely(err)) {
859 anc_data[0] = err;
860 anc_data[1] = msg_data_sz(msg);
Allan Stephens4b087b22006-06-25 23:47:44 -0700861 if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100862 return res;
863 if (anc_data[1] &&
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900864 (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865 msg_data(msg))))
866 return res;
867 }
868
869 /* Optionally capture message destination object */
870
871 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
872 switch (dest_type) {
873 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700874 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100875 anc_data[0] = msg_nametype(msg);
876 anc_data[1] = msg_namelower(msg);
877 anc_data[2] = msg_namelower(msg);
878 break;
879 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700880 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100881 anc_data[0] = msg_nametype(msg);
882 anc_data[1] = msg_namelower(msg);
883 anc_data[2] = msg_nameupper(msg);
884 break;
885 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700886 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100887 anc_data[0] = tport->conn_type;
888 anc_data[1] = tport->conn_instance;
889 anc_data[2] = tport->conn_instance;
890 break;
891 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700892 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893 }
Allan Stephens3546c752006-06-25 23:45:24 -0700894 if (has_name &&
Allan Stephens4b087b22006-06-25 23:47:44 -0700895 (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100896 return res;
897
898 return 0;
899}
900
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900901/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902 * recv_msg - receive packet-oriented message
903 * @iocb: (unused)
904 * @m: descriptor for message info
905 * @buf_len: total size of user buffer area
906 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900907 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
909 * If the complete message doesn't fit in user area, truncate it.
910 *
911 * Returns size of returned message data, errno otherwise
912 */
913
914static int recv_msg(struct kiocb *iocb, struct socket *sock,
915 struct msghdr *m, size_t buf_len, int flags)
916{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700917 struct sock *sk = sock->sk;
918 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100919 struct sk_buff *buf;
920 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100921 unsigned int sz;
922 u32 err;
923 int res;
924
Allan Stephens0c3141e2008-04-15 00:22:02 -0700925 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926
927 if (m->msg_iovlen != 1)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700928 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100929
930 if (unlikely(!buf_len))
931 return -EINVAL;
932
Allan Stephens0c3141e2008-04-15 00:22:02 -0700933 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934
Allan Stephens0c3141e2008-04-15 00:22:02 -0700935 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936 res = -ENOTCONN;
937 goto exit;
938 }
939
Allan Stephens0c3141e2008-04-15 00:22:02 -0700940restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100941
Allan Stephens0c3141e2008-04-15 00:22:02 -0700942 /* Look for a message in receive queue; wait if necessary */
943
944 while (skb_queue_empty(&sk->sk_receive_queue)) {
945 if (sock->state == SS_DISCONNECTING) {
946 res = -ENOTCONN;
947 goto exit;
948 }
949 if (flags & MSG_DONTWAIT) {
950 res = -EWOULDBLOCK;
951 goto exit;
952 }
953 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +0000954 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -0700955 (!skb_queue_empty(&sk->sk_receive_queue) ||
956 (sock->state == SS_DISCONNECTING)));
957 lock_sock(sk);
958 if (res)
959 goto exit;
960 }
961
962 /* Look at first message in receive queue */
963
964 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100965 msg = buf_msg(buf);
966 sz = msg_data_sz(msg);
967 err = msg_errcode(msg);
968
969 /* Complete connection setup for an implied connect */
970
971 if (unlikely(sock->state == SS_CONNECTING)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700972 res = auto_connect(sock, msg);
973 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974 goto exit;
975 }
976
977 /* Discard an empty non-errored message & try again */
978
979 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700980 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100981 goto restart;
982 }
983
984 /* Capture sender's address (optional) */
985
986 set_orig_addr(m, msg);
987
988 /* Capture ancillary data (optional) */
989
Allan Stephens0c3141e2008-04-15 00:22:02 -0700990 res = anc_data_recv(m, msg, tport);
991 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100992 goto exit;
993
994 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900995
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996 if (!err) {
997 if (unlikely(buf_len < sz)) {
998 sz = buf_len;
999 m->msg_flags |= MSG_TRUNC;
1000 }
1001 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
1002 sz))) {
1003 res = -EFAULT;
1004 goto exit;
1005 }
1006 res = sz;
1007 } else {
1008 if ((sock->state == SS_READY) ||
1009 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1010 res = 0;
1011 else
1012 res = -ECONNRESET;
1013 }
1014
1015 /* Consume received message (optional) */
1016
1017 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -07001018 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -07001019 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1020 tipc_acknowledge(tport->ref, tport->conn_unacked);
1021 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001022 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001023exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001024 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001025 return res;
1026}
1027
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001028/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 * recv_stream - receive stream-oriented data
1030 * @iocb: (unused)
1031 * @m: descriptor for message info
1032 * @buf_len: total size of user buffer area
1033 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001034 *
1035 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036 * will optionally wait for more; never truncates data.
1037 *
1038 * Returns size of returned message data, errno otherwise
1039 */
1040
1041static int recv_stream(struct kiocb *iocb, struct socket *sock,
1042 struct msghdr *m, size_t buf_len, int flags)
1043{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001044 struct sock *sk = sock->sk;
1045 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046 struct sk_buff *buf;
1047 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001048 unsigned int sz;
Florian Westphal3720d402010-08-17 11:00:04 +00001049 int sz_to_copy, target, needed;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 int sz_copied = 0;
Al Viro28c4dad2006-10-10 22:45:57 +01001051 char __user *crs = m->msg_iov->iov_base;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052 unsigned char *buf_crs;
1053 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001054 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055
1056 /* Catch invalid receive attempts */
1057
Allan Stephens0c3141e2008-04-15 00:22:02 -07001058 if (m->msg_iovlen != 1)
1059 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
1060
Per Lidenb97bf3f2006-01-02 19:04:38 +01001061 if (unlikely(!buf_len))
1062 return -EINVAL;
1063
Allan Stephens0c3141e2008-04-15 00:22:02 -07001064 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001065
Allan Stephens0c3141e2008-04-15 00:22:02 -07001066 if (unlikely((sock->state == SS_UNCONNECTED) ||
1067 (sock->state == SS_CONNECTING))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068 res = -ENOTCONN;
1069 goto exit;
1070 }
1071
Florian Westphal3720d402010-08-17 11:00:04 +00001072 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
1073
Allan Stephens0c3141e2008-04-15 00:22:02 -07001074restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001075
Allan Stephens0c3141e2008-04-15 00:22:02 -07001076 /* Look for a message in receive queue; wait if necessary */
1077
1078 while (skb_queue_empty(&sk->sk_receive_queue)) {
1079 if (sock->state == SS_DISCONNECTING) {
1080 res = -ENOTCONN;
1081 goto exit;
1082 }
1083 if (flags & MSG_DONTWAIT) {
1084 res = -EWOULDBLOCK;
1085 goto exit;
1086 }
1087 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001088 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001089 (!skb_queue_empty(&sk->sk_receive_queue) ||
1090 (sock->state == SS_DISCONNECTING)));
1091 lock_sock(sk);
1092 if (res)
1093 goto exit;
1094 }
1095
1096 /* Look at first message in receive queue */
1097
1098 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001099 msg = buf_msg(buf);
1100 sz = msg_data_sz(msg);
1101 err = msg_errcode(msg);
1102
1103 /* Discard an empty non-errored message & try again */
1104
1105 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001106 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001107 goto restart;
1108 }
1109
1110 /* Optionally capture sender's address & ancillary data of first msg */
1111
1112 if (sz_copied == 0) {
1113 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001114 res = anc_data_recv(m, msg, tport);
1115 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116 goto exit;
1117 }
1118
1119 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001120
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121 if (!err) {
1122 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
Allan Stephens7a8036c2008-04-15 00:15:15 -07001123 sz = (unsigned char *)msg + msg_size(msg) - buf_crs;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124
1125 needed = (buf_len - sz_copied);
1126 sz_to_copy = (sz <= needed) ? sz : needed;
1127 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1128 res = -EFAULT;
1129 goto exit;
1130 }
1131 sz_copied += sz_to_copy;
1132
1133 if (sz_to_copy < sz) {
1134 if (!(flags & MSG_PEEK))
1135 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1136 goto exit;
1137 }
1138
1139 crs += sz_to_copy;
1140 } else {
1141 if (sz_copied != 0)
1142 goto exit; /* can't add error msg to valid data */
1143
1144 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1145 res = 0;
1146 else
1147 res = -ECONNRESET;
1148 }
1149
1150 /* Consume received message (optional) */
1151
1152 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001153 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1154 tipc_acknowledge(tport->ref, tport->conn_unacked);
1155 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001156 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001157
1158 /* Loop around if more data is required */
1159
Joe Perchesf64f9e72009-11-29 16:55:45 -08001160 if ((sz_copied < buf_len) && /* didn't get all requested data */
1161 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001162 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001163 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1164 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001165 goto restart;
1166
1167exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001168 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001169 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170}
1171
1172/**
Allan Stephens1819b832008-04-15 00:15:50 -07001173 * rx_queue_full - determine if receive queue can accept another message
1174 * @msg: message to be added to queue
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175 * @queue_size: current size of queue
1176 * @base: nominal maximum size of queue
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001177 *
Allan Stephens1819b832008-04-15 00:15:50 -07001178 * Returns 1 if queue is unable to accept message, 0 otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179 */
1180
Allan Stephens1819b832008-04-15 00:15:50 -07001181static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182{
1183 u32 threshold;
1184 u32 imp = msg_importance(msg);
1185
1186 if (imp == TIPC_LOW_IMPORTANCE)
1187 threshold = base;
1188 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1189 threshold = base * 2;
1190 else if (imp == TIPC_HIGH_IMPORTANCE)
1191 threshold = base * 100;
1192 else
1193 return 0;
1194
1195 if (msg_connected(msg))
1196 threshold *= 4;
1197
Eric Dumazeta02cec22010-09-22 20:43:57 +00001198 return queue_size >= threshold;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199}
1200
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001201/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001202 * filter_rcv - validate incoming message
1203 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001204 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001205 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001206 * Enqueues message on receive queue if acceptable; optionally handles
1207 * disconnect indication for a connected socket.
1208 *
1209 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001210 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001211 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1212 */
1213
Allan Stephens0c3141e2008-04-15 00:22:02 -07001214static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001216 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001217 struct tipc_msg *msg = buf_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218 u32 recv_q_len;
1219
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 /* Reject message if it is wrong sort of message for socket */
1221
1222 /*
1223 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1224 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1225 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1226 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001227
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 if (sock->state == SS_READY) {
1229 if (msg_connected(msg)) {
1230 msg_dbg(msg, "dispatch filter 1\n");
1231 return TIPC_ERR_NO_PORT;
1232 }
1233 } else {
1234 if (msg_mcast(msg)) {
1235 msg_dbg(msg, "dispatch filter 2\n");
1236 return TIPC_ERR_NO_PORT;
1237 }
1238 if (sock->state == SS_CONNECTED) {
1239 if (!msg_connected(msg)) {
1240 msg_dbg(msg, "dispatch filter 3\n");
1241 return TIPC_ERR_NO_PORT;
1242 }
1243 }
1244 else if (sock->state == SS_CONNECTING) {
1245 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1246 msg_dbg(msg, "dispatch filter 4\n");
1247 return TIPC_ERR_NO_PORT;
1248 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001249 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001250 else if (sock->state == SS_LISTENING) {
1251 if (msg_connected(msg) || msg_errcode(msg)) {
1252 msg_dbg(msg, "dispatch filter 5\n");
1253 return TIPC_ERR_NO_PORT;
1254 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001255 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001256 else if (sock->state == SS_DISCONNECTING) {
1257 msg_dbg(msg, "dispatch filter 6\n");
1258 return TIPC_ERR_NO_PORT;
1259 }
1260 else /* (sock->state == SS_UNCONNECTED) */ {
1261 if (msg_connected(msg) || msg_errcode(msg)) {
1262 msg_dbg(msg, "dispatch filter 7\n");
1263 return TIPC_ERR_NO_PORT;
1264 }
1265 }
1266 }
1267
1268 /* Reject message if there isn't room to queue it */
1269
Allan Stephens1819b832008-04-15 00:15:50 -07001270 recv_q_len = (u32)atomic_read(&tipc_queue_size);
1271 if (unlikely(recv_q_len >= OVERLOAD_LIMIT_BASE)) {
1272 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001273 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001274 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001275 recv_q_len = skb_queue_len(&sk->sk_receive_queue);
Allan Stephens1819b832008-04-15 00:15:50 -07001276 if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
1277 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001278 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001279 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001280
Allan Stephens0c3141e2008-04-15 00:22:02 -07001281 /* Enqueue message (finally!) */
1282
1283 msg_dbg(msg, "<DISP<: ");
1284 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1285 atomic_inc(&tipc_queue_size);
1286 __skb_queue_tail(&sk->sk_receive_queue, buf);
1287
Per Lidenb97bf3f2006-01-02 19:04:38 +01001288 /* Initiate connection termination for an incoming 'FIN' */
1289
1290 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1291 sock->state = SS_DISCONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001292 tipc_disconnect_port(tipc_sk_port(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293 }
1294
Eric Dumazetaa395142010-04-20 13:03:51 +00001295 if (waitqueue_active(sk_sleep(sk)))
1296 wake_up_interruptible(sk_sleep(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001297 return TIPC_OK;
1298}
1299
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001300/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001301 * backlog_rcv - handle incoming message from backlog queue
1302 * @sk: socket
1303 * @buf: message
1304 *
1305 * Caller must hold socket lock, but not port lock.
1306 *
1307 * Returns 0
1308 */
1309
1310static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1311{
1312 u32 res;
1313
1314 res = filter_rcv(sk, buf);
1315 if (res)
1316 tipc_reject_msg(buf, res);
1317 return 0;
1318}
1319
1320/**
1321 * dispatch - handle incoming message
1322 * @tport: TIPC port that received message
1323 * @buf: message
1324 *
1325 * Called with port lock already taken.
1326 *
1327 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1328 */
1329
1330static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1331{
1332 struct sock *sk = (struct sock *)tport->usr_handle;
1333 u32 res;
1334
1335 /*
1336 * Process message if socket is unlocked; otherwise add to backlog queue
1337 *
1338 * This code is based on sk_receive_skb(), but must be distinct from it
1339 * since a TIPC-specific filter/reject mechanism is utilized
1340 */
1341
1342 bh_lock_sock(sk);
1343 if (!sock_owned_by_user(sk)) {
1344 res = filter_rcv(sk, buf);
1345 } else {
Zhu Yia3a858f2010-03-04 18:01:47 +00001346 if (sk_add_backlog(sk, buf))
Zhu Yi53eecb12010-03-04 18:01:45 +00001347 res = TIPC_ERR_OVERLOAD;
1348 else
1349 res = TIPC_OK;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001350 }
1351 bh_unlock_sock(sk);
1352
1353 return res;
1354}
1355
1356/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357 * wakeupdispatch - wake up port after congestion
1358 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001359 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001360 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 */
1362
1363static void wakeupdispatch(struct tipc_port *tport)
1364{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001365 struct sock *sk = (struct sock *)tport->usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366
Eric Dumazetaa395142010-04-20 13:03:51 +00001367 if (waitqueue_active(sk_sleep(sk)))
1368 wake_up_interruptible(sk_sleep(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001369}
1370
1371/**
1372 * connect - establish a connection to another TIPC port
1373 * @sock: socket structure
1374 * @dest: socket address for destination port
1375 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001376 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001377 *
1378 * Returns 0 on success, errno otherwise
1379 */
1380
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001381static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001382 int flags)
1383{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001384 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001385 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1386 struct msghdr m = {NULL,};
1387 struct sk_buff *buf;
1388 struct tipc_msg *msg;
Allan Stephens564e83b2010-08-17 11:00:15 +00001389 long timeout;
Allan Stephensb89741a2008-04-15 00:20:37 -07001390 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001391
Allan Stephens0c3141e2008-04-15 00:22:02 -07001392 lock_sock(sk);
1393
Allan Stephensb89741a2008-04-15 00:20:37 -07001394 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001395
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 /* For now, TIPC does not support the non-blocking form of connect() */
Allan Stephens4934c692008-04-15 00:16:19 -07001402
Allan Stephens0c3141e2008-04-15 00:22:02 -07001403 if (flags & O_NONBLOCK) {
Allan Stephens35997e32010-08-17 11:00:05 +00001404 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001405 goto exit;
1406 }
Allan Stephens4934c692008-04-15 00:16:19 -07001407
Allan Stephensb89741a2008-04-15 00:20:37 -07001408 /* Issue Posix-compliant error code if socket is in the wrong state */
Allan Stephens51f9cc12006-06-25 23:49:06 -07001409
Allan Stephens0c3141e2008-04-15 00:22:02 -07001410 if (sock->state == SS_LISTENING) {
1411 res = -EOPNOTSUPP;
1412 goto exit;
1413 }
1414 if (sock->state == SS_CONNECTING) {
1415 res = -EALREADY;
1416 goto exit;
1417 }
1418 if (sock->state != SS_UNCONNECTED) {
1419 res = -EISCONN;
1420 goto exit;
1421 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422
Allan Stephensb89741a2008-04-15 00:20:37 -07001423 /*
1424 * Reject connection attempt using multicast address
1425 *
1426 * Note: send_msg() validates the rest of the address fields,
1427 * so there's no need to do it here
1428 */
Allan Stephens51f9cc12006-06-25 23:49:06 -07001429
Allan Stephens0c3141e2008-04-15 00:22:02 -07001430 if (dst->addrtype == TIPC_ADDR_MCAST) {
1431 res = -EINVAL;
1432 goto exit;
1433 }
1434
1435 /* Reject any messages already in receive queue (very unlikely) */
1436
1437 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001438
Allan Stephensb89741a2008-04-15 00:20:37 -07001439 /* Send a 'SYN-' to destination */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001440
Allan Stephensb89741a2008-04-15 00:20:37 -07001441 m.msg_name = dest;
1442 m.msg_namelen = destlen;
1443 res = send_msg(NULL, sock, &m, 0);
1444 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001445 goto exit;
Allan Stephensb89741a2008-04-15 00:20:37 -07001446 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001447
Allan Stephens0c3141e2008-04-15 00:22:02 -07001448 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001449
Allan Stephens564e83b2010-08-17 11:00:15 +00001450 timeout = tipc_sk(sk)->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001451 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001452 res = wait_event_interruptible_timeout(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001453 (!skb_queue_empty(&sk->sk_receive_queue) ||
1454 (sock->state != SS_CONNECTING)),
Allan Stephens564e83b2010-08-17 11:00:15 +00001455 timeout ? timeout : MAX_SCHEDULE_TIMEOUT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001456 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001457
Allan Stephensb89741a2008-04-15 00:20:37 -07001458 if (res > 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001459 buf = skb_peek(&sk->sk_receive_queue);
1460 if (buf != NULL) {
1461 msg = buf_msg(buf);
1462 res = auto_connect(sock, msg);
1463 if (!res) {
1464 if (!msg_data_sz(msg))
1465 advance_rx_queue(sk);
1466 }
1467 } else {
1468 if (sock->state == SS_CONNECTED) {
1469 res = -EISCONN;
1470 } else {
1471 res = -ECONNREFUSED;
1472 }
Allan Stephensb89741a2008-04-15 00:20:37 -07001473 }
1474 } else {
1475 if (res == 0)
1476 res = -ETIMEDOUT;
1477 else
1478 ; /* leave "res" unchanged */
1479 sock->state = SS_DISCONNECTING;
1480 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001481
Allan Stephens0c3141e2008-04-15 00:22:02 -07001482exit:
1483 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001484 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001485}
1486
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001487/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488 * listen - allow socket to listen for incoming connections
1489 * @sock: socket structure
1490 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001491 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001492 * Returns 0 on success, errno otherwise
1493 */
1494
1495static int listen(struct socket *sock, int len)
1496{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001497 struct sock *sk = sock->sk;
1498 int res;
1499
1500 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001501
1502 if (sock->state == SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001503 res = -EOPNOTSUPP;
1504 else if (sock->state != SS_UNCONNECTED)
1505 res = -EINVAL;
1506 else {
1507 sock->state = SS_LISTENING;
1508 res = 0;
1509 }
1510
1511 release_sock(sk);
1512 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001513}
1514
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001515/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001516 * accept - wait for connection request
1517 * @sock: listening socket
1518 * @newsock: new socket that is to be connected
1519 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001520 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001521 * Returns 0 on success, errno otherwise
1522 */
1523
Allan Stephens0c3141e2008-04-15 00:22:02 -07001524static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001525{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001526 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001527 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001528 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001529
Allan Stephens0c3141e2008-04-15 00:22:02 -07001530 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001531
Allan Stephens0c3141e2008-04-15 00:22:02 -07001532 if (sock->state == SS_READY) {
1533 res = -EOPNOTSUPP;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001534 goto exit;
1535 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001536 if (sock->state != SS_LISTENING) {
1537 res = -EINVAL;
1538 goto exit;
1539 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001540
Allan Stephens0c3141e2008-04-15 00:22:02 -07001541 while (skb_queue_empty(&sk->sk_receive_queue)) {
1542 if (flags & O_NONBLOCK) {
1543 res = -EWOULDBLOCK;
1544 goto exit;
1545 }
1546 release_sock(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001547 res = wait_event_interruptible(*sk_sleep(sk),
Allan Stephens0c3141e2008-04-15 00:22:02 -07001548 (!skb_queue_empty(&sk->sk_receive_queue)));
1549 lock_sock(sk);
1550 if (res)
1551 goto exit;
1552 }
1553
1554 buf = skb_peek(&sk->sk_receive_queue);
1555
Eric Paris3f378b62009-11-05 22:18:14 -08001556 res = tipc_create(sock_net(sock->sk), new_sock, 0, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001557 if (!res) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001558 struct sock *new_sk = new_sock->sk;
Allan Stephens2da59912008-07-14 22:43:32 -07001559 struct tipc_sock *new_tsock = tipc_sk(new_sk);
1560 struct tipc_port *new_tport = new_tsock->p;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001561 u32 new_ref = new_tport->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001562 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001563
1564 lock_sock(new_sk);
1565
1566 /*
1567 * Reject any stray messages received by new socket
1568 * before the socket lock was taken (very, very unlikely)
1569 */
1570
1571 reject_rx_queue(new_sk);
1572
1573 /* Connect new socket to it's peer */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574
Allan Stephens2da59912008-07-14 22:43:32 -07001575 new_tsock->peer_name.ref = msg_origport(msg);
1576 new_tsock->peer_name.node = msg_orignode(msg);
1577 tipc_connect2port(new_ref, &new_tsock->peer_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001578 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001579
1580 tipc_set_portimportance(new_ref, msg_importance(msg));
1581 if (msg_named(msg)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001582 new_tport->conn_type = msg_nametype(msg);
1583 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001584 }
1585
Allan Stephens0c3141e2008-04-15 00:22:02 -07001586 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001587 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1588 * Respond to 'SYN+' by queuing it on new socket.
1589 */
1590
1591 msg_dbg(msg,"<ACC<: ");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001592 if (!msg_data_sz(msg)) {
1593 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001594
Allan Stephens0c3141e2008-04-15 00:22:02 -07001595 advance_rx_queue(sk);
1596 send_packet(NULL, new_sock, &m, 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001597 } else {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001598 __skb_dequeue(&sk->sk_receive_queue);
1599 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001601 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001602 }
1603exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001604 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001605 return res;
1606}
1607
1608/**
1609 * shutdown - shutdown socket connection
1610 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001611 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001612 *
1613 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001614 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001615 * Returns 0 on success, errno otherwise
1616 */
1617
1618static int shutdown(struct socket *sock, int how)
1619{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001620 struct sock *sk = sock->sk;
1621 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001622 struct sk_buff *buf;
1623 int res;
1624
Allan Stephense247a8f2008-03-06 15:05:38 -08001625 if (how != SHUT_RDWR)
1626 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001627
Allan Stephens0c3141e2008-04-15 00:22:02 -07001628 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001629
1630 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001631 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001632 case SS_CONNECTED:
1633
Allan Stephens0c3141e2008-04-15 00:22:02 -07001634 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001635restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001636 buf = __skb_dequeue(&sk->sk_receive_queue);
1637 if (buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001638 atomic_dec(&tipc_queue_size);
1639 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1640 buf_discard(buf);
1641 goto restart;
1642 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001643 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001644 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001645 } else {
1646 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001647 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001648
1649 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001650
1651 /* fall through */
1652
1653 case SS_DISCONNECTING:
1654
Allan Stephens0c3141e2008-04-15 00:22:02 -07001655 /* Discard any unreceived messages; wake up sleeping tasks */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656
Allan Stephens0c3141e2008-04-15 00:22:02 -07001657 discard_rx_queue(sk);
Eric Dumazetaa395142010-04-20 13:03:51 +00001658 if (waitqueue_active(sk_sleep(sk)))
1659 wake_up_interruptible(sk_sleep(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001660 res = 0;
1661 break;
1662
1663 default:
1664 res = -ENOTCONN;
1665 }
1666
Allan Stephens0c3141e2008-04-15 00:22:02 -07001667 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001668 return res;
1669}
1670
1671/**
1672 * setsockopt - set socket option
1673 * @sock: socket structure
1674 * @lvl: option level
1675 * @opt: option identifier
1676 * @ov: pointer to new option value
1677 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001678 *
1679 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001680 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001681 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001682 * Returns 0 on success, errno otherwise
1683 */
1684
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001685static int setsockopt(struct socket *sock,
David S. Millerb7058842009-09-30 16:12:20 -07001686 int lvl, int opt, char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001687{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001688 struct sock *sk = sock->sk;
1689 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690 u32 value;
1691 int res;
1692
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001693 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1694 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001695 if (lvl != SOL_TIPC)
1696 return -ENOPROTOOPT;
1697 if (ol < sizeof(value))
1698 return -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001699 if ((res = get_user(value, (u32 __user *)ov)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001700 return res;
1701
Allan Stephens0c3141e2008-04-15 00:22:02 -07001702 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001703
Per Lidenb97bf3f2006-01-02 19:04:38 +01001704 switch (opt) {
1705 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001706 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001707 break;
1708 case TIPC_SRC_DROPPABLE:
1709 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001710 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001711 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001712 res = -ENOPROTOOPT;
1713 break;
1714 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001715 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001716 break;
1717 case TIPC_CONN_TIMEOUT:
Allan Stephens564e83b2010-08-17 11:00:15 +00001718 tipc_sk(sk)->conn_timeout = msecs_to_jiffies(value);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001719 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001720 break;
1721 default:
1722 res = -EINVAL;
1723 }
1724
Allan Stephens0c3141e2008-04-15 00:22:02 -07001725 release_sock(sk);
1726
Per Lidenb97bf3f2006-01-02 19:04:38 +01001727 return res;
1728}
1729
1730/**
1731 * getsockopt - get socket option
1732 * @sock: socket structure
1733 * @lvl: option level
1734 * @opt: option identifier
1735 * @ov: receptacle for option value
1736 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001737 *
1738 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001739 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001740 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001741 * Returns 0 on success, errno otherwise
1742 */
1743
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001744static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001745 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001747 struct sock *sk = sock->sk;
1748 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001749 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001750 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001751 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001752
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001753 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1754 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001755 if (lvl != SOL_TIPC)
1756 return -ENOPROTOOPT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001757 if ((res = get_user(len, ol)))
1758 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001759
Allan Stephens0c3141e2008-04-15 00:22:02 -07001760 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001761
1762 switch (opt) {
1763 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001764 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001765 break;
1766 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001767 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001768 break;
1769 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001770 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001771 break;
1772 case TIPC_CONN_TIMEOUT:
Allan Stephens564e83b2010-08-17 11:00:15 +00001773 value = jiffies_to_msecs(tipc_sk(sk)->conn_timeout);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001774 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001775 break;
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00001776 case TIPC_NODE_RECVQ_DEPTH:
1777 value = (u32)atomic_read(&tipc_queue_size);
1778 break;
1779 case TIPC_SOCK_RECVQ_DEPTH:
1780 value = skb_queue_len(&sk->sk_receive_queue);
1781 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001782 default:
1783 res = -EINVAL;
1784 }
1785
Allan Stephens0c3141e2008-04-15 00:22:02 -07001786 release_sock(sk);
1787
Per Lidenb97bf3f2006-01-02 19:04:38 +01001788 if (res) {
1789 /* "get" failed */
1790 }
1791 else if (len < sizeof(value)) {
1792 res = -EINVAL;
1793 }
Pavel Emelyanov653252c2008-04-25 01:49:48 -07001794 else if (copy_to_user(ov, &value, sizeof(value))) {
1795 res = -EFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001796 }
1797 else {
1798 res = put_user(sizeof(value), ol);
1799 }
1800
Per Lidenb97bf3f2006-01-02 19:04:38 +01001801 return res;
1802}
1803
1804/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001805 * Protocol switches for the various types of TIPC sockets
1806 */
1807
Florian Westphalbca65ea2008-02-07 18:18:01 -08001808static const struct proto_ops msg_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001809 .owner = THIS_MODULE,
1810 .family = AF_TIPC,
1811 .release = release,
1812 .bind = bind,
1813 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001814 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001815 .accept = accept,
1816 .getname = get_name,
1817 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001818 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001819 .listen = listen,
1820 .shutdown = shutdown,
1821 .setsockopt = setsockopt,
1822 .getsockopt = getsockopt,
1823 .sendmsg = send_msg,
1824 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001825 .mmap = sock_no_mmap,
1826 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001827};
1828
Florian Westphalbca65ea2008-02-07 18:18:01 -08001829static const struct proto_ops packet_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001830 .owner = THIS_MODULE,
1831 .family = AF_TIPC,
1832 .release = release,
1833 .bind = bind,
1834 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001835 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001836 .accept = accept,
1837 .getname = get_name,
1838 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001839 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001840 .listen = listen,
1841 .shutdown = shutdown,
1842 .setsockopt = setsockopt,
1843 .getsockopt = getsockopt,
1844 .sendmsg = send_packet,
1845 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001846 .mmap = sock_no_mmap,
1847 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001848};
1849
Florian Westphalbca65ea2008-02-07 18:18:01 -08001850static const struct proto_ops stream_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001851 .owner = THIS_MODULE,
1852 .family = AF_TIPC,
1853 .release = release,
1854 .bind = bind,
1855 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001856 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001857 .accept = accept,
1858 .getname = get_name,
1859 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001860 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001861 .listen = listen,
1862 .shutdown = shutdown,
1863 .setsockopt = setsockopt,
1864 .getsockopt = getsockopt,
1865 .sendmsg = send_stream,
1866 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001867 .mmap = sock_no_mmap,
1868 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001869};
1870
Florian Westphalbca65ea2008-02-07 18:18:01 -08001871static const struct net_proto_family tipc_family_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001872 .owner = THIS_MODULE,
1873 .family = AF_TIPC,
1874 .create = tipc_create
1875};
1876
1877static struct proto tipc_proto = {
1878 .name = "TIPC",
1879 .owner = THIS_MODULE,
1880 .obj_size = sizeof(struct tipc_sock)
1881};
1882
1883/**
Per Liden4323add2006-01-18 00:38:21 +01001884 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001885 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886 * Returns 0 on success, errno otherwise
1887 */
Per Liden4323add2006-01-18 00:38:21 +01001888int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001889{
1890 int res;
1891
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001892 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001893 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001894 err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001895 goto out;
1896 }
1897
1898 res = sock_register(&tipc_family_ops);
1899 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001900 err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001901 proto_unregister(&tipc_proto);
1902 goto out;
1903 }
1904
1905 sockets_enabled = 1;
1906 out:
1907 return res;
1908}
1909
1910/**
Per Liden4323add2006-01-18 00:38:21 +01001911 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001912 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001913
Per Liden4323add2006-01-18 00:38:21 +01001914void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001915{
1916 if (!sockets_enabled)
1917 return;
1918
1919 sockets_enabled = 0;
1920 sock_unregister(tipc_family_ops.family);
1921 proto_unregister(&tipc_proto);
1922}
1923