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