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