blob: fbbac9ba2862fea71431f9a3c6ac1c7f0778cd3c [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*
2 * net/tipc/socket.c: TIPC socket API
3 *
4 * Copyright (c) 2001-2007, 2012-2017, Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
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.
19 *
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
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include <linux/rhashtable.h>
38#include <linux/sched/signal.h>
39
40#include "core.h"
41#include "name_table.h"
42#include "node.h"
43#include "link.h"
44#include "name_distr.h"
45#include "socket.h"
46#include "bcast.h"
47#include "netlink.h"
48#include "group.h"
David Brazdil0f672f62019-12-10 10:32:29 +000049#include "trace.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000050
David Brazdil0f672f62019-12-10 10:32:29 +000051#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000052#define CONN_PROBING_INTV msecs_to_jiffies(3600000) /* [ms] => 1 h */
53#define TIPC_FWD_MSG 1
54#define TIPC_MAX_PORT 0xffffffff
55#define TIPC_MIN_PORT 1
56#define TIPC_ACK_RATE 4 /* ACK at 1/4 of of rcv window size */
57
58enum {
59 TIPC_LISTEN = TCP_LISTEN,
60 TIPC_ESTABLISHED = TCP_ESTABLISHED,
61 TIPC_OPEN = TCP_CLOSE,
62 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
63 TIPC_CONNECTING = TCP_SYN_SENT,
64};
65
66struct sockaddr_pair {
67 struct sockaddr_tipc sock;
68 struct sockaddr_tipc member;
69};
70
71/**
72 * struct tipc_sock - TIPC socket structure
73 * @sk: socket - interacts with 'port' and with user via the socket API
74 * @conn_type: TIPC type used when connection was established
75 * @conn_instance: TIPC instance used when connection was established
76 * @published: non-zero if port has one or more associated names
77 * @max_pkt: maximum packet size "hint" used when building messages sent by port
78 * @portid: unique port identity in TIPC socket hash table
79 * @phdr: preformatted message header used when sending messages
80 * #cong_links: list of congested links
81 * @publications: list of publications for port
82 * @blocking_link: address of the congested link we are currently sleeping on
83 * @pub_count: total # of publications port has made during its lifetime
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084 * @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
86 * @cong_link_cnt: number of congested links
87 * @snt_unacked: # messages sent by socket, and not yet acked by peer
88 * @rcv_unacked: # messages read by user, but not yet acked back to peer
89 * @peer: 'connected' peer for dgram/rdm
90 * @node: hash table node
91 * @mc_method: cookie for use between socket and broadcast layer
92 * @rcu: rcu struct for tipc_sock
93 */
94struct tipc_sock {
95 struct sock sk;
96 u32 conn_type;
97 u32 conn_instance;
98 int published;
99 u32 max_pkt;
100 u32 portid;
101 struct tipc_msg phdr;
102 struct list_head cong_links;
103 struct list_head publications;
104 u32 pub_count;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105 atomic_t dupl_rcvcnt;
David Brazdil0f672f62019-12-10 10:32:29 +0000106 u16 conn_timeout;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000107 bool probe_unacked;
108 u16 cong_link_cnt;
109 u16 snt_unacked;
110 u16 snd_win;
111 u16 peer_caps;
112 u16 rcv_unacked;
113 u16 rcv_win;
114 struct sockaddr_tipc peer;
115 struct rhash_head node;
116 struct tipc_mc_method mc_method;
117 struct rcu_head rcu;
118 struct tipc_group *group;
119 bool group_is_open;
120};
121
122static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
123static void tipc_data_ready(struct sock *sk);
124static void tipc_write_space(struct sock *sk);
125static void tipc_sock_destruct(struct sock *sk);
126static int tipc_release(struct socket *sock);
127static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
128 bool kern);
129static void tipc_sk_timeout(struct timer_list *t);
130static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
131 struct tipc_name_seq const *seq);
132static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
133 struct tipc_name_seq const *seq);
134static int tipc_sk_leave(struct tipc_sock *tsk);
135static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
136static int tipc_sk_insert(struct tipc_sock *tsk);
137static void tipc_sk_remove(struct tipc_sock *tsk);
138static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
139static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
140
141static const struct proto_ops packet_ops;
142static const struct proto_ops stream_ops;
143static const struct proto_ops msg_ops;
144static struct proto tipc_proto;
145static const struct rhashtable_params tsk_rht_params;
146
147static u32 tsk_own_node(struct tipc_sock *tsk)
148{
149 return msg_prevnode(&tsk->phdr);
150}
151
152static u32 tsk_peer_node(struct tipc_sock *tsk)
153{
154 return msg_destnode(&tsk->phdr);
155}
156
157static u32 tsk_peer_port(struct tipc_sock *tsk)
158{
159 return msg_destport(&tsk->phdr);
160}
161
162static bool tsk_unreliable(struct tipc_sock *tsk)
163{
164 return msg_src_droppable(&tsk->phdr) != 0;
165}
166
167static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
168{
169 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
170}
171
172static bool tsk_unreturnable(struct tipc_sock *tsk)
173{
174 return msg_dest_droppable(&tsk->phdr) != 0;
175}
176
177static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
178{
179 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
180}
181
182static int tsk_importance(struct tipc_sock *tsk)
183{
184 return msg_importance(&tsk->phdr);
185}
186
187static int tsk_set_importance(struct tipc_sock *tsk, int imp)
188{
189 if (imp > TIPC_CRITICAL_IMPORTANCE)
190 return -EINVAL;
191 msg_set_importance(&tsk->phdr, (u32)imp);
192 return 0;
193}
194
195static struct tipc_sock *tipc_sk(const struct sock *sk)
196{
197 return container_of(sk, struct tipc_sock, sk);
198}
199
200static bool tsk_conn_cong(struct tipc_sock *tsk)
201{
202 return tsk->snt_unacked > tsk->snd_win;
203}
204
205static u16 tsk_blocks(int len)
206{
207 return ((len / FLOWCTL_BLK_SZ) + 1);
208}
209
210/* tsk_blocks(): translate a buffer size in bytes to number of
211 * advertisable blocks, taking into account the ratio truesize(len)/len
212 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
213 */
214static u16 tsk_adv_blocks(int len)
215{
216 return len / FLOWCTL_BLK_SZ / 4;
217}
218
219/* tsk_inc(): increment counter for sent or received data
220 * - If block based flow control is not supported by peer we
221 * fall back to message based ditto, incrementing the counter
222 */
223static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
224{
225 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
226 return ((msglen / FLOWCTL_BLK_SZ) + 1);
227 return 1;
228}
229
230/**
231 * tsk_advance_rx_queue - discard first buffer in socket receive queue
232 *
233 * Caller must hold socket lock
234 */
235static void tsk_advance_rx_queue(struct sock *sk)
236{
David Brazdil0f672f62019-12-10 10:32:29 +0000237 trace_tipc_sk_advance_rx(sk, NULL, TIPC_DUMP_SK_RCVQ, " ");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000238 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
239}
240
241/* tipc_sk_respond() : send response message back to sender
242 */
243static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
244{
245 u32 selector;
246 u32 dnode;
247 u32 onode = tipc_own_addr(sock_net(sk));
248
249 if (!tipc_msg_reverse(onode, &skb, err))
250 return;
251
David Brazdil0f672f62019-12-10 10:32:29 +0000252 trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE, "@sk_respond!");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000253 dnode = msg_destnode(buf_msg(skb));
254 selector = msg_origport(buf_msg(skb));
255 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
256}
257
258/**
259 * tsk_rej_rx_queue - reject all buffers in socket receive queue
260 *
261 * Caller must hold socket lock
262 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200263static void tsk_rej_rx_queue(struct sock *sk, int error)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000264{
265 struct sk_buff *skb;
266
267 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
Olivier Deprez0e641232021-09-23 10:07:05 +0200268 tipc_sk_respond(sk, skb, error);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000269}
270
271static bool tipc_sk_connected(struct sock *sk)
272{
273 return sk->sk_state == TIPC_ESTABLISHED;
274}
275
276/* tipc_sk_type_connectionless - check if the socket is datagram socket
277 * @sk: socket
278 *
279 * Returns true if connection less, false otherwise
280 */
281static bool tipc_sk_type_connectionless(struct sock *sk)
282{
283 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
284}
285
286/* tsk_peer_msg - verify if message was sent by connected port's peer
287 *
288 * Handles cases where the node's network address has changed from
289 * the default of <0.0.0> to its configured setting.
290 */
291static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
292{
293 struct sock *sk = &tsk->sk;
294 u32 self = tipc_own_addr(sock_net(sk));
295 u32 peer_port = tsk_peer_port(tsk);
296 u32 orig_node, peer_node;
297
298 if (unlikely(!tipc_sk_connected(sk)))
299 return false;
300
301 if (unlikely(msg_origport(msg) != peer_port))
302 return false;
303
304 orig_node = msg_orignode(msg);
305 peer_node = tsk_peer_node(tsk);
306
307 if (likely(orig_node == peer_node))
308 return true;
309
310 if (!orig_node && peer_node == self)
311 return true;
312
313 if (!peer_node && orig_node == self)
314 return true;
315
316 return false;
317}
318
319/* tipc_set_sk_state - set the sk_state of the socket
320 * @sk: socket
321 *
322 * Caller must hold socket lock
323 *
324 * Returns 0 on success, errno otherwise
325 */
326static int tipc_set_sk_state(struct sock *sk, int state)
327{
328 int oldsk_state = sk->sk_state;
329 int res = -EINVAL;
330
331 switch (state) {
332 case TIPC_OPEN:
333 res = 0;
334 break;
335 case TIPC_LISTEN:
336 case TIPC_CONNECTING:
337 if (oldsk_state == TIPC_OPEN)
338 res = 0;
339 break;
340 case TIPC_ESTABLISHED:
341 if (oldsk_state == TIPC_CONNECTING ||
342 oldsk_state == TIPC_OPEN)
343 res = 0;
344 break;
345 case TIPC_DISCONNECTING:
346 if (oldsk_state == TIPC_CONNECTING ||
347 oldsk_state == TIPC_ESTABLISHED)
348 res = 0;
349 break;
350 }
351
352 if (!res)
353 sk->sk_state = state;
354
355 return res;
356}
357
358static int tipc_sk_sock_err(struct socket *sock, long *timeout)
359{
360 struct sock *sk = sock->sk;
361 int err = sock_error(sk);
362 int typ = sock->type;
363
364 if (err)
365 return err;
366 if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
367 if (sk->sk_state == TIPC_DISCONNECTING)
368 return -EPIPE;
369 else if (!tipc_sk_connected(sk))
370 return -ENOTCONN;
371 }
372 if (!*timeout)
373 return -EAGAIN;
374 if (signal_pending(current))
375 return sock_intr_errno(*timeout);
376
377 return 0;
378}
379
380#define tipc_wait_for_cond(sock_, timeo_, condition_) \
381({ \
David Brazdil0f672f62019-12-10 10:32:29 +0000382 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000383 struct sock *sk_; \
384 int rc_; \
385 \
386 while ((rc_ = !(condition_))) { \
David Brazdil0f672f62019-12-10 10:32:29 +0000387 /* coupled with smp_wmb() in tipc_sk_proto_rcv() */ \
388 smp_rmb(); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000389 sk_ = (sock_)->sk; \
390 rc_ = tipc_sk_sock_err((sock_), timeo_); \
391 if (rc_) \
392 break; \
David Brazdil0f672f62019-12-10 10:32:29 +0000393 add_wait_queue(sk_sleep(sk_), &wait_); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000394 release_sock(sk_); \
395 *(timeo_) = wait_woken(&wait_, TASK_INTERRUPTIBLE, *(timeo_)); \
396 sched_annotate_sleep(); \
397 lock_sock(sk_); \
398 remove_wait_queue(sk_sleep(sk_), &wait_); \
399 } \
400 rc_; \
401})
402
403/**
404 * tipc_sk_create - create a TIPC socket
405 * @net: network namespace (must be default network)
406 * @sock: pre-allocated socket structure
407 * @protocol: protocol indicator (must be 0)
408 * @kern: caused by kernel or by userspace?
409 *
410 * This routine creates additional data structures used by the TIPC socket,
411 * initializes them, and links them together.
412 *
413 * Returns 0 on success, errno otherwise
414 */
415static int tipc_sk_create(struct net *net, struct socket *sock,
416 int protocol, int kern)
417{
418 const struct proto_ops *ops;
419 struct sock *sk;
420 struct tipc_sock *tsk;
421 struct tipc_msg *msg;
422
423 /* Validate arguments */
424 if (unlikely(protocol != 0))
425 return -EPROTONOSUPPORT;
426
427 switch (sock->type) {
428 case SOCK_STREAM:
429 ops = &stream_ops;
430 break;
431 case SOCK_SEQPACKET:
432 ops = &packet_ops;
433 break;
434 case SOCK_DGRAM:
435 case SOCK_RDM:
436 ops = &msg_ops;
437 break;
438 default:
439 return -EPROTOTYPE;
440 }
441
442 /* Allocate socket's protocol area */
443 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
444 if (sk == NULL)
445 return -ENOMEM;
446
447 tsk = tipc_sk(sk);
448 tsk->max_pkt = MAX_PKT_DEFAULT;
449 INIT_LIST_HEAD(&tsk->publications);
450 INIT_LIST_HEAD(&tsk->cong_links);
451 msg = &tsk->phdr;
452
453 /* Finish initializing socket data structures */
454 sock->ops = ops;
455 sock_init_data(sock, sk);
456 tipc_set_sk_state(sk, TIPC_OPEN);
457 if (tipc_sk_insert(tsk)) {
458 pr_warn("Socket create failed; port number exhausted\n");
459 return -EINVAL;
460 }
461
462 /* Ensure tsk is visible before we read own_addr. */
463 smp_mb();
464
465 tipc_msg_init(tipc_own_addr(net), msg, TIPC_LOW_IMPORTANCE,
466 TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
467
468 msg_set_origport(msg, tsk->portid);
469 timer_setup(&sk->sk_timer, tipc_sk_timeout, 0);
470 sk->sk_shutdown = 0;
471 sk->sk_backlog_rcv = tipc_sk_backlog_rcv;
472 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
473 sk->sk_data_ready = tipc_data_ready;
474 sk->sk_write_space = tipc_write_space;
475 sk->sk_destruct = tipc_sock_destruct;
476 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
477 tsk->group_is_open = true;
478 atomic_set(&tsk->dupl_rcvcnt, 0);
479
480 /* Start out with safe limits until we receive an advertised window */
481 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
482 tsk->rcv_win = tsk->snd_win;
483
484 if (tipc_sk_type_connectionless(sk)) {
485 tsk_set_unreturnable(tsk, true);
486 if (sock->type == SOCK_DGRAM)
487 tsk_set_unreliable(tsk, true);
488 }
David Brazdil0f672f62019-12-10 10:32:29 +0000489 __skb_queue_head_init(&tsk->mc_method.deferredq);
490 trace_tipc_sk_create(sk, NULL, TIPC_DUMP_NONE, " ");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000491 return 0;
492}
493
494static void tipc_sk_callback(struct rcu_head *head)
495{
496 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
497
498 sock_put(&tsk->sk);
499}
500
501/* Caller should hold socket lock for the socket. */
502static void __tipc_shutdown(struct socket *sock, int error)
503{
504 struct sock *sk = sock->sk;
505 struct tipc_sock *tsk = tipc_sk(sk);
506 struct net *net = sock_net(sk);
Olivier Deprez0e641232021-09-23 10:07:05 +0200507 long timeout = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000508 u32 dnode = tsk_peer_node(tsk);
509 struct sk_buff *skb;
510
511 /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
512 tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
513 !tsk_conn_cong(tsk)));
514
David Brazdil0f672f62019-12-10 10:32:29 +0000515 /* Remove any pending SYN message */
516 __skb_queue_purge(&sk->sk_write_queue);
517
Olivier Deprez0e641232021-09-23 10:07:05 +0200518 /* Remove partially received buffer if any */
519 skb = skb_peek(&sk->sk_receive_queue);
520 if (skb && TIPC_SKB_CB(skb)->bytes_read) {
521 __skb_unlink(skb, &sk->sk_receive_queue);
522 kfree_skb(skb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000523 }
524
Olivier Deprez0e641232021-09-23 10:07:05 +0200525 /* Reject all unreceived messages if connectionless */
526 if (tipc_sk_type_connectionless(sk)) {
527 tsk_rej_rx_queue(sk, error);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000528 return;
Olivier Deprez0e641232021-09-23 10:07:05 +0200529 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000530
Olivier Deprez0e641232021-09-23 10:07:05 +0200531 switch (sk->sk_state) {
532 case TIPC_CONNECTING:
533 case TIPC_ESTABLISHED:
534 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
535 tipc_node_remove_conn(net, dnode, tsk->portid);
536 /* Send a FIN+/- to its peer */
537 skb = __skb_dequeue(&sk->sk_receive_queue);
538 if (skb) {
539 __skb_queue_purge(&sk->sk_receive_queue);
540 tipc_sk_respond(sk, skb, error);
541 break;
542 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000543 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
544 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
545 tsk_own_node(tsk), tsk_peer_port(tsk),
546 tsk->portid, error);
547 if (skb)
548 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Olivier Deprez0e641232021-09-23 10:07:05 +0200549 break;
550 case TIPC_LISTEN:
551 /* Reject all SYN messages */
552 tsk_rej_rx_queue(sk, error);
553 break;
554 default:
555 __skb_queue_purge(&sk->sk_receive_queue);
556 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000557 }
558}
559
560/**
561 * tipc_release - destroy a TIPC socket
562 * @sock: socket to destroy
563 *
564 * This routine cleans up any messages that are still queued on the socket.
565 * For DGRAM and RDM socket types, all queued messages are rejected.
566 * For SEQPACKET and STREAM socket types, the first message is rejected
567 * and any others are discarded. (If the first message on a STREAM socket
568 * is partially-read, it is discarded and the next one is rejected instead.)
569 *
570 * NOTE: Rejected messages are not necessarily returned to the sender! They
571 * are returned or discarded according to the "destination droppable" setting
572 * specified for the message by the sender.
573 *
574 * Returns 0 on success, errno otherwise
575 */
576static int tipc_release(struct socket *sock)
577{
578 struct sock *sk = sock->sk;
579 struct tipc_sock *tsk;
580
581 /*
582 * Exit if socket isn't fully initialized (occurs when a failed accept()
583 * releases a pre-allocated child socket that was never used)
584 */
585 if (sk == NULL)
586 return 0;
587
588 tsk = tipc_sk(sk);
589 lock_sock(sk);
590
David Brazdil0f672f62019-12-10 10:32:29 +0000591 trace_tipc_sk_release(sk, NULL, TIPC_DUMP_ALL, " ");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000592 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
593 sk->sk_shutdown = SHUTDOWN_MASK;
594 tipc_sk_leave(tsk);
595 tipc_sk_withdraw(tsk, 0, NULL);
David Brazdil0f672f62019-12-10 10:32:29 +0000596 __skb_queue_purge(&tsk->mc_method.deferredq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000597 sk_stop_timer(sk, &sk->sk_timer);
598 tipc_sk_remove(tsk);
599
600 sock_orphan(sk);
601 /* Reject any messages that accumulated in backlog queue */
602 release_sock(sk);
603 tipc_dest_list_purge(&tsk->cong_links);
604 tsk->cong_link_cnt = 0;
605 call_rcu(&tsk->rcu, tipc_sk_callback);
606 sock->sk = NULL;
607
608 return 0;
609}
610
611/**
612 * tipc_bind - associate or disassocate TIPC name(s) with a socket
613 * @sock: socket structure
614 * @uaddr: socket address describing name(s) and desired operation
615 * @uaddr_len: size of socket address data structure
616 *
617 * Name and name sequence binding is indicated using a positive scope value;
618 * a negative scope value unbinds the specified name. Specifying no name
619 * (i.e. a socket address length of 0) unbinds all names from the socket.
620 *
621 * Returns 0 on success, errno otherwise
622 *
623 * NOTE: This routine doesn't need to take the socket lock since it doesn't
624 * access any non-constant socket information.
625 */
626static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
627 int uaddr_len)
628{
629 struct sock *sk = sock->sk;
630 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
631 struct tipc_sock *tsk = tipc_sk(sk);
632 int res = -EINVAL;
633
634 lock_sock(sk);
635 if (unlikely(!uaddr_len)) {
636 res = tipc_sk_withdraw(tsk, 0, NULL);
637 goto exit;
638 }
639 if (tsk->group) {
640 res = -EACCES;
641 goto exit;
642 }
643 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
644 res = -EINVAL;
645 goto exit;
646 }
647 if (addr->family != AF_TIPC) {
648 res = -EAFNOSUPPORT;
649 goto exit;
650 }
651
652 if (addr->addrtype == TIPC_ADDR_NAME)
653 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
654 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
655 res = -EAFNOSUPPORT;
656 goto exit;
657 }
658
659 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
660 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
661 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
662 res = -EACCES;
663 goto exit;
664 }
665
666 res = (addr->scope >= 0) ?
667 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
668 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
669exit:
670 release_sock(sk);
671 return res;
672}
673
674/**
675 * tipc_getname - get port ID of socket or peer socket
676 * @sock: socket structure
677 * @uaddr: area for returned socket address
678 * @uaddr_len: area for returned length of socket address
679 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
680 *
681 * Returns 0 on success, errno otherwise
682 *
683 * NOTE: This routine doesn't need to take the socket lock since it only
684 * accesses socket information that is unchanging (or which changes in
685 * a completely predictable manner).
686 */
687static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
688 int peer)
689{
690 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
691 struct sock *sk = sock->sk;
692 struct tipc_sock *tsk = tipc_sk(sk);
693
694 memset(addr, 0, sizeof(*addr));
695 if (peer) {
696 if ((!tipc_sk_connected(sk)) &&
697 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
698 return -ENOTCONN;
699 addr->addr.id.ref = tsk_peer_port(tsk);
700 addr->addr.id.node = tsk_peer_node(tsk);
701 } else {
702 addr->addr.id.ref = tsk->portid;
703 addr->addr.id.node = tipc_own_addr(sock_net(sk));
704 }
705
706 addr->addrtype = TIPC_ADDR_ID;
707 addr->family = AF_TIPC;
708 addr->scope = 0;
709 addr->addr.name.domain = 0;
710
711 return sizeof(*addr);
712}
713
714/**
715 * tipc_poll - read and possibly block on pollmask
716 * @file: file structure associated with the socket
717 * @sock: socket for which to calculate the poll bits
718 * @wait: ???
719 *
720 * Returns pollmask value
721 *
722 * COMMENTARY:
723 * It appears that the usual socket locking mechanisms are not useful here
724 * since the pollmask info is potentially out-of-date the moment this routine
725 * exits. TCP and other protocols seem to rely on higher level poll routines
726 * to handle any preventable race conditions, so TIPC will do the same ...
727 *
728 * IMPORTANT: The fact that a read or write operation is indicated does NOT
729 * imply that the operation will succeed, merely that it should be performed
730 * and will not block.
731 */
732static __poll_t tipc_poll(struct file *file, struct socket *sock,
733 poll_table *wait)
734{
735 struct sock *sk = sock->sk;
736 struct tipc_sock *tsk = tipc_sk(sk);
737 __poll_t revents = 0;
738
739 sock_poll_wait(file, sock, wait);
David Brazdil0f672f62019-12-10 10:32:29 +0000740 trace_tipc_sk_poll(sk, NULL, TIPC_DUMP_ALL, " ");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000741
742 if (sk->sk_shutdown & RCV_SHUTDOWN)
743 revents |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
744 if (sk->sk_shutdown == SHUTDOWN_MASK)
745 revents |= EPOLLHUP;
746
747 switch (sk->sk_state) {
748 case TIPC_ESTABLISHED:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000749 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
750 revents |= EPOLLOUT;
David Brazdil0f672f62019-12-10 10:32:29 +0000751 /* fall through */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000752 case TIPC_LISTEN:
David Brazdil0f672f62019-12-10 10:32:29 +0000753 case TIPC_CONNECTING:
754 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000755 revents |= EPOLLIN | EPOLLRDNORM;
756 break;
757 case TIPC_OPEN:
758 if (tsk->group_is_open && !tsk->cong_link_cnt)
759 revents |= EPOLLOUT;
760 if (!tipc_sk_type_connectionless(sk))
761 break;
David Brazdil0f672f62019-12-10 10:32:29 +0000762 if (skb_queue_empty_lockless(&sk->sk_receive_queue))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000763 break;
764 revents |= EPOLLIN | EPOLLRDNORM;
765 break;
766 case TIPC_DISCONNECTING:
767 revents = EPOLLIN | EPOLLRDNORM | EPOLLHUP;
768 break;
769 }
770 return revents;
771}
772
773/**
774 * tipc_sendmcast - send multicast message
775 * @sock: socket structure
776 * @seq: destination address
777 * @msg: message to send
778 * @dlen: length of data to send
779 * @timeout: timeout to wait for wakeup
780 *
781 * Called from function tipc_sendmsg(), which has done all sanity checks
782 * Returns the number of bytes sent on success, or errno
783 */
784static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
785 struct msghdr *msg, size_t dlen, long timeout)
786{
787 struct sock *sk = sock->sk;
788 struct tipc_sock *tsk = tipc_sk(sk);
789 struct tipc_msg *hdr = &tsk->phdr;
790 struct net *net = sock_net(sk);
791 int mtu = tipc_bcast_get_mtu(net);
792 struct tipc_mc_method *method = &tsk->mc_method;
793 struct sk_buff_head pkts;
794 struct tipc_nlist dsts;
795 int rc;
796
797 if (tsk->group)
798 return -EACCES;
799
800 /* Block or return if any destination link is congested */
801 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
802 if (unlikely(rc))
803 return rc;
804
805 /* Lookup destination nodes */
806 tipc_nlist_init(&dsts, tipc_own_addr(net));
807 tipc_nametbl_lookup_dst_nodes(net, seq->type, seq->lower,
808 seq->upper, &dsts);
809 if (!dsts.local && !dsts.remote)
810 return -EHOSTUNREACH;
811
812 /* Build message header */
813 msg_set_type(hdr, TIPC_MCAST_MSG);
814 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
815 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
816 msg_set_destport(hdr, 0);
817 msg_set_destnode(hdr, 0);
818 msg_set_nametype(hdr, seq->type);
819 msg_set_namelower(hdr, seq->lower);
820 msg_set_nameupper(hdr, seq->upper);
821
822 /* Build message as chain of buffers */
David Brazdil0f672f62019-12-10 10:32:29 +0000823 __skb_queue_head_init(&pkts);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000824 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
825
826 /* Send message if build was successful */
David Brazdil0f672f62019-12-10 10:32:29 +0000827 if (unlikely(rc == dlen)) {
828 trace_tipc_sk_sendmcast(sk, skb_peek(&pkts),
829 TIPC_DUMP_SK_SNDQ, " ");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000830 rc = tipc_mcast_xmit(net, &pkts, method, &dsts,
831 &tsk->cong_link_cnt);
David Brazdil0f672f62019-12-10 10:32:29 +0000832 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000833
834 tipc_nlist_purge(&dsts);
835
836 return rc ? rc : dlen;
837}
838
839/**
840 * tipc_send_group_msg - send a message to a member in the group
841 * @net: network namespace
842 * @m: message to send
843 * @mb: group member
844 * @dnode: destination node
845 * @dport: destination port
846 * @dlen: total length of message data
847 */
848static int tipc_send_group_msg(struct net *net, struct tipc_sock *tsk,
849 struct msghdr *m, struct tipc_member *mb,
850 u32 dnode, u32 dport, int dlen)
851{
852 u16 bc_snd_nxt = tipc_group_bc_snd_nxt(tsk->group);
853 struct tipc_mc_method *method = &tsk->mc_method;
854 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
855 struct tipc_msg *hdr = &tsk->phdr;
856 struct sk_buff_head pkts;
857 int mtu, rc;
858
859 /* Complete message header */
860 msg_set_type(hdr, TIPC_GRP_UCAST_MSG);
861 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
862 msg_set_destport(hdr, dport);
863 msg_set_destnode(hdr, dnode);
864 msg_set_grp_bc_seqno(hdr, bc_snd_nxt);
865
866 /* Build message as chain of buffers */
David Brazdil0f672f62019-12-10 10:32:29 +0000867 __skb_queue_head_init(&pkts);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000868 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
869 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
870 if (unlikely(rc != dlen))
871 return rc;
872
873 /* Send message */
874 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
875 if (unlikely(rc == -ELINKCONG)) {
876 tipc_dest_push(&tsk->cong_links, dnode, 0);
877 tsk->cong_link_cnt++;
878 }
879
880 /* Update send window */
881 tipc_group_update_member(mb, blks);
882
883 /* A broadcast sent within next EXPIRE period must follow same path */
884 method->rcast = true;
885 method->mandatory = true;
886 return dlen;
887}
888
889/**
890 * tipc_send_group_unicast - send message to a member in the group
891 * @sock: socket structure
892 * @m: message to send
893 * @dlen: total length of message data
894 * @timeout: timeout to wait for wakeup
895 *
896 * Called from function tipc_sendmsg(), which has done all sanity checks
897 * Returns the number of bytes sent on success, or errno
898 */
899static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m,
900 int dlen, long timeout)
901{
902 struct sock *sk = sock->sk;
903 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
904 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
905 struct tipc_sock *tsk = tipc_sk(sk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000906 struct net *net = sock_net(sk);
907 struct tipc_member *mb = NULL;
908 u32 node, port;
909 int rc;
910
911 node = dest->addr.id.node;
912 port = dest->addr.id.ref;
913 if (!port && !node)
914 return -EHOSTUNREACH;
915
916 /* Block or return if destination link or member is congested */
917 rc = tipc_wait_for_cond(sock, &timeout,
918 !tipc_dest_find(&tsk->cong_links, node, 0) &&
David Brazdil0f672f62019-12-10 10:32:29 +0000919 tsk->group &&
920 !tipc_group_cong(tsk->group, node, port, blks,
921 &mb));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000922 if (unlikely(rc))
923 return rc;
924
925 if (unlikely(!mb))
926 return -EHOSTUNREACH;
927
928 rc = tipc_send_group_msg(net, tsk, m, mb, node, port, dlen);
929
930 return rc ? rc : dlen;
931}
932
933/**
934 * tipc_send_group_anycast - send message to any member with given identity
935 * @sock: socket structure
936 * @m: message to send
937 * @dlen: total length of message data
938 * @timeout: timeout to wait for wakeup
939 *
940 * Called from function tipc_sendmsg(), which has done all sanity checks
941 * Returns the number of bytes sent on success, or errno
942 */
943static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m,
944 int dlen, long timeout)
945{
946 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
947 struct sock *sk = sock->sk;
948 struct tipc_sock *tsk = tipc_sk(sk);
949 struct list_head *cong_links = &tsk->cong_links;
950 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000951 struct tipc_msg *hdr = &tsk->phdr;
952 struct tipc_member *first = NULL;
953 struct tipc_member *mbr = NULL;
954 struct net *net = sock_net(sk);
955 u32 node, port, exclude;
956 struct list_head dsts;
957 u32 type, inst, scope;
958 int lookups = 0;
959 int dstcnt, rc;
960 bool cong;
961
962 INIT_LIST_HEAD(&dsts);
963
964 type = msg_nametype(hdr);
965 inst = dest->addr.name.name.instance;
966 scope = msg_lookup_scope(hdr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000967
968 while (++lookups < 4) {
David Brazdil0f672f62019-12-10 10:32:29 +0000969 exclude = tipc_group_exclude(tsk->group);
970
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000971 first = NULL;
972
973 /* Look for a non-congested destination member, if any */
974 while (1) {
975 if (!tipc_nametbl_lookup(net, type, inst, scope, &dsts,
976 &dstcnt, exclude, false))
977 return -EHOSTUNREACH;
978 tipc_dest_pop(&dsts, &node, &port);
David Brazdil0f672f62019-12-10 10:32:29 +0000979 cong = tipc_group_cong(tsk->group, node, port, blks,
980 &mbr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000981 if (!cong)
982 break;
983 if (mbr == first)
984 break;
985 if (!first)
986 first = mbr;
987 }
988
989 /* Start over if destination was not in member list */
990 if (unlikely(!mbr))
991 continue;
992
993 if (likely(!cong && !tipc_dest_find(cong_links, node, 0)))
994 break;
995
996 /* Block or return if destination link or member is congested */
997 rc = tipc_wait_for_cond(sock, &timeout,
998 !tipc_dest_find(cong_links, node, 0) &&
David Brazdil0f672f62019-12-10 10:32:29 +0000999 tsk->group &&
1000 !tipc_group_cong(tsk->group, node, port,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001001 blks, &mbr));
1002 if (unlikely(rc))
1003 return rc;
1004
1005 /* Send, unless destination disappeared while waiting */
1006 if (likely(mbr))
1007 break;
1008 }
1009
1010 if (unlikely(lookups >= 4))
1011 return -EHOSTUNREACH;
1012
1013 rc = tipc_send_group_msg(net, tsk, m, mbr, node, port, dlen);
1014
1015 return rc ? rc : dlen;
1016}
1017
1018/**
1019 * tipc_send_group_bcast - send message to all members in communication group
1020 * @sk: socket structure
1021 * @m: message to send
1022 * @dlen: total length of message data
1023 * @timeout: timeout to wait for wakeup
1024 *
1025 * Called from function tipc_sendmsg(), which has done all sanity checks
1026 * Returns the number of bytes sent on success, or errno
1027 */
1028static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m,
1029 int dlen, long timeout)
1030{
1031 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1032 struct sock *sk = sock->sk;
1033 struct net *net = sock_net(sk);
1034 struct tipc_sock *tsk = tipc_sk(sk);
David Brazdil0f672f62019-12-10 10:32:29 +00001035 struct tipc_nlist *dsts;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001036 struct tipc_mc_method *method = &tsk->mc_method;
1037 bool ack = method->mandatory && method->rcast;
1038 int blks = tsk_blocks(MCAST_H_SIZE + dlen);
1039 struct tipc_msg *hdr = &tsk->phdr;
1040 int mtu = tipc_bcast_get_mtu(net);
1041 struct sk_buff_head pkts;
1042 int rc = -EHOSTUNREACH;
1043
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001044 /* Block or return if any destination link or member is congested */
David Brazdil0f672f62019-12-10 10:32:29 +00001045 rc = tipc_wait_for_cond(sock, &timeout,
1046 !tsk->cong_link_cnt && tsk->group &&
1047 !tipc_group_bc_cong(tsk->group, blks));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001048 if (unlikely(rc))
1049 return rc;
1050
David Brazdil0f672f62019-12-10 10:32:29 +00001051 dsts = tipc_group_dests(tsk->group);
1052 if (!dsts->local && !dsts->remote)
1053 return -EHOSTUNREACH;
1054
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001055 /* Complete message header */
1056 if (dest) {
1057 msg_set_type(hdr, TIPC_GRP_MCAST_MSG);
1058 msg_set_nameinst(hdr, dest->addr.name.name.instance);
1059 } else {
1060 msg_set_type(hdr, TIPC_GRP_BCAST_MSG);
1061 msg_set_nameinst(hdr, 0);
1062 }
1063 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
1064 msg_set_destport(hdr, 0);
1065 msg_set_destnode(hdr, 0);
David Brazdil0f672f62019-12-10 10:32:29 +00001066 msg_set_grp_bc_seqno(hdr, tipc_group_bc_snd_nxt(tsk->group));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001067
1068 /* Avoid getting stuck with repeated forced replicasts */
1069 msg_set_grp_bc_ack_req(hdr, ack);
1070
1071 /* Build message as chain of buffers */
David Brazdil0f672f62019-12-10 10:32:29 +00001072 __skb_queue_head_init(&pkts);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001073 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1074 if (unlikely(rc != dlen))
1075 return rc;
1076
1077 /* Send message */
1078 rc = tipc_mcast_xmit(net, &pkts, method, dsts, &tsk->cong_link_cnt);
1079 if (unlikely(rc))
1080 return rc;
1081
1082 /* Update broadcast sequence number and send windows */
1083 tipc_group_update_bc_members(tsk->group, blks, ack);
1084
1085 /* Broadcast link is now free to choose method for next broadcast */
1086 method->mandatory = false;
1087 method->expires = jiffies;
1088
1089 return dlen;
1090}
1091
1092/**
1093 * tipc_send_group_mcast - send message to all members with given identity
1094 * @sock: socket structure
1095 * @m: message to send
1096 * @dlen: total length of message data
1097 * @timeout: timeout to wait for wakeup
1098 *
1099 * Called from function tipc_sendmsg(), which has done all sanity checks
1100 * Returns the number of bytes sent on success, or errno
1101 */
1102static int tipc_send_group_mcast(struct socket *sock, struct msghdr *m,
1103 int dlen, long timeout)
1104{
1105 struct sock *sk = sock->sk;
1106 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1107 struct tipc_sock *tsk = tipc_sk(sk);
1108 struct tipc_group *grp = tsk->group;
1109 struct tipc_msg *hdr = &tsk->phdr;
1110 struct net *net = sock_net(sk);
1111 u32 type, inst, scope, exclude;
1112 struct list_head dsts;
1113 u32 dstcnt;
1114
1115 INIT_LIST_HEAD(&dsts);
1116
1117 type = msg_nametype(hdr);
1118 inst = dest->addr.name.name.instance;
1119 scope = msg_lookup_scope(hdr);
1120 exclude = tipc_group_exclude(grp);
1121
1122 if (!tipc_nametbl_lookup(net, type, inst, scope, &dsts,
1123 &dstcnt, exclude, true))
1124 return -EHOSTUNREACH;
1125
1126 if (dstcnt == 1) {
1127 tipc_dest_pop(&dsts, &dest->addr.id.node, &dest->addr.id.ref);
1128 return tipc_send_group_unicast(sock, m, dlen, timeout);
1129 }
1130
1131 tipc_dest_list_purge(&dsts);
1132 return tipc_send_group_bcast(sock, m, dlen, timeout);
1133}
1134
1135/**
1136 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
1137 * @arrvq: queue with arriving messages, to be cloned after destination lookup
1138 * @inputq: queue with cloned messages, delivered to socket after dest lookup
1139 *
1140 * Multi-threaded: parallel calls with reference to same queues may occur
1141 */
1142void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
1143 struct sk_buff_head *inputq)
1144{
1145 u32 self = tipc_own_addr(net);
1146 u32 type, lower, upper, scope;
1147 struct sk_buff *skb, *_skb;
1148 u32 portid, onode;
1149 struct sk_buff_head tmpq;
1150 struct list_head dports;
1151 struct tipc_msg *hdr;
1152 int user, mtyp, hlen;
1153 bool exact;
1154
1155 __skb_queue_head_init(&tmpq);
1156 INIT_LIST_HEAD(&dports);
1157
1158 skb = tipc_skb_peek(arrvq, &inputq->lock);
1159 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
1160 hdr = buf_msg(skb);
1161 user = msg_user(hdr);
1162 mtyp = msg_type(hdr);
1163 hlen = skb_headroom(skb) + msg_hdr_sz(hdr);
1164 onode = msg_orignode(hdr);
1165 type = msg_nametype(hdr);
1166
1167 if (mtyp == TIPC_GRP_UCAST_MSG || user == GROUP_PROTOCOL) {
1168 spin_lock_bh(&inputq->lock);
1169 if (skb_peek(arrvq) == skb) {
1170 __skb_dequeue(arrvq);
1171 __skb_queue_tail(inputq, skb);
1172 }
1173 kfree_skb(skb);
1174 spin_unlock_bh(&inputq->lock);
1175 continue;
1176 }
1177
1178 /* Group messages require exact scope match */
1179 if (msg_in_group(hdr)) {
1180 lower = 0;
1181 upper = ~0;
1182 scope = msg_lookup_scope(hdr);
1183 exact = true;
1184 } else {
1185 /* TIPC_NODE_SCOPE means "any scope" in this context */
1186 if (onode == self)
1187 scope = TIPC_NODE_SCOPE;
1188 else
1189 scope = TIPC_CLUSTER_SCOPE;
1190 exact = false;
1191 lower = msg_namelower(hdr);
1192 upper = msg_nameupper(hdr);
1193 }
1194
1195 /* Create destination port list: */
1196 tipc_nametbl_mc_lookup(net, type, lower, upper,
1197 scope, exact, &dports);
1198
1199 /* Clone message per destination */
1200 while (tipc_dest_pop(&dports, NULL, &portid)) {
1201 _skb = __pskb_copy(skb, hlen, GFP_ATOMIC);
1202 if (_skb) {
1203 msg_set_destport(buf_msg(_skb), portid);
1204 __skb_queue_tail(&tmpq, _skb);
1205 continue;
1206 }
1207 pr_warn("Failed to clone mcast rcv buffer\n");
1208 }
1209 /* Append to inputq if not already done by other thread */
1210 spin_lock_bh(&inputq->lock);
1211 if (skb_peek(arrvq) == skb) {
1212 skb_queue_splice_tail_init(&tmpq, inputq);
Olivier Deprez0e641232021-09-23 10:07:05 +02001213 /* Decrease the skb's refcnt as increasing in the
1214 * function tipc_skb_peek
1215 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001216 kfree_skb(__skb_dequeue(arrvq));
1217 }
1218 spin_unlock_bh(&inputq->lock);
1219 __skb_queue_purge(&tmpq);
1220 kfree_skb(skb);
1221 }
1222 tipc_sk_rcv(net, inputq);
1223}
1224
1225/**
1226 * tipc_sk_conn_proto_rcv - receive a connection mng protocol message
1227 * @tsk: receiving socket
1228 * @skb: pointer to message buffer.
1229 */
1230static void tipc_sk_conn_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
1231 struct sk_buff_head *inputq,
1232 struct sk_buff_head *xmitq)
1233{
1234 struct tipc_msg *hdr = buf_msg(skb);
1235 u32 onode = tsk_own_node(tsk);
1236 struct sock *sk = &tsk->sk;
1237 int mtyp = msg_type(hdr);
1238 bool conn_cong;
1239
1240 /* Ignore if connection cannot be validated: */
David Brazdil0f672f62019-12-10 10:32:29 +00001241 if (!tsk_peer_msg(tsk, hdr)) {
1242 trace_tipc_sk_drop_msg(sk, skb, TIPC_DUMP_NONE, "@proto_rcv!");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001243 goto exit;
David Brazdil0f672f62019-12-10 10:32:29 +00001244 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001245
1246 if (unlikely(msg_errcode(hdr))) {
1247 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1248 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
1249 tsk_peer_port(tsk));
1250 sk->sk_state_change(sk);
1251
1252 /* State change is ignored if socket already awake,
1253 * - convert msg to abort msg and add to inqueue
1254 */
1255 msg_set_user(hdr, TIPC_CRITICAL_IMPORTANCE);
1256 msg_set_type(hdr, TIPC_CONN_MSG);
1257 msg_set_size(hdr, BASIC_H_SIZE);
1258 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
1259 __skb_queue_tail(inputq, skb);
1260 return;
1261 }
1262
1263 tsk->probe_unacked = false;
1264
1265 if (mtyp == CONN_PROBE) {
1266 msg_set_type(hdr, CONN_PROBE_REPLY);
1267 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
1268 __skb_queue_tail(xmitq, skb);
1269 return;
1270 } else if (mtyp == CONN_ACK) {
1271 conn_cong = tsk_conn_cong(tsk);
1272 tsk->snt_unacked -= msg_conn_ack(hdr);
1273 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1274 tsk->snd_win = msg_adv_win(hdr);
1275 if (conn_cong)
1276 sk->sk_write_space(sk);
1277 } else if (mtyp != CONN_PROBE_REPLY) {
1278 pr_warn("Received unknown CONN_PROTO msg\n");
1279 }
1280exit:
1281 kfree_skb(skb);
1282}
1283
1284/**
1285 * tipc_sendmsg - send message in connectionless manner
1286 * @sock: socket structure
1287 * @m: message to send
1288 * @dsz: amount of user data to be sent
1289 *
1290 * Message must have an destination specified explicitly.
1291 * Used for SOCK_RDM and SOCK_DGRAM messages,
1292 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
1293 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
1294 *
1295 * Returns the number of bytes sent on success, or errno otherwise
1296 */
1297static int tipc_sendmsg(struct socket *sock,
1298 struct msghdr *m, size_t dsz)
1299{
1300 struct sock *sk = sock->sk;
1301 int ret;
1302
1303 lock_sock(sk);
1304 ret = __tipc_sendmsg(sock, m, dsz);
1305 release_sock(sk);
1306
1307 return ret;
1308}
1309
1310static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
1311{
1312 struct sock *sk = sock->sk;
1313 struct net *net = sock_net(sk);
1314 struct tipc_sock *tsk = tipc_sk(sk);
1315 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1316 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1317 struct list_head *clinks = &tsk->cong_links;
1318 bool syn = !tipc_sk_type_connectionless(sk);
1319 struct tipc_group *grp = tsk->group;
1320 struct tipc_msg *hdr = &tsk->phdr;
1321 struct tipc_name_seq *seq;
1322 struct sk_buff_head pkts;
Olivier Deprez0e641232021-09-23 10:07:05 +02001323 u32 dport = 0, dnode = 0;
1324 u32 type = 0, inst = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001325 int mtu, rc;
1326
1327 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
1328 return -EMSGSIZE;
1329
1330 if (likely(dest)) {
1331 if (unlikely(m->msg_namelen < sizeof(*dest)))
1332 return -EINVAL;
1333 if (unlikely(dest->family != AF_TIPC))
1334 return -EINVAL;
1335 }
1336
1337 if (grp) {
1338 if (!dest)
1339 return tipc_send_group_bcast(sock, m, dlen, timeout);
1340 if (dest->addrtype == TIPC_ADDR_NAME)
1341 return tipc_send_group_anycast(sock, m, dlen, timeout);
1342 if (dest->addrtype == TIPC_ADDR_ID)
1343 return tipc_send_group_unicast(sock, m, dlen, timeout);
1344 if (dest->addrtype == TIPC_ADDR_MCAST)
1345 return tipc_send_group_mcast(sock, m, dlen, timeout);
1346 return -EINVAL;
1347 }
1348
1349 if (unlikely(!dest)) {
1350 dest = &tsk->peer;
David Brazdil0f672f62019-12-10 10:32:29 +00001351 if (!syn && dest->family != AF_TIPC)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001352 return -EDESTADDRREQ;
1353 }
1354
1355 if (unlikely(syn)) {
1356 if (sk->sk_state == TIPC_LISTEN)
1357 return -EPIPE;
1358 if (sk->sk_state != TIPC_OPEN)
1359 return -EISCONN;
1360 if (tsk->published)
1361 return -EOPNOTSUPP;
1362 if (dest->addrtype == TIPC_ADDR_NAME) {
1363 tsk->conn_type = dest->addr.name.name.type;
1364 tsk->conn_instance = dest->addr.name.name.instance;
1365 }
David Brazdil0f672f62019-12-10 10:32:29 +00001366 msg_set_syn(hdr, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001367 }
1368
1369 seq = &dest->addr.nameseq;
1370 if (dest->addrtype == TIPC_ADDR_MCAST)
1371 return tipc_sendmcast(sock, seq, m, dlen, timeout);
1372
1373 if (dest->addrtype == TIPC_ADDR_NAME) {
1374 type = dest->addr.name.name.type;
1375 inst = dest->addr.name.name.instance;
1376 dnode = dest->addr.name.domain;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001377 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001378 if (unlikely(!dport && !dnode))
1379 return -EHOSTUNREACH;
1380 } else if (dest->addrtype == TIPC_ADDR_ID) {
1381 dnode = dest->addr.id.node;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001382 } else {
1383 return -EINVAL;
1384 }
1385
1386 /* Block or return if destination link is congested */
1387 rc = tipc_wait_for_cond(sock, &timeout,
1388 !tipc_dest_find(clinks, dnode, 0));
1389 if (unlikely(rc))
1390 return rc;
1391
Olivier Deprez0e641232021-09-23 10:07:05 +02001392 if (dest->addrtype == TIPC_ADDR_NAME) {
1393 msg_set_type(hdr, TIPC_NAMED_MSG);
1394 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
1395 msg_set_nametype(hdr, type);
1396 msg_set_nameinst(hdr, inst);
1397 msg_set_lookup_scope(hdr, tipc_node2scope(dnode));
1398 msg_set_destnode(hdr, dnode);
1399 msg_set_destport(hdr, dport);
1400 } else { /* TIPC_ADDR_ID */
1401 msg_set_type(hdr, TIPC_DIRECT_MSG);
1402 msg_set_lookup_scope(hdr, 0);
1403 msg_set_destnode(hdr, dnode);
1404 msg_set_destport(hdr, dest->addr.id.ref);
1405 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
1406 }
1407
David Brazdil0f672f62019-12-10 10:32:29 +00001408 __skb_queue_head_init(&pkts);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001409 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
1410 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1411 if (unlikely(rc != dlen))
1412 return rc;
Olivier Deprez0e641232021-09-23 10:07:05 +02001413 if (unlikely(syn && !tipc_msg_skb_clone(&pkts, &sk->sk_write_queue))) {
1414 __skb_queue_purge(&pkts);
David Brazdil0f672f62019-12-10 10:32:29 +00001415 return -ENOMEM;
Olivier Deprez0e641232021-09-23 10:07:05 +02001416 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001417
David Brazdil0f672f62019-12-10 10:32:29 +00001418 trace_tipc_sk_sendmsg(sk, skb_peek(&pkts), TIPC_DUMP_SK_SNDQ, " ");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001419 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1420 if (unlikely(rc == -ELINKCONG)) {
1421 tipc_dest_push(clinks, dnode, 0);
1422 tsk->cong_link_cnt++;
1423 rc = 0;
1424 }
1425
1426 if (unlikely(syn && !rc))
1427 tipc_set_sk_state(sk, TIPC_CONNECTING);
1428
1429 return rc ? rc : dlen;
1430}
1431
1432/**
1433 * tipc_sendstream - send stream-oriented data
1434 * @sock: socket structure
1435 * @m: data to send
1436 * @dsz: total length of data to be transmitted
1437 *
1438 * Used for SOCK_STREAM data.
1439 *
1440 * Returns the number of bytes sent on success (or partial success),
1441 * or errno if no data sent
1442 */
1443static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
1444{
1445 struct sock *sk = sock->sk;
1446 int ret;
1447
1448 lock_sock(sk);
1449 ret = __tipc_sendstream(sock, m, dsz);
1450 release_sock(sk);
1451
1452 return ret;
1453}
1454
1455static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
1456{
1457 struct sock *sk = sock->sk;
1458 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1459 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1460 struct tipc_sock *tsk = tipc_sk(sk);
1461 struct tipc_msg *hdr = &tsk->phdr;
1462 struct net *net = sock_net(sk);
1463 struct sk_buff_head pkts;
1464 u32 dnode = tsk_peer_node(tsk);
1465 int send, sent = 0;
1466 int rc = 0;
1467
David Brazdil0f672f62019-12-10 10:32:29 +00001468 __skb_queue_head_init(&pkts);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001469
1470 if (unlikely(dlen > INT_MAX))
1471 return -EMSGSIZE;
1472
1473 /* Handle implicit connection setup */
1474 if (unlikely(dest)) {
1475 rc = __tipc_sendmsg(sock, m, dlen);
1476 if (dlen && dlen == rc) {
1477 tsk->peer_caps = tipc_node_get_capabilities(net, dnode);
1478 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
1479 }
1480 return rc;
1481 }
1482
1483 do {
1484 rc = tipc_wait_for_cond(sock, &timeout,
1485 (!tsk->cong_link_cnt &&
1486 !tsk_conn_cong(tsk) &&
1487 tipc_sk_connected(sk)));
1488 if (unlikely(rc))
1489 break;
1490
1491 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
1492 rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
1493 if (unlikely(rc != send))
1494 break;
1495
David Brazdil0f672f62019-12-10 10:32:29 +00001496 trace_tipc_sk_sendstream(sk, skb_peek(&pkts),
1497 TIPC_DUMP_SK_SNDQ, " ");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001498 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1499 if (unlikely(rc == -ELINKCONG)) {
1500 tsk->cong_link_cnt = 1;
1501 rc = 0;
1502 }
1503 if (likely(!rc)) {
1504 tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
1505 sent += send;
1506 }
1507 } while (sent < dlen && !rc);
1508
1509 return sent ? sent : rc;
1510}
1511
1512/**
1513 * tipc_send_packet - send a connection-oriented message
1514 * @sock: socket structure
1515 * @m: message to send
1516 * @dsz: length of data to be transmitted
1517 *
1518 * Used for SOCK_SEQPACKET messages.
1519 *
1520 * Returns the number of bytes sent on success, or errno otherwise
1521 */
1522static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
1523{
1524 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1525 return -EMSGSIZE;
1526
1527 return tipc_sendstream(sock, m, dsz);
1528}
1529
1530/* tipc_sk_finish_conn - complete the setup of a connection
1531 */
1532static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
1533 u32 peer_node)
1534{
1535 struct sock *sk = &tsk->sk;
1536 struct net *net = sock_net(sk);
1537 struct tipc_msg *msg = &tsk->phdr;
1538
David Brazdil0f672f62019-12-10 10:32:29 +00001539 msg_set_syn(msg, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001540 msg_set_destnode(msg, peer_node);
1541 msg_set_destport(msg, peer_port);
1542 msg_set_type(msg, TIPC_CONN_MSG);
1543 msg_set_lookup_scope(msg, 0);
1544 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1545
1546 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
1547 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
1548 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1549 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
1550 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
David Brazdil0f672f62019-12-10 10:32:29 +00001551 __skb_queue_purge(&sk->sk_write_queue);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001552 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1553 return;
1554
1555 /* Fall back to message based flow control */
1556 tsk->rcv_win = FLOWCTL_MSG_WIN;
1557 tsk->snd_win = FLOWCTL_MSG_WIN;
1558}
1559
1560/**
1561 * tipc_sk_set_orig_addr - capture sender's address for received message
1562 * @m: descriptor for message info
1563 * @hdr: received message header
1564 *
1565 * Note: Address is not captured if not requested by receiver.
1566 */
1567static void tipc_sk_set_orig_addr(struct msghdr *m, struct sk_buff *skb)
1568{
1569 DECLARE_SOCKADDR(struct sockaddr_pair *, srcaddr, m->msg_name);
1570 struct tipc_msg *hdr = buf_msg(skb);
1571
1572 if (!srcaddr)
1573 return;
1574
1575 srcaddr->sock.family = AF_TIPC;
1576 srcaddr->sock.addrtype = TIPC_ADDR_ID;
1577 srcaddr->sock.scope = 0;
1578 srcaddr->sock.addr.id.ref = msg_origport(hdr);
1579 srcaddr->sock.addr.id.node = msg_orignode(hdr);
1580 srcaddr->sock.addr.name.domain = 0;
1581 m->msg_namelen = sizeof(struct sockaddr_tipc);
1582
1583 if (!msg_in_group(hdr))
1584 return;
1585
1586 /* Group message users may also want to know sending member's id */
1587 srcaddr->member.family = AF_TIPC;
1588 srcaddr->member.addrtype = TIPC_ADDR_NAME;
1589 srcaddr->member.scope = 0;
1590 srcaddr->member.addr.name.name.type = msg_nametype(hdr);
1591 srcaddr->member.addr.name.name.instance = TIPC_SKB_CB(skb)->orig_member;
1592 srcaddr->member.addr.name.domain = 0;
1593 m->msg_namelen = sizeof(*srcaddr);
1594}
1595
1596/**
1597 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
1598 * @m: descriptor for message info
1599 * @skb: received message buffer
1600 * @tsk: TIPC port associated with message
1601 *
1602 * Note: Ancillary data is not captured if not requested by receiver.
1603 *
1604 * Returns 0 if successful, otherwise errno
1605 */
1606static int tipc_sk_anc_data_recv(struct msghdr *m, struct sk_buff *skb,
1607 struct tipc_sock *tsk)
1608{
1609 struct tipc_msg *msg;
1610 u32 anc_data[3];
1611 u32 err;
1612 u32 dest_type;
1613 int has_name;
1614 int res;
1615
1616 if (likely(m->msg_controllen == 0))
1617 return 0;
1618 msg = buf_msg(skb);
1619
1620 /* Optionally capture errored message object(s) */
1621 err = msg ? msg_errcode(msg) : 0;
1622 if (unlikely(err)) {
1623 anc_data[0] = err;
1624 anc_data[1] = msg_data_sz(msg);
1625 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1626 if (res)
1627 return res;
1628 if (anc_data[1]) {
1629 if (skb_linearize(skb))
1630 return -ENOMEM;
1631 msg = buf_msg(skb);
1632 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1633 msg_data(msg));
1634 if (res)
1635 return res;
1636 }
1637 }
1638
1639 /* Optionally capture message destination object */
1640 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1641 switch (dest_type) {
1642 case TIPC_NAMED_MSG:
1643 has_name = 1;
1644 anc_data[0] = msg_nametype(msg);
1645 anc_data[1] = msg_namelower(msg);
1646 anc_data[2] = msg_namelower(msg);
1647 break;
1648 case TIPC_MCAST_MSG:
1649 has_name = 1;
1650 anc_data[0] = msg_nametype(msg);
1651 anc_data[1] = msg_namelower(msg);
1652 anc_data[2] = msg_nameupper(msg);
1653 break;
1654 case TIPC_CONN_MSG:
1655 has_name = (tsk->conn_type != 0);
1656 anc_data[0] = tsk->conn_type;
1657 anc_data[1] = tsk->conn_instance;
1658 anc_data[2] = tsk->conn_instance;
1659 break;
1660 default:
1661 has_name = 0;
1662 }
1663 if (has_name) {
1664 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1665 if (res)
1666 return res;
1667 }
1668
1669 return 0;
1670}
1671
1672static void tipc_sk_send_ack(struct tipc_sock *tsk)
1673{
1674 struct sock *sk = &tsk->sk;
1675 struct net *net = sock_net(sk);
1676 struct sk_buff *skb = NULL;
1677 struct tipc_msg *msg;
1678 u32 peer_port = tsk_peer_port(tsk);
1679 u32 dnode = tsk_peer_node(tsk);
1680
1681 if (!tipc_sk_connected(sk))
1682 return;
1683 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1684 dnode, tsk_own_node(tsk), peer_port,
1685 tsk->portid, TIPC_OK);
1686 if (!skb)
1687 return;
1688 msg = buf_msg(skb);
1689 msg_set_conn_ack(msg, tsk->rcv_unacked);
1690 tsk->rcv_unacked = 0;
1691
1692 /* Adjust to and advertize the correct window limit */
1693 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1694 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1695 msg_set_adv_win(msg, tsk->rcv_win);
1696 }
1697 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
1698}
1699
1700static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1701{
1702 struct sock *sk = sock->sk;
David Brazdil0f672f62019-12-10 10:32:29 +00001703 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001704 long timeo = *timeop;
1705 int err = sock_error(sk);
1706
1707 if (err)
1708 return err;
1709
1710 for (;;) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001711 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
1712 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1713 err = -ENOTCONN;
1714 break;
1715 }
David Brazdil0f672f62019-12-10 10:32:29 +00001716 add_wait_queue(sk_sleep(sk), &wait);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001717 release_sock(sk);
David Brazdil0f672f62019-12-10 10:32:29 +00001718 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
1719 sched_annotate_sleep();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001720 lock_sock(sk);
David Brazdil0f672f62019-12-10 10:32:29 +00001721 remove_wait_queue(sk_sleep(sk), &wait);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001722 }
1723 err = 0;
1724 if (!skb_queue_empty(&sk->sk_receive_queue))
1725 break;
1726 err = -EAGAIN;
1727 if (!timeo)
1728 break;
1729 err = sock_intr_errno(timeo);
1730 if (signal_pending(current))
1731 break;
1732
1733 err = sock_error(sk);
1734 if (err)
1735 break;
1736 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001737 *timeop = timeo;
1738 return err;
1739}
1740
1741/**
1742 * tipc_recvmsg - receive packet-oriented message
1743 * @m: descriptor for message info
1744 * @buflen: length of user buffer area
1745 * @flags: receive flags
1746 *
1747 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1748 * If the complete message doesn't fit in user area, truncate it.
1749 *
1750 * Returns size of returned message data, errno otherwise
1751 */
1752static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
1753 size_t buflen, int flags)
1754{
1755 struct sock *sk = sock->sk;
1756 bool connected = !tipc_sk_type_connectionless(sk);
1757 struct tipc_sock *tsk = tipc_sk(sk);
1758 int rc, err, hlen, dlen, copy;
Olivier Deprez0e641232021-09-23 10:07:05 +02001759 struct tipc_skb_cb *skb_cb;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001760 struct sk_buff_head xmitq;
1761 struct tipc_msg *hdr;
1762 struct sk_buff *skb;
1763 bool grp_evt;
1764 long timeout;
1765
1766 /* Catch invalid receive requests */
1767 if (unlikely(!buflen))
1768 return -EINVAL;
1769
1770 lock_sock(sk);
1771 if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
1772 rc = -ENOTCONN;
1773 goto exit;
1774 }
1775 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1776
1777 /* Step rcv queue to first msg with data or error; wait if necessary */
1778 do {
1779 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1780 if (unlikely(rc))
1781 goto exit;
1782 skb = skb_peek(&sk->sk_receive_queue);
Olivier Deprez0e641232021-09-23 10:07:05 +02001783 skb_cb = TIPC_SKB_CB(skb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001784 hdr = buf_msg(skb);
1785 dlen = msg_data_sz(hdr);
1786 hlen = msg_hdr_sz(hdr);
1787 err = msg_errcode(hdr);
1788 grp_evt = msg_is_grp_evt(hdr);
1789 if (likely(dlen || err))
1790 break;
1791 tsk_advance_rx_queue(sk);
1792 } while (1);
1793
1794 /* Collect msg meta data, including error code and rejected data */
1795 tipc_sk_set_orig_addr(m, skb);
1796 rc = tipc_sk_anc_data_recv(m, skb, tsk);
1797 if (unlikely(rc))
1798 goto exit;
1799 hdr = buf_msg(skb);
1800
1801 /* Capture data if non-error msg, otherwise just set return value */
1802 if (likely(!err)) {
Olivier Deprez0e641232021-09-23 10:07:05 +02001803 int offset = skb_cb->bytes_read;
1804
1805 copy = min_t(int, dlen - offset, buflen);
1806 rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
1807 if (unlikely(rc))
1808 goto exit;
1809 if (unlikely(offset + copy < dlen)) {
1810 if (flags & MSG_EOR) {
1811 if (!(flags & MSG_PEEK))
1812 skb_cb->bytes_read = offset + copy;
1813 } else {
1814 m->msg_flags |= MSG_TRUNC;
1815 skb_cb->bytes_read = 0;
1816 }
1817 } else {
1818 if (flags & MSG_EOR)
1819 m->msg_flags |= MSG_EOR;
1820 skb_cb->bytes_read = 0;
1821 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001822 } else {
1823 copy = 0;
1824 rc = 0;
Olivier Deprez0e641232021-09-23 10:07:05 +02001825 if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001826 rc = -ECONNRESET;
Olivier Deprez0e641232021-09-23 10:07:05 +02001827 goto exit;
1828 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001829 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001830
1831 /* Mark message as group event if applicable */
1832 if (unlikely(grp_evt)) {
1833 if (msg_grp_evt(hdr) == TIPC_WITHDRAWN)
1834 m->msg_flags |= MSG_EOR;
1835 m->msg_flags |= MSG_OOB;
1836 copy = 0;
1837 }
1838
1839 /* Caption of data or error code/rejected data was successful */
1840 if (unlikely(flags & MSG_PEEK))
1841 goto exit;
1842
1843 /* Send group flow control advertisement when applicable */
1844 if (tsk->group && msg_in_group(hdr) && !grp_evt) {
David Brazdil0f672f62019-12-10 10:32:29 +00001845 __skb_queue_head_init(&xmitq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001846 tipc_group_update_rcv_win(tsk->group, tsk_blocks(hlen + dlen),
1847 msg_orignode(hdr), msg_origport(hdr),
1848 &xmitq);
1849 tipc_node_distr_xmit(sock_net(sk), &xmitq);
1850 }
1851
Olivier Deprez0e641232021-09-23 10:07:05 +02001852 if (skb_cb->bytes_read)
1853 goto exit;
1854
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001855 tsk_advance_rx_queue(sk);
1856
1857 if (likely(!connected))
1858 goto exit;
1859
1860 /* Send connection flow control advertisement when applicable */
1861 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1862 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
1863 tipc_sk_send_ack(tsk);
1864exit:
1865 release_sock(sk);
1866 return rc ? rc : copy;
1867}
1868
1869/**
1870 * tipc_recvstream - receive stream-oriented data
1871 * @m: descriptor for message info
1872 * @buflen: total size of user buffer area
1873 * @flags: receive flags
1874 *
1875 * Used for SOCK_STREAM messages only. If not enough data is available
1876 * will optionally wait for more; never truncates data.
1877 *
1878 * Returns size of returned message data, errno otherwise
1879 */
1880static int tipc_recvstream(struct socket *sock, struct msghdr *m,
1881 size_t buflen, int flags)
1882{
1883 struct sock *sk = sock->sk;
1884 struct tipc_sock *tsk = tipc_sk(sk);
1885 struct sk_buff *skb;
1886 struct tipc_msg *hdr;
1887 struct tipc_skb_cb *skb_cb;
1888 bool peek = flags & MSG_PEEK;
1889 int offset, required, copy, copied = 0;
1890 int hlen, dlen, err, rc;
1891 long timeout;
1892
1893 /* Catch invalid receive attempts */
1894 if (unlikely(!buflen))
1895 return -EINVAL;
1896
1897 lock_sock(sk);
1898
1899 if (unlikely(sk->sk_state == TIPC_OPEN)) {
1900 rc = -ENOTCONN;
1901 goto exit;
1902 }
1903 required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
1904 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1905
1906 do {
1907 /* Look at first msg in receive queue; wait if necessary */
1908 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1909 if (unlikely(rc))
1910 break;
1911 skb = skb_peek(&sk->sk_receive_queue);
1912 skb_cb = TIPC_SKB_CB(skb);
1913 hdr = buf_msg(skb);
1914 dlen = msg_data_sz(hdr);
1915 hlen = msg_hdr_sz(hdr);
1916 err = msg_errcode(hdr);
1917
1918 /* Discard any empty non-errored (SYN-) message */
1919 if (unlikely(!dlen && !err)) {
1920 tsk_advance_rx_queue(sk);
1921 continue;
1922 }
1923
1924 /* Collect msg meta data, incl. error code and rejected data */
1925 if (!copied) {
1926 tipc_sk_set_orig_addr(m, skb);
1927 rc = tipc_sk_anc_data_recv(m, skb, tsk);
1928 if (rc)
1929 break;
1930 hdr = buf_msg(skb);
1931 }
1932
1933 /* Copy data if msg ok, otherwise return error/partial data */
1934 if (likely(!err)) {
1935 offset = skb_cb->bytes_read;
1936 copy = min_t(int, dlen - offset, buflen - copied);
1937 rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
1938 if (unlikely(rc))
1939 break;
1940 copied += copy;
1941 offset += copy;
1942 if (unlikely(offset < dlen)) {
1943 if (!peek)
1944 skb_cb->bytes_read = offset;
1945 break;
1946 }
1947 } else {
1948 rc = 0;
1949 if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control)
1950 rc = -ECONNRESET;
1951 if (copied || rc)
1952 break;
1953 }
1954
1955 if (unlikely(peek))
1956 break;
1957
1958 tsk_advance_rx_queue(sk);
1959
1960 /* Send connection flow control advertisement when applicable */
1961 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1962 if (unlikely(tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE))
1963 tipc_sk_send_ack(tsk);
1964
1965 /* Exit if all requested data or FIN/error received */
1966 if (copied == buflen || err)
1967 break;
1968
1969 } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required);
1970exit:
1971 release_sock(sk);
1972 return copied ? copied : rc;
1973}
1974
1975/**
1976 * tipc_write_space - wake up thread if port congestion is released
1977 * @sk: socket
1978 */
1979static void tipc_write_space(struct sock *sk)
1980{
1981 struct socket_wq *wq;
1982
1983 rcu_read_lock();
1984 wq = rcu_dereference(sk->sk_wq);
1985 if (skwq_has_sleeper(wq))
1986 wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
1987 EPOLLWRNORM | EPOLLWRBAND);
1988 rcu_read_unlock();
1989}
1990
1991/**
1992 * tipc_data_ready - wake up threads to indicate messages have been received
1993 * @sk: socket
1994 * @len: the length of messages
1995 */
1996static void tipc_data_ready(struct sock *sk)
1997{
1998 struct socket_wq *wq;
1999
2000 rcu_read_lock();
2001 wq = rcu_dereference(sk->sk_wq);
2002 if (skwq_has_sleeper(wq))
2003 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
2004 EPOLLRDNORM | EPOLLRDBAND);
2005 rcu_read_unlock();
2006}
2007
2008static void tipc_sock_destruct(struct sock *sk)
2009{
2010 __skb_queue_purge(&sk->sk_receive_queue);
2011}
2012
2013static void tipc_sk_proto_rcv(struct sock *sk,
2014 struct sk_buff_head *inputq,
2015 struct sk_buff_head *xmitq)
2016{
2017 struct sk_buff *skb = __skb_dequeue(inputq);
2018 struct tipc_sock *tsk = tipc_sk(sk);
2019 struct tipc_msg *hdr = buf_msg(skb);
2020 struct tipc_group *grp = tsk->group;
2021 bool wakeup = false;
2022
2023 switch (msg_user(hdr)) {
2024 case CONN_MANAGER:
2025 tipc_sk_conn_proto_rcv(tsk, skb, inputq, xmitq);
2026 return;
2027 case SOCK_WAKEUP:
2028 tipc_dest_del(&tsk->cong_links, msg_orignode(hdr), 0);
David Brazdil0f672f62019-12-10 10:32:29 +00002029 /* coupled with smp_rmb() in tipc_wait_for_cond() */
2030 smp_wmb();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002031 tsk->cong_link_cnt--;
2032 wakeup = true;
2033 break;
2034 case GROUP_PROTOCOL:
2035 tipc_group_proto_rcv(grp, &wakeup, hdr, inputq, xmitq);
2036 break;
2037 case TOP_SRV:
2038 tipc_group_member_evt(tsk->group, &wakeup, &sk->sk_rcvbuf,
2039 hdr, inputq, xmitq);
2040 break;
2041 default:
2042 break;
2043 }
2044
2045 if (wakeup)
2046 sk->sk_write_space(sk);
2047
2048 kfree_skb(skb);
2049}
2050
2051/**
David Brazdil0f672f62019-12-10 10:32:29 +00002052 * tipc_sk_filter_connect - check incoming message for a connection-based socket
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002053 * @tsk: TIPC socket
David Brazdil0f672f62019-12-10 10:32:29 +00002054 * @skb: pointer to message buffer.
2055 * Returns true if message should be added to receive queue, false otherwise
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002056 */
2057static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
2058{
2059 struct sock *sk = &tsk->sk;
2060 struct net *net = sock_net(sk);
2061 struct tipc_msg *hdr = buf_msg(skb);
David Brazdil0f672f62019-12-10 10:32:29 +00002062 bool con_msg = msg_connected(hdr);
2063 u32 pport = tsk_peer_port(tsk);
2064 u32 pnode = tsk_peer_node(tsk);
2065 u32 oport = msg_origport(hdr);
2066 u32 onode = msg_orignode(hdr);
2067 int err = msg_errcode(hdr);
2068 unsigned long delay;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002069
2070 if (unlikely(msg_mcast(hdr)))
2071 return false;
2072
2073 switch (sk->sk_state) {
2074 case TIPC_CONNECTING:
David Brazdil0f672f62019-12-10 10:32:29 +00002075 /* Setup ACK */
2076 if (likely(con_msg)) {
2077 if (err)
2078 break;
2079 tipc_sk_finish_conn(tsk, oport, onode);
2080 msg_set_importance(&tsk->phdr, msg_importance(hdr));
2081 /* ACK+ message with data is added to receive queue */
2082 if (msg_data_sz(hdr))
2083 return true;
2084 /* Empty ACK-, - wake up sleeping connect() and drop */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002085 sk->sk_state_change(sk);
David Brazdil0f672f62019-12-10 10:32:29 +00002086 msg_set_dest_droppable(hdr, 1);
2087 return false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002088 }
David Brazdil0f672f62019-12-10 10:32:29 +00002089 /* Ignore connectionless message if not from listening socket */
2090 if (oport != pport || onode != pnode)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002091 return false;
2092
David Brazdil0f672f62019-12-10 10:32:29 +00002093 /* Rejected SYN */
2094 if (err != TIPC_ERR_OVERLOAD)
2095 break;
2096
2097 /* Prepare for new setup attempt if we have a SYN clone */
2098 if (skb_queue_empty(&sk->sk_write_queue))
2099 break;
2100 get_random_bytes(&delay, 2);
2101 delay %= (tsk->conn_timeout / 4);
2102 delay = msecs_to_jiffies(delay + 100);
2103 sk_reset_timer(sk, &sk->sk_timer, jiffies + delay);
2104 return false;
2105 case TIPC_OPEN:
2106 case TIPC_DISCONNECTING:
2107 return false;
2108 case TIPC_LISTEN:
2109 /* Accept only SYN message */
2110 if (!msg_is_syn(hdr) &&
2111 tipc_node_get_capabilities(net, onode) & TIPC_SYN_BIT)
2112 return false;
2113 if (!con_msg && !err)
2114 return true;
2115 return false;
2116 case TIPC_ESTABLISHED:
2117 /* Accept only connection-based messages sent by peer */
2118 if (likely(con_msg && !err && pport == oport && pnode == onode))
2119 return true;
2120 if (!tsk_peer_msg(tsk, hdr))
2121 return false;
2122 if (!err)
2123 return true;
2124 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2125 tipc_node_remove_conn(net, pnode, tsk->portid);
2126 sk->sk_state_change(sk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002127 return true;
2128 default:
2129 pr_err("Unknown sk_state %u\n", sk->sk_state);
2130 }
David Brazdil0f672f62019-12-10 10:32:29 +00002131 /* Abort connection setup attempt */
2132 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2133 sk->sk_err = ECONNREFUSED;
2134 sk->sk_state_change(sk);
2135 return true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002136}
2137
2138/**
2139 * rcvbuf_limit - get proper overload limit of socket receive queue
2140 * @sk: socket
2141 * @skb: message
2142 *
2143 * For connection oriented messages, irrespective of importance,
2144 * default queue limit is 2 MB.
2145 *
2146 * For connectionless messages, queue limits are based on message
2147 * importance as follows:
2148 *
2149 * TIPC_LOW_IMPORTANCE (2 MB)
2150 * TIPC_MEDIUM_IMPORTANCE (4 MB)
2151 * TIPC_HIGH_IMPORTANCE (8 MB)
2152 * TIPC_CRITICAL_IMPORTANCE (16 MB)
2153 *
2154 * Returns overload limit according to corresponding message importance
2155 */
2156static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
2157{
2158 struct tipc_sock *tsk = tipc_sk(sk);
2159 struct tipc_msg *hdr = buf_msg(skb);
2160
2161 if (unlikely(msg_in_group(hdr)))
David Brazdil0f672f62019-12-10 10:32:29 +00002162 return READ_ONCE(sk->sk_rcvbuf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002163
2164 if (unlikely(!msg_connected(hdr)))
David Brazdil0f672f62019-12-10 10:32:29 +00002165 return READ_ONCE(sk->sk_rcvbuf) << msg_importance(hdr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002166
2167 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
David Brazdil0f672f62019-12-10 10:32:29 +00002168 return READ_ONCE(sk->sk_rcvbuf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002169
2170 return FLOWCTL_MSG_LIM;
2171}
2172
2173/**
2174 * tipc_sk_filter_rcv - validate incoming message
2175 * @sk: socket
2176 * @skb: pointer to message.
2177 *
2178 * Enqueues message on receive queue if acceptable; optionally handles
2179 * disconnect indication for a connected socket.
2180 *
2181 * Called with socket lock already taken
2182 *
2183 */
2184static void tipc_sk_filter_rcv(struct sock *sk, struct sk_buff *skb,
2185 struct sk_buff_head *xmitq)
2186{
2187 bool sk_conn = !tipc_sk_type_connectionless(sk);
2188 struct tipc_sock *tsk = tipc_sk(sk);
2189 struct tipc_group *grp = tsk->group;
2190 struct tipc_msg *hdr = buf_msg(skb);
2191 struct net *net = sock_net(sk);
2192 struct sk_buff_head inputq;
David Brazdil0f672f62019-12-10 10:32:29 +00002193 int mtyp = msg_type(hdr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002194 int limit, err = TIPC_OK;
2195
David Brazdil0f672f62019-12-10 10:32:29 +00002196 trace_tipc_sk_filter_rcv(sk, skb, TIPC_DUMP_ALL, " ");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002197 TIPC_SKB_CB(skb)->bytes_read = 0;
2198 __skb_queue_head_init(&inputq);
2199 __skb_queue_tail(&inputq, skb);
2200
2201 if (unlikely(!msg_isdata(hdr)))
2202 tipc_sk_proto_rcv(sk, &inputq, xmitq);
2203
2204 if (unlikely(grp))
2205 tipc_group_filter_msg(grp, &inputq, xmitq);
2206
David Brazdil0f672f62019-12-10 10:32:29 +00002207 if (unlikely(!grp) && mtyp == TIPC_MCAST_MSG)
2208 tipc_mcast_filter_msg(net, &tsk->mc_method.deferredq, &inputq);
2209
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002210 /* Validate and add to receive buffer if there is space */
2211 while ((skb = __skb_dequeue(&inputq))) {
2212 hdr = buf_msg(skb);
2213 limit = rcvbuf_limit(sk, skb);
2214 if ((sk_conn && !tipc_sk_filter_connect(tsk, skb)) ||
2215 (!sk_conn && msg_connected(hdr)) ||
2216 (!grp && msg_in_group(hdr)))
2217 err = TIPC_ERR_NO_PORT;
2218 else if (sk_rmem_alloc_get(sk) + skb->truesize >= limit) {
David Brazdil0f672f62019-12-10 10:32:29 +00002219 trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL,
2220 "err_overload2!");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002221 atomic_inc(&sk->sk_drops);
2222 err = TIPC_ERR_OVERLOAD;
2223 }
2224
2225 if (unlikely(err)) {
David Brazdil0f672f62019-12-10 10:32:29 +00002226 if (tipc_msg_reverse(tipc_own_addr(net), &skb, err)) {
2227 trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE,
2228 "@filter_rcv!");
2229 __skb_queue_tail(xmitq, skb);
2230 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002231 err = TIPC_OK;
2232 continue;
2233 }
2234 __skb_queue_tail(&sk->sk_receive_queue, skb);
2235 skb_set_owner_r(skb, sk);
David Brazdil0f672f62019-12-10 10:32:29 +00002236 trace_tipc_sk_overlimit2(sk, skb, TIPC_DUMP_ALL,
2237 "rcvq >90% allocated!");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002238 sk->sk_data_ready(sk);
2239 }
2240}
2241
2242/**
2243 * tipc_sk_backlog_rcv - handle incoming message from backlog queue
2244 * @sk: socket
2245 * @skb: message
2246 *
2247 * Caller must hold socket lock
2248 */
2249static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
2250{
2251 unsigned int before = sk_rmem_alloc_get(sk);
2252 struct sk_buff_head xmitq;
2253 unsigned int added;
2254
2255 __skb_queue_head_init(&xmitq);
2256
2257 tipc_sk_filter_rcv(sk, skb, &xmitq);
2258 added = sk_rmem_alloc_get(sk) - before;
2259 atomic_add(added, &tipc_sk(sk)->dupl_rcvcnt);
2260
2261 /* Send pending response/rejected messages, if any */
2262 tipc_node_distr_xmit(sock_net(sk), &xmitq);
2263 return 0;
2264}
2265
2266/**
2267 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
2268 * inputq and try adding them to socket or backlog queue
2269 * @inputq: list of incoming buffers with potentially different destinations
2270 * @sk: socket where the buffers should be enqueued
2271 * @dport: port number for the socket
2272 *
2273 * Caller must hold socket lock
2274 */
2275static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
2276 u32 dport, struct sk_buff_head *xmitq)
2277{
Olivier Deprez0e641232021-09-23 10:07:05 +02002278 unsigned long time_limit = jiffies + usecs_to_jiffies(20000);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002279 struct sk_buff *skb;
2280 unsigned int lim;
2281 atomic_t *dcnt;
2282 u32 onode;
2283
2284 while (skb_queue_len(inputq)) {
2285 if (unlikely(time_after_eq(jiffies, time_limit)))
2286 return;
2287
2288 skb = tipc_skb_dequeue(inputq, dport);
2289 if (unlikely(!skb))
2290 return;
2291
2292 /* Add message directly to receive queue if possible */
2293 if (!sock_owned_by_user(sk)) {
2294 tipc_sk_filter_rcv(sk, skb, xmitq);
2295 continue;
2296 }
2297
2298 /* Try backlog, compensating for double-counted bytes */
2299 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
2300 if (!sk->sk_backlog.len)
2301 atomic_set(dcnt, 0);
2302 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
David Brazdil0f672f62019-12-10 10:32:29 +00002303 if (likely(!sk_add_backlog(sk, skb, lim))) {
2304 trace_tipc_sk_overlimit1(sk, skb, TIPC_DUMP_ALL,
2305 "bklg & rcvq >90% allocated!");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002306 continue;
David Brazdil0f672f62019-12-10 10:32:29 +00002307 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002308
David Brazdil0f672f62019-12-10 10:32:29 +00002309 trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL, "err_overload!");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002310 /* Overload => reject message back to sender */
2311 onode = tipc_own_addr(sock_net(sk));
2312 atomic_inc(&sk->sk_drops);
David Brazdil0f672f62019-12-10 10:32:29 +00002313 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD)) {
2314 trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_ALL,
2315 "@sk_enqueue!");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002316 __skb_queue_tail(xmitq, skb);
David Brazdil0f672f62019-12-10 10:32:29 +00002317 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002318 break;
2319 }
2320}
2321
2322/**
2323 * tipc_sk_rcv - handle a chain of incoming buffers
2324 * @inputq: buffer list containing the buffers
2325 * Consumes all buffers in list until inputq is empty
2326 * Note: may be called in multiple threads referring to the same queue
2327 */
2328void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
2329{
2330 struct sk_buff_head xmitq;
2331 u32 dnode, dport = 0;
2332 int err;
2333 struct tipc_sock *tsk;
2334 struct sock *sk;
2335 struct sk_buff *skb;
2336
2337 __skb_queue_head_init(&xmitq);
2338 while (skb_queue_len(inputq)) {
2339 dport = tipc_skb_peek_port(inputq, dport);
2340 tsk = tipc_sk_lookup(net, dport);
2341
2342 if (likely(tsk)) {
2343 sk = &tsk->sk;
2344 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
2345 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
2346 spin_unlock_bh(&sk->sk_lock.slock);
2347 }
2348 /* Send pending response/rejected messages, if any */
2349 tipc_node_distr_xmit(sock_net(sk), &xmitq);
2350 sock_put(sk);
2351 continue;
2352 }
2353 /* No destination socket => dequeue skb if still there */
2354 skb = tipc_skb_dequeue(inputq, dport);
2355 if (!skb)
2356 return;
2357
2358 /* Try secondary lookup if unresolved named message */
2359 err = TIPC_ERR_NO_PORT;
2360 if (tipc_msg_lookup_dest(net, skb, &err))
2361 goto xmit;
2362
2363 /* Prepare for message rejection */
2364 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
2365 continue;
David Brazdil0f672f62019-12-10 10:32:29 +00002366
2367 trace_tipc_sk_rej_msg(NULL, skb, TIPC_DUMP_NONE, "@sk_rcv!");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002368xmit:
2369 dnode = msg_destnode(buf_msg(skb));
2370 tipc_node_xmit_skb(net, skb, dnode, dport);
2371 }
2372}
2373
2374static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
2375{
2376 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2377 struct sock *sk = sock->sk;
2378 int done;
2379
2380 do {
2381 int err = sock_error(sk);
2382 if (err)
2383 return err;
2384 if (!*timeo_p)
2385 return -ETIMEDOUT;
2386 if (signal_pending(current))
2387 return sock_intr_errno(*timeo_p);
2388
2389 add_wait_queue(sk_sleep(sk), &wait);
2390 done = sk_wait_event(sk, timeo_p,
2391 sk->sk_state != TIPC_CONNECTING, &wait);
2392 remove_wait_queue(sk_sleep(sk), &wait);
2393 } while (!done);
2394 return 0;
2395}
2396
David Brazdil0f672f62019-12-10 10:32:29 +00002397static bool tipc_sockaddr_is_sane(struct sockaddr_tipc *addr)
2398{
2399 if (addr->family != AF_TIPC)
2400 return false;
2401 if (addr->addrtype == TIPC_SERVICE_RANGE)
2402 return (addr->addr.nameseq.lower <= addr->addr.nameseq.upper);
2403 return (addr->addrtype == TIPC_SERVICE_ADDR ||
2404 addr->addrtype == TIPC_SOCKET_ADDR);
2405}
2406
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002407/**
2408 * tipc_connect - establish a connection to another TIPC port
2409 * @sock: socket structure
2410 * @dest: socket address for destination port
2411 * @destlen: size of socket address data structure
2412 * @flags: file-related flags associated with socket
2413 *
2414 * Returns 0 on success, errno otherwise
2415 */
2416static int tipc_connect(struct socket *sock, struct sockaddr *dest,
2417 int destlen, int flags)
2418{
2419 struct sock *sk = sock->sk;
2420 struct tipc_sock *tsk = tipc_sk(sk);
2421 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
2422 struct msghdr m = {NULL,};
2423 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
2424 int previous;
2425 int res = 0;
2426
2427 if (destlen != sizeof(struct sockaddr_tipc))
2428 return -EINVAL;
2429
2430 lock_sock(sk);
2431
2432 if (tsk->group) {
2433 res = -EINVAL;
2434 goto exit;
2435 }
2436
2437 if (dst->family == AF_UNSPEC) {
2438 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
2439 if (!tipc_sk_type_connectionless(sk))
2440 res = -EINVAL;
2441 goto exit;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002442 }
David Brazdil0f672f62019-12-10 10:32:29 +00002443 if (!tipc_sockaddr_is_sane(dst)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002444 res = -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002445 goto exit;
David Brazdil0f672f62019-12-10 10:32:29 +00002446 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002447 /* DGRAM/RDM connect(), just save the destaddr */
2448 if (tipc_sk_type_connectionless(sk)) {
2449 memcpy(&tsk->peer, dest, destlen);
2450 goto exit;
David Brazdil0f672f62019-12-10 10:32:29 +00002451 } else if (dst->addrtype == TIPC_SERVICE_RANGE) {
2452 res = -EINVAL;
2453 goto exit;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002454 }
2455
2456 previous = sk->sk_state;
2457
2458 switch (sk->sk_state) {
2459 case TIPC_OPEN:
2460 /* Send a 'SYN-' to destination */
2461 m.msg_name = dest;
2462 m.msg_namelen = destlen;
2463
2464 /* If connect is in non-blocking case, set MSG_DONTWAIT to
2465 * indicate send_msg() is never blocked.
2466 */
2467 if (!timeout)
2468 m.msg_flags = MSG_DONTWAIT;
2469
2470 res = __tipc_sendmsg(sock, &m, 0);
2471 if ((res < 0) && (res != -EWOULDBLOCK))
2472 goto exit;
2473
2474 /* Just entered TIPC_CONNECTING state; the only
2475 * difference is that return value in non-blocking
2476 * case is EINPROGRESS, rather than EALREADY.
2477 */
2478 res = -EINPROGRESS;
David Brazdil0f672f62019-12-10 10:32:29 +00002479 /* fall through */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002480 case TIPC_CONNECTING:
2481 if (!timeout) {
2482 if (previous == TIPC_CONNECTING)
2483 res = -EALREADY;
2484 goto exit;
2485 }
2486 timeout = msecs_to_jiffies(timeout);
2487 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
2488 res = tipc_wait_for_connect(sock, &timeout);
2489 break;
2490 case TIPC_ESTABLISHED:
2491 res = -EISCONN;
2492 break;
2493 default:
2494 res = -EINVAL;
2495 }
2496
2497exit:
2498 release_sock(sk);
2499 return res;
2500}
2501
2502/**
2503 * tipc_listen - allow socket to listen for incoming connections
2504 * @sock: socket structure
2505 * @len: (unused)
2506 *
2507 * Returns 0 on success, errno otherwise
2508 */
2509static int tipc_listen(struct socket *sock, int len)
2510{
2511 struct sock *sk = sock->sk;
2512 int res;
2513
2514 lock_sock(sk);
2515 res = tipc_set_sk_state(sk, TIPC_LISTEN);
2516 release_sock(sk);
2517
2518 return res;
2519}
2520
2521static int tipc_wait_for_accept(struct socket *sock, long timeo)
2522{
2523 struct sock *sk = sock->sk;
Olivier Deprez0e641232021-09-23 10:07:05 +02002524 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002525 int err;
2526
2527 /* True wake-one mechanism for incoming connections: only
2528 * one process gets woken up, not the 'whole herd'.
2529 * Since we do not 'race & poll' for established sockets
2530 * anymore, the common case will execute the loop only once.
2531 */
2532 for (;;) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002533 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Olivier Deprez0e641232021-09-23 10:07:05 +02002534 add_wait_queue(sk_sleep(sk), &wait);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002535 release_sock(sk);
Olivier Deprez0e641232021-09-23 10:07:05 +02002536 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002537 lock_sock(sk);
Olivier Deprez0e641232021-09-23 10:07:05 +02002538 remove_wait_queue(sk_sleep(sk), &wait);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002539 }
2540 err = 0;
2541 if (!skb_queue_empty(&sk->sk_receive_queue))
2542 break;
2543 err = -EAGAIN;
2544 if (!timeo)
2545 break;
2546 err = sock_intr_errno(timeo);
2547 if (signal_pending(current))
2548 break;
2549 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002550 return err;
2551}
2552
2553/**
2554 * tipc_accept - wait for connection request
2555 * @sock: listening socket
2556 * @newsock: new socket that is to be connected
2557 * @flags: file-related flags associated with socket
2558 *
2559 * Returns 0 on success, errno otherwise
2560 */
2561static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
2562 bool kern)
2563{
2564 struct sock *new_sk, *sk = sock->sk;
2565 struct sk_buff *buf;
2566 struct tipc_sock *new_tsock;
2567 struct tipc_msg *msg;
2568 long timeo;
2569 int res;
2570
2571 lock_sock(sk);
2572
2573 if (sk->sk_state != TIPC_LISTEN) {
2574 res = -EINVAL;
2575 goto exit;
2576 }
2577 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2578 res = tipc_wait_for_accept(sock, timeo);
2579 if (res)
2580 goto exit;
2581
2582 buf = skb_peek(&sk->sk_receive_queue);
2583
2584 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
2585 if (res)
2586 goto exit;
2587 security_sk_clone(sock->sk, new_sock->sk);
2588
2589 new_sk = new_sock->sk;
2590 new_tsock = tipc_sk(new_sk);
2591 msg = buf_msg(buf);
2592
2593 /* we lock on new_sk; but lockdep sees the lock on sk */
2594 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
2595
2596 /*
2597 * Reject any stray messages received by new socket
2598 * before the socket lock was taken (very, very unlikely)
2599 */
Olivier Deprez0e641232021-09-23 10:07:05 +02002600 tsk_rej_rx_queue(new_sk, TIPC_ERR_NO_PORT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002601
2602 /* Connect new socket to it's peer */
2603 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
2604
2605 tsk_set_importance(new_tsock, msg_importance(msg));
2606 if (msg_named(msg)) {
2607 new_tsock->conn_type = msg_nametype(msg);
2608 new_tsock->conn_instance = msg_nameinst(msg);
2609 }
2610
2611 /*
2612 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2613 * Respond to 'SYN+' by queuing it on new socket.
2614 */
2615 if (!msg_data_sz(msg)) {
2616 struct msghdr m = {NULL,};
2617
2618 tsk_advance_rx_queue(sk);
2619 __tipc_sendstream(new_sock, &m, 0);
2620 } else {
2621 __skb_dequeue(&sk->sk_receive_queue);
2622 __skb_queue_head(&new_sk->sk_receive_queue, buf);
2623 skb_set_owner_r(buf, new_sk);
2624 }
2625 release_sock(new_sk);
2626exit:
2627 release_sock(sk);
2628 return res;
2629}
2630
2631/**
2632 * tipc_shutdown - shutdown socket connection
2633 * @sock: socket structure
2634 * @how: direction to close (must be SHUT_RDWR)
2635 *
2636 * Terminates connection (if necessary), then purges socket's receive queue.
2637 *
2638 * Returns 0 on success, errno otherwise
2639 */
2640static int tipc_shutdown(struct socket *sock, int how)
2641{
2642 struct sock *sk = sock->sk;
2643 int res;
2644
2645 if (how != SHUT_RDWR)
2646 return -EINVAL;
2647
2648 lock_sock(sk);
2649
David Brazdil0f672f62019-12-10 10:32:29 +00002650 trace_tipc_sk_shutdown(sk, NULL, TIPC_DUMP_ALL, " ");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002651 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
Olivier Deprez0e641232021-09-23 10:07:05 +02002652 sk->sk_shutdown = SHUTDOWN_MASK;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002653
2654 if (sk->sk_state == TIPC_DISCONNECTING) {
2655 /* Discard any unreceived messages */
2656 __skb_queue_purge(&sk->sk_receive_queue);
2657
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002658 res = 0;
2659 } else {
2660 res = -ENOTCONN;
2661 }
Olivier Deprez0e641232021-09-23 10:07:05 +02002662 /* Wake up anyone sleeping in poll. */
2663 sk->sk_state_change(sk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002664
2665 release_sock(sk);
2666 return res;
2667}
2668
David Brazdil0f672f62019-12-10 10:32:29 +00002669static void tipc_sk_check_probing_state(struct sock *sk,
2670 struct sk_buff_head *list)
2671{
2672 struct tipc_sock *tsk = tipc_sk(sk);
2673 u32 pnode = tsk_peer_node(tsk);
2674 u32 pport = tsk_peer_port(tsk);
2675 u32 self = tsk_own_node(tsk);
2676 u32 oport = tsk->portid;
2677 struct sk_buff *skb;
2678
2679 if (tsk->probe_unacked) {
2680 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2681 sk->sk_err = ECONNABORTED;
2682 tipc_node_remove_conn(sock_net(sk), pnode, pport);
2683 sk->sk_state_change(sk);
2684 return;
2685 }
2686 /* Prepare new probe */
2687 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, 0,
2688 pnode, self, pport, oport, TIPC_OK);
2689 if (skb)
2690 __skb_queue_tail(list, skb);
2691 tsk->probe_unacked = true;
2692 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
2693}
2694
2695static void tipc_sk_retry_connect(struct sock *sk, struct sk_buff_head *list)
2696{
2697 struct tipc_sock *tsk = tipc_sk(sk);
2698
2699 /* Try again later if dest link is congested */
2700 if (tsk->cong_link_cnt) {
2701 sk_reset_timer(sk, &sk->sk_timer, msecs_to_jiffies(100));
2702 return;
2703 }
2704 /* Prepare SYN for retransmit */
2705 tipc_msg_skb_clone(&sk->sk_write_queue, list);
2706}
2707
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002708static void tipc_sk_timeout(struct timer_list *t)
2709{
2710 struct sock *sk = from_timer(sk, t, sk_timer);
2711 struct tipc_sock *tsk = tipc_sk(sk);
David Brazdil0f672f62019-12-10 10:32:29 +00002712 u32 pnode = tsk_peer_node(tsk);
2713 struct sk_buff_head list;
2714 int rc = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002715
David Brazdil0f672f62019-12-10 10:32:29 +00002716 __skb_queue_head_init(&list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002717 bh_lock_sock(sk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002718
2719 /* Try again later if socket is busy */
2720 if (sock_owned_by_user(sk)) {
2721 sk_reset_timer(sk, &sk->sk_timer, jiffies + HZ / 20);
David Brazdil0f672f62019-12-10 10:32:29 +00002722 bh_unlock_sock(sk);
Olivier Deprez0e641232021-09-23 10:07:05 +02002723 sock_put(sk);
David Brazdil0f672f62019-12-10 10:32:29 +00002724 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002725 }
2726
David Brazdil0f672f62019-12-10 10:32:29 +00002727 if (sk->sk_state == TIPC_ESTABLISHED)
2728 tipc_sk_check_probing_state(sk, &list);
2729 else if (sk->sk_state == TIPC_CONNECTING)
2730 tipc_sk_retry_connect(sk, &list);
2731
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002732 bh_unlock_sock(sk);
David Brazdil0f672f62019-12-10 10:32:29 +00002733
2734 if (!skb_queue_empty(&list))
2735 rc = tipc_node_xmit(sock_net(sk), &list, pnode, tsk->portid);
2736
2737 /* SYN messages may cause link congestion */
2738 if (rc == -ELINKCONG) {
2739 tipc_dest_push(&tsk->cong_links, pnode, 0);
2740 tsk->cong_link_cnt = 1;
2741 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002742 sock_put(sk);
2743}
2744
2745static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
2746 struct tipc_name_seq const *seq)
2747{
2748 struct sock *sk = &tsk->sk;
2749 struct net *net = sock_net(sk);
2750 struct publication *publ;
2751 u32 key;
2752
2753 if (scope != TIPC_NODE_SCOPE)
2754 scope = TIPC_CLUSTER_SCOPE;
2755
2756 if (tipc_sk_connected(sk))
2757 return -EINVAL;
2758 key = tsk->portid + tsk->pub_count + 1;
2759 if (key == tsk->portid)
2760 return -EADDRINUSE;
2761
2762 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
2763 scope, tsk->portid, key);
2764 if (unlikely(!publ))
2765 return -EINVAL;
2766
2767 list_add(&publ->binding_sock, &tsk->publications);
2768 tsk->pub_count++;
2769 tsk->published = 1;
2770 return 0;
2771}
2772
2773static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
2774 struct tipc_name_seq const *seq)
2775{
2776 struct net *net = sock_net(&tsk->sk);
2777 struct publication *publ;
2778 struct publication *safe;
2779 int rc = -EINVAL;
2780
2781 if (scope != TIPC_NODE_SCOPE)
2782 scope = TIPC_CLUSTER_SCOPE;
2783
2784 list_for_each_entry_safe(publ, safe, &tsk->publications, binding_sock) {
2785 if (seq) {
2786 if (publ->scope != scope)
2787 continue;
2788 if (publ->type != seq->type)
2789 continue;
2790 if (publ->lower != seq->lower)
2791 continue;
2792 if (publ->upper != seq->upper)
2793 break;
2794 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2795 publ->upper, publ->key);
2796 rc = 0;
2797 break;
2798 }
2799 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2800 publ->upper, publ->key);
2801 rc = 0;
2802 }
2803 if (list_empty(&tsk->publications))
2804 tsk->published = 0;
2805 return rc;
2806}
2807
2808/* tipc_sk_reinit: set non-zero address in all existing sockets
2809 * when we go from standalone to network mode.
2810 */
2811void tipc_sk_reinit(struct net *net)
2812{
2813 struct tipc_net *tn = net_generic(net, tipc_net_id);
2814 struct rhashtable_iter iter;
2815 struct tipc_sock *tsk;
2816 struct tipc_msg *msg;
2817
2818 rhashtable_walk_enter(&tn->sk_rht, &iter);
2819
2820 do {
2821 rhashtable_walk_start(&iter);
2822
2823 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
David Brazdil0f672f62019-12-10 10:32:29 +00002824 sock_hold(&tsk->sk);
2825 rhashtable_walk_stop(&iter);
2826 lock_sock(&tsk->sk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002827 msg = &tsk->phdr;
2828 msg_set_prevnode(msg, tipc_own_addr(net));
2829 msg_set_orignode(msg, tipc_own_addr(net));
David Brazdil0f672f62019-12-10 10:32:29 +00002830 release_sock(&tsk->sk);
2831 rhashtable_walk_start(&iter);
2832 sock_put(&tsk->sk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002833 }
2834
2835 rhashtable_walk_stop(&iter);
2836 } while (tsk == ERR_PTR(-EAGAIN));
2837
2838 rhashtable_walk_exit(&iter);
2839}
2840
2841static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
2842{
2843 struct tipc_net *tn = net_generic(net, tipc_net_id);
2844 struct tipc_sock *tsk;
2845
2846 rcu_read_lock();
2847 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
2848 if (tsk)
2849 sock_hold(&tsk->sk);
2850 rcu_read_unlock();
2851
2852 return tsk;
2853}
2854
2855static int tipc_sk_insert(struct tipc_sock *tsk)
2856{
2857 struct sock *sk = &tsk->sk;
2858 struct net *net = sock_net(sk);
2859 struct tipc_net *tn = net_generic(net, tipc_net_id);
2860 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2861 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2862
2863 while (remaining--) {
2864 portid++;
2865 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2866 portid = TIPC_MIN_PORT;
2867 tsk->portid = portid;
2868 sock_hold(&tsk->sk);
2869 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2870 tsk_rht_params))
2871 return 0;
2872 sock_put(&tsk->sk);
2873 }
2874
2875 return -1;
2876}
2877
2878static void tipc_sk_remove(struct tipc_sock *tsk)
2879{
2880 struct sock *sk = &tsk->sk;
2881 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
2882
2883 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
2884 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
2885 __sock_put(sk);
2886 }
2887}
2888
2889static const struct rhashtable_params tsk_rht_params = {
2890 .nelem_hint = 192,
2891 .head_offset = offsetof(struct tipc_sock, node),
2892 .key_offset = offsetof(struct tipc_sock, portid),
2893 .key_len = sizeof(u32), /* portid */
2894 .max_size = 1048576,
2895 .min_size = 256,
2896 .automatic_shrinking = true,
2897};
2898
2899int tipc_sk_rht_init(struct net *net)
2900{
2901 struct tipc_net *tn = net_generic(net, tipc_net_id);
2902
2903 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
2904}
2905
2906void tipc_sk_rht_destroy(struct net *net)
2907{
2908 struct tipc_net *tn = net_generic(net, tipc_net_id);
2909
2910 /* Wait for socket readers to complete */
2911 synchronize_net();
2912
2913 rhashtable_destroy(&tn->sk_rht);
2914}
2915
2916static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq)
2917{
2918 struct net *net = sock_net(&tsk->sk);
2919 struct tipc_group *grp = tsk->group;
2920 struct tipc_msg *hdr = &tsk->phdr;
2921 struct tipc_name_seq seq;
2922 int rc;
2923
2924 if (mreq->type < TIPC_RESERVED_TYPES)
2925 return -EACCES;
2926 if (mreq->scope > TIPC_NODE_SCOPE)
2927 return -EINVAL;
2928 if (grp)
2929 return -EACCES;
2930 grp = tipc_group_create(net, tsk->portid, mreq, &tsk->group_is_open);
2931 if (!grp)
2932 return -ENOMEM;
2933 tsk->group = grp;
2934 msg_set_lookup_scope(hdr, mreq->scope);
2935 msg_set_nametype(hdr, mreq->type);
2936 msg_set_dest_droppable(hdr, true);
2937 seq.type = mreq->type;
2938 seq.lower = mreq->instance;
2939 seq.upper = seq.lower;
2940 tipc_nametbl_build_group(net, grp, mreq->type, mreq->scope);
2941 rc = tipc_sk_publish(tsk, mreq->scope, &seq);
2942 if (rc) {
2943 tipc_group_delete(net, grp);
2944 tsk->group = NULL;
2945 return rc;
2946 }
2947 /* Eliminate any risk that a broadcast overtakes sent JOINs */
2948 tsk->mc_method.rcast = true;
2949 tsk->mc_method.mandatory = true;
2950 tipc_group_join(net, grp, &tsk->sk.sk_rcvbuf);
2951 return rc;
2952}
2953
2954static int tipc_sk_leave(struct tipc_sock *tsk)
2955{
2956 struct net *net = sock_net(&tsk->sk);
2957 struct tipc_group *grp = tsk->group;
2958 struct tipc_name_seq seq;
2959 int scope;
2960
2961 if (!grp)
2962 return -EINVAL;
2963 tipc_group_self(grp, &seq, &scope);
2964 tipc_group_delete(net, grp);
2965 tsk->group = NULL;
2966 tipc_sk_withdraw(tsk, scope, &seq);
2967 return 0;
2968}
2969
2970/**
2971 * tipc_setsockopt - set socket option
2972 * @sock: socket structure
2973 * @lvl: option level
2974 * @opt: option identifier
2975 * @ov: pointer to new option value
2976 * @ol: length of option value
2977 *
2978 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
2979 * (to ease compatibility).
2980 *
2981 * Returns 0 on success, errno otherwise
2982 */
2983static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2984 char __user *ov, unsigned int ol)
2985{
2986 struct sock *sk = sock->sk;
2987 struct tipc_sock *tsk = tipc_sk(sk);
2988 struct tipc_group_req mreq;
2989 u32 value = 0;
2990 int res = 0;
2991
2992 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2993 return 0;
2994 if (lvl != SOL_TIPC)
2995 return -ENOPROTOOPT;
2996
2997 switch (opt) {
2998 case TIPC_IMPORTANCE:
2999 case TIPC_SRC_DROPPABLE:
3000 case TIPC_DEST_DROPPABLE:
3001 case TIPC_CONN_TIMEOUT:
3002 if (ol < sizeof(value))
3003 return -EINVAL;
3004 if (get_user(value, (u32 __user *)ov))
3005 return -EFAULT;
3006 break;
3007 case TIPC_GROUP_JOIN:
3008 if (ol < sizeof(mreq))
3009 return -EINVAL;
3010 if (copy_from_user(&mreq, ov, sizeof(mreq)))
3011 return -EFAULT;
3012 break;
3013 default:
3014 if (ov || ol)
3015 return -EINVAL;
3016 }
3017
3018 lock_sock(sk);
3019
3020 switch (opt) {
3021 case TIPC_IMPORTANCE:
3022 res = tsk_set_importance(tsk, value);
3023 break;
3024 case TIPC_SRC_DROPPABLE:
3025 if (sock->type != SOCK_STREAM)
3026 tsk_set_unreliable(tsk, value);
3027 else
3028 res = -ENOPROTOOPT;
3029 break;
3030 case TIPC_DEST_DROPPABLE:
3031 tsk_set_unreturnable(tsk, value);
3032 break;
3033 case TIPC_CONN_TIMEOUT:
3034 tipc_sk(sk)->conn_timeout = value;
3035 break;
3036 case TIPC_MCAST_BROADCAST:
3037 tsk->mc_method.rcast = false;
3038 tsk->mc_method.mandatory = true;
3039 break;
3040 case TIPC_MCAST_REPLICAST:
3041 tsk->mc_method.rcast = true;
3042 tsk->mc_method.mandatory = true;
3043 break;
3044 case TIPC_GROUP_JOIN:
3045 res = tipc_sk_join(tsk, &mreq);
3046 break;
3047 case TIPC_GROUP_LEAVE:
3048 res = tipc_sk_leave(tsk);
3049 break;
3050 default:
3051 res = -EINVAL;
3052 }
3053
3054 release_sock(sk);
3055
3056 return res;
3057}
3058
3059/**
3060 * tipc_getsockopt - get socket option
3061 * @sock: socket structure
3062 * @lvl: option level
3063 * @opt: option identifier
3064 * @ov: receptacle for option value
3065 * @ol: receptacle for length of option value
3066 *
3067 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
3068 * (to ease compatibility).
3069 *
3070 * Returns 0 on success, errno otherwise
3071 */
3072static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
3073 char __user *ov, int __user *ol)
3074{
3075 struct sock *sk = sock->sk;
3076 struct tipc_sock *tsk = tipc_sk(sk);
3077 struct tipc_name_seq seq;
3078 int len, scope;
3079 u32 value;
3080 int res;
3081
3082 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
3083 return put_user(0, ol);
3084 if (lvl != SOL_TIPC)
3085 return -ENOPROTOOPT;
3086 res = get_user(len, ol);
3087 if (res)
3088 return res;
3089
3090 lock_sock(sk);
3091
3092 switch (opt) {
3093 case TIPC_IMPORTANCE:
3094 value = tsk_importance(tsk);
3095 break;
3096 case TIPC_SRC_DROPPABLE:
3097 value = tsk_unreliable(tsk);
3098 break;
3099 case TIPC_DEST_DROPPABLE:
3100 value = tsk_unreturnable(tsk);
3101 break;
3102 case TIPC_CONN_TIMEOUT:
3103 value = tsk->conn_timeout;
3104 /* no need to set "res", since already 0 at this point */
3105 break;
3106 case TIPC_NODE_RECVQ_DEPTH:
3107 value = 0; /* was tipc_queue_size, now obsolete */
3108 break;
3109 case TIPC_SOCK_RECVQ_DEPTH:
3110 value = skb_queue_len(&sk->sk_receive_queue);
3111 break;
David Brazdil0f672f62019-12-10 10:32:29 +00003112 case TIPC_SOCK_RECVQ_USED:
3113 value = sk_rmem_alloc_get(sk);
3114 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003115 case TIPC_GROUP_JOIN:
3116 seq.type = 0;
3117 if (tsk->group)
3118 tipc_group_self(tsk->group, &seq, &scope);
3119 value = seq.type;
3120 break;
3121 default:
3122 res = -EINVAL;
3123 }
3124
3125 release_sock(sk);
3126
3127 if (res)
3128 return res; /* "get" failed */
3129
3130 if (len < sizeof(value))
3131 return -EINVAL;
3132
3133 if (copy_to_user(ov, &value, sizeof(value)))
3134 return -EFAULT;
3135
3136 return put_user(sizeof(value), ol);
3137}
3138
3139static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
3140{
3141 struct net *net = sock_net(sock->sk);
3142 struct tipc_sioc_nodeid_req nr = {0};
3143 struct tipc_sioc_ln_req lnr;
3144 void __user *argp = (void __user *)arg;
3145
3146 switch (cmd) {
3147 case SIOCGETLINKNAME:
3148 if (copy_from_user(&lnr, argp, sizeof(lnr)))
3149 return -EFAULT;
3150 if (!tipc_node_get_linkname(net,
3151 lnr.bearer_id & 0xffff, lnr.peer,
3152 lnr.linkname, TIPC_MAX_LINK_NAME)) {
3153 if (copy_to_user(argp, &lnr, sizeof(lnr)))
3154 return -EFAULT;
3155 return 0;
3156 }
3157 return -EADDRNOTAVAIL;
3158 case SIOCGETNODEID:
3159 if (copy_from_user(&nr, argp, sizeof(nr)))
3160 return -EFAULT;
3161 if (!tipc_node_get_id(net, nr.peer, nr.node_id))
3162 return -EADDRNOTAVAIL;
3163 if (copy_to_user(argp, &nr, sizeof(nr)))
3164 return -EFAULT;
3165 return 0;
3166 default:
3167 return -ENOIOCTLCMD;
3168 }
3169}
3170
3171static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
3172{
3173 struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
3174 struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
3175 u32 onode = tipc_own_addr(sock_net(sock1->sk));
3176
3177 tsk1->peer.family = AF_TIPC;
3178 tsk1->peer.addrtype = TIPC_ADDR_ID;
3179 tsk1->peer.scope = TIPC_NODE_SCOPE;
3180 tsk1->peer.addr.id.ref = tsk2->portid;
3181 tsk1->peer.addr.id.node = onode;
3182 tsk2->peer.family = AF_TIPC;
3183 tsk2->peer.addrtype = TIPC_ADDR_ID;
3184 tsk2->peer.scope = TIPC_NODE_SCOPE;
3185 tsk2->peer.addr.id.ref = tsk1->portid;
3186 tsk2->peer.addr.id.node = onode;
3187
3188 tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
3189 tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
3190 return 0;
3191}
3192
3193/* Protocol switches for the various types of TIPC sockets */
3194
3195static const struct proto_ops msg_ops = {
3196 .owner = THIS_MODULE,
3197 .family = AF_TIPC,
3198 .release = tipc_release,
3199 .bind = tipc_bind,
3200 .connect = tipc_connect,
3201 .socketpair = tipc_socketpair,
3202 .accept = sock_no_accept,
3203 .getname = tipc_getname,
3204 .poll = tipc_poll,
3205 .ioctl = tipc_ioctl,
3206 .listen = sock_no_listen,
3207 .shutdown = tipc_shutdown,
3208 .setsockopt = tipc_setsockopt,
3209 .getsockopt = tipc_getsockopt,
3210 .sendmsg = tipc_sendmsg,
3211 .recvmsg = tipc_recvmsg,
3212 .mmap = sock_no_mmap,
3213 .sendpage = sock_no_sendpage
3214};
3215
3216static const struct proto_ops packet_ops = {
3217 .owner = THIS_MODULE,
3218 .family = AF_TIPC,
3219 .release = tipc_release,
3220 .bind = tipc_bind,
3221 .connect = tipc_connect,
3222 .socketpair = tipc_socketpair,
3223 .accept = tipc_accept,
3224 .getname = tipc_getname,
3225 .poll = tipc_poll,
3226 .ioctl = tipc_ioctl,
3227 .listen = tipc_listen,
3228 .shutdown = tipc_shutdown,
3229 .setsockopt = tipc_setsockopt,
3230 .getsockopt = tipc_getsockopt,
3231 .sendmsg = tipc_send_packet,
3232 .recvmsg = tipc_recvmsg,
3233 .mmap = sock_no_mmap,
3234 .sendpage = sock_no_sendpage
3235};
3236
3237static const struct proto_ops stream_ops = {
3238 .owner = THIS_MODULE,
3239 .family = AF_TIPC,
3240 .release = tipc_release,
3241 .bind = tipc_bind,
3242 .connect = tipc_connect,
3243 .socketpair = tipc_socketpair,
3244 .accept = tipc_accept,
3245 .getname = tipc_getname,
3246 .poll = tipc_poll,
3247 .ioctl = tipc_ioctl,
3248 .listen = tipc_listen,
3249 .shutdown = tipc_shutdown,
3250 .setsockopt = tipc_setsockopt,
3251 .getsockopt = tipc_getsockopt,
3252 .sendmsg = tipc_sendstream,
3253 .recvmsg = tipc_recvstream,
3254 .mmap = sock_no_mmap,
3255 .sendpage = sock_no_sendpage
3256};
3257
3258static const struct net_proto_family tipc_family_ops = {
3259 .owner = THIS_MODULE,
3260 .family = AF_TIPC,
3261 .create = tipc_sk_create
3262};
3263
3264static struct proto tipc_proto = {
3265 .name = "TIPC",
3266 .owner = THIS_MODULE,
3267 .obj_size = sizeof(struct tipc_sock),
3268 .sysctl_rmem = sysctl_tipc_rmem
3269};
3270
3271/**
3272 * tipc_socket_init - initialize TIPC socket interface
3273 *
3274 * Returns 0 on success, errno otherwise
3275 */
3276int tipc_socket_init(void)
3277{
3278 int res;
3279
3280 res = proto_register(&tipc_proto, 1);
3281 if (res) {
3282 pr_err("Failed to register TIPC protocol type\n");
3283 goto out;
3284 }
3285
3286 res = sock_register(&tipc_family_ops);
3287 if (res) {
3288 pr_err("Failed to register TIPC socket type\n");
3289 proto_unregister(&tipc_proto);
3290 goto out;
3291 }
3292 out:
3293 return res;
3294}
3295
3296/**
3297 * tipc_socket_stop - stop TIPC socket interface
3298 */
3299void tipc_socket_stop(void)
3300{
3301 sock_unregister(tipc_family_ops.family);
3302 proto_unregister(&tipc_proto);
3303}
3304
3305/* Caller should hold socket lock for the passed tipc socket. */
3306static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
3307{
3308 u32 peer_node;
3309 u32 peer_port;
3310 struct nlattr *nest;
3311
3312 peer_node = tsk_peer_node(tsk);
3313 peer_port = tsk_peer_port(tsk);
3314
David Brazdil0f672f62019-12-10 10:32:29 +00003315 nest = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_CON);
3316 if (!nest)
3317 return -EMSGSIZE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003318
3319 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
3320 goto msg_full;
3321 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
3322 goto msg_full;
3323
3324 if (tsk->conn_type != 0) {
3325 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
3326 goto msg_full;
3327 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
3328 goto msg_full;
3329 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
3330 goto msg_full;
3331 }
3332 nla_nest_end(skb, nest);
3333
3334 return 0;
3335
3336msg_full:
3337 nla_nest_cancel(skb, nest);
3338
3339 return -EMSGSIZE;
3340}
3341
3342static int __tipc_nl_add_sk_info(struct sk_buff *skb, struct tipc_sock
3343 *tsk)
3344{
3345 struct net *net = sock_net(skb->sk);
3346 struct sock *sk = &tsk->sk;
3347
3348 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid) ||
3349 nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr(net)))
3350 return -EMSGSIZE;
3351
3352 if (tipc_sk_connected(sk)) {
3353 if (__tipc_nl_add_sk_con(skb, tsk))
3354 return -EMSGSIZE;
3355 } else if (!list_empty(&tsk->publications)) {
3356 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
3357 return -EMSGSIZE;
3358 }
3359 return 0;
3360}
3361
3362/* Caller should hold socket lock for the passed tipc socket. */
3363static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
3364 struct tipc_sock *tsk)
3365{
3366 struct nlattr *attrs;
3367 void *hdr;
3368
3369 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3370 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
3371 if (!hdr)
3372 goto msg_cancel;
3373
David Brazdil0f672f62019-12-10 10:32:29 +00003374 attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003375 if (!attrs)
3376 goto genlmsg_cancel;
3377
3378 if (__tipc_nl_add_sk_info(skb, tsk))
3379 goto attr_msg_cancel;
3380
3381 nla_nest_end(skb, attrs);
3382 genlmsg_end(skb, hdr);
3383
3384 return 0;
3385
3386attr_msg_cancel:
3387 nla_nest_cancel(skb, attrs);
3388genlmsg_cancel:
3389 genlmsg_cancel(skb, hdr);
3390msg_cancel:
3391 return -EMSGSIZE;
3392}
3393
3394int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,
3395 int (*skb_handler)(struct sk_buff *skb,
3396 struct netlink_callback *cb,
3397 struct tipc_sock *tsk))
3398{
3399 struct rhashtable_iter *iter = (void *)cb->args[4];
3400 struct tipc_sock *tsk;
3401 int err;
3402
3403 rhashtable_walk_start(iter);
3404 while ((tsk = rhashtable_walk_next(iter)) != NULL) {
3405 if (IS_ERR(tsk)) {
3406 err = PTR_ERR(tsk);
3407 if (err == -EAGAIN) {
3408 err = 0;
3409 continue;
3410 }
3411 break;
3412 }
3413
3414 sock_hold(&tsk->sk);
3415 rhashtable_walk_stop(iter);
3416 lock_sock(&tsk->sk);
3417 err = skb_handler(skb, cb, tsk);
3418 if (err) {
3419 release_sock(&tsk->sk);
3420 sock_put(&tsk->sk);
3421 goto out;
3422 }
3423 release_sock(&tsk->sk);
3424 rhashtable_walk_start(iter);
3425 sock_put(&tsk->sk);
3426 }
3427 rhashtable_walk_stop(iter);
3428out:
3429 return skb->len;
3430}
3431EXPORT_SYMBOL(tipc_nl_sk_walk);
3432
3433int tipc_dump_start(struct netlink_callback *cb)
3434{
3435 return __tipc_dump_start(cb, sock_net(cb->skb->sk));
3436}
3437EXPORT_SYMBOL(tipc_dump_start);
3438
3439int __tipc_dump_start(struct netlink_callback *cb, struct net *net)
3440{
3441 /* tipc_nl_name_table_dump() uses cb->args[0...3]. */
3442 struct rhashtable_iter *iter = (void *)cb->args[4];
3443 struct tipc_net *tn = tipc_net(net);
3444
3445 if (!iter) {
3446 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3447 if (!iter)
3448 return -ENOMEM;
3449
3450 cb->args[4] = (long)iter;
3451 }
3452
3453 rhashtable_walk_enter(&tn->sk_rht, iter);
3454 return 0;
3455}
3456
3457int tipc_dump_done(struct netlink_callback *cb)
3458{
3459 struct rhashtable_iter *hti = (void *)cb->args[4];
3460
3461 rhashtable_walk_exit(hti);
3462 kfree(hti);
3463 return 0;
3464}
3465EXPORT_SYMBOL(tipc_dump_done);
3466
3467int tipc_sk_fill_sock_diag(struct sk_buff *skb, struct netlink_callback *cb,
3468 struct tipc_sock *tsk, u32 sk_filter_state,
3469 u64 (*tipc_diag_gen_cookie)(struct sock *sk))
3470{
3471 struct sock *sk = &tsk->sk;
3472 struct nlattr *attrs;
3473 struct nlattr *stat;
3474
3475 /*filter response w.r.t sk_state*/
3476 if (!(sk_filter_state & (1 << sk->sk_state)))
3477 return 0;
3478
David Brazdil0f672f62019-12-10 10:32:29 +00003479 attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003480 if (!attrs)
3481 goto msg_cancel;
3482
3483 if (__tipc_nl_add_sk_info(skb, tsk))
3484 goto attr_msg_cancel;
3485
3486 if (nla_put_u32(skb, TIPC_NLA_SOCK_TYPE, (u32)sk->sk_type) ||
3487 nla_put_u32(skb, TIPC_NLA_SOCK_TIPC_STATE, (u32)sk->sk_state) ||
3488 nla_put_u32(skb, TIPC_NLA_SOCK_INO, sock_i_ino(sk)) ||
3489 nla_put_u32(skb, TIPC_NLA_SOCK_UID,
3490 from_kuid_munged(sk_user_ns(NETLINK_CB(cb->skb).sk),
3491 sock_i_uid(sk))) ||
3492 nla_put_u64_64bit(skb, TIPC_NLA_SOCK_COOKIE,
3493 tipc_diag_gen_cookie(sk),
3494 TIPC_NLA_SOCK_PAD))
3495 goto attr_msg_cancel;
3496
David Brazdil0f672f62019-12-10 10:32:29 +00003497 stat = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_STAT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003498 if (!stat)
3499 goto attr_msg_cancel;
3500
3501 if (nla_put_u32(skb, TIPC_NLA_SOCK_STAT_RCVQ,
3502 skb_queue_len(&sk->sk_receive_queue)) ||
3503 nla_put_u32(skb, TIPC_NLA_SOCK_STAT_SENDQ,
3504 skb_queue_len(&sk->sk_write_queue)) ||
3505 nla_put_u32(skb, TIPC_NLA_SOCK_STAT_DROP,
3506 atomic_read(&sk->sk_drops)))
3507 goto stat_msg_cancel;
3508
3509 if (tsk->cong_link_cnt &&
3510 nla_put_flag(skb, TIPC_NLA_SOCK_STAT_LINK_CONG))
3511 goto stat_msg_cancel;
3512
3513 if (tsk_conn_cong(tsk) &&
3514 nla_put_flag(skb, TIPC_NLA_SOCK_STAT_CONN_CONG))
3515 goto stat_msg_cancel;
3516
3517 nla_nest_end(skb, stat);
3518
3519 if (tsk->group)
3520 if (tipc_group_fill_sock_diag(tsk->group, skb))
3521 goto stat_msg_cancel;
3522
3523 nla_nest_end(skb, attrs);
3524
3525 return 0;
3526
3527stat_msg_cancel:
3528 nla_nest_cancel(skb, stat);
3529attr_msg_cancel:
3530 nla_nest_cancel(skb, attrs);
3531msg_cancel:
3532 return -EMSGSIZE;
3533}
3534EXPORT_SYMBOL(tipc_sk_fill_sock_diag);
3535
3536int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
3537{
3538 return tipc_nl_sk_walk(skb, cb, __tipc_nl_add_sk);
3539}
3540
3541/* Caller should hold socket lock for the passed tipc socket. */
3542static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
3543 struct netlink_callback *cb,
3544 struct publication *publ)
3545{
3546 void *hdr;
3547 struct nlattr *attrs;
3548
3549 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3550 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
3551 if (!hdr)
3552 goto msg_cancel;
3553
David Brazdil0f672f62019-12-10 10:32:29 +00003554 attrs = nla_nest_start_noflag(skb, TIPC_NLA_PUBL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003555 if (!attrs)
3556 goto genlmsg_cancel;
3557
3558 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
3559 goto attr_msg_cancel;
3560 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
3561 goto attr_msg_cancel;
3562 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
3563 goto attr_msg_cancel;
3564 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
3565 goto attr_msg_cancel;
3566
3567 nla_nest_end(skb, attrs);
3568 genlmsg_end(skb, hdr);
3569
3570 return 0;
3571
3572attr_msg_cancel:
3573 nla_nest_cancel(skb, attrs);
3574genlmsg_cancel:
3575 genlmsg_cancel(skb, hdr);
3576msg_cancel:
3577 return -EMSGSIZE;
3578}
3579
3580/* Caller should hold socket lock for the passed tipc socket. */
3581static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
3582 struct netlink_callback *cb,
3583 struct tipc_sock *tsk, u32 *last_publ)
3584{
3585 int err;
3586 struct publication *p;
3587
3588 if (*last_publ) {
3589 list_for_each_entry(p, &tsk->publications, binding_sock) {
3590 if (p->key == *last_publ)
3591 break;
3592 }
3593 if (p->key != *last_publ) {
3594 /* We never set seq or call nl_dump_check_consistent()
3595 * this means that setting prev_seq here will cause the
3596 * consistence check to fail in the netlink callback
3597 * handler. Resulting in the last NLMSG_DONE message
3598 * having the NLM_F_DUMP_INTR flag set.
3599 */
3600 cb->prev_seq = 1;
3601 *last_publ = 0;
3602 return -EPIPE;
3603 }
3604 } else {
3605 p = list_first_entry(&tsk->publications, struct publication,
3606 binding_sock);
3607 }
3608
3609 list_for_each_entry_from(p, &tsk->publications, binding_sock) {
3610 err = __tipc_nl_add_sk_publ(skb, cb, p);
3611 if (err) {
3612 *last_publ = p->key;
3613 return err;
3614 }
3615 }
3616 *last_publ = 0;
3617
3618 return 0;
3619}
3620
3621int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
3622{
3623 int err;
3624 u32 tsk_portid = cb->args[0];
3625 u32 last_publ = cb->args[1];
3626 u32 done = cb->args[2];
3627 struct net *net = sock_net(skb->sk);
3628 struct tipc_sock *tsk;
3629
3630 if (!tsk_portid) {
3631 struct nlattr **attrs;
3632 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
3633
3634 err = tipc_nlmsg_parse(cb->nlh, &attrs);
3635 if (err)
3636 return err;
3637
3638 if (!attrs[TIPC_NLA_SOCK])
3639 return -EINVAL;
3640
David Brazdil0f672f62019-12-10 10:32:29 +00003641 err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
3642 attrs[TIPC_NLA_SOCK],
3643 tipc_nl_sock_policy, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00003644 if (err)
3645 return err;
3646
3647 if (!sock[TIPC_NLA_SOCK_REF])
3648 return -EINVAL;
3649
3650 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
3651 }
3652
3653 if (done)
3654 return 0;
3655
3656 tsk = tipc_sk_lookup(net, tsk_portid);
3657 if (!tsk)
3658 return -EINVAL;
3659
3660 lock_sock(&tsk->sk);
3661 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
3662 if (!err)
3663 done = 1;
3664 release_sock(&tsk->sk);
3665 sock_put(&tsk->sk);
3666
3667 cb->args[0] = tsk_portid;
3668 cb->args[1] = last_publ;
3669 cb->args[2] = done;
3670
3671 return skb->len;
3672}
David Brazdil0f672f62019-12-10 10:32:29 +00003673
3674/**
3675 * tipc_sk_filtering - check if a socket should be traced
3676 * @sk: the socket to be examined
3677 * @sysctl_tipc_sk_filter[]: the socket tuple for filtering,
3678 * (portid, sock type, name type, name lower, name upper)
3679 *
3680 * Returns true if the socket meets the socket tuple data
3681 * (value 0 = 'any') or when there is no tuple set (all = 0),
3682 * otherwise false
3683 */
3684bool tipc_sk_filtering(struct sock *sk)
3685{
3686 struct tipc_sock *tsk;
3687 struct publication *p;
3688 u32 _port, _sktype, _type, _lower, _upper;
3689 u32 type = 0, lower = 0, upper = 0;
3690
3691 if (!sk)
3692 return true;
3693
3694 tsk = tipc_sk(sk);
3695
3696 _port = sysctl_tipc_sk_filter[0];
3697 _sktype = sysctl_tipc_sk_filter[1];
3698 _type = sysctl_tipc_sk_filter[2];
3699 _lower = sysctl_tipc_sk_filter[3];
3700 _upper = sysctl_tipc_sk_filter[4];
3701
3702 if (!_port && !_sktype && !_type && !_lower && !_upper)
3703 return true;
3704
3705 if (_port)
3706 return (_port == tsk->portid);
3707
3708 if (_sktype && _sktype != sk->sk_type)
3709 return false;
3710
3711 if (tsk->published) {
3712 p = list_first_entry_or_null(&tsk->publications,
3713 struct publication, binding_sock);
3714 if (p) {
3715 type = p->type;
3716 lower = p->lower;
3717 upper = p->upper;
3718 }
3719 }
3720
3721 if (!tipc_sk_type_connectionless(sk)) {
3722 type = tsk->conn_type;
3723 lower = tsk->conn_instance;
3724 upper = tsk->conn_instance;
3725 }
3726
3727 if ((_type && _type != type) || (_lower && _lower != lower) ||
3728 (_upper && _upper != upper))
3729 return false;
3730
3731 return true;
3732}
3733
3734u32 tipc_sock_get_portid(struct sock *sk)
3735{
3736 return (sk) ? (tipc_sk(sk))->portid : 0;
3737}
3738
3739/**
3740 * tipc_sk_overlimit1 - check if socket rx queue is about to be overloaded,
3741 * both the rcv and backlog queues are considered
3742 * @sk: tipc sk to be checked
3743 * @skb: tipc msg to be checked
3744 *
3745 * Returns true if the socket rx queue allocation is > 90%, otherwise false
3746 */
3747
3748bool tipc_sk_overlimit1(struct sock *sk, struct sk_buff *skb)
3749{
3750 atomic_t *dcnt = &tipc_sk(sk)->dupl_rcvcnt;
3751 unsigned int lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
3752 unsigned int qsize = sk->sk_backlog.len + sk_rmem_alloc_get(sk);
3753
3754 return (qsize > lim * 90 / 100);
3755}
3756
3757/**
3758 * tipc_sk_overlimit2 - check if socket rx queue is about to be overloaded,
3759 * only the rcv queue is considered
3760 * @sk: tipc sk to be checked
3761 * @skb: tipc msg to be checked
3762 *
3763 * Returns true if the socket rx queue allocation is > 90%, otherwise false
3764 */
3765
3766bool tipc_sk_overlimit2(struct sock *sk, struct sk_buff *skb)
3767{
3768 unsigned int lim = rcvbuf_limit(sk, skb);
3769 unsigned int qsize = sk_rmem_alloc_get(sk);
3770
3771 return (qsize > lim * 90 / 100);
3772}
3773
3774/**
3775 * tipc_sk_dump - dump TIPC socket
3776 * @sk: tipc sk to be dumped
3777 * @dqueues: bitmask to decide if any socket queue to be dumped?
3778 * - TIPC_DUMP_NONE: don't dump socket queues
3779 * - TIPC_DUMP_SK_SNDQ: dump socket send queue
3780 * - TIPC_DUMP_SK_RCVQ: dump socket rcv queue
3781 * - TIPC_DUMP_SK_BKLGQ: dump socket backlog queue
3782 * - TIPC_DUMP_ALL: dump all the socket queues above
3783 * @buf: returned buffer of dump data in format
3784 */
3785int tipc_sk_dump(struct sock *sk, u16 dqueues, char *buf)
3786{
3787 int i = 0;
3788 size_t sz = (dqueues) ? SK_LMAX : SK_LMIN;
3789 struct tipc_sock *tsk;
3790 struct publication *p;
3791 bool tsk_connected;
3792
3793 if (!sk) {
3794 i += scnprintf(buf, sz, "sk data: (null)\n");
3795 return i;
3796 }
3797
3798 tsk = tipc_sk(sk);
3799 tsk_connected = !tipc_sk_type_connectionless(sk);
3800
3801 i += scnprintf(buf, sz, "sk data: %u", sk->sk_type);
3802 i += scnprintf(buf + i, sz - i, " %d", sk->sk_state);
3803 i += scnprintf(buf + i, sz - i, " %x", tsk_own_node(tsk));
3804 i += scnprintf(buf + i, sz - i, " %u", tsk->portid);
3805 i += scnprintf(buf + i, sz - i, " | %u", tsk_connected);
3806 if (tsk_connected) {
3807 i += scnprintf(buf + i, sz - i, " %x", tsk_peer_node(tsk));
3808 i += scnprintf(buf + i, sz - i, " %u", tsk_peer_port(tsk));
3809 i += scnprintf(buf + i, sz - i, " %u", tsk->conn_type);
3810 i += scnprintf(buf + i, sz - i, " %u", tsk->conn_instance);
3811 }
3812 i += scnprintf(buf + i, sz - i, " | %u", tsk->published);
3813 if (tsk->published) {
3814 p = list_first_entry_or_null(&tsk->publications,
3815 struct publication, binding_sock);
3816 i += scnprintf(buf + i, sz - i, " %u", (p) ? p->type : 0);
3817 i += scnprintf(buf + i, sz - i, " %u", (p) ? p->lower : 0);
3818 i += scnprintf(buf + i, sz - i, " %u", (p) ? p->upper : 0);
3819 }
3820 i += scnprintf(buf + i, sz - i, " | %u", tsk->snd_win);
3821 i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_win);
3822 i += scnprintf(buf + i, sz - i, " %u", tsk->max_pkt);
3823 i += scnprintf(buf + i, sz - i, " %x", tsk->peer_caps);
3824 i += scnprintf(buf + i, sz - i, " %u", tsk->cong_link_cnt);
3825 i += scnprintf(buf + i, sz - i, " %u", tsk->snt_unacked);
3826 i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_unacked);
3827 i += scnprintf(buf + i, sz - i, " %u", atomic_read(&tsk->dupl_rcvcnt));
3828 i += scnprintf(buf + i, sz - i, " %u", sk->sk_shutdown);
3829 i += scnprintf(buf + i, sz - i, " | %d", sk_wmem_alloc_get(sk));
3830 i += scnprintf(buf + i, sz - i, " %d", sk->sk_sndbuf);
3831 i += scnprintf(buf + i, sz - i, " | %d", sk_rmem_alloc_get(sk));
3832 i += scnprintf(buf + i, sz - i, " %d", sk->sk_rcvbuf);
3833 i += scnprintf(buf + i, sz - i, " | %d\n", READ_ONCE(sk->sk_backlog.len));
3834
3835 if (dqueues & TIPC_DUMP_SK_SNDQ) {
3836 i += scnprintf(buf + i, sz - i, "sk_write_queue: ");
3837 i += tipc_list_dump(&sk->sk_write_queue, false, buf + i);
3838 }
3839
3840 if (dqueues & TIPC_DUMP_SK_RCVQ) {
3841 i += scnprintf(buf + i, sz - i, "sk_receive_queue: ");
3842 i += tipc_list_dump(&sk->sk_receive_queue, false, buf + i);
3843 }
3844
3845 if (dqueues & TIPC_DUMP_SK_BKLGQ) {
3846 i += scnprintf(buf + i, sz - i, "sk_backlog:\n head ");
3847 i += tipc_skb_dump(sk->sk_backlog.head, false, buf + i);
3848 if (sk->sk_backlog.tail != sk->sk_backlog.head) {
3849 i += scnprintf(buf + i, sz - i, " tail ");
3850 i += tipc_skb_dump(sk->sk_backlog.tail, false,
3851 buf + i);
3852 }
3853 }
3854
3855 return i;
3856}