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