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