]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/tipc/socket.c
tipc: remove tsk->connected from tipc_sock
[mirror_ubuntu-bionic-kernel.git] / net / tipc / socket.c
CommitLineData
b97bf3fd 1/*
02c00c2a 2 * net/tipc/socket.c: TIPC socket API
c4307285 3 *
3c724acd 4 * Copyright (c) 2001-2007, 2012-2015, 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
07f6c4bc 37#include <linux/rhashtable.h>
b97bf3fd 38#include "core.h"
e2dafe87 39#include "name_table.h"
78acb1f9 40#include "node.h"
e2dafe87 41#include "link.h"
c637c103 42#include "name_distr.h"
2e84c60b 43#include "socket.h"
a6bf70f7 44#include "bcast.h"
49cc66ea 45#include "netlink.h"
2cf8aa19 46
07f6c4bc
YX
47#define SS_LISTENING -1 /* socket is listening */
48#define SS_READY -2 /* socket is connectionless */
b97bf3fd 49
07f6c4bc 50#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
2f55c437 51#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
07f6c4bc
YX
52#define TIPC_FWD_MSG 1
53#define TIPC_CONN_OK 0
54#define TIPC_CONN_PROBING 1
55#define TIPC_MAX_PORT 0xffffffff
56#define TIPC_MIN_PORT 1
301bae56
JPM
57
58/**
59 * struct tipc_sock - TIPC socket structure
60 * @sk: socket - interacts with 'port' and with user via the socket API
301bae56
JPM
61 * @conn_type: TIPC type used when connection was established
62 * @conn_instance: TIPC instance used when connection was established
63 * @published: non-zero if port has one or more associated names
64 * @max_pkt: maximum packet size "hint" used when building messages sent by port
07f6c4bc 65 * @portid: unique port identity in TIPC socket hash table
301bae56 66 * @phdr: preformatted message header used when sending messages
301bae56
JPM
67 * @publications: list of publications for port
68 * @pub_count: total # of publications port has made during its lifetime
69 * @probing_state:
2f55c437 70 * @probing_intv:
301bae56
JPM
71 * @conn_timeout: the time we can wait for an unresponded setup request
72 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
73 * @link_cong: non-zero if owner must sleep because of link congestion
74 * @sent_unacked: # messages sent by socket, and not yet acked by peer
75 * @rcv_unacked: # messages read by user, but not yet acked back to peer
aeda16b6 76 * @peer: 'connected' peer for dgram/rdm
07f6c4bc
YX
77 * @node: hash table node
78 * @rcu: rcu struct for tipc_sock
301bae56
JPM
79 */
80struct tipc_sock {
81 struct sock sk;
301bae56
JPM
82 u32 conn_type;
83 u32 conn_instance;
84 int published;
85 u32 max_pkt;
07f6c4bc 86 u32 portid;
301bae56
JPM
87 struct tipc_msg phdr;
88 struct list_head sock_list;
89 struct list_head publications;
90 u32 pub_count;
91 u32 probing_state;
2f55c437 92 unsigned long probing_intv;
301bae56
JPM
93 uint conn_timeout;
94 atomic_t dupl_rcvcnt;
95 bool link_cong;
10724cc7
JPM
96 u16 snt_unacked;
97 u16 snd_win;
60020e18 98 u16 peer_caps;
10724cc7
JPM
99 u16 rcv_unacked;
100 u16 rcv_win;
aeda16b6 101 struct sockaddr_tipc peer;
07f6c4bc
YX
102 struct rhash_head node;
103 struct rcu_head rcu;
301bae56 104};
b97bf3fd 105
4f4482dc 106static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
676d2369 107static void tipc_data_ready(struct sock *sk);
f288bef4 108static void tipc_write_space(struct sock *sk);
f4195d1e 109static void tipc_sock_destruct(struct sock *sk);
247f0f3c
YX
110static int tipc_release(struct socket *sock);
111static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
0abd8ff2 112static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
f2f2a96a 113static void tipc_sk_timeout(unsigned long data);
301bae56 114static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
0fc87aae 115 struct tipc_name_seq const *seq);
301bae56 116static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
0fc87aae 117 struct tipc_name_seq const *seq);
e05b31f4 118static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
07f6c4bc
YX
119static int tipc_sk_insert(struct tipc_sock *tsk);
120static void tipc_sk_remove(struct tipc_sock *tsk);
39a0295f
YX
121static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
122 size_t dsz);
123static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
b97bf3fd 124
bca65eae
FW
125static const struct proto_ops packet_ops;
126static const struct proto_ops stream_ops;
127static const struct proto_ops msg_ops;
b97bf3fd
PL
128static struct proto tipc_proto;
129
6cca7289
HX
130static const struct rhashtable_params tsk_rht_params;
131
c4307285 132/*
0c3141e9
AS
133 * Revised TIPC socket locking policy:
134 *
135 * Most socket operations take the standard socket lock when they start
136 * and hold it until they finish (or until they need to sleep). Acquiring
137 * this lock grants the owner exclusive access to the fields of the socket
138 * data structures, with the exception of the backlog queue. A few socket
139 * operations can be done without taking the socket lock because they only
140 * read socket information that never changes during the life of the socket.
141 *
142 * Socket operations may acquire the lock for the associated TIPC port if they
143 * need to perform an operation on the port. If any routine needs to acquire
144 * both the socket lock and the port lock it must take the socket lock first
145 * to avoid the risk of deadlock.
146 *
147 * The dispatcher handling incoming messages cannot grab the socket lock in
148 * the standard fashion, since invoked it runs at the BH level and cannot block.
149 * Instead, it checks to see if the socket lock is currently owned by someone,
150 * and either handles the message itself or adds it to the socket's backlog
151 * queue; in the latter case the queued message is processed once the process
152 * owning the socket lock releases it.
153 *
154 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
155 * the problem of a blocked socket operation preventing any other operations
156 * from occurring. However, applications must be careful if they have
157 * multiple threads trying to send (or receive) on the same socket, as these
158 * operations might interfere with each other. For example, doing a connect
159 * and a receive at the same time might allow the receive to consume the
160 * ACK message meant for the connect. While additional work could be done
161 * to try and overcome this, it doesn't seem to be worthwhile at the present.
162 *
163 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
164 * that another operation that must be performed in a non-blocking manner is
165 * not delayed for very long because the lock has already been taken.
166 *
167 * NOTE: This code assumes that certain fields of a port/socket pair are
168 * constant over its lifetime; such fields can be examined without taking
169 * the socket lock and/or port lock, and do not need to be re-read even
170 * after resuming processing after waiting. These fields include:
171 * - socket type
172 * - pointer to socket sk structure (aka tipc_sock structure)
173 * - pointer to port structure
174 * - port reference
175 */
176
c5898636
JPM
177static u32 tsk_own_node(struct tipc_sock *tsk)
178{
179 return msg_prevnode(&tsk->phdr);
180}
181
301bae56 182static u32 tsk_peer_node(struct tipc_sock *tsk)
2e84c60b 183{
301bae56 184 return msg_destnode(&tsk->phdr);
2e84c60b
JPM
185}
186
301bae56 187static u32 tsk_peer_port(struct tipc_sock *tsk)
2e84c60b 188{
301bae56 189 return msg_destport(&tsk->phdr);
2e84c60b
JPM
190}
191
301bae56 192static bool tsk_unreliable(struct tipc_sock *tsk)
2e84c60b 193{
301bae56 194 return msg_src_droppable(&tsk->phdr) != 0;
2e84c60b
JPM
195}
196
301bae56 197static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
2e84c60b 198{
301bae56 199 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
2e84c60b
JPM
200}
201
301bae56 202static bool tsk_unreturnable(struct tipc_sock *tsk)
2e84c60b 203{
301bae56 204 return msg_dest_droppable(&tsk->phdr) != 0;
2e84c60b
JPM
205}
206
301bae56 207static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
2e84c60b 208{
301bae56 209 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
2e84c60b
JPM
210}
211
301bae56 212static int tsk_importance(struct tipc_sock *tsk)
2e84c60b 213{
301bae56 214 return msg_importance(&tsk->phdr);
2e84c60b
JPM
215}
216
301bae56 217static int tsk_set_importance(struct tipc_sock *tsk, int imp)
2e84c60b
JPM
218{
219 if (imp > TIPC_CRITICAL_IMPORTANCE)
220 return -EINVAL;
301bae56 221 msg_set_importance(&tsk->phdr, (u32)imp);
2e84c60b
JPM
222 return 0;
223}
8826cde6 224
301bae56
JPM
225static struct tipc_sock *tipc_sk(const struct sock *sk)
226{
227 return container_of(sk, struct tipc_sock, sk);
228}
229
10724cc7 230static bool tsk_conn_cong(struct tipc_sock *tsk)
301bae56 231{
10724cc7
JPM
232 return tsk->snt_unacked >= tsk->snd_win;
233}
234
235/* tsk_blocks(): translate a buffer size in bytes to number of
236 * advertisable blocks, taking into account the ratio truesize(len)/len
237 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
238 */
239static u16 tsk_adv_blocks(int len)
240{
241 return len / FLOWCTL_BLK_SZ / 4;
242}
243
244/* tsk_inc(): increment counter for sent or received data
245 * - If block based flow control is not supported by peer we
246 * fall back to message based ditto, incrementing the counter
247 */
248static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
249{
250 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
251 return ((msglen / FLOWCTL_BLK_SZ) + 1);
252 return 1;
301bae56
JPM
253}
254
0c3141e9 255/**
2e84c60b 256 * tsk_advance_rx_queue - discard first buffer in socket receive queue
0c3141e9
AS
257 *
258 * Caller must hold socket lock
b97bf3fd 259 */
2e84c60b 260static void tsk_advance_rx_queue(struct sock *sk)
b97bf3fd 261{
5f6d9123 262 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
b97bf3fd
PL
263}
264
bcd3ffd4
JPM
265/* tipc_sk_respond() : send response message back to sender
266 */
267static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
268{
269 u32 selector;
270 u32 dnode;
271 u32 onode = tipc_own_addr(sock_net(sk));
272
273 if (!tipc_msg_reverse(onode, &skb, err))
274 return;
275
276 dnode = msg_destnode(buf_msg(skb));
277 selector = msg_origport(buf_msg(skb));
278 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
279}
280
b97bf3fd 281/**
2e84c60b 282 * tsk_rej_rx_queue - reject all buffers in socket receive queue
0c3141e9
AS
283 *
284 * Caller must hold socket lock
b97bf3fd 285 */
2e84c60b 286static void tsk_rej_rx_queue(struct sock *sk)
b97bf3fd 287{
a6ca1094 288 struct sk_buff *skb;
0c3141e9 289
bcd3ffd4
JPM
290 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
291 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
b97bf3fd
PL
292}
293
d6fb7e9c
PB
294static bool tipc_sk_connected(struct sock *sk)
295{
296 return sk->sk_socket->state == SS_CONNECTED;
297}
298
2e84c60b 299/* tsk_peer_msg - verify if message was sent by connected port's peer
0fc87aae
JPM
300 *
301 * Handles cases where the node's network address has changed from
302 * the default of <0.0.0> to its configured setting.
303 */
2e84c60b 304static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
0fc87aae 305{
d6fb7e9c
PB
306 struct sock *sk = &tsk->sk;
307 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
301bae56 308 u32 peer_port = tsk_peer_port(tsk);
0fc87aae
JPM
309 u32 orig_node;
310 u32 peer_node;
311
d6fb7e9c 312 if (unlikely(!tipc_sk_connected(sk)))
0fc87aae
JPM
313 return false;
314
315 if (unlikely(msg_origport(msg) != peer_port))
316 return false;
317
318 orig_node = msg_orignode(msg);
301bae56 319 peer_node = tsk_peer_node(tsk);
0fc87aae
JPM
320
321 if (likely(orig_node == peer_node))
322 return true;
323
34747539 324 if (!orig_node && (peer_node == tn->own_addr))
0fc87aae
JPM
325 return true;
326
34747539 327 if (!peer_node && (orig_node == tn->own_addr))
0fc87aae
JPM
328 return true;
329
330 return false;
331}
332
b97bf3fd 333/**
c5fa7b3c 334 * tipc_sk_create - create a TIPC socket
0c3141e9 335 * @net: network namespace (must be default network)
b97bf3fd
PL
336 * @sock: pre-allocated socket structure
337 * @protocol: protocol indicator (must be 0)
3f378b68 338 * @kern: caused by kernel or by userspace?
c4307285 339 *
0c3141e9
AS
340 * This routine creates additional data structures used by the TIPC socket,
341 * initializes them, and links them together.
b97bf3fd
PL
342 *
343 * Returns 0 on success, errno otherwise
344 */
58ed9442
JPM
345static int tipc_sk_create(struct net *net, struct socket *sock,
346 int protocol, int kern)
b97bf3fd 347{
c5898636 348 struct tipc_net *tn;
0c3141e9
AS
349 const struct proto_ops *ops;
350 socket_state state;
b97bf3fd 351 struct sock *sk;
58ed9442 352 struct tipc_sock *tsk;
5b8fa7ce 353 struct tipc_msg *msg;
0c3141e9
AS
354
355 /* Validate arguments */
b97bf3fd
PL
356 if (unlikely(protocol != 0))
357 return -EPROTONOSUPPORT;
358
b97bf3fd
PL
359 switch (sock->type) {
360 case SOCK_STREAM:
0c3141e9
AS
361 ops = &stream_ops;
362 state = SS_UNCONNECTED;
b97bf3fd
PL
363 break;
364 case SOCK_SEQPACKET:
0c3141e9
AS
365 ops = &packet_ops;
366 state = SS_UNCONNECTED;
b97bf3fd
PL
367 break;
368 case SOCK_DGRAM:
b97bf3fd 369 case SOCK_RDM:
0c3141e9
AS
370 ops = &msg_ops;
371 state = SS_READY;
b97bf3fd 372 break;
49978651 373 default:
49978651 374 return -EPROTOTYPE;
b97bf3fd
PL
375 }
376
0c3141e9 377 /* Allocate socket's protocol area */
11aa9c28 378 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
0c3141e9 379 if (sk == NULL)
b97bf3fd 380 return -ENOMEM;
b97bf3fd 381
58ed9442 382 tsk = tipc_sk(sk);
301bae56 383 tsk->max_pkt = MAX_PKT_DEFAULT;
301bae56
JPM
384 INIT_LIST_HEAD(&tsk->publications);
385 msg = &tsk->phdr;
c5898636
JPM
386 tn = net_generic(sock_net(sk), tipc_net_id);
387 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
5b8fa7ce 388 NAMED_H_SIZE, 0);
b97bf3fd 389
0c3141e9 390 /* Finish initializing socket data structures */
0c3141e9
AS
391 sock->ops = ops;
392 sock->state = state;
0c3141e9 393 sock_init_data(sock, sk);
07f6c4bc 394 if (tipc_sk_insert(tsk)) {
c19ca6cb 395 pr_warn("Socket create failed; port number exhausted\n");
07f6c4bc
YX
396 return -EINVAL;
397 }
398 msg_set_origport(msg, tsk->portid);
3721e9c7 399 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
4f4482dc 400 sk->sk_backlog_rcv = tipc_backlog_rcv;
cc79dd1b 401 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
f288bef4
YX
402 sk->sk_data_ready = tipc_data_ready;
403 sk->sk_write_space = tipc_write_space;
f4195d1e 404 sk->sk_destruct = tipc_sock_destruct;
4f4482dc
JPM
405 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
406 atomic_set(&tsk->dupl_rcvcnt, 0);
7ef43eba 407
10724cc7
JPM
408 /* Start out with safe limits until we receive an advertised window */
409 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
410 tsk->rcv_win = tsk->snd_win;
411
0c3141e9 412 if (sock->state == SS_READY) {
301bae56 413 tsk_set_unreturnable(tsk, true);
0c3141e9 414 if (sock->type == SOCK_DGRAM)
301bae56 415 tsk_set_unreliable(tsk, true);
0c3141e9 416 }
b97bf3fd
PL
417 return 0;
418}
419
07f6c4bc
YX
420static void tipc_sk_callback(struct rcu_head *head)
421{
422 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
423
424 sock_put(&tsk->sk);
425}
426
b97bf3fd 427/**
247f0f3c 428 * tipc_release - destroy a TIPC socket
b97bf3fd
PL
429 * @sock: socket to destroy
430 *
431 * This routine cleans up any messages that are still queued on the socket.
432 * For DGRAM and RDM socket types, all queued messages are rejected.
433 * For SEQPACKET and STREAM socket types, the first message is rejected
434 * and any others are discarded. (If the first message on a STREAM socket
435 * is partially-read, it is discarded and the next one is rejected instead.)
c4307285 436 *
b97bf3fd
PL
437 * NOTE: Rejected messages are not necessarily returned to the sender! They
438 * are returned or discarded according to the "destination droppable" setting
439 * specified for the message by the sender.
440 *
441 * Returns 0 on success, errno otherwise
442 */
247f0f3c 443static int tipc_release(struct socket *sock)
b97bf3fd 444{
b97bf3fd 445 struct sock *sk = sock->sk;
357c4774 446 struct net *net;
58ed9442 447 struct tipc_sock *tsk;
a6ca1094 448 struct sk_buff *skb;
1ea23a21 449 u32 dnode;
b97bf3fd 450
0c3141e9
AS
451 /*
452 * Exit if socket isn't fully initialized (occurs when a failed accept()
453 * releases a pre-allocated child socket that was never used)
454 */
0c3141e9 455 if (sk == NULL)
b97bf3fd 456 return 0;
c4307285 457
357c4774 458 net = sock_net(sk);
58ed9442 459 tsk = tipc_sk(sk);
0c3141e9
AS
460 lock_sock(sk);
461
462 /*
463 * Reject all unreceived messages, except on an active connection
464 * (which disconnects locally & sends a 'FIN+' to peer)
465 */
301bae56 466 dnode = tsk_peer_node(tsk);
b97bf3fd 467 while (sock->state != SS_DISCONNECTING) {
a6ca1094
YX
468 skb = __skb_dequeue(&sk->sk_receive_queue);
469 if (skb == NULL)
b97bf3fd 470 break;
ba8aebe9 471 if (TIPC_SKB_CB(skb)->bytes_read)
a6ca1094 472 kfree_skb(skb);
0c3141e9
AS
473 else {
474 if ((sock->state == SS_CONNECTING) ||
475 (sock->state == SS_CONNECTED)) {
476 sock->state = SS_DISCONNECTING;
f2f9800d 477 tipc_node_remove_conn(net, dnode, tsk->portid);
0c3141e9 478 }
bcd3ffd4 479 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
0c3141e9 480 }
b97bf3fd
PL
481 }
482
301bae56 483 tipc_sk_withdraw(tsk, 0, NULL);
1ea23a21 484 sk_stop_timer(sk, &sk->sk_timer);
07f6c4bc 485 tipc_sk_remove(tsk);
d6fb7e9c 486 if (tipc_sk_connected(sk)) {
c5898636 487 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
34747539 488 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
c5898636 489 tsk_own_node(tsk), tsk_peer_port(tsk),
07f6c4bc 490 tsk->portid, TIPC_ERR_NO_PORT);
a6ca1094 491 if (skb)
af9b028e 492 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
f2f9800d 493 tipc_node_remove_conn(net, dnode, tsk->portid);
5b8fa7ce 494 }
b97bf3fd 495
0c3141e9 496 /* Reject any messages that accumulated in backlog queue */
0c3141e9
AS
497 sock->state = SS_DISCONNECTING;
498 release_sock(sk);
07f6c4bc
YX
499
500 call_rcu(&tsk->rcu, tipc_sk_callback);
0c3141e9 501 sock->sk = NULL;
b97bf3fd 502
065d7e39 503 return 0;
b97bf3fd
PL
504}
505
506/**
247f0f3c 507 * tipc_bind - associate or disassocate TIPC name(s) with a socket
b97bf3fd
PL
508 * @sock: socket structure
509 * @uaddr: socket address describing name(s) and desired operation
510 * @uaddr_len: size of socket address data structure
c4307285 511 *
b97bf3fd
PL
512 * Name and name sequence binding is indicated using a positive scope value;
513 * a negative scope value unbinds the specified name. Specifying no name
514 * (i.e. a socket address length of 0) unbinds all names from the socket.
c4307285 515 *
b97bf3fd 516 * Returns 0 on success, errno otherwise
0c3141e9
AS
517 *
518 * NOTE: This routine doesn't need to take the socket lock since it doesn't
519 * access any non-constant socket information.
b97bf3fd 520 */
247f0f3c
YX
521static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
522 int uaddr_len)
b97bf3fd 523{
84602761 524 struct sock *sk = sock->sk;
b97bf3fd 525 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
58ed9442 526 struct tipc_sock *tsk = tipc_sk(sk);
84602761 527 int res = -EINVAL;
b97bf3fd 528
84602761
YX
529 lock_sock(sk);
530 if (unlikely(!uaddr_len)) {
301bae56 531 res = tipc_sk_withdraw(tsk, 0, NULL);
84602761
YX
532 goto exit;
533 }
c4307285 534
84602761
YX
535 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
536 res = -EINVAL;
537 goto exit;
538 }
539 if (addr->family != AF_TIPC) {
540 res = -EAFNOSUPPORT;
541 goto exit;
542 }
b97bf3fd 543
b97bf3fd
PL
544 if (addr->addrtype == TIPC_ADDR_NAME)
545 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
84602761
YX
546 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
547 res = -EAFNOSUPPORT;
548 goto exit;
549 }
c4307285 550
13a2e898 551 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
7d0ab17b 552 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
84602761
YX
553 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
554 res = -EACCES;
555 goto exit;
556 }
c422f1bd 557
84602761 558 res = (addr->scope > 0) ?
301bae56
JPM
559 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
560 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
84602761
YX
561exit:
562 release_sock(sk);
563 return res;
b97bf3fd
PL
564}
565
c4307285 566/**
247f0f3c 567 * tipc_getname - get port ID of socket or peer socket
b97bf3fd
PL
568 * @sock: socket structure
569 * @uaddr: area for returned socket address
570 * @uaddr_len: area for returned length of socket address
2da59918 571 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
c4307285 572 *
b97bf3fd 573 * Returns 0 on success, errno otherwise
0c3141e9 574 *
2da59918
AS
575 * NOTE: This routine doesn't need to take the socket lock since it only
576 * accesses socket information that is unchanging (or which changes in
0e65967e 577 * a completely predictable manner).
b97bf3fd 578 */
247f0f3c
YX
579static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
580 int *uaddr_len, int peer)
b97bf3fd 581{
b97bf3fd 582 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
58ed9442 583 struct tipc_sock *tsk = tipc_sk(sock->sk);
34747539 584 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
b97bf3fd 585
88f8a5e3 586 memset(addr, 0, sizeof(*addr));
0c3141e9 587 if (peer) {
2da59918
AS
588 if ((sock->state != SS_CONNECTED) &&
589 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
590 return -ENOTCONN;
301bae56
JPM
591 addr->addr.id.ref = tsk_peer_port(tsk);
592 addr->addr.id.node = tsk_peer_node(tsk);
0c3141e9 593 } else {
07f6c4bc 594 addr->addr.id.ref = tsk->portid;
34747539 595 addr->addr.id.node = tn->own_addr;
0c3141e9 596 }
b97bf3fd
PL
597
598 *uaddr_len = sizeof(*addr);
599 addr->addrtype = TIPC_ADDR_ID;
600 addr->family = AF_TIPC;
601 addr->scope = 0;
b97bf3fd
PL
602 addr->addr.name.domain = 0;
603
0c3141e9 604 return 0;
b97bf3fd
PL
605}
606
607/**
247f0f3c 608 * tipc_poll - read and possibly block on pollmask
b97bf3fd
PL
609 * @file: file structure associated with the socket
610 * @sock: socket for which to calculate the poll bits
611 * @wait: ???
612 *
9b674e82
AS
613 * Returns pollmask value
614 *
615 * COMMENTARY:
616 * It appears that the usual socket locking mechanisms are not useful here
617 * since the pollmask info is potentially out-of-date the moment this routine
618 * exits. TCP and other protocols seem to rely on higher level poll routines
619 * to handle any preventable race conditions, so TIPC will do the same ...
620 *
621 * TIPC sets the returned events as follows:
f662c070
AS
622 *
623 * socket state flags set
624 * ------------ ---------
625 * unconnected no read flags
c4fc298a 626 * POLLOUT if port is not congested
f662c070
AS
627 *
628 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
629 * no write flags
630 *
631 * connected POLLIN/POLLRDNORM if data in rx queue
632 * POLLOUT if port is not congested
633 *
634 * disconnecting POLLIN/POLLRDNORM/POLLHUP
635 * no write flags
636 *
637 * listening POLLIN if SYN in rx queue
638 * no write flags
639 *
640 * ready POLLIN/POLLRDNORM if data in rx queue
641 * [connectionless] POLLOUT (since port cannot be congested)
642 *
643 * IMPORTANT: The fact that a read or write operation is indicated does NOT
644 * imply that the operation will succeed, merely that it should be performed
645 * and will not block.
b97bf3fd 646 */
247f0f3c
YX
647static unsigned int tipc_poll(struct file *file, struct socket *sock,
648 poll_table *wait)
b97bf3fd 649{
9b674e82 650 struct sock *sk = sock->sk;
58ed9442 651 struct tipc_sock *tsk = tipc_sk(sk);
f662c070 652 u32 mask = 0;
9b674e82 653
f288bef4 654 sock_poll_wait(file, sk_sleep(sk), wait);
9b674e82 655
f662c070 656 switch ((int)sock->state) {
c4fc298a 657 case SS_UNCONNECTED:
60120526 658 if (!tsk->link_cong)
c4fc298a
EH
659 mask |= POLLOUT;
660 break;
f662c070
AS
661 case SS_READY:
662 case SS_CONNECTED:
301bae56 663 if (!tsk->link_cong && !tsk_conn_cong(tsk))
f662c070
AS
664 mask |= POLLOUT;
665 /* fall thru' */
666 case SS_CONNECTING:
667 case SS_LISTENING:
668 if (!skb_queue_empty(&sk->sk_receive_queue))
669 mask |= (POLLIN | POLLRDNORM);
670 break;
671 case SS_DISCONNECTING:
672 mask = (POLLIN | POLLRDNORM | POLLHUP);
673 break;
674 }
9b674e82
AS
675
676 return mask;
b97bf3fd
PL
677}
678
0abd8ff2
JPM
679/**
680 * tipc_sendmcast - send multicast message
681 * @sock: socket structure
682 * @seq: destination address
562640f3 683 * @msg: message to send
0abd8ff2
JPM
684 * @dsz: total length of message data
685 * @timeo: timeout to wait for wakeup
686 *
687 * Called from function tipc_sendmsg(), which has done all sanity checks
688 * Returns the number of bytes sent on success, or errno
689 */
690static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
562640f3 691 struct msghdr *msg, size_t dsz, long timeo)
0abd8ff2
JPM
692{
693 struct sock *sk = sock->sk;
c5898636 694 struct tipc_sock *tsk = tipc_sk(sk);
f2f9800d 695 struct net *net = sock_net(sk);
c5898636 696 struct tipc_msg *mhdr = &tsk->phdr;
f214fc40 697 struct sk_buff_head pktchain;
f25dcc76 698 struct iov_iter save = msg->msg_iter;
0abd8ff2
JPM
699 uint mtu;
700 int rc;
701
7cf87fa2
PB
702 if (!timeo && tsk->link_cong)
703 return -ELINKCONG;
704
0abd8ff2
JPM
705 msg_set_type(mhdr, TIPC_MCAST_MSG);
706 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
707 msg_set_destport(mhdr, 0);
708 msg_set_destnode(mhdr, 0);
709 msg_set_nametype(mhdr, seq->type);
710 msg_set_namelower(mhdr, seq->lower);
711 msg_set_nameupper(mhdr, seq->upper);
712 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
713
f214fc40
PB
714 skb_queue_head_init(&pktchain);
715
0abd8ff2 716new_mtu:
959e1781 717 mtu = tipc_bcast_get_mtu(net);
f214fc40 718 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &pktchain);
0abd8ff2
JPM
719 if (unlikely(rc < 0))
720 return rc;
721
722 do {
f214fc40 723 rc = tipc_bcast_xmit(net, &pktchain);
22d85c79
JPM
724 if (likely(!rc))
725 return dsz;
726
727 if (rc == -ELINKCONG) {
728 tsk->link_cong = 1;
729 rc = tipc_wait_for_sndmsg(sock, &timeo);
730 if (!rc)
731 continue;
0abd8ff2 732 }
f214fc40 733 __skb_queue_purge(&pktchain);
f25dcc76
AV
734 if (rc == -EMSGSIZE) {
735 msg->msg_iter = save;
0abd8ff2 736 goto new_mtu;
f25dcc76 737 }
22d85c79
JPM
738 break;
739 } while (1);
0abd8ff2
JPM
740 return rc;
741}
742
cb1b7280
JPM
743/**
744 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
745 * @arrvq: queue with arriving messages, to be cloned after destination lookup
746 * @inputq: queue with cloned messages, delivered to socket after dest lookup
747 *
748 * Multi-threaded: parallel calls with reference to same queues may occur
078bec82 749 */
cb1b7280
JPM
750void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
751 struct sk_buff_head *inputq)
078bec82 752{
cb1b7280 753 struct tipc_msg *msg;
3c724acd 754 struct tipc_plist dports;
3c724acd 755 u32 portid;
078bec82 756 u32 scope = TIPC_CLUSTER_SCOPE;
cb1b7280
JPM
757 struct sk_buff_head tmpq;
758 uint hsz;
759 struct sk_buff *skb, *_skb;
3c724acd 760
cb1b7280 761 __skb_queue_head_init(&tmpq);
3c724acd 762 tipc_plist_init(&dports);
078bec82 763
cb1b7280
JPM
764 skb = tipc_skb_peek(arrvq, &inputq->lock);
765 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
766 msg = buf_msg(skb);
767 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
768
769 if (in_own_node(net, msg_orignode(msg)))
770 scope = TIPC_NODE_SCOPE;
771
772 /* Create destination port list and message clones: */
773 tipc_nametbl_mc_translate(net,
774 msg_nametype(msg), msg_namelower(msg),
775 msg_nameupper(msg), scope, &dports);
776 portid = tipc_plist_pop(&dports);
777 for (; portid; portid = tipc_plist_pop(&dports)) {
778 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
779 if (_skb) {
780 msg_set_destport(buf_msg(_skb), portid);
781 __skb_queue_tail(&tmpq, _skb);
782 continue;
783 }
784 pr_warn("Failed to clone mcast rcv buffer\n");
078bec82 785 }
cb1b7280
JPM
786 /* Append to inputq if not already done by other thread */
787 spin_lock_bh(&inputq->lock);
788 if (skb_peek(arrvq) == skb) {
789 skb_queue_splice_tail_init(&tmpq, inputq);
790 kfree_skb(__skb_dequeue(arrvq));
791 }
792 spin_unlock_bh(&inputq->lock);
793 __skb_queue_purge(&tmpq);
794 kfree_skb(skb);
078bec82 795 }
cb1b7280 796 tipc_sk_rcv(net, inputq);
078bec82
JPM
797}
798
ac0074ee
JPM
799/**
800 * tipc_sk_proto_rcv - receive a connection mng protocol message
801 * @tsk: receiving socket
bcd3ffd4 802 * @skb: pointer to message buffer.
ac0074ee 803 */
f1d048f2
JPM
804static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
805 struct sk_buff_head *xmitq)
ac0074ee 806{
bcd3ffd4 807 struct sock *sk = &tsk->sk;
f1d048f2 808 u32 onode = tsk_own_node(tsk);
bcd3ffd4
JPM
809 struct tipc_msg *hdr = buf_msg(skb);
810 int mtyp = msg_type(hdr);
10724cc7 811 bool conn_cong;
bcd3ffd4 812
ac0074ee 813 /* Ignore if connection cannot be validated: */
bcd3ffd4 814 if (!tsk_peer_msg(tsk, hdr))
ac0074ee
JPM
815 goto exit;
816
301bae56 817 tsk->probing_state = TIPC_CONN_OK;
ac0074ee 818
bcd3ffd4
JPM
819 if (mtyp == CONN_PROBE) {
820 msg_set_type(hdr, CONN_PROBE_REPLY);
f1d048f2
JPM
821 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
822 __skb_queue_tail(xmitq, skb);
bcd3ffd4
JPM
823 return;
824 } else if (mtyp == CONN_ACK) {
301bae56 825 conn_cong = tsk_conn_cong(tsk);
10724cc7
JPM
826 tsk->snt_unacked -= msg_conn_ack(hdr);
827 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
828 tsk->snd_win = msg_adv_win(hdr);
60120526 829 if (conn_cong)
bcd3ffd4
JPM
830 sk->sk_write_space(sk);
831 } else if (mtyp != CONN_PROBE_REPLY) {
832 pr_warn("Received unknown CONN_PROTO msg\n");
ac0074ee 833 }
ac0074ee 834exit:
bcd3ffd4 835 kfree_skb(skb);
ac0074ee
JPM
836}
837
3f40504f
YX
838static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
839{
840 struct sock *sk = sock->sk;
58ed9442 841 struct tipc_sock *tsk = tipc_sk(sk);
3f40504f
YX
842 DEFINE_WAIT(wait);
843 int done;
844
845 do {
846 int err = sock_error(sk);
847 if (err)
848 return err;
849 if (sock->state == SS_DISCONNECTING)
850 return -EPIPE;
851 if (!*timeo_p)
852 return -EAGAIN;
853 if (signal_pending(current))
854 return sock_intr_errno(*timeo_p);
855
856 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
60120526 857 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
3f40504f
YX
858 finish_wait(sk_sleep(sk), &wait);
859 } while (!done);
860 return 0;
861}
862
b97bf3fd 863/**
247f0f3c 864 * tipc_sendmsg - send message in connectionless manner
b97bf3fd
PL
865 * @sock: socket structure
866 * @m: message to send
e2dafe87 867 * @dsz: amount of user data to be sent
c4307285 868 *
b97bf3fd 869 * Message must have an destination specified explicitly.
c4307285 870 * Used for SOCK_RDM and SOCK_DGRAM messages,
b97bf3fd
PL
871 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
872 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
c4307285 873 *
b97bf3fd
PL
874 * Returns the number of bytes sent on success, or errno otherwise
875 */
1b784140 876static int tipc_sendmsg(struct socket *sock,
e2dafe87 877 struct msghdr *m, size_t dsz)
39a0295f
YX
878{
879 struct sock *sk = sock->sk;
880 int ret;
881
882 lock_sock(sk);
883 ret = __tipc_sendmsg(sock, m, dsz);
884 release_sock(sk);
885
886 return ret;
887}
888
889static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
b97bf3fd 890{
e2dafe87 891 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
0c3141e9 892 struct sock *sk = sock->sk;
58ed9442 893 struct tipc_sock *tsk = tipc_sk(sk);
f2f9800d 894 struct net *net = sock_net(sk);
301bae56 895 struct tipc_msg *mhdr = &tsk->phdr;
e2dafe87 896 u32 dnode, dport;
f214fc40 897 struct sk_buff_head pktchain;
a6ca1094 898 struct sk_buff *skb;
f2f8036e 899 struct tipc_name_seq *seq;
f25dcc76 900 struct iov_iter save;
e2dafe87 901 u32 mtu;
3f40504f 902 long timeo;
88b17b6a 903 int rc;
b97bf3fd 904
e2dafe87 905 if (dsz > TIPC_MAX_USER_MSG_SIZE)
c29c3f70 906 return -EMSGSIZE;
f2f8036e 907 if (unlikely(!dest)) {
87227fe7 908 if (sock->state == SS_READY && tsk->peer.family == AF_TIPC)
aeda16b6 909 dest = &tsk->peer;
f2f8036e
EH
910 else
911 return -EDESTADDRREQ;
912 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
913 dest->family != AF_TIPC) {
914 return -EINVAL;
915 }
e2dafe87 916 if (unlikely(sock->state != SS_READY)) {
39a0295f
YX
917 if (sock->state == SS_LISTENING)
918 return -EPIPE;
919 if (sock->state != SS_UNCONNECTED)
920 return -EISCONN;
921 if (tsk->published)
922 return -EOPNOTSUPP;
3388007b 923 if (dest->addrtype == TIPC_ADDR_NAME) {
301bae56
JPM
924 tsk->conn_type = dest->addr.name.name.type;
925 tsk->conn_instance = dest->addr.name.name.instance;
3388007b 926 }
b97bf3fd 927 }
f2f8036e 928 seq = &dest->addr.nameseq;
3f40504f 929 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
e2dafe87
JPM
930
931 if (dest->addrtype == TIPC_ADDR_MCAST) {
39a0295f 932 return tipc_sendmcast(sock, seq, m, dsz, timeo);
e2dafe87
JPM
933 } else if (dest->addrtype == TIPC_ADDR_NAME) {
934 u32 type = dest->addr.name.name.type;
935 u32 inst = dest->addr.name.name.instance;
936 u32 domain = dest->addr.name.domain;
937
938 dnode = domain;
939 msg_set_type(mhdr, TIPC_NAMED_MSG);
940 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
941 msg_set_nametype(mhdr, type);
942 msg_set_nameinst(mhdr, inst);
943 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
4ac1c8d0 944 dport = tipc_nametbl_translate(net, type, inst, &dnode);
e2dafe87
JPM
945 msg_set_destnode(mhdr, dnode);
946 msg_set_destport(mhdr, dport);
39a0295f
YX
947 if (unlikely(!dport && !dnode))
948 return -EHOSTUNREACH;
e2dafe87
JPM
949 } else if (dest->addrtype == TIPC_ADDR_ID) {
950 dnode = dest->addr.id.node;
951 msg_set_type(mhdr, TIPC_DIRECT_MSG);
952 msg_set_lookup_scope(mhdr, 0);
953 msg_set_destnode(mhdr, dnode);
954 msg_set_destport(mhdr, dest->addr.id.ref);
955 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
956 }
957
f214fc40 958 skb_queue_head_init(&pktchain);
f25dcc76 959 save = m->msg_iter;
e2dafe87 960new_mtu:
f2f9800d 961 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
f214fc40 962 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &pktchain);
e2dafe87 963 if (rc < 0)
39a0295f 964 return rc;
e2dafe87
JPM
965
966 do {
f214fc40 967 skb = skb_peek(&pktchain);
a6ca1094 968 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
f214fc40 969 rc = tipc_node_xmit(net, &pktchain, dnode, tsk->portid);
22d85c79 970 if (likely(!rc)) {
e2dafe87 971 if (sock->state != SS_READY)
0c3141e9 972 sock->state = SS_CONNECTING;
22d85c79 973 return dsz;
c4307285 974 }
22d85c79
JPM
975 if (rc == -ELINKCONG) {
976 tsk->link_cong = 1;
977 rc = tipc_wait_for_sndmsg(sock, &timeo);
978 if (!rc)
979 continue;
980 }
f214fc40 981 __skb_queue_purge(&pktchain);
f25dcc76
AV
982 if (rc == -EMSGSIZE) {
983 m->msg_iter = save;
e2dafe87 984 goto new_mtu;
f25dcc76 985 }
22d85c79
JPM
986 break;
987 } while (1);
e2dafe87
JPM
988
989 return rc;
b97bf3fd
PL
990}
991
391a6dd1
YX
992static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
993{
994 struct sock *sk = sock->sk;
58ed9442 995 struct tipc_sock *tsk = tipc_sk(sk);
391a6dd1
YX
996 DEFINE_WAIT(wait);
997 int done;
998
999 do {
1000 int err = sock_error(sk);
1001 if (err)
1002 return err;
1003 if (sock->state == SS_DISCONNECTING)
1004 return -EPIPE;
1005 else if (sock->state != SS_CONNECTED)
1006 return -ENOTCONN;
1007 if (!*timeo_p)
1008 return -EAGAIN;
1009 if (signal_pending(current))
1010 return sock_intr_errno(*timeo_p);
1011
1012 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1013 done = sk_wait_event(sk, timeo_p,
60120526 1014 (!tsk->link_cong &&
301bae56 1015 !tsk_conn_cong(tsk)) ||
d6fb7e9c 1016 !tipc_sk_connected(sk));
391a6dd1
YX
1017 finish_wait(sk_sleep(sk), &wait);
1018 } while (!done);
1019 return 0;
1020}
1021
c4307285 1022/**
4ccfe5e0 1023 * tipc_send_stream - send stream-oriented data
b97bf3fd 1024 * @sock: socket structure
4ccfe5e0
JPM
1025 * @m: data to send
1026 * @dsz: total length of data to be transmitted
c4307285 1027 *
4ccfe5e0 1028 * Used for SOCK_STREAM data.
c4307285 1029 *
4ccfe5e0
JPM
1030 * Returns the number of bytes sent on success (or partial success),
1031 * or errno if no data sent
b97bf3fd 1032 */
1b784140 1033static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
39a0295f
YX
1034{
1035 struct sock *sk = sock->sk;
1036 int ret;
1037
1038 lock_sock(sk);
1039 ret = __tipc_send_stream(sock, m, dsz);
1040 release_sock(sk);
1041
1042 return ret;
1043}
1044
1045static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
b97bf3fd 1046{
0c3141e9 1047 struct sock *sk = sock->sk;
f2f9800d 1048 struct net *net = sock_net(sk);
58ed9442 1049 struct tipc_sock *tsk = tipc_sk(sk);
301bae56 1050 struct tipc_msg *mhdr = &tsk->phdr;
f214fc40 1051 struct sk_buff_head pktchain;
342dfc30 1052 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
07f6c4bc 1053 u32 portid = tsk->portid;
4ccfe5e0 1054 int rc = -EINVAL;
391a6dd1 1055 long timeo;
4ccfe5e0
JPM
1056 u32 dnode;
1057 uint mtu, send, sent = 0;
f25dcc76 1058 struct iov_iter save;
10724cc7 1059 int hlen = MIN_H_SIZE;
b97bf3fd
PL
1060
1061 /* Handle implied connection establishment */
4ccfe5e0 1062 if (unlikely(dest)) {
39a0295f 1063 rc = __tipc_sendmsg(sock, m, dsz);
10724cc7 1064 hlen = msg_hdr_sz(mhdr);
4ccfe5e0 1065 if (dsz && (dsz == rc))
10724cc7 1066 tsk->snt_unacked = tsk_inc(tsk, dsz + hlen);
4ccfe5e0
JPM
1067 return rc;
1068 }
1069 if (dsz > (uint)INT_MAX)
c29c3f70
AS
1070 return -EMSGSIZE;
1071
391a6dd1
YX
1072 if (unlikely(sock->state != SS_CONNECTED)) {
1073 if (sock->state == SS_DISCONNECTING)
39a0295f 1074 return -EPIPE;
391a6dd1 1075 else
39a0295f 1076 return -ENOTCONN;
391a6dd1 1077 }
1d835874 1078
391a6dd1 1079 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
7cf87fa2
PB
1080 if (!timeo && tsk->link_cong)
1081 return -ELINKCONG;
1082
301bae56 1083 dnode = tsk_peer_node(tsk);
f214fc40 1084 skb_queue_head_init(&pktchain);
4ccfe5e0
JPM
1085
1086next:
f25dcc76 1087 save = m->msg_iter;
301bae56 1088 mtu = tsk->max_pkt;
4ccfe5e0 1089 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
f214fc40 1090 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &pktchain);
4ccfe5e0 1091 if (unlikely(rc < 0))
39a0295f 1092 return rc;
f214fc40 1093
c4307285 1094 do {
301bae56 1095 if (likely(!tsk_conn_cong(tsk))) {
f214fc40 1096 rc = tipc_node_xmit(net, &pktchain, dnode, portid);
4ccfe5e0 1097 if (likely(!rc)) {
10724cc7 1098 tsk->snt_unacked += tsk_inc(tsk, send + hlen);
4ccfe5e0
JPM
1099 sent += send;
1100 if (sent == dsz)
22d85c79 1101 return dsz;
4ccfe5e0
JPM
1102 goto next;
1103 }
1104 if (rc == -EMSGSIZE) {
f214fc40 1105 __skb_queue_purge(&pktchain);
f2f9800d
YX
1106 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1107 portid);
f25dcc76 1108 m->msg_iter = save;
4ccfe5e0
JPM
1109 goto next;
1110 }
1111 if (rc != -ELINKCONG)
1112 break;
22d85c79 1113
50100a5e 1114 tsk->link_cong = 1;
4ccfe5e0
JPM
1115 }
1116 rc = tipc_wait_for_sndpkt(sock, &timeo);
1117 } while (!rc);
39a0295f 1118
f214fc40 1119 __skb_queue_purge(&pktchain);
4ccfe5e0 1120 return sent ? sent : rc;
b97bf3fd
PL
1121}
1122
c4307285 1123/**
4ccfe5e0 1124 * tipc_send_packet - send a connection-oriented message
b97bf3fd 1125 * @sock: socket structure
4ccfe5e0
JPM
1126 * @m: message to send
1127 * @dsz: length of data to be transmitted
c4307285 1128 *
4ccfe5e0 1129 * Used for SOCK_SEQPACKET messages.
c4307285 1130 *
4ccfe5e0 1131 * Returns the number of bytes sent on success, or errno otherwise
b97bf3fd 1132 */
1b784140 1133static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
b97bf3fd 1134{
4ccfe5e0
JPM
1135 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1136 return -EMSGSIZE;
b97bf3fd 1137
1b784140 1138 return tipc_send_stream(sock, m, dsz);
b97bf3fd
PL
1139}
1140
dadebc00 1141/* tipc_sk_finish_conn - complete the setup of a connection
b97bf3fd 1142 */
301bae56 1143static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
dadebc00 1144 u32 peer_node)
b97bf3fd 1145{
3721e9c7
YX
1146 struct sock *sk = &tsk->sk;
1147 struct net *net = sock_net(sk);
301bae56 1148 struct tipc_msg *msg = &tsk->phdr;
b97bf3fd 1149
dadebc00
JPM
1150 msg_set_destnode(msg, peer_node);
1151 msg_set_destport(msg, peer_port);
1152 msg_set_type(msg, TIPC_CONN_MSG);
1153 msg_set_lookup_scope(msg, 0);
1154 msg_set_hdr_sz(msg, SHORT_H_SIZE);
584d24b3 1155
2f55c437 1156 tsk->probing_intv = CONN_PROBING_INTERVAL;
301bae56 1157 tsk->probing_state = TIPC_CONN_OK;
3721e9c7 1158 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
f2f9800d
YX
1159 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1160 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
60020e18 1161 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
10724cc7
JPM
1162 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1163 return;
1164
1165 /* Fall back to message based flow control */
1166 tsk->rcv_win = FLOWCTL_MSG_WIN;
1167 tsk->snd_win = FLOWCTL_MSG_WIN;
b97bf3fd
PL
1168}
1169
1170/**
1171 * set_orig_addr - capture sender's address for received message
1172 * @m: descriptor for message info
1173 * @msg: received message header
c4307285 1174 *
b97bf3fd
PL
1175 * Note: Address is not captured if not requested by receiver.
1176 */
05790c64 1177static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
b97bf3fd 1178{
342dfc30 1179 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
b97bf3fd 1180
c4307285 1181 if (addr) {
b97bf3fd
PL
1182 addr->family = AF_TIPC;
1183 addr->addrtype = TIPC_ADDR_ID;
60085c3d 1184 memset(&addr->addr, 0, sizeof(addr->addr));
b97bf3fd
PL
1185 addr->addr.id.ref = msg_origport(msg);
1186 addr->addr.id.node = msg_orignode(msg);
0e65967e
AS
1187 addr->addr.name.domain = 0; /* could leave uninitialized */
1188 addr->scope = 0; /* could leave uninitialized */
b97bf3fd
PL
1189 m->msg_namelen = sizeof(struct sockaddr_tipc);
1190 }
1191}
1192
1193/**
301bae56 1194 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
b97bf3fd
PL
1195 * @m: descriptor for message info
1196 * @msg: received message header
301bae56 1197 * @tsk: TIPC port associated with message
c4307285 1198 *
b97bf3fd 1199 * Note: Ancillary data is not captured if not requested by receiver.
c4307285 1200 *
b97bf3fd
PL
1201 * Returns 0 if successful, otherwise errno
1202 */
301bae56
JPM
1203static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1204 struct tipc_sock *tsk)
b97bf3fd
PL
1205{
1206 u32 anc_data[3];
1207 u32 err;
1208 u32 dest_type;
3546c750 1209 int has_name;
b97bf3fd
PL
1210 int res;
1211
1212 if (likely(m->msg_controllen == 0))
1213 return 0;
1214
1215 /* Optionally capture errored message object(s) */
b97bf3fd
PL
1216 err = msg ? msg_errcode(msg) : 0;
1217 if (unlikely(err)) {
1218 anc_data[0] = err;
1219 anc_data[1] = msg_data_sz(msg);
2db9983a
AS
1220 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1221 if (res)
b97bf3fd 1222 return res;
2db9983a
AS
1223 if (anc_data[1]) {
1224 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1225 msg_data(msg));
1226 if (res)
1227 return res;
1228 }
b97bf3fd
PL
1229 }
1230
1231 /* Optionally capture message destination object */
b97bf3fd
PL
1232 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1233 switch (dest_type) {
1234 case TIPC_NAMED_MSG:
3546c750 1235 has_name = 1;
b97bf3fd
PL
1236 anc_data[0] = msg_nametype(msg);
1237 anc_data[1] = msg_namelower(msg);
1238 anc_data[2] = msg_namelower(msg);
1239 break;
1240 case TIPC_MCAST_MSG:
3546c750 1241 has_name = 1;
b97bf3fd
PL
1242 anc_data[0] = msg_nametype(msg);
1243 anc_data[1] = msg_namelower(msg);
1244 anc_data[2] = msg_nameupper(msg);
1245 break;
1246 case TIPC_CONN_MSG:
301bae56
JPM
1247 has_name = (tsk->conn_type != 0);
1248 anc_data[0] = tsk->conn_type;
1249 anc_data[1] = tsk->conn_instance;
1250 anc_data[2] = tsk->conn_instance;
b97bf3fd
PL
1251 break;
1252 default:
3546c750 1253 has_name = 0;
b97bf3fd 1254 }
2db9983a
AS
1255 if (has_name) {
1256 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1257 if (res)
1258 return res;
1259 }
b97bf3fd
PL
1260
1261 return 0;
1262}
1263
10724cc7 1264static void tipc_sk_send_ack(struct tipc_sock *tsk)
739f5e4e 1265{
d6fb7e9c
PB
1266 struct sock *sk = &tsk->sk;
1267 struct net *net = sock_net(sk);
a6ca1094 1268 struct sk_buff *skb = NULL;
739f5e4e 1269 struct tipc_msg *msg;
301bae56
JPM
1270 u32 peer_port = tsk_peer_port(tsk);
1271 u32 dnode = tsk_peer_node(tsk);
739f5e4e 1272
d6fb7e9c 1273 if (!tipc_sk_connected(sk))
739f5e4e 1274 return;
c5898636
JPM
1275 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1276 dnode, tsk_own_node(tsk), peer_port,
1277 tsk->portid, TIPC_OK);
a6ca1094 1278 if (!skb)
739f5e4e 1279 return;
a6ca1094 1280 msg = buf_msg(skb);
10724cc7
JPM
1281 msg_set_conn_ack(msg, tsk->rcv_unacked);
1282 tsk->rcv_unacked = 0;
1283
1284 /* Adjust to and advertize the correct window limit */
1285 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1286 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1287 msg_set_adv_win(msg, tsk->rcv_win);
1288 }
af9b028e 1289 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
739f5e4e
JPM
1290}
1291
85d3fc94 1292static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
9bbb4ecc
YX
1293{
1294 struct sock *sk = sock->sk;
1295 DEFINE_WAIT(wait);
85d3fc94 1296 long timeo = *timeop;
9bbb4ecc
YX
1297 int err;
1298
1299 for (;;) {
1300 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
fe8e4649 1301 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
9bbb4ecc
YX
1302 if (sock->state == SS_DISCONNECTING) {
1303 err = -ENOTCONN;
1304 break;
1305 }
1306 release_sock(sk);
1307 timeo = schedule_timeout(timeo);
1308 lock_sock(sk);
1309 }
1310 err = 0;
1311 if (!skb_queue_empty(&sk->sk_receive_queue))
1312 break;
9bbb4ecc
YX
1313 err = -EAGAIN;
1314 if (!timeo)
1315 break;
143fe22f
EH
1316 err = sock_intr_errno(timeo);
1317 if (signal_pending(current))
1318 break;
9bbb4ecc
YX
1319 }
1320 finish_wait(sk_sleep(sk), &wait);
85d3fc94 1321 *timeop = timeo;
9bbb4ecc
YX
1322 return err;
1323}
1324
c4307285 1325/**
247f0f3c 1326 * tipc_recvmsg - receive packet-oriented message
b97bf3fd
PL
1327 * @m: descriptor for message info
1328 * @buf_len: total size of user buffer area
1329 * @flags: receive flags
c4307285 1330 *
b97bf3fd
PL
1331 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1332 * If the complete message doesn't fit in user area, truncate it.
1333 *
1334 * Returns size of returned message data, errno otherwise
1335 */
1b784140
YX
1336static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1337 int flags)
b97bf3fd 1338{
0c3141e9 1339 struct sock *sk = sock->sk;
58ed9442 1340 struct tipc_sock *tsk = tipc_sk(sk);
b97bf3fd
PL
1341 struct sk_buff *buf;
1342 struct tipc_msg *msg;
9bbb4ecc 1343 long timeo;
b97bf3fd
PL
1344 unsigned int sz;
1345 u32 err;
10724cc7 1346 int res, hlen;
b97bf3fd 1347
0c3141e9 1348 /* Catch invalid receive requests */
b97bf3fd
PL
1349 if (unlikely(!buf_len))
1350 return -EINVAL;
1351
0c3141e9 1352 lock_sock(sk);
b97bf3fd 1353
0c3141e9
AS
1354 if (unlikely(sock->state == SS_UNCONNECTED)) {
1355 res = -ENOTCONN;
b97bf3fd
PL
1356 goto exit;
1357 }
1358
9bbb4ecc 1359 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
0c3141e9 1360restart:
b97bf3fd 1361
0c3141e9 1362 /* Look for a message in receive queue; wait if necessary */
85d3fc94 1363 res = tipc_wait_for_rcvmsg(sock, &timeo);
9bbb4ecc
YX
1364 if (res)
1365 goto exit;
b97bf3fd 1366
0c3141e9 1367 /* Look at first message in receive queue */
0c3141e9 1368 buf = skb_peek(&sk->sk_receive_queue);
b97bf3fd
PL
1369 msg = buf_msg(buf);
1370 sz = msg_data_sz(msg);
10724cc7 1371 hlen = msg_hdr_sz(msg);
b97bf3fd
PL
1372 err = msg_errcode(msg);
1373
b97bf3fd 1374 /* Discard an empty non-errored message & try again */
b97bf3fd 1375 if ((!sz) && (!err)) {
2e84c60b 1376 tsk_advance_rx_queue(sk);
b97bf3fd
PL
1377 goto restart;
1378 }
1379
1380 /* Capture sender's address (optional) */
b97bf3fd
PL
1381 set_orig_addr(m, msg);
1382
1383 /* Capture ancillary data (optional) */
301bae56 1384 res = tipc_sk_anc_data_recv(m, msg, tsk);
0c3141e9 1385 if (res)
b97bf3fd
PL
1386 goto exit;
1387
1388 /* Capture message data (if valid) & compute return value (always) */
b97bf3fd
PL
1389 if (!err) {
1390 if (unlikely(buf_len < sz)) {
1391 sz = buf_len;
1392 m->msg_flags |= MSG_TRUNC;
1393 }
10724cc7 1394 res = skb_copy_datagram_msg(buf, hlen, m, sz);
0232fd0a 1395 if (res)
b97bf3fd 1396 goto exit;
b97bf3fd
PL
1397 res = sz;
1398 } else {
1399 if ((sock->state == SS_READY) ||
1400 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1401 res = 0;
1402 else
1403 res = -ECONNRESET;
1404 }
1405
10724cc7
JPM
1406 if (unlikely(flags & MSG_PEEK))
1407 goto exit;
1408
1409 if (likely(sock->state != SS_READY)) {
1410 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1411 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1412 tipc_sk_send_ack(tsk);
c4307285 1413 }
10724cc7 1414 tsk_advance_rx_queue(sk);
b97bf3fd 1415exit:
0c3141e9 1416 release_sock(sk);
b97bf3fd
PL
1417 return res;
1418}
1419
c4307285 1420/**
247f0f3c 1421 * tipc_recv_stream - receive stream-oriented data
b97bf3fd
PL
1422 * @m: descriptor for message info
1423 * @buf_len: total size of user buffer area
1424 * @flags: receive flags
c4307285
YH
1425 *
1426 * Used for SOCK_STREAM messages only. If not enough data is available
b97bf3fd
PL
1427 * will optionally wait for more; never truncates data.
1428 *
1429 * Returns size of returned message data, errno otherwise
1430 */
1b784140
YX
1431static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1432 size_t buf_len, int flags)
b97bf3fd 1433{
0c3141e9 1434 struct sock *sk = sock->sk;
58ed9442 1435 struct tipc_sock *tsk = tipc_sk(sk);
b97bf3fd
PL
1436 struct sk_buff *buf;
1437 struct tipc_msg *msg;
9bbb4ecc 1438 long timeo;
b97bf3fd 1439 unsigned int sz;
ba8aebe9 1440 int target;
b97bf3fd 1441 int sz_copied = 0;
b97bf3fd 1442 u32 err;
10724cc7 1443 int res = 0, hlen;
b97bf3fd 1444
0c3141e9 1445 /* Catch invalid receive attempts */
b97bf3fd
PL
1446 if (unlikely(!buf_len))
1447 return -EINVAL;
1448
0c3141e9 1449 lock_sock(sk);
b97bf3fd 1450
9bbb4ecc 1451 if (unlikely(sock->state == SS_UNCONNECTED)) {
0c3141e9 1452 res = -ENOTCONN;
b97bf3fd
PL
1453 goto exit;
1454 }
1455
3720d40b 1456 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
9bbb4ecc 1457 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
b97bf3fd 1458
617d3c7a 1459restart:
0c3141e9 1460 /* Look for a message in receive queue; wait if necessary */
85d3fc94 1461 res = tipc_wait_for_rcvmsg(sock, &timeo);
9bbb4ecc
YX
1462 if (res)
1463 goto exit;
b97bf3fd 1464
0c3141e9 1465 /* Look at first message in receive queue */
0c3141e9 1466 buf = skb_peek(&sk->sk_receive_queue);
b97bf3fd
PL
1467 msg = buf_msg(buf);
1468 sz = msg_data_sz(msg);
10724cc7 1469 hlen = msg_hdr_sz(msg);
b97bf3fd
PL
1470 err = msg_errcode(msg);
1471
1472 /* Discard an empty non-errored message & try again */
b97bf3fd 1473 if ((!sz) && (!err)) {
2e84c60b 1474 tsk_advance_rx_queue(sk);
b97bf3fd
PL
1475 goto restart;
1476 }
1477
1478 /* Optionally capture sender's address & ancillary data of first msg */
b97bf3fd
PL
1479 if (sz_copied == 0) {
1480 set_orig_addr(m, msg);
301bae56 1481 res = tipc_sk_anc_data_recv(m, msg, tsk);
0c3141e9 1482 if (res)
b97bf3fd
PL
1483 goto exit;
1484 }
1485
1486 /* Capture message data (if valid) & compute return value (always) */
b97bf3fd 1487 if (!err) {
ba8aebe9
PB
1488 u32 offset = TIPC_SKB_CB(buf)->bytes_read;
1489 u32 needed;
1490 int sz_to_copy;
b97bf3fd 1491
0232fd0a 1492 sz -= offset;
b97bf3fd 1493 needed = (buf_len - sz_copied);
ba8aebe9 1494 sz_to_copy = min(sz, needed);
0232fd0a 1495
10724cc7 1496 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy);
0232fd0a 1497 if (res)
b97bf3fd 1498 goto exit;
0232fd0a 1499
b97bf3fd
PL
1500 sz_copied += sz_to_copy;
1501
1502 if (sz_to_copy < sz) {
1503 if (!(flags & MSG_PEEK))
ba8aebe9
PB
1504 TIPC_SKB_CB(buf)->bytes_read =
1505 offset + sz_to_copy;
b97bf3fd
PL
1506 goto exit;
1507 }
b97bf3fd
PL
1508 } else {
1509 if (sz_copied != 0)
1510 goto exit; /* can't add error msg to valid data */
1511
1512 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1513 res = 0;
1514 else
1515 res = -ECONNRESET;
1516 }
1517
10724cc7
JPM
1518 if (unlikely(flags & MSG_PEEK))
1519 goto exit;
1520
1521 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1522 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1523 tipc_sk_send_ack(tsk);
1524 tsk_advance_rx_queue(sk);
b97bf3fd
PL
1525
1526 /* Loop around if more data is required */
f64f9e71
JP
1527 if ((sz_copied < buf_len) && /* didn't get all requested data */
1528 (!skb_queue_empty(&sk->sk_receive_queue) ||
3720d40b 1529 (sz_copied < target)) && /* and more is ready or required */
f64f9e71 1530 (!err)) /* and haven't reached a FIN */
b97bf3fd
PL
1531 goto restart;
1532
1533exit:
0c3141e9 1534 release_sock(sk);
a3b0a5a9 1535 return sz_copied ? sz_copied : res;
b97bf3fd
PL
1536}
1537
f288bef4
YX
1538/**
1539 * tipc_write_space - wake up thread if port congestion is released
1540 * @sk: socket
1541 */
1542static void tipc_write_space(struct sock *sk)
1543{
1544 struct socket_wq *wq;
1545
1546 rcu_read_lock();
1547 wq = rcu_dereference(sk->sk_wq);
1ce0bf50 1548 if (skwq_has_sleeper(wq))
f288bef4
YX
1549 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1550 POLLWRNORM | POLLWRBAND);
1551 rcu_read_unlock();
1552}
1553
1554/**
1555 * tipc_data_ready - wake up threads to indicate messages have been received
1556 * @sk: socket
1557 * @len: the length of messages
1558 */
676d2369 1559static void tipc_data_ready(struct sock *sk)
f288bef4
YX
1560{
1561 struct socket_wq *wq;
1562
1563 rcu_read_lock();
1564 wq = rcu_dereference(sk->sk_wq);
1ce0bf50 1565 if (skwq_has_sleeper(wq))
f288bef4
YX
1566 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1567 POLLRDNORM | POLLRDBAND);
1568 rcu_read_unlock();
1569}
1570
f4195d1e
YX
1571static void tipc_sock_destruct(struct sock *sk)
1572{
1573 __skb_queue_purge(&sk->sk_receive_queue);
1574}
1575
7e6c131e
YX
1576/**
1577 * filter_connect - Handle all incoming messages for a connection-based socket
58ed9442 1578 * @tsk: TIPC socket
1186adf7 1579 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
7e6c131e 1580 *
cda3696d 1581 * Returns true if everything ok, false otherwise
7e6c131e 1582 */
cda3696d 1583static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
7e6c131e 1584{
58ed9442 1585 struct sock *sk = &tsk->sk;
f2f9800d 1586 struct net *net = sock_net(sk);
8826cde6 1587 struct socket *sock = sk->sk_socket;
cda3696d 1588 struct tipc_msg *hdr = buf_msg(skb);
7e6c131e 1589
cda3696d
JPM
1590 if (unlikely(msg_mcast(hdr)))
1591 return false;
7e6c131e
YX
1592
1593 switch ((int)sock->state) {
1594 case SS_CONNECTED:
cda3696d 1595
7e6c131e 1596 /* Accept only connection-based messages sent by peer */
cda3696d
JPM
1597 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1598 return false;
1599
1600 if (unlikely(msg_errcode(hdr))) {
1601 sock->state = SS_DISCONNECTING;
cda3696d
JPM
1602 /* Let timer expire on it's own */
1603 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1604 tsk->portid);
4891d8fe 1605 sk->sk_state_change(sk);
7e6c131e 1606 }
cda3696d
JPM
1607 return true;
1608
7e6c131e 1609 case SS_CONNECTING:
dadebc00 1610
cda3696d
JPM
1611 /* Accept only ACK or NACK message */
1612 if (unlikely(!msg_connected(hdr)))
1613 return false;
dadebc00 1614
cda3696d 1615 if (unlikely(msg_errcode(hdr))) {
584d24b3 1616 sock->state = SS_DISCONNECTING;
2c8d8518 1617 sk->sk_err = ECONNREFUSED;
cda3696d 1618 return true;
584d24b3
YX
1619 }
1620
cda3696d 1621 if (unlikely(!msg_isdata(hdr))) {
584d24b3 1622 sock->state = SS_DISCONNECTING;
dadebc00 1623 sk->sk_err = EINVAL;
cda3696d 1624 return true;
584d24b3
YX
1625 }
1626
cda3696d
JPM
1627 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1628 msg_set_importance(&tsk->phdr, msg_importance(hdr));
dadebc00
JPM
1629 sock->state = SS_CONNECTED;
1630
cda3696d
JPM
1631 /* If 'ACK+' message, add to socket receive queue */
1632 if (msg_data_sz(hdr))
1633 return true;
1634
1635 /* If empty 'ACK-' message, wake up sleeping connect() */
1636 if (waitqueue_active(sk_sleep(sk)))
1637 wake_up_interruptible(sk_sleep(sk));
1638
1639 /* 'ACK-' message is neither accepted nor rejected: */
1640 msg_set_dest_droppable(hdr, 1);
1641 return false;
1642
7e6c131e
YX
1643 case SS_LISTENING:
1644 case SS_UNCONNECTED:
cda3696d 1645
7e6c131e 1646 /* Accept only SYN message */
cda3696d
JPM
1647 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1648 return true;
7e6c131e
YX
1649 break;
1650 case SS_DISCONNECTING:
1651 break;
1652 default:
1653 pr_err("Unknown socket state %u\n", sock->state);
1654 }
cda3696d 1655 return false;
7e6c131e
YX
1656}
1657
aba79f33
YX
1658/**
1659 * rcvbuf_limit - get proper overload limit of socket receive queue
1660 * @sk: socket
10724cc7 1661 * @skb: message
aba79f33 1662 *
10724cc7
JPM
1663 * For connection oriented messages, irrespective of importance,
1664 * default queue limit is 2 MB.
aba79f33 1665 *
10724cc7
JPM
1666 * For connectionless messages, queue limits are based on message
1667 * importance as follows:
aba79f33 1668 *
10724cc7
JPM
1669 * TIPC_LOW_IMPORTANCE (2 MB)
1670 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1671 * TIPC_HIGH_IMPORTANCE (8 MB)
1672 * TIPC_CRITICAL_IMPORTANCE (16 MB)
aba79f33
YX
1673 *
1674 * Returns overload limit according to corresponding message importance
1675 */
10724cc7 1676static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
aba79f33 1677{
10724cc7
JPM
1678 struct tipc_sock *tsk = tipc_sk(sk);
1679 struct tipc_msg *hdr = buf_msg(skb);
1680
1681 if (unlikely(!msg_connected(hdr)))
1682 return sk->sk_rcvbuf << msg_importance(hdr);
aba79f33 1683
10724cc7
JPM
1684 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1685 return sk->sk_rcvbuf;
0cee6bbe 1686
10724cc7 1687 return FLOWCTL_MSG_LIM;
aba79f33
YX
1688}
1689
c4307285 1690/**
0c3141e9
AS
1691 * filter_rcv - validate incoming message
1692 * @sk: socket
cda3696d 1693 * @skb: pointer to message.
c4307285 1694 *
0c3141e9
AS
1695 * Enqueues message on receive queue if acceptable; optionally handles
1696 * disconnect indication for a connected socket.
1697 *
1186adf7 1698 * Called with socket lock already taken
c4307285 1699 *
cda3696d 1700 * Returns true if message was added to socket receive queue, otherwise false
b97bf3fd 1701 */
f1d048f2
JPM
1702static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1703 struct sk_buff_head *xmitq)
b97bf3fd 1704{
0c3141e9 1705 struct socket *sock = sk->sk_socket;
58ed9442 1706 struct tipc_sock *tsk = tipc_sk(sk);
cda3696d
JPM
1707 struct tipc_msg *hdr = buf_msg(skb);
1708 unsigned int limit = rcvbuf_limit(sk, skb);
1709 int err = TIPC_OK;
1710 int usr = msg_user(hdr);
b97bf3fd 1711
cda3696d 1712 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
f1d048f2 1713 tipc_sk_proto_rcv(tsk, skb, xmitq);
cda3696d 1714 return false;
1186adf7 1715 }
ec8a2e56 1716
cda3696d
JPM
1717 if (unlikely(usr == SOCK_WAKEUP)) {
1718 kfree_skb(skb);
50100a5e
JPM
1719 tsk->link_cong = 0;
1720 sk->sk_write_space(sk);
cda3696d 1721 return false;
50100a5e
JPM
1722 }
1723
cda3696d
JPM
1724 /* Drop if illegal message type */
1725 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1726 kfree_skb(skb);
1727 return false;
1728 }
0c3141e9 1729
cda3696d
JPM
1730 /* Reject if wrong message type for current socket state */
1731 if (unlikely(sock->state == SS_READY)) {
1732 if (msg_connected(hdr)) {
1733 err = TIPC_ERR_NO_PORT;
1734 goto reject;
1735 }
1736 } else if (unlikely(!filter_connect(tsk, skb))) {
1737 err = TIPC_ERR_NO_PORT;
1738 goto reject;
b97bf3fd
PL
1739 }
1740
1741 /* Reject message if there isn't room to queue it */
cda3696d
JPM
1742 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1743 err = TIPC_ERR_OVERLOAD;
1744 goto reject;
1745 }
b97bf3fd 1746
aba79f33 1747 /* Enqueue message */
ba8aebe9 1748 TIPC_SKB_CB(skb)->bytes_read = 0;
cda3696d
JPM
1749 __skb_queue_tail(&sk->sk_receive_queue, skb);
1750 skb_set_owner_r(skb, sk);
0c3141e9 1751
676d2369 1752 sk->sk_data_ready(sk);
cda3696d
JPM
1753 return true;
1754
1755reject:
f1d048f2
JPM
1756 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1757 __skb_queue_tail(xmitq, skb);
cda3696d 1758 return false;
0c3141e9 1759}
b97bf3fd 1760
0c3141e9 1761/**
4f4482dc 1762 * tipc_backlog_rcv - handle incoming message from backlog queue
0c3141e9 1763 * @sk: socket
a6ca1094 1764 * @skb: message
0c3141e9 1765 *
e3a77561 1766 * Caller must hold socket lock
0c3141e9
AS
1767 *
1768 * Returns 0
1769 */
a6ca1094 1770static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
0c3141e9 1771{
cda3696d 1772 unsigned int truesize = skb->truesize;
f1d048f2
JPM
1773 struct sk_buff_head xmitq;
1774 u32 dnode, selector;
0c3141e9 1775
f1d048f2
JPM
1776 __skb_queue_head_init(&xmitq);
1777
1778 if (likely(filter_rcv(sk, skb, &xmitq))) {
cda3696d 1779 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
f1d048f2
JPM
1780 return 0;
1781 }
1782
1783 if (skb_queue_empty(&xmitq))
1784 return 0;
1785
1786 /* Send response/rejected message */
1787 skb = __skb_dequeue(&xmitq);
1788 dnode = msg_destnode(buf_msg(skb));
1789 selector = msg_origport(buf_msg(skb));
1790 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
0c3141e9
AS
1791 return 0;
1792}
1793
d570d864 1794/**
c637c103
JPM
1795 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1796 * inputq and try adding them to socket or backlog queue
1797 * @inputq: list of incoming buffers with potentially different destinations
1798 * @sk: socket where the buffers should be enqueued
1799 * @dport: port number for the socket
d570d864
JPM
1800 *
1801 * Caller must hold socket lock
d570d864 1802 */
cda3696d 1803static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
f1d048f2 1804 u32 dport, struct sk_buff_head *xmitq)
d570d864 1805{
f1d048f2
JPM
1806 unsigned long time_limit = jiffies + 2;
1807 struct sk_buff *skb;
d570d864
JPM
1808 unsigned int lim;
1809 atomic_t *dcnt;
f1d048f2 1810 u32 onode;
c637c103
JPM
1811
1812 while (skb_queue_len(inputq)) {
51a00daf 1813 if (unlikely(time_after_eq(jiffies, time_limit)))
cda3696d
JPM
1814 return;
1815
c637c103
JPM
1816 skb = tipc_skb_dequeue(inputq, dport);
1817 if (unlikely(!skb))
cda3696d
JPM
1818 return;
1819
1820 /* Add message directly to receive queue if possible */
c637c103 1821 if (!sock_owned_by_user(sk)) {
f1d048f2 1822 filter_rcv(sk, skb, xmitq);
cda3696d 1823 continue;
c637c103 1824 }
cda3696d
JPM
1825
1826 /* Try backlog, compensating for double-counted bytes */
c637c103 1827 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
7c8bcfb1 1828 if (!sk->sk_backlog.len)
c637c103
JPM
1829 atomic_set(dcnt, 0);
1830 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1831 if (likely(!sk_add_backlog(sk, skb, lim)))
1832 continue;
cda3696d
JPM
1833
1834 /* Overload => reject message back to sender */
f1d048f2
JPM
1835 onode = tipc_own_addr(sock_net(sk));
1836 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1837 __skb_queue_tail(xmitq, skb);
cda3696d 1838 break;
c637c103 1839 }
d570d864
JPM
1840}
1841
0c3141e9 1842/**
c637c103
JPM
1843 * tipc_sk_rcv - handle a chain of incoming buffers
1844 * @inputq: buffer list containing the buffers
1845 * Consumes all buffers in list until inputq is empty
1846 * Note: may be called in multiple threads referring to the same queue
0c3141e9 1847 */
cda3696d 1848void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
0c3141e9 1849{
f1d048f2 1850 struct sk_buff_head xmitq;
c637c103 1851 u32 dnode, dport = 0;
9871b27f 1852 int err;
9816f061 1853 struct tipc_sock *tsk;
9816f061 1854 struct sock *sk;
cda3696d 1855 struct sk_buff *skb;
9816f061 1856
f1d048f2 1857 __skb_queue_head_init(&xmitq);
c637c103 1858 while (skb_queue_len(inputq)) {
c637c103
JPM
1859 dport = tipc_skb_peek_port(inputq, dport);
1860 tsk = tipc_sk_lookup(net, dport);
cda3696d 1861
c637c103
JPM
1862 if (likely(tsk)) {
1863 sk = &tsk->sk;
1864 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
f1d048f2 1865 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
c637c103 1866 spin_unlock_bh(&sk->sk_lock.slock);
c637c103 1867 }
f1d048f2
JPM
1868 /* Send pending response/rejected messages, if any */
1869 while ((skb = __skb_dequeue(&xmitq))) {
1870 dnode = msg_destnode(buf_msg(skb));
1871 tipc_node_xmit_skb(net, skb, dnode, dport);
1872 }
c637c103 1873 sock_put(sk);
c637c103 1874 continue;
c637c103 1875 }
cda3696d
JPM
1876
1877 /* No destination socket => dequeue skb if still there */
1878 skb = tipc_skb_dequeue(inputq, dport);
1879 if (!skb)
1880 return;
1881
1882 /* Try secondary lookup if unresolved named message */
1883 err = TIPC_ERR_NO_PORT;
1884 if (tipc_msg_lookup_dest(net, skb, &err))
1885 goto xmit;
1886
1887 /* Prepare for message rejection */
1888 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
c637c103 1889 continue;
e3a77561 1890xmit:
cda3696d 1891 dnode = msg_destnode(buf_msg(skb));
af9b028e 1892 tipc_node_xmit_skb(net, skb, dnode, dport);
c637c103 1893 }
b97bf3fd
PL
1894}
1895
78eb3a53
YX
1896static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1897{
1898 struct sock *sk = sock->sk;
1899 DEFINE_WAIT(wait);
1900 int done;
1901
1902 do {
1903 int err = sock_error(sk);
1904 if (err)
1905 return err;
1906 if (!*timeo_p)
1907 return -ETIMEDOUT;
1908 if (signal_pending(current))
1909 return sock_intr_errno(*timeo_p);
1910
1911 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1912 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1913 finish_wait(sk_sleep(sk), &wait);
1914 } while (!done);
1915 return 0;
1916}
1917
b97bf3fd 1918/**
247f0f3c 1919 * tipc_connect - establish a connection to another TIPC port
b97bf3fd
PL
1920 * @sock: socket structure
1921 * @dest: socket address for destination port
1922 * @destlen: size of socket address data structure
0c3141e9 1923 * @flags: file-related flags associated with socket
b97bf3fd
PL
1924 *
1925 * Returns 0 on success, errno otherwise
1926 */
247f0f3c
YX
1927static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1928 int destlen, int flags)
b97bf3fd 1929{
0c3141e9 1930 struct sock *sk = sock->sk;
f2f8036e 1931 struct tipc_sock *tsk = tipc_sk(sk);
b89741a0
AS
1932 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1933 struct msghdr m = {NULL,};
f2f8036e 1934 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
78eb3a53 1935 socket_state previous;
f2f8036e 1936 int res = 0;
b89741a0 1937
0c3141e9
AS
1938 lock_sock(sk);
1939
f2f8036e 1940 /* DGRAM/RDM connect(), just save the destaddr */
0c3141e9 1941 if (sock->state == SS_READY) {
f2f8036e 1942 if (dst->family == AF_UNSPEC) {
aeda16b6 1943 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
610600c8
SL
1944 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1945 res = -EINVAL;
f2f8036e 1946 } else {
aeda16b6 1947 memcpy(&tsk->peer, dest, destlen);
f2f8036e 1948 }
0c3141e9
AS
1949 goto exit;
1950 }
b89741a0 1951
b89741a0
AS
1952 /*
1953 * Reject connection attempt using multicast address
1954 *
1955 * Note: send_msg() validates the rest of the address fields,
1956 * so there's no need to do it here
1957 */
0c3141e9
AS
1958 if (dst->addrtype == TIPC_ADDR_MCAST) {
1959 res = -EINVAL;
1960 goto exit;
1961 }
1962
78eb3a53 1963 previous = sock->state;
584d24b3
YX
1964 switch (sock->state) {
1965 case SS_UNCONNECTED:
1966 /* Send a 'SYN-' to destination */
1967 m.msg_name = dest;
1968 m.msg_namelen = destlen;
1969
1970 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1971 * indicate send_msg() is never blocked.
1972 */
1973 if (!timeout)
1974 m.msg_flags = MSG_DONTWAIT;
1975
39a0295f 1976 res = __tipc_sendmsg(sock, &m, 0);
584d24b3
YX
1977 if ((res < 0) && (res != -EWOULDBLOCK))
1978 goto exit;
1979
1980 /* Just entered SS_CONNECTING state; the only
1981 * difference is that return value in non-blocking
1982 * case is EINPROGRESS, rather than EALREADY.
1983 */
1984 res = -EINPROGRESS;
584d24b3 1985 case SS_CONNECTING:
78eb3a53
YX
1986 if (previous == SS_CONNECTING)
1987 res = -EALREADY;
1988 if (!timeout)
1989 goto exit;
1990 timeout = msecs_to_jiffies(timeout);
1991 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1992 res = tipc_wait_for_connect(sock, &timeout);
584d24b3
YX
1993 break;
1994 case SS_CONNECTED:
1995 res = -EISCONN;
1996 break;
1997 default:
1998 res = -EINVAL;
78eb3a53 1999 break;
b89741a0 2000 }
0c3141e9
AS
2001exit:
2002 release_sock(sk);
b89741a0 2003 return res;
b97bf3fd
PL
2004}
2005
c4307285 2006/**
247f0f3c 2007 * tipc_listen - allow socket to listen for incoming connections
b97bf3fd
PL
2008 * @sock: socket structure
2009 * @len: (unused)
c4307285 2010 *
b97bf3fd
PL
2011 * Returns 0 on success, errno otherwise
2012 */
247f0f3c 2013static int tipc_listen(struct socket *sock, int len)
b97bf3fd 2014{
0c3141e9
AS
2015 struct sock *sk = sock->sk;
2016 int res;
2017
2018 lock_sock(sk);
b97bf3fd 2019
245f3d34 2020 if (sock->state != SS_UNCONNECTED)
0c3141e9
AS
2021 res = -EINVAL;
2022 else {
2023 sock->state = SS_LISTENING;
2024 res = 0;
2025 }
2026
2027 release_sock(sk);
2028 return res;
b97bf3fd
PL
2029}
2030
6398e23c
YX
2031static int tipc_wait_for_accept(struct socket *sock, long timeo)
2032{
2033 struct sock *sk = sock->sk;
2034 DEFINE_WAIT(wait);
2035 int err;
2036
2037 /* True wake-one mechanism for incoming connections: only
2038 * one process gets woken up, not the 'whole herd'.
2039 * Since we do not 'race & poll' for established sockets
2040 * anymore, the common case will execute the loop only once.
2041 */
2042 for (;;) {
2043 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2044 TASK_INTERRUPTIBLE);
fe8e4649 2045 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
6398e23c
YX
2046 release_sock(sk);
2047 timeo = schedule_timeout(timeo);
2048 lock_sock(sk);
2049 }
2050 err = 0;
2051 if (!skb_queue_empty(&sk->sk_receive_queue))
2052 break;
2053 err = -EINVAL;
2054 if (sock->state != SS_LISTENING)
2055 break;
6398e23c
YX
2056 err = -EAGAIN;
2057 if (!timeo)
2058 break;
143fe22f
EH
2059 err = sock_intr_errno(timeo);
2060 if (signal_pending(current))
2061 break;
6398e23c
YX
2062 }
2063 finish_wait(sk_sleep(sk), &wait);
2064 return err;
2065}
2066
c4307285 2067/**
247f0f3c 2068 * tipc_accept - wait for connection request
b97bf3fd
PL
2069 * @sock: listening socket
2070 * @newsock: new socket that is to be connected
2071 * @flags: file-related flags associated with socket
c4307285 2072 *
b97bf3fd
PL
2073 * Returns 0 on success, errno otherwise
2074 */
247f0f3c 2075static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
b97bf3fd 2076{
0fef8f20 2077 struct sock *new_sk, *sk = sock->sk;
b97bf3fd 2078 struct sk_buff *buf;
301bae56 2079 struct tipc_sock *new_tsock;
0fef8f20 2080 struct tipc_msg *msg;
6398e23c 2081 long timeo;
0c3141e9 2082 int res;
b97bf3fd 2083
0c3141e9 2084 lock_sock(sk);
b97bf3fd 2085
0c3141e9
AS
2086 if (sock->state != SS_LISTENING) {
2087 res = -EINVAL;
b97bf3fd
PL
2088 goto exit;
2089 }
6398e23c
YX
2090 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2091 res = tipc_wait_for_accept(sock, timeo);
2092 if (res)
2093 goto exit;
0c3141e9
AS
2094
2095 buf = skb_peek(&sk->sk_receive_queue);
2096
cb5da847 2097 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 0);
0fef8f20
PG
2098 if (res)
2099 goto exit;
fdd75ea8 2100 security_sk_clone(sock->sk, new_sock->sk);
b97bf3fd 2101
0fef8f20 2102 new_sk = new_sock->sk;
301bae56 2103 new_tsock = tipc_sk(new_sk);
0fef8f20 2104 msg = buf_msg(buf);
b97bf3fd 2105
0fef8f20
PG
2106 /* we lock on new_sk; but lockdep sees the lock on sk */
2107 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
2108
2109 /*
2110 * Reject any stray messages received by new socket
2111 * before the socket lock was taken (very, very unlikely)
2112 */
2e84c60b 2113 tsk_rej_rx_queue(new_sk);
0fef8f20
PG
2114
2115 /* Connect new socket to it's peer */
301bae56 2116 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
0fef8f20
PG
2117 new_sock->state = SS_CONNECTED;
2118
301bae56 2119 tsk_set_importance(new_tsock, msg_importance(msg));
0fef8f20 2120 if (msg_named(msg)) {
301bae56
JPM
2121 new_tsock->conn_type = msg_nametype(msg);
2122 new_tsock->conn_instance = msg_nameinst(msg);
b97bf3fd 2123 }
0fef8f20
PG
2124
2125 /*
2126 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2127 * Respond to 'SYN+' by queuing it on new socket.
2128 */
2129 if (!msg_data_sz(msg)) {
2130 struct msghdr m = {NULL,};
2131
2e84c60b 2132 tsk_advance_rx_queue(sk);
39a0295f 2133 __tipc_send_stream(new_sock, &m, 0);
0fef8f20
PG
2134 } else {
2135 __skb_dequeue(&sk->sk_receive_queue);
2136 __skb_queue_head(&new_sk->sk_receive_queue, buf);
aba79f33 2137 skb_set_owner_r(buf, new_sk);
0fef8f20
PG
2138 }
2139 release_sock(new_sk);
b97bf3fd 2140exit:
0c3141e9 2141 release_sock(sk);
b97bf3fd
PL
2142 return res;
2143}
2144
2145/**
247f0f3c 2146 * tipc_shutdown - shutdown socket connection
b97bf3fd 2147 * @sock: socket structure
e247a8f5 2148 * @how: direction to close (must be SHUT_RDWR)
b97bf3fd
PL
2149 *
2150 * Terminates connection (if necessary), then purges socket's receive queue.
c4307285 2151 *
b97bf3fd
PL
2152 * Returns 0 on success, errno otherwise
2153 */
247f0f3c 2154static int tipc_shutdown(struct socket *sock, int how)
b97bf3fd 2155{
0c3141e9 2156 struct sock *sk = sock->sk;
f2f9800d 2157 struct net *net = sock_net(sk);
58ed9442 2158 struct tipc_sock *tsk = tipc_sk(sk);
a6ca1094 2159 struct sk_buff *skb;
cda3696d
JPM
2160 u32 dnode = tsk_peer_node(tsk);
2161 u32 dport = tsk_peer_port(tsk);
2162 u32 onode = tipc_own_addr(net);
2163 u32 oport = tsk->portid;
b97bf3fd
PL
2164 int res;
2165
e247a8f5
AS
2166 if (how != SHUT_RDWR)
2167 return -EINVAL;
b97bf3fd 2168
0c3141e9 2169 lock_sock(sk);
b97bf3fd
PL
2170
2171 switch (sock->state) {
0c3141e9 2172 case SS_CONNECTING:
b97bf3fd
PL
2173 case SS_CONNECTED:
2174
b97bf3fd 2175restart:
bcd3ffd4
JPM
2176 dnode = tsk_peer_node(tsk);
2177
617d3c7a 2178 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
a6ca1094
YX
2179 skb = __skb_dequeue(&sk->sk_receive_queue);
2180 if (skb) {
ba8aebe9 2181 if (TIPC_SKB_CB(skb)->bytes_read) {
a6ca1094 2182 kfree_skb(skb);
b97bf3fd
PL
2183 goto restart;
2184 }
bcd3ffd4 2185 tipc_sk_respond(sk, skb, TIPC_CONN_SHUTDOWN);
0c3141e9 2186 } else {
c5898636 2187 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
80e44c22 2188 TIPC_CONN_MSG, SHORT_H_SIZE,
cda3696d
JPM
2189 0, dnode, onode, dport, oport,
2190 TIPC_CONN_SHUTDOWN);
d2fbdf76
VN
2191 if (skb)
2192 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
b97bf3fd 2193 }
0c3141e9 2194 sock->state = SS_DISCONNECTING;
f2f9800d 2195 tipc_node_remove_conn(net, dnode, tsk->portid);
b97bf3fd
PL
2196 /* fall through */
2197
2198 case SS_DISCONNECTING:
2199
75031151 2200 /* Discard any unreceived messages */
57467e56 2201 __skb_queue_purge(&sk->sk_receive_queue);
75031151
YX
2202
2203 /* Wake up anyone sleeping in poll */
2204 sk->sk_state_change(sk);
b97bf3fd
PL
2205 res = 0;
2206 break;
2207
2208 default:
2209 res = -ENOTCONN;
2210 }
2211
0c3141e9 2212 release_sock(sk);
b97bf3fd
PL
2213 return res;
2214}
2215
f2f2a96a 2216static void tipc_sk_timeout(unsigned long data)
57289015 2217{
f2f2a96a
YX
2218 struct tipc_sock *tsk = (struct tipc_sock *)data;
2219 struct sock *sk = &tsk->sk;
a6ca1094 2220 struct sk_buff *skb = NULL;
57289015 2221 u32 peer_port, peer_node;
c5898636 2222 u32 own_node = tsk_own_node(tsk);
57289015 2223
6c9808ce 2224 bh_lock_sock(sk);
d6fb7e9c 2225 if (!tipc_sk_connected(sk)) {
6c9808ce
JPM
2226 bh_unlock_sock(sk);
2227 goto exit;
57289015 2228 }
301bae56
JPM
2229 peer_port = tsk_peer_port(tsk);
2230 peer_node = tsk_peer_node(tsk);
57289015 2231
301bae56 2232 if (tsk->probing_state == TIPC_CONN_PROBING) {
b3be5e3e
EH
2233 if (!sock_owned_by_user(sk)) {
2234 sk->sk_socket->state = SS_DISCONNECTING;
b3be5e3e
EH
2235 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2236 tsk_peer_port(tsk));
2237 sk->sk_state_change(sk);
2238 } else {
2239 /* Try again later */
2240 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2241 }
2242
57289015 2243 } else {
c5898636
JPM
2244 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2245 INT_H_SIZE, 0, peer_node, own_node,
f2f2a96a 2246 peer_port, tsk->portid, TIPC_OK);
301bae56 2247 tsk->probing_state = TIPC_CONN_PROBING;
3721e9c7 2248 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
57289015
JPM
2249 }
2250 bh_unlock_sock(sk);
a6ca1094 2251 if (skb)
af9b028e 2252 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
6c9808ce 2253exit:
07f6c4bc 2254 sock_put(sk);
57289015
JPM
2255}
2256
301bae56 2257static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
0fc87aae
JPM
2258 struct tipc_name_seq const *seq)
2259{
d6fb7e9c
PB
2260 struct sock *sk = &tsk->sk;
2261 struct net *net = sock_net(sk);
0fc87aae
JPM
2262 struct publication *publ;
2263 u32 key;
2264
d6fb7e9c 2265 if (tipc_sk_connected(sk))
0fc87aae 2266 return -EINVAL;
07f6c4bc
YX
2267 key = tsk->portid + tsk->pub_count + 1;
2268 if (key == tsk->portid)
0fc87aae
JPM
2269 return -EADDRINUSE;
2270
f2f9800d 2271 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
07f6c4bc 2272 scope, tsk->portid, key);
0fc87aae
JPM
2273 if (unlikely(!publ))
2274 return -EINVAL;
2275
301bae56
JPM
2276 list_add(&publ->pport_list, &tsk->publications);
2277 tsk->pub_count++;
2278 tsk->published = 1;
0fc87aae
JPM
2279 return 0;
2280}
2281
301bae56 2282static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
0fc87aae
JPM
2283 struct tipc_name_seq const *seq)
2284{
f2f9800d 2285 struct net *net = sock_net(&tsk->sk);
0fc87aae
JPM
2286 struct publication *publ;
2287 struct publication *safe;
2288 int rc = -EINVAL;
2289
301bae56 2290 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
0fc87aae
JPM
2291 if (seq) {
2292 if (publ->scope != scope)
2293 continue;
2294 if (publ->type != seq->type)
2295 continue;
2296 if (publ->lower != seq->lower)
2297 continue;
2298 if (publ->upper != seq->upper)
2299 break;
f2f9800d 2300 tipc_nametbl_withdraw(net, publ->type, publ->lower,
0fc87aae
JPM
2301 publ->ref, publ->key);
2302 rc = 0;
2303 break;
2304 }
f2f9800d 2305 tipc_nametbl_withdraw(net, publ->type, publ->lower,
0fc87aae
JPM
2306 publ->ref, publ->key);
2307 rc = 0;
2308 }
301bae56
JPM
2309 if (list_empty(&tsk->publications))
2310 tsk->published = 0;
0fc87aae
JPM
2311 return rc;
2312}
2313
5a9ee0be
JPM
2314/* tipc_sk_reinit: set non-zero address in all existing sockets
2315 * when we go from standalone to network mode.
2316 */
e05b31f4 2317void tipc_sk_reinit(struct net *net)
5a9ee0be 2318{
e05b31f4 2319 struct tipc_net *tn = net_generic(net, tipc_net_id);
07f6c4bc
YX
2320 const struct bucket_table *tbl;
2321 struct rhash_head *pos;
2322 struct tipc_sock *tsk;
5a9ee0be 2323 struct tipc_msg *msg;
07f6c4bc 2324 int i;
5a9ee0be 2325
07f6c4bc 2326 rcu_read_lock();
e05b31f4 2327 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
07f6c4bc
YX
2328 for (i = 0; i < tbl->size; i++) {
2329 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2330 spin_lock_bh(&tsk->sk.sk_lock.slock);
2331 msg = &tsk->phdr;
34747539
YX
2332 msg_set_prevnode(msg, tn->own_addr);
2333 msg_set_orignode(msg, tn->own_addr);
07f6c4bc
YX
2334 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2335 }
5a9ee0be 2336 }
07f6c4bc 2337 rcu_read_unlock();
5a9ee0be
JPM
2338}
2339
e05b31f4 2340static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
808d90f9 2341{
e05b31f4 2342 struct tipc_net *tn = net_generic(net, tipc_net_id);
07f6c4bc 2343 struct tipc_sock *tsk;
808d90f9 2344
07f6c4bc 2345 rcu_read_lock();
6cca7289 2346 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
07f6c4bc
YX
2347 if (tsk)
2348 sock_hold(&tsk->sk);
2349 rcu_read_unlock();
808d90f9 2350
07f6c4bc 2351 return tsk;
808d90f9
JPM
2352}
2353
07f6c4bc 2354static int tipc_sk_insert(struct tipc_sock *tsk)
808d90f9 2355{
e05b31f4
YX
2356 struct sock *sk = &tsk->sk;
2357 struct net *net = sock_net(sk);
2358 struct tipc_net *tn = net_generic(net, tipc_net_id);
07f6c4bc
YX
2359 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2360 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
808d90f9 2361
07f6c4bc
YX
2362 while (remaining--) {
2363 portid++;
2364 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2365 portid = TIPC_MIN_PORT;
2366 tsk->portid = portid;
2367 sock_hold(&tsk->sk);
6cca7289
HX
2368 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2369 tsk_rht_params))
07f6c4bc
YX
2370 return 0;
2371 sock_put(&tsk->sk);
808d90f9
JPM
2372 }
2373
07f6c4bc 2374 return -1;
808d90f9
JPM
2375}
2376
07f6c4bc 2377static void tipc_sk_remove(struct tipc_sock *tsk)
808d90f9 2378{
07f6c4bc 2379 struct sock *sk = &tsk->sk;
e05b31f4 2380 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
808d90f9 2381
6cca7289 2382 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
07f6c4bc
YX
2383 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2384 __sock_put(sk);
808d90f9 2385 }
808d90f9
JPM
2386}
2387
6cca7289
HX
2388static const struct rhashtable_params tsk_rht_params = {
2389 .nelem_hint = 192,
2390 .head_offset = offsetof(struct tipc_sock, node),
2391 .key_offset = offsetof(struct tipc_sock, portid),
2392 .key_len = sizeof(u32), /* portid */
6cca7289
HX
2393 .max_size = 1048576,
2394 .min_size = 256,
b5e2c150 2395 .automatic_shrinking = true,
6cca7289
HX
2396};
2397
e05b31f4 2398int tipc_sk_rht_init(struct net *net)
808d90f9 2399{
e05b31f4 2400 struct tipc_net *tn = net_generic(net, tipc_net_id);
6cca7289
HX
2401
2402 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
808d90f9
JPM
2403}
2404
e05b31f4 2405void tipc_sk_rht_destroy(struct net *net)
808d90f9 2406{
e05b31f4
YX
2407 struct tipc_net *tn = net_generic(net, tipc_net_id);
2408
07f6c4bc
YX
2409 /* Wait for socket readers to complete */
2410 synchronize_net();
808d90f9 2411
e05b31f4 2412 rhashtable_destroy(&tn->sk_rht);
808d90f9
JPM
2413}
2414
b97bf3fd 2415/**
247f0f3c 2416 * tipc_setsockopt - set socket option
b97bf3fd
PL
2417 * @sock: socket structure
2418 * @lvl: option level
2419 * @opt: option identifier
2420 * @ov: pointer to new option value
2421 * @ol: length of option value
c4307285
YH
2422 *
2423 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
b97bf3fd 2424 * (to ease compatibility).
c4307285 2425 *
b97bf3fd
PL
2426 * Returns 0 on success, errno otherwise
2427 */
247f0f3c
YX
2428static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2429 char __user *ov, unsigned int ol)
b97bf3fd 2430{
0c3141e9 2431 struct sock *sk = sock->sk;
58ed9442 2432 struct tipc_sock *tsk = tipc_sk(sk);
b97bf3fd
PL
2433 u32 value;
2434 int res;
2435
c4307285
YH
2436 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2437 return 0;
b97bf3fd
PL
2438 if (lvl != SOL_TIPC)
2439 return -ENOPROTOOPT;
2440 if (ol < sizeof(value))
2441 return -EINVAL;
2db9983a
AS
2442 res = get_user(value, (u32 __user *)ov);
2443 if (res)
b97bf3fd
PL
2444 return res;
2445
0c3141e9 2446 lock_sock(sk);
c4307285 2447
b97bf3fd
PL
2448 switch (opt) {
2449 case TIPC_IMPORTANCE:
301bae56 2450 res = tsk_set_importance(tsk, value);
b97bf3fd
PL
2451 break;
2452 case TIPC_SRC_DROPPABLE:
2453 if (sock->type != SOCK_STREAM)
301bae56 2454 tsk_set_unreliable(tsk, value);
c4307285 2455 else
b97bf3fd
PL
2456 res = -ENOPROTOOPT;
2457 break;
2458 case TIPC_DEST_DROPPABLE:
301bae56 2459 tsk_set_unreturnable(tsk, value);
b97bf3fd
PL
2460 break;
2461 case TIPC_CONN_TIMEOUT:
a0f40f02 2462 tipc_sk(sk)->conn_timeout = value;
0c3141e9 2463 /* no need to set "res", since already 0 at this point */
b97bf3fd
PL
2464 break;
2465 default:
2466 res = -EINVAL;
2467 }
2468
0c3141e9
AS
2469 release_sock(sk);
2470
b97bf3fd
PL
2471 return res;
2472}
2473
2474/**
247f0f3c 2475 * tipc_getsockopt - get socket option
b97bf3fd
PL
2476 * @sock: socket structure
2477 * @lvl: option level
2478 * @opt: option identifier
2479 * @ov: receptacle for option value
2480 * @ol: receptacle for length of option value
c4307285
YH
2481 *
2482 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
b97bf3fd 2483 * (to ease compatibility).
c4307285 2484 *
b97bf3fd
PL
2485 * Returns 0 on success, errno otherwise
2486 */
247f0f3c
YX
2487static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2488 char __user *ov, int __user *ol)
b97bf3fd 2489{
0c3141e9 2490 struct sock *sk = sock->sk;
58ed9442 2491 struct tipc_sock *tsk = tipc_sk(sk);
c4307285 2492 int len;
b97bf3fd 2493 u32 value;
c4307285 2494 int res;
b97bf3fd 2495
c4307285
YH
2496 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2497 return put_user(0, ol);
b97bf3fd
PL
2498 if (lvl != SOL_TIPC)
2499 return -ENOPROTOOPT;
2db9983a
AS
2500 res = get_user(len, ol);
2501 if (res)
c4307285 2502 return res;
b97bf3fd 2503
0c3141e9 2504 lock_sock(sk);
b97bf3fd
PL
2505
2506 switch (opt) {
2507 case TIPC_IMPORTANCE:
301bae56 2508 value = tsk_importance(tsk);
b97bf3fd
PL
2509 break;
2510 case TIPC_SRC_DROPPABLE:
301bae56 2511 value = tsk_unreliable(tsk);
b97bf3fd
PL
2512 break;
2513 case TIPC_DEST_DROPPABLE:
301bae56 2514 value = tsk_unreturnable(tsk);
b97bf3fd
PL
2515 break;
2516 case TIPC_CONN_TIMEOUT:
301bae56 2517 value = tsk->conn_timeout;
0c3141e9 2518 /* no need to set "res", since already 0 at this point */
b97bf3fd 2519 break;
0e65967e 2520 case TIPC_NODE_RECVQ_DEPTH:
9da3d475 2521 value = 0; /* was tipc_queue_size, now obsolete */
6650613d 2522 break;
0e65967e 2523 case TIPC_SOCK_RECVQ_DEPTH:
6650613d 2524 value = skb_queue_len(&sk->sk_receive_queue);
2525 break;
b97bf3fd
PL
2526 default:
2527 res = -EINVAL;
2528 }
2529
0c3141e9
AS
2530 release_sock(sk);
2531
25860c3b
PG
2532 if (res)
2533 return res; /* "get" failed */
b97bf3fd 2534
25860c3b
PG
2535 if (len < sizeof(value))
2536 return -EINVAL;
2537
2538 if (copy_to_user(ov, &value, sizeof(value)))
2539 return -EFAULT;
2540
2541 return put_user(sizeof(value), ol);
b97bf3fd
PL
2542}
2543
f2f9800d 2544static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
78acb1f9 2545{
f2f9800d 2546 struct sock *sk = sock->sk;
78acb1f9
EH
2547 struct tipc_sioc_ln_req lnr;
2548 void __user *argp = (void __user *)arg;
2549
2550 switch (cmd) {
2551 case SIOCGETLINKNAME:
2552 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2553 return -EFAULT;
f2f9800d
YX
2554 if (!tipc_node_get_linkname(sock_net(sk),
2555 lnr.bearer_id & 0xffff, lnr.peer,
78acb1f9
EH
2556 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2557 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2558 return -EFAULT;
2559 return 0;
2560 }
2561 return -EADDRNOTAVAIL;
78acb1f9
EH
2562 default:
2563 return -ENOIOCTLCMD;
2564 }
2565}
2566
ae86b9e3
BH
2567/* Protocol switches for the various types of TIPC sockets */
2568
bca65eae 2569static const struct proto_ops msg_ops = {
0e65967e 2570 .owner = THIS_MODULE,
b97bf3fd 2571 .family = AF_TIPC,
247f0f3c
YX
2572 .release = tipc_release,
2573 .bind = tipc_bind,
2574 .connect = tipc_connect,
5eee6a6d 2575 .socketpair = sock_no_socketpair,
245f3d34 2576 .accept = sock_no_accept,
247f0f3c
YX
2577 .getname = tipc_getname,
2578 .poll = tipc_poll,
78acb1f9 2579 .ioctl = tipc_ioctl,
245f3d34 2580 .listen = sock_no_listen,
247f0f3c
YX
2581 .shutdown = tipc_shutdown,
2582 .setsockopt = tipc_setsockopt,
2583 .getsockopt = tipc_getsockopt,
2584 .sendmsg = tipc_sendmsg,
2585 .recvmsg = tipc_recvmsg,
8238745a
YH
2586 .mmap = sock_no_mmap,
2587 .sendpage = sock_no_sendpage
b97bf3fd
PL
2588};
2589
bca65eae 2590static const struct proto_ops packet_ops = {
0e65967e 2591 .owner = THIS_MODULE,
b97bf3fd 2592 .family = AF_TIPC,
247f0f3c
YX
2593 .release = tipc_release,
2594 .bind = tipc_bind,
2595 .connect = tipc_connect,
5eee6a6d 2596 .socketpair = sock_no_socketpair,
247f0f3c
YX
2597 .accept = tipc_accept,
2598 .getname = tipc_getname,
2599 .poll = tipc_poll,
78acb1f9 2600 .ioctl = tipc_ioctl,
247f0f3c
YX
2601 .listen = tipc_listen,
2602 .shutdown = tipc_shutdown,
2603 .setsockopt = tipc_setsockopt,
2604 .getsockopt = tipc_getsockopt,
2605 .sendmsg = tipc_send_packet,
2606 .recvmsg = tipc_recvmsg,
8238745a
YH
2607 .mmap = sock_no_mmap,
2608 .sendpage = sock_no_sendpage
b97bf3fd
PL
2609};
2610
bca65eae 2611static const struct proto_ops stream_ops = {
0e65967e 2612 .owner = THIS_MODULE,
b97bf3fd 2613 .family = AF_TIPC,
247f0f3c
YX
2614 .release = tipc_release,
2615 .bind = tipc_bind,
2616 .connect = tipc_connect,
5eee6a6d 2617 .socketpair = sock_no_socketpair,
247f0f3c
YX
2618 .accept = tipc_accept,
2619 .getname = tipc_getname,
2620 .poll = tipc_poll,
78acb1f9 2621 .ioctl = tipc_ioctl,
247f0f3c
YX
2622 .listen = tipc_listen,
2623 .shutdown = tipc_shutdown,
2624 .setsockopt = tipc_setsockopt,
2625 .getsockopt = tipc_getsockopt,
2626 .sendmsg = tipc_send_stream,
2627 .recvmsg = tipc_recv_stream,
8238745a
YH
2628 .mmap = sock_no_mmap,
2629 .sendpage = sock_no_sendpage
b97bf3fd
PL
2630};
2631
bca65eae 2632static const struct net_proto_family tipc_family_ops = {
0e65967e 2633 .owner = THIS_MODULE,
b97bf3fd 2634 .family = AF_TIPC,
c5fa7b3c 2635 .create = tipc_sk_create
b97bf3fd
PL
2636};
2637
2638static struct proto tipc_proto = {
2639 .name = "TIPC",
2640 .owner = THIS_MODULE,
cc79dd1b
YX
2641 .obj_size = sizeof(struct tipc_sock),
2642 .sysctl_rmem = sysctl_tipc_rmem
b97bf3fd
PL
2643};
2644
2645/**
4323add6 2646 * tipc_socket_init - initialize TIPC socket interface
c4307285 2647 *
b97bf3fd
PL
2648 * Returns 0 on success, errno otherwise
2649 */
4323add6 2650int tipc_socket_init(void)
b97bf3fd
PL
2651{
2652 int res;
2653
c4307285 2654 res = proto_register(&tipc_proto, 1);
b97bf3fd 2655 if (res) {
2cf8aa19 2656 pr_err("Failed to register TIPC protocol type\n");
b97bf3fd
PL
2657 goto out;
2658 }
2659
2660 res = sock_register(&tipc_family_ops);
2661 if (res) {
2cf8aa19 2662 pr_err("Failed to register TIPC socket type\n");
b97bf3fd
PL
2663 proto_unregister(&tipc_proto);
2664 goto out;
2665 }
b97bf3fd
PL
2666 out:
2667 return res;
2668}
2669
2670/**
4323add6 2671 * tipc_socket_stop - stop TIPC socket interface
b97bf3fd 2672 */
4323add6 2673void tipc_socket_stop(void)
b97bf3fd 2674{
b97bf3fd
PL
2675 sock_unregister(tipc_family_ops.family);
2676 proto_unregister(&tipc_proto);
2677}
34b78a12
RA
2678
2679/* Caller should hold socket lock for the passed tipc socket. */
d8182804 2680static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
34b78a12
RA
2681{
2682 u32 peer_node;
2683 u32 peer_port;
2684 struct nlattr *nest;
2685
2686 peer_node = tsk_peer_node(tsk);
2687 peer_port = tsk_peer_port(tsk);
2688
2689 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2690
2691 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2692 goto msg_full;
2693 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2694 goto msg_full;
2695
2696 if (tsk->conn_type != 0) {
2697 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2698 goto msg_full;
2699 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2700 goto msg_full;
2701 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2702 goto msg_full;
2703 }
2704 nla_nest_end(skb, nest);
2705
2706 return 0;
2707
2708msg_full:
2709 nla_nest_cancel(skb, nest);
2710
2711 return -EMSGSIZE;
2712}
2713
2714/* Caller should hold socket lock for the passed tipc socket. */
d8182804
RA
2715static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2716 struct tipc_sock *tsk)
34b78a12
RA
2717{
2718 int err;
2719 void *hdr;
2720 struct nlattr *attrs;
34747539
YX
2721 struct net *net = sock_net(skb->sk);
2722 struct tipc_net *tn = net_generic(net, tipc_net_id);
d6fb7e9c 2723 struct sock *sk = &tsk->sk;
34b78a12
RA
2724
2725 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
bfb3e5dd 2726 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
34b78a12
RA
2727 if (!hdr)
2728 goto msg_cancel;
2729
2730 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2731 if (!attrs)
2732 goto genlmsg_cancel;
07f6c4bc 2733 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
34b78a12 2734 goto attr_msg_cancel;
34747539 2735 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
34b78a12
RA
2736 goto attr_msg_cancel;
2737
d6fb7e9c 2738 if (tipc_sk_connected(sk)) {
34b78a12
RA
2739 err = __tipc_nl_add_sk_con(skb, tsk);
2740 if (err)
2741 goto attr_msg_cancel;
2742 } else if (!list_empty(&tsk->publications)) {
2743 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2744 goto attr_msg_cancel;
2745 }
2746 nla_nest_end(skb, attrs);
2747 genlmsg_end(skb, hdr);
2748
2749 return 0;
2750
2751attr_msg_cancel:
2752 nla_nest_cancel(skb, attrs);
2753genlmsg_cancel:
2754 genlmsg_cancel(skb, hdr);
2755msg_cancel:
2756 return -EMSGSIZE;
2757}
2758
2759int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2760{
2761 int err;
2762 struct tipc_sock *tsk;
07f6c4bc
YX
2763 const struct bucket_table *tbl;
2764 struct rhash_head *pos;
e05b31f4
YX
2765 struct net *net = sock_net(skb->sk);
2766 struct tipc_net *tn = net_generic(net, tipc_net_id);
d6e164e3
RA
2767 u32 tbl_id = cb->args[0];
2768 u32 prev_portid = cb->args[1];
34b78a12 2769
07f6c4bc 2770 rcu_read_lock();
e05b31f4 2771 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
d6e164e3
RA
2772 for (; tbl_id < tbl->size; tbl_id++) {
2773 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
07f6c4bc 2774 spin_lock_bh(&tsk->sk.sk_lock.slock);
d6e164e3
RA
2775 if (prev_portid && prev_portid != tsk->portid) {
2776 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2777 continue;
2778 }
2779
07f6c4bc 2780 err = __tipc_nl_add_sk(skb, cb, tsk);
d6e164e3
RA
2781 if (err) {
2782 prev_portid = tsk->portid;
2783 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2784 goto out;
2785 }
2786 prev_portid = 0;
07f6c4bc 2787 spin_unlock_bh(&tsk->sk.sk_lock.slock);
07f6c4bc 2788 }
34b78a12 2789 }
d6e164e3 2790out:
07f6c4bc 2791 rcu_read_unlock();
d6e164e3
RA
2792 cb->args[0] = tbl_id;
2793 cb->args[1] = prev_portid;
34b78a12
RA
2794
2795 return skb->len;
2796}
1a1a143d
RA
2797
2798/* Caller should hold socket lock for the passed tipc socket. */
d8182804
RA
2799static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2800 struct netlink_callback *cb,
2801 struct publication *publ)
1a1a143d
RA
2802{
2803 void *hdr;
2804 struct nlattr *attrs;
2805
2806 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
bfb3e5dd 2807 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
1a1a143d
RA
2808 if (!hdr)
2809 goto msg_cancel;
2810
2811 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2812 if (!attrs)
2813 goto genlmsg_cancel;
2814
2815 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2816 goto attr_msg_cancel;
2817 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2818 goto attr_msg_cancel;
2819 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2820 goto attr_msg_cancel;
2821 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2822 goto attr_msg_cancel;
2823
2824 nla_nest_end(skb, attrs);
2825 genlmsg_end(skb, hdr);
2826
2827 return 0;
2828
2829attr_msg_cancel:
2830 nla_nest_cancel(skb, attrs);
2831genlmsg_cancel:
2832 genlmsg_cancel(skb, hdr);
2833msg_cancel:
2834 return -EMSGSIZE;
2835}
2836
2837/* Caller should hold socket lock for the passed tipc socket. */
d8182804
RA
2838static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2839 struct netlink_callback *cb,
2840 struct tipc_sock *tsk, u32 *last_publ)
1a1a143d
RA
2841{
2842 int err;
2843 struct publication *p;
2844
2845 if (*last_publ) {
2846 list_for_each_entry(p, &tsk->publications, pport_list) {
2847 if (p->key == *last_publ)
2848 break;
2849 }
2850 if (p->key != *last_publ) {
2851 /* We never set seq or call nl_dump_check_consistent()
2852 * this means that setting prev_seq here will cause the
2853 * consistence check to fail in the netlink callback
2854 * handler. Resulting in the last NLMSG_DONE message
2855 * having the NLM_F_DUMP_INTR flag set.
2856 */
2857 cb->prev_seq = 1;
2858 *last_publ = 0;
2859 return -EPIPE;
2860 }
2861 } else {
2862 p = list_first_entry(&tsk->publications, struct publication,
2863 pport_list);
2864 }
2865
2866 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2867 err = __tipc_nl_add_sk_publ(skb, cb, p);
2868 if (err) {
2869 *last_publ = p->key;
2870 return err;
2871 }
2872 }
2873 *last_publ = 0;
2874
2875 return 0;
2876}
2877
2878int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2879{
2880 int err;
07f6c4bc 2881 u32 tsk_portid = cb->args[0];
1a1a143d
RA
2882 u32 last_publ = cb->args[1];
2883 u32 done = cb->args[2];
e05b31f4 2884 struct net *net = sock_net(skb->sk);
1a1a143d
RA
2885 struct tipc_sock *tsk;
2886
07f6c4bc 2887 if (!tsk_portid) {
1a1a143d
RA
2888 struct nlattr **attrs;
2889 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2890
2891 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2892 if (err)
2893 return err;
2894
45e093ae
RA
2895 if (!attrs[TIPC_NLA_SOCK])
2896 return -EINVAL;
2897
1a1a143d
RA
2898 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2899 attrs[TIPC_NLA_SOCK],
2900 tipc_nl_sock_policy);
2901 if (err)
2902 return err;
2903
2904 if (!sock[TIPC_NLA_SOCK_REF])
2905 return -EINVAL;
2906
07f6c4bc 2907 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
1a1a143d
RA
2908 }
2909
2910 if (done)
2911 return 0;
2912
e05b31f4 2913 tsk = tipc_sk_lookup(net, tsk_portid);
1a1a143d
RA
2914 if (!tsk)
2915 return -EINVAL;
2916
2917 lock_sock(&tsk->sk);
2918 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2919 if (!err)
2920 done = 1;
2921 release_sock(&tsk->sk);
07f6c4bc 2922 sock_put(&tsk->sk);
1a1a143d 2923
07f6c4bc 2924 cb->args[0] = tsk_portid;
1a1a143d
RA
2925 cb->args[1] = last_publ;
2926 cb->args[2] = done;
2927
2928 return skb->len;
2929}