2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2001-2007, 2012-2019, Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
6 * Copyright (c) 2020-2021, Red Hat Inc
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
21 * Alternatively, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL") version 2 as published by the Free
23 * Software Foundation.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 #include <linux/rhashtable.h>
39 #include <linux/sched/signal.h>
42 #include "name_table.h"
45 #include "name_distr.h"
52 #define NAGLE_START_INIT 4
53 #define NAGLE_START_MAX 1024
54 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
55 #define CONN_PROBING_INTV msecs_to_jiffies(3600000) /* [ms] => 1 h */
56 #define TIPC_MAX_PORT 0xffffffff
57 #define TIPC_MIN_PORT 1
58 #define TIPC_ACK_RATE 4 /* ACK at 1/4 of rcv window size */
61 TIPC_LISTEN = TCP_LISTEN,
62 TIPC_ESTABLISHED = TCP_ESTABLISHED,
63 TIPC_OPEN = TCP_CLOSE,
64 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
65 TIPC_CONNECTING = TCP_SYN_SENT,
68 struct sockaddr_pair {
69 struct sockaddr_tipc sock;
70 struct sockaddr_tipc member;
74 * struct tipc_sock - TIPC socket structure
75 * @sk: socket - interacts with 'port' and with user via the socket API
76 * @conn_type: TIPC type used when connection was established
77 * @conn_instance: TIPC instance used when connection was established
78 * @published: non-zero if port has one or more associated names
79 * @max_pkt: maximum packet size "hint" used when building messages sent by port
80 * @maxnagle: maximum size of msg which can be subject to nagle
81 * @portid: unique port identity in TIPC socket hash table
82 * @phdr: preformatted message header used when sending messages
83 * @cong_links: list of congested links
84 * @publications: list of publications for port
85 * @blocking_link: address of the congested link we are currently sleeping on
86 * @pub_count: total # of publications port has made during its lifetime
87 * @conn_timeout: the time we can wait for an unresponded setup request
88 * @probe_unacked: probe has not received ack yet
89 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
90 * @cong_link_cnt: number of congested links
91 * @snt_unacked: # messages sent by socket, and not yet acked by peer
92 * @snd_win: send window size
93 * @peer_caps: peer capabilities mask
94 * @rcv_unacked: # messages read by user, but not yet acked back to peer
95 * @rcv_win: receive window size
96 * @peer: 'connected' peer for dgram/rdm
97 * @node: hash table node
98 * @mc_method: cookie for use between socket and broadcast layer
99 * @rcu: rcu struct for tipc_sock
100 * @group: TIPC communications group
101 * @oneway: message count in one direction (FIXME)
102 * @nagle_start: current nagle value
103 * @snd_backlog: send backlog count
104 * @msg_acc: messages accepted; used in managing backlog and nagle
105 * @pkt_cnt: TIPC socket packet count
106 * @expect_ack: whether this TIPC socket is expecting an ack
107 * @nodelay: setsockopt() TIPC_NODELAY setting
108 * @group_is_open: TIPC socket group is fully open (FIXME)
117 struct tipc_msg phdr;
118 struct list_head cong_links;
119 struct list_head publications;
121 atomic_t dupl_rcvcnt;
130 struct sockaddr_tipc peer;
131 struct rhash_head node;
132 struct tipc_mc_method mc_method;
134 struct tipc_group *group;
146 static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
147 static void tipc_data_ready(struct sock *sk);
148 static void tipc_write_space(struct sock *sk);
149 static void tipc_sock_destruct(struct sock *sk);
150 static int tipc_release(struct socket *sock);
151 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
153 static void tipc_sk_timeout(struct timer_list *t);
154 static int tipc_sk_publish(struct tipc_sock *tsk, struct tipc_uaddr *ua);
155 static int tipc_sk_withdraw(struct tipc_sock *tsk, struct tipc_uaddr *ua);
156 static int tipc_sk_leave(struct tipc_sock *tsk);
157 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
158 static int tipc_sk_insert(struct tipc_sock *tsk);
159 static void tipc_sk_remove(struct tipc_sock *tsk);
160 static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
161 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
162 static void tipc_sk_push_backlog(struct tipc_sock *tsk, bool nagle_ack);
164 static const struct proto_ops packet_ops;
165 static const struct proto_ops stream_ops;
166 static const struct proto_ops msg_ops;
167 static struct proto tipc_proto;
168 static const struct rhashtable_params tsk_rht_params;
170 static u32 tsk_own_node(struct tipc_sock *tsk)
172 return msg_prevnode(&tsk->phdr);
175 static u32 tsk_peer_node(struct tipc_sock *tsk)
177 return msg_destnode(&tsk->phdr);
180 static u32 tsk_peer_port(struct tipc_sock *tsk)
182 return msg_destport(&tsk->phdr);
185 static bool tsk_unreliable(struct tipc_sock *tsk)
187 return msg_src_droppable(&tsk->phdr) != 0;
190 static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
192 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
195 static bool tsk_unreturnable(struct tipc_sock *tsk)
197 return msg_dest_droppable(&tsk->phdr) != 0;
200 static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
202 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
205 static int tsk_importance(struct tipc_sock *tsk)
207 return msg_importance(&tsk->phdr);
210 static struct tipc_sock *tipc_sk(const struct sock *sk)
212 return container_of(sk, struct tipc_sock, sk);
215 int tsk_set_importance(struct sock *sk, int imp)
217 if (imp > TIPC_CRITICAL_IMPORTANCE)
219 msg_set_importance(&tipc_sk(sk)->phdr, (u32)imp);
223 static bool tsk_conn_cong(struct tipc_sock *tsk)
225 return tsk->snt_unacked > tsk->snd_win;
228 static u16 tsk_blocks(int len)
230 return ((len / FLOWCTL_BLK_SZ) + 1);
233 /* tsk_blocks(): translate a buffer size in bytes to number of
234 * advertisable blocks, taking into account the ratio truesize(len)/len
235 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
237 static u16 tsk_adv_blocks(int len)
239 return len / FLOWCTL_BLK_SZ / 4;
242 /* tsk_inc(): increment counter for sent or received data
243 * - If block based flow control is not supported by peer we
244 * fall back to message based ditto, incrementing the counter
246 static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
248 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
249 return ((msglen / FLOWCTL_BLK_SZ) + 1);
253 /* tsk_set_nagle - enable/disable nagle property by manipulating maxnagle
255 static void tsk_set_nagle(struct tipc_sock *tsk)
257 struct sock *sk = &tsk->sk;
260 if (sk->sk_type != SOCK_STREAM)
264 if (!(tsk->peer_caps & TIPC_NAGLE))
266 /* Limit node local buffer size to avoid receive queue overflow */
267 if (tsk->max_pkt == MAX_MSG_SIZE)
268 tsk->maxnagle = 1500;
270 tsk->maxnagle = tsk->max_pkt;
274 * tsk_advance_rx_queue - discard first buffer in socket receive queue
275 * @sk: network socket
277 * Caller must hold socket lock
279 static void tsk_advance_rx_queue(struct sock *sk)
281 trace_tipc_sk_advance_rx(sk, NULL, TIPC_DUMP_SK_RCVQ, " ");
282 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
285 /* tipc_sk_respond() : send response message back to sender
287 static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
291 u32 onode = tipc_own_addr(sock_net(sk));
293 if (!tipc_msg_reverse(onode, &skb, err))
296 trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE, "@sk_respond!");
297 dnode = msg_destnode(buf_msg(skb));
298 selector = msg_origport(buf_msg(skb));
299 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
303 * tsk_rej_rx_queue - reject all buffers in socket receive queue
304 * @sk: network socket
305 * @error: response error code
307 * Caller must hold socket lock
309 static void tsk_rej_rx_queue(struct sock *sk, int error)
313 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
314 tipc_sk_respond(sk, skb, error);
317 static bool tipc_sk_connected(struct sock *sk)
319 return sk->sk_state == TIPC_ESTABLISHED;
322 /* tipc_sk_type_connectionless - check if the socket is datagram socket
325 * Returns true if connection less, false otherwise
327 static bool tipc_sk_type_connectionless(struct sock *sk)
329 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
332 /* tsk_peer_msg - verify if message was sent by connected port's peer
334 * Handles cases where the node's network address has changed from
335 * the default of <0.0.0> to its configured setting.
337 static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
339 struct sock *sk = &tsk->sk;
340 u32 self = tipc_own_addr(sock_net(sk));
341 u32 peer_port = tsk_peer_port(tsk);
342 u32 orig_node, peer_node;
344 if (unlikely(!tipc_sk_connected(sk)))
347 if (unlikely(msg_origport(msg) != peer_port))
350 orig_node = msg_orignode(msg);
351 peer_node = tsk_peer_node(tsk);
353 if (likely(orig_node == peer_node))
356 if (!orig_node && peer_node == self)
359 if (!peer_node && orig_node == self)
365 /* tipc_set_sk_state - set the sk_state of the socket
368 * Caller must hold socket lock
370 * Returns 0 on success, errno otherwise
372 static int tipc_set_sk_state(struct sock *sk, int state)
374 int oldsk_state = sk->sk_state;
382 case TIPC_CONNECTING:
383 if (oldsk_state == TIPC_OPEN)
386 case TIPC_ESTABLISHED:
387 if (oldsk_state == TIPC_CONNECTING ||
388 oldsk_state == TIPC_OPEN)
391 case TIPC_DISCONNECTING:
392 if (oldsk_state == TIPC_CONNECTING ||
393 oldsk_state == TIPC_ESTABLISHED)
399 sk->sk_state = state;
404 static int tipc_sk_sock_err(struct socket *sock, long *timeout)
406 struct sock *sk = sock->sk;
407 int err = sock_error(sk);
408 int typ = sock->type;
412 if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
413 if (sk->sk_state == TIPC_DISCONNECTING)
415 else if (!tipc_sk_connected(sk))
420 if (signal_pending(current))
421 return sock_intr_errno(*timeout);
426 #define tipc_wait_for_cond(sock_, timeo_, condition_) \
428 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
432 while ((rc_ = !(condition_))) { \
433 /* coupled with smp_wmb() in tipc_sk_proto_rcv() */ \
436 rc_ = tipc_sk_sock_err((sock_), timeo_); \
439 add_wait_queue(sk_sleep(sk_), &wait_); \
441 *(timeo_) = wait_woken(&wait_, TASK_INTERRUPTIBLE, *(timeo_)); \
442 sched_annotate_sleep(); \
444 remove_wait_queue(sk_sleep(sk_), &wait_); \
450 * tipc_sk_create - create a TIPC socket
451 * @net: network namespace (must be default network)
452 * @sock: pre-allocated socket structure
453 * @protocol: protocol indicator (must be 0)
454 * @kern: caused by kernel or by userspace?
456 * This routine creates additional data structures used by the TIPC socket,
457 * initializes them, and links them together.
459 * Return: 0 on success, errno otherwise
461 static int tipc_sk_create(struct net *net, struct socket *sock,
462 int protocol, int kern)
464 const struct proto_ops *ops;
466 struct tipc_sock *tsk;
467 struct tipc_msg *msg;
469 /* Validate arguments */
470 if (unlikely(protocol != 0))
471 return -EPROTONOSUPPORT;
473 switch (sock->type) {
488 /* Allocate socket's protocol area */
489 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
494 tsk->max_pkt = MAX_PKT_DEFAULT;
496 tsk->nagle_start = NAGLE_START_INIT;
497 INIT_LIST_HEAD(&tsk->publications);
498 INIT_LIST_HEAD(&tsk->cong_links);
501 /* Finish initializing socket data structures */
503 sock_init_data(sock, sk);
504 tipc_set_sk_state(sk, TIPC_OPEN);
505 if (tipc_sk_insert(tsk)) {
506 pr_warn("Socket create failed; port number exhausted\n");
510 /* Ensure tsk is visible before we read own_addr. */
513 tipc_msg_init(tipc_own_addr(net), msg, TIPC_LOW_IMPORTANCE,
514 TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
516 msg_set_origport(msg, tsk->portid);
517 timer_setup(&sk->sk_timer, tipc_sk_timeout, 0);
519 sk->sk_backlog_rcv = tipc_sk_backlog_rcv;
520 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
521 sk->sk_data_ready = tipc_data_ready;
522 sk->sk_write_space = tipc_write_space;
523 sk->sk_destruct = tipc_sock_destruct;
524 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
525 tsk->group_is_open = true;
526 atomic_set(&tsk->dupl_rcvcnt, 0);
528 /* Start out with safe limits until we receive an advertised window */
529 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
530 tsk->rcv_win = tsk->snd_win;
532 if (tipc_sk_type_connectionless(sk)) {
533 tsk_set_unreturnable(tsk, true);
534 if (sock->type == SOCK_DGRAM)
535 tsk_set_unreliable(tsk, true);
537 __skb_queue_head_init(&tsk->mc_method.deferredq);
538 trace_tipc_sk_create(sk, NULL, TIPC_DUMP_NONE, " ");
542 static void tipc_sk_callback(struct rcu_head *head)
544 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
549 /* Caller should hold socket lock for the socket. */
550 static void __tipc_shutdown(struct socket *sock, int error)
552 struct sock *sk = sock->sk;
553 struct tipc_sock *tsk = tipc_sk(sk);
554 struct net *net = sock_net(sk);
555 long timeout = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
556 u32 dnode = tsk_peer_node(tsk);
559 /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
560 tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
561 !tsk_conn_cong(tsk)));
563 /* Push out delayed messages if in Nagle mode */
564 tipc_sk_push_backlog(tsk, false);
565 /* Remove pending SYN */
566 __skb_queue_purge(&sk->sk_write_queue);
568 /* Remove partially received buffer if any */
569 skb = skb_peek(&sk->sk_receive_queue);
570 if (skb && TIPC_SKB_CB(skb)->bytes_read) {
571 __skb_unlink(skb, &sk->sk_receive_queue);
575 /* Reject all unreceived messages if connectionless */
576 if (tipc_sk_type_connectionless(sk)) {
577 tsk_rej_rx_queue(sk, error);
581 switch (sk->sk_state) {
582 case TIPC_CONNECTING:
583 case TIPC_ESTABLISHED:
584 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
585 tipc_node_remove_conn(net, dnode, tsk->portid);
586 /* Send a FIN+/- to its peer */
587 skb = __skb_dequeue(&sk->sk_receive_queue);
589 __skb_queue_purge(&sk->sk_receive_queue);
590 tipc_sk_respond(sk, skb, error);
593 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
594 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
595 tsk_own_node(tsk), tsk_peer_port(tsk),
598 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
601 /* Reject all SYN messages */
602 tsk_rej_rx_queue(sk, error);
605 __skb_queue_purge(&sk->sk_receive_queue);
611 * tipc_release - destroy a TIPC socket
612 * @sock: socket to destroy
614 * This routine cleans up any messages that are still queued on the socket.
615 * For DGRAM and RDM socket types, all queued messages are rejected.
616 * For SEQPACKET and STREAM socket types, the first message is rejected
617 * and any others are discarded. (If the first message on a STREAM socket
618 * is partially-read, it is discarded and the next one is rejected instead.)
620 * NOTE: Rejected messages are not necessarily returned to the sender! They
621 * are returned or discarded according to the "destination droppable" setting
622 * specified for the message by the sender.
624 * Return: 0 on success, errno otherwise
626 static int tipc_release(struct socket *sock)
628 struct sock *sk = sock->sk;
629 struct tipc_sock *tsk;
632 * Exit if socket isn't fully initialized (occurs when a failed accept()
633 * releases a pre-allocated child socket that was never used)
641 trace_tipc_sk_release(sk, NULL, TIPC_DUMP_ALL, " ");
642 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
643 sk->sk_shutdown = SHUTDOWN_MASK;
645 tipc_sk_withdraw(tsk, NULL);
646 __skb_queue_purge(&tsk->mc_method.deferredq);
647 sk_stop_timer(sk, &sk->sk_timer);
651 /* Reject any messages that accumulated in backlog queue */
653 tipc_dest_list_purge(&tsk->cong_links);
654 tsk->cong_link_cnt = 0;
655 call_rcu(&tsk->rcu, tipc_sk_callback);
662 * __tipc_bind - associate or disassocate TIPC name(s) with a socket
663 * @sock: socket structure
664 * @skaddr: socket address describing name(s) and desired operation
665 * @alen: size of socket address data structure
667 * Name and name sequence binding is indicated using a positive scope value;
668 * a negative scope value unbinds the specified name. Specifying no name
669 * (i.e. a socket address length of 0) unbinds all names from the socket.
671 * Return: 0 on success, errno otherwise
673 * NOTE: This routine doesn't need to take the socket lock since it doesn't
674 * access any non-constant socket information.
676 static int __tipc_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
678 struct tipc_uaddr *ua = (struct tipc_uaddr *)skaddr;
679 struct tipc_sock *tsk = tipc_sk(sock->sk);
683 return tipc_sk_withdraw(tsk, NULL);
685 if (ua->addrtype == TIPC_SERVICE_ADDR) {
686 ua->addrtype = TIPC_SERVICE_RANGE;
687 ua->sr.upper = ua->sr.lower;
691 ua->scope = -ua->scope;
693 /* Users may still use deprecated TIPC_ZONE_SCOPE */
694 if (ua->scope != TIPC_NODE_SCOPE)
695 ua->scope = TIPC_CLUSTER_SCOPE;
701 return tipc_sk_withdraw(tsk, ua);
702 return tipc_sk_publish(tsk, ua);
705 int tipc_sk_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
710 res = __tipc_bind(sock, skaddr, alen);
711 release_sock(sock->sk);
715 static int tipc_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
717 struct tipc_uaddr *ua = (struct tipc_uaddr *)skaddr;
718 u32 atype = ua->addrtype;
721 if (!tipc_uaddr_valid(ua, alen))
723 if (atype == TIPC_SOCKET_ADDR)
724 return -EAFNOSUPPORT;
725 if (ua->sr.type < TIPC_RESERVED_TYPES) {
726 pr_warn_once("Can't bind to reserved service type %u\n",
731 return tipc_sk_bind(sock, skaddr, alen);
735 * tipc_getname - get port ID of socket or peer socket
736 * @sock: socket structure
737 * @uaddr: area for returned socket address
738 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
740 * Return: 0 on success, errno otherwise
742 * NOTE: This routine doesn't need to take the socket lock since it only
743 * accesses socket information that is unchanging (or which changes in
744 * a completely predictable manner).
746 static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
749 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
750 struct sock *sk = sock->sk;
751 struct tipc_sock *tsk = tipc_sk(sk);
753 memset(addr, 0, sizeof(*addr));
755 if ((!tipc_sk_connected(sk)) &&
756 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
758 addr->addr.id.ref = tsk_peer_port(tsk);
759 addr->addr.id.node = tsk_peer_node(tsk);
761 addr->addr.id.ref = tsk->portid;
762 addr->addr.id.node = tipc_own_addr(sock_net(sk));
765 addr->addrtype = TIPC_SOCKET_ADDR;
766 addr->family = AF_TIPC;
768 addr->addr.name.domain = 0;
770 return sizeof(*addr);
774 * tipc_poll - read and possibly block on pollmask
775 * @file: file structure associated with the socket
776 * @sock: socket for which to calculate the poll bits
779 * Return: pollmask value
782 * It appears that the usual socket locking mechanisms are not useful here
783 * since the pollmask info is potentially out-of-date the moment this routine
784 * exits. TCP and other protocols seem to rely on higher level poll routines
785 * to handle any preventable race conditions, so TIPC will do the same ...
787 * IMPORTANT: The fact that a read or write operation is indicated does NOT
788 * imply that the operation will succeed, merely that it should be performed
789 * and will not block.
791 static __poll_t tipc_poll(struct file *file, struct socket *sock,
794 struct sock *sk = sock->sk;
795 struct tipc_sock *tsk = tipc_sk(sk);
796 __poll_t revents = 0;
798 sock_poll_wait(file, sock, wait);
799 trace_tipc_sk_poll(sk, NULL, TIPC_DUMP_ALL, " ");
801 if (sk->sk_shutdown & RCV_SHUTDOWN)
802 revents |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
803 if (sk->sk_shutdown == SHUTDOWN_MASK)
806 switch (sk->sk_state) {
807 case TIPC_ESTABLISHED:
808 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
812 case TIPC_CONNECTING:
813 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
814 revents |= EPOLLIN | EPOLLRDNORM;
817 if (tsk->group_is_open && !tsk->cong_link_cnt)
819 if (!tipc_sk_type_connectionless(sk))
821 if (skb_queue_empty_lockless(&sk->sk_receive_queue))
823 revents |= EPOLLIN | EPOLLRDNORM;
825 case TIPC_DISCONNECTING:
826 revents = EPOLLIN | EPOLLRDNORM | EPOLLHUP;
833 * tipc_sendmcast - send multicast message
834 * @sock: socket structure
835 * @ua: destination address struct
836 * @msg: message to send
837 * @dlen: length of data to send
838 * @timeout: timeout to wait for wakeup
840 * Called from function tipc_sendmsg(), which has done all sanity checks
841 * Return: the number of bytes sent on success, or errno
843 static int tipc_sendmcast(struct socket *sock, struct tipc_uaddr *ua,
844 struct msghdr *msg, size_t dlen, long timeout)
846 struct sock *sk = sock->sk;
847 struct tipc_sock *tsk = tipc_sk(sk);
848 struct tipc_msg *hdr = &tsk->phdr;
849 struct net *net = sock_net(sk);
850 int mtu = tipc_bcast_get_mtu(net);
851 struct sk_buff_head pkts;
852 struct tipc_nlist dsts;
858 /* Block or return if any destination link is congested */
859 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
863 /* Lookup destination nodes */
864 tipc_nlist_init(&dsts, tipc_own_addr(net));
865 tipc_nametbl_lookup_mcast_nodes(net, ua, &dsts);
866 if (!dsts.local && !dsts.remote)
867 return -EHOSTUNREACH;
869 /* Build message header */
870 msg_set_type(hdr, TIPC_MCAST_MSG);
871 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
872 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
873 msg_set_destport(hdr, 0);
874 msg_set_destnode(hdr, 0);
875 msg_set_nametype(hdr, ua->sr.type);
876 msg_set_namelower(hdr, ua->sr.lower);
877 msg_set_nameupper(hdr, ua->sr.upper);
879 /* Build message as chain of buffers */
880 __skb_queue_head_init(&pkts);
881 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
883 /* Send message if build was successful */
884 if (unlikely(rc == dlen)) {
885 trace_tipc_sk_sendmcast(sk, skb_peek(&pkts),
886 TIPC_DUMP_SK_SNDQ, " ");
887 rc = tipc_mcast_xmit(net, &pkts, &tsk->mc_method, &dsts,
888 &tsk->cong_link_cnt);
891 tipc_nlist_purge(&dsts);
893 return rc ? rc : dlen;
897 * tipc_send_group_msg - send a message to a member in the group
898 * @net: network namespace
900 * @m: message to send
902 * @dnode: destination node
903 * @dport: destination port
904 * @dlen: total length of message data
906 static int tipc_send_group_msg(struct net *net, struct tipc_sock *tsk,
907 struct msghdr *m, struct tipc_member *mb,
908 u32 dnode, u32 dport, int dlen)
910 u16 bc_snd_nxt = tipc_group_bc_snd_nxt(tsk->group);
911 struct tipc_mc_method *method = &tsk->mc_method;
912 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
913 struct tipc_msg *hdr = &tsk->phdr;
914 struct sk_buff_head pkts;
917 /* Complete message header */
918 msg_set_type(hdr, TIPC_GRP_UCAST_MSG);
919 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
920 msg_set_destport(hdr, dport);
921 msg_set_destnode(hdr, dnode);
922 msg_set_grp_bc_seqno(hdr, bc_snd_nxt);
924 /* Build message as chain of buffers */
925 __skb_queue_head_init(&pkts);
926 mtu = tipc_node_get_mtu(net, dnode, tsk->portid, false);
927 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
928 if (unlikely(rc != dlen))
932 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
933 if (unlikely(rc == -ELINKCONG)) {
934 tipc_dest_push(&tsk->cong_links, dnode, 0);
935 tsk->cong_link_cnt++;
938 /* Update send window */
939 tipc_group_update_member(mb, blks);
941 /* A broadcast sent within next EXPIRE period must follow same path */
942 method->rcast = true;
943 method->mandatory = true;
948 * tipc_send_group_unicast - send message to a member in the group
949 * @sock: socket structure
950 * @m: message to send
951 * @dlen: total length of message data
952 * @timeout: timeout to wait for wakeup
954 * Called from function tipc_sendmsg(), which has done all sanity checks
955 * Return: the number of bytes sent on success, or errno
957 static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m,
958 int dlen, long timeout)
960 struct sock *sk = sock->sk;
961 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
962 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
963 struct tipc_sock *tsk = tipc_sk(sk);
964 struct net *net = sock_net(sk);
965 struct tipc_member *mb = NULL;
972 return -EHOSTUNREACH;
974 /* Block or return if destination link or member is congested */
975 rc = tipc_wait_for_cond(sock, &timeout,
976 !tipc_dest_find(&tsk->cong_links, node, 0) &&
978 !tipc_group_cong(tsk->group, node, port, blks,
984 return -EHOSTUNREACH;
986 rc = tipc_send_group_msg(net, tsk, m, mb, node, port, dlen);
988 return rc ? rc : dlen;
992 * tipc_send_group_anycast - send message to any member with given identity
993 * @sock: socket structure
994 * @m: message to send
995 * @dlen: total length of message data
996 * @timeout: timeout to wait for wakeup
998 * Called from function tipc_sendmsg(), which has done all sanity checks
999 * Return: the number of bytes sent on success, or errno
1001 static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m,
1002 int dlen, long timeout)
1004 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
1005 struct sock *sk = sock->sk;
1006 struct tipc_sock *tsk = tipc_sk(sk);
1007 struct list_head *cong_links = &tsk->cong_links;
1008 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
1009 struct tipc_msg *hdr = &tsk->phdr;
1010 struct tipc_member *first = NULL;
1011 struct tipc_member *mbr = NULL;
1012 struct net *net = sock_net(sk);
1013 u32 node, port, exclude;
1014 struct list_head dsts;
1019 INIT_LIST_HEAD(&dsts);
1020 ua->sa.type = msg_nametype(hdr);
1021 ua->scope = msg_lookup_scope(hdr);
1023 while (++lookups < 4) {
1024 exclude = tipc_group_exclude(tsk->group);
1028 /* Look for a non-congested destination member, if any */
1030 if (!tipc_nametbl_lookup_group(net, ua, &dsts, &dstcnt,
1032 return -EHOSTUNREACH;
1033 tipc_dest_pop(&dsts, &node, &port);
1034 cong = tipc_group_cong(tsk->group, node, port, blks,
1044 /* Start over if destination was not in member list */
1048 if (likely(!cong && !tipc_dest_find(cong_links, node, 0)))
1051 /* Block or return if destination link or member is congested */
1052 rc = tipc_wait_for_cond(sock, &timeout,
1053 !tipc_dest_find(cong_links, node, 0) &&
1055 !tipc_group_cong(tsk->group, node, port,
1060 /* Send, unless destination disappeared while waiting */
1065 if (unlikely(lookups >= 4))
1066 return -EHOSTUNREACH;
1068 rc = tipc_send_group_msg(net, tsk, m, mbr, node, port, dlen);
1070 return rc ? rc : dlen;
1074 * tipc_send_group_bcast - send message to all members in communication group
1075 * @sock: socket structure
1076 * @m: message to send
1077 * @dlen: total length of message data
1078 * @timeout: timeout to wait for wakeup
1080 * Called from function tipc_sendmsg(), which has done all sanity checks
1081 * Return: the number of bytes sent on success, or errno
1083 static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m,
1084 int dlen, long timeout)
1086 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
1087 struct sock *sk = sock->sk;
1088 struct net *net = sock_net(sk);
1089 struct tipc_sock *tsk = tipc_sk(sk);
1090 struct tipc_nlist *dsts;
1091 struct tipc_mc_method *method = &tsk->mc_method;
1092 bool ack = method->mandatory && method->rcast;
1093 int blks = tsk_blocks(MCAST_H_SIZE + dlen);
1094 struct tipc_msg *hdr = &tsk->phdr;
1095 int mtu = tipc_bcast_get_mtu(net);
1096 struct sk_buff_head pkts;
1097 int rc = -EHOSTUNREACH;
1099 /* Block or return if any destination link or member is congested */
1100 rc = tipc_wait_for_cond(sock, &timeout,
1101 !tsk->cong_link_cnt && tsk->group &&
1102 !tipc_group_bc_cong(tsk->group, blks));
1106 dsts = tipc_group_dests(tsk->group);
1107 if (!dsts->local && !dsts->remote)
1108 return -EHOSTUNREACH;
1110 /* Complete message header */
1112 msg_set_type(hdr, TIPC_GRP_MCAST_MSG);
1113 msg_set_nameinst(hdr, ua->sa.instance);
1115 msg_set_type(hdr, TIPC_GRP_BCAST_MSG);
1116 msg_set_nameinst(hdr, 0);
1118 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
1119 msg_set_destport(hdr, 0);
1120 msg_set_destnode(hdr, 0);
1121 msg_set_grp_bc_seqno(hdr, tipc_group_bc_snd_nxt(tsk->group));
1123 /* Avoid getting stuck with repeated forced replicasts */
1124 msg_set_grp_bc_ack_req(hdr, ack);
1126 /* Build message as chain of buffers */
1127 __skb_queue_head_init(&pkts);
1128 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1129 if (unlikely(rc != dlen))
1133 rc = tipc_mcast_xmit(net, &pkts, method, dsts, &tsk->cong_link_cnt);
1137 /* Update broadcast sequence number and send windows */
1138 tipc_group_update_bc_members(tsk->group, blks, ack);
1140 /* Broadcast link is now free to choose method for next broadcast */
1141 method->mandatory = false;
1142 method->expires = jiffies;
1148 * tipc_send_group_mcast - send message to all members with given identity
1149 * @sock: socket structure
1150 * @m: message to send
1151 * @dlen: total length of message data
1152 * @timeout: timeout to wait for wakeup
1154 * Called from function tipc_sendmsg(), which has done all sanity checks
1155 * Return: the number of bytes sent on success, or errno
1157 static int tipc_send_group_mcast(struct socket *sock, struct msghdr *m,
1158 int dlen, long timeout)
1160 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
1161 struct sock *sk = sock->sk;
1162 struct tipc_sock *tsk = tipc_sk(sk);
1163 struct tipc_group *grp = tsk->group;
1164 struct tipc_msg *hdr = &tsk->phdr;
1165 struct net *net = sock_net(sk);
1166 struct list_head dsts;
1167 u32 dstcnt, exclude;
1169 INIT_LIST_HEAD(&dsts);
1170 ua->sa.type = msg_nametype(hdr);
1171 ua->scope = msg_lookup_scope(hdr);
1172 exclude = tipc_group_exclude(grp);
1174 if (!tipc_nametbl_lookup_group(net, ua, &dsts, &dstcnt, exclude, true))
1175 return -EHOSTUNREACH;
1178 tipc_dest_pop(&dsts, &ua->sk.node, &ua->sk.ref);
1179 return tipc_send_group_unicast(sock, m, dlen, timeout);
1182 tipc_dest_list_purge(&dsts);
1183 return tipc_send_group_bcast(sock, m, dlen, timeout);
1187 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
1188 * @net: the associated network namespace
1189 * @arrvq: queue with arriving messages, to be cloned after destination lookup
1190 * @inputq: queue with cloned messages, delivered to socket after dest lookup
1192 * Multi-threaded: parallel calls with reference to same queues may occur
1194 void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
1195 struct sk_buff_head *inputq)
1197 u32 self = tipc_own_addr(net);
1198 struct sk_buff *skb, *_skb;
1200 struct sk_buff_head tmpq;
1201 struct list_head dports;
1202 struct tipc_msg *hdr;
1203 struct tipc_uaddr ua;
1204 int user, mtyp, hlen;
1207 __skb_queue_head_init(&tmpq);
1208 INIT_LIST_HEAD(&dports);
1209 ua.addrtype = TIPC_SERVICE_RANGE;
1211 skb = tipc_skb_peek(arrvq, &inputq->lock);
1212 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
1214 user = msg_user(hdr);
1215 mtyp = msg_type(hdr);
1216 hlen = skb_headroom(skb) + msg_hdr_sz(hdr);
1217 onode = msg_orignode(hdr);
1218 ua.sr.type = msg_nametype(hdr);
1220 if (mtyp == TIPC_GRP_UCAST_MSG || user == GROUP_PROTOCOL) {
1221 spin_lock_bh(&inputq->lock);
1222 if (skb_peek(arrvq) == skb) {
1223 __skb_dequeue(arrvq);
1224 __skb_queue_tail(inputq, skb);
1227 spin_unlock_bh(&inputq->lock);
1231 /* Group messages require exact scope match */
1232 if (msg_in_group(hdr)) {
1235 ua.scope = msg_lookup_scope(hdr);
1238 /* TIPC_NODE_SCOPE means "any scope" in this context */
1240 ua.scope = TIPC_NODE_SCOPE;
1242 ua.scope = TIPC_CLUSTER_SCOPE;
1244 ua.sr.lower = msg_namelower(hdr);
1245 ua.sr.upper = msg_nameupper(hdr);
1248 /* Create destination port list: */
1249 tipc_nametbl_lookup_mcast_sockets(net, &ua, exact, &dports);
1251 /* Clone message per destination */
1252 while (tipc_dest_pop(&dports, NULL, &portid)) {
1253 _skb = __pskb_copy(skb, hlen, GFP_ATOMIC);
1255 msg_set_destport(buf_msg(_skb), portid);
1256 __skb_queue_tail(&tmpq, _skb);
1259 pr_warn("Failed to clone mcast rcv buffer\n");
1261 /* Append to inputq if not already done by other thread */
1262 spin_lock_bh(&inputq->lock);
1263 if (skb_peek(arrvq) == skb) {
1264 skb_queue_splice_tail_init(&tmpq, inputq);
1265 /* Decrease the skb's refcnt as increasing in the
1266 * function tipc_skb_peek
1268 kfree_skb(__skb_dequeue(arrvq));
1270 spin_unlock_bh(&inputq->lock);
1271 __skb_queue_purge(&tmpq);
1274 tipc_sk_rcv(net, inputq);
1277 /* tipc_sk_push_backlog(): send accumulated buffers in socket write queue
1278 * when socket is in Nagle mode
1280 static void tipc_sk_push_backlog(struct tipc_sock *tsk, bool nagle_ack)
1282 struct sk_buff_head *txq = &tsk->sk.sk_write_queue;
1283 struct sk_buff *skb = skb_peek_tail(txq);
1284 struct net *net = sock_net(&tsk->sk);
1285 u32 dnode = tsk_peer_node(tsk);
1289 tsk->pkt_cnt += skb_queue_len(txq);
1290 if (!tsk->pkt_cnt || tsk->msg_acc / tsk->pkt_cnt < 2) {
1292 if (tsk->nagle_start < NAGLE_START_MAX)
1293 tsk->nagle_start *= 2;
1294 tsk->expect_ack = false;
1295 pr_debug("tsk %10u: bad nagle %u -> %u, next start %u!\n",
1296 tsk->portid, tsk->msg_acc, tsk->pkt_cnt,
1299 tsk->nagle_start = NAGLE_START_INIT;
1301 msg_set_ack_required(buf_msg(skb));
1302 tsk->expect_ack = true;
1304 tsk->expect_ack = false;
1311 if (!skb || tsk->cong_link_cnt)
1314 /* Do not send SYN again after congestion */
1315 if (msg_is_syn(buf_msg(skb)))
1319 tsk->pkt_cnt += skb_queue_len(txq);
1320 tsk->snt_unacked += tsk->snd_backlog;
1321 tsk->snd_backlog = 0;
1322 rc = tipc_node_xmit(net, txq, dnode, tsk->portid);
1323 if (rc == -ELINKCONG)
1324 tsk->cong_link_cnt = 1;
1328 * tipc_sk_conn_proto_rcv - receive a connection mng protocol message
1329 * @tsk: receiving socket
1330 * @skb: pointer to message buffer.
1331 * @inputq: buffer list containing the buffers
1332 * @xmitq: output message area
1334 static void tipc_sk_conn_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
1335 struct sk_buff_head *inputq,
1336 struct sk_buff_head *xmitq)
1338 struct tipc_msg *hdr = buf_msg(skb);
1339 u32 onode = tsk_own_node(tsk);
1340 struct sock *sk = &tsk->sk;
1341 int mtyp = msg_type(hdr);
1344 /* Ignore if connection cannot be validated: */
1345 if (!tsk_peer_msg(tsk, hdr)) {
1346 trace_tipc_sk_drop_msg(sk, skb, TIPC_DUMP_NONE, "@proto_rcv!");
1350 if (unlikely(msg_errcode(hdr))) {
1351 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1352 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
1353 tsk_peer_port(tsk));
1354 sk->sk_state_change(sk);
1356 /* State change is ignored if socket already awake,
1357 * - convert msg to abort msg and add to inqueue
1359 msg_set_user(hdr, TIPC_CRITICAL_IMPORTANCE);
1360 msg_set_type(hdr, TIPC_CONN_MSG);
1361 msg_set_size(hdr, BASIC_H_SIZE);
1362 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
1363 __skb_queue_tail(inputq, skb);
1367 tsk->probe_unacked = false;
1369 if (mtyp == CONN_PROBE) {
1370 msg_set_type(hdr, CONN_PROBE_REPLY);
1371 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
1372 __skb_queue_tail(xmitq, skb);
1374 } else if (mtyp == CONN_ACK) {
1375 was_cong = tsk_conn_cong(tsk);
1376 tipc_sk_push_backlog(tsk, msg_nagle_ack(hdr));
1377 tsk->snt_unacked -= msg_conn_ack(hdr);
1378 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1379 tsk->snd_win = msg_adv_win(hdr);
1380 if (was_cong && !tsk_conn_cong(tsk))
1381 sk->sk_write_space(sk);
1382 } else if (mtyp != CONN_PROBE_REPLY) {
1383 pr_warn("Received unknown CONN_PROTO msg\n");
1390 * tipc_sendmsg - send message in connectionless manner
1391 * @sock: socket structure
1392 * @m: message to send
1393 * @dsz: amount of user data to be sent
1395 * Message must have an destination specified explicitly.
1396 * Used for SOCK_RDM and SOCK_DGRAM messages,
1397 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
1398 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
1400 * Return: the number of bytes sent on success, or errno otherwise
1402 static int tipc_sendmsg(struct socket *sock,
1403 struct msghdr *m, size_t dsz)
1405 struct sock *sk = sock->sk;
1409 ret = __tipc_sendmsg(sock, m, dsz);
1415 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
1417 struct sock *sk = sock->sk;
1418 struct net *net = sock_net(sk);
1419 struct tipc_sock *tsk = tipc_sk(sk);
1420 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
1421 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1422 struct list_head *clinks = &tsk->cong_links;
1423 bool syn = !tipc_sk_type_connectionless(sk);
1424 struct tipc_group *grp = tsk->group;
1425 struct tipc_msg *hdr = &tsk->phdr;
1426 struct tipc_socket_addr skaddr;
1427 struct sk_buff_head pkts;
1430 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
1434 if (!tipc_uaddr_valid(ua, m->msg_namelen))
1436 atype = ua->addrtype;
1439 /* If socket belongs to a communication group follow other paths */
1442 return tipc_send_group_bcast(sock, m, dlen, timeout);
1443 if (atype == TIPC_SERVICE_ADDR)
1444 return tipc_send_group_anycast(sock, m, dlen, timeout);
1445 if (atype == TIPC_SOCKET_ADDR)
1446 return tipc_send_group_unicast(sock, m, dlen, timeout);
1447 if (atype == TIPC_SERVICE_RANGE)
1448 return tipc_send_group_mcast(sock, m, dlen, timeout);
1453 ua = (struct tipc_uaddr *)&tsk->peer;
1454 if (!syn && ua->family != AF_TIPC)
1455 return -EDESTADDRREQ;
1456 atype = ua->addrtype;
1459 if (unlikely(syn)) {
1460 if (sk->sk_state == TIPC_LISTEN)
1462 if (sk->sk_state != TIPC_OPEN)
1466 if (atype == TIPC_SERVICE_ADDR) {
1467 tsk->conn_type = ua->sa.type;
1468 tsk->conn_instance = ua->sa.instance;
1470 msg_set_syn(hdr, 1);
1473 /* Determine destination */
1474 if (atype == TIPC_SERVICE_RANGE) {
1475 return tipc_sendmcast(sock, ua, m, dlen, timeout);
1476 } else if (atype == TIPC_SERVICE_ADDR) {
1477 skaddr.node = ua->lookup_node;
1478 ua->scope = tipc_node2scope(skaddr.node);
1479 if (!tipc_nametbl_lookup_anycast(net, ua, &skaddr))
1480 return -EHOSTUNREACH;
1481 } else if (atype == TIPC_SOCKET_ADDR) {
1487 /* Block or return if destination link is congested */
1488 rc = tipc_wait_for_cond(sock, &timeout,
1489 !tipc_dest_find(clinks, skaddr.node, 0));
1493 /* Finally build message header */
1494 msg_set_destnode(hdr, skaddr.node);
1495 msg_set_destport(hdr, skaddr.ref);
1496 if (atype == TIPC_SERVICE_ADDR) {
1497 msg_set_type(hdr, TIPC_NAMED_MSG);
1498 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
1499 msg_set_nametype(hdr, ua->sa.type);
1500 msg_set_nameinst(hdr, ua->sa.instance);
1501 msg_set_lookup_scope(hdr, ua->scope);
1502 } else { /* TIPC_SOCKET_ADDR */
1503 msg_set_type(hdr, TIPC_DIRECT_MSG);
1504 msg_set_lookup_scope(hdr, 0);
1505 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
1508 /* Add message body */
1509 __skb_queue_head_init(&pkts);
1510 mtu = tipc_node_get_mtu(net, skaddr.node, tsk->portid, true);
1511 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1512 if (unlikely(rc != dlen))
1514 if (unlikely(syn && !tipc_msg_skb_clone(&pkts, &sk->sk_write_queue))) {
1515 __skb_queue_purge(&pkts);
1520 trace_tipc_sk_sendmsg(sk, skb_peek(&pkts), TIPC_DUMP_SK_SNDQ, " ");
1521 rc = tipc_node_xmit(net, &pkts, skaddr.node, tsk->portid);
1522 if (unlikely(rc == -ELINKCONG)) {
1523 tipc_dest_push(clinks, skaddr.node, 0);
1524 tsk->cong_link_cnt++;
1528 if (unlikely(syn && !rc))
1529 tipc_set_sk_state(sk, TIPC_CONNECTING);
1531 return rc ? rc : dlen;
1535 * tipc_sendstream - send stream-oriented data
1536 * @sock: socket structure
1538 * @dsz: total length of data to be transmitted
1540 * Used for SOCK_STREAM data.
1542 * Return: the number of bytes sent on success (or partial success),
1543 * or errno if no data sent
1545 static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
1547 struct sock *sk = sock->sk;
1551 ret = __tipc_sendstream(sock, m, dsz);
1557 static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
1559 struct sock *sk = sock->sk;
1560 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1561 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1562 struct sk_buff_head *txq = &sk->sk_write_queue;
1563 struct tipc_sock *tsk = tipc_sk(sk);
1564 struct tipc_msg *hdr = &tsk->phdr;
1565 struct net *net = sock_net(sk);
1566 struct sk_buff *skb;
1567 u32 dnode = tsk_peer_node(tsk);
1568 int maxnagle = tsk->maxnagle;
1569 int maxpkt = tsk->max_pkt;
1573 if (unlikely(dlen > INT_MAX))
1576 /* Handle implicit connection setup */
1577 if (unlikely(dest)) {
1578 rc = __tipc_sendmsg(sock, m, dlen);
1579 if (dlen && dlen == rc) {
1580 tsk->peer_caps = tipc_node_get_capabilities(net, dnode);
1581 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
1587 rc = tipc_wait_for_cond(sock, &timeout,
1588 (!tsk->cong_link_cnt &&
1589 !tsk_conn_cong(tsk) &&
1590 tipc_sk_connected(sk)));
1593 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
1594 blocks = tsk->snd_backlog;
1595 if (tsk->oneway++ >= tsk->nagle_start && maxnagle &&
1597 rc = tipc_msg_append(hdr, m, send, maxnagle, txq);
1598 if (unlikely(rc < 0))
1602 if (blocks <= 64 && tsk->expect_ack) {
1603 tsk->snd_backlog = blocks;
1606 } else if (blocks > 64) {
1607 tsk->pkt_cnt += skb_queue_len(txq);
1609 skb = skb_peek_tail(txq);
1611 msg_set_ack_required(buf_msg(skb));
1612 tsk->expect_ack = true;
1614 tsk->expect_ack = false;
1620 rc = tipc_msg_build(hdr, m, sent, send, maxpkt, txq);
1621 if (unlikely(rc != send))
1623 blocks += tsk_inc(tsk, send + MIN_H_SIZE);
1625 trace_tipc_sk_sendstream(sk, skb_peek(txq),
1626 TIPC_DUMP_SK_SNDQ, " ");
1627 rc = tipc_node_xmit(net, txq, dnode, tsk->portid);
1628 if (unlikely(rc == -ELINKCONG)) {
1629 tsk->cong_link_cnt = 1;
1633 tsk->snt_unacked += blocks;
1634 tsk->snd_backlog = 0;
1637 } while (sent < dlen && !rc);
1639 return sent ? sent : rc;
1643 * tipc_send_packet - send a connection-oriented message
1644 * @sock: socket structure
1645 * @m: message to send
1646 * @dsz: length of data to be transmitted
1648 * Used for SOCK_SEQPACKET messages.
1650 * Return: the number of bytes sent on success, or errno otherwise
1652 static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
1654 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1657 return tipc_sendstream(sock, m, dsz);
1660 /* tipc_sk_finish_conn - complete the setup of a connection
1662 static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
1665 struct sock *sk = &tsk->sk;
1666 struct net *net = sock_net(sk);
1667 struct tipc_msg *msg = &tsk->phdr;
1669 msg_set_syn(msg, 0);
1670 msg_set_destnode(msg, peer_node);
1671 msg_set_destport(msg, peer_port);
1672 msg_set_type(msg, TIPC_CONN_MSG);
1673 msg_set_lookup_scope(msg, 0);
1674 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1676 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
1677 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
1678 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1679 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid, true);
1680 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
1682 __skb_queue_purge(&sk->sk_write_queue);
1683 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1686 /* Fall back to message based flow control */
1687 tsk->rcv_win = FLOWCTL_MSG_WIN;
1688 tsk->snd_win = FLOWCTL_MSG_WIN;
1692 * tipc_sk_set_orig_addr - capture sender's address for received message
1693 * @m: descriptor for message info
1694 * @skb: received message
1696 * Note: Address is not captured if not requested by receiver.
1698 static void tipc_sk_set_orig_addr(struct msghdr *m, struct sk_buff *skb)
1700 DECLARE_SOCKADDR(struct sockaddr_pair *, srcaddr, m->msg_name);
1701 struct tipc_msg *hdr = buf_msg(skb);
1706 srcaddr->sock.family = AF_TIPC;
1707 srcaddr->sock.addrtype = TIPC_SOCKET_ADDR;
1708 srcaddr->sock.scope = 0;
1709 srcaddr->sock.addr.id.ref = msg_origport(hdr);
1710 srcaddr->sock.addr.id.node = msg_orignode(hdr);
1711 srcaddr->sock.addr.name.domain = 0;
1712 m->msg_namelen = sizeof(struct sockaddr_tipc);
1714 if (!msg_in_group(hdr))
1717 /* Group message users may also want to know sending member's id */
1718 srcaddr->member.family = AF_TIPC;
1719 srcaddr->member.addrtype = TIPC_SERVICE_ADDR;
1720 srcaddr->member.scope = 0;
1721 srcaddr->member.addr.name.name.type = msg_nametype(hdr);
1722 srcaddr->member.addr.name.name.instance = TIPC_SKB_CB(skb)->orig_member;
1723 srcaddr->member.addr.name.domain = 0;
1724 m->msg_namelen = sizeof(*srcaddr);
1728 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
1729 * @m: descriptor for message info
1730 * @skb: received message buffer
1731 * @tsk: TIPC port associated with message
1733 * Note: Ancillary data is not captured if not requested by receiver.
1735 * Return: 0 if successful, otherwise errno
1737 static int tipc_sk_anc_data_recv(struct msghdr *m, struct sk_buff *skb,
1738 struct tipc_sock *tsk)
1740 struct tipc_msg *msg;
1747 if (likely(m->msg_controllen == 0))
1751 /* Optionally capture errored message object(s) */
1752 err = msg ? msg_errcode(msg) : 0;
1753 if (unlikely(err)) {
1755 anc_data[1] = msg_data_sz(msg);
1756 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1760 if (skb_linearize(skb))
1763 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1770 /* Optionally capture message destination object */
1771 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1772 switch (dest_type) {
1773 case TIPC_NAMED_MSG:
1775 anc_data[0] = msg_nametype(msg);
1776 anc_data[1] = msg_namelower(msg);
1777 anc_data[2] = msg_namelower(msg);
1779 case TIPC_MCAST_MSG:
1781 anc_data[0] = msg_nametype(msg);
1782 anc_data[1] = msg_namelower(msg);
1783 anc_data[2] = msg_nameupper(msg);
1786 has_name = (tsk->conn_type != 0);
1787 anc_data[0] = tsk->conn_type;
1788 anc_data[1] = tsk->conn_instance;
1789 anc_data[2] = tsk->conn_instance;
1795 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1803 static struct sk_buff *tipc_sk_build_ack(struct tipc_sock *tsk)
1805 struct sock *sk = &tsk->sk;
1806 struct sk_buff *skb = NULL;
1807 struct tipc_msg *msg;
1808 u32 peer_port = tsk_peer_port(tsk);
1809 u32 dnode = tsk_peer_node(tsk);
1811 if (!tipc_sk_connected(sk))
1813 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1814 dnode, tsk_own_node(tsk), peer_port,
1815 tsk->portid, TIPC_OK);
1819 msg_set_conn_ack(msg, tsk->rcv_unacked);
1820 tsk->rcv_unacked = 0;
1822 /* Adjust to and advertize the correct window limit */
1823 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1824 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1825 msg_set_adv_win(msg, tsk->rcv_win);
1830 static void tipc_sk_send_ack(struct tipc_sock *tsk)
1832 struct sk_buff *skb;
1834 skb = tipc_sk_build_ack(tsk);
1838 tipc_node_xmit_skb(sock_net(&tsk->sk), skb, tsk_peer_node(tsk),
1839 msg_link_selector(buf_msg(skb)));
1842 static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1844 struct sock *sk = sock->sk;
1845 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1846 long timeo = *timeop;
1847 int err = sock_error(sk);
1853 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
1854 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1858 add_wait_queue(sk_sleep(sk), &wait);
1860 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
1861 sched_annotate_sleep();
1863 remove_wait_queue(sk_sleep(sk), &wait);
1866 if (!skb_queue_empty(&sk->sk_receive_queue))
1871 err = sock_intr_errno(timeo);
1872 if (signal_pending(current))
1875 err = sock_error(sk);
1884 * tipc_recvmsg - receive packet-oriented message
1885 * @sock: network socket
1886 * @m: descriptor for message info
1887 * @buflen: length of user buffer area
1888 * @flags: receive flags
1890 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1891 * If the complete message doesn't fit in user area, truncate it.
1893 * Return: size of returned message data, errno otherwise
1895 static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
1896 size_t buflen, int flags)
1898 struct sock *sk = sock->sk;
1899 bool connected = !tipc_sk_type_connectionless(sk);
1900 struct tipc_sock *tsk = tipc_sk(sk);
1901 int rc, err, hlen, dlen, copy;
1902 struct sk_buff_head xmitq;
1903 struct tipc_msg *hdr;
1904 struct sk_buff *skb;
1908 /* Catch invalid receive requests */
1909 if (unlikely(!buflen))
1913 if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
1917 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1919 /* Step rcv queue to first msg with data or error; wait if necessary */
1921 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1924 skb = skb_peek(&sk->sk_receive_queue);
1926 dlen = msg_data_sz(hdr);
1927 hlen = msg_hdr_sz(hdr);
1928 err = msg_errcode(hdr);
1929 grp_evt = msg_is_grp_evt(hdr);
1930 if (likely(dlen || err))
1932 tsk_advance_rx_queue(sk);
1935 /* Collect msg meta data, including error code and rejected data */
1936 tipc_sk_set_orig_addr(m, skb);
1937 rc = tipc_sk_anc_data_recv(m, skb, tsk);
1942 /* Capture data if non-error msg, otherwise just set return value */
1944 copy = min_t(int, dlen, buflen);
1945 if (unlikely(copy != dlen))
1946 m->msg_flags |= MSG_TRUNC;
1947 rc = skb_copy_datagram_msg(skb, hlen, m, copy);
1951 if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control)
1957 /* Mark message as group event if applicable */
1958 if (unlikely(grp_evt)) {
1959 if (msg_grp_evt(hdr) == TIPC_WITHDRAWN)
1960 m->msg_flags |= MSG_EOR;
1961 m->msg_flags |= MSG_OOB;
1965 /* Caption of data or error code/rejected data was successful */
1966 if (unlikely(flags & MSG_PEEK))
1969 /* Send group flow control advertisement when applicable */
1970 if (tsk->group && msg_in_group(hdr) && !grp_evt) {
1971 __skb_queue_head_init(&xmitq);
1972 tipc_group_update_rcv_win(tsk->group, tsk_blocks(hlen + dlen),
1973 msg_orignode(hdr), msg_origport(hdr),
1975 tipc_node_distr_xmit(sock_net(sk), &xmitq);
1978 tsk_advance_rx_queue(sk);
1980 if (likely(!connected))
1983 /* Send connection flow control advertisement when applicable */
1984 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1985 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
1986 tipc_sk_send_ack(tsk);
1989 return rc ? rc : copy;
1993 * tipc_recvstream - receive stream-oriented data
1994 * @sock: network socket
1995 * @m: descriptor for message info
1996 * @buflen: total size of user buffer area
1997 * @flags: receive flags
1999 * Used for SOCK_STREAM messages only. If not enough data is available
2000 * will optionally wait for more; never truncates data.
2002 * Return: size of returned message data, errno otherwise
2004 static int tipc_recvstream(struct socket *sock, struct msghdr *m,
2005 size_t buflen, int flags)
2007 struct sock *sk = sock->sk;
2008 struct tipc_sock *tsk = tipc_sk(sk);
2009 struct sk_buff *skb;
2010 struct tipc_msg *hdr;
2011 struct tipc_skb_cb *skb_cb;
2012 bool peek = flags & MSG_PEEK;
2013 int offset, required, copy, copied = 0;
2014 int hlen, dlen, err, rc;
2017 /* Catch invalid receive attempts */
2018 if (unlikely(!buflen))
2023 if (unlikely(sk->sk_state == TIPC_OPEN)) {
2027 required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
2028 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
2031 /* Look at first msg in receive queue; wait if necessary */
2032 rc = tipc_wait_for_rcvmsg(sock, &timeout);
2035 skb = skb_peek(&sk->sk_receive_queue);
2036 skb_cb = TIPC_SKB_CB(skb);
2038 dlen = msg_data_sz(hdr);
2039 hlen = msg_hdr_sz(hdr);
2040 err = msg_errcode(hdr);
2042 /* Discard any empty non-errored (SYN-) message */
2043 if (unlikely(!dlen && !err)) {
2044 tsk_advance_rx_queue(sk);
2048 /* Collect msg meta data, incl. error code and rejected data */
2050 tipc_sk_set_orig_addr(m, skb);
2051 rc = tipc_sk_anc_data_recv(m, skb, tsk);
2057 /* Copy data if msg ok, otherwise return error/partial data */
2059 offset = skb_cb->bytes_read;
2060 copy = min_t(int, dlen - offset, buflen - copied);
2061 rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
2066 if (unlikely(offset < dlen)) {
2068 skb_cb->bytes_read = offset;
2073 if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control)
2082 tsk_advance_rx_queue(sk);
2084 /* Send connection flow control advertisement when applicable */
2085 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
2086 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
2087 tipc_sk_send_ack(tsk);
2089 /* Exit if all requested data or FIN/error received */
2090 if (copied == buflen || err)
2093 } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required);
2096 return copied ? copied : rc;
2100 * tipc_write_space - wake up thread if port congestion is released
2103 static void tipc_write_space(struct sock *sk)
2105 struct socket_wq *wq;
2108 wq = rcu_dereference(sk->sk_wq);
2109 if (skwq_has_sleeper(wq))
2110 wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
2111 EPOLLWRNORM | EPOLLWRBAND);
2116 * tipc_data_ready - wake up threads to indicate messages have been received
2119 static void tipc_data_ready(struct sock *sk)
2121 struct socket_wq *wq;
2124 wq = rcu_dereference(sk->sk_wq);
2125 if (skwq_has_sleeper(wq))
2126 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
2127 EPOLLRDNORM | EPOLLRDBAND);
2131 static void tipc_sock_destruct(struct sock *sk)
2133 __skb_queue_purge(&sk->sk_receive_queue);
2136 static void tipc_sk_proto_rcv(struct sock *sk,
2137 struct sk_buff_head *inputq,
2138 struct sk_buff_head *xmitq)
2140 struct sk_buff *skb = __skb_dequeue(inputq);
2141 struct tipc_sock *tsk = tipc_sk(sk);
2142 struct tipc_msg *hdr = buf_msg(skb);
2143 struct tipc_group *grp = tsk->group;
2144 bool wakeup = false;
2146 switch (msg_user(hdr)) {
2148 tipc_sk_conn_proto_rcv(tsk, skb, inputq, xmitq);
2151 tipc_dest_del(&tsk->cong_links, msg_orignode(hdr), 0);
2152 /* coupled with smp_rmb() in tipc_wait_for_cond() */
2154 tsk->cong_link_cnt--;
2156 tipc_sk_push_backlog(tsk, false);
2158 case GROUP_PROTOCOL:
2159 tipc_group_proto_rcv(grp, &wakeup, hdr, inputq, xmitq);
2162 tipc_group_member_evt(tsk->group, &wakeup, &sk->sk_rcvbuf,
2163 hdr, inputq, xmitq);
2170 sk->sk_write_space(sk);
2176 * tipc_sk_filter_connect - check incoming message for a connection-based socket
2178 * @skb: pointer to message buffer.
2179 * @xmitq: for Nagle ACK if any
2180 * Return: true if message should be added to receive queue, false otherwise
2182 static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb,
2183 struct sk_buff_head *xmitq)
2185 struct sock *sk = &tsk->sk;
2186 struct net *net = sock_net(sk);
2187 struct tipc_msg *hdr = buf_msg(skb);
2188 bool con_msg = msg_connected(hdr);
2189 u32 pport = tsk_peer_port(tsk);
2190 u32 pnode = tsk_peer_node(tsk);
2191 u32 oport = msg_origport(hdr);
2192 u32 onode = msg_orignode(hdr);
2193 int err = msg_errcode(hdr);
2194 unsigned long delay;
2196 if (unlikely(msg_mcast(hdr)))
2200 switch (sk->sk_state) {
2201 case TIPC_CONNECTING:
2203 if (likely(con_msg)) {
2206 tipc_sk_finish_conn(tsk, oport, onode);
2207 msg_set_importance(&tsk->phdr, msg_importance(hdr));
2208 /* ACK+ message with data is added to receive queue */
2209 if (msg_data_sz(hdr))
2211 /* Empty ACK-, - wake up sleeping connect() and drop */
2212 sk->sk_state_change(sk);
2213 msg_set_dest_droppable(hdr, 1);
2216 /* Ignore connectionless message if not from listening socket */
2217 if (oport != pport || onode != pnode)
2221 if (err != TIPC_ERR_OVERLOAD)
2224 /* Prepare for new setup attempt if we have a SYN clone */
2225 if (skb_queue_empty(&sk->sk_write_queue))
2227 get_random_bytes(&delay, 2);
2228 delay %= (tsk->conn_timeout / 4);
2229 delay = msecs_to_jiffies(delay + 100);
2230 sk_reset_timer(sk, &sk->sk_timer, jiffies + delay);
2233 case TIPC_DISCONNECTING:
2236 /* Accept only SYN message */
2237 if (!msg_is_syn(hdr) &&
2238 tipc_node_get_capabilities(net, onode) & TIPC_SYN_BIT)
2240 if (!con_msg && !err)
2243 case TIPC_ESTABLISHED:
2244 if (!skb_queue_empty(&sk->sk_write_queue))
2245 tipc_sk_push_backlog(tsk, false);
2246 /* Accept only connection-based messages sent by peer */
2247 if (likely(con_msg && !err && pport == oport &&
2249 if (msg_ack_required(hdr)) {
2250 struct sk_buff *skb;
2252 skb = tipc_sk_build_ack(tsk);
2254 msg_set_nagle_ack(buf_msg(skb));
2255 __skb_queue_tail(xmitq, skb);
2260 if (!tsk_peer_msg(tsk, hdr))
2264 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2265 tipc_node_remove_conn(net, pnode, tsk->portid);
2266 sk->sk_state_change(sk);
2269 pr_err("Unknown sk_state %u\n", sk->sk_state);
2271 /* Abort connection setup attempt */
2272 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2273 sk->sk_err = ECONNREFUSED;
2274 sk->sk_state_change(sk);
2279 * rcvbuf_limit - get proper overload limit of socket receive queue
2283 * For connection oriented messages, irrespective of importance,
2284 * default queue limit is 2 MB.
2286 * For connectionless messages, queue limits are based on message
2287 * importance as follows:
2289 * TIPC_LOW_IMPORTANCE (2 MB)
2290 * TIPC_MEDIUM_IMPORTANCE (4 MB)
2291 * TIPC_HIGH_IMPORTANCE (8 MB)
2292 * TIPC_CRITICAL_IMPORTANCE (16 MB)
2294 * Return: overload limit according to corresponding message importance
2296 static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
2298 struct tipc_sock *tsk = tipc_sk(sk);
2299 struct tipc_msg *hdr = buf_msg(skb);
2301 if (unlikely(msg_in_group(hdr)))
2302 return READ_ONCE(sk->sk_rcvbuf);
2304 if (unlikely(!msg_connected(hdr)))
2305 return READ_ONCE(sk->sk_rcvbuf) << msg_importance(hdr);
2307 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
2308 return READ_ONCE(sk->sk_rcvbuf);
2310 return FLOWCTL_MSG_LIM;
2314 * tipc_sk_filter_rcv - validate incoming message
2316 * @skb: pointer to message.
2317 * @xmitq: output message area (FIXME)
2319 * Enqueues message on receive queue if acceptable; optionally handles
2320 * disconnect indication for a connected socket.
2322 * Called with socket lock already taken
2324 static void tipc_sk_filter_rcv(struct sock *sk, struct sk_buff *skb,
2325 struct sk_buff_head *xmitq)
2327 bool sk_conn = !tipc_sk_type_connectionless(sk);
2328 struct tipc_sock *tsk = tipc_sk(sk);
2329 struct tipc_group *grp = tsk->group;
2330 struct tipc_msg *hdr = buf_msg(skb);
2331 struct net *net = sock_net(sk);
2332 struct sk_buff_head inputq;
2333 int mtyp = msg_type(hdr);
2334 int limit, err = TIPC_OK;
2336 trace_tipc_sk_filter_rcv(sk, skb, TIPC_DUMP_ALL, " ");
2337 TIPC_SKB_CB(skb)->bytes_read = 0;
2338 __skb_queue_head_init(&inputq);
2339 __skb_queue_tail(&inputq, skb);
2341 if (unlikely(!msg_isdata(hdr)))
2342 tipc_sk_proto_rcv(sk, &inputq, xmitq);
2345 tipc_group_filter_msg(grp, &inputq, xmitq);
2347 if (unlikely(!grp) && mtyp == TIPC_MCAST_MSG)
2348 tipc_mcast_filter_msg(net, &tsk->mc_method.deferredq, &inputq);
2350 /* Validate and add to receive buffer if there is space */
2351 while ((skb = __skb_dequeue(&inputq))) {
2353 limit = rcvbuf_limit(sk, skb);
2354 if ((sk_conn && !tipc_sk_filter_connect(tsk, skb, xmitq)) ||
2355 (!sk_conn && msg_connected(hdr)) ||
2356 (!grp && msg_in_group(hdr)))
2357 err = TIPC_ERR_NO_PORT;
2358 else if (sk_rmem_alloc_get(sk) + skb->truesize >= limit) {
2359 trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL,
2361 atomic_inc(&sk->sk_drops);
2362 err = TIPC_ERR_OVERLOAD;
2365 if (unlikely(err)) {
2366 if (tipc_msg_reverse(tipc_own_addr(net), &skb, err)) {
2367 trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE,
2369 __skb_queue_tail(xmitq, skb);
2374 __skb_queue_tail(&sk->sk_receive_queue, skb);
2375 skb_set_owner_r(skb, sk);
2376 trace_tipc_sk_overlimit2(sk, skb, TIPC_DUMP_ALL,
2377 "rcvq >90% allocated!");
2378 sk->sk_data_ready(sk);
2383 * tipc_sk_backlog_rcv - handle incoming message from backlog queue
2387 * Caller must hold socket lock
2389 static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
2391 unsigned int before = sk_rmem_alloc_get(sk);
2392 struct sk_buff_head xmitq;
2395 __skb_queue_head_init(&xmitq);
2397 tipc_sk_filter_rcv(sk, skb, &xmitq);
2398 added = sk_rmem_alloc_get(sk) - before;
2399 atomic_add(added, &tipc_sk(sk)->dupl_rcvcnt);
2401 /* Send pending response/rejected messages, if any */
2402 tipc_node_distr_xmit(sock_net(sk), &xmitq);
2407 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
2408 * inputq and try adding them to socket or backlog queue
2409 * @inputq: list of incoming buffers with potentially different destinations
2410 * @sk: socket where the buffers should be enqueued
2411 * @dport: port number for the socket
2412 * @xmitq: output queue
2414 * Caller must hold socket lock
2416 static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
2417 u32 dport, struct sk_buff_head *xmitq)
2419 unsigned long time_limit = jiffies + 2;
2420 struct sk_buff *skb;
2425 while (skb_queue_len(inputq)) {
2426 if (unlikely(time_after_eq(jiffies, time_limit)))
2429 skb = tipc_skb_dequeue(inputq, dport);
2433 /* Add message directly to receive queue if possible */
2434 if (!sock_owned_by_user(sk)) {
2435 tipc_sk_filter_rcv(sk, skb, xmitq);
2439 /* Try backlog, compensating for double-counted bytes */
2440 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
2441 if (!sk->sk_backlog.len)
2442 atomic_set(dcnt, 0);
2443 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
2444 if (likely(!sk_add_backlog(sk, skb, lim))) {
2445 trace_tipc_sk_overlimit1(sk, skb, TIPC_DUMP_ALL,
2446 "bklg & rcvq >90% allocated!");
2450 trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL, "err_overload!");
2451 /* Overload => reject message back to sender */
2452 onode = tipc_own_addr(sock_net(sk));
2453 atomic_inc(&sk->sk_drops);
2454 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD)) {
2455 trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_ALL,
2457 __skb_queue_tail(xmitq, skb);
2464 * tipc_sk_rcv - handle a chain of incoming buffers
2465 * @net: the associated network namespace
2466 * @inputq: buffer list containing the buffers
2467 * Consumes all buffers in list until inputq is empty
2468 * Note: may be called in multiple threads referring to the same queue
2470 void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
2472 struct sk_buff_head xmitq;
2473 u32 dnode, dport = 0;
2475 struct tipc_sock *tsk;
2477 struct sk_buff *skb;
2479 __skb_queue_head_init(&xmitq);
2480 while (skb_queue_len(inputq)) {
2481 dport = tipc_skb_peek_port(inputq, dport);
2482 tsk = tipc_sk_lookup(net, dport);
2486 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
2487 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
2488 spin_unlock_bh(&sk->sk_lock.slock);
2490 /* Send pending response/rejected messages, if any */
2491 tipc_node_distr_xmit(sock_net(sk), &xmitq);
2495 /* No destination socket => dequeue skb if still there */
2496 skb = tipc_skb_dequeue(inputq, dport);
2500 /* Try secondary lookup if unresolved named message */
2501 err = TIPC_ERR_NO_PORT;
2502 if (tipc_msg_lookup_dest(net, skb, &err))
2505 /* Prepare for message rejection */
2506 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
2509 trace_tipc_sk_rej_msg(NULL, skb, TIPC_DUMP_NONE, "@sk_rcv!");
2511 dnode = msg_destnode(buf_msg(skb));
2512 tipc_node_xmit_skb(net, skb, dnode, dport);
2516 static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
2518 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2519 struct sock *sk = sock->sk;
2523 int err = sock_error(sk);
2528 if (signal_pending(current))
2529 return sock_intr_errno(*timeo_p);
2530 if (sk->sk_state == TIPC_DISCONNECTING)
2533 add_wait_queue(sk_sleep(sk), &wait);
2534 done = sk_wait_event(sk, timeo_p, tipc_sk_connected(sk),
2536 remove_wait_queue(sk_sleep(sk), &wait);
2541 static bool tipc_sockaddr_is_sane(struct sockaddr_tipc *addr)
2543 if (addr->family != AF_TIPC)
2545 if (addr->addrtype == TIPC_SERVICE_RANGE)
2546 return (addr->addr.nameseq.lower <= addr->addr.nameseq.upper);
2547 return (addr->addrtype == TIPC_SERVICE_ADDR ||
2548 addr->addrtype == TIPC_SOCKET_ADDR);
2552 * tipc_connect - establish a connection to another TIPC port
2553 * @sock: socket structure
2554 * @dest: socket address for destination port
2555 * @destlen: size of socket address data structure
2556 * @flags: file-related flags associated with socket
2558 * Return: 0 on success, errno otherwise
2560 static int tipc_connect(struct socket *sock, struct sockaddr *dest,
2561 int destlen, int flags)
2563 struct sock *sk = sock->sk;
2564 struct tipc_sock *tsk = tipc_sk(sk);
2565 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
2566 struct msghdr m = {NULL,};
2567 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
2571 if (destlen != sizeof(struct sockaddr_tipc))
2581 if (dst->family == AF_UNSPEC) {
2582 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
2583 if (!tipc_sk_type_connectionless(sk))
2587 if (!tipc_sockaddr_is_sane(dst)) {
2591 /* DGRAM/RDM connect(), just save the destaddr */
2592 if (tipc_sk_type_connectionless(sk)) {
2593 memcpy(&tsk->peer, dest, destlen);
2595 } else if (dst->addrtype == TIPC_SERVICE_RANGE) {
2600 previous = sk->sk_state;
2602 switch (sk->sk_state) {
2604 /* Send a 'SYN-' to destination */
2606 m.msg_namelen = destlen;
2608 /* If connect is in non-blocking case, set MSG_DONTWAIT to
2609 * indicate send_msg() is never blocked.
2612 m.msg_flags = MSG_DONTWAIT;
2614 res = __tipc_sendmsg(sock, &m, 0);
2615 if ((res < 0) && (res != -EWOULDBLOCK))
2618 /* Just entered TIPC_CONNECTING state; the only
2619 * difference is that return value in non-blocking
2620 * case is EINPROGRESS, rather than EALREADY.
2624 case TIPC_CONNECTING:
2626 if (previous == TIPC_CONNECTING)
2630 timeout = msecs_to_jiffies(timeout);
2631 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
2632 res = tipc_wait_for_connect(sock, &timeout);
2634 case TIPC_ESTABLISHED:
2647 * tipc_listen - allow socket to listen for incoming connections
2648 * @sock: socket structure
2651 * Return: 0 on success, errno otherwise
2653 static int tipc_listen(struct socket *sock, int len)
2655 struct sock *sk = sock->sk;
2659 res = tipc_set_sk_state(sk, TIPC_LISTEN);
2665 static int tipc_wait_for_accept(struct socket *sock, long timeo)
2667 struct sock *sk = sock->sk;
2671 /* True wake-one mechanism for incoming connections: only
2672 * one process gets woken up, not the 'whole herd'.
2673 * Since we do not 'race & poll' for established sockets
2674 * anymore, the common case will execute the loop only once.
2677 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2678 TASK_INTERRUPTIBLE);
2679 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
2681 timeo = schedule_timeout(timeo);
2685 if (!skb_queue_empty(&sk->sk_receive_queue))
2690 err = sock_intr_errno(timeo);
2691 if (signal_pending(current))
2694 finish_wait(sk_sleep(sk), &wait);
2699 * tipc_accept - wait for connection request
2700 * @sock: listening socket
2701 * @new_sock: new socket that is to be connected
2702 * @flags: file-related flags associated with socket
2703 * @kern: caused by kernel or by userspace?
2705 * Return: 0 on success, errno otherwise
2707 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
2710 struct sock *new_sk, *sk = sock->sk;
2711 struct sk_buff *buf;
2712 struct tipc_sock *new_tsock;
2713 struct tipc_msg *msg;
2719 if (sk->sk_state != TIPC_LISTEN) {
2723 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2724 res = tipc_wait_for_accept(sock, timeo);
2728 buf = skb_peek(&sk->sk_receive_queue);
2730 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
2733 security_sk_clone(sock->sk, new_sock->sk);
2735 new_sk = new_sock->sk;
2736 new_tsock = tipc_sk(new_sk);
2739 /* we lock on new_sk; but lockdep sees the lock on sk */
2740 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
2743 * Reject any stray messages received by new socket
2744 * before the socket lock was taken (very, very unlikely)
2746 tsk_rej_rx_queue(new_sk, TIPC_ERR_NO_PORT);
2748 /* Connect new socket to it's peer */
2749 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
2751 tsk_set_importance(new_sk, msg_importance(msg));
2752 if (msg_named(msg)) {
2753 new_tsock->conn_type = msg_nametype(msg);
2754 new_tsock->conn_instance = msg_nameinst(msg);
2758 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2759 * Respond to 'SYN+' by queuing it on new socket.
2761 if (!msg_data_sz(msg)) {
2762 struct msghdr m = {NULL,};
2764 tsk_advance_rx_queue(sk);
2765 __tipc_sendstream(new_sock, &m, 0);
2767 __skb_dequeue(&sk->sk_receive_queue);
2768 __skb_queue_head(&new_sk->sk_receive_queue, buf);
2769 skb_set_owner_r(buf, new_sk);
2771 release_sock(new_sk);
2778 * tipc_shutdown - shutdown socket connection
2779 * @sock: socket structure
2780 * @how: direction to close (must be SHUT_RDWR)
2782 * Terminates connection (if necessary), then purges socket's receive queue.
2784 * Return: 0 on success, errno otherwise
2786 static int tipc_shutdown(struct socket *sock, int how)
2788 struct sock *sk = sock->sk;
2791 if (how != SHUT_RDWR)
2796 trace_tipc_sk_shutdown(sk, NULL, TIPC_DUMP_ALL, " ");
2797 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2798 sk->sk_shutdown = SHUTDOWN_MASK;
2800 if (sk->sk_state == TIPC_DISCONNECTING) {
2801 /* Discard any unreceived messages */
2802 __skb_queue_purge(&sk->sk_receive_queue);
2808 /* Wake up anyone sleeping in poll. */
2809 sk->sk_state_change(sk);
2815 static void tipc_sk_check_probing_state(struct sock *sk,
2816 struct sk_buff_head *list)
2818 struct tipc_sock *tsk = tipc_sk(sk);
2819 u32 pnode = tsk_peer_node(tsk);
2820 u32 pport = tsk_peer_port(tsk);
2821 u32 self = tsk_own_node(tsk);
2822 u32 oport = tsk->portid;
2823 struct sk_buff *skb;
2825 if (tsk->probe_unacked) {
2826 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2827 sk->sk_err = ECONNABORTED;
2828 tipc_node_remove_conn(sock_net(sk), pnode, pport);
2829 sk->sk_state_change(sk);
2832 /* Prepare new probe */
2833 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, 0,
2834 pnode, self, pport, oport, TIPC_OK);
2836 __skb_queue_tail(list, skb);
2837 tsk->probe_unacked = true;
2838 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
2841 static void tipc_sk_retry_connect(struct sock *sk, struct sk_buff_head *list)
2843 struct tipc_sock *tsk = tipc_sk(sk);
2845 /* Try again later if dest link is congested */
2846 if (tsk->cong_link_cnt) {
2847 sk_reset_timer(sk, &sk->sk_timer, msecs_to_jiffies(100));
2850 /* Prepare SYN for retransmit */
2851 tipc_msg_skb_clone(&sk->sk_write_queue, list);
2854 static void tipc_sk_timeout(struct timer_list *t)
2856 struct sock *sk = from_timer(sk, t, sk_timer);
2857 struct tipc_sock *tsk = tipc_sk(sk);
2858 u32 pnode = tsk_peer_node(tsk);
2859 struct sk_buff_head list;
2862 __skb_queue_head_init(&list);
2865 /* Try again later if socket is busy */
2866 if (sock_owned_by_user(sk)) {
2867 sk_reset_timer(sk, &sk->sk_timer, jiffies + HZ / 20);
2873 if (sk->sk_state == TIPC_ESTABLISHED)
2874 tipc_sk_check_probing_state(sk, &list);
2875 else if (sk->sk_state == TIPC_CONNECTING)
2876 tipc_sk_retry_connect(sk, &list);
2880 if (!skb_queue_empty(&list))
2881 rc = tipc_node_xmit(sock_net(sk), &list, pnode, tsk->portid);
2883 /* SYN messages may cause link congestion */
2884 if (rc == -ELINKCONG) {
2885 tipc_dest_push(&tsk->cong_links, pnode, 0);
2886 tsk->cong_link_cnt = 1;
2891 static int tipc_sk_publish(struct tipc_sock *tsk, struct tipc_uaddr *ua)
2893 struct sock *sk = &tsk->sk;
2894 struct net *net = sock_net(sk);
2895 struct tipc_socket_addr skaddr;
2896 struct publication *p;
2899 if (tipc_sk_connected(sk))
2901 key = tsk->portid + tsk->pub_count + 1;
2902 if (key == tsk->portid)
2904 skaddr.ref = tsk->portid;
2905 skaddr.node = tipc_own_addr(net);
2906 p = tipc_nametbl_publish(net, ua, &skaddr, key);
2910 list_add(&p->binding_sock, &tsk->publications);
2912 tsk->published = true;
2916 static int tipc_sk_withdraw(struct tipc_sock *tsk, struct tipc_uaddr *ua)
2918 struct net *net = sock_net(&tsk->sk);
2919 struct publication *safe, *p;
2920 struct tipc_uaddr _ua;
2923 list_for_each_entry_safe(p, safe, &tsk->publications, binding_sock) {
2925 tipc_uaddr(&_ua, TIPC_SERVICE_RANGE, p->scope,
2926 p->sr.type, p->sr.lower, p->sr.upper);
2927 tipc_nametbl_withdraw(net, &_ua, &p->sk, p->key);
2930 /* Unbind specific publication */
2931 if (p->scope != ua->scope)
2933 if (p->sr.type != ua->sr.type)
2935 if (p->sr.lower != ua->sr.lower)
2937 if (p->sr.upper != ua->sr.upper)
2939 tipc_nametbl_withdraw(net, ua, &p->sk, p->key);
2943 if (list_empty(&tsk->publications)) {
2950 /* tipc_sk_reinit: set non-zero address in all existing sockets
2951 * when we go from standalone to network mode.
2953 void tipc_sk_reinit(struct net *net)
2955 struct tipc_net *tn = net_generic(net, tipc_net_id);
2956 struct rhashtable_iter iter;
2957 struct tipc_sock *tsk;
2958 struct tipc_msg *msg;
2960 rhashtable_walk_enter(&tn->sk_rht, &iter);
2963 rhashtable_walk_start(&iter);
2965 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
2966 sock_hold(&tsk->sk);
2967 rhashtable_walk_stop(&iter);
2968 lock_sock(&tsk->sk);
2970 msg_set_prevnode(msg, tipc_own_addr(net));
2971 msg_set_orignode(msg, tipc_own_addr(net));
2972 release_sock(&tsk->sk);
2973 rhashtable_walk_start(&iter);
2977 rhashtable_walk_stop(&iter);
2978 } while (tsk == ERR_PTR(-EAGAIN));
2980 rhashtable_walk_exit(&iter);
2983 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
2985 struct tipc_net *tn = net_generic(net, tipc_net_id);
2986 struct tipc_sock *tsk;
2989 tsk = rhashtable_lookup(&tn->sk_rht, &portid, tsk_rht_params);
2991 sock_hold(&tsk->sk);
2997 static int tipc_sk_insert(struct tipc_sock *tsk)
2999 struct sock *sk = &tsk->sk;
3000 struct net *net = sock_net(sk);
3001 struct tipc_net *tn = net_generic(net, tipc_net_id);
3002 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
3003 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
3005 while (remaining--) {
3007 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
3008 portid = TIPC_MIN_PORT;
3009 tsk->portid = portid;
3010 sock_hold(&tsk->sk);
3011 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
3020 static void tipc_sk_remove(struct tipc_sock *tsk)
3022 struct sock *sk = &tsk->sk;
3023 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
3025 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
3026 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
3031 static const struct rhashtable_params tsk_rht_params = {
3033 .head_offset = offsetof(struct tipc_sock, node),
3034 .key_offset = offsetof(struct tipc_sock, portid),
3035 .key_len = sizeof(u32), /* portid */
3036 .max_size = 1048576,
3038 .automatic_shrinking = true,
3041 int tipc_sk_rht_init(struct net *net)
3043 struct tipc_net *tn = net_generic(net, tipc_net_id);
3045 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
3048 void tipc_sk_rht_destroy(struct net *net)
3050 struct tipc_net *tn = net_generic(net, tipc_net_id);
3052 /* Wait for socket readers to complete */
3055 rhashtable_destroy(&tn->sk_rht);
3058 static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq)
3060 struct net *net = sock_net(&tsk->sk);
3061 struct tipc_group *grp = tsk->group;
3062 struct tipc_msg *hdr = &tsk->phdr;
3063 struct tipc_uaddr ua;
3066 if (mreq->type < TIPC_RESERVED_TYPES)
3068 if (mreq->scope > TIPC_NODE_SCOPE)
3070 if (mreq->scope != TIPC_NODE_SCOPE)
3071 mreq->scope = TIPC_CLUSTER_SCOPE;
3074 grp = tipc_group_create(net, tsk->portid, mreq, &tsk->group_is_open);
3078 msg_set_lookup_scope(hdr, mreq->scope);
3079 msg_set_nametype(hdr, mreq->type);
3080 msg_set_dest_droppable(hdr, true);
3081 tipc_uaddr(&ua, TIPC_SERVICE_RANGE, mreq->scope,
3082 mreq->type, mreq->instance, mreq->instance);
3083 tipc_nametbl_build_group(net, grp, &ua);
3084 rc = tipc_sk_publish(tsk, &ua);
3086 tipc_group_delete(net, grp);
3090 /* Eliminate any risk that a broadcast overtakes sent JOINs */
3091 tsk->mc_method.rcast = true;
3092 tsk->mc_method.mandatory = true;
3093 tipc_group_join(net, grp, &tsk->sk.sk_rcvbuf);
3097 static int tipc_sk_leave(struct tipc_sock *tsk)
3099 struct net *net = sock_net(&tsk->sk);
3100 struct tipc_group *grp = tsk->group;
3101 struct tipc_uaddr ua;
3106 ua.addrtype = TIPC_SERVICE_RANGE;
3107 tipc_group_self(grp, &ua.sr, &scope);
3109 tipc_group_delete(net, grp);
3111 tipc_sk_withdraw(tsk, &ua);
3116 * tipc_setsockopt - set socket option
3117 * @sock: socket structure
3118 * @lvl: option level
3119 * @opt: option identifier
3120 * @ov: pointer to new option value
3121 * @ol: length of option value
3123 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
3124 * (to ease compatibility).
3126 * Return: 0 on success, errno otherwise
3128 static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
3129 sockptr_t ov, unsigned int ol)
3131 struct sock *sk = sock->sk;
3132 struct tipc_sock *tsk = tipc_sk(sk);
3133 struct tipc_group_req mreq;
3137 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
3139 if (lvl != SOL_TIPC)
3140 return -ENOPROTOOPT;
3143 case TIPC_IMPORTANCE:
3144 case TIPC_SRC_DROPPABLE:
3145 case TIPC_DEST_DROPPABLE:
3146 case TIPC_CONN_TIMEOUT:
3148 if (ol < sizeof(value))
3150 if (copy_from_sockptr(&value, ov, sizeof(u32)))
3153 case TIPC_GROUP_JOIN:
3154 if (ol < sizeof(mreq))
3156 if (copy_from_sockptr(&mreq, ov, sizeof(mreq)))
3160 if (!sockptr_is_null(ov) || ol)
3167 case TIPC_IMPORTANCE:
3168 res = tsk_set_importance(sk, value);
3170 case TIPC_SRC_DROPPABLE:
3171 if (sock->type != SOCK_STREAM)
3172 tsk_set_unreliable(tsk, value);
3176 case TIPC_DEST_DROPPABLE:
3177 tsk_set_unreturnable(tsk, value);
3179 case TIPC_CONN_TIMEOUT:
3180 tipc_sk(sk)->conn_timeout = value;
3182 case TIPC_MCAST_BROADCAST:
3183 tsk->mc_method.rcast = false;
3184 tsk->mc_method.mandatory = true;
3186 case TIPC_MCAST_REPLICAST:
3187 tsk->mc_method.rcast = true;
3188 tsk->mc_method.mandatory = true;
3190 case TIPC_GROUP_JOIN:
3191 res = tipc_sk_join(tsk, &mreq);
3193 case TIPC_GROUP_LEAVE:
3194 res = tipc_sk_leave(tsk);
3197 tsk->nodelay = !!value;
3210 * tipc_getsockopt - get socket option
3211 * @sock: socket structure
3212 * @lvl: option level
3213 * @opt: option identifier
3214 * @ov: receptacle for option value
3215 * @ol: receptacle for length of option value
3217 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
3218 * (to ease compatibility).
3220 * Return: 0 on success, errno otherwise
3222 static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
3223 char __user *ov, int __user *ol)
3225 struct sock *sk = sock->sk;
3226 struct tipc_sock *tsk = tipc_sk(sk);
3227 struct tipc_service_range seq;
3232 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
3233 return put_user(0, ol);
3234 if (lvl != SOL_TIPC)
3235 return -ENOPROTOOPT;
3236 res = get_user(len, ol);
3243 case TIPC_IMPORTANCE:
3244 value = tsk_importance(tsk);
3246 case TIPC_SRC_DROPPABLE:
3247 value = tsk_unreliable(tsk);
3249 case TIPC_DEST_DROPPABLE:
3250 value = tsk_unreturnable(tsk);
3252 case TIPC_CONN_TIMEOUT:
3253 value = tsk->conn_timeout;
3254 /* no need to set "res", since already 0 at this point */
3256 case TIPC_NODE_RECVQ_DEPTH:
3257 value = 0; /* was tipc_queue_size, now obsolete */
3259 case TIPC_SOCK_RECVQ_DEPTH:
3260 value = skb_queue_len(&sk->sk_receive_queue);
3262 case TIPC_SOCK_RECVQ_USED:
3263 value = sk_rmem_alloc_get(sk);
3265 case TIPC_GROUP_JOIN:
3268 tipc_group_self(tsk->group, &seq, &scope);
3278 return res; /* "get" failed */
3280 if (len < sizeof(value))
3283 if (copy_to_user(ov, &value, sizeof(value)))
3286 return put_user(sizeof(value), ol);
3289 static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
3291 struct net *net = sock_net(sock->sk);
3292 struct tipc_sioc_nodeid_req nr = {0};
3293 struct tipc_sioc_ln_req lnr;
3294 void __user *argp = (void __user *)arg;
3297 case SIOCGETLINKNAME:
3298 if (copy_from_user(&lnr, argp, sizeof(lnr)))
3300 if (!tipc_node_get_linkname(net,
3301 lnr.bearer_id & 0xffff, lnr.peer,
3302 lnr.linkname, TIPC_MAX_LINK_NAME)) {
3303 if (copy_to_user(argp, &lnr, sizeof(lnr)))
3307 return -EADDRNOTAVAIL;
3309 if (copy_from_user(&nr, argp, sizeof(nr)))
3311 if (!tipc_node_get_id(net, nr.peer, nr.node_id))
3312 return -EADDRNOTAVAIL;
3313 if (copy_to_user(argp, &nr, sizeof(nr)))
3317 return -ENOIOCTLCMD;
3321 static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
3323 struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
3324 struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
3325 u32 onode = tipc_own_addr(sock_net(sock1->sk));
3327 tsk1->peer.family = AF_TIPC;
3328 tsk1->peer.addrtype = TIPC_SOCKET_ADDR;
3329 tsk1->peer.scope = TIPC_NODE_SCOPE;
3330 tsk1->peer.addr.id.ref = tsk2->portid;
3331 tsk1->peer.addr.id.node = onode;
3332 tsk2->peer.family = AF_TIPC;
3333 tsk2->peer.addrtype = TIPC_SOCKET_ADDR;
3334 tsk2->peer.scope = TIPC_NODE_SCOPE;
3335 tsk2->peer.addr.id.ref = tsk1->portid;
3336 tsk2->peer.addr.id.node = onode;
3338 tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
3339 tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
3343 /* Protocol switches for the various types of TIPC sockets */
3345 static const struct proto_ops msg_ops = {
3346 .owner = THIS_MODULE,
3348 .release = tipc_release,
3350 .connect = tipc_connect,
3351 .socketpair = tipc_socketpair,
3352 .accept = sock_no_accept,
3353 .getname = tipc_getname,
3355 .ioctl = tipc_ioctl,
3356 .listen = sock_no_listen,
3357 .shutdown = tipc_shutdown,
3358 .setsockopt = tipc_setsockopt,
3359 .getsockopt = tipc_getsockopt,
3360 .sendmsg = tipc_sendmsg,
3361 .recvmsg = tipc_recvmsg,
3362 .mmap = sock_no_mmap,
3363 .sendpage = sock_no_sendpage
3366 static const struct proto_ops packet_ops = {
3367 .owner = THIS_MODULE,
3369 .release = tipc_release,
3371 .connect = tipc_connect,
3372 .socketpair = tipc_socketpair,
3373 .accept = tipc_accept,
3374 .getname = tipc_getname,
3376 .ioctl = tipc_ioctl,
3377 .listen = tipc_listen,
3378 .shutdown = tipc_shutdown,
3379 .setsockopt = tipc_setsockopt,
3380 .getsockopt = tipc_getsockopt,
3381 .sendmsg = tipc_send_packet,
3382 .recvmsg = tipc_recvmsg,
3383 .mmap = sock_no_mmap,
3384 .sendpage = sock_no_sendpage
3387 static const struct proto_ops stream_ops = {
3388 .owner = THIS_MODULE,
3390 .release = tipc_release,
3392 .connect = tipc_connect,
3393 .socketpair = tipc_socketpair,
3394 .accept = tipc_accept,
3395 .getname = tipc_getname,
3397 .ioctl = tipc_ioctl,
3398 .listen = tipc_listen,
3399 .shutdown = tipc_shutdown,
3400 .setsockopt = tipc_setsockopt,
3401 .getsockopt = tipc_getsockopt,
3402 .sendmsg = tipc_sendstream,
3403 .recvmsg = tipc_recvstream,
3404 .mmap = sock_no_mmap,
3405 .sendpage = sock_no_sendpage
3408 static const struct net_proto_family tipc_family_ops = {
3409 .owner = THIS_MODULE,
3411 .create = tipc_sk_create
3414 static struct proto tipc_proto = {
3416 .owner = THIS_MODULE,
3417 .obj_size = sizeof(struct tipc_sock),
3418 .sysctl_rmem = sysctl_tipc_rmem
3422 * tipc_socket_init - initialize TIPC socket interface
3424 * Return: 0 on success, errno otherwise
3426 int tipc_socket_init(void)
3430 res = proto_register(&tipc_proto, 1);
3432 pr_err("Failed to register TIPC protocol type\n");
3436 res = sock_register(&tipc_family_ops);
3438 pr_err("Failed to register TIPC socket type\n");
3439 proto_unregister(&tipc_proto);
3447 * tipc_socket_stop - stop TIPC socket interface
3449 void tipc_socket_stop(void)
3451 sock_unregister(tipc_family_ops.family);
3452 proto_unregister(&tipc_proto);
3455 /* Caller should hold socket lock for the passed tipc socket. */
3456 static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
3460 struct nlattr *nest;
3462 peer_node = tsk_peer_node(tsk);
3463 peer_port = tsk_peer_port(tsk);
3465 nest = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_CON);
3469 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
3471 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
3474 if (tsk->conn_type != 0) {
3475 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
3477 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
3479 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
3482 nla_nest_end(skb, nest);
3487 nla_nest_cancel(skb, nest);
3492 static int __tipc_nl_add_sk_info(struct sk_buff *skb, struct tipc_sock
3495 struct net *net = sock_net(skb->sk);
3496 struct sock *sk = &tsk->sk;
3498 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid) ||
3499 nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr(net)))
3502 if (tipc_sk_connected(sk)) {
3503 if (__tipc_nl_add_sk_con(skb, tsk))
3505 } else if (!list_empty(&tsk->publications)) {
3506 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
3512 /* Caller should hold socket lock for the passed tipc socket. */
3513 static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
3514 struct tipc_sock *tsk)
3516 struct nlattr *attrs;
3519 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3520 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
3524 attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK);
3526 goto genlmsg_cancel;
3528 if (__tipc_nl_add_sk_info(skb, tsk))
3529 goto attr_msg_cancel;
3531 nla_nest_end(skb, attrs);
3532 genlmsg_end(skb, hdr);
3537 nla_nest_cancel(skb, attrs);
3539 genlmsg_cancel(skb, hdr);
3544 int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,
3545 int (*skb_handler)(struct sk_buff *skb,
3546 struct netlink_callback *cb,
3547 struct tipc_sock *tsk))
3549 struct rhashtable_iter *iter = (void *)cb->args[4];
3550 struct tipc_sock *tsk;
3553 rhashtable_walk_start(iter);
3554 while ((tsk = rhashtable_walk_next(iter)) != NULL) {
3557 if (err == -EAGAIN) {
3564 sock_hold(&tsk->sk);
3565 rhashtable_walk_stop(iter);
3566 lock_sock(&tsk->sk);
3567 err = skb_handler(skb, cb, tsk);
3569 release_sock(&tsk->sk);
3573 release_sock(&tsk->sk);
3574 rhashtable_walk_start(iter);
3577 rhashtable_walk_stop(iter);
3581 EXPORT_SYMBOL(tipc_nl_sk_walk);
3583 int tipc_dump_start(struct netlink_callback *cb)
3585 return __tipc_dump_start(cb, sock_net(cb->skb->sk));
3587 EXPORT_SYMBOL(tipc_dump_start);
3589 int __tipc_dump_start(struct netlink_callback *cb, struct net *net)
3591 /* tipc_nl_name_table_dump() uses cb->args[0...3]. */
3592 struct rhashtable_iter *iter = (void *)cb->args[4];
3593 struct tipc_net *tn = tipc_net(net);
3596 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3600 cb->args[4] = (long)iter;
3603 rhashtable_walk_enter(&tn->sk_rht, iter);
3607 int tipc_dump_done(struct netlink_callback *cb)
3609 struct rhashtable_iter *hti = (void *)cb->args[4];
3611 rhashtable_walk_exit(hti);
3615 EXPORT_SYMBOL(tipc_dump_done);
3617 int tipc_sk_fill_sock_diag(struct sk_buff *skb, struct netlink_callback *cb,
3618 struct tipc_sock *tsk, u32 sk_filter_state,
3619 u64 (*tipc_diag_gen_cookie)(struct sock *sk))
3621 struct sock *sk = &tsk->sk;
3622 struct nlattr *attrs;
3623 struct nlattr *stat;
3625 /*filter response w.r.t sk_state*/
3626 if (!(sk_filter_state & (1 << sk->sk_state)))
3629 attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK);
3633 if (__tipc_nl_add_sk_info(skb, tsk))
3634 goto attr_msg_cancel;
3636 if (nla_put_u32(skb, TIPC_NLA_SOCK_TYPE, (u32)sk->sk_type) ||
3637 nla_put_u32(skb, TIPC_NLA_SOCK_TIPC_STATE, (u32)sk->sk_state) ||
3638 nla_put_u32(skb, TIPC_NLA_SOCK_INO, sock_i_ino(sk)) ||
3639 nla_put_u32(skb, TIPC_NLA_SOCK_UID,
3640 from_kuid_munged(sk_user_ns(NETLINK_CB(cb->skb).sk),
3642 nla_put_u64_64bit(skb, TIPC_NLA_SOCK_COOKIE,
3643 tipc_diag_gen_cookie(sk),
3645 goto attr_msg_cancel;
3647 stat = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_STAT);
3649 goto attr_msg_cancel;
3651 if (nla_put_u32(skb, TIPC_NLA_SOCK_STAT_RCVQ,
3652 skb_queue_len(&sk->sk_receive_queue)) ||
3653 nla_put_u32(skb, TIPC_NLA_SOCK_STAT_SENDQ,
3654 skb_queue_len(&sk->sk_write_queue)) ||
3655 nla_put_u32(skb, TIPC_NLA_SOCK_STAT_DROP,
3656 atomic_read(&sk->sk_drops)))
3657 goto stat_msg_cancel;
3659 if (tsk->cong_link_cnt &&
3660 nla_put_flag(skb, TIPC_NLA_SOCK_STAT_LINK_CONG))
3661 goto stat_msg_cancel;
3663 if (tsk_conn_cong(tsk) &&
3664 nla_put_flag(skb, TIPC_NLA_SOCK_STAT_CONN_CONG))
3665 goto stat_msg_cancel;
3667 nla_nest_end(skb, stat);
3670 if (tipc_group_fill_sock_diag(tsk->group, skb))
3671 goto stat_msg_cancel;
3673 nla_nest_end(skb, attrs);
3678 nla_nest_cancel(skb, stat);
3680 nla_nest_cancel(skb, attrs);
3684 EXPORT_SYMBOL(tipc_sk_fill_sock_diag);
3686 int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
3688 return tipc_nl_sk_walk(skb, cb, __tipc_nl_add_sk);
3691 /* Caller should hold socket lock for the passed tipc socket. */
3692 static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
3693 struct netlink_callback *cb,
3694 struct publication *publ)
3697 struct nlattr *attrs;
3699 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3700 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
3704 attrs = nla_nest_start_noflag(skb, TIPC_NLA_PUBL);
3706 goto genlmsg_cancel;
3708 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
3709 goto attr_msg_cancel;
3710 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->sr.type))
3711 goto attr_msg_cancel;
3712 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->sr.lower))
3713 goto attr_msg_cancel;
3714 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->sr.upper))
3715 goto attr_msg_cancel;
3717 nla_nest_end(skb, attrs);
3718 genlmsg_end(skb, hdr);
3723 nla_nest_cancel(skb, attrs);
3725 genlmsg_cancel(skb, hdr);
3730 /* Caller should hold socket lock for the passed tipc socket. */
3731 static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
3732 struct netlink_callback *cb,
3733 struct tipc_sock *tsk, u32 *last_publ)
3736 struct publication *p;
3739 list_for_each_entry(p, &tsk->publications, binding_sock) {
3740 if (p->key == *last_publ)
3743 if (p->key != *last_publ) {
3744 /* We never set seq or call nl_dump_check_consistent()
3745 * this means that setting prev_seq here will cause the
3746 * consistence check to fail in the netlink callback
3747 * handler. Resulting in the last NLMSG_DONE message
3748 * having the NLM_F_DUMP_INTR flag set.
3755 p = list_first_entry(&tsk->publications, struct publication,
3759 list_for_each_entry_from(p, &tsk->publications, binding_sock) {
3760 err = __tipc_nl_add_sk_publ(skb, cb, p);
3762 *last_publ = p->key;
3771 int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
3774 u32 tsk_portid = cb->args[0];
3775 u32 last_publ = cb->args[1];
3776 u32 done = cb->args[2];
3777 struct net *net = sock_net(skb->sk);
3778 struct tipc_sock *tsk;
3781 struct nlattr **attrs = genl_dumpit_info(cb)->attrs;
3782 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
3784 if (!attrs[TIPC_NLA_SOCK])
3787 err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
3788 attrs[TIPC_NLA_SOCK],
3789 tipc_nl_sock_policy, NULL);
3793 if (!sock[TIPC_NLA_SOCK_REF])
3796 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
3802 tsk = tipc_sk_lookup(net, tsk_portid);
3806 lock_sock(&tsk->sk);
3807 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
3810 release_sock(&tsk->sk);
3813 cb->args[0] = tsk_portid;
3814 cb->args[1] = last_publ;
3821 * tipc_sk_filtering - check if a socket should be traced
3822 * @sk: the socket to be examined
3824 * @sysctl_tipc_sk_filter is used as the socket tuple for filtering:
3825 * (portid, sock type, name type, name lower, name upper)
3827 * Return: true if the socket meets the socket tuple data
3828 * (value 0 = 'any') or when there is no tuple set (all = 0),
3831 bool tipc_sk_filtering(struct sock *sk)
3833 struct tipc_sock *tsk;
3834 struct publication *p;
3835 u32 _port, _sktype, _type, _lower, _upper;
3836 u32 type = 0, lower = 0, upper = 0;
3843 _port = sysctl_tipc_sk_filter[0];
3844 _sktype = sysctl_tipc_sk_filter[1];
3845 _type = sysctl_tipc_sk_filter[2];
3846 _lower = sysctl_tipc_sk_filter[3];
3847 _upper = sysctl_tipc_sk_filter[4];
3849 if (!_port && !_sktype && !_type && !_lower && !_upper)
3853 return (_port == tsk->portid);
3855 if (_sktype && _sktype != sk->sk_type)
3858 if (tsk->published) {
3859 p = list_first_entry_or_null(&tsk->publications,
3860 struct publication, binding_sock);
3863 lower = p->sr.lower;
3864 upper = p->sr.upper;
3868 if (!tipc_sk_type_connectionless(sk)) {
3869 type = tsk->conn_type;
3870 lower = tsk->conn_instance;
3871 upper = tsk->conn_instance;
3874 if ((_type && _type != type) || (_lower && _lower != lower) ||
3875 (_upper && _upper != upper))
3881 u32 tipc_sock_get_portid(struct sock *sk)
3883 return (sk) ? (tipc_sk(sk))->portid : 0;
3887 * tipc_sk_overlimit1 - check if socket rx queue is about to be overloaded,
3888 * both the rcv and backlog queues are considered
3889 * @sk: tipc sk to be checked
3890 * @skb: tipc msg to be checked
3892 * Return: true if the socket rx queue allocation is > 90%, otherwise false
3895 bool tipc_sk_overlimit1(struct sock *sk, struct sk_buff *skb)
3897 atomic_t *dcnt = &tipc_sk(sk)->dupl_rcvcnt;
3898 unsigned int lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
3899 unsigned int qsize = sk->sk_backlog.len + sk_rmem_alloc_get(sk);
3901 return (qsize > lim * 90 / 100);
3905 * tipc_sk_overlimit2 - check if socket rx queue is about to be overloaded,
3906 * only the rcv queue is considered
3907 * @sk: tipc sk to be checked
3908 * @skb: tipc msg to be checked
3910 * Return: true if the socket rx queue allocation is > 90%, otherwise false
3913 bool tipc_sk_overlimit2(struct sock *sk, struct sk_buff *skb)
3915 unsigned int lim = rcvbuf_limit(sk, skb);
3916 unsigned int qsize = sk_rmem_alloc_get(sk);
3918 return (qsize > lim * 90 / 100);
3922 * tipc_sk_dump - dump TIPC socket
3923 * @sk: tipc sk to be dumped
3924 * @dqueues: bitmask to decide if any socket queue to be dumped?
3925 * - TIPC_DUMP_NONE: don't dump socket queues
3926 * - TIPC_DUMP_SK_SNDQ: dump socket send queue
3927 * - TIPC_DUMP_SK_RCVQ: dump socket rcv queue
3928 * - TIPC_DUMP_SK_BKLGQ: dump socket backlog queue
3929 * - TIPC_DUMP_ALL: dump all the socket queues above
3930 * @buf: returned buffer of dump data in format
3932 int tipc_sk_dump(struct sock *sk, u16 dqueues, char *buf)
3935 size_t sz = (dqueues) ? SK_LMAX : SK_LMIN;
3936 struct tipc_sock *tsk;
3937 struct publication *p;
3941 i += scnprintf(buf, sz, "sk data: (null)\n");
3946 tsk_connected = !tipc_sk_type_connectionless(sk);
3948 i += scnprintf(buf, sz, "sk data: %u", sk->sk_type);
3949 i += scnprintf(buf + i, sz - i, " %d", sk->sk_state);
3950 i += scnprintf(buf + i, sz - i, " %x", tsk_own_node(tsk));
3951 i += scnprintf(buf + i, sz - i, " %u", tsk->portid);
3952 i += scnprintf(buf + i, sz - i, " | %u", tsk_connected);
3953 if (tsk_connected) {
3954 i += scnprintf(buf + i, sz - i, " %x", tsk_peer_node(tsk));
3955 i += scnprintf(buf + i, sz - i, " %u", tsk_peer_port(tsk));
3956 i += scnprintf(buf + i, sz - i, " %u", tsk->conn_type);
3957 i += scnprintf(buf + i, sz - i, " %u", tsk->conn_instance);
3959 i += scnprintf(buf + i, sz - i, " | %u", tsk->published);
3960 if (tsk->published) {
3961 p = list_first_entry_or_null(&tsk->publications,
3962 struct publication, binding_sock);
3963 i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.type : 0);
3964 i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.lower : 0);
3965 i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.upper : 0);
3967 i += scnprintf(buf + i, sz - i, " | %u", tsk->snd_win);
3968 i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_win);
3969 i += scnprintf(buf + i, sz - i, " %u", tsk->max_pkt);
3970 i += scnprintf(buf + i, sz - i, " %x", tsk->peer_caps);
3971 i += scnprintf(buf + i, sz - i, " %u", tsk->cong_link_cnt);
3972 i += scnprintf(buf + i, sz - i, " %u", tsk->snt_unacked);
3973 i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_unacked);
3974 i += scnprintf(buf + i, sz - i, " %u", atomic_read(&tsk->dupl_rcvcnt));
3975 i += scnprintf(buf + i, sz - i, " %u", sk->sk_shutdown);
3976 i += scnprintf(buf + i, sz - i, " | %d", sk_wmem_alloc_get(sk));
3977 i += scnprintf(buf + i, sz - i, " %d", sk->sk_sndbuf);
3978 i += scnprintf(buf + i, sz - i, " | %d", sk_rmem_alloc_get(sk));
3979 i += scnprintf(buf + i, sz - i, " %d", sk->sk_rcvbuf);
3980 i += scnprintf(buf + i, sz - i, " | %d\n", READ_ONCE(sk->sk_backlog.len));
3982 if (dqueues & TIPC_DUMP_SK_SNDQ) {
3983 i += scnprintf(buf + i, sz - i, "sk_write_queue: ");
3984 i += tipc_list_dump(&sk->sk_write_queue, false, buf + i);
3987 if (dqueues & TIPC_DUMP_SK_RCVQ) {
3988 i += scnprintf(buf + i, sz - i, "sk_receive_queue: ");
3989 i += tipc_list_dump(&sk->sk_receive_queue, false, buf + i);
3992 if (dqueues & TIPC_DUMP_SK_BKLGQ) {
3993 i += scnprintf(buf + i, sz - i, "sk_backlog:\n head ");
3994 i += tipc_skb_dump(sk->sk_backlog.head, false, buf + i);
3995 if (sk->sk_backlog.tail != sk->sk_backlog.head) {
3996 i += scnprintf(buf + i, sz - i, " tail ");
3997 i += tipc_skb_dump(sk->sk_backlog.tail, false,