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