tipc: replace port pointer with socket pointer in registry
[deliverable/linux.git] / net / tipc / socket.c
CommitLineData
b97bf3fd 1/*
02c00c2a 2 * net/tipc/socket.c: TIPC socket API
c4307285 3 *
8826cde6 4 * Copyright (c) 2001-2007, 2012-2014, 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"
e2dafe87 39#include "name_table.h"
78acb1f9 40#include "node.h"
e2dafe87 41#include "link.h"
2cf8aa19 42#include <linux/export.h>
5a9ee0be 43#include "config.h"
2cf8aa19 44
b97bf3fd
PL
45#define SS_LISTENING -1 /* socket is listening */
46#define SS_READY -2 /* socket is connectionless */
47
3654ea02 48#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
dadebc00 49#define CONN_PROBING_INTERVAL 3600000 /* [ms] => 1 h */
ac0074ee 50#define TIPC_FWD_MSG 1
b97bf3fd 51
4f4482dc 52static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
676d2369 53static void tipc_data_ready(struct sock *sk);
f288bef4 54static void tipc_write_space(struct sock *sk);
247f0f3c
YX
55static int tipc_release(struct socket *sock);
56static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
0abd8ff2 57static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
57289015 58static void tipc_sk_timeout(unsigned long ref);
b97bf3fd 59
bca65eae
FW
60static const struct proto_ops packet_ops;
61static const struct proto_ops stream_ops;
62static const struct proto_ops msg_ops;
b97bf3fd
PL
63
64static struct proto tipc_proto;
c5fa7b3c 65static struct proto tipc_proto_kern;
b97bf3fd 66
c4307285 67/*
0c3141e9
AS
68 * Revised TIPC socket locking policy:
69 *
70 * Most socket operations take the standard socket lock when they start
71 * and hold it until they finish (or until they need to sleep). Acquiring
72 * this lock grants the owner exclusive access to the fields of the socket
73 * data structures, with the exception of the backlog queue. A few socket
74 * operations can be done without taking the socket lock because they only
75 * read socket information that never changes during the life of the socket.
76 *
77 * Socket operations may acquire the lock for the associated TIPC port if they
78 * need to perform an operation on the port. If any routine needs to acquire
79 * both the socket lock and the port lock it must take the socket lock first
80 * to avoid the risk of deadlock.
81 *
82 * The dispatcher handling incoming messages cannot grab the socket lock in
83 * the standard fashion, since invoked it runs at the BH level and cannot block.
84 * Instead, it checks to see if the socket lock is currently owned by someone,
85 * and either handles the message itself or adds it to the socket's backlog
86 * queue; in the latter case the queued message is processed once the process
87 * owning the socket lock releases it.
88 *
89 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
90 * the problem of a blocked socket operation preventing any other operations
91 * from occurring. However, applications must be careful if they have
92 * multiple threads trying to send (or receive) on the same socket, as these
93 * operations might interfere with each other. For example, doing a connect
94 * and a receive at the same time might allow the receive to consume the
95 * ACK message meant for the connect. While additional work could be done
96 * to try and overcome this, it doesn't seem to be worthwhile at the present.
97 *
98 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
99 * that another operation that must be performed in a non-blocking manner is
100 * not delayed for very long because the lock has already been taken.
101 *
102 * NOTE: This code assumes that certain fields of a port/socket pair are
103 * constant over its lifetime; such fields can be examined without taking
104 * the socket lock and/or port lock, and do not need to be re-read even
105 * after resuming processing after waiting. These fields include:
106 * - socket type
107 * - pointer to socket sk structure (aka tipc_sock structure)
108 * - pointer to port structure
109 * - port reference
110 */
111
8826cde6
JPM
112#include "socket.h"
113
5a9ee0be
JPM
114/* tipc_sk_lock_next: find & lock next socket in registry from given port number
115*/
116static struct tipc_sock *tipc_sk_lock_next(u32 *ref)
117{
9b50fd08 118 return (struct tipc_sock *)tipc_ref_lock_next(ref);
5a9ee0be
JPM
119}
120
0c3141e9
AS
121/**
122 * advance_rx_queue - discard first buffer in socket receive queue
123 *
124 * Caller must hold socket lock
b97bf3fd 125 */
0c3141e9 126static void advance_rx_queue(struct sock *sk)
b97bf3fd 127{
5f6d9123 128 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
b97bf3fd
PL
129}
130
b97bf3fd 131/**
0c3141e9
AS
132 * reject_rx_queue - reject all buffers in socket receive queue
133 *
134 * Caller must hold socket lock
b97bf3fd 135 */
0c3141e9 136static void reject_rx_queue(struct sock *sk)
b97bf3fd 137{
0c3141e9 138 struct sk_buff *buf;
8db1bae3 139 u32 dnode;
0c3141e9 140
8db1bae3
JPM
141 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
142 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
9fbfb8b1 143 tipc_link_xmit(buf, dnode, 0);
8db1bae3 144 }
b97bf3fd
PL
145}
146
147/**
c5fa7b3c 148 * tipc_sk_create - create a TIPC socket
0c3141e9 149 * @net: network namespace (must be default network)
b97bf3fd
PL
150 * @sock: pre-allocated socket structure
151 * @protocol: protocol indicator (must be 0)
3f378b68 152 * @kern: caused by kernel or by userspace?
c4307285 153 *
0c3141e9
AS
154 * This routine creates additional data structures used by the TIPC socket,
155 * initializes them, and links them together.
b97bf3fd
PL
156 *
157 * Returns 0 on success, errno otherwise
158 */
58ed9442
JPM
159static int tipc_sk_create(struct net *net, struct socket *sock,
160 int protocol, int kern)
b97bf3fd 161{
0c3141e9
AS
162 const struct proto_ops *ops;
163 socket_state state;
b97bf3fd 164 struct sock *sk;
58ed9442
JPM
165 struct tipc_sock *tsk;
166 struct tipc_port *port;
5b8fa7ce 167 struct tipc_msg *msg;
58ed9442 168 u32 ref;
0c3141e9
AS
169
170 /* Validate arguments */
b97bf3fd
PL
171 if (unlikely(protocol != 0))
172 return -EPROTONOSUPPORT;
173
b97bf3fd
PL
174 switch (sock->type) {
175 case SOCK_STREAM:
0c3141e9
AS
176 ops = &stream_ops;
177 state = SS_UNCONNECTED;
b97bf3fd
PL
178 break;
179 case SOCK_SEQPACKET:
0c3141e9
AS
180 ops = &packet_ops;
181 state = SS_UNCONNECTED;
b97bf3fd
PL
182 break;
183 case SOCK_DGRAM:
b97bf3fd 184 case SOCK_RDM:
0c3141e9
AS
185 ops = &msg_ops;
186 state = SS_READY;
b97bf3fd 187 break;
49978651 188 default:
49978651 189 return -EPROTOTYPE;
b97bf3fd
PL
190 }
191
0c3141e9 192 /* Allocate socket's protocol area */
c5fa7b3c
YX
193 if (!kern)
194 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
195 else
196 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
197
0c3141e9 198 if (sk == NULL)
b97bf3fd 199 return -ENOMEM;
b97bf3fd 200
58ed9442
JPM
201 tsk = tipc_sk(sk);
202 port = &tsk->port;
9b50fd08 203 ref = tipc_ref_acquire(tsk, &port->lock);
58ed9442 204 if (!ref) {
5b8fa7ce 205 pr_warn("Socket create failed; reference table exhausted\n");
0c3141e9
AS
206 return -ENOMEM;
207 }
5b8fa7ce
JPM
208 port->max_pkt = MAX_PKT_DEFAULT;
209 port->ref = ref;
210 INIT_LIST_HEAD(&port->publications);
5b8fa7ce 211
5b8fa7ce
JPM
212 msg = &port->phdr;
213 tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
214 NAMED_H_SIZE, 0);
215 msg_set_origport(msg, ref);
b97bf3fd 216
0c3141e9 217 /* Finish initializing socket data structures */
0c3141e9
AS
218 sock->ops = ops;
219 sock->state = state;
0c3141e9 220 sock_init_data(sock, sk);
57289015 221 k_init_timer(&port->timer, (Handler)tipc_sk_timeout, ref);
4f4482dc 222 sk->sk_backlog_rcv = tipc_backlog_rcv;
cc79dd1b 223 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
f288bef4
YX
224 sk->sk_data_ready = tipc_data_ready;
225 sk->sk_write_space = tipc_write_space;
4f4482dc 226 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
60120526 227 tsk->sent_unacked = 0;
4f4482dc 228 atomic_set(&tsk->dupl_rcvcnt, 0);
58ed9442 229 tipc_port_unlock(port);
7ef43eba 230
0c3141e9 231 if (sock->state == SS_READY) {
58ed9442 232 tipc_port_set_unreturnable(port, true);
0c3141e9 233 if (sock->type == SOCK_DGRAM)
58ed9442 234 tipc_port_set_unreliable(port, true);
0c3141e9 235 }
b97bf3fd
PL
236 return 0;
237}
238
c5fa7b3c
YX
239/**
240 * tipc_sock_create_local - create TIPC socket from inside TIPC module
241 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
242 *
243 * We cannot use sock_creat_kern here because it bumps module user count.
244 * Since socket owner and creator is the same module we must make sure
245 * that module count remains zero for module local sockets, otherwise
246 * we cannot do rmmod.
247 *
248 * Returns 0 on success, errno otherwise
249 */
250int tipc_sock_create_local(int type, struct socket **res)
251{
252 int rc;
c5fa7b3c
YX
253
254 rc = sock_create_lite(AF_TIPC, type, 0, res);
255 if (rc < 0) {
256 pr_err("Failed to create kernel socket\n");
257 return rc;
258 }
259 tipc_sk_create(&init_net, *res, 0, 1);
260
c5fa7b3c
YX
261 return 0;
262}
263
264/**
265 * tipc_sock_release_local - release socket created by tipc_sock_create_local
266 * @sock: the socket to be released.
267 *
268 * Module reference count is not incremented when such sockets are created,
269 * so we must keep it from being decremented when they are released.
270 */
271void tipc_sock_release_local(struct socket *sock)
272{
247f0f3c 273 tipc_release(sock);
c5fa7b3c
YX
274 sock->ops = NULL;
275 sock_release(sock);
276}
277
278/**
279 * tipc_sock_accept_local - accept a connection on a socket created
280 * with tipc_sock_create_local. Use this function to avoid that
281 * module reference count is inadvertently incremented.
282 *
283 * @sock: the accepting socket
284 * @newsock: reference to the new socket to be created
285 * @flags: socket flags
286 */
287
288int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
ae8509c4 289 int flags)
c5fa7b3c
YX
290{
291 struct sock *sk = sock->sk;
292 int ret;
293
294 ret = sock_create_lite(sk->sk_family, sk->sk_type,
295 sk->sk_protocol, newsock);
296 if (ret < 0)
297 return ret;
298
247f0f3c 299 ret = tipc_accept(sock, *newsock, flags);
c5fa7b3c
YX
300 if (ret < 0) {
301 sock_release(*newsock);
302 return ret;
303 }
304 (*newsock)->ops = sock->ops;
305 return ret;
306}
307
b97bf3fd 308/**
247f0f3c 309 * tipc_release - destroy a TIPC socket
b97bf3fd
PL
310 * @sock: socket to destroy
311 *
312 * This routine cleans up any messages that are still queued on the socket.
313 * For DGRAM and RDM socket types, all queued messages are rejected.
314 * For SEQPACKET and STREAM socket types, the first message is rejected
315 * and any others are discarded. (If the first message on a STREAM socket
316 * is partially-read, it is discarded and the next one is rejected instead.)
c4307285 317 *
b97bf3fd
PL
318 * NOTE: Rejected messages are not necessarily returned to the sender! They
319 * are returned or discarded according to the "destination droppable" setting
320 * specified for the message by the sender.
321 *
322 * Returns 0 on success, errno otherwise
323 */
247f0f3c 324static int tipc_release(struct socket *sock)
b97bf3fd 325{
b97bf3fd 326 struct sock *sk = sock->sk;
58ed9442
JPM
327 struct tipc_sock *tsk;
328 struct tipc_port *port;
b97bf3fd 329 struct sk_buff *buf;
8db1bae3 330 u32 dnode;
b97bf3fd 331
0c3141e9
AS
332 /*
333 * Exit if socket isn't fully initialized (occurs when a failed accept()
334 * releases a pre-allocated child socket that was never used)
335 */
0c3141e9 336 if (sk == NULL)
b97bf3fd 337 return 0;
c4307285 338
58ed9442
JPM
339 tsk = tipc_sk(sk);
340 port = &tsk->port;
0c3141e9
AS
341 lock_sock(sk);
342
343 /*
344 * Reject all unreceived messages, except on an active connection
345 * (which disconnects locally & sends a 'FIN+' to peer)
346 */
5b8fa7ce 347 dnode = tipc_port_peernode(port);
b97bf3fd 348 while (sock->state != SS_DISCONNECTING) {
0c3141e9
AS
349 buf = __skb_dequeue(&sk->sk_receive_queue);
350 if (buf == NULL)
b97bf3fd 351 break;
40682432 352 if (TIPC_SKB_CB(buf)->handle != NULL)
5f6d9123 353 kfree_skb(buf);
0c3141e9
AS
354 else {
355 if ((sock->state == SS_CONNECTING) ||
356 (sock->state == SS_CONNECTED)) {
357 sock->state = SS_DISCONNECTING;
dadebc00 358 port->connected = 0;
5b8fa7ce 359 tipc_node_remove_conn(dnode, port->ref);
0c3141e9 360 }
8db1bae3 361 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
9fbfb8b1 362 tipc_link_xmit(buf, dnode, 0);
0c3141e9 363 }
b97bf3fd
PL
364 }
365
5b8fa7ce
JPM
366 tipc_withdraw(port, 0, NULL);
367 spin_lock_bh(port->lock);
368 tipc_ref_discard(port->ref);
369 spin_unlock_bh(port->lock);
370 k_cancel_timer(&port->timer);
371 if (port->connected) {
372 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
373 SHORT_H_SIZE, 0, dnode, tipc_own_addr,
374 tipc_port_peerport(port),
375 port->ref, TIPC_ERR_NO_PORT);
376 if (buf)
377 tipc_link_xmit(buf, dnode, port->ref);
378 tipc_node_remove_conn(dnode, port->ref);
379 }
5b8fa7ce 380 k_term_timer(&port->timer);
b97bf3fd 381
0c3141e9 382 /* Discard any remaining (connection-based) messages in receive queue */
57467e56 383 __skb_queue_purge(&sk->sk_receive_queue);
b97bf3fd 384
0c3141e9 385 /* Reject any messages that accumulated in backlog queue */
0c3141e9
AS
386 sock->state = SS_DISCONNECTING;
387 release_sock(sk);
b97bf3fd 388 sock_put(sk);
0c3141e9 389 sock->sk = NULL;
b97bf3fd 390
065d7e39 391 return 0;
b97bf3fd
PL
392}
393
394/**
247f0f3c 395 * tipc_bind - associate or disassocate TIPC name(s) with a socket
b97bf3fd
PL
396 * @sock: socket structure
397 * @uaddr: socket address describing name(s) and desired operation
398 * @uaddr_len: size of socket address data structure
c4307285 399 *
b97bf3fd
PL
400 * Name and name sequence binding is indicated using a positive scope value;
401 * a negative scope value unbinds the specified name. Specifying no name
402 * (i.e. a socket address length of 0) unbinds all names from the socket.
c4307285 403 *
b97bf3fd 404 * Returns 0 on success, errno otherwise
0c3141e9
AS
405 *
406 * NOTE: This routine doesn't need to take the socket lock since it doesn't
407 * access any non-constant socket information.
b97bf3fd 408 */
247f0f3c
YX
409static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
410 int uaddr_len)
b97bf3fd 411{
84602761 412 struct sock *sk = sock->sk;
b97bf3fd 413 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
58ed9442 414 struct tipc_sock *tsk = tipc_sk(sk);
84602761 415 int res = -EINVAL;
b97bf3fd 416
84602761
YX
417 lock_sock(sk);
418 if (unlikely(!uaddr_len)) {
58ed9442 419 res = tipc_withdraw(&tsk->port, 0, NULL);
84602761
YX
420 goto exit;
421 }
c4307285 422
84602761
YX
423 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
424 res = -EINVAL;
425 goto exit;
426 }
427 if (addr->family != AF_TIPC) {
428 res = -EAFNOSUPPORT;
429 goto exit;
430 }
b97bf3fd 431
b97bf3fd
PL
432 if (addr->addrtype == TIPC_ADDR_NAME)
433 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
84602761
YX
434 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
435 res = -EAFNOSUPPORT;
436 goto exit;
437 }
c4307285 438
13a2e898 439 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
7d0ab17b 440 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
84602761
YX
441 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
442 res = -EACCES;
443 goto exit;
444 }
c422f1bd 445
84602761 446 res = (addr->scope > 0) ?
58ed9442
JPM
447 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
448 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
84602761
YX
449exit:
450 release_sock(sk);
451 return res;
b97bf3fd
PL
452}
453
c4307285 454/**
247f0f3c 455 * tipc_getname - get port ID of socket or peer socket
b97bf3fd
PL
456 * @sock: socket structure
457 * @uaddr: area for returned socket address
458 * @uaddr_len: area for returned length of socket address
2da59918 459 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
c4307285 460 *
b97bf3fd 461 * Returns 0 on success, errno otherwise
0c3141e9 462 *
2da59918
AS
463 * NOTE: This routine doesn't need to take the socket lock since it only
464 * accesses socket information that is unchanging (or which changes in
0e65967e 465 * a completely predictable manner).
b97bf3fd 466 */
247f0f3c
YX
467static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
468 int *uaddr_len, int peer)
b97bf3fd 469{
b97bf3fd 470 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
58ed9442 471 struct tipc_sock *tsk = tipc_sk(sock->sk);
b97bf3fd 472
88f8a5e3 473 memset(addr, 0, sizeof(*addr));
0c3141e9 474 if (peer) {
2da59918
AS
475 if ((sock->state != SS_CONNECTED) &&
476 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
477 return -ENOTCONN;
58ed9442
JPM
478 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
479 addr->addr.id.node = tipc_port_peernode(&tsk->port);
0c3141e9 480 } else {
58ed9442 481 addr->addr.id.ref = tsk->port.ref;
b924dcf0 482 addr->addr.id.node = tipc_own_addr;
0c3141e9 483 }
b97bf3fd
PL
484
485 *uaddr_len = sizeof(*addr);
486 addr->addrtype = TIPC_ADDR_ID;
487 addr->family = AF_TIPC;
488 addr->scope = 0;
b97bf3fd
PL
489 addr->addr.name.domain = 0;
490
0c3141e9 491 return 0;
b97bf3fd
PL
492}
493
494/**
247f0f3c 495 * tipc_poll - read and possibly block on pollmask
b97bf3fd
PL
496 * @file: file structure associated with the socket
497 * @sock: socket for which to calculate the poll bits
498 * @wait: ???
499 *
9b674e82
AS
500 * Returns pollmask value
501 *
502 * COMMENTARY:
503 * It appears that the usual socket locking mechanisms are not useful here
504 * since the pollmask info is potentially out-of-date the moment this routine
505 * exits. TCP and other protocols seem to rely on higher level poll routines
506 * to handle any preventable race conditions, so TIPC will do the same ...
507 *
508 * TIPC sets the returned events as follows:
f662c070
AS
509 *
510 * socket state flags set
511 * ------------ ---------
512 * unconnected no read flags
c4fc298a 513 * POLLOUT if port is not congested
f662c070
AS
514 *
515 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
516 * no write flags
517 *
518 * connected POLLIN/POLLRDNORM if data in rx queue
519 * POLLOUT if port is not congested
520 *
521 * disconnecting POLLIN/POLLRDNORM/POLLHUP
522 * no write flags
523 *
524 * listening POLLIN if SYN in rx queue
525 * no write flags
526 *
527 * ready POLLIN/POLLRDNORM if data in rx queue
528 * [connectionless] POLLOUT (since port cannot be congested)
529 *
530 * IMPORTANT: The fact that a read or write operation is indicated does NOT
531 * imply that the operation will succeed, merely that it should be performed
532 * and will not block.
b97bf3fd 533 */
247f0f3c
YX
534static unsigned int tipc_poll(struct file *file, struct socket *sock,
535 poll_table *wait)
b97bf3fd 536{
9b674e82 537 struct sock *sk = sock->sk;
58ed9442 538 struct tipc_sock *tsk = tipc_sk(sk);
f662c070 539 u32 mask = 0;
9b674e82 540
f288bef4 541 sock_poll_wait(file, sk_sleep(sk), wait);
9b674e82 542
f662c070 543 switch ((int)sock->state) {
c4fc298a 544 case SS_UNCONNECTED:
60120526 545 if (!tsk->link_cong)
c4fc298a
EH
546 mask |= POLLOUT;
547 break;
f662c070
AS
548 case SS_READY:
549 case SS_CONNECTED:
60120526 550 if (!tsk->link_cong && !tipc_sk_conn_cong(tsk))
f662c070
AS
551 mask |= POLLOUT;
552 /* fall thru' */
553 case SS_CONNECTING:
554 case SS_LISTENING:
555 if (!skb_queue_empty(&sk->sk_receive_queue))
556 mask |= (POLLIN | POLLRDNORM);
557 break;
558 case SS_DISCONNECTING:
559 mask = (POLLIN | POLLRDNORM | POLLHUP);
560 break;
561 }
9b674e82
AS
562
563 return mask;
b97bf3fd
PL
564}
565
0abd8ff2
JPM
566/**
567 * tipc_sendmcast - send multicast message
568 * @sock: socket structure
569 * @seq: destination address
570 * @iov: message data to send
571 * @dsz: total length of message data
572 * @timeo: timeout to wait for wakeup
573 *
574 * Called from function tipc_sendmsg(), which has done all sanity checks
575 * Returns the number of bytes sent on success, or errno
576 */
577static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
578 struct iovec *iov, size_t dsz, long timeo)
579{
580 struct sock *sk = sock->sk;
581 struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr;
582 struct sk_buff *buf;
583 uint mtu;
584 int rc;
585
586 msg_set_type(mhdr, TIPC_MCAST_MSG);
587 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
588 msg_set_destport(mhdr, 0);
589 msg_set_destnode(mhdr, 0);
590 msg_set_nametype(mhdr, seq->type);
591 msg_set_namelower(mhdr, seq->lower);
592 msg_set_nameupper(mhdr, seq->upper);
593 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
594
595new_mtu:
596 mtu = tipc_bclink_get_mtu();
9fbfb8b1 597 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
0abd8ff2
JPM
598 if (unlikely(rc < 0))
599 return rc;
600
601 do {
602 rc = tipc_bclink_xmit(buf);
603 if (likely(rc >= 0)) {
604 rc = dsz;
605 break;
606 }
607 if (rc == -EMSGSIZE)
608 goto new_mtu;
609 if (rc != -ELINKCONG)
610 break;
50100a5e 611 tipc_sk(sk)->link_cong = 1;
0abd8ff2
JPM
612 rc = tipc_wait_for_sndmsg(sock, &timeo);
613 if (rc)
614 kfree_skb_list(buf);
615 } while (!rc);
616 return rc;
617}
618
078bec82
JPM
619/* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
620 */
621void tipc_sk_mcast_rcv(struct sk_buff *buf)
622{
623 struct tipc_msg *msg = buf_msg(buf);
624 struct tipc_port_list dports = {0, NULL, };
625 struct tipc_port_list *item;
626 struct sk_buff *b;
627 uint i, last, dst = 0;
628 u32 scope = TIPC_CLUSTER_SCOPE;
629
630 if (in_own_node(msg_orignode(msg)))
631 scope = TIPC_NODE_SCOPE;
632
633 /* Create destination port list: */
634 tipc_nametbl_mc_translate(msg_nametype(msg),
635 msg_namelower(msg),
636 msg_nameupper(msg),
637 scope,
638 &dports);
639 last = dports.count;
640 if (!last) {
641 kfree_skb(buf);
642 return;
643 }
644
645 for (item = &dports; item; item = item->next) {
646 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
647 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
648 if (!b) {
649 pr_warn("Failed do clone mcast rcv buffer\n");
650 continue;
651 }
652 msg_set_destport(msg, item->ports[i]);
653 tipc_sk_rcv(b);
654 }
655 }
656 tipc_port_list_free(&dports);
657}
658
ac0074ee
JPM
659/**
660 * tipc_sk_proto_rcv - receive a connection mng protocol message
661 * @tsk: receiving socket
662 * @dnode: node to send response message to, if any
663 * @buf: buffer containing protocol message
664 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
665 * (CONN_PROBE_REPLY) message should be forwarded.
666 */
52f50ce5
WY
667static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
668 struct sk_buff *buf)
ac0074ee
JPM
669{
670 struct tipc_msg *msg = buf_msg(buf);
671 struct tipc_port *port = &tsk->port;
60120526 672 int conn_cong;
ac0074ee
JPM
673
674 /* Ignore if connection cannot be validated: */
675 if (!port->connected || !tipc_port_peer_msg(port, msg))
676 goto exit;
677
678 port->probing_state = TIPC_CONN_OK;
679
680 if (msg_type(msg) == CONN_ACK) {
60120526
JPM
681 conn_cong = tipc_sk_conn_cong(tsk);
682 tsk->sent_unacked -= msg_msgcnt(msg);
683 if (conn_cong)
50100a5e 684 tsk->sk.sk_write_space(&tsk->sk);
ac0074ee
JPM
685 } else if (msg_type(msg) == CONN_PROBE) {
686 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
687 return TIPC_OK;
688 msg_set_type(msg, CONN_PROBE_REPLY);
689 return TIPC_FWD_MSG;
690 }
691 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
692exit:
693 kfree_skb(buf);
694 return TIPC_OK;
695}
696
c4307285 697/**
b97bf3fd
PL
698 * dest_name_check - verify user is permitted to send to specified port name
699 * @dest: destination address
700 * @m: descriptor for message to be sent
c4307285 701 *
b97bf3fd
PL
702 * Prevents restricted configuration commands from being issued by
703 * unauthorized users.
c4307285 704 *
b97bf3fd
PL
705 * Returns 0 if permission is granted, otherwise errno
706 */
05790c64 707static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
b97bf3fd
PL
708{
709 struct tipc_cfg_msg_hdr hdr;
710
e2dafe87
JPM
711 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
712 return 0;
c4307285
YH
713 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
714 return 0;
715 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
716 return 0;
c4307285
YH
717 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
718 return -EACCES;
b97bf3fd 719
3f8dd944
AS
720 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
721 return -EMSGSIZE;
c4307285 722 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
b97bf3fd 723 return -EFAULT;
70cb2347 724 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
b97bf3fd 725 return -EACCES;
c4307285 726
b97bf3fd
PL
727 return 0;
728}
729
3f40504f
YX
730static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
731{
732 struct sock *sk = sock->sk;
58ed9442 733 struct tipc_sock *tsk = tipc_sk(sk);
3f40504f
YX
734 DEFINE_WAIT(wait);
735 int done;
736
737 do {
738 int err = sock_error(sk);
739 if (err)
740 return err;
741 if (sock->state == SS_DISCONNECTING)
742 return -EPIPE;
743 if (!*timeo_p)
744 return -EAGAIN;
745 if (signal_pending(current))
746 return sock_intr_errno(*timeo_p);
747
748 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
60120526 749 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
3f40504f
YX
750 finish_wait(sk_sleep(sk), &wait);
751 } while (!done);
752 return 0;
753}
754
b97bf3fd 755/**
247f0f3c 756 * tipc_sendmsg - send message in connectionless manner
0c3141e9 757 * @iocb: if NULL, indicates that socket lock is already held
b97bf3fd
PL
758 * @sock: socket structure
759 * @m: message to send
e2dafe87 760 * @dsz: amount of user data to be sent
c4307285 761 *
b97bf3fd 762 * Message must have an destination specified explicitly.
c4307285 763 * Used for SOCK_RDM and SOCK_DGRAM messages,
b97bf3fd
PL
764 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
765 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
c4307285 766 *
b97bf3fd
PL
767 * Returns the number of bytes sent on success, or errno otherwise
768 */
247f0f3c 769static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
e2dafe87 770 struct msghdr *m, size_t dsz)
b97bf3fd 771{
e2dafe87 772 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
0c3141e9 773 struct sock *sk = sock->sk;
58ed9442 774 struct tipc_sock *tsk = tipc_sk(sk);
5c311421 775 struct tipc_port *port = &tsk->port;
e2dafe87
JPM
776 struct tipc_msg *mhdr = &port->phdr;
777 struct iovec *iov = m->msg_iov;
778 u32 dnode, dport;
779 struct sk_buff *buf;
780 struct tipc_name_seq *seq = &dest->addr.nameseq;
781 u32 mtu;
3f40504f 782 long timeo;
e2dafe87 783 int rc = -EINVAL;
b97bf3fd
PL
784
785 if (unlikely(!dest))
786 return -EDESTADDRREQ;
e2dafe87 787
51f9cc1f
AS
788 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
789 (dest->family != AF_TIPC)))
b97bf3fd 790 return -EINVAL;
e2dafe87
JPM
791
792 if (dsz > TIPC_MAX_USER_MSG_SIZE)
c29c3f70 793 return -EMSGSIZE;
b97bf3fd 794
0c3141e9
AS
795 if (iocb)
796 lock_sock(sk);
797
e2dafe87 798 if (unlikely(sock->state != SS_READY)) {
0c3141e9 799 if (sock->state == SS_LISTENING) {
e2dafe87 800 rc = -EPIPE;
0c3141e9
AS
801 goto exit;
802 }
803 if (sock->state != SS_UNCONNECTED) {
e2dafe87 804 rc = -EISCONN;
0c3141e9
AS
805 goto exit;
806 }
58ed9442 807 if (tsk->port.published) {
e2dafe87 808 rc = -EOPNOTSUPP;
0c3141e9
AS
809 goto exit;
810 }
3388007b 811 if (dest->addrtype == TIPC_ADDR_NAME) {
58ed9442
JPM
812 tsk->port.conn_type = dest->addr.name.name.type;
813 tsk->port.conn_instance = dest->addr.name.name.instance;
3388007b 814 }
b97bf3fd 815 }
e2dafe87
JPM
816 rc = dest_name_check(dest, m);
817 if (rc)
818 goto exit;
b97bf3fd 819
3f40504f 820 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
e2dafe87
JPM
821
822 if (dest->addrtype == TIPC_ADDR_MCAST) {
823 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
824 goto exit;
825 } else if (dest->addrtype == TIPC_ADDR_NAME) {
826 u32 type = dest->addr.name.name.type;
827 u32 inst = dest->addr.name.name.instance;
828 u32 domain = dest->addr.name.domain;
829
830 dnode = domain;
831 msg_set_type(mhdr, TIPC_NAMED_MSG);
832 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
833 msg_set_nametype(mhdr, type);
834 msg_set_nameinst(mhdr, inst);
835 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
836 dport = tipc_nametbl_translate(type, inst, &dnode);
837 msg_set_destnode(mhdr, dnode);
838 msg_set_destport(mhdr, dport);
839 if (unlikely(!dport && !dnode)) {
840 rc = -EHOSTUNREACH;
841 goto exit;
c4307285 842 }
e2dafe87
JPM
843 } else if (dest->addrtype == TIPC_ADDR_ID) {
844 dnode = dest->addr.id.node;
845 msg_set_type(mhdr, TIPC_DIRECT_MSG);
846 msg_set_lookup_scope(mhdr, 0);
847 msg_set_destnode(mhdr, dnode);
848 msg_set_destport(mhdr, dest->addr.id.ref);
849 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
850 }
851
852new_mtu:
853 mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
9fbfb8b1 854 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
e2dafe87
JPM
855 if (rc < 0)
856 goto exit;
857
858 do {
50100a5e 859 TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong;
9fbfb8b1 860 rc = tipc_link_xmit(buf, dnode, tsk->port.ref);
e2dafe87
JPM
861 if (likely(rc >= 0)) {
862 if (sock->state != SS_READY)
0c3141e9 863 sock->state = SS_CONNECTING;
e2dafe87 864 rc = dsz;
0c3141e9 865 break;
c4307285 866 }
e2dafe87
JPM
867 if (rc == -EMSGSIZE)
868 goto new_mtu;
e2dafe87 869 if (rc != -ELINKCONG)
0c3141e9 870 break;
50100a5e 871 tsk->link_cong = 1;
e2dafe87 872 rc = tipc_wait_for_sndmsg(sock, &timeo);
70452dcb
EH
873 if (rc)
874 kfree_skb_list(buf);
e2dafe87 875 } while (!rc);
0c3141e9
AS
876exit:
877 if (iocb)
878 release_sock(sk);
e2dafe87
JPM
879
880 return rc;
b97bf3fd
PL
881}
882
391a6dd1
YX
883static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
884{
885 struct sock *sk = sock->sk;
58ed9442 886 struct tipc_sock *tsk = tipc_sk(sk);
391a6dd1
YX
887 DEFINE_WAIT(wait);
888 int done;
889
890 do {
891 int err = sock_error(sk);
892 if (err)
893 return err;
894 if (sock->state == SS_DISCONNECTING)
895 return -EPIPE;
896 else if (sock->state != SS_CONNECTED)
897 return -ENOTCONN;
898 if (!*timeo_p)
899 return -EAGAIN;
900 if (signal_pending(current))
901 return sock_intr_errno(*timeo_p);
902
903 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
904 done = sk_wait_event(sk, timeo_p,
60120526
JPM
905 (!tsk->link_cong &&
906 !tipc_sk_conn_cong(tsk)) ||
907 !tsk->port.connected);
391a6dd1
YX
908 finish_wait(sk_sleep(sk), &wait);
909 } while (!done);
910 return 0;
911}
912
c4307285 913/**
4ccfe5e0
JPM
914 * tipc_send_stream - send stream-oriented data
915 * @iocb: (unused)
b97bf3fd 916 * @sock: socket structure
4ccfe5e0
JPM
917 * @m: data to send
918 * @dsz: total length of data to be transmitted
c4307285 919 *
4ccfe5e0 920 * Used for SOCK_STREAM data.
c4307285 921 *
4ccfe5e0
JPM
922 * Returns the number of bytes sent on success (or partial success),
923 * or errno if no data sent
b97bf3fd 924 */
4ccfe5e0
JPM
925static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
926 struct msghdr *m, size_t dsz)
b97bf3fd 927{
0c3141e9 928 struct sock *sk = sock->sk;
58ed9442 929 struct tipc_sock *tsk = tipc_sk(sk);
4ccfe5e0
JPM
930 struct tipc_port *port = &tsk->port;
931 struct tipc_msg *mhdr = &port->phdr;
932 struct sk_buff *buf;
342dfc30 933 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
4ccfe5e0
JPM
934 u32 ref = port->ref;
935 int rc = -EINVAL;
391a6dd1 936 long timeo;
4ccfe5e0
JPM
937 u32 dnode;
938 uint mtu, send, sent = 0;
b97bf3fd
PL
939
940 /* Handle implied connection establishment */
4ccfe5e0
JPM
941 if (unlikely(dest)) {
942 rc = tipc_sendmsg(iocb, sock, m, dsz);
943 if (dsz && (dsz == rc))
60120526 944 tsk->sent_unacked = 1;
4ccfe5e0
JPM
945 return rc;
946 }
947 if (dsz > (uint)INT_MAX)
c29c3f70
AS
948 return -EMSGSIZE;
949
0c3141e9
AS
950 if (iocb)
951 lock_sock(sk);
b97bf3fd 952
391a6dd1
YX
953 if (unlikely(sock->state != SS_CONNECTED)) {
954 if (sock->state == SS_DISCONNECTING)
4ccfe5e0 955 rc = -EPIPE;
391a6dd1 956 else
4ccfe5e0 957 rc = -ENOTCONN;
391a6dd1
YX
958 goto exit;
959 }
1d835874 960
391a6dd1 961 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
4ccfe5e0 962 dnode = tipc_port_peernode(port);
4ccfe5e0
JPM
963
964next:
965 mtu = port->max_pkt;
966 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
9fbfb8b1 967 rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf);
4ccfe5e0
JPM
968 if (unlikely(rc < 0))
969 goto exit;
c4307285 970 do {
60120526 971 if (likely(!tipc_sk_conn_cong(tsk))) {
9fbfb8b1 972 rc = tipc_link_xmit(buf, dnode, ref);
4ccfe5e0 973 if (likely(!rc)) {
60120526 974 tsk->sent_unacked++;
4ccfe5e0
JPM
975 sent += send;
976 if (sent == dsz)
977 break;
978 goto next;
979 }
980 if (rc == -EMSGSIZE) {
981 port->max_pkt = tipc_node_get_mtu(dnode, ref);
982 goto next;
983 }
984 if (rc != -ELINKCONG)
985 break;
50100a5e 986 tsk->link_cong = 1;
4ccfe5e0
JPM
987 }
988 rc = tipc_wait_for_sndpkt(sock, &timeo);
70452dcb
EH
989 if (rc)
990 kfree_skb_list(buf);
4ccfe5e0 991 } while (!rc);
391a6dd1 992exit:
0c3141e9
AS
993 if (iocb)
994 release_sock(sk);
4ccfe5e0 995 return sent ? sent : rc;
b97bf3fd
PL
996}
997
c4307285 998/**
4ccfe5e0
JPM
999 * tipc_send_packet - send a connection-oriented message
1000 * @iocb: if NULL, indicates that socket lock is already held
b97bf3fd 1001 * @sock: socket structure
4ccfe5e0
JPM
1002 * @m: message to send
1003 * @dsz: length of data to be transmitted
c4307285 1004 *
4ccfe5e0 1005 * Used for SOCK_SEQPACKET messages.
c4307285 1006 *
4ccfe5e0 1007 * Returns the number of bytes sent on success, or errno otherwise
b97bf3fd 1008 */
4ccfe5e0
JPM
1009static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
1010 struct msghdr *m, size_t dsz)
b97bf3fd 1011{
4ccfe5e0
JPM
1012 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1013 return -EMSGSIZE;
b97bf3fd 1014
4ccfe5e0 1015 return tipc_send_stream(iocb, sock, m, dsz);
b97bf3fd
PL
1016}
1017
dadebc00 1018/* tipc_sk_finish_conn - complete the setup of a connection
b97bf3fd 1019 */
dadebc00
JPM
1020static void tipc_sk_finish_conn(struct tipc_port *port, u32 peer_port,
1021 u32 peer_node)
b97bf3fd 1022{
dadebc00 1023 struct tipc_msg *msg = &port->phdr;
b97bf3fd 1024
dadebc00
JPM
1025 msg_set_destnode(msg, peer_node);
1026 msg_set_destport(msg, peer_port);
1027 msg_set_type(msg, TIPC_CONN_MSG);
1028 msg_set_lookup_scope(msg, 0);
1029 msg_set_hdr_sz(msg, SHORT_H_SIZE);
584d24b3 1030
dadebc00
JPM
1031 port->probing_interval = CONN_PROBING_INTERVAL;
1032 port->probing_state = TIPC_CONN_OK;
1033 port->connected = 1;
1034 k_start_timer(&port->timer, port->probing_interval);
1035 tipc_node_add_conn(peer_node, port->ref, peer_port);
1036 port->max_pkt = tipc_node_get_mtu(peer_node, port->ref);
b97bf3fd
PL
1037}
1038
1039/**
1040 * set_orig_addr - capture sender's address for received message
1041 * @m: descriptor for message info
1042 * @msg: received message header
c4307285 1043 *
b97bf3fd
PL
1044 * Note: Address is not captured if not requested by receiver.
1045 */
05790c64 1046static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
b97bf3fd 1047{
342dfc30 1048 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
b97bf3fd 1049
c4307285 1050 if (addr) {
b97bf3fd
PL
1051 addr->family = AF_TIPC;
1052 addr->addrtype = TIPC_ADDR_ID;
60085c3d 1053 memset(&addr->addr, 0, sizeof(addr->addr));
b97bf3fd
PL
1054 addr->addr.id.ref = msg_origport(msg);
1055 addr->addr.id.node = msg_orignode(msg);
0e65967e
AS
1056 addr->addr.name.domain = 0; /* could leave uninitialized */
1057 addr->scope = 0; /* could leave uninitialized */
b97bf3fd
PL
1058 m->msg_namelen = sizeof(struct sockaddr_tipc);
1059 }
1060}
1061
1062/**
c4307285 1063 * anc_data_recv - optionally capture ancillary data for received message
b97bf3fd
PL
1064 * @m: descriptor for message info
1065 * @msg: received message header
1066 * @tport: TIPC port associated with message
c4307285 1067 *
b97bf3fd 1068 * Note: Ancillary data is not captured if not requested by receiver.
c4307285 1069 *
b97bf3fd
PL
1070 * Returns 0 if successful, otherwise errno
1071 */
05790c64 1072static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
ae8509c4 1073 struct tipc_port *tport)
b97bf3fd
PL
1074{
1075 u32 anc_data[3];
1076 u32 err;
1077 u32 dest_type;
3546c750 1078 int has_name;
b97bf3fd
PL
1079 int res;
1080
1081 if (likely(m->msg_controllen == 0))
1082 return 0;
1083
1084 /* Optionally capture errored message object(s) */
b97bf3fd
PL
1085 err = msg ? msg_errcode(msg) : 0;
1086 if (unlikely(err)) {
1087 anc_data[0] = err;
1088 anc_data[1] = msg_data_sz(msg);
2db9983a
AS
1089 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1090 if (res)
b97bf3fd 1091 return res;
2db9983a
AS
1092 if (anc_data[1]) {
1093 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1094 msg_data(msg));
1095 if (res)
1096 return res;
1097 }
b97bf3fd
PL
1098 }
1099
1100 /* Optionally capture message destination object */
b97bf3fd
PL
1101 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1102 switch (dest_type) {
1103 case TIPC_NAMED_MSG:
3546c750 1104 has_name = 1;
b97bf3fd
PL
1105 anc_data[0] = msg_nametype(msg);
1106 anc_data[1] = msg_namelower(msg);
1107 anc_data[2] = msg_namelower(msg);
1108 break;
1109 case TIPC_MCAST_MSG:
3546c750 1110 has_name = 1;
b97bf3fd
PL
1111 anc_data[0] = msg_nametype(msg);
1112 anc_data[1] = msg_namelower(msg);
1113 anc_data[2] = msg_nameupper(msg);
1114 break;
1115 case TIPC_CONN_MSG:
3546c750 1116 has_name = (tport->conn_type != 0);
b97bf3fd
PL
1117 anc_data[0] = tport->conn_type;
1118 anc_data[1] = tport->conn_instance;
1119 anc_data[2] = tport->conn_instance;
1120 break;
1121 default:
3546c750 1122 has_name = 0;
b97bf3fd 1123 }
2db9983a
AS
1124 if (has_name) {
1125 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1126 if (res)
1127 return res;
1128 }
b97bf3fd
PL
1129
1130 return 0;
1131}
1132
739f5e4e
JPM
1133static void tipc_sk_send_ack(struct tipc_port *port, uint ack)
1134{
1135 struct sk_buff *buf = NULL;
1136 struct tipc_msg *msg;
1137 u32 peer_port = tipc_port_peerport(port);
1138 u32 dnode = tipc_port_peernode(port);
1139
1140 if (!port->connected)
1141 return;
1142 buf = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode,
1143 tipc_own_addr, peer_port, port->ref, TIPC_OK);
1144 if (!buf)
1145 return;
1146 msg = buf_msg(buf);
1147 msg_set_msgcnt(msg, ack);
1148 tipc_link_xmit(buf, dnode, msg_link_selector(msg));
1149}
1150
85d3fc94 1151static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
9bbb4ecc
YX
1152{
1153 struct sock *sk = sock->sk;
1154 DEFINE_WAIT(wait);
85d3fc94 1155 long timeo = *timeop;
9bbb4ecc
YX
1156 int err;
1157
1158 for (;;) {
1159 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
fe8e4649 1160 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
9bbb4ecc
YX
1161 if (sock->state == SS_DISCONNECTING) {
1162 err = -ENOTCONN;
1163 break;
1164 }
1165 release_sock(sk);
1166 timeo = schedule_timeout(timeo);
1167 lock_sock(sk);
1168 }
1169 err = 0;
1170 if (!skb_queue_empty(&sk->sk_receive_queue))
1171 break;
1172 err = sock_intr_errno(timeo);
1173 if (signal_pending(current))
1174 break;
1175 err = -EAGAIN;
1176 if (!timeo)
1177 break;
1178 }
1179 finish_wait(sk_sleep(sk), &wait);
85d3fc94 1180 *timeop = timeo;
9bbb4ecc
YX
1181 return err;
1182}
1183
c4307285 1184/**
247f0f3c 1185 * tipc_recvmsg - receive packet-oriented message
b97bf3fd
PL
1186 * @iocb: (unused)
1187 * @m: descriptor for message info
1188 * @buf_len: total size of user buffer area
1189 * @flags: receive flags
c4307285 1190 *
b97bf3fd
PL
1191 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1192 * If the complete message doesn't fit in user area, truncate it.
1193 *
1194 * Returns size of returned message data, errno otherwise
1195 */
247f0f3c
YX
1196static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1197 struct msghdr *m, size_t buf_len, int flags)
b97bf3fd 1198{
0c3141e9 1199 struct sock *sk = sock->sk;
58ed9442
JPM
1200 struct tipc_sock *tsk = tipc_sk(sk);
1201 struct tipc_port *port = &tsk->port;
b97bf3fd
PL
1202 struct sk_buff *buf;
1203 struct tipc_msg *msg;
9bbb4ecc 1204 long timeo;
b97bf3fd
PL
1205 unsigned int sz;
1206 u32 err;
1207 int res;
1208
0c3141e9 1209 /* Catch invalid receive requests */
b97bf3fd
PL
1210 if (unlikely(!buf_len))
1211 return -EINVAL;
1212
0c3141e9 1213 lock_sock(sk);
b97bf3fd 1214
0c3141e9
AS
1215 if (unlikely(sock->state == SS_UNCONNECTED)) {
1216 res = -ENOTCONN;
b97bf3fd
PL
1217 goto exit;
1218 }
1219
9bbb4ecc 1220 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
0c3141e9 1221restart:
b97bf3fd 1222
0c3141e9 1223 /* Look for a message in receive queue; wait if necessary */
85d3fc94 1224 res = tipc_wait_for_rcvmsg(sock, &timeo);
9bbb4ecc
YX
1225 if (res)
1226 goto exit;
b97bf3fd 1227
0c3141e9 1228 /* Look at first message in receive queue */
0c3141e9 1229 buf = skb_peek(&sk->sk_receive_queue);
b97bf3fd
PL
1230 msg = buf_msg(buf);
1231 sz = msg_data_sz(msg);
1232 err = msg_errcode(msg);
1233
b97bf3fd 1234 /* Discard an empty non-errored message & try again */
b97bf3fd 1235 if ((!sz) && (!err)) {
0c3141e9 1236 advance_rx_queue(sk);
b97bf3fd
PL
1237 goto restart;
1238 }
1239
1240 /* Capture sender's address (optional) */
b97bf3fd
PL
1241 set_orig_addr(m, msg);
1242
1243 /* Capture ancillary data (optional) */
58ed9442 1244 res = anc_data_recv(m, msg, port);
0c3141e9 1245 if (res)
b97bf3fd
PL
1246 goto exit;
1247
1248 /* Capture message data (if valid) & compute return value (always) */
b97bf3fd
PL
1249 if (!err) {
1250 if (unlikely(buf_len < sz)) {
1251 sz = buf_len;
1252 m->msg_flags |= MSG_TRUNC;
1253 }
0232fd0a
AS
1254 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1255 m->msg_iov, sz);
1256 if (res)
b97bf3fd 1257 goto exit;
b97bf3fd
PL
1258 res = sz;
1259 } else {
1260 if ((sock->state == SS_READY) ||
1261 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1262 res = 0;
1263 else
1264 res = -ECONNRESET;
1265 }
1266
1267 /* Consume received message (optional) */
b97bf3fd 1268 if (likely(!(flags & MSG_PEEK))) {
99009806 1269 if ((sock->state != SS_READY) &&
60120526 1270 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
739f5e4e 1271 tipc_sk_send_ack(port, tsk->rcv_unacked);
60120526
JPM
1272 tsk->rcv_unacked = 0;
1273 }
0c3141e9 1274 advance_rx_queue(sk);
c4307285 1275 }
b97bf3fd 1276exit:
0c3141e9 1277 release_sock(sk);
b97bf3fd
PL
1278 return res;
1279}
1280
c4307285 1281/**
247f0f3c 1282 * tipc_recv_stream - receive stream-oriented data
b97bf3fd
PL
1283 * @iocb: (unused)
1284 * @m: descriptor for message info
1285 * @buf_len: total size of user buffer area
1286 * @flags: receive flags
c4307285
YH
1287 *
1288 * Used for SOCK_STREAM messages only. If not enough data is available
b97bf3fd
PL
1289 * will optionally wait for more; never truncates data.
1290 *
1291 * Returns size of returned message data, errno otherwise
1292 */
247f0f3c
YX
1293static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1294 struct msghdr *m, size_t buf_len, int flags)
b97bf3fd 1295{
0c3141e9 1296 struct sock *sk = sock->sk;
58ed9442
JPM
1297 struct tipc_sock *tsk = tipc_sk(sk);
1298 struct tipc_port *port = &tsk->port;
b97bf3fd
PL
1299 struct sk_buff *buf;
1300 struct tipc_msg *msg;
9bbb4ecc 1301 long timeo;
b97bf3fd 1302 unsigned int sz;
3720d40b 1303 int sz_to_copy, target, needed;
b97bf3fd 1304 int sz_copied = 0;
b97bf3fd 1305 u32 err;
0c3141e9 1306 int res = 0;
b97bf3fd 1307
0c3141e9 1308 /* Catch invalid receive attempts */
b97bf3fd
PL
1309 if (unlikely(!buf_len))
1310 return -EINVAL;
1311
0c3141e9 1312 lock_sock(sk);
b97bf3fd 1313
9bbb4ecc 1314 if (unlikely(sock->state == SS_UNCONNECTED)) {
0c3141e9 1315 res = -ENOTCONN;
b97bf3fd
PL
1316 goto exit;
1317 }
1318
3720d40b 1319 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
9bbb4ecc 1320 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
b97bf3fd 1321
617d3c7a 1322restart:
0c3141e9 1323 /* Look for a message in receive queue; wait if necessary */
85d3fc94 1324 res = tipc_wait_for_rcvmsg(sock, &timeo);
9bbb4ecc
YX
1325 if (res)
1326 goto exit;
b97bf3fd 1327
0c3141e9 1328 /* Look at first message in receive queue */
0c3141e9 1329 buf = skb_peek(&sk->sk_receive_queue);
b97bf3fd
PL
1330 msg = buf_msg(buf);
1331 sz = msg_data_sz(msg);
1332 err = msg_errcode(msg);
1333
1334 /* Discard an empty non-errored message & try again */
b97bf3fd 1335 if ((!sz) && (!err)) {
0c3141e9 1336 advance_rx_queue(sk);
b97bf3fd
PL
1337 goto restart;
1338 }
1339
1340 /* Optionally capture sender's address & ancillary data of first msg */
b97bf3fd
PL
1341 if (sz_copied == 0) {
1342 set_orig_addr(m, msg);
58ed9442 1343 res = anc_data_recv(m, msg, port);
0c3141e9 1344 if (res)
b97bf3fd
PL
1345 goto exit;
1346 }
1347
1348 /* Capture message data (if valid) & compute return value (always) */
b97bf3fd 1349 if (!err) {
0232fd0a 1350 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
b97bf3fd 1351
0232fd0a 1352 sz -= offset;
b97bf3fd
PL
1353 needed = (buf_len - sz_copied);
1354 sz_to_copy = (sz <= needed) ? sz : needed;
0232fd0a
AS
1355
1356 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1357 m->msg_iov, sz_to_copy);
1358 if (res)
b97bf3fd 1359 goto exit;
0232fd0a 1360
b97bf3fd
PL
1361 sz_copied += sz_to_copy;
1362
1363 if (sz_to_copy < sz) {
1364 if (!(flags & MSG_PEEK))
0232fd0a
AS
1365 TIPC_SKB_CB(buf)->handle =
1366 (void *)(unsigned long)(offset + sz_to_copy);
b97bf3fd
PL
1367 goto exit;
1368 }
b97bf3fd
PL
1369 } else {
1370 if (sz_copied != 0)
1371 goto exit; /* can't add error msg to valid data */
1372
1373 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1374 res = 0;
1375 else
1376 res = -ECONNRESET;
1377 }
1378
1379 /* Consume received message (optional) */
b97bf3fd 1380 if (likely(!(flags & MSG_PEEK))) {
60120526 1381 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
739f5e4e 1382 tipc_sk_send_ack(port, tsk->rcv_unacked);
60120526
JPM
1383 tsk->rcv_unacked = 0;
1384 }
0c3141e9 1385 advance_rx_queue(sk);
c4307285 1386 }
b97bf3fd
PL
1387
1388 /* Loop around if more data is required */
f64f9e71
JP
1389 if ((sz_copied < buf_len) && /* didn't get all requested data */
1390 (!skb_queue_empty(&sk->sk_receive_queue) ||
3720d40b 1391 (sz_copied < target)) && /* and more is ready or required */
f64f9e71
JP
1392 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1393 (!err)) /* and haven't reached a FIN */
b97bf3fd
PL
1394 goto restart;
1395
1396exit:
0c3141e9 1397 release_sock(sk);
a3b0a5a9 1398 return sz_copied ? sz_copied : res;
b97bf3fd
PL
1399}
1400
f288bef4
YX
1401/**
1402 * tipc_write_space - wake up thread if port congestion is released
1403 * @sk: socket
1404 */
1405static void tipc_write_space(struct sock *sk)
1406{
1407 struct socket_wq *wq;
1408
1409 rcu_read_lock();
1410 wq = rcu_dereference(sk->sk_wq);
1411 if (wq_has_sleeper(wq))
1412 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1413 POLLWRNORM | POLLWRBAND);
1414 rcu_read_unlock();
1415}
1416
1417/**
1418 * tipc_data_ready - wake up threads to indicate messages have been received
1419 * @sk: socket
1420 * @len: the length of messages
1421 */
676d2369 1422static void tipc_data_ready(struct sock *sk)
f288bef4
YX
1423{
1424 struct socket_wq *wq;
1425
1426 rcu_read_lock();
1427 wq = rcu_dereference(sk->sk_wq);
1428 if (wq_has_sleeper(wq))
1429 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1430 POLLRDNORM | POLLRDBAND);
1431 rcu_read_unlock();
1432}
1433
7e6c131e
YX
1434/**
1435 * filter_connect - Handle all incoming messages for a connection-based socket
58ed9442 1436 * @tsk: TIPC socket
7e6c131e
YX
1437 * @msg: message
1438 *
e4de5fab 1439 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
7e6c131e 1440 */
e4de5fab 1441static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
7e6c131e 1442{
58ed9442
JPM
1443 struct sock *sk = &tsk->sk;
1444 struct tipc_port *port = &tsk->port;
8826cde6 1445 struct socket *sock = sk->sk_socket;
7e6c131e 1446 struct tipc_msg *msg = buf_msg(*buf);
8826cde6 1447
e4de5fab 1448 int retval = -TIPC_ERR_NO_PORT;
7e6c131e
YX
1449
1450 if (msg_mcast(msg))
1451 return retval;
1452
1453 switch ((int)sock->state) {
1454 case SS_CONNECTED:
1455 /* Accept only connection-based messages sent by peer */
8826cde6 1456 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
7e6c131e
YX
1457 if (unlikely(msg_errcode(msg))) {
1458 sock->state = SS_DISCONNECTING;
dadebc00
JPM
1459 port->connected = 0;
1460 /* let timer expire on it's own */
1461 tipc_node_remove_conn(tipc_port_peernode(port),
1462 port->ref);
7e6c131e
YX
1463 }
1464 retval = TIPC_OK;
1465 }
1466 break;
1467 case SS_CONNECTING:
1468 /* Accept only ACK or NACK message */
dadebc00
JPM
1469
1470 if (unlikely(!msg_connected(msg)))
1471 break;
1472
584d24b3
YX
1473 if (unlikely(msg_errcode(msg))) {
1474 sock->state = SS_DISCONNECTING;
2c8d8518 1475 sk->sk_err = ECONNREFUSED;
584d24b3
YX
1476 retval = TIPC_OK;
1477 break;
1478 }
1479
dadebc00 1480 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
584d24b3 1481 sock->state = SS_DISCONNECTING;
dadebc00 1482 sk->sk_err = EINVAL;
7e6c131e 1483 retval = TIPC_OK;
584d24b3
YX
1484 break;
1485 }
1486
dadebc00
JPM
1487 tipc_sk_finish_conn(port, msg_origport(msg), msg_orignode(msg));
1488 msg_set_importance(&port->phdr, msg_importance(msg));
1489 sock->state = SS_CONNECTED;
1490
584d24b3
YX
1491 /* If an incoming message is an 'ACK-', it should be
1492 * discarded here because it doesn't contain useful
1493 * data. In addition, we should try to wake up
1494 * connect() routine if sleeping.
1495 */
1496 if (msg_data_sz(msg) == 0) {
1497 kfree_skb(*buf);
1498 *buf = NULL;
1499 if (waitqueue_active(sk_sleep(sk)))
1500 wake_up_interruptible(sk_sleep(sk));
1501 }
1502 retval = TIPC_OK;
7e6c131e
YX
1503 break;
1504 case SS_LISTENING:
1505 case SS_UNCONNECTED:
1506 /* Accept only SYN message */
1507 if (!msg_connected(msg) && !(msg_errcode(msg)))
1508 retval = TIPC_OK;
1509 break;
1510 case SS_DISCONNECTING:
1511 break;
1512 default:
1513 pr_err("Unknown socket state %u\n", sock->state);
1514 }
1515 return retval;
1516}
1517
aba79f33
YX
1518/**
1519 * rcvbuf_limit - get proper overload limit of socket receive queue
1520 * @sk: socket
1521 * @buf: message
1522 *
1523 * For all connection oriented messages, irrespective of importance,
1524 * the default overload value (i.e. 67MB) is set as limit.
1525 *
1526 * For all connectionless messages, by default new queue limits are
1527 * as belows:
1528 *
cc79dd1b
YX
1529 * TIPC_LOW_IMPORTANCE (4 MB)
1530 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1531 * TIPC_HIGH_IMPORTANCE (16 MB)
1532 * TIPC_CRITICAL_IMPORTANCE (32 MB)
aba79f33
YX
1533 *
1534 * Returns overload limit according to corresponding message importance
1535 */
1536static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1537{
1538 struct tipc_msg *msg = buf_msg(buf);
aba79f33
YX
1539
1540 if (msg_connected(msg))
0cee6bbe 1541 return sysctl_tipc_rmem[2];
1542
1543 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1544 msg_importance(msg);
aba79f33
YX
1545}
1546
c4307285 1547/**
0c3141e9
AS
1548 * filter_rcv - validate incoming message
1549 * @sk: socket
b97bf3fd 1550 * @buf: message
c4307285 1551 *
0c3141e9
AS
1552 * Enqueues message on receive queue if acceptable; optionally handles
1553 * disconnect indication for a connected socket.
1554 *
1555 * Called with socket lock already taken; port lock may also be taken.
c4307285 1556 *
e4de5fab 1557 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
ac0074ee 1558 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
b97bf3fd 1559 */
e4de5fab 1560static int filter_rcv(struct sock *sk, struct sk_buff *buf)
b97bf3fd 1561{
0c3141e9 1562 struct socket *sock = sk->sk_socket;
58ed9442 1563 struct tipc_sock *tsk = tipc_sk(sk);
b97bf3fd 1564 struct tipc_msg *msg = buf_msg(buf);
aba79f33 1565 unsigned int limit = rcvbuf_limit(sk, buf);
ac0074ee 1566 u32 onode;
e4de5fab 1567 int rc = TIPC_OK;
b97bf3fd 1568
ac0074ee
JPM
1569 if (unlikely(msg_user(msg) == CONN_MANAGER))
1570 return tipc_sk_proto_rcv(tsk, &onode, buf);
ec8a2e56 1571
50100a5e
JPM
1572 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1573 kfree_skb(buf);
1574 tsk->link_cong = 0;
1575 sk->sk_write_space(sk);
1576 return TIPC_OK;
1577 }
1578
b97bf3fd 1579 /* Reject message if it is wrong sort of message for socket */
aad58547 1580 if (msg_type(msg) > TIPC_DIRECT_MSG)
e4de5fab 1581 return -TIPC_ERR_NO_PORT;
0c3141e9 1582
b97bf3fd 1583 if (sock->state == SS_READY) {
b29f1428 1584 if (msg_connected(msg))
e4de5fab 1585 return -TIPC_ERR_NO_PORT;
b97bf3fd 1586 } else {
e4de5fab
JPM
1587 rc = filter_connect(tsk, &buf);
1588 if (rc != TIPC_OK || buf == NULL)
1589 return rc;
b97bf3fd
PL
1590 }
1591
1592 /* Reject message if there isn't room to queue it */
aba79f33 1593 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
e4de5fab 1594 return -TIPC_ERR_OVERLOAD;
b97bf3fd 1595
aba79f33 1596 /* Enqueue message */
40682432 1597 TIPC_SKB_CB(buf)->handle = NULL;
0c3141e9 1598 __skb_queue_tail(&sk->sk_receive_queue, buf);
aba79f33 1599 skb_set_owner_r(buf, sk);
0c3141e9 1600
676d2369 1601 sk->sk_data_ready(sk);
0c3141e9
AS
1602 return TIPC_OK;
1603}
b97bf3fd 1604
0c3141e9 1605/**
4f4482dc 1606 * tipc_backlog_rcv - handle incoming message from backlog queue
0c3141e9
AS
1607 * @sk: socket
1608 * @buf: message
1609 *
1610 * Caller must hold socket lock, but not port lock.
1611 *
1612 * Returns 0
1613 */
4f4482dc 1614static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
0c3141e9 1615{
e4de5fab 1616 int rc;
8db1bae3 1617 u32 onode;
4f4482dc 1618 struct tipc_sock *tsk = tipc_sk(sk);
02c00c2a 1619 uint truesize = buf->truesize;
0c3141e9 1620
e4de5fab 1621 rc = filter_rcv(sk, buf);
4f4482dc 1622
ac0074ee
JPM
1623 if (likely(!rc)) {
1624 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1625 atomic_add(truesize, &tsk->dupl_rcvcnt);
1626 return 0;
1627 }
1628
1629 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1630 return 0;
1631
9fbfb8b1 1632 tipc_link_xmit(buf, onode, 0);
4f4482dc 1633
0c3141e9
AS
1634 return 0;
1635}
1636
1637/**
24be34b5 1638 * tipc_sk_rcv - handle incoming message
9816f061
JPM
1639 * @buf: buffer containing arriving message
1640 * Consumes buffer
1641 * Returns 0 if success, or errno: -EHOSTUNREACH
0c3141e9 1642 */
9816f061 1643int tipc_sk_rcv(struct sk_buff *buf)
0c3141e9 1644{
9816f061
JPM
1645 struct tipc_sock *tsk;
1646 struct tipc_port *port;
1647 struct sock *sk;
1648 u32 dport = msg_destport(buf_msg(buf));
e4de5fab 1649 int rc = TIPC_OK;
4f4482dc 1650 uint limit;
8db1bae3 1651 u32 dnode;
9816f061 1652
5a379074 1653 /* Validate destination and message */
9b50fd08
JPM
1654 tsk = tipc_port_lock(dport);
1655 if (unlikely(!tsk)) {
5a379074 1656 rc = tipc_msg_eval(buf, &dnode);
9816f061
JPM
1657 goto exit;
1658 }
9b50fd08 1659 port = &tsk->port;
9816f061
JPM
1660 sk = &tsk->sk;
1661
1662 /* Queue message */
0c3141e9 1663 bh_lock_sock(sk);
9816f061 1664
0c3141e9 1665 if (!sock_owned_by_user(sk)) {
e4de5fab 1666 rc = filter_rcv(sk, buf);
0c3141e9 1667 } else {
4f4482dc
JPM
1668 if (sk->sk_backlog.len == 0)
1669 atomic_set(&tsk->dupl_rcvcnt, 0);
1670 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1671 if (sk_add_backlog(sk, buf, limit))
e4de5fab 1672 rc = -TIPC_ERR_OVERLOAD;
0c3141e9
AS
1673 }
1674 bh_unlock_sock(sk);
9816f061 1675 tipc_port_unlock(port);
0c3141e9 1676
e4de5fab 1677 if (likely(!rc))
9816f061
JPM
1678 return 0;
1679exit:
5a379074 1680 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
8db1bae3 1681 return -EHOSTUNREACH;
5a379074 1682
9fbfb8b1 1683 tipc_link_xmit(buf, dnode, 0);
5a379074 1684 return (rc < 0) ? -EHOSTUNREACH : 0;
b97bf3fd
PL
1685}
1686
78eb3a53
YX
1687static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1688{
1689 struct sock *sk = sock->sk;
1690 DEFINE_WAIT(wait);
1691 int done;
1692
1693 do {
1694 int err = sock_error(sk);
1695 if (err)
1696 return err;
1697 if (!*timeo_p)
1698 return -ETIMEDOUT;
1699 if (signal_pending(current))
1700 return sock_intr_errno(*timeo_p);
1701
1702 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1703 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1704 finish_wait(sk_sleep(sk), &wait);
1705 } while (!done);
1706 return 0;
1707}
1708
b97bf3fd 1709/**
247f0f3c 1710 * tipc_connect - establish a connection to another TIPC port
b97bf3fd
PL
1711 * @sock: socket structure
1712 * @dest: socket address for destination port
1713 * @destlen: size of socket address data structure
0c3141e9 1714 * @flags: file-related flags associated with socket
b97bf3fd
PL
1715 *
1716 * Returns 0 on success, errno otherwise
1717 */
247f0f3c
YX
1718static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1719 int destlen, int flags)
b97bf3fd 1720{
0c3141e9 1721 struct sock *sk = sock->sk;
b89741a0
AS
1722 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1723 struct msghdr m = {NULL,};
78eb3a53
YX
1724 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1725 socket_state previous;
b89741a0
AS
1726 int res;
1727
0c3141e9
AS
1728 lock_sock(sk);
1729
b89741a0 1730 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
0c3141e9
AS
1731 if (sock->state == SS_READY) {
1732 res = -EOPNOTSUPP;
1733 goto exit;
1734 }
b89741a0 1735
b89741a0
AS
1736 /*
1737 * Reject connection attempt using multicast address
1738 *
1739 * Note: send_msg() validates the rest of the address fields,
1740 * so there's no need to do it here
1741 */
0c3141e9
AS
1742 if (dst->addrtype == TIPC_ADDR_MCAST) {
1743 res = -EINVAL;
1744 goto exit;
1745 }
1746
78eb3a53 1747 previous = sock->state;
584d24b3
YX
1748 switch (sock->state) {
1749 case SS_UNCONNECTED:
1750 /* Send a 'SYN-' to destination */
1751 m.msg_name = dest;
1752 m.msg_namelen = destlen;
1753
1754 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1755 * indicate send_msg() is never blocked.
1756 */
1757 if (!timeout)
1758 m.msg_flags = MSG_DONTWAIT;
1759
247f0f3c 1760 res = tipc_sendmsg(NULL, sock, &m, 0);
584d24b3
YX
1761 if ((res < 0) && (res != -EWOULDBLOCK))
1762 goto exit;
1763
1764 /* Just entered SS_CONNECTING state; the only
1765 * difference is that return value in non-blocking
1766 * case is EINPROGRESS, rather than EALREADY.
1767 */
1768 res = -EINPROGRESS;
584d24b3 1769 case SS_CONNECTING:
78eb3a53
YX
1770 if (previous == SS_CONNECTING)
1771 res = -EALREADY;
1772 if (!timeout)
1773 goto exit;
1774 timeout = msecs_to_jiffies(timeout);
1775 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1776 res = tipc_wait_for_connect(sock, &timeout);
584d24b3
YX
1777 break;
1778 case SS_CONNECTED:
1779 res = -EISCONN;
1780 break;
1781 default:
1782 res = -EINVAL;
78eb3a53 1783 break;
b89741a0 1784 }
0c3141e9
AS
1785exit:
1786 release_sock(sk);
b89741a0 1787 return res;
b97bf3fd
PL
1788}
1789
c4307285 1790/**
247f0f3c 1791 * tipc_listen - allow socket to listen for incoming connections
b97bf3fd
PL
1792 * @sock: socket structure
1793 * @len: (unused)
c4307285 1794 *
b97bf3fd
PL
1795 * Returns 0 on success, errno otherwise
1796 */
247f0f3c 1797static int tipc_listen(struct socket *sock, int len)
b97bf3fd 1798{
0c3141e9
AS
1799 struct sock *sk = sock->sk;
1800 int res;
1801
1802 lock_sock(sk);
b97bf3fd 1803
245f3d34 1804 if (sock->state != SS_UNCONNECTED)
0c3141e9
AS
1805 res = -EINVAL;
1806 else {
1807 sock->state = SS_LISTENING;
1808 res = 0;
1809 }
1810
1811 release_sock(sk);
1812 return res;
b97bf3fd
PL
1813}
1814
6398e23c
YX
1815static int tipc_wait_for_accept(struct socket *sock, long timeo)
1816{
1817 struct sock *sk = sock->sk;
1818 DEFINE_WAIT(wait);
1819 int err;
1820
1821 /* True wake-one mechanism for incoming connections: only
1822 * one process gets woken up, not the 'whole herd'.
1823 * Since we do not 'race & poll' for established sockets
1824 * anymore, the common case will execute the loop only once.
1825 */
1826 for (;;) {
1827 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1828 TASK_INTERRUPTIBLE);
fe8e4649 1829 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
6398e23c
YX
1830 release_sock(sk);
1831 timeo = schedule_timeout(timeo);
1832 lock_sock(sk);
1833 }
1834 err = 0;
1835 if (!skb_queue_empty(&sk->sk_receive_queue))
1836 break;
1837 err = -EINVAL;
1838 if (sock->state != SS_LISTENING)
1839 break;
1840 err = sock_intr_errno(timeo);
1841 if (signal_pending(current))
1842 break;
1843 err = -EAGAIN;
1844 if (!timeo)
1845 break;
1846 }
1847 finish_wait(sk_sleep(sk), &wait);
1848 return err;
1849}
1850
c4307285 1851/**
247f0f3c 1852 * tipc_accept - wait for connection request
b97bf3fd
PL
1853 * @sock: listening socket
1854 * @newsock: new socket that is to be connected
1855 * @flags: file-related flags associated with socket
c4307285 1856 *
b97bf3fd
PL
1857 * Returns 0 on success, errno otherwise
1858 */
247f0f3c 1859static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
b97bf3fd 1860{
0fef8f20 1861 struct sock *new_sk, *sk = sock->sk;
b97bf3fd 1862 struct sk_buff *buf;
8826cde6 1863 struct tipc_port *new_port;
0fef8f20 1864 struct tipc_msg *msg;
6398e23c 1865 long timeo;
0c3141e9 1866 int res;
b97bf3fd 1867
0c3141e9 1868 lock_sock(sk);
b97bf3fd 1869
0c3141e9
AS
1870 if (sock->state != SS_LISTENING) {
1871 res = -EINVAL;
b97bf3fd
PL
1872 goto exit;
1873 }
6398e23c
YX
1874 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1875 res = tipc_wait_for_accept(sock, timeo);
1876 if (res)
1877 goto exit;
0c3141e9
AS
1878
1879 buf = skb_peek(&sk->sk_receive_queue);
1880
c5fa7b3c 1881 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
0fef8f20
PG
1882 if (res)
1883 goto exit;
b97bf3fd 1884
0fef8f20 1885 new_sk = new_sock->sk;
58ed9442 1886 new_port = &tipc_sk(new_sk)->port;
0fef8f20 1887 msg = buf_msg(buf);
b97bf3fd 1888
0fef8f20
PG
1889 /* we lock on new_sk; but lockdep sees the lock on sk */
1890 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
1891
1892 /*
1893 * Reject any stray messages received by new socket
1894 * before the socket lock was taken (very, very unlikely)
1895 */
1896 reject_rx_queue(new_sk);
1897
1898 /* Connect new socket to it's peer */
dadebc00 1899 tipc_sk_finish_conn(new_port, msg_origport(msg), msg_orignode(msg));
0fef8f20
PG
1900 new_sock->state = SS_CONNECTED;
1901
3b4f302d 1902 tipc_port_set_importance(new_port, msg_importance(msg));
0fef8f20 1903 if (msg_named(msg)) {
8826cde6
JPM
1904 new_port->conn_type = msg_nametype(msg);
1905 new_port->conn_instance = msg_nameinst(msg);
b97bf3fd 1906 }
0fef8f20
PG
1907
1908 /*
1909 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1910 * Respond to 'SYN+' by queuing it on new socket.
1911 */
1912 if (!msg_data_sz(msg)) {
1913 struct msghdr m = {NULL,};
1914
1915 advance_rx_queue(sk);
247f0f3c 1916 tipc_send_packet(NULL, new_sock, &m, 0);
0fef8f20
PG
1917 } else {
1918 __skb_dequeue(&sk->sk_receive_queue);
1919 __skb_queue_head(&new_sk->sk_receive_queue, buf);
aba79f33 1920 skb_set_owner_r(buf, new_sk);
0fef8f20
PG
1921 }
1922 release_sock(new_sk);
b97bf3fd 1923exit:
0c3141e9 1924 release_sock(sk);
b97bf3fd
PL
1925 return res;
1926}
1927
1928/**
247f0f3c 1929 * tipc_shutdown - shutdown socket connection
b97bf3fd 1930 * @sock: socket structure
e247a8f5 1931 * @how: direction to close (must be SHUT_RDWR)
b97bf3fd
PL
1932 *
1933 * Terminates connection (if necessary), then purges socket's receive queue.
c4307285 1934 *
b97bf3fd
PL
1935 * Returns 0 on success, errno otherwise
1936 */
247f0f3c 1937static int tipc_shutdown(struct socket *sock, int how)
b97bf3fd 1938{
0c3141e9 1939 struct sock *sk = sock->sk;
58ed9442
JPM
1940 struct tipc_sock *tsk = tipc_sk(sk);
1941 struct tipc_port *port = &tsk->port;
b97bf3fd 1942 struct sk_buff *buf;
80e44c22 1943 u32 dnode;
b97bf3fd
PL
1944 int res;
1945
e247a8f5
AS
1946 if (how != SHUT_RDWR)
1947 return -EINVAL;
b97bf3fd 1948
0c3141e9 1949 lock_sock(sk);
b97bf3fd
PL
1950
1951 switch (sock->state) {
0c3141e9 1952 case SS_CONNECTING:
b97bf3fd
PL
1953 case SS_CONNECTED:
1954
b97bf3fd 1955restart:
617d3c7a 1956 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
0c3141e9
AS
1957 buf = __skb_dequeue(&sk->sk_receive_queue);
1958 if (buf) {
40682432 1959 if (TIPC_SKB_CB(buf)->handle != NULL) {
5f6d9123 1960 kfree_skb(buf);
b97bf3fd
PL
1961 goto restart;
1962 }
80e44c22
JPM
1963 if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN))
1964 tipc_link_xmit(buf, dnode, port->ref);
dadebc00 1965 tipc_node_remove_conn(dnode, port->ref);
0c3141e9 1966 } else {
80e44c22
JPM
1967 dnode = tipc_port_peernode(port);
1968 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
1969 TIPC_CONN_MSG, SHORT_H_SIZE,
1970 0, dnode, tipc_own_addr,
1971 tipc_port_peerport(port),
1972 port->ref, TIPC_CONN_SHUTDOWN);
1973 tipc_link_xmit(buf, dnode, port->ref);
b97bf3fd 1974 }
dadebc00 1975 port->connected = 0;
0c3141e9 1976 sock->state = SS_DISCONNECTING;
dadebc00 1977 tipc_node_remove_conn(dnode, port->ref);
b97bf3fd
PL
1978 /* fall through */
1979
1980 case SS_DISCONNECTING:
1981
75031151 1982 /* Discard any unreceived messages */
57467e56 1983 __skb_queue_purge(&sk->sk_receive_queue);
75031151
YX
1984
1985 /* Wake up anyone sleeping in poll */
1986 sk->sk_state_change(sk);
b97bf3fd
PL
1987 res = 0;
1988 break;
1989
1990 default:
1991 res = -ENOTCONN;
1992 }
1993
0c3141e9 1994 release_sock(sk);
b97bf3fd
PL
1995 return res;
1996}
1997
57289015
JPM
1998static void tipc_sk_timeout(unsigned long ref)
1999{
9b50fd08
JPM
2000 struct tipc_sock *tsk = tipc_port_lock(ref);
2001 struct tipc_port *port;
57289015
JPM
2002 struct sock *sk;
2003 struct sk_buff *buf = NULL;
2004 struct tipc_msg *msg = NULL;
2005 u32 peer_port, peer_node;
2006
9b50fd08 2007 if (!tsk)
57289015
JPM
2008 return;
2009
9b50fd08 2010 port = &tsk->port;
57289015
JPM
2011 if (!port->connected) {
2012 tipc_port_unlock(port);
2013 return;
2014 }
57289015
JPM
2015 sk = &tsk->sk;
2016 bh_lock_sock(sk);
2017 peer_port = tipc_port_peerport(port);
2018 peer_node = tipc_port_peernode(port);
2019
2020 if (port->probing_state == TIPC_CONN_PROBING) {
2021 /* Previous probe not answered -> self abort */
2022 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
2023 SHORT_H_SIZE, 0, tipc_own_addr,
2024 peer_node, ref, peer_port,
2025 TIPC_ERR_NO_PORT);
2026 } else {
2027 buf = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
2028 0, peer_node, tipc_own_addr,
2029 peer_port, ref, TIPC_OK);
2030 port->probing_state = TIPC_CONN_PROBING;
2031 k_start_timer(&port->timer, port->probing_interval);
2032 }
2033 bh_unlock_sock(sk);
2034 tipc_port_unlock(port);
2035 if (!buf)
2036 return;
2037
2038 msg = buf_msg(buf);
2039 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
2040}
2041
5a9ee0be
JPM
2042static int tipc_sk_show(struct tipc_port *port, char *buf,
2043 int len, int full_id)
2044{
2045 struct publication *publ;
2046 int ret;
2047
2048 if (full_id)
2049 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
2050 tipc_zone(tipc_own_addr),
2051 tipc_cluster(tipc_own_addr),
2052 tipc_node(tipc_own_addr), port->ref);
2053 else
2054 ret = tipc_snprintf(buf, len, "%-10u:", port->ref);
2055
2056 if (port->connected) {
2057 u32 dport = tipc_port_peerport(port);
2058 u32 destnode = tipc_port_peernode(port);
2059
2060 ret += tipc_snprintf(buf + ret, len - ret,
2061 " connected to <%u.%u.%u:%u>",
2062 tipc_zone(destnode),
2063 tipc_cluster(destnode),
2064 tipc_node(destnode), dport);
2065 if (port->conn_type != 0)
2066 ret += tipc_snprintf(buf + ret, len - ret,
2067 " via {%u,%u}", port->conn_type,
2068 port->conn_instance);
2069 } else if (port->published) {
2070 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
2071 list_for_each_entry(publ, &port->publications, pport_list) {
2072 if (publ->lower == publ->upper)
2073 ret += tipc_snprintf(buf + ret, len - ret,
2074 " {%u,%u}", publ->type,
2075 publ->lower);
2076 else
2077 ret += tipc_snprintf(buf + ret, len - ret,
2078 " {%u,%u,%u}", publ->type,
2079 publ->lower, publ->upper);
2080 }
2081 }
2082 ret += tipc_snprintf(buf + ret, len - ret, "\n");
2083 return ret;
2084}
2085
2086struct sk_buff *tipc_sk_socks_show(void)
2087{
2088 struct sk_buff *buf;
2089 struct tlv_desc *rep_tlv;
2090 char *pb;
2091 int pb_len;
2092 struct tipc_sock *tsk;
2093 int str_len = 0;
2094 u32 ref = 0;
2095
2096 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2097 if (!buf)
2098 return NULL;
2099 rep_tlv = (struct tlv_desc *)buf->data;
2100 pb = TLV_DATA(rep_tlv);
2101 pb_len = ULTRA_STRING_MAX_LEN;
2102
2103 tsk = tipc_sk_lock_next(&ref);
2104 for (; tsk; tsk = tipc_sk_lock_next(&ref)) {
2105 bh_lock_sock(&tsk->sk);
2106 str_len += tipc_sk_show(&tsk->port, pb + str_len,
2107 pb_len - str_len, 0);
2108 bh_unlock_sock(&tsk->sk);
2109 tipc_port_unlock(&tsk->port);
2110 }
2111 str_len += 1; /* for "\0" */
2112 skb_put(buf, TLV_SPACE(str_len));
2113 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2114
2115 return buf;
2116}
2117
2118/* tipc_sk_reinit: set non-zero address in all existing sockets
2119 * when we go from standalone to network mode.
2120 */
2121void tipc_sk_reinit(void)
2122{
2123 struct tipc_msg *msg;
2124 u32 ref = 0;
2125 struct tipc_sock *tsk = tipc_sk_lock_next(&ref);
2126
2127 for (; tsk; tsk = tipc_sk_lock_next(&ref)) {
2128 bh_lock_sock(&tsk->sk);
2129 msg = &tsk->port.phdr;
2130 msg_set_prevnode(msg, tipc_own_addr);
2131 msg_set_orignode(msg, tipc_own_addr);
2132 bh_unlock_sock(&tsk->sk);
2133 tipc_port_unlock(&tsk->port);
2134 }
2135}
2136
b97bf3fd 2137/**
247f0f3c 2138 * tipc_setsockopt - set socket option
b97bf3fd
PL
2139 * @sock: socket structure
2140 * @lvl: option level
2141 * @opt: option identifier
2142 * @ov: pointer to new option value
2143 * @ol: length of option value
c4307285
YH
2144 *
2145 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
b97bf3fd 2146 * (to ease compatibility).
c4307285 2147 *
b97bf3fd
PL
2148 * Returns 0 on success, errno otherwise
2149 */
247f0f3c
YX
2150static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2151 char __user *ov, unsigned int ol)
b97bf3fd 2152{
0c3141e9 2153 struct sock *sk = sock->sk;
58ed9442
JPM
2154 struct tipc_sock *tsk = tipc_sk(sk);
2155 struct tipc_port *port = &tsk->port;
b97bf3fd
PL
2156 u32 value;
2157 int res;
2158
c4307285
YH
2159 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2160 return 0;
b97bf3fd
PL
2161 if (lvl != SOL_TIPC)
2162 return -ENOPROTOOPT;
2163 if (ol < sizeof(value))
2164 return -EINVAL;
2db9983a
AS
2165 res = get_user(value, (u32 __user *)ov);
2166 if (res)
b97bf3fd
PL
2167 return res;
2168
0c3141e9 2169 lock_sock(sk);
c4307285 2170
b97bf3fd
PL
2171 switch (opt) {
2172 case TIPC_IMPORTANCE:
ac32c7f7 2173 res = tipc_port_set_importance(port, value);
b97bf3fd
PL
2174 break;
2175 case TIPC_SRC_DROPPABLE:
2176 if (sock->type != SOCK_STREAM)
58ed9442 2177 tipc_port_set_unreliable(port, value);
c4307285 2178 else
b97bf3fd
PL
2179 res = -ENOPROTOOPT;
2180 break;
2181 case TIPC_DEST_DROPPABLE:
58ed9442 2182 tipc_port_set_unreturnable(port, value);
b97bf3fd
PL
2183 break;
2184 case TIPC_CONN_TIMEOUT:
a0f40f02 2185 tipc_sk(sk)->conn_timeout = value;
0c3141e9 2186 /* no need to set "res", since already 0 at this point */
b97bf3fd
PL
2187 break;
2188 default:
2189 res = -EINVAL;
2190 }
2191
0c3141e9
AS
2192 release_sock(sk);
2193
b97bf3fd
PL
2194 return res;
2195}
2196
2197/**
247f0f3c 2198 * tipc_getsockopt - get socket option
b97bf3fd
PL
2199 * @sock: socket structure
2200 * @lvl: option level
2201 * @opt: option identifier
2202 * @ov: receptacle for option value
2203 * @ol: receptacle for length of option value
c4307285
YH
2204 *
2205 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
b97bf3fd 2206 * (to ease compatibility).
c4307285 2207 *
b97bf3fd
PL
2208 * Returns 0 on success, errno otherwise
2209 */
247f0f3c
YX
2210static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2211 char __user *ov, int __user *ol)
b97bf3fd 2212{
0c3141e9 2213 struct sock *sk = sock->sk;
58ed9442
JPM
2214 struct tipc_sock *tsk = tipc_sk(sk);
2215 struct tipc_port *port = &tsk->port;
c4307285 2216 int len;
b97bf3fd 2217 u32 value;
c4307285 2218 int res;
b97bf3fd 2219
c4307285
YH
2220 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2221 return put_user(0, ol);
b97bf3fd
PL
2222 if (lvl != SOL_TIPC)
2223 return -ENOPROTOOPT;
2db9983a
AS
2224 res = get_user(len, ol);
2225 if (res)
c4307285 2226 return res;
b97bf3fd 2227
0c3141e9 2228 lock_sock(sk);
b97bf3fd
PL
2229
2230 switch (opt) {
2231 case TIPC_IMPORTANCE:
58ed9442 2232 value = tipc_port_importance(port);
b97bf3fd
PL
2233 break;
2234 case TIPC_SRC_DROPPABLE:
58ed9442 2235 value = tipc_port_unreliable(port);
b97bf3fd
PL
2236 break;
2237 case TIPC_DEST_DROPPABLE:
58ed9442 2238 value = tipc_port_unreturnable(port);
b97bf3fd
PL
2239 break;
2240 case TIPC_CONN_TIMEOUT:
a0f40f02 2241 value = tipc_sk(sk)->conn_timeout;
0c3141e9 2242 /* no need to set "res", since already 0 at this point */
b97bf3fd 2243 break;
0e65967e 2244 case TIPC_NODE_RECVQ_DEPTH:
9da3d475 2245 value = 0; /* was tipc_queue_size, now obsolete */
6650613d 2246 break;
0e65967e 2247 case TIPC_SOCK_RECVQ_DEPTH:
6650613d 2248 value = skb_queue_len(&sk->sk_receive_queue);
2249 break;
b97bf3fd
PL
2250 default:
2251 res = -EINVAL;
2252 }
2253
0c3141e9
AS
2254 release_sock(sk);
2255
25860c3b
PG
2256 if (res)
2257 return res; /* "get" failed */
b97bf3fd 2258
25860c3b
PG
2259 if (len < sizeof(value))
2260 return -EINVAL;
2261
2262 if (copy_to_user(ov, &value, sizeof(value)))
2263 return -EFAULT;
2264
2265 return put_user(sizeof(value), ol);
b97bf3fd
PL
2266}
2267
52f50ce5 2268static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
78acb1f9
EH
2269{
2270 struct tipc_sioc_ln_req lnr;
2271 void __user *argp = (void __user *)arg;
2272
2273 switch (cmd) {
2274 case SIOCGETLINKNAME:
2275 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2276 return -EFAULT;
2277 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
2278 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2279 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2280 return -EFAULT;
2281 return 0;
2282 }
2283 return -EADDRNOTAVAIL;
78acb1f9
EH
2284 default:
2285 return -ENOIOCTLCMD;
2286 }
2287}
2288
ae86b9e3
BH
2289/* Protocol switches for the various types of TIPC sockets */
2290
bca65eae 2291static const struct proto_ops msg_ops = {
0e65967e 2292 .owner = THIS_MODULE,
b97bf3fd 2293 .family = AF_TIPC,
247f0f3c
YX
2294 .release = tipc_release,
2295 .bind = tipc_bind,
2296 .connect = tipc_connect,
5eee6a6d 2297 .socketpair = sock_no_socketpair,
245f3d34 2298 .accept = sock_no_accept,
247f0f3c
YX
2299 .getname = tipc_getname,
2300 .poll = tipc_poll,
78acb1f9 2301 .ioctl = tipc_ioctl,
245f3d34 2302 .listen = sock_no_listen,
247f0f3c
YX
2303 .shutdown = tipc_shutdown,
2304 .setsockopt = tipc_setsockopt,
2305 .getsockopt = tipc_getsockopt,
2306 .sendmsg = tipc_sendmsg,
2307 .recvmsg = tipc_recvmsg,
8238745a
YH
2308 .mmap = sock_no_mmap,
2309 .sendpage = sock_no_sendpage
b97bf3fd
PL
2310};
2311
bca65eae 2312static const struct proto_ops packet_ops = {
0e65967e 2313 .owner = THIS_MODULE,
b97bf3fd 2314 .family = AF_TIPC,
247f0f3c
YX
2315 .release = tipc_release,
2316 .bind = tipc_bind,
2317 .connect = tipc_connect,
5eee6a6d 2318 .socketpair = sock_no_socketpair,
247f0f3c
YX
2319 .accept = tipc_accept,
2320 .getname = tipc_getname,
2321 .poll = tipc_poll,
78acb1f9 2322 .ioctl = tipc_ioctl,
247f0f3c
YX
2323 .listen = tipc_listen,
2324 .shutdown = tipc_shutdown,
2325 .setsockopt = tipc_setsockopt,
2326 .getsockopt = tipc_getsockopt,
2327 .sendmsg = tipc_send_packet,
2328 .recvmsg = tipc_recvmsg,
8238745a
YH
2329 .mmap = sock_no_mmap,
2330 .sendpage = sock_no_sendpage
b97bf3fd
PL
2331};
2332
bca65eae 2333static const struct proto_ops stream_ops = {
0e65967e 2334 .owner = THIS_MODULE,
b97bf3fd 2335 .family = AF_TIPC,
247f0f3c
YX
2336 .release = tipc_release,
2337 .bind = tipc_bind,
2338 .connect = tipc_connect,
5eee6a6d 2339 .socketpair = sock_no_socketpair,
247f0f3c
YX
2340 .accept = tipc_accept,
2341 .getname = tipc_getname,
2342 .poll = tipc_poll,
78acb1f9 2343 .ioctl = tipc_ioctl,
247f0f3c
YX
2344 .listen = tipc_listen,
2345 .shutdown = tipc_shutdown,
2346 .setsockopt = tipc_setsockopt,
2347 .getsockopt = tipc_getsockopt,
2348 .sendmsg = tipc_send_stream,
2349 .recvmsg = tipc_recv_stream,
8238745a
YH
2350 .mmap = sock_no_mmap,
2351 .sendpage = sock_no_sendpage
b97bf3fd
PL
2352};
2353
bca65eae 2354static const struct net_proto_family tipc_family_ops = {
0e65967e 2355 .owner = THIS_MODULE,
b97bf3fd 2356 .family = AF_TIPC,
c5fa7b3c 2357 .create = tipc_sk_create
b97bf3fd
PL
2358};
2359
2360static struct proto tipc_proto = {
2361 .name = "TIPC",
2362 .owner = THIS_MODULE,
cc79dd1b
YX
2363 .obj_size = sizeof(struct tipc_sock),
2364 .sysctl_rmem = sysctl_tipc_rmem
b97bf3fd
PL
2365};
2366
c5fa7b3c
YX
2367static struct proto tipc_proto_kern = {
2368 .name = "TIPC",
2369 .obj_size = sizeof(struct tipc_sock),
2370 .sysctl_rmem = sysctl_tipc_rmem
2371};
2372
b97bf3fd 2373/**
4323add6 2374 * tipc_socket_init - initialize TIPC socket interface
c4307285 2375 *
b97bf3fd
PL
2376 * Returns 0 on success, errno otherwise
2377 */
4323add6 2378int tipc_socket_init(void)
b97bf3fd
PL
2379{
2380 int res;
2381
c4307285 2382 res = proto_register(&tipc_proto, 1);
b97bf3fd 2383 if (res) {
2cf8aa19 2384 pr_err("Failed to register TIPC protocol type\n");
b97bf3fd
PL
2385 goto out;
2386 }
2387
2388 res = sock_register(&tipc_family_ops);
2389 if (res) {
2cf8aa19 2390 pr_err("Failed to register TIPC socket type\n");
b97bf3fd
PL
2391 proto_unregister(&tipc_proto);
2392 goto out;
2393 }
b97bf3fd
PL
2394 out:
2395 return res;
2396}
2397
2398/**
4323add6 2399 * tipc_socket_stop - stop TIPC socket interface
b97bf3fd 2400 */
4323add6 2401void tipc_socket_stop(void)
b97bf3fd 2402{
b97bf3fd
PL
2403 sock_unregister(tipc_family_ops.family);
2404 proto_unregister(&tipc_proto);
2405}
This page took 0.877556 seconds and 5 git commands to generate.