tipc: eliminate redundant locking
[linux-2.6-block.git] / net / tipc / socket.c
CommitLineData
b97bf3fd
PL
1/*
2 * net/tipc/socket.c: TIPC socket API
c4307285 3 *
8826cde6 4 * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
c5fa7b3c 5 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
b97bf3fd
PL
6 * All rights reserved.
7 *
9ea1fd3c 8 * Redistribution and use in source and binary forms, with or without
b97bf3fd
PL
9 * modification, are permitted provided that the following conditions are met:
10 *
9ea1fd3c
PL
11 * 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.
b97bf3fd 19 *
9ea1fd3c
PL
20 * 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
b97bf3fd
PL
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
b97bf3fd 37#include "core.h"
d265fef6 38#include "port.h"
b97bf3fd 39
2cf8aa19 40#include <linux/export.h>
2cf8aa19 41
b97bf3fd
PL
42#define SS_LISTENING -1 /* socket is listening */
43#define SS_READY -2 /* socket is connectionless */
44
3654ea02 45#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
b97bf3fd 46
0c3141e9 47static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
f288bef4
YX
48static void tipc_data_ready(struct sock *sk, int len);
49static void tipc_write_space(struct sock *sk);
247f0f3c
YX
50static int tipc_release(struct socket *sock);
51static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
b97bf3fd 52
bca65eae
FW
53static const struct proto_ops packet_ops;
54static const struct proto_ops stream_ops;
55static const struct proto_ops msg_ops;
b97bf3fd
PL
56
57static struct proto tipc_proto;
c5fa7b3c 58static struct proto tipc_proto_kern;
b97bf3fd 59
c4307285 60/*
0c3141e9
AS
61 * Revised TIPC socket locking policy:
62 *
63 * Most socket operations take the standard socket lock when they start
64 * and hold it until they finish (or until they need to sleep). Acquiring
65 * this lock grants the owner exclusive access to the fields of the socket
66 * data structures, with the exception of the backlog queue. A few socket
67 * operations can be done without taking the socket lock because they only
68 * read socket information that never changes during the life of the socket.
69 *
70 * Socket operations may acquire the lock for the associated TIPC port if they
71 * need to perform an operation on the port. If any routine needs to acquire
72 * both the socket lock and the port lock it must take the socket lock first
73 * to avoid the risk of deadlock.
74 *
75 * The dispatcher handling incoming messages cannot grab the socket lock in
76 * the standard fashion, since invoked it runs at the BH level and cannot block.
77 * Instead, it checks to see if the socket lock is currently owned by someone,
78 * and either handles the message itself or adds it to the socket's backlog
79 * queue; in the latter case the queued message is processed once the process
80 * owning the socket lock releases it.
81 *
82 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
83 * the problem of a blocked socket operation preventing any other operations
84 * from occurring. However, applications must be careful if they have
85 * multiple threads trying to send (or receive) on the same socket, as these
86 * operations might interfere with each other. For example, doing a connect
87 * and a receive at the same time might allow the receive to consume the
88 * ACK message meant for the connect. While additional work could be done
89 * to try and overcome this, it doesn't seem to be worthwhile at the present.
90 *
91 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
92 * that another operation that must be performed in a non-blocking manner is
93 * not delayed for very long because the lock has already been taken.
94 *
95 * NOTE: This code assumes that certain fields of a port/socket pair are
96 * constant over its lifetime; such fields can be examined without taking
97 * the socket lock and/or port lock, and do not need to be re-read even
98 * after resuming processing after waiting. These fields include:
99 * - socket type
100 * - pointer to socket sk structure (aka tipc_sock structure)
101 * - pointer to port structure
102 * - port reference
103 */
104
8826cde6
JPM
105#include "socket.h"
106
0c3141e9
AS
107/**
108 * advance_rx_queue - discard first buffer in socket receive queue
109 *
110 * Caller must hold socket lock
b97bf3fd 111 */
0c3141e9 112static void advance_rx_queue(struct sock *sk)
b97bf3fd 113{
5f6d9123 114 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
b97bf3fd
PL
115}
116
b97bf3fd 117/**
0c3141e9
AS
118 * reject_rx_queue - reject all buffers in socket receive queue
119 *
120 * Caller must hold socket lock
b97bf3fd 121 */
0c3141e9 122static void reject_rx_queue(struct sock *sk)
b97bf3fd 123{
0c3141e9
AS
124 struct sk_buff *buf;
125
9da3d475 126 while ((buf = __skb_dequeue(&sk->sk_receive_queue)))
0c3141e9 127 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
b97bf3fd
PL
128}
129
130/**
c5fa7b3c 131 * tipc_sk_create - create a TIPC socket
0c3141e9 132 * @net: network namespace (must be default network)
b97bf3fd
PL
133 * @sock: pre-allocated socket structure
134 * @protocol: protocol indicator (must be 0)
3f378b68 135 * @kern: caused by kernel or by userspace?
c4307285 136 *
0c3141e9
AS
137 * This routine creates additional data structures used by the TIPC socket,
138 * initializes them, and links them together.
b97bf3fd
PL
139 *
140 * Returns 0 on success, errno otherwise
141 */
c5fa7b3c
YX
142static int tipc_sk_create(struct net *net, struct socket *sock, int protocol,
143 int kern)
b97bf3fd 144{
0c3141e9
AS
145 const struct proto_ops *ops;
146 socket_state state;
b97bf3fd 147 struct sock *sk;
7ef43eba 148 struct tipc_port *tp_ptr;
0c3141e9
AS
149
150 /* Validate arguments */
b97bf3fd
PL
151 if (unlikely(protocol != 0))
152 return -EPROTONOSUPPORT;
153
b97bf3fd
PL
154 switch (sock->type) {
155 case SOCK_STREAM:
0c3141e9
AS
156 ops = &stream_ops;
157 state = SS_UNCONNECTED;
b97bf3fd
PL
158 break;
159 case SOCK_SEQPACKET:
0c3141e9
AS
160 ops = &packet_ops;
161 state = SS_UNCONNECTED;
b97bf3fd
PL
162 break;
163 case SOCK_DGRAM:
b97bf3fd 164 case SOCK_RDM:
0c3141e9
AS
165 ops = &msg_ops;
166 state = SS_READY;
b97bf3fd 167 break;
49978651 168 default:
49978651 169 return -EPROTOTYPE;
b97bf3fd
PL
170 }
171
0c3141e9 172 /* Allocate socket's protocol area */
c5fa7b3c
YX
173 if (!kern)
174 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
175 else
176 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
177
0c3141e9 178 if (sk == NULL)
b97bf3fd 179 return -ENOMEM;
b97bf3fd 180
24be34b5
JPM
181 tp_ptr = tipc_sk_port(sk);
182 if (!tipc_port_init(tp_ptr, TIPC_LOW_IMPORTANCE)) {
0c3141e9
AS
183 sk_free(sk);
184 return -ENOMEM;
185 }
b97bf3fd 186
0c3141e9 187 /* Finish initializing socket data structures */
0c3141e9
AS
188 sock->ops = ops;
189 sock->state = state;
b97bf3fd 190
0c3141e9 191 sock_init_data(sock, sk);
0c3141e9 192 sk->sk_backlog_rcv = backlog_rcv;
cc79dd1b 193 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
f288bef4
YX
194 sk->sk_data_ready = tipc_data_ready;
195 sk->sk_write_space = tipc_write_space;
a0f40f02 196 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
7ef43eba
AS
197 spin_unlock_bh(tp_ptr->lock);
198
0c3141e9 199 if (sock->state == SS_READY) {
3b4f302d 200 tipc_port_set_unreturnable(tp_ptr, true);
0c3141e9 201 if (sock->type == SOCK_DGRAM)
3b4f302d 202 tipc_port_set_unreliable(tp_ptr, true);
0c3141e9 203 }
b97bf3fd
PL
204 return 0;
205}
206
c5fa7b3c
YX
207/**
208 * tipc_sock_create_local - create TIPC socket from inside TIPC module
209 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
210 *
211 * We cannot use sock_creat_kern here because it bumps module user count.
212 * Since socket owner and creator is the same module we must make sure
213 * that module count remains zero for module local sockets, otherwise
214 * we cannot do rmmod.
215 *
216 * Returns 0 on success, errno otherwise
217 */
218int tipc_sock_create_local(int type, struct socket **res)
219{
220 int rc;
c5fa7b3c
YX
221
222 rc = sock_create_lite(AF_TIPC, type, 0, res);
223 if (rc < 0) {
224 pr_err("Failed to create kernel socket\n");
225 return rc;
226 }
227 tipc_sk_create(&init_net, *res, 0, 1);
228
c5fa7b3c
YX
229 return 0;
230}
231
232/**
233 * tipc_sock_release_local - release socket created by tipc_sock_create_local
234 * @sock: the socket to be released.
235 *
236 * Module reference count is not incremented when such sockets are created,
237 * so we must keep it from being decremented when they are released.
238 */
239void tipc_sock_release_local(struct socket *sock)
240{
247f0f3c 241 tipc_release(sock);
c5fa7b3c
YX
242 sock->ops = NULL;
243 sock_release(sock);
244}
245
246/**
247 * tipc_sock_accept_local - accept a connection on a socket created
248 * with tipc_sock_create_local. Use this function to avoid that
249 * module reference count is inadvertently incremented.
250 *
251 * @sock: the accepting socket
252 * @newsock: reference to the new socket to be created
253 * @flags: socket flags
254 */
255
256int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
ae8509c4 257 int flags)
c5fa7b3c
YX
258{
259 struct sock *sk = sock->sk;
260 int ret;
261
262 ret = sock_create_lite(sk->sk_family, sk->sk_type,
263 sk->sk_protocol, newsock);
264 if (ret < 0)
265 return ret;
266
247f0f3c 267 ret = tipc_accept(sock, *newsock, flags);
c5fa7b3c
YX
268 if (ret < 0) {
269 sock_release(*newsock);
270 return ret;
271 }
272 (*newsock)->ops = sock->ops;
273 return ret;
274}
275
b97bf3fd 276/**
247f0f3c 277 * tipc_release - destroy a TIPC socket
b97bf3fd
PL
278 * @sock: socket to destroy
279 *
280 * This routine cleans up any messages that are still queued on the socket.
281 * For DGRAM and RDM socket types, all queued messages are rejected.
282 * For SEQPACKET and STREAM socket types, the first message is rejected
283 * and any others are discarded. (If the first message on a STREAM socket
284 * is partially-read, it is discarded and the next one is rejected instead.)
c4307285 285 *
b97bf3fd
PL
286 * NOTE: Rejected messages are not necessarily returned to the sender! They
287 * are returned or discarded according to the "destination droppable" setting
288 * specified for the message by the sender.
289 *
290 * Returns 0 on success, errno otherwise
291 */
247f0f3c 292static int tipc_release(struct socket *sock)
b97bf3fd 293{
b97bf3fd 294 struct sock *sk = sock->sk;
0c3141e9 295 struct tipc_port *tport;
b97bf3fd 296 struct sk_buff *buf;
0c3141e9 297 int res;
b97bf3fd 298
0c3141e9
AS
299 /*
300 * Exit if socket isn't fully initialized (occurs when a failed accept()
301 * releases a pre-allocated child socket that was never used)
302 */
0c3141e9 303 if (sk == NULL)
b97bf3fd 304 return 0;
c4307285 305
0c3141e9
AS
306 tport = tipc_sk_port(sk);
307 lock_sock(sk);
308
309 /*
310 * Reject all unreceived messages, except on an active connection
311 * (which disconnects locally & sends a 'FIN+' to peer)
312 */
b97bf3fd 313 while (sock->state != SS_DISCONNECTING) {
0c3141e9
AS
314 buf = __skb_dequeue(&sk->sk_receive_queue);
315 if (buf == NULL)
b97bf3fd 316 break;
40682432 317 if (TIPC_SKB_CB(buf)->handle != NULL)
5f6d9123 318 kfree_skb(buf);
0c3141e9
AS
319 else {
320 if ((sock->state == SS_CONNECTING) ||
321 (sock->state == SS_CONNECTED)) {
322 sock->state = SS_DISCONNECTING;
247f0f3c 323 tipc_port_disconnect(tport->ref);
0c3141e9 324 }
b97bf3fd 325 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
0c3141e9 326 }
b97bf3fd
PL
327 }
328
0c3141e9
AS
329 /*
330 * Delete TIPC port; this ensures no more messages are queued
331 * (also disconnects an active connection & sends a 'FIN-' to peer)
332 */
24be34b5 333 tipc_port_destroy(tport);
b97bf3fd 334
0c3141e9 335 /* Discard any remaining (connection-based) messages in receive queue */
57467e56 336 __skb_queue_purge(&sk->sk_receive_queue);
b97bf3fd 337
0c3141e9 338 /* Reject any messages that accumulated in backlog queue */
0c3141e9
AS
339 sock->state = SS_DISCONNECTING;
340 release_sock(sk);
b97bf3fd
PL
341
342 sock_put(sk);
0c3141e9 343 sock->sk = NULL;
b97bf3fd 344
b97bf3fd
PL
345 return res;
346}
347
348/**
247f0f3c 349 * tipc_bind - associate or disassocate TIPC name(s) with a socket
b97bf3fd
PL
350 * @sock: socket structure
351 * @uaddr: socket address describing name(s) and desired operation
352 * @uaddr_len: size of socket address data structure
c4307285 353 *
b97bf3fd
PL
354 * Name and name sequence binding is indicated using a positive scope value;
355 * a negative scope value unbinds the specified name. Specifying no name
356 * (i.e. a socket address length of 0) unbinds all names from the socket.
c4307285 357 *
b97bf3fd 358 * Returns 0 on success, errno otherwise
0c3141e9
AS
359 *
360 * NOTE: This routine doesn't need to take the socket lock since it doesn't
361 * access any non-constant socket information.
b97bf3fd 362 */
247f0f3c
YX
363static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
364 int uaddr_len)
b97bf3fd 365{
84602761 366 struct sock *sk = sock->sk;
b97bf3fd 367 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
84602761
YX
368 struct tipc_port *tport = tipc_sk_port(sock->sk);
369 int res = -EINVAL;
b97bf3fd 370
84602761
YX
371 lock_sock(sk);
372 if (unlikely(!uaddr_len)) {
373 res = tipc_withdraw(tport, 0, NULL);
374 goto exit;
375 }
c4307285 376
84602761
YX
377 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
378 res = -EINVAL;
379 goto exit;
380 }
381 if (addr->family != AF_TIPC) {
382 res = -EAFNOSUPPORT;
383 goto exit;
384 }
b97bf3fd 385
b97bf3fd
PL
386 if (addr->addrtype == TIPC_ADDR_NAME)
387 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
84602761
YX
388 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
389 res = -EAFNOSUPPORT;
390 goto exit;
391 }
c4307285 392
13a2e898 393 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
7d0ab17b 394 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
84602761
YX
395 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
396 res = -EACCES;
397 goto exit;
398 }
c422f1bd 399
84602761
YX
400 res = (addr->scope > 0) ?
401 tipc_publish(tport, addr->scope, &addr->addr.nameseq) :
402 tipc_withdraw(tport, -addr->scope, &addr->addr.nameseq);
403exit:
404 release_sock(sk);
405 return res;
b97bf3fd
PL
406}
407
c4307285 408/**
247f0f3c 409 * tipc_getname - get port ID of socket or peer socket
b97bf3fd
PL
410 * @sock: socket structure
411 * @uaddr: area for returned socket address
412 * @uaddr_len: area for returned length of socket address
2da59918 413 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
c4307285 414 *
b97bf3fd 415 * Returns 0 on success, errno otherwise
0c3141e9 416 *
2da59918
AS
417 * NOTE: This routine doesn't need to take the socket lock since it only
418 * accesses socket information that is unchanging (or which changes in
0e65967e 419 * a completely predictable manner).
b97bf3fd 420 */
247f0f3c
YX
421static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
422 int *uaddr_len, int peer)
b97bf3fd 423{
b97bf3fd 424 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
8826cde6 425 struct tipc_port *port = tipc_sk_port(sock->sk);
b97bf3fd 426
88f8a5e3 427 memset(addr, 0, sizeof(*addr));
0c3141e9 428 if (peer) {
2da59918
AS
429 if ((sock->state != SS_CONNECTED) &&
430 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
431 return -ENOTCONN;
8826cde6
JPM
432 addr->addr.id.ref = tipc_port_peerport(port);
433 addr->addr.id.node = tipc_port_peernode(port);
0c3141e9 434 } else {
8826cde6 435 addr->addr.id.ref = port->ref;
b924dcf0 436 addr->addr.id.node = tipc_own_addr;
0c3141e9 437 }
b97bf3fd
PL
438
439 *uaddr_len = sizeof(*addr);
440 addr->addrtype = TIPC_ADDR_ID;
441 addr->family = AF_TIPC;
442 addr->scope = 0;
b97bf3fd
PL
443 addr->addr.name.domain = 0;
444
0c3141e9 445 return 0;
b97bf3fd
PL
446}
447
448/**
247f0f3c 449 * tipc_poll - read and possibly block on pollmask
b97bf3fd
PL
450 * @file: file structure associated with the socket
451 * @sock: socket for which to calculate the poll bits
452 * @wait: ???
453 *
9b674e82
AS
454 * Returns pollmask value
455 *
456 * COMMENTARY:
457 * It appears that the usual socket locking mechanisms are not useful here
458 * since the pollmask info is potentially out-of-date the moment this routine
459 * exits. TCP and other protocols seem to rely on higher level poll routines
460 * to handle any preventable race conditions, so TIPC will do the same ...
461 *
462 * TIPC sets the returned events as follows:
f662c070
AS
463 *
464 * socket state flags set
465 * ------------ ---------
466 * unconnected no read flags
c4fc298a 467 * POLLOUT if port is not congested
f662c070
AS
468 *
469 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
470 * no write flags
471 *
472 * connected POLLIN/POLLRDNORM if data in rx queue
473 * POLLOUT if port is not congested
474 *
475 * disconnecting POLLIN/POLLRDNORM/POLLHUP
476 * no write flags
477 *
478 * listening POLLIN if SYN in rx queue
479 * no write flags
480 *
481 * ready POLLIN/POLLRDNORM if data in rx queue
482 * [connectionless] POLLOUT (since port cannot be congested)
483 *
484 * IMPORTANT: The fact that a read or write operation is indicated does NOT
485 * imply that the operation will succeed, merely that it should be performed
486 * and will not block.
b97bf3fd 487 */
247f0f3c
YX
488static unsigned int tipc_poll(struct file *file, struct socket *sock,
489 poll_table *wait)
b97bf3fd 490{
9b674e82 491 struct sock *sk = sock->sk;
f662c070 492 u32 mask = 0;
9b674e82 493
f288bef4 494 sock_poll_wait(file, sk_sleep(sk), wait);
9b674e82 495
f662c070 496 switch ((int)sock->state) {
c4fc298a
EH
497 case SS_UNCONNECTED:
498 if (!tipc_sk_port(sk)->congested)
499 mask |= POLLOUT;
500 break;
f662c070
AS
501 case SS_READY:
502 case SS_CONNECTED:
503 if (!tipc_sk_port(sk)->congested)
504 mask |= POLLOUT;
505 /* fall thru' */
506 case SS_CONNECTING:
507 case SS_LISTENING:
508 if (!skb_queue_empty(&sk->sk_receive_queue))
509 mask |= (POLLIN | POLLRDNORM);
510 break;
511 case SS_DISCONNECTING:
512 mask = (POLLIN | POLLRDNORM | POLLHUP);
513 break;
514 }
9b674e82
AS
515
516 return mask;
b97bf3fd
PL
517}
518
c4307285 519/**
b97bf3fd
PL
520 * dest_name_check - verify user is permitted to send to specified port name
521 * @dest: destination address
522 * @m: descriptor for message to be sent
c4307285 523 *
b97bf3fd
PL
524 * Prevents restricted configuration commands from being issued by
525 * unauthorized users.
c4307285 526 *
b97bf3fd
PL
527 * Returns 0 if permission is granted, otherwise errno
528 */
05790c64 529static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
b97bf3fd
PL
530{
531 struct tipc_cfg_msg_hdr hdr;
532
c4307285
YH
533 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
534 return 0;
535 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
536 return 0;
c4307285
YH
537 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
538 return -EACCES;
b97bf3fd 539
3f8dd944
AS
540 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
541 return -EMSGSIZE;
c4307285 542 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
b97bf3fd 543 return -EFAULT;
70cb2347 544 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
b97bf3fd 545 return -EACCES;
c4307285 546
b97bf3fd
PL
547 return 0;
548}
549
3f40504f
YX
550static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
551{
552 struct sock *sk = sock->sk;
553 struct tipc_port *tport = tipc_sk_port(sk);
554 DEFINE_WAIT(wait);
555 int done;
556
557 do {
558 int err = sock_error(sk);
559 if (err)
560 return err;
561 if (sock->state == SS_DISCONNECTING)
562 return -EPIPE;
563 if (!*timeo_p)
564 return -EAGAIN;
565 if (signal_pending(current))
566 return sock_intr_errno(*timeo_p);
567
568 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
569 done = sk_wait_event(sk, timeo_p, !tport->congested);
570 finish_wait(sk_sleep(sk), &wait);
571 } while (!done);
572 return 0;
573}
574
b97bf3fd 575/**
247f0f3c 576 * tipc_sendmsg - send message in connectionless manner
0c3141e9 577 * @iocb: if NULL, indicates that socket lock is already held
b97bf3fd
PL
578 * @sock: socket structure
579 * @m: message to send
e9024f0f 580 * @total_len: length of message
c4307285 581 *
b97bf3fd 582 * Message must have an destination specified explicitly.
c4307285 583 * Used for SOCK_RDM and SOCK_DGRAM messages,
b97bf3fd
PL
584 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
585 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
c4307285 586 *
b97bf3fd
PL
587 * Returns the number of bytes sent on success, or errno otherwise
588 */
247f0f3c
YX
589static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
590 struct msghdr *m, size_t total_len)
b97bf3fd 591{
0c3141e9
AS
592 struct sock *sk = sock->sk;
593 struct tipc_port *tport = tipc_sk_port(sk);
342dfc30 594 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
b97bf3fd 595 int needs_conn;
3f40504f 596 long timeo;
b97bf3fd
PL
597 int res = -EINVAL;
598
599 if (unlikely(!dest))
600 return -EDESTADDRREQ;
51f9cc1f
AS
601 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
602 (dest->family != AF_TIPC)))
b97bf3fd 603 return -EINVAL;
97f8b87e 604 if (total_len > TIPC_MAX_USER_MSG_SIZE)
c29c3f70 605 return -EMSGSIZE;
b97bf3fd 606
0c3141e9
AS
607 if (iocb)
608 lock_sock(sk);
609
b97bf3fd
PL
610 needs_conn = (sock->state != SS_READY);
611 if (unlikely(needs_conn)) {
0c3141e9
AS
612 if (sock->state == SS_LISTENING) {
613 res = -EPIPE;
614 goto exit;
615 }
616 if (sock->state != SS_UNCONNECTED) {
617 res = -EISCONN;
618 goto exit;
619 }
5d21cb70 620 if (tport->published) {
0c3141e9
AS
621 res = -EOPNOTSUPP;
622 goto exit;
623 }
3388007b 624 if (dest->addrtype == TIPC_ADDR_NAME) {
0c3141e9
AS
625 tport->conn_type = dest->addr.name.name.type;
626 tport->conn_instance = dest->addr.name.name.instance;
3388007b 627 }
b97bf3fd
PL
628
629 /* Abort any pending connection attempts (very unlikely) */
0c3141e9 630 reject_rx_queue(sk);
b97bf3fd
PL
631 }
632
3f40504f 633 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
c4307285
YH
634 do {
635 if (dest->addrtype == TIPC_ADDR_NAME) {
2db9983a
AS
636 res = dest_name_check(dest, m);
637 if (res)
0c3141e9
AS
638 break;
639 res = tipc_send2name(tport->ref,
c4307285
YH
640 &dest->addr.name.name,
641 dest->addr.name.domain,
26896904
AS
642 m->msg_iov,
643 total_len);
0e65967e 644 } else if (dest->addrtype == TIPC_ADDR_ID) {
0c3141e9 645 res = tipc_send2port(tport->ref,
c4307285 646 &dest->addr.id,
26896904
AS
647 m->msg_iov,
648 total_len);
0e65967e 649 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
b97bf3fd
PL
650 if (needs_conn) {
651 res = -EOPNOTSUPP;
0c3141e9 652 break;
b97bf3fd 653 }
2db9983a
AS
654 res = dest_name_check(dest, m);
655 if (res)
0c3141e9 656 break;
247f0f3c
YX
657 res = tipc_port_mcast_xmit(tport->ref,
658 &dest->addr.nameseq,
659 m->msg_iov,
660 total_len);
c4307285
YH
661 }
662 if (likely(res != -ELINKCONG)) {
a016892c 663 if (needs_conn && (res >= 0))
0c3141e9 664 sock->state = SS_CONNECTING;
0c3141e9 665 break;
c4307285 666 }
3f40504f
YX
667 res = tipc_wait_for_sndmsg(sock, &timeo);
668 if (res)
0c3141e9 669 break;
c4307285 670 } while (1);
0c3141e9
AS
671
672exit:
673 if (iocb)
674 release_sock(sk);
675 return res;
b97bf3fd
PL
676}
677
391a6dd1
YX
678static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
679{
680 struct sock *sk = sock->sk;
681 struct tipc_port *tport = tipc_sk_port(sk);
682 DEFINE_WAIT(wait);
683 int done;
684
685 do {
686 int err = sock_error(sk);
687 if (err)
688 return err;
689 if (sock->state == SS_DISCONNECTING)
690 return -EPIPE;
691 else if (sock->state != SS_CONNECTED)
692 return -ENOTCONN;
693 if (!*timeo_p)
694 return -EAGAIN;
695 if (signal_pending(current))
696 return sock_intr_errno(*timeo_p);
697
698 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
699 done = sk_wait_event(sk, timeo_p,
700 (!tport->congested || !tport->connected));
701 finish_wait(sk_sleep(sk), &wait);
702 } while (!done);
703 return 0;
704}
705
c4307285 706/**
247f0f3c 707 * tipc_send_packet - send a connection-oriented message
0c3141e9 708 * @iocb: if NULL, indicates that socket lock is already held
b97bf3fd
PL
709 * @sock: socket structure
710 * @m: message to send
e9024f0f 711 * @total_len: length of message
c4307285 712 *
b97bf3fd 713 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
c4307285 714 *
b97bf3fd
PL
715 * Returns the number of bytes sent on success, or errno otherwise
716 */
247f0f3c
YX
717static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
718 struct msghdr *m, size_t total_len)
b97bf3fd 719{
0c3141e9
AS
720 struct sock *sk = sock->sk;
721 struct tipc_port *tport = tipc_sk_port(sk);
342dfc30 722 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
391a6dd1
YX
723 int res = -EINVAL;
724 long timeo;
b97bf3fd
PL
725
726 /* Handle implied connection establishment */
b97bf3fd 727 if (unlikely(dest))
247f0f3c 728 return tipc_sendmsg(iocb, sock, m, total_len);
b97bf3fd 729
97f8b87e 730 if (total_len > TIPC_MAX_USER_MSG_SIZE)
c29c3f70
AS
731 return -EMSGSIZE;
732
0c3141e9
AS
733 if (iocb)
734 lock_sock(sk);
b97bf3fd 735
391a6dd1
YX
736 if (unlikely(sock->state != SS_CONNECTED)) {
737 if (sock->state == SS_DISCONNECTING)
738 res = -EPIPE;
739 else
740 res = -ENOTCONN;
741 goto exit;
742 }
1d835874 743
391a6dd1 744 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
c4307285 745 do {
9446b87a 746 res = tipc_send(tport->ref, m->msg_iov, total_len);
a016892c 747 if (likely(res != -ELINKCONG))
0c3141e9 748 break;
391a6dd1
YX
749 res = tipc_wait_for_sndpkt(sock, &timeo);
750 if (res)
0c3141e9 751 break;
c4307285 752 } while (1);
391a6dd1 753exit:
0c3141e9
AS
754 if (iocb)
755 release_sock(sk);
756 return res;
b97bf3fd
PL
757}
758
c4307285 759/**
247f0f3c 760 * tipc_send_stream - send stream-oriented data
b97bf3fd
PL
761 * @iocb: (unused)
762 * @sock: socket structure
763 * @m: data to send
764 * @total_len: total length of data to be sent
c4307285 765 *
b97bf3fd 766 * Used for SOCK_STREAM data.
c4307285
YH
767 *
768 * Returns the number of bytes sent on success (or partial success),
1303e8f1 769 * or errno if no data sent
b97bf3fd 770 */
247f0f3c
YX
771static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
772 struct msghdr *m, size_t total_len)
b97bf3fd 773{
0c3141e9
AS
774 struct sock *sk = sock->sk;
775 struct tipc_port *tport = tipc_sk_port(sk);
b97bf3fd
PL
776 struct msghdr my_msg;
777 struct iovec my_iov;
778 struct iovec *curr_iov;
779 int curr_iovlen;
780 char __user *curr_start;
05646c91 781 u32 hdr_size;
b97bf3fd
PL
782 int curr_left;
783 int bytes_to_send;
1303e8f1 784 int bytes_sent;
b97bf3fd 785 int res;
c4307285 786
0c3141e9
AS
787 lock_sock(sk);
788
05646c91 789 /* Handle special cases where there is no connection */
c4307285 790 if (unlikely(sock->state != SS_CONNECTED)) {
3b8401fe 791 if (sock->state == SS_UNCONNECTED)
247f0f3c 792 res = tipc_send_packet(NULL, sock, m, total_len);
b0555976 793 else
794 res = sock->state == SS_DISCONNECTING ? -EPIPE : -ENOTCONN;
3b8401fe 795 goto exit;
c4307285 796 }
b97bf3fd 797
0c3141e9
AS
798 if (unlikely(m->msg_name)) {
799 res = -EISCONN;
800 goto exit;
801 }
eb5959c2 802
97f8b87e 803 if (total_len > (unsigned int)INT_MAX) {
c29c3f70
AS
804 res = -EMSGSIZE;
805 goto exit;
806 }
807
c4307285 808 /*
b97bf3fd
PL
809 * Send each iovec entry using one or more messages
810 *
c4307285 811 * Note: This algorithm is good for the most likely case
b97bf3fd
PL
812 * (i.e. one large iovec entry), but could be improved to pass sets
813 * of small iovec entries into send_packet().
814 */
1303e8f1
AS
815 curr_iov = m->msg_iov;
816 curr_iovlen = m->msg_iovlen;
b97bf3fd
PL
817 my_msg.msg_iov = &my_iov;
818 my_msg.msg_iovlen = 1;
eb5959c2
AS
819 my_msg.msg_flags = m->msg_flags;
820 my_msg.msg_name = NULL;
1303e8f1 821 bytes_sent = 0;
b97bf3fd 822
05646c91
AS
823 hdr_size = msg_hdr_sz(&tport->phdr);
824
b97bf3fd
PL
825 while (curr_iovlen--) {
826 curr_start = curr_iov->iov_base;
827 curr_left = curr_iov->iov_len;
828
829 while (curr_left) {
05646c91
AS
830 bytes_to_send = tport->max_pkt - hdr_size;
831 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
832 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
833 if (curr_left < bytes_to_send)
834 bytes_to_send = curr_left;
b97bf3fd
PL
835 my_iov.iov_base = curr_start;
836 my_iov.iov_len = bytes_to_send;
247f0f3c
YX
837 res = tipc_send_packet(NULL, sock, &my_msg,
838 bytes_to_send);
2db9983a 839 if (res < 0) {
0c3141e9 840 if (bytes_sent)
05646c91 841 res = bytes_sent;
0c3141e9 842 goto exit;
1303e8f1 843 }
b97bf3fd
PL
844 curr_left -= bytes_to_send;
845 curr_start += bytes_to_send;
1303e8f1 846 bytes_sent += bytes_to_send;
b97bf3fd
PL
847 }
848
849 curr_iov++;
850 }
0c3141e9
AS
851 res = bytes_sent;
852exit:
853 release_sock(sk);
854 return res;
b97bf3fd
PL
855}
856
857/**
858 * auto_connect - complete connection setup to a remote port
859 * @sock: socket structure
b97bf3fd 860 * @msg: peer's response message
c4307285 861 *
b97bf3fd
PL
862 * Returns 0 on success, errno otherwise
863 */
0c3141e9 864static int auto_connect(struct socket *sock, struct tipc_msg *msg)
b97bf3fd 865{
8826cde6 866 struct tipc_port *p_ptr = tipc_sk_port(sock->sk);
f9fef18c
JPM
867 struct tipc_portid peer;
868
869 peer.ref = msg_origport(msg);
870 peer.node = msg_orignode(msg);
b97bf3fd 871
8826cde6 872 p_ptr = tipc_port_deref(p_ptr->ref);
584d24b3
YX
873 if (!p_ptr)
874 return -EINVAL;
875
f9fef18c 876 __tipc_port_connect(p_ptr->ref, p_ptr, &peer);
584d24b3
YX
877
878 if (msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)
879 return -EINVAL;
880 msg_set_importance(&p_ptr->phdr, (u32)msg_importance(msg));
b97bf3fd
PL
881 sock->state = SS_CONNECTED;
882 return 0;
883}
884
885/**
886 * set_orig_addr - capture sender's address for received message
887 * @m: descriptor for message info
888 * @msg: received message header
c4307285 889 *
b97bf3fd
PL
890 * Note: Address is not captured if not requested by receiver.
891 */
05790c64 892static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
b97bf3fd 893{
342dfc30 894 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
b97bf3fd 895
c4307285 896 if (addr) {
b97bf3fd
PL
897 addr->family = AF_TIPC;
898 addr->addrtype = TIPC_ADDR_ID;
60085c3d 899 memset(&addr->addr, 0, sizeof(addr->addr));
b97bf3fd
PL
900 addr->addr.id.ref = msg_origport(msg);
901 addr->addr.id.node = msg_orignode(msg);
0e65967e
AS
902 addr->addr.name.domain = 0; /* could leave uninitialized */
903 addr->scope = 0; /* could leave uninitialized */
b97bf3fd
PL
904 m->msg_namelen = sizeof(struct sockaddr_tipc);
905 }
906}
907
908/**
c4307285 909 * anc_data_recv - optionally capture ancillary data for received message
b97bf3fd
PL
910 * @m: descriptor for message info
911 * @msg: received message header
912 * @tport: TIPC port associated with message
c4307285 913 *
b97bf3fd 914 * Note: Ancillary data is not captured if not requested by receiver.
c4307285 915 *
b97bf3fd
PL
916 * Returns 0 if successful, otherwise errno
917 */
05790c64 918static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
ae8509c4 919 struct tipc_port *tport)
b97bf3fd
PL
920{
921 u32 anc_data[3];
922 u32 err;
923 u32 dest_type;
3546c750 924 int has_name;
b97bf3fd
PL
925 int res;
926
927 if (likely(m->msg_controllen == 0))
928 return 0;
929
930 /* Optionally capture errored message object(s) */
b97bf3fd
PL
931 err = msg ? msg_errcode(msg) : 0;
932 if (unlikely(err)) {
933 anc_data[0] = err;
934 anc_data[1] = msg_data_sz(msg);
2db9983a
AS
935 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
936 if (res)
b97bf3fd 937 return res;
2db9983a
AS
938 if (anc_data[1]) {
939 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
940 msg_data(msg));
941 if (res)
942 return res;
943 }
b97bf3fd
PL
944 }
945
946 /* Optionally capture message destination object */
b97bf3fd
PL
947 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
948 switch (dest_type) {
949 case TIPC_NAMED_MSG:
3546c750 950 has_name = 1;
b97bf3fd
PL
951 anc_data[0] = msg_nametype(msg);
952 anc_data[1] = msg_namelower(msg);
953 anc_data[2] = msg_namelower(msg);
954 break;
955 case TIPC_MCAST_MSG:
3546c750 956 has_name = 1;
b97bf3fd
PL
957 anc_data[0] = msg_nametype(msg);
958 anc_data[1] = msg_namelower(msg);
959 anc_data[2] = msg_nameupper(msg);
960 break;
961 case TIPC_CONN_MSG:
3546c750 962 has_name = (tport->conn_type != 0);
b97bf3fd
PL
963 anc_data[0] = tport->conn_type;
964 anc_data[1] = tport->conn_instance;
965 anc_data[2] = tport->conn_instance;
966 break;
967 default:
3546c750 968 has_name = 0;
b97bf3fd 969 }
2db9983a
AS
970 if (has_name) {
971 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
972 if (res)
973 return res;
974 }
b97bf3fd
PL
975
976 return 0;
977}
978
9bbb4ecc
YX
979static int tipc_wait_for_rcvmsg(struct socket *sock, long timeo)
980{
981 struct sock *sk = sock->sk;
982 DEFINE_WAIT(wait);
983 int err;
984
985 for (;;) {
986 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
987 if (skb_queue_empty(&sk->sk_receive_queue)) {
988 if (sock->state == SS_DISCONNECTING) {
989 err = -ENOTCONN;
990 break;
991 }
992 release_sock(sk);
993 timeo = schedule_timeout(timeo);
994 lock_sock(sk);
995 }
996 err = 0;
997 if (!skb_queue_empty(&sk->sk_receive_queue))
998 break;
999 err = sock_intr_errno(timeo);
1000 if (signal_pending(current))
1001 break;
1002 err = -EAGAIN;
1003 if (!timeo)
1004 break;
1005 }
1006 finish_wait(sk_sleep(sk), &wait);
1007 return err;
1008}
1009
c4307285 1010/**
247f0f3c 1011 * tipc_recvmsg - receive packet-oriented message
b97bf3fd
PL
1012 * @iocb: (unused)
1013 * @m: descriptor for message info
1014 * @buf_len: total size of user buffer area
1015 * @flags: receive flags
c4307285 1016 *
b97bf3fd
PL
1017 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1018 * If the complete message doesn't fit in user area, truncate it.
1019 *
1020 * Returns size of returned message data, errno otherwise
1021 */
247f0f3c
YX
1022static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1023 struct msghdr *m, size_t buf_len, int flags)
b97bf3fd 1024{
0c3141e9
AS
1025 struct sock *sk = sock->sk;
1026 struct tipc_port *tport = tipc_sk_port(sk);
b97bf3fd
PL
1027 struct sk_buff *buf;
1028 struct tipc_msg *msg;
9bbb4ecc 1029 long timeo;
b97bf3fd
PL
1030 unsigned int sz;
1031 u32 err;
1032 int res;
1033
0c3141e9 1034 /* Catch invalid receive requests */
b97bf3fd
PL
1035 if (unlikely(!buf_len))
1036 return -EINVAL;
1037
0c3141e9 1038 lock_sock(sk);
b97bf3fd 1039
0c3141e9
AS
1040 if (unlikely(sock->state == SS_UNCONNECTED)) {
1041 res = -ENOTCONN;
b97bf3fd
PL
1042 goto exit;
1043 }
1044
9bbb4ecc 1045 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
0c3141e9 1046restart:
b97bf3fd 1047
0c3141e9 1048 /* Look for a message in receive queue; wait if necessary */
9bbb4ecc
YX
1049 res = tipc_wait_for_rcvmsg(sock, timeo);
1050 if (res)
1051 goto exit;
b97bf3fd 1052
0c3141e9 1053 /* Look at first message in receive queue */
0c3141e9 1054 buf = skb_peek(&sk->sk_receive_queue);
b97bf3fd
PL
1055 msg = buf_msg(buf);
1056 sz = msg_data_sz(msg);
1057 err = msg_errcode(msg);
1058
b97bf3fd 1059 /* Discard an empty non-errored message & try again */
b97bf3fd 1060 if ((!sz) && (!err)) {
0c3141e9 1061 advance_rx_queue(sk);
b97bf3fd
PL
1062 goto restart;
1063 }
1064
1065 /* Capture sender's address (optional) */
b97bf3fd
PL
1066 set_orig_addr(m, msg);
1067
1068 /* Capture ancillary data (optional) */
0c3141e9
AS
1069 res = anc_data_recv(m, msg, tport);
1070 if (res)
b97bf3fd
PL
1071 goto exit;
1072
1073 /* Capture message data (if valid) & compute return value (always) */
b97bf3fd
PL
1074 if (!err) {
1075 if (unlikely(buf_len < sz)) {
1076 sz = buf_len;
1077 m->msg_flags |= MSG_TRUNC;
1078 }
0232fd0a
AS
1079 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1080 m->msg_iov, sz);
1081 if (res)
b97bf3fd 1082 goto exit;
b97bf3fd
PL
1083 res = sz;
1084 } else {
1085 if ((sock->state == SS_READY) ||
1086 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1087 res = 0;
1088 else
1089 res = -ECONNRESET;
1090 }
1091
1092 /* Consume received message (optional) */
b97bf3fd 1093 if (likely(!(flags & MSG_PEEK))) {
99009806 1094 if ((sock->state != SS_READY) &&
0c3141e9
AS
1095 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1096 tipc_acknowledge(tport->ref, tport->conn_unacked);
1097 advance_rx_queue(sk);
c4307285 1098 }
b97bf3fd 1099exit:
0c3141e9 1100 release_sock(sk);
b97bf3fd
PL
1101 return res;
1102}
1103
c4307285 1104/**
247f0f3c 1105 * tipc_recv_stream - receive stream-oriented data
b97bf3fd
PL
1106 * @iocb: (unused)
1107 * @m: descriptor for message info
1108 * @buf_len: total size of user buffer area
1109 * @flags: receive flags
c4307285
YH
1110 *
1111 * Used for SOCK_STREAM messages only. If not enough data is available
b97bf3fd
PL
1112 * will optionally wait for more; never truncates data.
1113 *
1114 * Returns size of returned message data, errno otherwise
1115 */
247f0f3c
YX
1116static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1117 struct msghdr *m, size_t buf_len, int flags)
b97bf3fd 1118{
0c3141e9
AS
1119 struct sock *sk = sock->sk;
1120 struct tipc_port *tport = tipc_sk_port(sk);
b97bf3fd
PL
1121 struct sk_buff *buf;
1122 struct tipc_msg *msg;
9bbb4ecc 1123 long timeo;
b97bf3fd 1124 unsigned int sz;
3720d40b 1125 int sz_to_copy, target, needed;
b97bf3fd 1126 int sz_copied = 0;
b97bf3fd 1127 u32 err;
0c3141e9 1128 int res = 0;
b97bf3fd 1129
0c3141e9 1130 /* Catch invalid receive attempts */
b97bf3fd
PL
1131 if (unlikely(!buf_len))
1132 return -EINVAL;
1133
0c3141e9 1134 lock_sock(sk);
b97bf3fd 1135
9bbb4ecc 1136 if (unlikely(sock->state == SS_UNCONNECTED)) {
0c3141e9 1137 res = -ENOTCONN;
b97bf3fd
PL
1138 goto exit;
1139 }
1140
3720d40b 1141 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
9bbb4ecc 1142 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
b97bf3fd 1143
617d3c7a 1144restart:
0c3141e9 1145 /* Look for a message in receive queue; wait if necessary */
9bbb4ecc
YX
1146 res = tipc_wait_for_rcvmsg(sock, timeo);
1147 if (res)
1148 goto exit;
b97bf3fd 1149
0c3141e9 1150 /* Look at first message in receive queue */
0c3141e9 1151 buf = skb_peek(&sk->sk_receive_queue);
b97bf3fd
PL
1152 msg = buf_msg(buf);
1153 sz = msg_data_sz(msg);
1154 err = msg_errcode(msg);
1155
1156 /* Discard an empty non-errored message & try again */
b97bf3fd 1157 if ((!sz) && (!err)) {
0c3141e9 1158 advance_rx_queue(sk);
b97bf3fd
PL
1159 goto restart;
1160 }
1161
1162 /* Optionally capture sender's address & ancillary data of first msg */
b97bf3fd
PL
1163 if (sz_copied == 0) {
1164 set_orig_addr(m, msg);
0c3141e9
AS
1165 res = anc_data_recv(m, msg, tport);
1166 if (res)
b97bf3fd
PL
1167 goto exit;
1168 }
1169
1170 /* Capture message data (if valid) & compute return value (always) */
b97bf3fd 1171 if (!err) {
0232fd0a 1172 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
b97bf3fd 1173
0232fd0a 1174 sz -= offset;
b97bf3fd
PL
1175 needed = (buf_len - sz_copied);
1176 sz_to_copy = (sz <= needed) ? sz : needed;
0232fd0a
AS
1177
1178 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1179 m->msg_iov, sz_to_copy);
1180 if (res)
b97bf3fd 1181 goto exit;
0232fd0a 1182
b97bf3fd
PL
1183 sz_copied += sz_to_copy;
1184
1185 if (sz_to_copy < sz) {
1186 if (!(flags & MSG_PEEK))
0232fd0a
AS
1187 TIPC_SKB_CB(buf)->handle =
1188 (void *)(unsigned long)(offset + sz_to_copy);
b97bf3fd
PL
1189 goto exit;
1190 }
b97bf3fd
PL
1191 } else {
1192 if (sz_copied != 0)
1193 goto exit; /* can't add error msg to valid data */
1194
1195 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1196 res = 0;
1197 else
1198 res = -ECONNRESET;
1199 }
1200
1201 /* Consume received message (optional) */
b97bf3fd 1202 if (likely(!(flags & MSG_PEEK))) {
0c3141e9
AS
1203 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1204 tipc_acknowledge(tport->ref, tport->conn_unacked);
1205 advance_rx_queue(sk);
c4307285 1206 }
b97bf3fd
PL
1207
1208 /* Loop around if more data is required */
f64f9e71
JP
1209 if ((sz_copied < buf_len) && /* didn't get all requested data */
1210 (!skb_queue_empty(&sk->sk_receive_queue) ||
3720d40b 1211 (sz_copied < target)) && /* and more is ready or required */
f64f9e71
JP
1212 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1213 (!err)) /* and haven't reached a FIN */
b97bf3fd
PL
1214 goto restart;
1215
1216exit:
0c3141e9 1217 release_sock(sk);
a3b0a5a9 1218 return sz_copied ? sz_copied : res;
b97bf3fd
PL
1219}
1220
f288bef4
YX
1221/**
1222 * tipc_write_space - wake up thread if port congestion is released
1223 * @sk: socket
1224 */
1225static void tipc_write_space(struct sock *sk)
1226{
1227 struct socket_wq *wq;
1228
1229 rcu_read_lock();
1230 wq = rcu_dereference(sk->sk_wq);
1231 if (wq_has_sleeper(wq))
1232 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1233 POLLWRNORM | POLLWRBAND);
1234 rcu_read_unlock();
1235}
1236
1237/**
1238 * tipc_data_ready - wake up threads to indicate messages have been received
1239 * @sk: socket
1240 * @len: the length of messages
1241 */
1242static void tipc_data_ready(struct sock *sk, int len)
1243{
1244 struct socket_wq *wq;
1245
1246 rcu_read_lock();
1247 wq = rcu_dereference(sk->sk_wq);
1248 if (wq_has_sleeper(wq))
1249 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1250 POLLRDNORM | POLLRDBAND);
1251 rcu_read_unlock();
1252}
1253
7e6c131e
YX
1254/**
1255 * filter_connect - Handle all incoming messages for a connection-based socket
8826cde6 1256 * @port: TIPC port
7e6c131e
YX
1257 * @msg: message
1258 *
1259 * Returns TIPC error status code and socket error status code
1260 * once it encounters some errors
1261 */
8826cde6 1262static u32 filter_connect(struct tipc_port *port, struct sk_buff **buf)
7e6c131e 1263{
8826cde6
JPM
1264 struct sock *sk = tipc_port_to_sk(port);
1265 struct socket *sock = sk->sk_socket;
7e6c131e 1266 struct tipc_msg *msg = buf_msg(*buf);
8826cde6 1267
7e6c131e 1268 u32 retval = TIPC_ERR_NO_PORT;
584d24b3 1269 int res;
7e6c131e
YX
1270
1271 if (msg_mcast(msg))
1272 return retval;
1273
1274 switch ((int)sock->state) {
1275 case SS_CONNECTED:
1276 /* Accept only connection-based messages sent by peer */
8826cde6 1277 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
7e6c131e
YX
1278 if (unlikely(msg_errcode(msg))) {
1279 sock->state = SS_DISCONNECTING;
8826cde6 1280 __tipc_port_disconnect(port);
7e6c131e
YX
1281 }
1282 retval = TIPC_OK;
1283 }
1284 break;
1285 case SS_CONNECTING:
1286 /* Accept only ACK or NACK message */
584d24b3
YX
1287 if (unlikely(msg_errcode(msg))) {
1288 sock->state = SS_DISCONNECTING;
2c8d8518 1289 sk->sk_err = ECONNREFUSED;
584d24b3
YX
1290 retval = TIPC_OK;
1291 break;
1292 }
1293
1294 if (unlikely(!msg_connected(msg)))
1295 break;
1296
1297 res = auto_connect(sock, msg);
1298 if (res) {
1299 sock->state = SS_DISCONNECTING;
2c8d8518 1300 sk->sk_err = -res;
7e6c131e 1301 retval = TIPC_OK;
584d24b3
YX
1302 break;
1303 }
1304
1305 /* If an incoming message is an 'ACK-', it should be
1306 * discarded here because it doesn't contain useful
1307 * data. In addition, we should try to wake up
1308 * connect() routine if sleeping.
1309 */
1310 if (msg_data_sz(msg) == 0) {
1311 kfree_skb(*buf);
1312 *buf = NULL;
1313 if (waitqueue_active(sk_sleep(sk)))
1314 wake_up_interruptible(sk_sleep(sk));
1315 }
1316 retval = TIPC_OK;
7e6c131e
YX
1317 break;
1318 case SS_LISTENING:
1319 case SS_UNCONNECTED:
1320 /* Accept only SYN message */
1321 if (!msg_connected(msg) && !(msg_errcode(msg)))
1322 retval = TIPC_OK;
1323 break;
1324 case SS_DISCONNECTING:
1325 break;
1326 default:
1327 pr_err("Unknown socket state %u\n", sock->state);
1328 }
1329 return retval;
1330}
1331
aba79f33
YX
1332/**
1333 * rcvbuf_limit - get proper overload limit of socket receive queue
1334 * @sk: socket
1335 * @buf: message
1336 *
1337 * For all connection oriented messages, irrespective of importance,
1338 * the default overload value (i.e. 67MB) is set as limit.
1339 *
1340 * For all connectionless messages, by default new queue limits are
1341 * as belows:
1342 *
cc79dd1b
YX
1343 * TIPC_LOW_IMPORTANCE (4 MB)
1344 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1345 * TIPC_HIGH_IMPORTANCE (16 MB)
1346 * TIPC_CRITICAL_IMPORTANCE (32 MB)
aba79f33
YX
1347 *
1348 * Returns overload limit according to corresponding message importance
1349 */
1350static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1351{
1352 struct tipc_msg *msg = buf_msg(buf);
aba79f33
YX
1353
1354 if (msg_connected(msg))
0cee6bbe 1355 return sysctl_tipc_rmem[2];
1356
1357 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1358 msg_importance(msg);
aba79f33
YX
1359}
1360
c4307285 1361/**
0c3141e9
AS
1362 * filter_rcv - validate incoming message
1363 * @sk: socket
b97bf3fd 1364 * @buf: message
c4307285 1365 *
0c3141e9
AS
1366 * Enqueues message on receive queue if acceptable; optionally handles
1367 * disconnect indication for a connected socket.
1368 *
1369 * Called with socket lock already taken; port lock may also be taken.
c4307285 1370 *
b97bf3fd
PL
1371 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1372 */
0c3141e9 1373static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
b97bf3fd 1374{
0c3141e9 1375 struct socket *sock = sk->sk_socket;
b97bf3fd 1376 struct tipc_msg *msg = buf_msg(buf);
aba79f33 1377 unsigned int limit = rcvbuf_limit(sk, buf);
7e6c131e 1378 u32 res = TIPC_OK;
b97bf3fd 1379
b97bf3fd 1380 /* Reject message if it is wrong sort of message for socket */
aad58547
AS
1381 if (msg_type(msg) > TIPC_DIRECT_MSG)
1382 return TIPC_ERR_NO_PORT;
0c3141e9 1383
b97bf3fd 1384 if (sock->state == SS_READY) {
b29f1428 1385 if (msg_connected(msg))
b97bf3fd 1386 return TIPC_ERR_NO_PORT;
b97bf3fd 1387 } else {
8826cde6 1388 res = filter_connect(tipc_sk_port(sk), &buf);
7e6c131e
YX
1389 if (res != TIPC_OK || buf == NULL)
1390 return res;
b97bf3fd
PL
1391 }
1392
1393 /* Reject message if there isn't room to queue it */
aba79f33
YX
1394 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1395 return TIPC_ERR_OVERLOAD;
b97bf3fd 1396
aba79f33 1397 /* Enqueue message */
40682432 1398 TIPC_SKB_CB(buf)->handle = NULL;
0c3141e9 1399 __skb_queue_tail(&sk->sk_receive_queue, buf);
aba79f33 1400 skb_set_owner_r(buf, sk);
0c3141e9 1401
f288bef4 1402 sk->sk_data_ready(sk, 0);
0c3141e9
AS
1403 return TIPC_OK;
1404}
b97bf3fd 1405
0c3141e9
AS
1406/**
1407 * backlog_rcv - handle incoming message from backlog queue
1408 * @sk: socket
1409 * @buf: message
1410 *
1411 * Caller must hold socket lock, but not port lock.
1412 *
1413 * Returns 0
1414 */
0c3141e9
AS
1415static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1416{
1417 u32 res;
1418
1419 res = filter_rcv(sk, buf);
1420 if (res)
1421 tipc_reject_msg(buf, res);
1422 return 0;
1423}
1424
1425/**
24be34b5
JPM
1426 * tipc_sk_rcv - handle incoming message
1427 * @sk: socket receiving message
0c3141e9
AS
1428 * @buf: message
1429 *
1430 * Called with port lock already taken.
1431 *
1432 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1433 */
24be34b5 1434u32 tipc_sk_rcv(struct sock *sk, struct sk_buff *buf)
0c3141e9 1435{
0c3141e9
AS
1436 u32 res;
1437
1438 /*
1439 * Process message if socket is unlocked; otherwise add to backlog queue
1440 *
1441 * This code is based on sk_receive_skb(), but must be distinct from it
1442 * since a TIPC-specific filter/reject mechanism is utilized
1443 */
0c3141e9
AS
1444 bh_lock_sock(sk);
1445 if (!sock_owned_by_user(sk)) {
1446 res = filter_rcv(sk, buf);
1447 } else {
aba79f33 1448 if (sk_add_backlog(sk, buf, rcvbuf_limit(sk, buf)))
53eecb1b
ZY
1449 res = TIPC_ERR_OVERLOAD;
1450 else
1451 res = TIPC_OK;
0c3141e9
AS
1452 }
1453 bh_unlock_sock(sk);
1454
1455 return res;
b97bf3fd
PL
1456}
1457
78eb3a53
YX
1458static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1459{
1460 struct sock *sk = sock->sk;
1461 DEFINE_WAIT(wait);
1462 int done;
1463
1464 do {
1465 int err = sock_error(sk);
1466 if (err)
1467 return err;
1468 if (!*timeo_p)
1469 return -ETIMEDOUT;
1470 if (signal_pending(current))
1471 return sock_intr_errno(*timeo_p);
1472
1473 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1474 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1475 finish_wait(sk_sleep(sk), &wait);
1476 } while (!done);
1477 return 0;
1478}
1479
b97bf3fd 1480/**
247f0f3c 1481 * tipc_connect - establish a connection to another TIPC port
b97bf3fd
PL
1482 * @sock: socket structure
1483 * @dest: socket address for destination port
1484 * @destlen: size of socket address data structure
0c3141e9 1485 * @flags: file-related flags associated with socket
b97bf3fd
PL
1486 *
1487 * Returns 0 on success, errno otherwise
1488 */
247f0f3c
YX
1489static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1490 int destlen, int flags)
b97bf3fd 1491{
0c3141e9 1492 struct sock *sk = sock->sk;
b89741a0
AS
1493 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1494 struct msghdr m = {NULL,};
78eb3a53
YX
1495 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1496 socket_state previous;
b89741a0
AS
1497 int res;
1498
0c3141e9
AS
1499 lock_sock(sk);
1500
b89741a0 1501 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
0c3141e9
AS
1502 if (sock->state == SS_READY) {
1503 res = -EOPNOTSUPP;
1504 goto exit;
1505 }
b89741a0 1506
b89741a0
AS
1507 /*
1508 * Reject connection attempt using multicast address
1509 *
1510 * Note: send_msg() validates the rest of the address fields,
1511 * so there's no need to do it here
1512 */
0c3141e9
AS
1513 if (dst->addrtype == TIPC_ADDR_MCAST) {
1514 res = -EINVAL;
1515 goto exit;
1516 }
1517
78eb3a53 1518 previous = sock->state;
584d24b3
YX
1519 switch (sock->state) {
1520 case SS_UNCONNECTED:
1521 /* Send a 'SYN-' to destination */
1522 m.msg_name = dest;
1523 m.msg_namelen = destlen;
1524
1525 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1526 * indicate send_msg() is never blocked.
1527 */
1528 if (!timeout)
1529 m.msg_flags = MSG_DONTWAIT;
1530
247f0f3c 1531 res = tipc_sendmsg(NULL, sock, &m, 0);
584d24b3
YX
1532 if ((res < 0) && (res != -EWOULDBLOCK))
1533 goto exit;
1534
1535 /* Just entered SS_CONNECTING state; the only
1536 * difference is that return value in non-blocking
1537 * case is EINPROGRESS, rather than EALREADY.
1538 */
1539 res = -EINPROGRESS;
584d24b3 1540 case SS_CONNECTING:
78eb3a53
YX
1541 if (previous == SS_CONNECTING)
1542 res = -EALREADY;
1543 if (!timeout)
1544 goto exit;
1545 timeout = msecs_to_jiffies(timeout);
1546 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1547 res = tipc_wait_for_connect(sock, &timeout);
584d24b3
YX
1548 break;
1549 case SS_CONNECTED:
1550 res = -EISCONN;
1551 break;
1552 default:
1553 res = -EINVAL;
78eb3a53 1554 break;
b89741a0 1555 }
0c3141e9
AS
1556exit:
1557 release_sock(sk);
b89741a0 1558 return res;
b97bf3fd
PL
1559}
1560
c4307285 1561/**
247f0f3c 1562 * tipc_listen - allow socket to listen for incoming connections
b97bf3fd
PL
1563 * @sock: socket structure
1564 * @len: (unused)
c4307285 1565 *
b97bf3fd
PL
1566 * Returns 0 on success, errno otherwise
1567 */
247f0f3c 1568static int tipc_listen(struct socket *sock, int len)
b97bf3fd 1569{
0c3141e9
AS
1570 struct sock *sk = sock->sk;
1571 int res;
1572
1573 lock_sock(sk);
b97bf3fd 1574
245f3d34 1575 if (sock->state != SS_UNCONNECTED)
0c3141e9
AS
1576 res = -EINVAL;
1577 else {
1578 sock->state = SS_LISTENING;
1579 res = 0;
1580 }
1581
1582 release_sock(sk);
1583 return res;
b97bf3fd
PL
1584}
1585
6398e23c
YX
1586static int tipc_wait_for_accept(struct socket *sock, long timeo)
1587{
1588 struct sock *sk = sock->sk;
1589 DEFINE_WAIT(wait);
1590 int err;
1591
1592 /* True wake-one mechanism for incoming connections: only
1593 * one process gets woken up, not the 'whole herd'.
1594 * Since we do not 'race & poll' for established sockets
1595 * anymore, the common case will execute the loop only once.
1596 */
1597 for (;;) {
1598 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1599 TASK_INTERRUPTIBLE);
1600 if (skb_queue_empty(&sk->sk_receive_queue)) {
1601 release_sock(sk);
1602 timeo = schedule_timeout(timeo);
1603 lock_sock(sk);
1604 }
1605 err = 0;
1606 if (!skb_queue_empty(&sk->sk_receive_queue))
1607 break;
1608 err = -EINVAL;
1609 if (sock->state != SS_LISTENING)
1610 break;
1611 err = sock_intr_errno(timeo);
1612 if (signal_pending(current))
1613 break;
1614 err = -EAGAIN;
1615 if (!timeo)
1616 break;
1617 }
1618 finish_wait(sk_sleep(sk), &wait);
1619 return err;
1620}
1621
c4307285 1622/**
247f0f3c 1623 * tipc_accept - wait for connection request
b97bf3fd
PL
1624 * @sock: listening socket
1625 * @newsock: new socket that is to be connected
1626 * @flags: file-related flags associated with socket
c4307285 1627 *
b97bf3fd
PL
1628 * Returns 0 on success, errno otherwise
1629 */
247f0f3c 1630static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
b97bf3fd 1631{
0fef8f20 1632 struct sock *new_sk, *sk = sock->sk;
b97bf3fd 1633 struct sk_buff *buf;
8826cde6 1634 struct tipc_port *new_port;
0fef8f20 1635 struct tipc_msg *msg;
f9fef18c 1636 struct tipc_portid peer;
0fef8f20 1637 u32 new_ref;
6398e23c 1638 long timeo;
0c3141e9 1639 int res;
b97bf3fd 1640
0c3141e9 1641 lock_sock(sk);
b97bf3fd 1642
0c3141e9
AS
1643 if (sock->state != SS_LISTENING) {
1644 res = -EINVAL;
b97bf3fd
PL
1645 goto exit;
1646 }
6398e23c
YX
1647 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1648 res = tipc_wait_for_accept(sock, timeo);
1649 if (res)
1650 goto exit;
0c3141e9
AS
1651
1652 buf = skb_peek(&sk->sk_receive_queue);
1653
c5fa7b3c 1654 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
0fef8f20
PG
1655 if (res)
1656 goto exit;
b97bf3fd 1657
0fef8f20 1658 new_sk = new_sock->sk;
8826cde6
JPM
1659 new_port = tipc_sk_port(new_sk);
1660 new_ref = new_port->ref;
0fef8f20 1661 msg = buf_msg(buf);
b97bf3fd 1662
0fef8f20
PG
1663 /* we lock on new_sk; but lockdep sees the lock on sk */
1664 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
1665
1666 /*
1667 * Reject any stray messages received by new socket
1668 * before the socket lock was taken (very, very unlikely)
1669 */
1670 reject_rx_queue(new_sk);
1671
1672 /* Connect new socket to it's peer */
f9fef18c
JPM
1673 peer.ref = msg_origport(msg);
1674 peer.node = msg_orignode(msg);
1675 tipc_port_connect(new_ref, &peer);
0fef8f20
PG
1676 new_sock->state = SS_CONNECTED;
1677
3b4f302d 1678 tipc_port_set_importance(new_port, msg_importance(msg));
0fef8f20 1679 if (msg_named(msg)) {
8826cde6
JPM
1680 new_port->conn_type = msg_nametype(msg);
1681 new_port->conn_instance = msg_nameinst(msg);
b97bf3fd 1682 }
0fef8f20
PG
1683
1684 /*
1685 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1686 * Respond to 'SYN+' by queuing it on new socket.
1687 */
1688 if (!msg_data_sz(msg)) {
1689 struct msghdr m = {NULL,};
1690
1691 advance_rx_queue(sk);
247f0f3c 1692 tipc_send_packet(NULL, new_sock, &m, 0);
0fef8f20
PG
1693 } else {
1694 __skb_dequeue(&sk->sk_receive_queue);
1695 __skb_queue_head(&new_sk->sk_receive_queue, buf);
aba79f33 1696 skb_set_owner_r(buf, new_sk);
0fef8f20
PG
1697 }
1698 release_sock(new_sk);
b97bf3fd 1699exit:
0c3141e9 1700 release_sock(sk);
b97bf3fd
PL
1701 return res;
1702}
1703
1704/**
247f0f3c 1705 * tipc_shutdown - shutdown socket connection
b97bf3fd 1706 * @sock: socket structure
e247a8f5 1707 * @how: direction to close (must be SHUT_RDWR)
b97bf3fd
PL
1708 *
1709 * Terminates connection (if necessary), then purges socket's receive queue.
c4307285 1710 *
b97bf3fd
PL
1711 * Returns 0 on success, errno otherwise
1712 */
247f0f3c 1713static int tipc_shutdown(struct socket *sock, int how)
b97bf3fd 1714{
0c3141e9
AS
1715 struct sock *sk = sock->sk;
1716 struct tipc_port *tport = tipc_sk_port(sk);
b97bf3fd
PL
1717 struct sk_buff *buf;
1718 int res;
1719
e247a8f5
AS
1720 if (how != SHUT_RDWR)
1721 return -EINVAL;
b97bf3fd 1722
0c3141e9 1723 lock_sock(sk);
b97bf3fd
PL
1724
1725 switch (sock->state) {
0c3141e9 1726 case SS_CONNECTING:
b97bf3fd
PL
1727 case SS_CONNECTED:
1728
b97bf3fd 1729restart:
617d3c7a 1730 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
0c3141e9
AS
1731 buf = __skb_dequeue(&sk->sk_receive_queue);
1732 if (buf) {
40682432 1733 if (TIPC_SKB_CB(buf)->handle != NULL) {
5f6d9123 1734 kfree_skb(buf);
b97bf3fd
PL
1735 goto restart;
1736 }
247f0f3c 1737 tipc_port_disconnect(tport->ref);
b97bf3fd 1738 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
0c3141e9 1739 } else {
247f0f3c 1740 tipc_port_shutdown(tport->ref);
b97bf3fd 1741 }
0c3141e9
AS
1742
1743 sock->state = SS_DISCONNECTING;
b97bf3fd
PL
1744
1745 /* fall through */
1746
1747 case SS_DISCONNECTING:
1748
75031151 1749 /* Discard any unreceived messages */
57467e56 1750 __skb_queue_purge(&sk->sk_receive_queue);
75031151
YX
1751
1752 /* Wake up anyone sleeping in poll */
1753 sk->sk_state_change(sk);
b97bf3fd
PL
1754 res = 0;
1755 break;
1756
1757 default:
1758 res = -ENOTCONN;
1759 }
1760
0c3141e9 1761 release_sock(sk);
b97bf3fd
PL
1762 return res;
1763}
1764
1765/**
247f0f3c 1766 * tipc_setsockopt - set socket option
b97bf3fd
PL
1767 * @sock: socket structure
1768 * @lvl: option level
1769 * @opt: option identifier
1770 * @ov: pointer to new option value
1771 * @ol: length of option value
c4307285
YH
1772 *
1773 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
b97bf3fd 1774 * (to ease compatibility).
c4307285 1775 *
b97bf3fd
PL
1776 * Returns 0 on success, errno otherwise
1777 */
247f0f3c
YX
1778static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
1779 char __user *ov, unsigned int ol)
b97bf3fd 1780{
0c3141e9
AS
1781 struct sock *sk = sock->sk;
1782 struct tipc_port *tport = tipc_sk_port(sk);
b97bf3fd
PL
1783 u32 value;
1784 int res;
1785
c4307285
YH
1786 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1787 return 0;
b97bf3fd
PL
1788 if (lvl != SOL_TIPC)
1789 return -ENOPROTOOPT;
1790 if (ol < sizeof(value))
1791 return -EINVAL;
2db9983a
AS
1792 res = get_user(value, (u32 __user *)ov);
1793 if (res)
b97bf3fd
PL
1794 return res;
1795
0c3141e9 1796 lock_sock(sk);
c4307285 1797
b97bf3fd
PL
1798 switch (opt) {
1799 case TIPC_IMPORTANCE:
3b4f302d 1800 tipc_port_set_importance(tport, value);
b97bf3fd
PL
1801 break;
1802 case TIPC_SRC_DROPPABLE:
1803 if (sock->type != SOCK_STREAM)
3b4f302d 1804 tipc_port_set_unreliable(tport, value);
c4307285 1805 else
b97bf3fd
PL
1806 res = -ENOPROTOOPT;
1807 break;
1808 case TIPC_DEST_DROPPABLE:
3b4f302d 1809 tipc_port_set_unreturnable(tport, value);
b97bf3fd
PL
1810 break;
1811 case TIPC_CONN_TIMEOUT:
a0f40f02 1812 tipc_sk(sk)->conn_timeout = value;
0c3141e9 1813 /* no need to set "res", since already 0 at this point */
b97bf3fd
PL
1814 break;
1815 default:
1816 res = -EINVAL;
1817 }
1818
0c3141e9
AS
1819 release_sock(sk);
1820
b97bf3fd
PL
1821 return res;
1822}
1823
1824/**
247f0f3c 1825 * tipc_getsockopt - get socket option
b97bf3fd
PL
1826 * @sock: socket structure
1827 * @lvl: option level
1828 * @opt: option identifier
1829 * @ov: receptacle for option value
1830 * @ol: receptacle for length of option value
c4307285
YH
1831 *
1832 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
b97bf3fd 1833 * (to ease compatibility).
c4307285 1834 *
b97bf3fd
PL
1835 * Returns 0 on success, errno otherwise
1836 */
247f0f3c
YX
1837static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
1838 char __user *ov, int __user *ol)
b97bf3fd 1839{
0c3141e9
AS
1840 struct sock *sk = sock->sk;
1841 struct tipc_port *tport = tipc_sk_port(sk);
c4307285 1842 int len;
b97bf3fd 1843 u32 value;
c4307285 1844 int res;
b97bf3fd 1845
c4307285
YH
1846 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1847 return put_user(0, ol);
b97bf3fd
PL
1848 if (lvl != SOL_TIPC)
1849 return -ENOPROTOOPT;
2db9983a
AS
1850 res = get_user(len, ol);
1851 if (res)
c4307285 1852 return res;
b97bf3fd 1853
0c3141e9 1854 lock_sock(sk);
b97bf3fd
PL
1855
1856 switch (opt) {
1857 case TIPC_IMPORTANCE:
3b4f302d 1858 value = tipc_port_importance(tport);
b97bf3fd
PL
1859 break;
1860 case TIPC_SRC_DROPPABLE:
3b4f302d 1861 value = tipc_port_unreliable(tport);
b97bf3fd
PL
1862 break;
1863 case TIPC_DEST_DROPPABLE:
3b4f302d 1864 value = tipc_port_unreturnable(tport);
b97bf3fd
PL
1865 break;
1866 case TIPC_CONN_TIMEOUT:
a0f40f02 1867 value = tipc_sk(sk)->conn_timeout;
0c3141e9 1868 /* no need to set "res", since already 0 at this point */
b97bf3fd 1869 break;
0e65967e 1870 case TIPC_NODE_RECVQ_DEPTH:
9da3d475 1871 value = 0; /* was tipc_queue_size, now obsolete */
6650613d 1872 break;
0e65967e 1873 case TIPC_SOCK_RECVQ_DEPTH:
6650613d 1874 value = skb_queue_len(&sk->sk_receive_queue);
1875 break;
b97bf3fd
PL
1876 default:
1877 res = -EINVAL;
1878 }
1879
0c3141e9
AS
1880 release_sock(sk);
1881
25860c3b
PG
1882 if (res)
1883 return res; /* "get" failed */
b97bf3fd 1884
25860c3b
PG
1885 if (len < sizeof(value))
1886 return -EINVAL;
1887
1888 if (copy_to_user(ov, &value, sizeof(value)))
1889 return -EFAULT;
1890
1891 return put_user(sizeof(value), ol);
b97bf3fd
PL
1892}
1893
ae86b9e3
BH
1894/* Protocol switches for the various types of TIPC sockets */
1895
bca65eae 1896static const struct proto_ops msg_ops = {
0e65967e 1897 .owner = THIS_MODULE,
b97bf3fd 1898 .family = AF_TIPC,
247f0f3c
YX
1899 .release = tipc_release,
1900 .bind = tipc_bind,
1901 .connect = tipc_connect,
5eee6a6d 1902 .socketpair = sock_no_socketpair,
245f3d34 1903 .accept = sock_no_accept,
247f0f3c
YX
1904 .getname = tipc_getname,
1905 .poll = tipc_poll,
5eee6a6d 1906 .ioctl = sock_no_ioctl,
245f3d34 1907 .listen = sock_no_listen,
247f0f3c
YX
1908 .shutdown = tipc_shutdown,
1909 .setsockopt = tipc_setsockopt,
1910 .getsockopt = tipc_getsockopt,
1911 .sendmsg = tipc_sendmsg,
1912 .recvmsg = tipc_recvmsg,
8238745a
YH
1913 .mmap = sock_no_mmap,
1914 .sendpage = sock_no_sendpage
b97bf3fd
PL
1915};
1916
bca65eae 1917static const struct proto_ops packet_ops = {
0e65967e 1918 .owner = THIS_MODULE,
b97bf3fd 1919 .family = AF_TIPC,
247f0f3c
YX
1920 .release = tipc_release,
1921 .bind = tipc_bind,
1922 .connect = tipc_connect,
5eee6a6d 1923 .socketpair = sock_no_socketpair,
247f0f3c
YX
1924 .accept = tipc_accept,
1925 .getname = tipc_getname,
1926 .poll = tipc_poll,
5eee6a6d 1927 .ioctl = sock_no_ioctl,
247f0f3c
YX
1928 .listen = tipc_listen,
1929 .shutdown = tipc_shutdown,
1930 .setsockopt = tipc_setsockopt,
1931 .getsockopt = tipc_getsockopt,
1932 .sendmsg = tipc_send_packet,
1933 .recvmsg = tipc_recvmsg,
8238745a
YH
1934 .mmap = sock_no_mmap,
1935 .sendpage = sock_no_sendpage
b97bf3fd
PL
1936};
1937
bca65eae 1938static const struct proto_ops stream_ops = {
0e65967e 1939 .owner = THIS_MODULE,
b97bf3fd 1940 .family = AF_TIPC,
247f0f3c
YX
1941 .release = tipc_release,
1942 .bind = tipc_bind,
1943 .connect = tipc_connect,
5eee6a6d 1944 .socketpair = sock_no_socketpair,
247f0f3c
YX
1945 .accept = tipc_accept,
1946 .getname = tipc_getname,
1947 .poll = tipc_poll,
5eee6a6d 1948 .ioctl = sock_no_ioctl,
247f0f3c
YX
1949 .listen = tipc_listen,
1950 .shutdown = tipc_shutdown,
1951 .setsockopt = tipc_setsockopt,
1952 .getsockopt = tipc_getsockopt,
1953 .sendmsg = tipc_send_stream,
1954 .recvmsg = tipc_recv_stream,
8238745a
YH
1955 .mmap = sock_no_mmap,
1956 .sendpage = sock_no_sendpage
b97bf3fd
PL
1957};
1958
bca65eae 1959static const struct net_proto_family tipc_family_ops = {
0e65967e 1960 .owner = THIS_MODULE,
b97bf3fd 1961 .family = AF_TIPC,
c5fa7b3c 1962 .create = tipc_sk_create
b97bf3fd
PL
1963};
1964
1965static struct proto tipc_proto = {
1966 .name = "TIPC",
1967 .owner = THIS_MODULE,
cc79dd1b
YX
1968 .obj_size = sizeof(struct tipc_sock),
1969 .sysctl_rmem = sysctl_tipc_rmem
b97bf3fd
PL
1970};
1971
c5fa7b3c
YX
1972static struct proto tipc_proto_kern = {
1973 .name = "TIPC",
1974 .obj_size = sizeof(struct tipc_sock),
1975 .sysctl_rmem = sysctl_tipc_rmem
1976};
1977
b97bf3fd 1978/**
4323add6 1979 * tipc_socket_init - initialize TIPC socket interface
c4307285 1980 *
b97bf3fd
PL
1981 * Returns 0 on success, errno otherwise
1982 */
4323add6 1983int tipc_socket_init(void)
b97bf3fd
PL
1984{
1985 int res;
1986
c4307285 1987 res = proto_register(&tipc_proto, 1);
b97bf3fd 1988 if (res) {
2cf8aa19 1989 pr_err("Failed to register TIPC protocol type\n");
b97bf3fd
PL
1990 goto out;
1991 }
1992
1993 res = sock_register(&tipc_family_ops);
1994 if (res) {
2cf8aa19 1995 pr_err("Failed to register TIPC socket type\n");
b97bf3fd
PL
1996 proto_unregister(&tipc_proto);
1997 goto out;
1998 }
b97bf3fd
PL
1999 out:
2000 return res;
2001}
2002
2003/**
4323add6 2004 * tipc_socket_stop - stop TIPC socket interface
b97bf3fd 2005 */
4323add6 2006void tipc_socket_stop(void)
b97bf3fd 2007{
b97bf3fd
PL
2008 sock_unregister(tipc_family_ops.family);
2009 proto_unregister(&tipc_proto);
2010}