2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2001-2007, 2012-2017, Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
37 #include <linux/rhashtable.h>
38 #include <linux/sched/signal.h>
41 #include "name_table.h"
44 #include "name_distr.h"
50 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
51 #define CONN_PROBING_INTV msecs_to_jiffies(3600000) /* [ms] => 1 h */
52 #define TIPC_FWD_MSG 1
53 #define TIPC_MAX_PORT 0xffffffff
54 #define TIPC_MIN_PORT 1
55 #define TIPC_ACK_RATE 4 /* ACK at 1/4 of of rcv window size */
58 TIPC_LISTEN = TCP_LISTEN,
59 TIPC_ESTABLISHED = TCP_ESTABLISHED,
60 TIPC_OPEN = TCP_CLOSE,
61 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
62 TIPC_CONNECTING = TCP_SYN_SENT,
65 struct sockaddr_pair {
66 struct sockaddr_tipc sock;
67 struct sockaddr_tipc member;
71 * struct tipc_sock - TIPC socket structure
72 * @sk: socket - interacts with 'port' and with user via the socket API
73 * @conn_type: TIPC type used when connection was established
74 * @conn_instance: TIPC instance used when connection was established
75 * @published: non-zero if port has one or more associated names
76 * @max_pkt: maximum packet size "hint" used when building messages sent by port
77 * @portid: unique port identity in TIPC socket hash table
78 * @phdr: preformatted message header used when sending messages
79 * #cong_links: list of congested links
80 * @publications: list of publications for port
81 * @blocking_link: address of the congested link we are currently sleeping on
82 * @pub_count: total # of publications port has made during its lifetime
84 * @conn_timeout: the time we can wait for an unresponded setup request
85 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
86 * @cong_link_cnt: number of congested links
87 * @snt_unacked: # messages sent by socket, and not yet acked by peer
88 * @rcv_unacked: # messages read by user, but not yet acked back to peer
89 * @peer: 'connected' peer for dgram/rdm
90 * @node: hash table node
91 * @mc_method: cookie for use between socket and broadcast layer
92 * @rcu: rcu struct for tipc_sock
101 struct tipc_msg phdr;
102 struct list_head cong_links;
103 struct list_head publications;
106 atomic_t dupl_rcvcnt;
114 struct sockaddr_tipc peer;
115 struct rhash_head node;
116 struct tipc_mc_method mc_method;
118 struct tipc_group *group;
122 static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
123 static void tipc_data_ready(struct sock *sk);
124 static void tipc_write_space(struct sock *sk);
125 static void tipc_sock_destruct(struct sock *sk);
126 static int tipc_release(struct socket *sock);
127 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
129 static void tipc_sk_timeout(struct timer_list *t);
130 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
131 struct tipc_name_seq const *seq);
132 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
133 struct tipc_name_seq const *seq);
134 static int tipc_sk_leave(struct tipc_sock *tsk);
135 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
136 static int tipc_sk_insert(struct tipc_sock *tsk);
137 static void tipc_sk_remove(struct tipc_sock *tsk);
138 static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
139 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
141 static const struct proto_ops packet_ops;
142 static const struct proto_ops stream_ops;
143 static const struct proto_ops msg_ops;
144 static struct proto tipc_proto;
145 static const struct rhashtable_params tsk_rht_params;
147 static u32 tsk_own_node(struct tipc_sock *tsk)
149 return msg_prevnode(&tsk->phdr);
152 static u32 tsk_peer_node(struct tipc_sock *tsk)
154 return msg_destnode(&tsk->phdr);
157 static u32 tsk_peer_port(struct tipc_sock *tsk)
159 return msg_destport(&tsk->phdr);
162 static bool tsk_unreliable(struct tipc_sock *tsk)
164 return msg_src_droppable(&tsk->phdr) != 0;
167 static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
169 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
172 static bool tsk_unreturnable(struct tipc_sock *tsk)
174 return msg_dest_droppable(&tsk->phdr) != 0;
177 static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
179 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
182 static int tsk_importance(struct tipc_sock *tsk)
184 return msg_importance(&tsk->phdr);
187 static int tsk_set_importance(struct tipc_sock *tsk, int imp)
189 if (imp > TIPC_CRITICAL_IMPORTANCE)
191 msg_set_importance(&tsk->phdr, (u32)imp);
195 static struct tipc_sock *tipc_sk(const struct sock *sk)
197 return container_of(sk, struct tipc_sock, sk);
200 static bool tsk_conn_cong(struct tipc_sock *tsk)
202 return tsk->snt_unacked > tsk->snd_win;
205 static u16 tsk_blocks(int len)
207 return ((len / FLOWCTL_BLK_SZ) + 1);
210 /* tsk_blocks(): translate a buffer size in bytes to number of
211 * advertisable blocks, taking into account the ratio truesize(len)/len
212 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
214 static u16 tsk_adv_blocks(int len)
216 return len / FLOWCTL_BLK_SZ / 4;
219 /* tsk_inc(): increment counter for sent or received data
220 * - If block based flow control is not supported by peer we
221 * fall back to message based ditto, incrementing the counter
223 static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
225 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
226 return ((msglen / FLOWCTL_BLK_SZ) + 1);
231 * tsk_advance_rx_queue - discard first buffer in socket receive queue
233 * Caller must hold socket lock
235 static void tsk_advance_rx_queue(struct sock *sk)
237 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
240 /* tipc_sk_respond() : send response message back to sender
242 static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
246 u32 onode = tipc_own_addr(sock_net(sk));
248 if (!tipc_msg_reverse(onode, &skb, err))
251 dnode = msg_destnode(buf_msg(skb));
252 selector = msg_origport(buf_msg(skb));
253 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
257 * tsk_rej_rx_queue - reject all buffers in socket receive queue
259 * Caller must hold socket lock
261 static void tsk_rej_rx_queue(struct sock *sk)
265 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
266 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
269 static bool tipc_sk_connected(struct sock *sk)
271 return sk->sk_state == TIPC_ESTABLISHED;
274 /* tipc_sk_type_connectionless - check if the socket is datagram socket
277 * Returns true if connection less, false otherwise
279 static bool tipc_sk_type_connectionless(struct sock *sk)
281 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
284 /* tsk_peer_msg - verify if message was sent by connected port's peer
286 * Handles cases where the node's network address has changed from
287 * the default of <0.0.0> to its configured setting.
289 static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
291 struct sock *sk = &tsk->sk;
292 u32 self = tipc_own_addr(sock_net(sk));
293 u32 peer_port = tsk_peer_port(tsk);
294 u32 orig_node, peer_node;
296 if (unlikely(!tipc_sk_connected(sk)))
299 if (unlikely(msg_origport(msg) != peer_port))
302 orig_node = msg_orignode(msg);
303 peer_node = tsk_peer_node(tsk);
305 if (likely(orig_node == peer_node))
308 if (!orig_node && peer_node == self)
311 if (!peer_node && orig_node == self)
317 /* tipc_set_sk_state - set the sk_state of the socket
320 * Caller must hold socket lock
322 * Returns 0 on success, errno otherwise
324 static int tipc_set_sk_state(struct sock *sk, int state)
326 int oldsk_state = sk->sk_state;
334 case TIPC_CONNECTING:
335 if (oldsk_state == TIPC_OPEN)
338 case TIPC_ESTABLISHED:
339 if (oldsk_state == TIPC_CONNECTING ||
340 oldsk_state == TIPC_OPEN)
343 case TIPC_DISCONNECTING:
344 if (oldsk_state == TIPC_CONNECTING ||
345 oldsk_state == TIPC_ESTABLISHED)
351 sk->sk_state = state;
356 static int tipc_sk_sock_err(struct socket *sock, long *timeout)
358 struct sock *sk = sock->sk;
359 int err = sock_error(sk);
360 int typ = sock->type;
364 if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
365 if (sk->sk_state == TIPC_DISCONNECTING)
367 else if (!tipc_sk_connected(sk))
372 if (signal_pending(current))
373 return sock_intr_errno(*timeout);
378 #define tipc_wait_for_cond(sock_, timeo_, condition_) \
380 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
384 while ((rc_ = !(condition_))) { \
385 /* coupled with smp_wmb() in tipc_sk_proto_rcv() */ \
388 rc_ = tipc_sk_sock_err((sock_), timeo_); \
391 add_wait_queue(sk_sleep(sk_), &wait_); \
393 *(timeo_) = wait_woken(&wait_, TASK_INTERRUPTIBLE, *(timeo_)); \
394 sched_annotate_sleep(); \
396 remove_wait_queue(sk_sleep(sk_), &wait_); \
402 * tipc_sk_create - create a TIPC socket
403 * @net: network namespace (must be default network)
404 * @sock: pre-allocated socket structure
405 * @protocol: protocol indicator (must be 0)
406 * @kern: caused by kernel or by userspace?
408 * This routine creates additional data structures used by the TIPC socket,
409 * initializes them, and links them together.
411 * Returns 0 on success, errno otherwise
413 static int tipc_sk_create(struct net *net, struct socket *sock,
414 int protocol, int kern)
416 const struct proto_ops *ops;
418 struct tipc_sock *tsk;
419 struct tipc_msg *msg;
421 /* Validate arguments */
422 if (unlikely(protocol != 0))
423 return -EPROTONOSUPPORT;
425 switch (sock->type) {
440 /* Allocate socket's protocol area */
441 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
446 tsk->max_pkt = MAX_PKT_DEFAULT;
447 INIT_LIST_HEAD(&tsk->publications);
448 INIT_LIST_HEAD(&tsk->cong_links);
451 /* Finish initializing socket data structures */
453 sock_init_data(sock, sk);
454 tipc_set_sk_state(sk, TIPC_OPEN);
455 if (tipc_sk_insert(tsk)) {
457 pr_warn("Socket create failed; port number exhausted\n");
461 /* Ensure tsk is visible before we read own_addr. */
464 tipc_msg_init(tipc_own_addr(net), msg, TIPC_LOW_IMPORTANCE,
465 TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
467 msg_set_origport(msg, tsk->portid);
468 timer_setup(&sk->sk_timer, tipc_sk_timeout, 0);
470 sk->sk_backlog_rcv = tipc_sk_backlog_rcv;
471 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
472 sk->sk_data_ready = tipc_data_ready;
473 sk->sk_write_space = tipc_write_space;
474 sk->sk_destruct = tipc_sock_destruct;
475 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
476 tsk->group_is_open = true;
477 atomic_set(&tsk->dupl_rcvcnt, 0);
479 /* Start out with safe limits until we receive an advertised window */
480 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
481 tsk->rcv_win = tsk->snd_win;
483 if (tipc_sk_type_connectionless(sk)) {
484 tsk_set_unreturnable(tsk, true);
485 if (sock->type == SOCK_DGRAM)
486 tsk_set_unreliable(tsk, true);
492 static void tipc_sk_callback(struct rcu_head *head)
494 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
499 /* Caller should hold socket lock for the socket. */
500 static void __tipc_shutdown(struct socket *sock, int error)
502 struct sock *sk = sock->sk;
503 struct tipc_sock *tsk = tipc_sk(sk);
504 struct net *net = sock_net(sk);
505 long timeout = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
506 u32 dnode = tsk_peer_node(tsk);
509 /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
510 tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
511 !tsk_conn_cong(tsk)));
513 /* Reject all unreceived messages, except on an active connection
514 * (which disconnects locally & sends a 'FIN+' to peer).
516 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
517 if (TIPC_SKB_CB(skb)->bytes_read) {
521 if (!tipc_sk_type_connectionless(sk) &&
522 sk->sk_state != TIPC_DISCONNECTING) {
523 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
524 tipc_node_remove_conn(net, dnode, tsk->portid);
526 tipc_sk_respond(sk, skb, error);
529 if (tipc_sk_type_connectionless(sk))
532 if (sk->sk_state != TIPC_DISCONNECTING) {
533 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
534 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
535 tsk_own_node(tsk), tsk_peer_port(tsk),
538 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
539 tipc_node_remove_conn(net, dnode, tsk->portid);
540 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
545 * tipc_release - destroy a TIPC socket
546 * @sock: socket to destroy
548 * This routine cleans up any messages that are still queued on the socket.
549 * For DGRAM and RDM socket types, all queued messages are rejected.
550 * For SEQPACKET and STREAM socket types, the first message is rejected
551 * and any others are discarded. (If the first message on a STREAM socket
552 * is partially-read, it is discarded and the next one is rejected instead.)
554 * NOTE: Rejected messages are not necessarily returned to the sender! They
555 * are returned or discarded according to the "destination droppable" setting
556 * specified for the message by the sender.
558 * Returns 0 on success, errno otherwise
560 static int tipc_release(struct socket *sock)
562 struct sock *sk = sock->sk;
563 struct tipc_sock *tsk;
566 * Exit if socket isn't fully initialized (occurs when a failed accept()
567 * releases a pre-allocated child socket that was never used)
575 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
576 sk->sk_shutdown = SHUTDOWN_MASK;
578 tipc_sk_withdraw(tsk, 0, NULL);
579 sk_stop_timer(sk, &sk->sk_timer);
583 /* Reject any messages that accumulated in backlog queue */
585 tipc_dest_list_purge(&tsk->cong_links);
586 tsk->cong_link_cnt = 0;
587 call_rcu(&tsk->rcu, tipc_sk_callback);
594 * tipc_bind - associate or disassocate TIPC name(s) with a socket
595 * @sock: socket structure
596 * @uaddr: socket address describing name(s) and desired operation
597 * @uaddr_len: size of socket address data structure
599 * Name and name sequence binding is indicated using a positive scope value;
600 * a negative scope value unbinds the specified name. Specifying no name
601 * (i.e. a socket address length of 0) unbinds all names from the socket.
603 * Returns 0 on success, errno otherwise
605 * NOTE: This routine doesn't need to take the socket lock since it doesn't
606 * access any non-constant socket information.
608 static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
611 struct sock *sk = sock->sk;
612 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
613 struct tipc_sock *tsk = tipc_sk(sk);
617 if (unlikely(!uaddr_len)) {
618 res = tipc_sk_withdraw(tsk, 0, NULL);
625 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
629 if (addr->family != AF_TIPC) {
634 if (addr->addrtype == TIPC_ADDR_NAME)
635 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
636 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
641 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
642 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
643 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
648 res = (addr->scope >= 0) ?
649 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
650 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
657 * tipc_getname - get port ID of socket or peer socket
658 * @sock: socket structure
659 * @uaddr: area for returned socket address
660 * @uaddr_len: area for returned length of socket address
661 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
663 * Returns 0 on success, errno otherwise
665 * NOTE: This routine doesn't need to take the socket lock since it only
666 * accesses socket information that is unchanging (or which changes in
667 * a completely predictable manner).
669 static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
672 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
673 struct sock *sk = sock->sk;
674 struct tipc_sock *tsk = tipc_sk(sk);
676 memset(addr, 0, sizeof(*addr));
678 if ((!tipc_sk_connected(sk)) &&
679 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
681 addr->addr.id.ref = tsk_peer_port(tsk);
682 addr->addr.id.node = tsk_peer_node(tsk);
684 addr->addr.id.ref = tsk->portid;
685 addr->addr.id.node = tipc_own_addr(sock_net(sk));
688 addr->addrtype = TIPC_ADDR_ID;
689 addr->family = AF_TIPC;
691 addr->addr.name.domain = 0;
693 return sizeof(*addr);
697 * tipc_poll - read and possibly block on pollmask
698 * @file: file structure associated with the socket
699 * @sock: socket for which to calculate the poll bits
702 * Returns pollmask value
705 * It appears that the usual socket locking mechanisms are not useful here
706 * since the pollmask info is potentially out-of-date the moment this routine
707 * exits. TCP and other protocols seem to rely on higher level poll routines
708 * to handle any preventable race conditions, so TIPC will do the same ...
710 * IMPORTANT: The fact that a read or write operation is indicated does NOT
711 * imply that the operation will succeed, merely that it should be performed
712 * and will not block.
714 static __poll_t tipc_poll(struct file *file, struct socket *sock,
717 struct sock *sk = sock->sk;
718 struct tipc_sock *tsk = tipc_sk(sk);
719 __poll_t revents = 0;
721 sock_poll_wait(file, sock, wait);
723 if (sk->sk_shutdown & RCV_SHUTDOWN)
724 revents |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
725 if (sk->sk_shutdown == SHUTDOWN_MASK)
728 switch (sk->sk_state) {
729 case TIPC_ESTABLISHED:
730 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
734 case TIPC_CONNECTING:
735 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
736 revents |= EPOLLIN | EPOLLRDNORM;
739 if (tsk->group_is_open && !tsk->cong_link_cnt)
741 if (!tipc_sk_type_connectionless(sk))
743 if (skb_queue_empty_lockless(&sk->sk_receive_queue))
745 revents |= EPOLLIN | EPOLLRDNORM;
747 case TIPC_DISCONNECTING:
748 revents = EPOLLIN | EPOLLRDNORM | EPOLLHUP;
755 * tipc_sendmcast - send multicast message
756 * @sock: socket structure
757 * @seq: destination address
758 * @msg: message to send
759 * @dlen: length of data to send
760 * @timeout: timeout to wait for wakeup
762 * Called from function tipc_sendmsg(), which has done all sanity checks
763 * Returns the number of bytes sent on success, or errno
765 static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
766 struct msghdr *msg, size_t dlen, long timeout)
768 struct sock *sk = sock->sk;
769 struct tipc_sock *tsk = tipc_sk(sk);
770 struct tipc_msg *hdr = &tsk->phdr;
771 struct net *net = sock_net(sk);
772 int mtu = tipc_bcast_get_mtu(net);
773 struct tipc_mc_method *method = &tsk->mc_method;
774 struct sk_buff_head pkts;
775 struct tipc_nlist dsts;
781 /* Block or return if any destination link is congested */
782 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
786 /* Lookup destination nodes */
787 tipc_nlist_init(&dsts, tipc_own_addr(net));
788 tipc_nametbl_lookup_dst_nodes(net, seq->type, seq->lower,
790 if (!dsts.local && !dsts.remote)
791 return -EHOSTUNREACH;
793 /* Build message header */
794 msg_set_type(hdr, TIPC_MCAST_MSG);
795 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
796 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
797 msg_set_destport(hdr, 0);
798 msg_set_destnode(hdr, 0);
799 msg_set_nametype(hdr, seq->type);
800 msg_set_namelower(hdr, seq->lower);
801 msg_set_nameupper(hdr, seq->upper);
803 /* Build message as chain of buffers */
804 __skb_queue_head_init(&pkts);
805 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
807 /* Send message if build was successful */
808 if (unlikely(rc == dlen))
809 rc = tipc_mcast_xmit(net, &pkts, method, &dsts,
810 &tsk->cong_link_cnt);
812 tipc_nlist_purge(&dsts);
814 return rc ? rc : dlen;
818 * tipc_send_group_msg - send a message to a member in the group
819 * @net: network namespace
820 * @m: message to send
822 * @dnode: destination node
823 * @dport: destination port
824 * @dlen: total length of message data
826 static int tipc_send_group_msg(struct net *net, struct tipc_sock *tsk,
827 struct msghdr *m, struct tipc_member *mb,
828 u32 dnode, u32 dport, int dlen)
830 u16 bc_snd_nxt = tipc_group_bc_snd_nxt(tsk->group);
831 struct tipc_mc_method *method = &tsk->mc_method;
832 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
833 struct tipc_msg *hdr = &tsk->phdr;
834 struct sk_buff_head pkts;
837 /* Complete message header */
838 msg_set_type(hdr, TIPC_GRP_UCAST_MSG);
839 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
840 msg_set_destport(hdr, dport);
841 msg_set_destnode(hdr, dnode);
842 msg_set_grp_bc_seqno(hdr, bc_snd_nxt);
844 /* Build message as chain of buffers */
845 __skb_queue_head_init(&pkts);
846 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
847 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
848 if (unlikely(rc != dlen))
852 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
853 if (unlikely(rc == -ELINKCONG)) {
854 tipc_dest_push(&tsk->cong_links, dnode, 0);
855 tsk->cong_link_cnt++;
858 /* Update send window */
859 tipc_group_update_member(mb, blks);
861 /* A broadcast sent within next EXPIRE period must follow same path */
862 method->rcast = true;
863 method->mandatory = true;
868 * tipc_send_group_unicast - send message to a member in the group
869 * @sock: socket structure
870 * @m: message to send
871 * @dlen: total length of message data
872 * @timeout: timeout to wait for wakeup
874 * Called from function tipc_sendmsg(), which has done all sanity checks
875 * Returns the number of bytes sent on success, or errno
877 static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m,
878 int dlen, long timeout)
880 struct sock *sk = sock->sk;
881 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
882 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
883 struct tipc_sock *tsk = tipc_sk(sk);
884 struct net *net = sock_net(sk);
885 struct tipc_member *mb = NULL;
889 node = dest->addr.id.node;
890 port = dest->addr.id.ref;
892 return -EHOSTUNREACH;
894 /* Block or return if destination link or member is congested */
895 rc = tipc_wait_for_cond(sock, &timeout,
896 !tipc_dest_find(&tsk->cong_links, node, 0) &&
898 !tipc_group_cong(tsk->group, node, port, blks,
904 return -EHOSTUNREACH;
906 rc = tipc_send_group_msg(net, tsk, m, mb, node, port, dlen);
908 return rc ? rc : dlen;
912 * tipc_send_group_anycast - send message to any member with given identity
913 * @sock: socket structure
914 * @m: message to send
915 * @dlen: total length of message data
916 * @timeout: timeout to wait for wakeup
918 * Called from function tipc_sendmsg(), which has done all sanity checks
919 * Returns the number of bytes sent on success, or errno
921 static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m,
922 int dlen, long timeout)
924 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
925 struct sock *sk = sock->sk;
926 struct tipc_sock *tsk = tipc_sk(sk);
927 struct list_head *cong_links = &tsk->cong_links;
928 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
929 struct tipc_msg *hdr = &tsk->phdr;
930 struct tipc_member *first = NULL;
931 struct tipc_member *mbr = NULL;
932 struct net *net = sock_net(sk);
933 u32 node, port, exclude;
934 struct list_head dsts;
935 u32 type, inst, scope;
940 INIT_LIST_HEAD(&dsts);
942 type = msg_nametype(hdr);
943 inst = dest->addr.name.name.instance;
944 scope = msg_lookup_scope(hdr);
946 while (++lookups < 4) {
947 exclude = tipc_group_exclude(tsk->group);
951 /* Look for a non-congested destination member, if any */
953 if (!tipc_nametbl_lookup(net, type, inst, scope, &dsts,
954 &dstcnt, exclude, false))
955 return -EHOSTUNREACH;
956 tipc_dest_pop(&dsts, &node, &port);
957 cong = tipc_group_cong(tsk->group, node, port, blks,
967 /* Start over if destination was not in member list */
971 if (likely(!cong && !tipc_dest_find(cong_links, node, 0)))
974 /* Block or return if destination link or member is congested */
975 rc = tipc_wait_for_cond(sock, &timeout,
976 !tipc_dest_find(cong_links, node, 0) &&
978 !tipc_group_cong(tsk->group, node, port,
983 /* Send, unless destination disappeared while waiting */
988 if (unlikely(lookups >= 4))
989 return -EHOSTUNREACH;
991 rc = tipc_send_group_msg(net, tsk, m, mbr, node, port, dlen);
993 return rc ? rc : dlen;
997 * tipc_send_group_bcast - send message to all members in communication group
998 * @sk: socket structure
999 * @m: message to send
1000 * @dlen: total length of message data
1001 * @timeout: timeout to wait for wakeup
1003 * Called from function tipc_sendmsg(), which has done all sanity checks
1004 * Returns the number of bytes sent on success, or errno
1006 static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m,
1007 int dlen, long timeout)
1009 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1010 struct sock *sk = sock->sk;
1011 struct net *net = sock_net(sk);
1012 struct tipc_sock *tsk = tipc_sk(sk);
1013 struct tipc_nlist *dsts;
1014 struct tipc_mc_method *method = &tsk->mc_method;
1015 bool ack = method->mandatory && method->rcast;
1016 int blks = tsk_blocks(MCAST_H_SIZE + dlen);
1017 struct tipc_msg *hdr = &tsk->phdr;
1018 int mtu = tipc_bcast_get_mtu(net);
1019 struct sk_buff_head pkts;
1020 int rc = -EHOSTUNREACH;
1022 /* Block or return if any destination link or member is congested */
1023 rc = tipc_wait_for_cond(sock, &timeout,
1024 !tsk->cong_link_cnt && tsk->group &&
1025 !tipc_group_bc_cong(tsk->group, blks));
1029 dsts = tipc_group_dests(tsk->group);
1030 if (!dsts->local && !dsts->remote)
1031 return -EHOSTUNREACH;
1033 /* Complete message header */
1035 msg_set_type(hdr, TIPC_GRP_MCAST_MSG);
1036 msg_set_nameinst(hdr, dest->addr.name.name.instance);
1038 msg_set_type(hdr, TIPC_GRP_BCAST_MSG);
1039 msg_set_nameinst(hdr, 0);
1041 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
1042 msg_set_destport(hdr, 0);
1043 msg_set_destnode(hdr, 0);
1044 msg_set_grp_bc_seqno(hdr, tipc_group_bc_snd_nxt(tsk->group));
1046 /* Avoid getting stuck with repeated forced replicasts */
1047 msg_set_grp_bc_ack_req(hdr, ack);
1049 /* Build message as chain of buffers */
1050 __skb_queue_head_init(&pkts);
1051 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1052 if (unlikely(rc != dlen))
1056 rc = tipc_mcast_xmit(net, &pkts, method, dsts, &tsk->cong_link_cnt);
1060 /* Update broadcast sequence number and send windows */
1061 tipc_group_update_bc_members(tsk->group, blks, ack);
1063 /* Broadcast link is now free to choose method for next broadcast */
1064 method->mandatory = false;
1065 method->expires = jiffies;
1071 * tipc_send_group_mcast - send message to all members with given identity
1072 * @sock: socket structure
1073 * @m: message to send
1074 * @dlen: total length of message data
1075 * @timeout: timeout to wait for wakeup
1077 * Called from function tipc_sendmsg(), which has done all sanity checks
1078 * Returns the number of bytes sent on success, or errno
1080 static int tipc_send_group_mcast(struct socket *sock, struct msghdr *m,
1081 int dlen, long timeout)
1083 struct sock *sk = sock->sk;
1084 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1085 struct tipc_sock *tsk = tipc_sk(sk);
1086 struct tipc_group *grp = tsk->group;
1087 struct tipc_msg *hdr = &tsk->phdr;
1088 struct net *net = sock_net(sk);
1089 u32 type, inst, scope, exclude;
1090 struct list_head dsts;
1093 INIT_LIST_HEAD(&dsts);
1095 type = msg_nametype(hdr);
1096 inst = dest->addr.name.name.instance;
1097 scope = msg_lookup_scope(hdr);
1098 exclude = tipc_group_exclude(grp);
1100 if (!tipc_nametbl_lookup(net, type, inst, scope, &dsts,
1101 &dstcnt, exclude, true))
1102 return -EHOSTUNREACH;
1105 tipc_dest_pop(&dsts, &dest->addr.id.node, &dest->addr.id.ref);
1106 return tipc_send_group_unicast(sock, m, dlen, timeout);
1109 tipc_dest_list_purge(&dsts);
1110 return tipc_send_group_bcast(sock, m, dlen, timeout);
1114 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
1115 * @arrvq: queue with arriving messages, to be cloned after destination lookup
1116 * @inputq: queue with cloned messages, delivered to socket after dest lookup
1118 * Multi-threaded: parallel calls with reference to same queues may occur
1120 void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
1121 struct sk_buff_head *inputq)
1123 u32 self = tipc_own_addr(net);
1124 u32 type, lower, upper, scope;
1125 struct sk_buff *skb, *_skb;
1127 struct sk_buff_head tmpq;
1128 struct list_head dports;
1129 struct tipc_msg *hdr;
1130 int user, mtyp, hlen;
1133 __skb_queue_head_init(&tmpq);
1134 INIT_LIST_HEAD(&dports);
1136 skb = tipc_skb_peek(arrvq, &inputq->lock);
1137 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
1139 user = msg_user(hdr);
1140 mtyp = msg_type(hdr);
1141 hlen = skb_headroom(skb) + msg_hdr_sz(hdr);
1142 onode = msg_orignode(hdr);
1143 type = msg_nametype(hdr);
1145 if (mtyp == TIPC_GRP_UCAST_MSG || user == GROUP_PROTOCOL) {
1146 spin_lock_bh(&inputq->lock);
1147 if (skb_peek(arrvq) == skb) {
1148 __skb_dequeue(arrvq);
1149 __skb_queue_tail(inputq, skb);
1152 spin_unlock_bh(&inputq->lock);
1156 /* Group messages require exact scope match */
1157 if (msg_in_group(hdr)) {
1160 scope = msg_lookup_scope(hdr);
1163 /* TIPC_NODE_SCOPE means "any scope" in this context */
1165 scope = TIPC_NODE_SCOPE;
1167 scope = TIPC_CLUSTER_SCOPE;
1169 lower = msg_namelower(hdr);
1170 upper = msg_nameupper(hdr);
1173 /* Create destination port list: */
1174 tipc_nametbl_mc_lookup(net, type, lower, upper,
1175 scope, exact, &dports);
1177 /* Clone message per destination */
1178 while (tipc_dest_pop(&dports, NULL, &portid)) {
1179 _skb = __pskb_copy(skb, hlen, GFP_ATOMIC);
1181 msg_set_destport(buf_msg(_skb), portid);
1182 __skb_queue_tail(&tmpq, _skb);
1185 pr_warn("Failed to clone mcast rcv buffer\n");
1187 /* Append to inputq if not already done by other thread */
1188 spin_lock_bh(&inputq->lock);
1189 if (skb_peek(arrvq) == skb) {
1190 skb_queue_splice_tail_init(&tmpq, inputq);
1191 /* Decrease the skb's refcnt as increasing in the
1192 * function tipc_skb_peek
1194 kfree_skb(__skb_dequeue(arrvq));
1196 spin_unlock_bh(&inputq->lock);
1197 __skb_queue_purge(&tmpq);
1200 tipc_sk_rcv(net, inputq);
1204 * tipc_sk_conn_proto_rcv - receive a connection mng protocol message
1205 * @tsk: receiving socket
1206 * @skb: pointer to message buffer.
1208 static void tipc_sk_conn_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
1209 struct sk_buff_head *inputq,
1210 struct sk_buff_head *xmitq)
1212 struct tipc_msg *hdr = buf_msg(skb);
1213 u32 onode = tsk_own_node(tsk);
1214 struct sock *sk = &tsk->sk;
1215 int mtyp = msg_type(hdr);
1218 /* Ignore if connection cannot be validated: */
1219 if (!tsk_peer_msg(tsk, hdr))
1222 if (unlikely(msg_errcode(hdr))) {
1223 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1224 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
1225 tsk_peer_port(tsk));
1226 sk->sk_state_change(sk);
1228 /* State change is ignored if socket already awake,
1229 * - convert msg to abort msg and add to inqueue
1231 msg_set_user(hdr, TIPC_CRITICAL_IMPORTANCE);
1232 msg_set_type(hdr, TIPC_CONN_MSG);
1233 msg_set_size(hdr, BASIC_H_SIZE);
1234 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
1235 __skb_queue_tail(inputq, skb);
1239 tsk->probe_unacked = false;
1241 if (mtyp == CONN_PROBE) {
1242 msg_set_type(hdr, CONN_PROBE_REPLY);
1243 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
1244 __skb_queue_tail(xmitq, skb);
1246 } else if (mtyp == CONN_ACK) {
1247 conn_cong = tsk_conn_cong(tsk);
1248 tsk->snt_unacked -= msg_conn_ack(hdr);
1249 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1250 tsk->snd_win = msg_adv_win(hdr);
1252 sk->sk_write_space(sk);
1253 } else if (mtyp != CONN_PROBE_REPLY) {
1254 pr_warn("Received unknown CONN_PROTO msg\n");
1261 * tipc_sendmsg - send message in connectionless manner
1262 * @sock: socket structure
1263 * @m: message to send
1264 * @dsz: amount of user data to be sent
1266 * Message must have an destination specified explicitly.
1267 * Used for SOCK_RDM and SOCK_DGRAM messages,
1268 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
1269 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
1271 * Returns the number of bytes sent on success, or errno otherwise
1273 static int tipc_sendmsg(struct socket *sock,
1274 struct msghdr *m, size_t dsz)
1276 struct sock *sk = sock->sk;
1280 ret = __tipc_sendmsg(sock, m, dsz);
1286 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
1288 struct sock *sk = sock->sk;
1289 struct net *net = sock_net(sk);
1290 struct tipc_sock *tsk = tipc_sk(sk);
1291 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1292 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1293 struct list_head *clinks = &tsk->cong_links;
1294 bool syn = !tipc_sk_type_connectionless(sk);
1295 struct tipc_group *grp = tsk->group;
1296 struct tipc_msg *hdr = &tsk->phdr;
1297 struct tipc_name_seq *seq;
1298 struct sk_buff_head pkts;
1299 u32 dport, dnode = 0;
1303 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
1307 if (unlikely(m->msg_namelen < sizeof(*dest)))
1309 if (unlikely(dest->family != AF_TIPC))
1315 return tipc_send_group_bcast(sock, m, dlen, timeout);
1316 if (dest->addrtype == TIPC_ADDR_NAME)
1317 return tipc_send_group_anycast(sock, m, dlen, timeout);
1318 if (dest->addrtype == TIPC_ADDR_ID)
1319 return tipc_send_group_unicast(sock, m, dlen, timeout);
1320 if (dest->addrtype == TIPC_ADDR_MCAST)
1321 return tipc_send_group_mcast(sock, m, dlen, timeout);
1325 if (unlikely(!dest)) {
1327 if (!syn && dest->family != AF_TIPC)
1328 return -EDESTADDRREQ;
1331 if (unlikely(syn)) {
1332 if (sk->sk_state == TIPC_LISTEN)
1334 if (sk->sk_state != TIPC_OPEN)
1338 if (dest->addrtype == TIPC_ADDR_NAME) {
1339 tsk->conn_type = dest->addr.name.name.type;
1340 tsk->conn_instance = dest->addr.name.name.instance;
1344 seq = &dest->addr.nameseq;
1345 if (dest->addrtype == TIPC_ADDR_MCAST)
1346 return tipc_sendmcast(sock, seq, m, dlen, timeout);
1348 if (dest->addrtype == TIPC_ADDR_NAME) {
1349 type = dest->addr.name.name.type;
1350 inst = dest->addr.name.name.instance;
1351 dnode = dest->addr.name.domain;
1352 msg_set_type(hdr, TIPC_NAMED_MSG);
1353 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
1354 msg_set_nametype(hdr, type);
1355 msg_set_nameinst(hdr, inst);
1356 msg_set_lookup_scope(hdr, tipc_node2scope(dnode));
1357 dport = tipc_nametbl_translate(net, type, inst, &dnode);
1358 msg_set_destnode(hdr, dnode);
1359 msg_set_destport(hdr, dport);
1360 if (unlikely(!dport && !dnode))
1361 return -EHOSTUNREACH;
1362 } else if (dest->addrtype == TIPC_ADDR_ID) {
1363 dnode = dest->addr.id.node;
1364 msg_set_type(hdr, TIPC_DIRECT_MSG);
1365 msg_set_lookup_scope(hdr, 0);
1366 msg_set_destnode(hdr, dnode);
1367 msg_set_destport(hdr, dest->addr.id.ref);
1368 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
1373 /* Block or return if destination link is congested */
1374 rc = tipc_wait_for_cond(sock, &timeout,
1375 !tipc_dest_find(clinks, dnode, 0));
1379 __skb_queue_head_init(&pkts);
1380 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
1381 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1382 if (unlikely(rc != dlen))
1385 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1386 if (unlikely(rc == -ELINKCONG)) {
1387 tipc_dest_push(clinks, dnode, 0);
1388 tsk->cong_link_cnt++;
1392 if (unlikely(syn && !rc))
1393 tipc_set_sk_state(sk, TIPC_CONNECTING);
1395 return rc ? rc : dlen;
1399 * tipc_sendstream - send stream-oriented data
1400 * @sock: socket structure
1402 * @dsz: total length of data to be transmitted
1404 * Used for SOCK_STREAM data.
1406 * Returns the number of bytes sent on success (or partial success),
1407 * or errno if no data sent
1409 static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
1411 struct sock *sk = sock->sk;
1415 ret = __tipc_sendstream(sock, m, dsz);
1421 static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
1423 struct sock *sk = sock->sk;
1424 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1425 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1426 struct tipc_sock *tsk = tipc_sk(sk);
1427 struct tipc_msg *hdr = &tsk->phdr;
1428 struct net *net = sock_net(sk);
1429 struct sk_buff_head pkts;
1430 u32 dnode = tsk_peer_node(tsk);
1434 __skb_queue_head_init(&pkts);
1436 if (unlikely(dlen > INT_MAX))
1439 /* Handle implicit connection setup */
1440 if (unlikely(dest)) {
1441 rc = __tipc_sendmsg(sock, m, dlen);
1442 if (dlen && dlen == rc) {
1443 tsk->peer_caps = tipc_node_get_capabilities(net, dnode);
1444 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
1450 rc = tipc_wait_for_cond(sock, &timeout,
1451 (!tsk->cong_link_cnt &&
1452 !tsk_conn_cong(tsk) &&
1453 tipc_sk_connected(sk)));
1457 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
1458 rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
1459 if (unlikely(rc != send))
1462 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1463 if (unlikely(rc == -ELINKCONG)) {
1464 tsk->cong_link_cnt = 1;
1468 tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
1471 } while (sent < dlen && !rc);
1473 return sent ? sent : rc;
1477 * tipc_send_packet - send a connection-oriented message
1478 * @sock: socket structure
1479 * @m: message to send
1480 * @dsz: length of data to be transmitted
1482 * Used for SOCK_SEQPACKET messages.
1484 * Returns the number of bytes sent on success, or errno otherwise
1486 static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
1488 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1491 return tipc_sendstream(sock, m, dsz);
1494 /* tipc_sk_finish_conn - complete the setup of a connection
1496 static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
1499 struct sock *sk = &tsk->sk;
1500 struct net *net = sock_net(sk);
1501 struct tipc_msg *msg = &tsk->phdr;
1503 msg_set_destnode(msg, peer_node);
1504 msg_set_destport(msg, peer_port);
1505 msg_set_type(msg, TIPC_CONN_MSG);
1506 msg_set_lookup_scope(msg, 0);
1507 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1509 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
1510 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
1511 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1512 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
1513 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
1514 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1517 /* Fall back to message based flow control */
1518 tsk->rcv_win = FLOWCTL_MSG_WIN;
1519 tsk->snd_win = FLOWCTL_MSG_WIN;
1523 * tipc_sk_set_orig_addr - capture sender's address for received message
1524 * @m: descriptor for message info
1525 * @hdr: received message header
1527 * Note: Address is not captured if not requested by receiver.
1529 static void tipc_sk_set_orig_addr(struct msghdr *m, struct sk_buff *skb)
1531 DECLARE_SOCKADDR(struct sockaddr_pair *, srcaddr, m->msg_name);
1532 struct tipc_msg *hdr = buf_msg(skb);
1537 srcaddr->sock.family = AF_TIPC;
1538 srcaddr->sock.addrtype = TIPC_ADDR_ID;
1539 srcaddr->sock.scope = 0;
1540 srcaddr->sock.addr.id.ref = msg_origport(hdr);
1541 srcaddr->sock.addr.id.node = msg_orignode(hdr);
1542 srcaddr->sock.addr.name.domain = 0;
1543 m->msg_namelen = sizeof(struct sockaddr_tipc);
1545 if (!msg_in_group(hdr))
1548 /* Group message users may also want to know sending member's id */
1549 srcaddr->member.family = AF_TIPC;
1550 srcaddr->member.addrtype = TIPC_ADDR_NAME;
1551 srcaddr->member.scope = 0;
1552 srcaddr->member.addr.name.name.type = msg_nametype(hdr);
1553 srcaddr->member.addr.name.name.instance = TIPC_SKB_CB(skb)->orig_member;
1554 srcaddr->member.addr.name.domain = 0;
1555 m->msg_namelen = sizeof(*srcaddr);
1559 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
1560 * @m: descriptor for message info
1561 * @skb: received message buffer
1562 * @tsk: TIPC port associated with message
1564 * Note: Ancillary data is not captured if not requested by receiver.
1566 * Returns 0 if successful, otherwise errno
1568 static int tipc_sk_anc_data_recv(struct msghdr *m, struct sk_buff *skb,
1569 struct tipc_sock *tsk)
1571 struct tipc_msg *msg;
1578 if (likely(m->msg_controllen == 0))
1582 /* Optionally capture errored message object(s) */
1583 err = msg ? msg_errcode(msg) : 0;
1584 if (unlikely(err)) {
1586 anc_data[1] = msg_data_sz(msg);
1587 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1591 if (skb_linearize(skb))
1594 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1601 /* Optionally capture message destination object */
1602 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1603 switch (dest_type) {
1604 case TIPC_NAMED_MSG:
1606 anc_data[0] = msg_nametype(msg);
1607 anc_data[1] = msg_namelower(msg);
1608 anc_data[2] = msg_namelower(msg);
1610 case TIPC_MCAST_MSG:
1612 anc_data[0] = msg_nametype(msg);
1613 anc_data[1] = msg_namelower(msg);
1614 anc_data[2] = msg_nameupper(msg);
1617 has_name = (tsk->conn_type != 0);
1618 anc_data[0] = tsk->conn_type;
1619 anc_data[1] = tsk->conn_instance;
1620 anc_data[2] = tsk->conn_instance;
1626 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1634 static void tipc_sk_send_ack(struct tipc_sock *tsk)
1636 struct sock *sk = &tsk->sk;
1637 struct net *net = sock_net(sk);
1638 struct sk_buff *skb = NULL;
1639 struct tipc_msg *msg;
1640 u32 peer_port = tsk_peer_port(tsk);
1641 u32 dnode = tsk_peer_node(tsk);
1643 if (!tipc_sk_connected(sk))
1645 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1646 dnode, tsk_own_node(tsk), peer_port,
1647 tsk->portid, TIPC_OK);
1651 msg_set_conn_ack(msg, tsk->rcv_unacked);
1652 tsk->rcv_unacked = 0;
1654 /* Adjust to and advertize the correct window limit */
1655 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1656 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1657 msg_set_adv_win(msg, tsk->rcv_win);
1659 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
1662 static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1664 struct sock *sk = sock->sk;
1666 long timeo = *timeop;
1667 int err = sock_error(sk);
1673 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1674 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
1675 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1680 timeo = schedule_timeout(timeo);
1684 if (!skb_queue_empty(&sk->sk_receive_queue))
1689 err = sock_intr_errno(timeo);
1690 if (signal_pending(current))
1693 err = sock_error(sk);
1697 finish_wait(sk_sleep(sk), &wait);
1703 * tipc_recvmsg - receive packet-oriented message
1704 * @m: descriptor for message info
1705 * @buflen: length of user buffer area
1706 * @flags: receive flags
1708 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1709 * If the complete message doesn't fit in user area, truncate it.
1711 * Returns size of returned message data, errno otherwise
1713 static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
1714 size_t buflen, int flags)
1716 struct sock *sk = sock->sk;
1717 bool connected = !tipc_sk_type_connectionless(sk);
1718 struct tipc_sock *tsk = tipc_sk(sk);
1719 int rc, err, hlen, dlen, copy;
1720 struct tipc_skb_cb *skb_cb;
1721 struct sk_buff_head xmitq;
1722 struct tipc_msg *hdr;
1723 struct sk_buff *skb;
1727 /* Catch invalid receive requests */
1728 if (unlikely(!buflen))
1732 if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
1736 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1738 /* Step rcv queue to first msg with data or error; wait if necessary */
1740 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1743 skb = skb_peek(&sk->sk_receive_queue);
1744 skb_cb = TIPC_SKB_CB(skb);
1746 dlen = msg_data_sz(hdr);
1747 hlen = msg_hdr_sz(hdr);
1748 err = msg_errcode(hdr);
1749 grp_evt = msg_is_grp_evt(hdr);
1750 if (likely(dlen || err))
1752 tsk_advance_rx_queue(sk);
1755 /* Collect msg meta data, including error code and rejected data */
1756 tipc_sk_set_orig_addr(m, skb);
1757 rc = tipc_sk_anc_data_recv(m, skb, tsk);
1762 /* Capture data if non-error msg, otherwise just set return value */
1764 int offset = skb_cb->bytes_read;
1766 copy = min_t(int, dlen - offset, buflen);
1767 rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
1770 if (unlikely(offset + copy < dlen)) {
1771 if (flags & MSG_EOR) {
1772 if (!(flags & MSG_PEEK))
1773 skb_cb->bytes_read = offset + copy;
1775 m->msg_flags |= MSG_TRUNC;
1776 skb_cb->bytes_read = 0;
1779 if (flags & MSG_EOR)
1780 m->msg_flags |= MSG_EOR;
1781 skb_cb->bytes_read = 0;
1786 if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control) {
1792 /* Mark message as group event if applicable */
1793 if (unlikely(grp_evt)) {
1794 if (msg_grp_evt(hdr) == TIPC_WITHDRAWN)
1795 m->msg_flags |= MSG_EOR;
1796 m->msg_flags |= MSG_OOB;
1800 /* Caption of data or error code/rejected data was successful */
1801 if (unlikely(flags & MSG_PEEK))
1804 /* Send group flow control advertisement when applicable */
1805 if (tsk->group && msg_in_group(hdr) && !grp_evt) {
1806 __skb_queue_head_init(&xmitq);
1807 tipc_group_update_rcv_win(tsk->group, tsk_blocks(hlen + dlen),
1808 msg_orignode(hdr), msg_origport(hdr),
1810 tipc_node_distr_xmit(sock_net(sk), &xmitq);
1813 if (skb_cb->bytes_read)
1816 tsk_advance_rx_queue(sk);
1818 if (likely(!connected))
1821 /* Send connection flow control advertisement when applicable */
1822 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1823 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
1824 tipc_sk_send_ack(tsk);
1827 return rc ? rc : copy;
1831 * tipc_recvstream - receive stream-oriented data
1832 * @m: descriptor for message info
1833 * @buflen: total size of user buffer area
1834 * @flags: receive flags
1836 * Used for SOCK_STREAM messages only. If not enough data is available
1837 * will optionally wait for more; never truncates data.
1839 * Returns size of returned message data, errno otherwise
1841 static int tipc_recvstream(struct socket *sock, struct msghdr *m,
1842 size_t buflen, int flags)
1844 struct sock *sk = sock->sk;
1845 struct tipc_sock *tsk = tipc_sk(sk);
1846 struct sk_buff *skb;
1847 struct tipc_msg *hdr;
1848 struct tipc_skb_cb *skb_cb;
1849 bool peek = flags & MSG_PEEK;
1850 int offset, required, copy, copied = 0;
1851 int hlen, dlen, err, rc;
1854 /* Catch invalid receive attempts */
1855 if (unlikely(!buflen))
1860 if (unlikely(sk->sk_state == TIPC_OPEN)) {
1864 required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
1865 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1868 /* Look at first msg in receive queue; wait if necessary */
1869 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1872 skb = skb_peek(&sk->sk_receive_queue);
1873 skb_cb = TIPC_SKB_CB(skb);
1875 dlen = msg_data_sz(hdr);
1876 hlen = msg_hdr_sz(hdr);
1877 err = msg_errcode(hdr);
1879 /* Discard any empty non-errored (SYN-) message */
1880 if (unlikely(!dlen && !err)) {
1881 tsk_advance_rx_queue(sk);
1885 /* Collect msg meta data, incl. error code and rejected data */
1887 tipc_sk_set_orig_addr(m, skb);
1888 rc = tipc_sk_anc_data_recv(m, skb, tsk);
1894 /* Copy data if msg ok, otherwise return error/partial data */
1896 offset = skb_cb->bytes_read;
1897 copy = min_t(int, dlen - offset, buflen - copied);
1898 rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
1903 if (unlikely(offset < dlen)) {
1905 skb_cb->bytes_read = offset;
1910 if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control)
1919 tsk_advance_rx_queue(sk);
1921 /* Send connection flow control advertisement when applicable */
1922 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1923 if (unlikely(tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE))
1924 tipc_sk_send_ack(tsk);
1926 /* Exit if all requested data or FIN/error received */
1927 if (copied == buflen || err)
1930 } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required);
1933 return copied ? copied : rc;
1937 * tipc_write_space - wake up thread if port congestion is released
1940 static void tipc_write_space(struct sock *sk)
1942 struct socket_wq *wq;
1945 wq = rcu_dereference(sk->sk_wq);
1946 if (skwq_has_sleeper(wq))
1947 wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
1948 EPOLLWRNORM | EPOLLWRBAND);
1953 * tipc_data_ready - wake up threads to indicate messages have been received
1955 * @len: the length of messages
1957 static void tipc_data_ready(struct sock *sk)
1959 struct socket_wq *wq;
1962 wq = rcu_dereference(sk->sk_wq);
1963 if (skwq_has_sleeper(wq))
1964 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
1965 EPOLLRDNORM | EPOLLRDBAND);
1969 static void tipc_sock_destruct(struct sock *sk)
1971 __skb_queue_purge(&sk->sk_receive_queue);
1974 static void tipc_sk_proto_rcv(struct sock *sk,
1975 struct sk_buff_head *inputq,
1976 struct sk_buff_head *xmitq)
1978 struct sk_buff *skb = __skb_dequeue(inputq);
1979 struct tipc_sock *tsk = tipc_sk(sk);
1980 struct tipc_msg *hdr = buf_msg(skb);
1981 struct tipc_group *grp = tsk->group;
1982 bool wakeup = false;
1984 switch (msg_user(hdr)) {
1986 tipc_sk_conn_proto_rcv(tsk, skb, inputq, xmitq);
1989 tipc_dest_del(&tsk->cong_links, msg_orignode(hdr), 0);
1990 /* coupled with smp_rmb() in tipc_wait_for_cond() */
1992 tsk->cong_link_cnt--;
1995 case GROUP_PROTOCOL:
1996 tipc_group_proto_rcv(grp, &wakeup, hdr, inputq, xmitq);
1999 tipc_group_member_evt(tsk->group, &wakeup, &sk->sk_rcvbuf,
2000 hdr, inputq, xmitq);
2007 sk->sk_write_space(sk);
2013 * tipc_filter_connect - Handle incoming message for a connection-based socket
2015 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
2017 * Returns true if everything ok, false otherwise
2019 static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
2021 struct sock *sk = &tsk->sk;
2022 struct net *net = sock_net(sk);
2023 struct tipc_msg *hdr = buf_msg(skb);
2024 u32 pport = msg_origport(hdr);
2025 u32 pnode = msg_orignode(hdr);
2027 if (unlikely(msg_mcast(hdr)))
2030 switch (sk->sk_state) {
2031 case TIPC_CONNECTING:
2032 /* Accept only ACK or NACK message */
2033 if (unlikely(!msg_connected(hdr))) {
2034 if (pport != tsk_peer_port(tsk) ||
2035 pnode != tsk_peer_node(tsk))
2038 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2039 sk->sk_err = ECONNREFUSED;
2040 sk->sk_state_change(sk);
2044 if (unlikely(msg_errcode(hdr))) {
2045 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2046 sk->sk_err = ECONNREFUSED;
2047 sk->sk_state_change(sk);
2051 if (unlikely(!msg_isdata(hdr))) {
2052 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2053 sk->sk_err = EINVAL;
2054 sk->sk_state_change(sk);
2058 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
2059 msg_set_importance(&tsk->phdr, msg_importance(hdr));
2061 /* If 'ACK+' message, add to socket receive queue */
2062 if (msg_data_sz(hdr))
2065 /* If empty 'ACK-' message, wake up sleeping connect() */
2066 sk->sk_state_change(sk);
2068 /* 'ACK-' message is neither accepted nor rejected: */
2069 msg_set_dest_droppable(hdr, 1);
2073 case TIPC_DISCONNECTING:
2076 /* Accept only SYN message */
2077 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
2080 case TIPC_ESTABLISHED:
2081 /* Accept only connection-based messages sent by peer */
2082 if (unlikely(!tsk_peer_msg(tsk, hdr)))
2085 if (unlikely(msg_errcode(hdr))) {
2086 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2087 /* Let timer expire on it's own */
2088 tipc_node_remove_conn(net, tsk_peer_node(tsk),
2090 sk->sk_state_change(sk);
2094 pr_err("Unknown sk_state %u\n", sk->sk_state);
2101 * rcvbuf_limit - get proper overload limit of socket receive queue
2105 * For connection oriented messages, irrespective of importance,
2106 * default queue limit is 2 MB.
2108 * For connectionless messages, queue limits are based on message
2109 * importance as follows:
2111 * TIPC_LOW_IMPORTANCE (2 MB)
2112 * TIPC_MEDIUM_IMPORTANCE (4 MB)
2113 * TIPC_HIGH_IMPORTANCE (8 MB)
2114 * TIPC_CRITICAL_IMPORTANCE (16 MB)
2116 * Returns overload limit according to corresponding message importance
2118 static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
2120 struct tipc_sock *tsk = tipc_sk(sk);
2121 struct tipc_msg *hdr = buf_msg(skb);
2123 if (unlikely(msg_in_group(hdr)))
2124 return sk->sk_rcvbuf;
2126 if (unlikely(!msg_connected(hdr)))
2127 return sk->sk_rcvbuf << msg_importance(hdr);
2129 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
2130 return sk->sk_rcvbuf;
2132 return FLOWCTL_MSG_LIM;
2136 * tipc_sk_filter_rcv - validate incoming message
2138 * @skb: pointer to message.
2140 * Enqueues message on receive queue if acceptable; optionally handles
2141 * disconnect indication for a connected socket.
2143 * Called with socket lock already taken
2146 static void tipc_sk_filter_rcv(struct sock *sk, struct sk_buff *skb,
2147 struct sk_buff_head *xmitq)
2149 bool sk_conn = !tipc_sk_type_connectionless(sk);
2150 struct tipc_sock *tsk = tipc_sk(sk);
2151 struct tipc_group *grp = tsk->group;
2152 struct tipc_msg *hdr = buf_msg(skb);
2153 struct net *net = sock_net(sk);
2154 struct sk_buff_head inputq;
2155 int limit, err = TIPC_OK;
2157 TIPC_SKB_CB(skb)->bytes_read = 0;
2158 __skb_queue_head_init(&inputq);
2159 __skb_queue_tail(&inputq, skb);
2161 if (unlikely(!msg_isdata(hdr)))
2162 tipc_sk_proto_rcv(sk, &inputq, xmitq);
2165 tipc_group_filter_msg(grp, &inputq, xmitq);
2167 /* Validate and add to receive buffer if there is space */
2168 while ((skb = __skb_dequeue(&inputq))) {
2170 limit = rcvbuf_limit(sk, skb);
2171 if ((sk_conn && !tipc_sk_filter_connect(tsk, skb)) ||
2172 (!sk_conn && msg_connected(hdr)) ||
2173 (!grp && msg_in_group(hdr)))
2174 err = TIPC_ERR_NO_PORT;
2175 else if (sk_rmem_alloc_get(sk) + skb->truesize >= limit) {
2176 atomic_inc(&sk->sk_drops);
2177 err = TIPC_ERR_OVERLOAD;
2180 if (unlikely(err)) {
2181 tipc_skb_reject(net, err, skb, xmitq);
2185 __skb_queue_tail(&sk->sk_receive_queue, skb);
2186 skb_set_owner_r(skb, sk);
2187 sk->sk_data_ready(sk);
2192 * tipc_sk_backlog_rcv - handle incoming message from backlog queue
2196 * Caller must hold socket lock
2198 static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
2200 unsigned int before = sk_rmem_alloc_get(sk);
2201 struct sk_buff_head xmitq;
2204 __skb_queue_head_init(&xmitq);
2206 tipc_sk_filter_rcv(sk, skb, &xmitq);
2207 added = sk_rmem_alloc_get(sk) - before;
2208 atomic_add(added, &tipc_sk(sk)->dupl_rcvcnt);
2210 /* Send pending response/rejected messages, if any */
2211 tipc_node_distr_xmit(sock_net(sk), &xmitq);
2216 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
2217 * inputq and try adding them to socket or backlog queue
2218 * @inputq: list of incoming buffers with potentially different destinations
2219 * @sk: socket where the buffers should be enqueued
2220 * @dport: port number for the socket
2222 * Caller must hold socket lock
2224 static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
2225 u32 dport, struct sk_buff_head *xmitq)
2227 unsigned long time_limit = jiffies + usecs_to_jiffies(20000);
2228 struct sk_buff *skb;
2233 while (skb_queue_len(inputq)) {
2234 if (unlikely(time_after_eq(jiffies, time_limit)))
2237 skb = tipc_skb_dequeue(inputq, dport);
2241 /* Add message directly to receive queue if possible */
2242 if (!sock_owned_by_user(sk)) {
2243 tipc_sk_filter_rcv(sk, skb, xmitq);
2247 /* Try backlog, compensating for double-counted bytes */
2248 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
2249 if (!sk->sk_backlog.len)
2250 atomic_set(dcnt, 0);
2251 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
2252 if (likely(!sk_add_backlog(sk, skb, lim)))
2255 /* Overload => reject message back to sender */
2256 onode = tipc_own_addr(sock_net(sk));
2257 atomic_inc(&sk->sk_drops);
2258 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
2259 __skb_queue_tail(xmitq, skb);
2265 * tipc_sk_rcv - handle a chain of incoming buffers
2266 * @inputq: buffer list containing the buffers
2267 * Consumes all buffers in list until inputq is empty
2268 * Note: may be called in multiple threads referring to the same queue
2270 void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
2272 struct sk_buff_head xmitq;
2273 u32 dnode, dport = 0;
2275 struct tipc_sock *tsk;
2277 struct sk_buff *skb;
2279 __skb_queue_head_init(&xmitq);
2280 while (skb_queue_len(inputq)) {
2281 dport = tipc_skb_peek_port(inputq, dport);
2282 tsk = tipc_sk_lookup(net, dport);
2286 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
2287 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
2288 spin_unlock_bh(&sk->sk_lock.slock);
2290 /* Send pending response/rejected messages, if any */
2291 tipc_node_distr_xmit(sock_net(sk), &xmitq);
2295 /* No destination socket => dequeue skb if still there */
2296 skb = tipc_skb_dequeue(inputq, dport);
2300 /* Try secondary lookup if unresolved named message */
2301 err = TIPC_ERR_NO_PORT;
2302 if (tipc_msg_lookup_dest(net, skb, &err))
2305 /* Prepare for message rejection */
2306 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
2309 dnode = msg_destnode(buf_msg(skb));
2310 tipc_node_xmit_skb(net, skb, dnode, dport);
2314 static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
2316 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2317 struct sock *sk = sock->sk;
2321 int err = sock_error(sk);
2326 if (signal_pending(current))
2327 return sock_intr_errno(*timeo_p);
2329 add_wait_queue(sk_sleep(sk), &wait);
2330 done = sk_wait_event(sk, timeo_p,
2331 sk->sk_state != TIPC_CONNECTING, &wait);
2332 remove_wait_queue(sk_sleep(sk), &wait);
2337 static bool tipc_sockaddr_is_sane(struct sockaddr_tipc *addr)
2339 if (addr->family != AF_TIPC)
2341 if (addr->addrtype == TIPC_SERVICE_RANGE)
2342 return (addr->addr.nameseq.lower <= addr->addr.nameseq.upper);
2343 return (addr->addrtype == TIPC_SERVICE_ADDR ||
2344 addr->addrtype == TIPC_SOCKET_ADDR);
2348 * tipc_connect - establish a connection to another TIPC port
2349 * @sock: socket structure
2350 * @dest: socket address for destination port
2351 * @destlen: size of socket address data structure
2352 * @flags: file-related flags associated with socket
2354 * Returns 0 on success, errno otherwise
2356 static int tipc_connect(struct socket *sock, struct sockaddr *dest,
2357 int destlen, int flags)
2359 struct sock *sk = sock->sk;
2360 struct tipc_sock *tsk = tipc_sk(sk);
2361 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
2362 struct msghdr m = {NULL,};
2363 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
2367 if (destlen != sizeof(struct sockaddr_tipc))
2377 if (dst->family == AF_UNSPEC) {
2378 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
2379 if (!tipc_sk_type_connectionless(sk))
2383 if (!tipc_sockaddr_is_sane(dst)) {
2387 /* DGRAM/RDM connect(), just save the destaddr */
2388 if (tipc_sk_type_connectionless(sk)) {
2389 memcpy(&tsk->peer, dest, destlen);
2391 } else if (dst->addrtype == TIPC_SERVICE_RANGE) {
2396 previous = sk->sk_state;
2398 switch (sk->sk_state) {
2400 /* Send a 'SYN-' to destination */
2402 m.msg_namelen = destlen;
2404 /* If connect is in non-blocking case, set MSG_DONTWAIT to
2405 * indicate send_msg() is never blocked.
2408 m.msg_flags = MSG_DONTWAIT;
2410 res = __tipc_sendmsg(sock, &m, 0);
2411 if ((res < 0) && (res != -EWOULDBLOCK))
2414 /* Just entered TIPC_CONNECTING state; the only
2415 * difference is that return value in non-blocking
2416 * case is EINPROGRESS, rather than EALREADY.
2420 case TIPC_CONNECTING:
2422 if (previous == TIPC_CONNECTING)
2426 timeout = msecs_to_jiffies(timeout);
2427 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
2428 res = tipc_wait_for_connect(sock, &timeout);
2430 case TIPC_ESTABLISHED:
2443 * tipc_listen - allow socket to listen for incoming connections
2444 * @sock: socket structure
2447 * Returns 0 on success, errno otherwise
2449 static int tipc_listen(struct socket *sock, int len)
2451 struct sock *sk = sock->sk;
2455 res = tipc_set_sk_state(sk, TIPC_LISTEN);
2461 static int tipc_wait_for_accept(struct socket *sock, long timeo)
2463 struct sock *sk = sock->sk;
2464 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2467 /* True wake-one mechanism for incoming connections: only
2468 * one process gets woken up, not the 'whole herd'.
2469 * Since we do not 'race & poll' for established sockets
2470 * anymore, the common case will execute the loop only once.
2473 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
2474 add_wait_queue(sk_sleep(sk), &wait);
2476 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
2478 remove_wait_queue(sk_sleep(sk), &wait);
2481 if (!skb_queue_empty(&sk->sk_receive_queue))
2486 err = sock_intr_errno(timeo);
2487 if (signal_pending(current))
2494 * tipc_accept - wait for connection request
2495 * @sock: listening socket
2496 * @newsock: new socket that is to be connected
2497 * @flags: file-related flags associated with socket
2499 * Returns 0 on success, errno otherwise
2501 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
2504 struct sock *new_sk, *sk = sock->sk;
2505 struct sk_buff *buf;
2506 struct tipc_sock *new_tsock;
2507 struct tipc_msg *msg;
2513 if (sk->sk_state != TIPC_LISTEN) {
2517 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2518 res = tipc_wait_for_accept(sock, timeo);
2522 buf = skb_peek(&sk->sk_receive_queue);
2524 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
2527 security_sk_clone(sock->sk, new_sock->sk);
2529 new_sk = new_sock->sk;
2530 new_tsock = tipc_sk(new_sk);
2533 /* we lock on new_sk; but lockdep sees the lock on sk */
2534 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
2537 * Reject any stray messages received by new socket
2538 * before the socket lock was taken (very, very unlikely)
2540 tsk_rej_rx_queue(new_sk);
2542 /* Connect new socket to it's peer */
2543 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
2545 tsk_set_importance(new_tsock, msg_importance(msg));
2546 if (msg_named(msg)) {
2547 new_tsock->conn_type = msg_nametype(msg);
2548 new_tsock->conn_instance = msg_nameinst(msg);
2552 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2553 * Respond to 'SYN+' by queuing it on new socket.
2555 if (!msg_data_sz(msg)) {
2556 struct msghdr m = {NULL,};
2558 tsk_advance_rx_queue(sk);
2559 __tipc_sendstream(new_sock, &m, 0);
2561 __skb_dequeue(&sk->sk_receive_queue);
2562 __skb_queue_head(&new_sk->sk_receive_queue, buf);
2563 skb_set_owner_r(buf, new_sk);
2565 release_sock(new_sk);
2572 * tipc_shutdown - shutdown socket connection
2573 * @sock: socket structure
2574 * @how: direction to close (must be SHUT_RDWR)
2576 * Terminates connection (if necessary), then purges socket's receive queue.
2578 * Returns 0 on success, errno otherwise
2580 static int tipc_shutdown(struct socket *sock, int how)
2582 struct sock *sk = sock->sk;
2585 if (how != SHUT_RDWR)
2590 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2591 sk->sk_shutdown = SHUTDOWN_MASK;
2593 if (sk->sk_state == TIPC_DISCONNECTING) {
2594 /* Discard any unreceived messages */
2595 __skb_queue_purge(&sk->sk_receive_queue);
2601 /* Wake up anyone sleeping in poll. */
2602 sk->sk_state_change(sk);
2608 static void tipc_sk_timeout(struct timer_list *t)
2610 struct sock *sk = from_timer(sk, t, sk_timer);
2611 struct tipc_sock *tsk = tipc_sk(sk);
2612 u32 peer_port = tsk_peer_port(tsk);
2613 u32 peer_node = tsk_peer_node(tsk);
2614 u32 own_node = tsk_own_node(tsk);
2615 u32 own_port = tsk->portid;
2616 struct net *net = sock_net(sk);
2617 struct sk_buff *skb = NULL;
2620 if (!tipc_sk_connected(sk))
2623 /* Try again later if socket is busy */
2624 if (sock_owned_by_user(sk)) {
2625 sk_reset_timer(sk, &sk->sk_timer, jiffies + HZ / 20);
2629 if (tsk->probe_unacked) {
2630 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2631 tipc_node_remove_conn(net, peer_node, peer_port);
2632 sk->sk_state_change(sk);
2635 /* Send new probe */
2636 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, 0,
2637 peer_node, own_node, peer_port, own_port,
2639 tsk->probe_unacked = true;
2640 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
2644 tipc_node_xmit_skb(net, skb, peer_node, own_port);
2648 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
2649 struct tipc_name_seq const *seq)
2651 struct sock *sk = &tsk->sk;
2652 struct net *net = sock_net(sk);
2653 struct publication *publ;
2656 if (scope != TIPC_NODE_SCOPE)
2657 scope = TIPC_CLUSTER_SCOPE;
2659 if (tipc_sk_connected(sk))
2661 key = tsk->portid + tsk->pub_count + 1;
2662 if (key == tsk->portid)
2665 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
2666 scope, tsk->portid, key);
2667 if (unlikely(!publ))
2670 list_add(&publ->binding_sock, &tsk->publications);
2676 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
2677 struct tipc_name_seq const *seq)
2679 struct net *net = sock_net(&tsk->sk);
2680 struct publication *publ;
2681 struct publication *safe;
2684 if (scope != TIPC_NODE_SCOPE)
2685 scope = TIPC_CLUSTER_SCOPE;
2687 list_for_each_entry_safe(publ, safe, &tsk->publications, binding_sock) {
2689 if (publ->scope != scope)
2691 if (publ->type != seq->type)
2693 if (publ->lower != seq->lower)
2695 if (publ->upper != seq->upper)
2697 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2698 publ->upper, publ->key);
2702 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2703 publ->upper, publ->key);
2706 if (list_empty(&tsk->publications))
2711 /* tipc_sk_reinit: set non-zero address in all existing sockets
2712 * when we go from standalone to network mode.
2714 void tipc_sk_reinit(struct net *net)
2716 struct tipc_net *tn = net_generic(net, tipc_net_id);
2717 struct rhashtable_iter iter;
2718 struct tipc_sock *tsk;
2719 struct tipc_msg *msg;
2721 rhashtable_walk_enter(&tn->sk_rht, &iter);
2724 rhashtable_walk_start(&iter);
2726 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
2727 sock_hold(&tsk->sk);
2728 rhashtable_walk_stop(&iter);
2729 lock_sock(&tsk->sk);
2731 msg_set_prevnode(msg, tipc_own_addr(net));
2732 msg_set_orignode(msg, tipc_own_addr(net));
2733 release_sock(&tsk->sk);
2734 rhashtable_walk_start(&iter);
2738 rhashtable_walk_stop(&iter);
2739 } while (tsk == ERR_PTR(-EAGAIN));
2741 rhashtable_walk_exit(&iter);
2744 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
2746 struct tipc_net *tn = net_generic(net, tipc_net_id);
2747 struct tipc_sock *tsk;
2750 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
2752 sock_hold(&tsk->sk);
2758 static int tipc_sk_insert(struct tipc_sock *tsk)
2760 struct sock *sk = &tsk->sk;
2761 struct net *net = sock_net(sk);
2762 struct tipc_net *tn = net_generic(net, tipc_net_id);
2763 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2764 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2766 while (remaining--) {
2768 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2769 portid = TIPC_MIN_PORT;
2770 tsk->portid = portid;
2771 sock_hold(&tsk->sk);
2772 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2781 static void tipc_sk_remove(struct tipc_sock *tsk)
2783 struct sock *sk = &tsk->sk;
2784 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
2786 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
2787 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
2792 static const struct rhashtable_params tsk_rht_params = {
2794 .head_offset = offsetof(struct tipc_sock, node),
2795 .key_offset = offsetof(struct tipc_sock, portid),
2796 .key_len = sizeof(u32), /* portid */
2797 .max_size = 1048576,
2799 .automatic_shrinking = true,
2802 int tipc_sk_rht_init(struct net *net)
2804 struct tipc_net *tn = net_generic(net, tipc_net_id);
2806 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
2809 void tipc_sk_rht_destroy(struct net *net)
2811 struct tipc_net *tn = net_generic(net, tipc_net_id);
2813 /* Wait for socket readers to complete */
2816 rhashtable_destroy(&tn->sk_rht);
2819 static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq)
2821 struct net *net = sock_net(&tsk->sk);
2822 struct tipc_group *grp = tsk->group;
2823 struct tipc_msg *hdr = &tsk->phdr;
2824 struct tipc_name_seq seq;
2827 if (mreq->type < TIPC_RESERVED_TYPES)
2829 if (mreq->scope > TIPC_NODE_SCOPE)
2833 grp = tipc_group_create(net, tsk->portid, mreq, &tsk->group_is_open);
2837 msg_set_lookup_scope(hdr, mreq->scope);
2838 msg_set_nametype(hdr, mreq->type);
2839 msg_set_dest_droppable(hdr, true);
2840 seq.type = mreq->type;
2841 seq.lower = mreq->instance;
2842 seq.upper = seq.lower;
2843 tipc_nametbl_build_group(net, grp, mreq->type, mreq->scope);
2844 rc = tipc_sk_publish(tsk, mreq->scope, &seq);
2846 tipc_group_delete(net, grp);
2850 /* Eliminate any risk that a broadcast overtakes sent JOINs */
2851 tsk->mc_method.rcast = true;
2852 tsk->mc_method.mandatory = true;
2853 tipc_group_join(net, grp, &tsk->sk.sk_rcvbuf);
2857 static int tipc_sk_leave(struct tipc_sock *tsk)
2859 struct net *net = sock_net(&tsk->sk);
2860 struct tipc_group *grp = tsk->group;
2861 struct tipc_name_seq seq;
2866 tipc_group_self(grp, &seq, &scope);
2867 tipc_group_delete(net, grp);
2869 tipc_sk_withdraw(tsk, scope, &seq);
2874 * tipc_setsockopt - set socket option
2875 * @sock: socket structure
2876 * @lvl: option level
2877 * @opt: option identifier
2878 * @ov: pointer to new option value
2879 * @ol: length of option value
2881 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
2882 * (to ease compatibility).
2884 * Returns 0 on success, errno otherwise
2886 static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2887 char __user *ov, unsigned int ol)
2889 struct sock *sk = sock->sk;
2890 struct tipc_sock *tsk = tipc_sk(sk);
2891 struct tipc_group_req mreq;
2895 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2897 if (lvl != SOL_TIPC)
2898 return -ENOPROTOOPT;
2901 case TIPC_IMPORTANCE:
2902 case TIPC_SRC_DROPPABLE:
2903 case TIPC_DEST_DROPPABLE:
2904 case TIPC_CONN_TIMEOUT:
2905 if (ol < sizeof(value))
2907 if (get_user(value, (u32 __user *)ov))
2910 case TIPC_GROUP_JOIN:
2911 if (ol < sizeof(mreq))
2913 if (copy_from_user(&mreq, ov, sizeof(mreq)))
2924 case TIPC_IMPORTANCE:
2925 res = tsk_set_importance(tsk, value);
2927 case TIPC_SRC_DROPPABLE:
2928 if (sock->type != SOCK_STREAM)
2929 tsk_set_unreliable(tsk, value);
2933 case TIPC_DEST_DROPPABLE:
2934 tsk_set_unreturnable(tsk, value);
2936 case TIPC_CONN_TIMEOUT:
2937 tipc_sk(sk)->conn_timeout = value;
2939 case TIPC_MCAST_BROADCAST:
2940 tsk->mc_method.rcast = false;
2941 tsk->mc_method.mandatory = true;
2943 case TIPC_MCAST_REPLICAST:
2944 tsk->mc_method.rcast = true;
2945 tsk->mc_method.mandatory = true;
2947 case TIPC_GROUP_JOIN:
2948 res = tipc_sk_join(tsk, &mreq);
2950 case TIPC_GROUP_LEAVE:
2951 res = tipc_sk_leave(tsk);
2963 * tipc_getsockopt - get socket option
2964 * @sock: socket structure
2965 * @lvl: option level
2966 * @opt: option identifier
2967 * @ov: receptacle for option value
2968 * @ol: receptacle for length of option value
2970 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
2971 * (to ease compatibility).
2973 * Returns 0 on success, errno otherwise
2975 static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2976 char __user *ov, int __user *ol)
2978 struct sock *sk = sock->sk;
2979 struct tipc_sock *tsk = tipc_sk(sk);
2980 struct tipc_name_seq seq;
2985 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2986 return put_user(0, ol);
2987 if (lvl != SOL_TIPC)
2988 return -ENOPROTOOPT;
2989 res = get_user(len, ol);
2996 case TIPC_IMPORTANCE:
2997 value = tsk_importance(tsk);
2999 case TIPC_SRC_DROPPABLE:
3000 value = tsk_unreliable(tsk);
3002 case TIPC_DEST_DROPPABLE:
3003 value = tsk_unreturnable(tsk);
3005 case TIPC_CONN_TIMEOUT:
3006 value = tsk->conn_timeout;
3007 /* no need to set "res", since already 0 at this point */
3009 case TIPC_NODE_RECVQ_DEPTH:
3010 value = 0; /* was tipc_queue_size, now obsolete */
3012 case TIPC_SOCK_RECVQ_DEPTH:
3013 value = skb_queue_len(&sk->sk_receive_queue);
3015 case TIPC_GROUP_JOIN:
3018 tipc_group_self(tsk->group, &seq, &scope);
3028 return res; /* "get" failed */
3030 if (len < sizeof(value))
3033 if (copy_to_user(ov, &value, sizeof(value)))
3036 return put_user(sizeof(value), ol);
3039 static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
3041 struct net *net = sock_net(sock->sk);
3042 struct tipc_sioc_nodeid_req nr = {0};
3043 struct tipc_sioc_ln_req lnr;
3044 void __user *argp = (void __user *)arg;
3047 case SIOCGETLINKNAME:
3048 if (copy_from_user(&lnr, argp, sizeof(lnr)))
3050 if (!tipc_node_get_linkname(net,
3051 lnr.bearer_id & 0xffff, lnr.peer,
3052 lnr.linkname, TIPC_MAX_LINK_NAME)) {
3053 if (copy_to_user(argp, &lnr, sizeof(lnr)))
3057 return -EADDRNOTAVAIL;
3059 if (copy_from_user(&nr, argp, sizeof(nr)))
3061 if (!tipc_node_get_id(net, nr.peer, nr.node_id))
3062 return -EADDRNOTAVAIL;
3063 if (copy_to_user(argp, &nr, sizeof(nr)))
3067 return -ENOIOCTLCMD;
3071 static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
3073 struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
3074 struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
3075 u32 onode = tipc_own_addr(sock_net(sock1->sk));
3077 tsk1->peer.family = AF_TIPC;
3078 tsk1->peer.addrtype = TIPC_ADDR_ID;
3079 tsk1->peer.scope = TIPC_NODE_SCOPE;
3080 tsk1->peer.addr.id.ref = tsk2->portid;
3081 tsk1->peer.addr.id.node = onode;
3082 tsk2->peer.family = AF_TIPC;
3083 tsk2->peer.addrtype = TIPC_ADDR_ID;
3084 tsk2->peer.scope = TIPC_NODE_SCOPE;
3085 tsk2->peer.addr.id.ref = tsk1->portid;
3086 tsk2->peer.addr.id.node = onode;
3088 tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
3089 tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
3093 /* Protocol switches for the various types of TIPC sockets */
3095 static const struct proto_ops msg_ops = {
3096 .owner = THIS_MODULE,
3098 .release = tipc_release,
3100 .connect = tipc_connect,
3101 .socketpair = tipc_socketpair,
3102 .accept = sock_no_accept,
3103 .getname = tipc_getname,
3105 .ioctl = tipc_ioctl,
3106 .listen = sock_no_listen,
3107 .shutdown = tipc_shutdown,
3108 .setsockopt = tipc_setsockopt,
3109 .getsockopt = tipc_getsockopt,
3110 .sendmsg = tipc_sendmsg,
3111 .recvmsg = tipc_recvmsg,
3112 .mmap = sock_no_mmap,
3113 .sendpage = sock_no_sendpage
3116 static const struct proto_ops packet_ops = {
3117 .owner = THIS_MODULE,
3119 .release = tipc_release,
3121 .connect = tipc_connect,
3122 .socketpair = tipc_socketpair,
3123 .accept = tipc_accept,
3124 .getname = tipc_getname,
3126 .ioctl = tipc_ioctl,
3127 .listen = tipc_listen,
3128 .shutdown = tipc_shutdown,
3129 .setsockopt = tipc_setsockopt,
3130 .getsockopt = tipc_getsockopt,
3131 .sendmsg = tipc_send_packet,
3132 .recvmsg = tipc_recvmsg,
3133 .mmap = sock_no_mmap,
3134 .sendpage = sock_no_sendpage
3137 static const struct proto_ops stream_ops = {
3138 .owner = THIS_MODULE,
3140 .release = tipc_release,
3142 .connect = tipc_connect,
3143 .socketpair = tipc_socketpair,
3144 .accept = tipc_accept,
3145 .getname = tipc_getname,
3147 .ioctl = tipc_ioctl,
3148 .listen = tipc_listen,
3149 .shutdown = tipc_shutdown,
3150 .setsockopt = tipc_setsockopt,
3151 .getsockopt = tipc_getsockopt,
3152 .sendmsg = tipc_sendstream,
3153 .recvmsg = tipc_recvstream,
3154 .mmap = sock_no_mmap,
3155 .sendpage = sock_no_sendpage
3158 static const struct net_proto_family tipc_family_ops = {
3159 .owner = THIS_MODULE,
3161 .create = tipc_sk_create
3164 static struct proto tipc_proto = {
3166 .owner = THIS_MODULE,
3167 .obj_size = sizeof(struct tipc_sock),
3168 .sysctl_rmem = sysctl_tipc_rmem
3172 * tipc_socket_init - initialize TIPC socket interface
3174 * Returns 0 on success, errno otherwise
3176 int tipc_socket_init(void)
3180 res = proto_register(&tipc_proto, 1);
3182 pr_err("Failed to register TIPC protocol type\n");
3186 res = sock_register(&tipc_family_ops);
3188 pr_err("Failed to register TIPC socket type\n");
3189 proto_unregister(&tipc_proto);
3197 * tipc_socket_stop - stop TIPC socket interface
3199 void tipc_socket_stop(void)
3201 sock_unregister(tipc_family_ops.family);
3202 proto_unregister(&tipc_proto);
3205 /* Caller should hold socket lock for the passed tipc socket. */
3206 static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
3210 struct nlattr *nest;
3212 peer_node = tsk_peer_node(tsk);
3213 peer_port = tsk_peer_port(tsk);
3215 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
3217 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
3219 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
3222 if (tsk->conn_type != 0) {
3223 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
3225 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
3227 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
3230 nla_nest_end(skb, nest);
3235 nla_nest_cancel(skb, nest);
3240 static int __tipc_nl_add_sk_info(struct sk_buff *skb, struct tipc_sock
3243 struct net *net = sock_net(skb->sk);
3244 struct sock *sk = &tsk->sk;
3246 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid) ||
3247 nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr(net)))
3250 if (tipc_sk_connected(sk)) {
3251 if (__tipc_nl_add_sk_con(skb, tsk))
3253 } else if (!list_empty(&tsk->publications)) {
3254 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
3260 /* Caller should hold socket lock for the passed tipc socket. */
3261 static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
3262 struct tipc_sock *tsk)
3264 struct nlattr *attrs;
3267 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3268 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
3272 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
3274 goto genlmsg_cancel;
3276 if (__tipc_nl_add_sk_info(skb, tsk))
3277 goto attr_msg_cancel;
3279 nla_nest_end(skb, attrs);
3280 genlmsg_end(skb, hdr);
3285 nla_nest_cancel(skb, attrs);
3287 genlmsg_cancel(skb, hdr);
3292 int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,
3293 int (*skb_handler)(struct sk_buff *skb,
3294 struct netlink_callback *cb,
3295 struct tipc_sock *tsk))
3297 struct rhashtable_iter *iter = (void *)cb->args[4];
3298 struct tipc_sock *tsk;
3301 rhashtable_walk_start(iter);
3302 while ((tsk = rhashtable_walk_next(iter)) != NULL) {
3305 if (err == -EAGAIN) {
3312 sock_hold(&tsk->sk);
3313 rhashtable_walk_stop(iter);
3314 lock_sock(&tsk->sk);
3315 err = skb_handler(skb, cb, tsk);
3317 release_sock(&tsk->sk);
3321 release_sock(&tsk->sk);
3322 rhashtable_walk_start(iter);
3325 rhashtable_walk_stop(iter);
3329 EXPORT_SYMBOL(tipc_nl_sk_walk);
3331 int tipc_dump_start(struct netlink_callback *cb)
3333 return __tipc_dump_start(cb, sock_net(cb->skb->sk));
3335 EXPORT_SYMBOL(tipc_dump_start);
3337 int __tipc_dump_start(struct netlink_callback *cb, struct net *net)
3339 /* tipc_nl_name_table_dump() uses cb->args[0...3]. */
3340 struct rhashtable_iter *iter = (void *)cb->args[4];
3341 struct tipc_net *tn = tipc_net(net);
3344 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3348 cb->args[4] = (long)iter;
3351 rhashtable_walk_enter(&tn->sk_rht, iter);
3355 int tipc_dump_done(struct netlink_callback *cb)
3357 struct rhashtable_iter *hti = (void *)cb->args[4];
3359 rhashtable_walk_exit(hti);
3363 EXPORT_SYMBOL(tipc_dump_done);
3365 int tipc_sk_fill_sock_diag(struct sk_buff *skb, struct netlink_callback *cb,
3366 struct tipc_sock *tsk, u32 sk_filter_state,
3367 u64 (*tipc_diag_gen_cookie)(struct sock *sk))
3369 struct sock *sk = &tsk->sk;
3370 struct nlattr *attrs;
3371 struct nlattr *stat;
3373 /*filter response w.r.t sk_state*/
3374 if (!(sk_filter_state & (1 << sk->sk_state)))
3377 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
3381 if (__tipc_nl_add_sk_info(skb, tsk))
3382 goto attr_msg_cancel;
3384 if (nla_put_u32(skb, TIPC_NLA_SOCK_TYPE, (u32)sk->sk_type) ||
3385 nla_put_u32(skb, TIPC_NLA_SOCK_TIPC_STATE, (u32)sk->sk_state) ||
3386 nla_put_u32(skb, TIPC_NLA_SOCK_INO, sock_i_ino(sk)) ||
3387 nla_put_u32(skb, TIPC_NLA_SOCK_UID,
3388 from_kuid_munged(sk_user_ns(NETLINK_CB(cb->skb).sk),
3390 nla_put_u64_64bit(skb, TIPC_NLA_SOCK_COOKIE,
3391 tipc_diag_gen_cookie(sk),
3393 goto attr_msg_cancel;
3395 stat = nla_nest_start(skb, TIPC_NLA_SOCK_STAT);
3397 goto attr_msg_cancel;
3399 if (nla_put_u32(skb, TIPC_NLA_SOCK_STAT_RCVQ,
3400 skb_queue_len(&sk->sk_receive_queue)) ||
3401 nla_put_u32(skb, TIPC_NLA_SOCK_STAT_SENDQ,
3402 skb_queue_len(&sk->sk_write_queue)) ||
3403 nla_put_u32(skb, TIPC_NLA_SOCK_STAT_DROP,
3404 atomic_read(&sk->sk_drops)))
3405 goto stat_msg_cancel;
3407 if (tsk->cong_link_cnt &&
3408 nla_put_flag(skb, TIPC_NLA_SOCK_STAT_LINK_CONG))
3409 goto stat_msg_cancel;
3411 if (tsk_conn_cong(tsk) &&
3412 nla_put_flag(skb, TIPC_NLA_SOCK_STAT_CONN_CONG))
3413 goto stat_msg_cancel;
3415 nla_nest_end(skb, stat);
3418 if (tipc_group_fill_sock_diag(tsk->group, skb))
3419 goto stat_msg_cancel;
3421 nla_nest_end(skb, attrs);
3426 nla_nest_cancel(skb, stat);
3428 nla_nest_cancel(skb, attrs);
3432 EXPORT_SYMBOL(tipc_sk_fill_sock_diag);
3434 int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
3436 return tipc_nl_sk_walk(skb, cb, __tipc_nl_add_sk);
3439 /* Caller should hold socket lock for the passed tipc socket. */
3440 static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
3441 struct netlink_callback *cb,
3442 struct publication *publ)
3445 struct nlattr *attrs;
3447 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3448 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
3452 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
3454 goto genlmsg_cancel;
3456 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
3457 goto attr_msg_cancel;
3458 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
3459 goto attr_msg_cancel;
3460 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
3461 goto attr_msg_cancel;
3462 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
3463 goto attr_msg_cancel;
3465 nla_nest_end(skb, attrs);
3466 genlmsg_end(skb, hdr);
3471 nla_nest_cancel(skb, attrs);
3473 genlmsg_cancel(skb, hdr);
3478 /* Caller should hold socket lock for the passed tipc socket. */
3479 static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
3480 struct netlink_callback *cb,
3481 struct tipc_sock *tsk, u32 *last_publ)
3484 struct publication *p;
3487 list_for_each_entry(p, &tsk->publications, binding_sock) {
3488 if (p->key == *last_publ)
3491 if (list_entry_is_head(p, &tsk->publications, binding_sock)) {
3492 /* We never set seq or call nl_dump_check_consistent()
3493 * this means that setting prev_seq here will cause the
3494 * consistence check to fail in the netlink callback
3495 * handler. Resulting in the last NLMSG_DONE message
3496 * having the NLM_F_DUMP_INTR flag set.
3503 p = list_first_entry(&tsk->publications, struct publication,
3507 list_for_each_entry_from(p, &tsk->publications, binding_sock) {
3508 err = __tipc_nl_add_sk_publ(skb, cb, p);
3510 *last_publ = p->key;
3519 int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
3522 u32 tsk_portid = cb->args[0];
3523 u32 last_publ = cb->args[1];
3524 u32 done = cb->args[2];
3525 struct net *net = sock_net(skb->sk);
3526 struct tipc_sock *tsk;
3529 struct nlattr **attrs;
3530 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
3532 err = tipc_nlmsg_parse(cb->nlh, &attrs);
3536 if (!attrs[TIPC_NLA_SOCK])
3539 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
3540 attrs[TIPC_NLA_SOCK],
3541 tipc_nl_sock_policy, NULL);
3545 if (!sock[TIPC_NLA_SOCK_REF])
3548 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
3554 tsk = tipc_sk_lookup(net, tsk_portid);
3558 lock_sock(&tsk->sk);
3559 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
3562 release_sock(&tsk->sk);
3565 cb->args[0] = tsk_portid;
3566 cb->args[1] = last_publ;