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