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