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