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