blob: 1848693ebb82d49e412f91a94809896bf2add529 [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>
43#include <linux/slab.h>
44#include <linux/poll.h>
Per Lidenb97bf3f2006-01-02 19:04:38 +010045#include <linux/fcntl.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)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900180 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700181 * This routine creates additional data structures used by the TIPC socket,
182 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 *
184 * Returns 0 on success, errno otherwise
185 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700186
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700187static int tipc_create(struct net *net, struct socket *sock, int protocol)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100188{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700189 const struct proto_ops *ops;
190 socket_state state;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191 struct sock *sk;
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700192 struct tipc_port *tp_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700193
194 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100195
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700196 if (net != &init_net)
197 return -EAFNOSUPPORT;
198
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199 if (unlikely(protocol != 0))
200 return -EPROTONOSUPPORT;
201
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202 switch (sock->type) {
203 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700204 ops = &stream_ops;
205 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206 break;
207 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700208 ops = &packet_ops;
209 state = SS_UNCONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210 break;
211 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700213 ops = &msg_ops;
214 state = SS_READY;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215 break;
Allan Stephens49978652006-06-25 23:47:18 -0700216 default:
Allan Stephens49978652006-06-25 23:47:18 -0700217 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218 }
219
Allan Stephens0c3141e2008-04-15 00:22:02 -0700220 /* Allocate socket's protocol area */
221
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700222 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700223 if (sk == NULL)
224 return -ENOMEM;
225
226 /* Allocate TIPC port for socket to use */
227
Allan Stephens0ea52242008-07-14 22:42:19 -0700228 tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
229 TIPC_LOW_IMPORTANCE);
230 if (unlikely(!tp_ptr)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700231 sk_free(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 return -ENOMEM;
233 }
234
Allan Stephens0c3141e2008-04-15 00:22:02 -0700235 /* Finish initializing socket data structures */
236
237 sock->ops = ops;
238 sock->state = state;
239
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240 sock_init_data(sock, sk);
Allan Stephens3654ea02008-04-13 21:35:11 -0700241 sk->sk_rcvtimeo = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700242 sk->sk_backlog_rcv = backlog_rcv;
Allan Stephens0ea52242008-07-14 22:42:19 -0700243 tipc_sk(sk)->p = tp_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700245 spin_unlock_bh(tp_ptr->lock);
246
Allan Stephens0c3141e2008-04-15 00:22:02 -0700247 if (sock->state == SS_READY) {
Allan Stephens0ea52242008-07-14 22:42:19 -0700248 tipc_set_portunreturnable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700249 if (sock->type == SOCK_DGRAM)
Allan Stephens0ea52242008-07-14 22:42:19 -0700250 tipc_set_portunreliable(tp_ptr->ref, 1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700251 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252
253 atomic_inc(&tipc_user_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254 return 0;
255}
256
257/**
258 * release - destroy a TIPC socket
259 * @sock: socket to destroy
260 *
261 * This routine cleans up any messages that are still queued on the socket.
262 * For DGRAM and RDM socket types, all queued messages are rejected.
263 * For SEQPACKET and STREAM socket types, the first message is rejected
264 * and any others are discarded. (If the first message on a STREAM socket
265 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900266 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100267 * NOTE: Rejected messages are not necessarily returned to the sender! They
268 * are returned or discarded according to the "destination droppable" setting
269 * specified for the message by the sender.
270 *
271 * Returns 0 on success, errno otherwise
272 */
273
274static int release(struct socket *sock)
275{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276 struct sock *sk = sock->sk;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700277 struct tipc_port *tport;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700279 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280
Allan Stephens0c3141e2008-04-15 00:22:02 -0700281 /*
282 * Exit if socket isn't fully initialized (occurs when a failed accept()
283 * releases a pre-allocated child socket that was never used)
284 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900285
Allan Stephens0c3141e2008-04-15 00:22:02 -0700286 if (sk == NULL)
287 return 0;
288
289 tport = tipc_sk_port(sk);
290 lock_sock(sk);
291
292 /*
293 * Reject all unreceived messages, except on an active connection
294 * (which disconnects locally & sends a 'FIN+' to peer)
295 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100296
297 while (sock->state != SS_DISCONNECTING) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700298 buf = __skb_dequeue(&sk->sk_receive_queue);
299 if (buf == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300 break;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700301 atomic_dec(&tipc_queue_size);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
303 buf_discard(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700304 else {
305 if ((sock->state == SS_CONNECTING) ||
306 (sock->state == SS_CONNECTED)) {
307 sock->state = SS_DISCONNECTING;
308 tipc_disconnect(tport->ref);
309 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700311 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 }
313
Allan Stephens0c3141e2008-04-15 00:22:02 -0700314 /*
315 * Delete TIPC port; this ensures no more messages are queued
316 * (also disconnects an active connection & sends a 'FIN-' to peer)
317 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318
Allan Stephens0c3141e2008-04-15 00:22:02 -0700319 res = tipc_deleteport(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320
Allan Stephens0c3141e2008-04-15 00:22:02 -0700321 /* Discard any remaining (connection-based) messages in receive queue */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322
Allan Stephens0c3141e2008-04-15 00:22:02 -0700323 discard_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324
Allan Stephens0c3141e2008-04-15 00:22:02 -0700325 /* Reject any messages that accumulated in backlog queue */
326
327 sock->state = SS_DISCONNECTING;
328 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329
330 sock_put(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700331 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900333 atomic_dec(&tipc_user_count);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100334 return res;
335}
336
337/**
338 * bind - associate or disassocate TIPC name(s) with a socket
339 * @sock: socket structure
340 * @uaddr: socket address describing name(s) and desired operation
341 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900342 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 * Name and name sequence binding is indicated using a positive scope value;
344 * a negative scope value unbinds the specified name. Specifying no name
345 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900346 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700348 *
349 * NOTE: This routine doesn't need to take the socket lock since it doesn't
350 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 */
352
353static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
354{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700356 u32 portref = tipc_sk_port(sock->sk)->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357
Allan Stephens0c3141e2008-04-15 00:22:02 -0700358 if (unlikely(!uaddr_len))
359 return tipc_withdraw(portref, 0, NULL);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900360
Allan Stephens0c3141e2008-04-15 00:22:02 -0700361 if (uaddr_len < sizeof(struct sockaddr_tipc))
362 return -EINVAL;
363 if (addr->family != AF_TIPC)
364 return -EAFNOSUPPORT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100365
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 if (addr->addrtype == TIPC_ADDR_NAME)
367 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700368 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
369 return -EAFNOSUPPORT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900370
Allan Stephens0c3141e2008-04-15 00:22:02 -0700371 return (addr->scope > 0) ?
372 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
373 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374}
375
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900376/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377 * get_name - get port ID of socket or peer socket
378 * @sock: socket structure
379 * @uaddr: area for returned socket address
380 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700381 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900382 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700384 *
Allan Stephens2da59912008-07-14 22:43:32 -0700385 * NOTE: This routine doesn't need to take the socket lock since it only
386 * accesses socket information that is unchanging (or which changes in
387 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388 */
389
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900390static int get_name(struct socket *sock, struct sockaddr *uaddr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 int *uaddr_len, int peer)
392{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Allan Stephens2da59912008-07-14 22:43:32 -0700394 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395
Allan Stephens0c3141e2008-04-15 00:22:02 -0700396 if (peer) {
Allan Stephens2da59912008-07-14 22:43:32 -0700397 if ((sock->state != SS_CONNECTED) &&
398 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
399 return -ENOTCONN;
400 addr->addr.id.ref = tsock->peer_name.ref;
401 addr->addr.id.node = tsock->peer_name.node;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700402 } else {
Allan Stephens2da59912008-07-14 22:43:32 -0700403 tipc_ownidentity(tsock->p->ref, &addr->addr.id);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700404 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100405
406 *uaddr_len = sizeof(*addr);
407 addr->addrtype = TIPC_ADDR_ID;
408 addr->family = AF_TIPC;
409 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 addr->addr.name.domain = 0;
411
Allan Stephens0c3141e2008-04-15 00:22:02 -0700412 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100413}
414
415/**
416 * poll - read and possibly block on pollmask
417 * @file: file structure associated with the socket
418 * @sock: socket for which to calculate the poll bits
419 * @wait: ???
420 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700421 * Returns pollmask value
422 *
423 * COMMENTARY:
424 * It appears that the usual socket locking mechanisms are not useful here
425 * since the pollmask info is potentially out-of-date the moment this routine
426 * exits. TCP and other protocols seem to rely on higher level poll routines
427 * to handle any preventable race conditions, so TIPC will do the same ...
428 *
429 * TIPC sets the returned events as follows:
430 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
431 * or if a connection-oriented socket is does not have an active connection
432 * (i.e. a read operation will not block).
433 * b) POLLOUT is set except when a socket's connection has been terminated
434 * (i.e. a write operation will not block).
435 * c) POLLHUP is set when a socket's connection has been terminated.
436 *
437 * IMPORTANT: The fact that a read or write operation will not block does NOT
438 * imply that the operation will succeed!
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439 */
440
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900441static unsigned int poll(struct file *file, struct socket *sock,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 poll_table *wait)
443{
Allan Stephens9b674e82008-03-26 16:48:21 -0700444 struct sock *sk = sock->sk;
445 u32 mask;
446
447 poll_wait(file, sk->sk_sleep, wait);
448
449 if (!skb_queue_empty(&sk->sk_receive_queue) ||
450 (sock->state == SS_UNCONNECTED) ||
451 (sock->state == SS_DISCONNECTING))
452 mask = (POLLRDNORM | POLLIN);
453 else
454 mask = 0;
455
456 if (sock->state == SS_DISCONNECTING)
457 mask |= POLLHUP;
458 else
459 mask |= POLLOUT;
460
461 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462}
463
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900464/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465 * dest_name_check - verify user is permitted to send to specified port name
466 * @dest: destination address
467 * @m: descriptor for message to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900468 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469 * Prevents restricted configuration commands from being issued by
470 * unauthorized users.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900471 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472 * Returns 0 if permission is granted, otherwise errno
473 */
474
Sam Ravnborg05790c62006-03-20 22:37:04 -0800475static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476{
477 struct tipc_cfg_msg_hdr hdr;
478
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900479 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
480 return 0;
481 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
482 return 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900483 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
484 return -EACCES;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900486 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487 return -EFAULT;
Allan Stephens70cb2342006-06-25 23:41:47 -0700488 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489 return -EACCES;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900490
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 return 0;
492}
493
494/**
495 * send_msg - send message in connectionless manner
Allan Stephens0c3141e2008-04-15 00:22:02 -0700496 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 * @sock: socket structure
498 * @m: message to send
Allan Stephense9024f02006-06-25 23:43:57 -0700499 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900500 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900502 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
504 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900505 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 * Returns the number of bytes sent on success, or errno otherwise
507 */
508
509static int send_msg(struct kiocb *iocb, struct socket *sock,
510 struct msghdr *m, size_t total_len)
511{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700512 struct sock *sk = sock->sk;
513 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900514 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515 int needs_conn;
516 int res = -EINVAL;
517
518 if (unlikely(!dest))
519 return -EDESTADDRREQ;
Allan Stephens51f9cc12006-06-25 23:49:06 -0700520 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
521 (dest->family != AF_TIPC)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522 return -EINVAL;
523
Allan Stephens0c3141e2008-04-15 00:22:02 -0700524 if (iocb)
525 lock_sock(sk);
526
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527 needs_conn = (sock->state != SS_READY);
528 if (unlikely(needs_conn)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700529 if (sock->state == SS_LISTENING) {
530 res = -EPIPE;
531 goto exit;
Allan Stephens33880072006-06-25 23:44:57 -0700532 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700533 if (sock->state != SS_UNCONNECTED) {
534 res = -EISCONN;
535 goto exit;
536 }
537 if ((tport->published) ||
538 ((sock->type == SOCK_STREAM) && (total_len != 0))) {
539 res = -EOPNOTSUPP;
540 goto exit;
541 }
542 if (dest->addrtype == TIPC_ADDR_NAME) {
543 tport->conn_type = dest->addr.name.name.type;
544 tport->conn_instance = dest->addr.name.name.instance;
545 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546
547 /* Abort any pending connection attempts (very unlikely) */
548
Allan Stephens0c3141e2008-04-15 00:22:02 -0700549 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 }
551
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900552 do {
553 if (dest->addrtype == TIPC_ADDR_NAME) {
554 if ((res = dest_name_check(dest, m)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700555 break;
556 res = tipc_send2name(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900557 &dest->addr.name.name,
558 dest->addr.name.domain,
559 m->msg_iovlen,
560 m->msg_iov);
561 }
562 else if (dest->addrtype == TIPC_ADDR_ID) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700563 res = tipc_send2port(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900564 &dest->addr.id,
565 m->msg_iovlen,
566 m->msg_iov);
567 }
568 else if (dest->addrtype == TIPC_ADDR_MCAST) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100569 if (needs_conn) {
570 res = -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700571 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900573 if ((res = dest_name_check(dest, m)))
Allan Stephens0c3141e2008-04-15 00:22:02 -0700574 break;
575 res = tipc_multicast(tport->ref,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900576 &dest->addr.nameseq,
577 0,
578 m->msg_iovlen,
579 m->msg_iov);
580 }
581 if (likely(res != -ELINKCONG)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700582 if (needs_conn && (res >= 0)) {
583 sock->state = SS_CONNECTING;
584 }
585 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900586 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587 if (m->msg_flags & MSG_DONTWAIT) {
588 res = -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700589 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700591 release_sock(sk);
592 res = wait_event_interruptible(*sk->sk_sleep,
593 !tport->congested);
594 lock_sock(sk);
595 if (res)
596 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900597 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700598
599exit:
600 if (iocb)
601 release_sock(sk);
602 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603}
604
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900605/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 * send_packet - send a connection-oriented message
Allan Stephens0c3141e2008-04-15 00:22:02 -0700607 * @iocb: if NULL, indicates that socket lock is already held
Per Lidenb97bf3f2006-01-02 19:04:38 +0100608 * @sock: socket structure
609 * @m: message to send
Allan Stephense9024f02006-06-25 23:43:57 -0700610 * @total_len: length of message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900611 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900613 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 * Returns the number of bytes sent on success, or errno otherwise
615 */
616
617static int send_packet(struct kiocb *iocb, struct socket *sock,
618 struct msghdr *m, size_t total_len)
619{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700620 struct sock *sk = sock->sk;
621 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900622 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623 int res;
624
625 /* Handle implied connection establishment */
626
627 if (unlikely(dest))
628 return send_msg(iocb, sock, m, total_len);
629
Allan Stephens0c3141e2008-04-15 00:22:02 -0700630 if (iocb)
631 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900633 do {
Allan Stephensbdd94782006-06-25 23:45:53 -0700634 if (unlikely(sock->state != SS_CONNECTED)) {
635 if (sock->state == SS_DISCONNECTING)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900636 res = -EPIPE;
Allan Stephensbdd94782006-06-25 23:45:53 -0700637 else
638 res = -ENOTCONN;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700639 break;
Allan Stephensbdd94782006-06-25 23:45:53 -0700640 }
641
Allan Stephens0c3141e2008-04-15 00:22:02 -0700642 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900643 if (likely(res != -ELINKCONG)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700644 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900645 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 if (m->msg_flags & MSG_DONTWAIT) {
647 res = -EWOULDBLOCK;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700648 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700650 release_sock(sk);
651 res = wait_event_interruptible(*sk->sk_sleep,
652 (!tport->congested || !tport->connected));
653 lock_sock(sk);
654 if (res)
655 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900656 } while (1);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700657
658 if (iocb)
659 release_sock(sk);
660 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661}
662
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900663/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664 * send_stream - send stream-oriented data
665 * @iocb: (unused)
666 * @sock: socket structure
667 * @m: data to send
668 * @total_len: total length of data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900669 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900671 *
672 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -0700673 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674 */
675
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676static int send_stream(struct kiocb *iocb, struct socket *sock,
677 struct msghdr *m, size_t total_len)
678{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700679 struct sock *sk = sock->sk;
680 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681 struct msghdr my_msg;
682 struct iovec my_iov;
683 struct iovec *curr_iov;
684 int curr_iovlen;
685 char __user *curr_start;
Allan Stephens05646c92007-06-10 17:25:24 -0700686 u32 hdr_size;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100687 int curr_left;
688 int bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700689 int bytes_sent;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100690 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900691
Allan Stephens0c3141e2008-04-15 00:22:02 -0700692 lock_sock(sk);
693
Allan Stephens05646c92007-06-10 17:25:24 -0700694 /* Handle special cases where there is no connection */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100695
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900696 if (unlikely(sock->state != SS_CONNECTED)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700697 if (sock->state == SS_UNCONNECTED) {
698 res = send_packet(NULL, sock, m, total_len);
699 goto exit;
700 } else if (sock->state == SS_DISCONNECTING) {
701 res = -EPIPE;
702 goto exit;
703 } else {
704 res = -ENOTCONN;
705 goto exit;
706 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900707 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708
Allan Stephens0c3141e2008-04-15 00:22:02 -0700709 if (unlikely(m->msg_name)) {
710 res = -EISCONN;
711 goto exit;
712 }
Allan Stephenseb5959c2006-10-16 21:43:54 -0700713
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900714 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100715 * Send each iovec entry using one or more messages
716 *
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900717 * Note: This algorithm is good for the most likely case
Per Lidenb97bf3f2006-01-02 19:04:38 +0100718 * (i.e. one large iovec entry), but could be improved to pass sets
719 * of small iovec entries into send_packet().
720 */
721
Allan Stephens1303e8f2006-06-25 23:46:50 -0700722 curr_iov = m->msg_iov;
723 curr_iovlen = m->msg_iovlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100724 my_msg.msg_iov = &my_iov;
725 my_msg.msg_iovlen = 1;
Allan Stephenseb5959c2006-10-16 21:43:54 -0700726 my_msg.msg_flags = m->msg_flags;
727 my_msg.msg_name = NULL;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700728 bytes_sent = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729
Allan Stephens05646c92007-06-10 17:25:24 -0700730 hdr_size = msg_hdr_sz(&tport->phdr);
731
Per Lidenb97bf3f2006-01-02 19:04:38 +0100732 while (curr_iovlen--) {
733 curr_start = curr_iov->iov_base;
734 curr_left = curr_iov->iov_len;
735
736 while (curr_left) {
Allan Stephens05646c92007-06-10 17:25:24 -0700737 bytes_to_send = tport->max_pkt - hdr_size;
738 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
739 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
740 if (curr_left < bytes_to_send)
741 bytes_to_send = curr_left;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742 my_iov.iov_base = curr_start;
743 my_iov.iov_len = bytes_to_send;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700744 if ((res = send_packet(NULL, sock, &my_msg, 0)) < 0) {
745 if (bytes_sent)
Allan Stephens05646c92007-06-10 17:25:24 -0700746 res = bytes_sent;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700747 goto exit;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700748 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749 curr_left -= bytes_to_send;
750 curr_start += bytes_to_send;
Allan Stephens1303e8f2006-06-25 23:46:50 -0700751 bytes_sent += bytes_to_send;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752 }
753
754 curr_iov++;
755 }
Allan Stephens0c3141e2008-04-15 00:22:02 -0700756 res = bytes_sent;
757exit:
758 release_sock(sk);
759 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100760}
761
762/**
763 * auto_connect - complete connection setup to a remote port
764 * @sock: socket structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100765 * @msg: peer's response message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900766 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100767 * Returns 0 on success, errno otherwise
768 */
769
Allan Stephens0c3141e2008-04-15 00:22:02 -0700770static int auto_connect(struct socket *sock, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771{
Allan Stephens2da59912008-07-14 22:43:32 -0700772 struct tipc_sock *tsock = tipc_sk(sock->sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773
774 if (msg_errcode(msg)) {
775 sock->state = SS_DISCONNECTING;
776 return -ECONNREFUSED;
777 }
778
Allan Stephens2da59912008-07-14 22:43:32 -0700779 tsock->peer_name.ref = msg_origport(msg);
780 tsock->peer_name.node = msg_orignode(msg);
781 tipc_connect2port(tsock->p->ref, &tsock->peer_name);
782 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100783 sock->state = SS_CONNECTED;
784 return 0;
785}
786
787/**
788 * set_orig_addr - capture sender's address for received message
789 * @m: descriptor for message info
790 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900791 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792 * Note: Address is not captured if not requested by receiver.
793 */
794
Sam Ravnborg05790c62006-03-20 22:37:04 -0800795static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100796{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900797 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900799 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100800 addr->family = AF_TIPC;
801 addr->addrtype = TIPC_ADDR_ID;
802 addr->addr.id.ref = msg_origport(msg);
803 addr->addr.id.node = msg_orignode(msg);
804 addr->addr.name.domain = 0; /* could leave uninitialized */
805 addr->scope = 0; /* could leave uninitialized */
806 m->msg_namelen = sizeof(struct sockaddr_tipc);
807 }
808}
809
810/**
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900811 * anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100812 * @m: descriptor for message info
813 * @msg: received message header
814 * @tport: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900815 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100816 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900817 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100818 * Returns 0 if successful, otherwise errno
819 */
820
Sam Ravnborg05790c62006-03-20 22:37:04 -0800821static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822 struct tipc_port *tport)
823{
824 u32 anc_data[3];
825 u32 err;
826 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -0700827 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828 int res;
829
830 if (likely(m->msg_controllen == 0))
831 return 0;
832
833 /* Optionally capture errored message object(s) */
834
835 err = msg ? msg_errcode(msg) : 0;
836 if (unlikely(err)) {
837 anc_data[0] = err;
838 anc_data[1] = msg_data_sz(msg);
Allan Stephens4b087b22006-06-25 23:47:44 -0700839 if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840 return res;
841 if (anc_data[1] &&
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900842 (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843 msg_data(msg))))
844 return res;
845 }
846
847 /* Optionally capture message destination object */
848
849 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
850 switch (dest_type) {
851 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700852 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100853 anc_data[0] = msg_nametype(msg);
854 anc_data[1] = msg_namelower(msg);
855 anc_data[2] = msg_namelower(msg);
856 break;
857 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700858 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100859 anc_data[0] = msg_nametype(msg);
860 anc_data[1] = msg_namelower(msg);
861 anc_data[2] = msg_nameupper(msg);
862 break;
863 case TIPC_CONN_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -0700864 has_name = (tport->conn_type != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100865 anc_data[0] = tport->conn_type;
866 anc_data[1] = tport->conn_instance;
867 anc_data[2] = tport->conn_instance;
868 break;
869 default:
Allan Stephens3546c752006-06-25 23:45:24 -0700870 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100871 }
Allan Stephens3546c752006-06-25 23:45:24 -0700872 if (has_name &&
Allan Stephens4b087b22006-06-25 23:47:44 -0700873 (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data)))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100874 return res;
875
876 return 0;
877}
878
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900879/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 * recv_msg - receive packet-oriented message
881 * @iocb: (unused)
882 * @m: descriptor for message info
883 * @buf_len: total size of user buffer area
884 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900885 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100886 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
887 * If the complete message doesn't fit in user area, truncate it.
888 *
889 * Returns size of returned message data, errno otherwise
890 */
891
892static int recv_msg(struct kiocb *iocb, struct socket *sock,
893 struct msghdr *m, size_t buf_len, int flags)
894{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700895 struct sock *sk = sock->sk;
896 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897 struct sk_buff *buf;
898 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899 unsigned int sz;
900 u32 err;
901 int res;
902
Allan Stephens0c3141e2008-04-15 00:22:02 -0700903 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904
905 if (m->msg_iovlen != 1)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700906 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907
908 if (unlikely(!buf_len))
909 return -EINVAL;
910
Allan Stephens0c3141e2008-04-15 00:22:02 -0700911 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100912
Allan Stephens0c3141e2008-04-15 00:22:02 -0700913 if (unlikely(sock->state == SS_UNCONNECTED)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914 res = -ENOTCONN;
915 goto exit;
916 }
917
Allan Stephens0c3141e2008-04-15 00:22:02 -0700918restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100919
Allan Stephens0c3141e2008-04-15 00:22:02 -0700920 /* Look for a message in receive queue; wait if necessary */
921
922 while (skb_queue_empty(&sk->sk_receive_queue)) {
923 if (sock->state == SS_DISCONNECTING) {
924 res = -ENOTCONN;
925 goto exit;
926 }
927 if (flags & MSG_DONTWAIT) {
928 res = -EWOULDBLOCK;
929 goto exit;
930 }
931 release_sock(sk);
932 res = wait_event_interruptible(*sk->sk_sleep,
933 (!skb_queue_empty(&sk->sk_receive_queue) ||
934 (sock->state == SS_DISCONNECTING)));
935 lock_sock(sk);
936 if (res)
937 goto exit;
938 }
939
940 /* Look at first message in receive queue */
941
942 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100943 msg = buf_msg(buf);
944 sz = msg_data_sz(msg);
945 err = msg_errcode(msg);
946
947 /* Complete connection setup for an implied connect */
948
949 if (unlikely(sock->state == SS_CONNECTING)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700950 res = auto_connect(sock, msg);
951 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100952 goto exit;
953 }
954
955 /* Discard an empty non-errored message & try again */
956
957 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -0700958 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959 goto restart;
960 }
961
962 /* Capture sender's address (optional) */
963
964 set_orig_addr(m, msg);
965
966 /* Capture ancillary data (optional) */
967
Allan Stephens0c3141e2008-04-15 00:22:02 -0700968 res = anc_data_recv(m, msg, tport);
969 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100970 goto exit;
971
972 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900973
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974 if (!err) {
975 if (unlikely(buf_len < sz)) {
976 sz = buf_len;
977 m->msg_flags |= MSG_TRUNC;
978 }
979 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
980 sz))) {
981 res = -EFAULT;
982 goto exit;
983 }
984 res = sz;
985 } else {
986 if ((sock->state == SS_READY) ||
987 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
988 res = 0;
989 else
990 res = -ECONNRESET;
991 }
992
993 /* Consume received message (optional) */
994
995 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens99009802008-04-15 00:06:12 -0700996 if ((sock->state != SS_READY) &&
Allan Stephens0c3141e2008-04-15 00:22:02 -0700997 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
998 tipc_acknowledge(tport->ref, tport->conn_unacked);
999 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001000 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001001exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001002 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001003 return res;
1004}
1005
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001006/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001007 * recv_stream - receive stream-oriented data
1008 * @iocb: (unused)
1009 * @m: descriptor for message info
1010 * @buf_len: total size of user buffer area
1011 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001012 *
1013 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014 * will optionally wait for more; never truncates data.
1015 *
1016 * Returns size of returned message data, errno otherwise
1017 */
1018
1019static int recv_stream(struct kiocb *iocb, struct socket *sock,
1020 struct msghdr *m, size_t buf_len, int flags)
1021{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001022 struct sock *sk = sock->sk;
1023 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 struct sk_buff *buf;
1025 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026 unsigned int sz;
1027 int sz_to_copy;
1028 int sz_copied = 0;
1029 int needed;
Al Viro28c4dad2006-10-10 22:45:57 +01001030 char __user *crs = m->msg_iov->iov_base;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001031 unsigned char *buf_crs;
1032 u32 err;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001033 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034
1035 /* Catch invalid receive attempts */
1036
Allan Stephens0c3141e2008-04-15 00:22:02 -07001037 if (m->msg_iovlen != 1)
1038 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
1039
Per Lidenb97bf3f2006-01-02 19:04:38 +01001040 if (unlikely(!buf_len))
1041 return -EINVAL;
1042
Allan Stephens0c3141e2008-04-15 00:22:02 -07001043 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001044
Allan Stephens0c3141e2008-04-15 00:22:02 -07001045 if (unlikely((sock->state == SS_UNCONNECTED) ||
1046 (sock->state == SS_CONNECTING))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001047 res = -ENOTCONN;
1048 goto exit;
1049 }
1050
Allan Stephens0c3141e2008-04-15 00:22:02 -07001051restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001052
Allan Stephens0c3141e2008-04-15 00:22:02 -07001053 /* Look for a message in receive queue; wait if necessary */
1054
1055 while (skb_queue_empty(&sk->sk_receive_queue)) {
1056 if (sock->state == SS_DISCONNECTING) {
1057 res = -ENOTCONN;
1058 goto exit;
1059 }
1060 if (flags & MSG_DONTWAIT) {
1061 res = -EWOULDBLOCK;
1062 goto exit;
1063 }
1064 release_sock(sk);
1065 res = wait_event_interruptible(*sk->sk_sleep,
1066 (!skb_queue_empty(&sk->sk_receive_queue) ||
1067 (sock->state == SS_DISCONNECTING)));
1068 lock_sock(sk);
1069 if (res)
1070 goto exit;
1071 }
1072
1073 /* Look at first message in receive queue */
1074
1075 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076 msg = buf_msg(buf);
1077 sz = msg_data_sz(msg);
1078 err = msg_errcode(msg);
1079
1080 /* Discard an empty non-errored message & try again */
1081
1082 if ((!sz) && (!err)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001083 advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001084 goto restart;
1085 }
1086
1087 /* Optionally capture sender's address & ancillary data of first msg */
1088
1089 if (sz_copied == 0) {
1090 set_orig_addr(m, msg);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001091 res = anc_data_recv(m, msg, tport);
1092 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001093 goto exit;
1094 }
1095
1096 /* Capture message data (if valid) & compute return value (always) */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001097
Per Lidenb97bf3f2006-01-02 19:04:38 +01001098 if (!err) {
1099 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
Allan Stephens7a8036c2008-04-15 00:15:15 -07001100 sz = (unsigned char *)msg + msg_size(msg) - buf_crs;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001101
1102 needed = (buf_len - sz_copied);
1103 sz_to_copy = (sz <= needed) ? sz : needed;
1104 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1105 res = -EFAULT;
1106 goto exit;
1107 }
1108 sz_copied += sz_to_copy;
1109
1110 if (sz_to_copy < sz) {
1111 if (!(flags & MSG_PEEK))
1112 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1113 goto exit;
1114 }
1115
1116 crs += sz_to_copy;
1117 } else {
1118 if (sz_copied != 0)
1119 goto exit; /* can't add error msg to valid data */
1120
1121 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1122 res = 0;
1123 else
1124 res = -ECONNRESET;
1125 }
1126
1127 /* Consume received message (optional) */
1128
1129 if (likely(!(flags & MSG_PEEK))) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001130 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1131 tipc_acknowledge(tport->ref, tport->conn_unacked);
1132 advance_rx_queue(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001133 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134
1135 /* Loop around if more data is required */
1136
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001137 if ((sz_copied < buf_len) /* didn't get all requested data */
Allan Stephens8642bd92008-07-14 22:42:51 -07001138 && (!skb_queue_empty(&sk->sk_receive_queue) ||
Allan Stephensa198d3a2008-04-15 00:07:15 -07001139 (flags & MSG_WAITALL))
1140 /* ... and more is ready or required */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
1142 && (!err) /* ... and haven't reached a FIN */
1143 )
1144 goto restart;
1145
1146exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001147 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001148 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001149}
1150
1151/**
Allan Stephens1819b832008-04-15 00:15:50 -07001152 * rx_queue_full - determine if receive queue can accept another message
1153 * @msg: message to be added to queue
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154 * @queue_size: current size of queue
1155 * @base: nominal maximum size of queue
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001156 *
Allan Stephens1819b832008-04-15 00:15:50 -07001157 * Returns 1 if queue is unable to accept message, 0 otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001158 */
1159
Allan Stephens1819b832008-04-15 00:15:50 -07001160static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161{
1162 u32 threshold;
1163 u32 imp = msg_importance(msg);
1164
1165 if (imp == TIPC_LOW_IMPORTANCE)
1166 threshold = base;
1167 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1168 threshold = base * 2;
1169 else if (imp == TIPC_HIGH_IMPORTANCE)
1170 threshold = base * 100;
1171 else
1172 return 0;
1173
1174 if (msg_connected(msg))
1175 threshold *= 4;
1176
Allan Stephens1819b832008-04-15 00:15:50 -07001177 return (queue_size >= threshold);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001178}
1179
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001180/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001181 * filter_rcv - validate incoming message
1182 * @sk: socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183 * @buf: message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001184 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001185 * Enqueues message on receive queue if acceptable; optionally handles
1186 * disconnect indication for a connected socket.
1187 *
1188 * Called with socket lock already taken; port lock may also be taken.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001189 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1191 */
1192
Allan Stephens0c3141e2008-04-15 00:22:02 -07001193static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001195 struct socket *sock = sk->sk_socket;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 struct tipc_msg *msg = buf_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197 u32 recv_q_len;
1198
Per Lidenb97bf3f2006-01-02 19:04:38 +01001199 /* Reject message if it is wrong sort of message for socket */
1200
1201 /*
1202 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1203 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1204 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1205 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001206
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 if (sock->state == SS_READY) {
1208 if (msg_connected(msg)) {
1209 msg_dbg(msg, "dispatch filter 1\n");
1210 return TIPC_ERR_NO_PORT;
1211 }
1212 } else {
1213 if (msg_mcast(msg)) {
1214 msg_dbg(msg, "dispatch filter 2\n");
1215 return TIPC_ERR_NO_PORT;
1216 }
1217 if (sock->state == SS_CONNECTED) {
1218 if (!msg_connected(msg)) {
1219 msg_dbg(msg, "dispatch filter 3\n");
1220 return TIPC_ERR_NO_PORT;
1221 }
1222 }
1223 else if (sock->state == SS_CONNECTING) {
1224 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1225 msg_dbg(msg, "dispatch filter 4\n");
1226 return TIPC_ERR_NO_PORT;
1227 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001228 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001229 else if (sock->state == SS_LISTENING) {
1230 if (msg_connected(msg) || msg_errcode(msg)) {
1231 msg_dbg(msg, "dispatch filter 5\n");
1232 return TIPC_ERR_NO_PORT;
1233 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001234 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001235 else if (sock->state == SS_DISCONNECTING) {
1236 msg_dbg(msg, "dispatch filter 6\n");
1237 return TIPC_ERR_NO_PORT;
1238 }
1239 else /* (sock->state == SS_UNCONNECTED) */ {
1240 if (msg_connected(msg) || msg_errcode(msg)) {
1241 msg_dbg(msg, "dispatch filter 7\n");
1242 return TIPC_ERR_NO_PORT;
1243 }
1244 }
1245 }
1246
1247 /* Reject message if there isn't room to queue it */
1248
Allan Stephens1819b832008-04-15 00:15:50 -07001249 recv_q_len = (u32)atomic_read(&tipc_queue_size);
1250 if (unlikely(recv_q_len >= OVERLOAD_LIMIT_BASE)) {
1251 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001253 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001254 recv_q_len = skb_queue_len(&sk->sk_receive_queue);
Allan Stephens1819b832008-04-15 00:15:50 -07001255 if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
1256 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001257 return TIPC_ERR_OVERLOAD;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001258 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259
Allan Stephens0c3141e2008-04-15 00:22:02 -07001260 /* Enqueue message (finally!) */
1261
1262 msg_dbg(msg, "<DISP<: ");
1263 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1264 atomic_inc(&tipc_queue_size);
1265 __skb_queue_tail(&sk->sk_receive_queue, buf);
1266
Per Lidenb97bf3f2006-01-02 19:04:38 +01001267 /* Initiate connection termination for an incoming 'FIN' */
1268
1269 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1270 sock->state = SS_DISCONNECTING;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001271 tipc_disconnect_port(tipc_sk_port(sk));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272 }
1273
Allan Stephens0c3141e2008-04-15 00:22:02 -07001274 if (waitqueue_active(sk->sk_sleep))
1275 wake_up_interruptible(sk->sk_sleep);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001276 return TIPC_OK;
1277}
1278
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001279/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001280 * backlog_rcv - handle incoming message from backlog queue
1281 * @sk: socket
1282 * @buf: message
1283 *
1284 * Caller must hold socket lock, but not port lock.
1285 *
1286 * Returns 0
1287 */
1288
1289static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1290{
1291 u32 res;
1292
1293 res = filter_rcv(sk, buf);
1294 if (res)
1295 tipc_reject_msg(buf, res);
1296 return 0;
1297}
1298
1299/**
1300 * dispatch - handle incoming message
1301 * @tport: TIPC port that received message
1302 * @buf: message
1303 *
1304 * Called with port lock already taken.
1305 *
1306 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1307 */
1308
1309static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1310{
1311 struct sock *sk = (struct sock *)tport->usr_handle;
1312 u32 res;
1313
1314 /*
1315 * Process message if socket is unlocked; otherwise add to backlog queue
1316 *
1317 * This code is based on sk_receive_skb(), but must be distinct from it
1318 * since a TIPC-specific filter/reject mechanism is utilized
1319 */
1320
1321 bh_lock_sock(sk);
1322 if (!sock_owned_by_user(sk)) {
1323 res = filter_rcv(sk, buf);
1324 } else {
1325 sk_add_backlog(sk, buf);
1326 res = TIPC_OK;
1327 }
1328 bh_unlock_sock(sk);
1329
1330 return res;
1331}
1332
1333/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334 * wakeupdispatch - wake up port after congestion
1335 * @tport: port to wakeup
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001336 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001337 * Called with port lock already taken.
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 */
1339
1340static void wakeupdispatch(struct tipc_port *tport)
1341{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001342 struct sock *sk = (struct sock *)tport->usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343
Allan Stephens0c3141e2008-04-15 00:22:02 -07001344 if (waitqueue_active(sk->sk_sleep))
1345 wake_up_interruptible(sk->sk_sleep);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346}
1347
1348/**
1349 * connect - establish a connection to another TIPC port
1350 * @sock: socket structure
1351 * @dest: socket address for destination port
1352 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001353 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001354 *
1355 * Returns 0 on success, errno otherwise
1356 */
1357
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001358static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001359 int flags)
1360{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001361 struct sock *sk = sock->sk;
Allan Stephensb89741a2008-04-15 00:20:37 -07001362 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1363 struct msghdr m = {NULL,};
1364 struct sk_buff *buf;
1365 struct tipc_msg *msg;
1366 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367
Allan Stephens0c3141e2008-04-15 00:22:02 -07001368 lock_sock(sk);
1369
Allan Stephensb89741a2008-04-15 00:20:37 -07001370 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371
Allan Stephens0c3141e2008-04-15 00:22:02 -07001372 if (sock->state == SS_READY) {
1373 res = -EOPNOTSUPP;
1374 goto exit;
1375 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376
Allan Stephensb89741a2008-04-15 00:20:37 -07001377 /* For now, TIPC does not support the non-blocking form of connect() */
Allan Stephens4934c692008-04-15 00:16:19 -07001378
Allan Stephens0c3141e2008-04-15 00:22:02 -07001379 if (flags & O_NONBLOCK) {
1380 res = -EWOULDBLOCK;
1381 goto exit;
1382 }
Allan Stephens4934c692008-04-15 00:16:19 -07001383
Allan Stephensb89741a2008-04-15 00:20:37 -07001384 /* Issue Posix-compliant error code if socket is in the wrong state */
Allan Stephens51f9cc12006-06-25 23:49:06 -07001385
Allan Stephens0c3141e2008-04-15 00:22:02 -07001386 if (sock->state == SS_LISTENING) {
1387 res = -EOPNOTSUPP;
1388 goto exit;
1389 }
1390 if (sock->state == SS_CONNECTING) {
1391 res = -EALREADY;
1392 goto exit;
1393 }
1394 if (sock->state != SS_UNCONNECTED) {
1395 res = -EISCONN;
1396 goto exit;
1397 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398
Allan Stephensb89741a2008-04-15 00:20:37 -07001399 /*
1400 * Reject connection attempt using multicast address
1401 *
1402 * Note: send_msg() validates the rest of the address fields,
1403 * so there's no need to do it here
1404 */
Allan Stephens51f9cc12006-06-25 23:49:06 -07001405
Allan Stephens0c3141e2008-04-15 00:22:02 -07001406 if (dst->addrtype == TIPC_ADDR_MCAST) {
1407 res = -EINVAL;
1408 goto exit;
1409 }
1410
1411 /* Reject any messages already in receive queue (very unlikely) */
1412
1413 reject_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001414
Allan Stephensb89741a2008-04-15 00:20:37 -07001415 /* Send a 'SYN-' to destination */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001416
Allan Stephensb89741a2008-04-15 00:20:37 -07001417 m.msg_name = dest;
1418 m.msg_namelen = destlen;
1419 res = send_msg(NULL, sock, &m, 0);
1420 if (res < 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001421 goto exit;
Allan Stephensb89741a2008-04-15 00:20:37 -07001422 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001423
Allan Stephens0c3141e2008-04-15 00:22:02 -07001424 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001425
Allan Stephens0c3141e2008-04-15 00:22:02 -07001426 release_sock(sk);
1427 res = wait_event_interruptible_timeout(*sk->sk_sleep,
1428 (!skb_queue_empty(&sk->sk_receive_queue) ||
1429 (sock->state != SS_CONNECTING)),
1430 sk->sk_rcvtimeo);
1431 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001432
Allan Stephensb89741a2008-04-15 00:20:37 -07001433 if (res > 0) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001434 buf = skb_peek(&sk->sk_receive_queue);
1435 if (buf != NULL) {
1436 msg = buf_msg(buf);
1437 res = auto_connect(sock, msg);
1438 if (!res) {
1439 if (!msg_data_sz(msg))
1440 advance_rx_queue(sk);
1441 }
1442 } else {
1443 if (sock->state == SS_CONNECTED) {
1444 res = -EISCONN;
1445 } else {
1446 res = -ECONNREFUSED;
1447 }
Allan Stephensb89741a2008-04-15 00:20:37 -07001448 }
1449 } else {
1450 if (res == 0)
1451 res = -ETIMEDOUT;
1452 else
1453 ; /* leave "res" unchanged */
1454 sock->state = SS_DISCONNECTING;
1455 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001456
Allan Stephens0c3141e2008-04-15 00:22:02 -07001457exit:
1458 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001459 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001460}
1461
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001462/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463 * listen - allow socket to listen for incoming connections
1464 * @sock: socket structure
1465 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001466 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467 * Returns 0 on success, errno otherwise
1468 */
1469
1470static int listen(struct socket *sock, int len)
1471{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001472 struct sock *sk = sock->sk;
1473 int res;
1474
1475 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001476
1477 if (sock->state == SS_READY)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001478 res = -EOPNOTSUPP;
1479 else if (sock->state != SS_UNCONNECTED)
1480 res = -EINVAL;
1481 else {
1482 sock->state = SS_LISTENING;
1483 res = 0;
1484 }
1485
1486 release_sock(sk);
1487 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488}
1489
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001490/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001491 * accept - wait for connection request
1492 * @sock: listening socket
1493 * @newsock: new socket that is to be connected
1494 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001495 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001496 * Returns 0 on success, errno otherwise
1497 */
1498
Allan Stephens0c3141e2008-04-15 00:22:02 -07001499static int accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001500{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001501 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001502 struct sk_buff *buf;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001503 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001504
Allan Stephens0c3141e2008-04-15 00:22:02 -07001505 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001506
Allan Stephens0c3141e2008-04-15 00:22:02 -07001507 if (sock->state == SS_READY) {
1508 res = -EOPNOTSUPP;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001509 goto exit;
1510 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001511 if (sock->state != SS_LISTENING) {
1512 res = -EINVAL;
1513 goto exit;
1514 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001515
Allan Stephens0c3141e2008-04-15 00:22:02 -07001516 while (skb_queue_empty(&sk->sk_receive_queue)) {
1517 if (flags & O_NONBLOCK) {
1518 res = -EWOULDBLOCK;
1519 goto exit;
1520 }
1521 release_sock(sk);
1522 res = wait_event_interruptible(*sk->sk_sleep,
1523 (!skb_queue_empty(&sk->sk_receive_queue)));
1524 lock_sock(sk);
1525 if (res)
1526 goto exit;
1527 }
1528
1529 buf = skb_peek(&sk->sk_receive_queue);
1530
1531 res = tipc_create(sock_net(sock->sk), new_sock, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001532 if (!res) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001533 struct sock *new_sk = new_sock->sk;
Allan Stephens2da59912008-07-14 22:43:32 -07001534 struct tipc_sock *new_tsock = tipc_sk(new_sk);
1535 struct tipc_port *new_tport = new_tsock->p;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001536 u32 new_ref = new_tport->ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001537 struct tipc_msg *msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001538
1539 lock_sock(new_sk);
1540
1541 /*
1542 * Reject any stray messages received by new socket
1543 * before the socket lock was taken (very, very unlikely)
1544 */
1545
1546 reject_rx_queue(new_sk);
1547
1548 /* Connect new socket to it's peer */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001549
Allan Stephens2da59912008-07-14 22:43:32 -07001550 new_tsock->peer_name.ref = msg_origport(msg);
1551 new_tsock->peer_name.node = msg_orignode(msg);
1552 tipc_connect2port(new_ref, &new_tsock->peer_name);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001553 new_sock->state = SS_CONNECTED;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001554
1555 tipc_set_portimportance(new_ref, msg_importance(msg));
1556 if (msg_named(msg)) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001557 new_tport->conn_type = msg_nametype(msg);
1558 new_tport->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001559 }
1560
Allan Stephens0c3141e2008-04-15 00:22:02 -07001561 /*
Per Lidenb97bf3f2006-01-02 19:04:38 +01001562 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1563 * Respond to 'SYN+' by queuing it on new socket.
1564 */
1565
1566 msg_dbg(msg,"<ACC<: ");
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001567 if (!msg_data_sz(msg)) {
1568 struct msghdr m = {NULL,};
Per Lidenb97bf3f2006-01-02 19:04:38 +01001569
Allan Stephens0c3141e2008-04-15 00:22:02 -07001570 advance_rx_queue(sk);
1571 send_packet(NULL, new_sock, &m, 0);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001572 } else {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001573 __skb_dequeue(&sk->sk_receive_queue);
1574 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001575 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001576 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001577 }
1578exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001579 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001580 return res;
1581}
1582
1583/**
1584 * shutdown - shutdown socket connection
1585 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08001586 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001587 *
1588 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001589 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001590 * Returns 0 on success, errno otherwise
1591 */
1592
1593static int shutdown(struct socket *sock, int how)
1594{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001595 struct sock *sk = sock->sk;
1596 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001597 struct sk_buff *buf;
1598 int res;
1599
Allan Stephense247a8f2008-03-06 15:05:38 -08001600 if (how != SHUT_RDWR)
1601 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001602
Allan Stephens0c3141e2008-04-15 00:22:02 -07001603 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001604
1605 switch (sock->state) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07001606 case SS_CONNECTING:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001607 case SS_CONNECTED:
1608
Allan Stephens0c3141e2008-04-15 00:22:02 -07001609 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001610restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001611 buf = __skb_dequeue(&sk->sk_receive_queue);
1612 if (buf) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001613 atomic_dec(&tipc_queue_size);
1614 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1615 buf_discard(buf);
1616 goto restart;
1617 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001618 tipc_disconnect(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001619 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001620 } else {
1621 tipc_shutdown(tport->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001622 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001623
1624 sock->state = SS_DISCONNECTING;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001625
1626 /* fall through */
1627
1628 case SS_DISCONNECTING:
1629
Allan Stephens0c3141e2008-04-15 00:22:02 -07001630 /* Discard any unreceived messages; wake up sleeping tasks */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001631
Allan Stephens0c3141e2008-04-15 00:22:02 -07001632 discard_rx_queue(sk);
1633 if (waitqueue_active(sk->sk_sleep))
1634 wake_up_interruptible(sk->sk_sleep);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001635 res = 0;
1636 break;
1637
1638 default:
1639 res = -ENOTCONN;
1640 }
1641
Allan Stephens0c3141e2008-04-15 00:22:02 -07001642 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001643 return res;
1644}
1645
1646/**
1647 * setsockopt - set socket option
1648 * @sock: socket structure
1649 * @lvl: option level
1650 * @opt: option identifier
1651 * @ov: pointer to new option value
1652 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001653 *
1654 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001655 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001656 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001657 * Returns 0 on success, errno otherwise
1658 */
1659
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001660static int setsockopt(struct socket *sock,
Allan Stephense9024f02006-06-25 23:43:57 -07001661 int lvl, int opt, char __user *ov, int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001662{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001663 struct sock *sk = sock->sk;
1664 struct tipc_port *tport = tipc_sk_port(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665 u32 value;
1666 int res;
1667
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001668 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1669 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670 if (lvl != SOL_TIPC)
1671 return -ENOPROTOOPT;
1672 if (ol < sizeof(value))
1673 return -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001674 if ((res = get_user(value, (u32 __user *)ov)))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001675 return res;
1676
Allan Stephens0c3141e2008-04-15 00:22:02 -07001677 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001678
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679 switch (opt) {
1680 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001681 res = tipc_set_portimportance(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001682 break;
1683 case TIPC_SRC_DROPPABLE:
1684 if (sock->type != SOCK_STREAM)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001685 res = tipc_set_portunreliable(tport->ref, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001686 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01001687 res = -ENOPROTOOPT;
1688 break;
1689 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001690 res = tipc_set_portunreturnable(tport->ref, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001691 break;
1692 case TIPC_CONN_TIMEOUT:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001693 sk->sk_rcvtimeo = msecs_to_jiffies(value);
1694 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001695 break;
1696 default:
1697 res = -EINVAL;
1698 }
1699
Allan Stephens0c3141e2008-04-15 00:22:02 -07001700 release_sock(sk);
1701
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702 return res;
1703}
1704
1705/**
1706 * getsockopt - get socket option
1707 * @sock: socket structure
1708 * @lvl: option level
1709 * @opt: option identifier
1710 * @ov: receptacle for option value
1711 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001712 *
1713 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01001714 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001715 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001716 * Returns 0 on success, errno otherwise
1717 */
1718
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001719static int getsockopt(struct socket *sock,
Al Viro28c4dad2006-10-10 22:45:57 +01001720 int lvl, int opt, char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001721{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001722 struct sock *sk = sock->sk;
1723 struct tipc_port *tport = tipc_sk_port(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001724 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001725 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001726 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001727
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001728 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1729 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001730 if (lvl != SOL_TIPC)
1731 return -ENOPROTOOPT;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001732 if ((res = get_user(len, ol)))
1733 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001734
Allan Stephens0c3141e2008-04-15 00:22:02 -07001735 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001736
1737 switch (opt) {
1738 case TIPC_IMPORTANCE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001739 res = tipc_portimportance(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001740 break;
1741 case TIPC_SRC_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001742 res = tipc_portunreliable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001743 break;
1744 case TIPC_DEST_DROPPABLE:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001745 res = tipc_portunreturnable(tport->ref, &value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001746 break;
1747 case TIPC_CONN_TIMEOUT:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001748 value = jiffies_to_msecs(sk->sk_rcvtimeo);
1749 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001750 break;
1751 default:
1752 res = -EINVAL;
1753 }
1754
Allan Stephens0c3141e2008-04-15 00:22:02 -07001755 release_sock(sk);
1756
Per Lidenb97bf3f2006-01-02 19:04:38 +01001757 if (res) {
1758 /* "get" failed */
1759 }
1760 else if (len < sizeof(value)) {
1761 res = -EINVAL;
1762 }
Pavel Emelyanov653252c2008-04-25 01:49:48 -07001763 else if (copy_to_user(ov, &value, sizeof(value))) {
1764 res = -EFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001765 }
1766 else {
1767 res = put_user(sizeof(value), ol);
1768 }
1769
Per Lidenb97bf3f2006-01-02 19:04:38 +01001770 return res;
1771}
1772
1773/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001774 * Protocol switches for the various types of TIPC sockets
1775 */
1776
Florian Westphalbca65ea2008-02-07 18:18:01 -08001777static const struct proto_ops msg_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001778 .owner = THIS_MODULE,
1779 .family = AF_TIPC,
1780 .release = release,
1781 .bind = bind,
1782 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001783 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001784 .accept = accept,
1785 .getname = get_name,
1786 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001787 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001788 .listen = listen,
1789 .shutdown = shutdown,
1790 .setsockopt = setsockopt,
1791 .getsockopt = getsockopt,
1792 .sendmsg = send_msg,
1793 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001794 .mmap = sock_no_mmap,
1795 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001796};
1797
Florian Westphalbca65ea2008-02-07 18:18:01 -08001798static const struct proto_ops packet_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001799 .owner = THIS_MODULE,
1800 .family = AF_TIPC,
1801 .release = release,
1802 .bind = bind,
1803 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001804 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001805 .accept = accept,
1806 .getname = get_name,
1807 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001808 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001809 .listen = listen,
1810 .shutdown = shutdown,
1811 .setsockopt = setsockopt,
1812 .getsockopt = getsockopt,
1813 .sendmsg = send_packet,
1814 .recvmsg = recv_msg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001815 .mmap = sock_no_mmap,
1816 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001817};
1818
Florian Westphalbca65ea2008-02-07 18:18:01 -08001819static const struct proto_ops stream_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001820 .owner = THIS_MODULE,
1821 .family = AF_TIPC,
1822 .release = release,
1823 .bind = bind,
1824 .connect = connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001825 .socketpair = sock_no_socketpair,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001826 .accept = accept,
1827 .getname = get_name,
1828 .poll = poll,
Allan Stephens5eee6a62007-06-10 17:24:55 -07001829 .ioctl = sock_no_ioctl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001830 .listen = listen,
1831 .shutdown = shutdown,
1832 .setsockopt = setsockopt,
1833 .getsockopt = getsockopt,
1834 .sendmsg = send_stream,
1835 .recvmsg = recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09001836 .mmap = sock_no_mmap,
1837 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01001838};
1839
Florian Westphalbca65ea2008-02-07 18:18:01 -08001840static const struct net_proto_family tipc_family_ops = {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001841 .owner = THIS_MODULE,
1842 .family = AF_TIPC,
1843 .create = tipc_create
1844};
1845
1846static struct proto tipc_proto = {
1847 .name = "TIPC",
1848 .owner = THIS_MODULE,
1849 .obj_size = sizeof(struct tipc_sock)
1850};
1851
1852/**
Per Liden4323add2006-01-18 00:38:21 +01001853 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001854 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001855 * Returns 0 on success, errno otherwise
1856 */
Per Liden4323add2006-01-18 00:38:21 +01001857int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001858{
1859 int res;
1860
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001861 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001862 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001863 err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001864 goto out;
1865 }
1866
1867 res = sock_register(&tipc_family_ops);
1868 if (res) {
Per Lidend0a14a92006-01-11 13:52:51 +01001869 err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001870 proto_unregister(&tipc_proto);
1871 goto out;
1872 }
1873
1874 sockets_enabled = 1;
1875 out:
1876 return res;
1877}
1878
1879/**
Per Liden4323add2006-01-18 00:38:21 +01001880 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01001881 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001882
Per Liden4323add2006-01-18 00:38:21 +01001883void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001884{
1885 if (!sockets_enabled)
1886 return;
1887
1888 sockets_enabled = 0;
1889 sock_unregister(tipc_family_ops.family);
1890 proto_unregister(&tipc_proto);
1891}
1892