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