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