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