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