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