2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2001-2007, 2012-2016, 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"
49 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
50 #define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
51 #define TIPC_FWD_MSG 1
52 #define TIPC_MAX_PORT 0xffffffff
53 #define TIPC_MIN_PORT 1
56 TIPC_LISTEN = TCP_LISTEN,
57 TIPC_ESTABLISHED = TCP_ESTABLISHED,
58 TIPC_OPEN = TCP_CLOSE,
59 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
60 TIPC_CONNECTING = TCP_SYN_SENT,
64 * struct tipc_sock - TIPC socket structure
65 * @sk: socket - interacts with 'port' and with user via the socket API
66 * @conn_type: TIPC type used when connection was established
67 * @conn_instance: TIPC instance used when connection was established
68 * @published: non-zero if port has one or more associated names
69 * @max_pkt: maximum packet size "hint" used when building messages sent by port
70 * @portid: unique port identity in TIPC socket hash table
71 * @phdr: preformatted message header used when sending messages
72 * #cong_links: list of congested links
73 * @publications: list of publications for port
74 * @blocking_link: address of the congested link we are currently sleeping on
75 * @pub_count: total # of publications port has made during its lifetime
77 * @conn_timeout: the time we can wait for an unresponded setup request
78 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
79 * @cong_link_cnt: number of congested links
80 * @sent_unacked: # messages sent by socket, and not yet acked by peer
81 * @rcv_unacked: # messages read by user, but not yet acked back to peer
82 * @peer: 'connected' peer for dgram/rdm
83 * @node: hash table node
84 * @mc_method: cookie for use between socket and broadcast layer
85 * @rcu: rcu struct for tipc_sock
95 struct list_head cong_links;
96 struct list_head publications;
107 struct sockaddr_tipc peer;
108 struct rhash_head node;
109 struct tipc_mc_method mc_method;
113 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
114 static void tipc_data_ready(struct sock *sk);
115 static void tipc_write_space(struct sock *sk);
116 static void tipc_sock_destruct(struct sock *sk);
117 static int tipc_release(struct socket *sock);
118 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
120 static void tipc_sk_timeout(unsigned long data);
121 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
122 struct tipc_name_seq const *seq);
123 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
124 struct tipc_name_seq const *seq);
125 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
126 static int tipc_sk_insert(struct tipc_sock *tsk);
127 static void tipc_sk_remove(struct tipc_sock *tsk);
128 static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
129 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
131 static const struct proto_ops packet_ops;
132 static const struct proto_ops stream_ops;
133 static const struct proto_ops msg_ops;
134 static struct proto tipc_proto;
135 static const struct rhashtable_params tsk_rht_params;
137 static u32 tsk_own_node(struct tipc_sock *tsk)
139 return msg_prevnode(&tsk->phdr);
142 static u32 tsk_peer_node(struct tipc_sock *tsk)
144 return msg_destnode(&tsk->phdr);
147 static u32 tsk_peer_port(struct tipc_sock *tsk)
149 return msg_destport(&tsk->phdr);
152 static bool tsk_unreliable(struct tipc_sock *tsk)
154 return msg_src_droppable(&tsk->phdr) != 0;
157 static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
159 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
162 static bool tsk_unreturnable(struct tipc_sock *tsk)
164 return msg_dest_droppable(&tsk->phdr) != 0;
167 static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
169 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
172 static int tsk_importance(struct tipc_sock *tsk)
174 return msg_importance(&tsk->phdr);
177 static int tsk_set_importance(struct tipc_sock *tsk, int imp)
179 if (imp > TIPC_CRITICAL_IMPORTANCE)
181 msg_set_importance(&tsk->phdr, (u32)imp);
185 static struct tipc_sock *tipc_sk(const struct sock *sk)
187 return container_of(sk, struct tipc_sock, sk);
190 static bool tsk_conn_cong(struct tipc_sock *tsk)
192 return tsk->snt_unacked > tsk->snd_win;
195 /* tsk_blocks(): translate a buffer size in bytes to number of
196 * advertisable blocks, taking into account the ratio truesize(len)/len
197 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
199 static u16 tsk_adv_blocks(int len)
201 return len / FLOWCTL_BLK_SZ / 4;
204 /* tsk_inc(): increment counter for sent or received data
205 * - If block based flow control is not supported by peer we
206 * fall back to message based ditto, incrementing the counter
208 static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
210 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
211 return ((msglen / FLOWCTL_BLK_SZ) + 1);
216 * tsk_advance_rx_queue - discard first buffer in socket receive queue
218 * Caller must hold socket lock
220 static void tsk_advance_rx_queue(struct sock *sk)
222 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
225 /* tipc_sk_respond() : send response message back to sender
227 static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
231 u32 onode = tipc_own_addr(sock_net(sk));
233 if (!tipc_msg_reverse(onode, &skb, err))
236 dnode = msg_destnode(buf_msg(skb));
237 selector = msg_origport(buf_msg(skb));
238 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
242 * tsk_rej_rx_queue - reject all buffers in socket receive queue
244 * Caller must hold socket lock
246 static void tsk_rej_rx_queue(struct sock *sk)
250 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
251 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
254 static bool tipc_sk_connected(struct sock *sk)
256 return sk->sk_state == TIPC_ESTABLISHED;
259 /* tipc_sk_type_connectionless - check if the socket is datagram socket
262 * Returns true if connection less, false otherwise
264 static bool tipc_sk_type_connectionless(struct sock *sk)
266 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
269 /* tsk_peer_msg - verify if message was sent by connected port's peer
271 * Handles cases where the node's network address has changed from
272 * the default of <0.0.0> to its configured setting.
274 static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
276 struct sock *sk = &tsk->sk;
277 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
278 u32 peer_port = tsk_peer_port(tsk);
282 if (unlikely(!tipc_sk_connected(sk)))
285 if (unlikely(msg_origport(msg) != peer_port))
288 orig_node = msg_orignode(msg);
289 peer_node = tsk_peer_node(tsk);
291 if (likely(orig_node == peer_node))
294 if (!orig_node && (peer_node == tn->own_addr))
297 if (!peer_node && (orig_node == tn->own_addr))
303 /* tipc_set_sk_state - set the sk_state of the socket
306 * Caller must hold socket lock
308 * Returns 0 on success, errno otherwise
310 static int tipc_set_sk_state(struct sock *sk, int state)
312 int oldsk_state = sk->sk_state;
320 case TIPC_CONNECTING:
321 if (oldsk_state == TIPC_OPEN)
324 case TIPC_ESTABLISHED:
325 if (oldsk_state == TIPC_CONNECTING ||
326 oldsk_state == TIPC_OPEN)
329 case TIPC_DISCONNECTING:
330 if (oldsk_state == TIPC_CONNECTING ||
331 oldsk_state == TIPC_ESTABLISHED)
337 sk->sk_state = state;
342 static int tipc_sk_sock_err(struct socket *sock, long *timeout)
344 struct sock *sk = sock->sk;
345 int err = sock_error(sk);
346 int typ = sock->type;
350 if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
351 if (sk->sk_state == TIPC_DISCONNECTING)
353 else if (!tipc_sk_connected(sk))
358 if (signal_pending(current))
359 return sock_intr_errno(*timeout);
364 #define tipc_wait_for_cond(sock_, timeout_, condition_) \
369 while (!(condition_) && !done_) { \
370 struct sock *sk_ = sock->sk; \
371 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
373 rc_ = tipc_sk_sock_err(sock_, timeout_); \
376 prepare_to_wait(sk_sleep(sk_), &wait_, \
377 TASK_INTERRUPTIBLE); \
378 done_ = sk_wait_event(sk_, timeout_, \
379 (condition_), &wait_); \
380 remove_wait_queue(sk_sleep(sk_), &wait_); \
386 * tipc_sk_create - create a TIPC socket
387 * @net: network namespace (must be default network)
388 * @sock: pre-allocated socket structure
389 * @protocol: protocol indicator (must be 0)
390 * @kern: caused by kernel or by userspace?
392 * This routine creates additional data structures used by the TIPC socket,
393 * initializes them, and links them together.
395 * Returns 0 on success, errno otherwise
397 static int tipc_sk_create(struct net *net, struct socket *sock,
398 int protocol, int kern)
401 const struct proto_ops *ops;
403 struct tipc_sock *tsk;
404 struct tipc_msg *msg;
406 /* Validate arguments */
407 if (unlikely(protocol != 0))
408 return -EPROTONOSUPPORT;
410 switch (sock->type) {
425 /* Allocate socket's protocol area */
426 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
431 tsk->max_pkt = MAX_PKT_DEFAULT;
432 INIT_LIST_HEAD(&tsk->publications);
433 INIT_LIST_HEAD(&tsk->cong_links);
435 tn = net_generic(sock_net(sk), tipc_net_id);
437 /* Finish initializing socket data structures */
439 sock_init_data(sock, sk);
440 tipc_set_sk_state(sk, TIPC_OPEN);
441 if (tipc_sk_insert(tsk)) {
442 pr_warn("Socket create failed; port number exhausted\n");
446 /* Ensure tsk is visible before we read own_addr. */
449 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
452 msg_set_origport(msg, tsk->portid);
453 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
455 sk->sk_backlog_rcv = tipc_backlog_rcv;
456 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
457 sk->sk_data_ready = tipc_data_ready;
458 sk->sk_write_space = tipc_write_space;
459 sk->sk_destruct = tipc_sock_destruct;
460 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
461 atomic_set(&tsk->dupl_rcvcnt, 0);
463 /* Start out with safe limits until we receive an advertised window */
464 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
465 tsk->rcv_win = tsk->snd_win;
467 if (tipc_sk_type_connectionless(sk)) {
468 tsk_set_unreturnable(tsk, true);
469 if (sock->type == SOCK_DGRAM)
470 tsk_set_unreliable(tsk, true);
476 static void tipc_sk_callback(struct rcu_head *head)
478 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
483 /* Caller should hold socket lock for the socket. */
484 static void __tipc_shutdown(struct socket *sock, int error)
486 struct sock *sk = sock->sk;
487 struct tipc_sock *tsk = tipc_sk(sk);
488 struct net *net = sock_net(sk);
489 long timeout = CONN_TIMEOUT_DEFAULT;
490 u32 dnode = tsk_peer_node(tsk);
493 /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
494 tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
495 !tsk_conn_cong(tsk)));
497 /* Reject all unreceived messages, except on an active connection
498 * (which disconnects locally & sends a 'FIN+' to peer).
500 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
501 if (TIPC_SKB_CB(skb)->bytes_read) {
505 if (!tipc_sk_type_connectionless(sk) &&
506 sk->sk_state != TIPC_DISCONNECTING) {
507 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
508 tipc_node_remove_conn(net, dnode, tsk->portid);
510 tipc_sk_respond(sk, skb, error);
513 if (tipc_sk_type_connectionless(sk))
516 if (sk->sk_state != TIPC_DISCONNECTING) {
517 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
518 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
519 tsk_own_node(tsk), tsk_peer_port(tsk),
522 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
523 tipc_node_remove_conn(net, dnode, tsk->portid);
524 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
529 * tipc_release - destroy a TIPC socket
530 * @sock: socket to destroy
532 * This routine cleans up any messages that are still queued on the socket.
533 * For DGRAM and RDM socket types, all queued messages are rejected.
534 * For SEQPACKET and STREAM socket types, the first message is rejected
535 * and any others are discarded. (If the first message on a STREAM socket
536 * is partially-read, it is discarded and the next one is rejected instead.)
538 * NOTE: Rejected messages are not necessarily returned to the sender! They
539 * are returned or discarded according to the "destination droppable" setting
540 * specified for the message by the sender.
542 * Returns 0 on success, errno otherwise
544 static int tipc_release(struct socket *sock)
546 struct sock *sk = sock->sk;
547 struct tipc_sock *tsk;
550 * Exit if socket isn't fully initialized (occurs when a failed accept()
551 * releases a pre-allocated child socket that was never used)
559 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
560 sk->sk_shutdown = SHUTDOWN_MASK;
561 tipc_sk_withdraw(tsk, 0, NULL);
562 sk_stop_timer(sk, &sk->sk_timer);
565 /* Reject any messages that accumulated in backlog queue */
567 u32_list_purge(&tsk->cong_links);
568 tsk->cong_link_cnt = 0;
569 call_rcu(&tsk->rcu, tipc_sk_callback);
576 * tipc_bind - associate or disassocate TIPC name(s) with a socket
577 * @sock: socket structure
578 * @uaddr: socket address describing name(s) and desired operation
579 * @uaddr_len: size of socket address data structure
581 * Name and name sequence binding is indicated using a positive scope value;
582 * a negative scope value unbinds the specified name. Specifying no name
583 * (i.e. a socket address length of 0) unbinds all names from the socket.
585 * Returns 0 on success, errno otherwise
587 * NOTE: This routine doesn't need to take the socket lock since it doesn't
588 * access any non-constant socket information.
590 static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
593 struct sock *sk = sock->sk;
594 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
595 struct tipc_sock *tsk = tipc_sk(sk);
599 if (unlikely(!uaddr_len)) {
600 res = tipc_sk_withdraw(tsk, 0, NULL);
604 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
608 if (addr->family != AF_TIPC) {
613 if (addr->addrtype == TIPC_ADDR_NAME)
614 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
615 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
620 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
621 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
622 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
627 res = (addr->scope > 0) ?
628 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
629 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
636 * tipc_getname - get port ID of socket or peer socket
637 * @sock: socket structure
638 * @uaddr: area for returned socket address
639 * @uaddr_len: area for returned length of socket address
640 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
642 * Returns 0 on success, errno otherwise
644 * NOTE: This routine doesn't need to take the socket lock since it only
645 * accesses socket information that is unchanging (or which changes in
646 * a completely predictable manner).
648 static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
649 int *uaddr_len, int peer)
651 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
652 struct sock *sk = sock->sk;
653 struct tipc_sock *tsk = tipc_sk(sk);
654 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
656 memset(addr, 0, sizeof(*addr));
658 if ((!tipc_sk_connected(sk)) &&
659 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
661 addr->addr.id.ref = tsk_peer_port(tsk);
662 addr->addr.id.node = tsk_peer_node(tsk);
664 addr->addr.id.ref = tsk->portid;
665 addr->addr.id.node = tn->own_addr;
668 *uaddr_len = sizeof(*addr);
669 addr->addrtype = TIPC_ADDR_ID;
670 addr->family = AF_TIPC;
672 addr->addr.name.domain = 0;
678 * tipc_poll - read and possibly block on pollmask
679 * @file: file structure associated with the socket
680 * @sock: socket for which to calculate the poll bits
683 * Returns pollmask value
686 * It appears that the usual socket locking mechanisms are not useful here
687 * since the pollmask info is potentially out-of-date the moment this routine
688 * exits. TCP and other protocols seem to rely on higher level poll routines
689 * to handle any preventable race conditions, so TIPC will do the same ...
691 * IMPORTANT: The fact that a read or write operation is indicated does NOT
692 * imply that the operation will succeed, merely that it should be performed
693 * and will not block.
695 static unsigned int tipc_poll(struct file *file, struct socket *sock,
698 struct sock *sk = sock->sk;
699 struct tipc_sock *tsk = tipc_sk(sk);
702 sock_poll_wait(file, sk_sleep(sk), wait);
704 if (sk->sk_shutdown & RCV_SHUTDOWN)
705 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
706 if (sk->sk_shutdown == SHUTDOWN_MASK)
709 switch (sk->sk_state) {
710 case TIPC_ESTABLISHED:
711 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
715 case TIPC_CONNECTING:
716 if (!skb_queue_empty(&sk->sk_receive_queue))
717 mask |= (POLLIN | POLLRDNORM);
720 if (!tsk->cong_link_cnt)
722 if (tipc_sk_type_connectionless(sk) &&
723 (!skb_queue_empty(&sk->sk_receive_queue)))
724 mask |= (POLLIN | POLLRDNORM);
726 case TIPC_DISCONNECTING:
727 mask = (POLLIN | POLLRDNORM | POLLHUP);
735 * tipc_sendmcast - send multicast message
736 * @sock: socket structure
737 * @seq: destination address
738 * @msg: message to send
739 * @dlen: length of data to send
740 * @timeout: timeout to wait for wakeup
742 * Called from function tipc_sendmsg(), which has done all sanity checks
743 * Returns the number of bytes sent on success, or errno
745 static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
746 struct msghdr *msg, size_t dlen, long timeout)
748 struct sock *sk = sock->sk;
749 struct tipc_sock *tsk = tipc_sk(sk);
750 struct tipc_msg *hdr = &tsk->phdr;
751 struct net *net = sock_net(sk);
752 int mtu = tipc_bcast_get_mtu(net);
753 struct tipc_mc_method *method = &tsk->mc_method;
754 u32 domain = addr_domain(net, TIPC_CLUSTER_SCOPE);
755 struct sk_buff_head pkts;
756 struct tipc_nlist dsts;
759 /* Block or return if any destination link is congested */
760 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
764 /* Lookup destination nodes */
765 tipc_nlist_init(&dsts, tipc_own_addr(net));
766 tipc_nametbl_lookup_dst_nodes(net, seq->type, seq->lower,
767 seq->upper, domain, &dsts);
768 if (!dsts.local && !dsts.remote)
769 return -EHOSTUNREACH;
771 /* Build message header */
772 msg_set_type(hdr, TIPC_MCAST_MSG);
773 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
774 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
775 msg_set_destport(hdr, 0);
776 msg_set_destnode(hdr, 0);
777 msg_set_nametype(hdr, seq->type);
778 msg_set_namelower(hdr, seq->lower);
779 msg_set_nameupper(hdr, seq->upper);
781 /* Build message as chain of buffers */
782 skb_queue_head_init(&pkts);
783 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
785 /* Send message if build was successful */
786 if (unlikely(rc == dlen))
787 rc = tipc_mcast_xmit(net, &pkts, method, &dsts,
788 &tsk->cong_link_cnt);
790 tipc_nlist_purge(&dsts);
792 return rc ? rc : dlen;
796 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
797 * @arrvq: queue with arriving messages, to be cloned after destination lookup
798 * @inputq: queue with cloned messages, delivered to socket after dest lookup
800 * Multi-threaded: parallel calls with reference to same queues may occur
802 void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
803 struct sk_buff_head *inputq)
805 struct tipc_msg *msg;
806 struct list_head dports;
808 u32 scope = TIPC_CLUSTER_SCOPE;
809 struct sk_buff_head tmpq;
811 struct sk_buff *skb, *_skb;
813 __skb_queue_head_init(&tmpq);
814 INIT_LIST_HEAD(&dports);
816 skb = tipc_skb_peek(arrvq, &inputq->lock);
817 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
819 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
821 if (in_own_node(net, msg_orignode(msg)))
822 scope = TIPC_NODE_SCOPE;
824 /* Create destination port list and message clones: */
825 tipc_nametbl_mc_translate(net,
826 msg_nametype(msg), msg_namelower(msg),
827 msg_nameupper(msg), scope, &dports);
828 portid = u32_pop(&dports);
829 for (; portid; portid = u32_pop(&dports)) {
830 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
832 msg_set_destport(buf_msg(_skb), portid);
833 __skb_queue_tail(&tmpq, _skb);
836 pr_warn("Failed to clone mcast rcv buffer\n");
838 /* Append to inputq if not already done by other thread */
839 spin_lock_bh(&inputq->lock);
840 if (skb_peek(arrvq) == skb) {
841 skb_queue_splice_tail_init(&tmpq, inputq);
842 kfree_skb(__skb_dequeue(arrvq));
844 spin_unlock_bh(&inputq->lock);
845 __skb_queue_purge(&tmpq);
848 tipc_sk_rcv(net, inputq);
852 * tipc_sk_proto_rcv - receive a connection mng protocol message
853 * @tsk: receiving socket
854 * @skb: pointer to message buffer.
856 static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
857 struct sk_buff_head *xmitq)
859 struct sock *sk = &tsk->sk;
860 u32 onode = tsk_own_node(tsk);
861 struct tipc_msg *hdr = buf_msg(skb);
862 int mtyp = msg_type(hdr);
865 /* Ignore if connection cannot be validated: */
866 if (!tsk_peer_msg(tsk, hdr))
869 tsk->probe_unacked = false;
871 if (mtyp == CONN_PROBE) {
872 msg_set_type(hdr, CONN_PROBE_REPLY);
873 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
874 __skb_queue_tail(xmitq, skb);
876 } else if (mtyp == CONN_ACK) {
877 conn_cong = tsk_conn_cong(tsk);
878 tsk->snt_unacked -= msg_conn_ack(hdr);
879 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
880 tsk->snd_win = msg_adv_win(hdr);
882 sk->sk_write_space(sk);
883 } else if (mtyp != CONN_PROBE_REPLY) {
884 pr_warn("Received unknown CONN_PROTO msg\n");
891 * tipc_sendmsg - send message in connectionless manner
892 * @sock: socket structure
893 * @m: message to send
894 * @dsz: amount of user data to be sent
896 * Message must have an destination specified explicitly.
897 * Used for SOCK_RDM and SOCK_DGRAM messages,
898 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
899 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
901 * Returns the number of bytes sent on success, or errno otherwise
903 static int tipc_sendmsg(struct socket *sock,
904 struct msghdr *m, size_t dsz)
906 struct sock *sk = sock->sk;
910 ret = __tipc_sendmsg(sock, m, dsz);
916 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
918 struct sock *sk = sock->sk;
919 struct net *net = sock_net(sk);
920 struct tipc_sock *tsk = tipc_sk(sk);
921 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
922 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
923 struct list_head *clinks = &tsk->cong_links;
924 bool syn = !tipc_sk_type_connectionless(sk);
925 struct tipc_msg *hdr = &tsk->phdr;
926 struct tipc_name_seq *seq;
927 struct sk_buff_head pkts;
928 u32 type, inst, domain;
932 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
935 if (unlikely(!dest)) {
937 if (!syn || dest->family != AF_TIPC)
938 return -EDESTADDRREQ;
941 if (unlikely(m->msg_namelen < sizeof(*dest)))
944 if (unlikely(dest->family != AF_TIPC))
948 if (sk->sk_state == TIPC_LISTEN)
950 if (sk->sk_state != TIPC_OPEN)
954 if (dest->addrtype == TIPC_ADDR_NAME) {
955 tsk->conn_type = dest->addr.name.name.type;
956 tsk->conn_instance = dest->addr.name.name.instance;
960 seq = &dest->addr.nameseq;
961 if (dest->addrtype == TIPC_ADDR_MCAST)
962 return tipc_sendmcast(sock, seq, m, dlen, timeout);
964 if (dest->addrtype == TIPC_ADDR_NAME) {
965 type = dest->addr.name.name.type;
966 inst = dest->addr.name.name.instance;
967 domain = dest->addr.name.domain;
969 msg_set_type(hdr, TIPC_NAMED_MSG);
970 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
971 msg_set_nametype(hdr, type);
972 msg_set_nameinst(hdr, inst);
973 msg_set_lookup_scope(hdr, tipc_addr_scope(domain));
974 dport = tipc_nametbl_translate(net, type, inst, &dnode);
975 msg_set_destnode(hdr, dnode);
976 msg_set_destport(hdr, dport);
977 if (unlikely(!dport && !dnode))
978 return -EHOSTUNREACH;
980 } else if (dest->addrtype == TIPC_ADDR_ID) {
981 dnode = dest->addr.id.node;
982 msg_set_type(hdr, TIPC_DIRECT_MSG);
983 msg_set_lookup_scope(hdr, 0);
984 msg_set_destnode(hdr, dnode);
985 msg_set_destport(hdr, dest->addr.id.ref);
986 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
989 /* Block or return if destination link is congested */
990 rc = tipc_wait_for_cond(sock, &timeout, !u32_find(clinks, dnode));
994 skb_queue_head_init(&pkts);
995 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
996 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
997 if (unlikely(rc != dlen))
1000 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1001 if (unlikely(rc == -ELINKCONG)) {
1002 u32_push(clinks, dnode);
1003 tsk->cong_link_cnt++;
1007 if (unlikely(syn && !rc))
1008 tipc_set_sk_state(sk, TIPC_CONNECTING);
1010 return rc ? rc : dlen;
1014 * tipc_sendstream - send stream-oriented data
1015 * @sock: socket structure
1017 * @dsz: total length of data to be transmitted
1019 * Used for SOCK_STREAM data.
1021 * Returns the number of bytes sent on success (or partial success),
1022 * or errno if no data sent
1024 static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
1026 struct sock *sk = sock->sk;
1030 ret = __tipc_sendstream(sock, m, dsz);
1036 static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
1038 struct sock *sk = sock->sk;
1039 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1040 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1041 struct tipc_sock *tsk = tipc_sk(sk);
1042 struct tipc_msg *hdr = &tsk->phdr;
1043 struct net *net = sock_net(sk);
1044 struct sk_buff_head pkts;
1045 u32 dnode = tsk_peer_node(tsk);
1049 skb_queue_head_init(&pkts);
1051 if (unlikely(dlen > INT_MAX))
1054 /* Handle implicit connection setup */
1055 if (unlikely(dest)) {
1056 rc = __tipc_sendmsg(sock, m, dlen);
1057 if (dlen && (dlen == rc))
1058 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
1063 rc = tipc_wait_for_cond(sock, &timeout,
1064 (!tsk->cong_link_cnt &&
1065 !tsk_conn_cong(tsk) &&
1066 tipc_sk_connected(sk)));
1070 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
1071 rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
1072 if (unlikely(rc != send))
1075 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1076 if (unlikely(rc == -ELINKCONG)) {
1077 tsk->cong_link_cnt = 1;
1081 tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
1084 } while (sent < dlen && !rc);
1086 return rc ? rc : sent;
1090 * tipc_send_packet - send a connection-oriented message
1091 * @sock: socket structure
1092 * @m: message to send
1093 * @dsz: length of data to be transmitted
1095 * Used for SOCK_SEQPACKET messages.
1097 * Returns the number of bytes sent on success, or errno otherwise
1099 static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
1101 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1104 return tipc_sendstream(sock, m, dsz);
1107 /* tipc_sk_finish_conn - complete the setup of a connection
1109 static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
1112 struct sock *sk = &tsk->sk;
1113 struct net *net = sock_net(sk);
1114 struct tipc_msg *msg = &tsk->phdr;
1116 msg_set_destnode(msg, peer_node);
1117 msg_set_destport(msg, peer_port);
1118 msg_set_type(msg, TIPC_CONN_MSG);
1119 msg_set_lookup_scope(msg, 0);
1120 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1122 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
1123 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
1124 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1125 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
1126 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
1127 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1130 /* Fall back to message based flow control */
1131 tsk->rcv_win = FLOWCTL_MSG_WIN;
1132 tsk->snd_win = FLOWCTL_MSG_WIN;
1136 * set_orig_addr - capture sender's address for received message
1137 * @m: descriptor for message info
1138 * @msg: received message header
1140 * Note: Address is not captured if not requested by receiver.
1142 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
1144 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
1147 addr->family = AF_TIPC;
1148 addr->addrtype = TIPC_ADDR_ID;
1149 memset(&addr->addr, 0, sizeof(addr->addr));
1150 addr->addr.id.ref = msg_origport(msg);
1151 addr->addr.id.node = msg_orignode(msg);
1152 addr->addr.name.domain = 0; /* could leave uninitialized */
1153 addr->scope = 0; /* could leave uninitialized */
1154 m->msg_namelen = sizeof(struct sockaddr_tipc);
1159 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
1160 * @m: descriptor for message info
1161 * @msg: received message header
1162 * @tsk: TIPC port associated with message
1164 * Note: Ancillary data is not captured if not requested by receiver.
1166 * Returns 0 if successful, otherwise errno
1168 static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1169 struct tipc_sock *tsk)
1177 if (likely(m->msg_controllen == 0))
1180 /* Optionally capture errored message object(s) */
1181 err = msg ? msg_errcode(msg) : 0;
1182 if (unlikely(err)) {
1184 anc_data[1] = msg_data_sz(msg);
1185 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1189 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1196 /* Optionally capture message destination object */
1197 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1198 switch (dest_type) {
1199 case TIPC_NAMED_MSG:
1201 anc_data[0] = msg_nametype(msg);
1202 anc_data[1] = msg_namelower(msg);
1203 anc_data[2] = msg_namelower(msg);
1205 case TIPC_MCAST_MSG:
1207 anc_data[0] = msg_nametype(msg);
1208 anc_data[1] = msg_namelower(msg);
1209 anc_data[2] = msg_nameupper(msg);
1212 has_name = (tsk->conn_type != 0);
1213 anc_data[0] = tsk->conn_type;
1214 anc_data[1] = tsk->conn_instance;
1215 anc_data[2] = tsk->conn_instance;
1221 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1229 static void tipc_sk_send_ack(struct tipc_sock *tsk)
1231 struct sock *sk = &tsk->sk;
1232 struct net *net = sock_net(sk);
1233 struct sk_buff *skb = NULL;
1234 struct tipc_msg *msg;
1235 u32 peer_port = tsk_peer_port(tsk);
1236 u32 dnode = tsk_peer_node(tsk);
1238 if (!tipc_sk_connected(sk))
1240 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1241 dnode, tsk_own_node(tsk), peer_port,
1242 tsk->portid, TIPC_OK);
1246 msg_set_conn_ack(msg, tsk->rcv_unacked);
1247 tsk->rcv_unacked = 0;
1249 /* Adjust to and advertize the correct window limit */
1250 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1251 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1252 msg_set_adv_win(msg, tsk->rcv_win);
1254 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
1257 static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1259 struct sock *sk = sock->sk;
1261 long timeo = *timeop;
1265 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1266 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
1267 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1272 timeo = schedule_timeout(timeo);
1276 if (!skb_queue_empty(&sk->sk_receive_queue))
1281 err = sock_intr_errno(timeo);
1282 if (signal_pending(current))
1285 finish_wait(sk_sleep(sk), &wait);
1291 * tipc_recvmsg - receive packet-oriented message
1292 * @m: descriptor for message info
1293 * @buf_len: total size of user buffer area
1294 * @flags: receive flags
1296 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1297 * If the complete message doesn't fit in user area, truncate it.
1299 * Returns size of returned message data, errno otherwise
1301 static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1304 struct sock *sk = sock->sk;
1305 struct tipc_sock *tsk = tipc_sk(sk);
1306 struct sk_buff *buf;
1307 struct tipc_msg *msg;
1308 bool is_connectionless = tipc_sk_type_connectionless(sk);
1314 /* Catch invalid receive requests */
1315 if (unlikely(!buf_len))
1320 if (!is_connectionless && unlikely(sk->sk_state == TIPC_OPEN)) {
1325 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1328 /* Look for a message in receive queue; wait if necessary */
1329 res = tipc_wait_for_rcvmsg(sock, &timeo);
1333 /* Look at first message in receive queue */
1334 buf = skb_peek(&sk->sk_receive_queue);
1336 sz = msg_data_sz(msg);
1337 hlen = msg_hdr_sz(msg);
1338 err = msg_errcode(msg);
1340 /* Discard an empty non-errored message & try again */
1341 if ((!sz) && (!err)) {
1342 tsk_advance_rx_queue(sk);
1346 /* Capture sender's address (optional) */
1347 set_orig_addr(m, msg);
1349 /* Capture ancillary data (optional) */
1350 res = tipc_sk_anc_data_recv(m, msg, tsk);
1354 /* Capture message data (if valid) & compute return value (always) */
1356 if (unlikely(buf_len < sz)) {
1358 m->msg_flags |= MSG_TRUNC;
1360 res = skb_copy_datagram_msg(buf, hlen, m, sz);
1365 if (is_connectionless || err == TIPC_CONN_SHUTDOWN ||
1372 if (unlikely(flags & MSG_PEEK))
1375 if (likely(!is_connectionless)) {
1376 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1377 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1378 tipc_sk_send_ack(tsk);
1380 tsk_advance_rx_queue(sk);
1387 * tipc_recv_stream - receive stream-oriented data
1388 * @m: descriptor for message info
1389 * @buf_len: total size of user buffer area
1390 * @flags: receive flags
1392 * Used for SOCK_STREAM messages only. If not enough data is available
1393 * will optionally wait for more; never truncates data.
1395 * Returns size of returned message data, errno otherwise
1397 static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1398 size_t buf_len, int flags)
1400 struct sock *sk = sock->sk;
1401 struct tipc_sock *tsk = tipc_sk(sk);
1402 struct sk_buff *buf;
1403 struct tipc_msg *msg;
1411 /* Catch invalid receive attempts */
1412 if (unlikely(!buf_len))
1417 if (unlikely(sk->sk_state == TIPC_OPEN)) {
1422 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
1423 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1426 /* Look for a message in receive queue; wait if necessary */
1427 res = tipc_wait_for_rcvmsg(sock, &timeo);
1431 /* Look at first message in receive queue */
1432 buf = skb_peek(&sk->sk_receive_queue);
1434 sz = msg_data_sz(msg);
1435 hlen = msg_hdr_sz(msg);
1436 err = msg_errcode(msg);
1438 /* Discard an empty non-errored message & try again */
1439 if ((!sz) && (!err)) {
1440 tsk_advance_rx_queue(sk);
1444 /* Optionally capture sender's address & ancillary data of first msg */
1445 if (sz_copied == 0) {
1446 set_orig_addr(m, msg);
1447 res = tipc_sk_anc_data_recv(m, msg, tsk);
1452 /* Capture message data (if valid) & compute return value (always) */
1454 u32 offset = TIPC_SKB_CB(buf)->bytes_read;
1459 needed = (buf_len - sz_copied);
1460 sz_to_copy = min(sz, needed);
1462 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy);
1466 sz_copied += sz_to_copy;
1468 if (sz_to_copy < sz) {
1469 if (!(flags & MSG_PEEK))
1470 TIPC_SKB_CB(buf)->bytes_read =
1471 offset + sz_to_copy;
1476 goto exit; /* can't add error msg to valid data */
1478 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1484 if (unlikely(flags & MSG_PEEK))
1487 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1488 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1489 tipc_sk_send_ack(tsk);
1490 tsk_advance_rx_queue(sk);
1492 /* Loop around if more data is required */
1493 if ((sz_copied < buf_len) && /* didn't get all requested data */
1494 (!skb_queue_empty(&sk->sk_receive_queue) ||
1495 (sz_copied < target)) && /* and more is ready or required */
1496 (!err)) /* and haven't reached a FIN */
1501 return sz_copied ? sz_copied : res;
1505 * tipc_write_space - wake up thread if port congestion is released
1508 static void tipc_write_space(struct sock *sk)
1510 struct socket_wq *wq;
1513 wq = rcu_dereference(sk->sk_wq);
1514 if (skwq_has_sleeper(wq))
1515 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1516 POLLWRNORM | POLLWRBAND);
1521 * tipc_data_ready - wake up threads to indicate messages have been received
1523 * @len: the length of messages
1525 static void tipc_data_ready(struct sock *sk)
1527 struct socket_wq *wq;
1530 wq = rcu_dereference(sk->sk_wq);
1531 if (skwq_has_sleeper(wq))
1532 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1533 POLLRDNORM | POLLRDBAND);
1537 static void tipc_sock_destruct(struct sock *sk)
1539 __skb_queue_purge(&sk->sk_receive_queue);
1543 * filter_connect - Handle all incoming messages for a connection-based socket
1545 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
1547 * Returns true if everything ok, false otherwise
1549 static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
1551 struct sock *sk = &tsk->sk;
1552 struct net *net = sock_net(sk);
1553 struct tipc_msg *hdr = buf_msg(skb);
1555 if (unlikely(msg_mcast(hdr)))
1558 switch (sk->sk_state) {
1559 case TIPC_CONNECTING:
1560 /* Accept only ACK or NACK message */
1561 if (unlikely(!msg_connected(hdr)))
1564 if (unlikely(msg_errcode(hdr))) {
1565 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1566 sk->sk_err = ECONNREFUSED;
1570 if (unlikely(!msg_isdata(hdr))) {
1571 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1572 sk->sk_err = EINVAL;
1576 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1577 msg_set_importance(&tsk->phdr, msg_importance(hdr));
1579 /* If 'ACK+' message, add to socket receive queue */
1580 if (msg_data_sz(hdr))
1583 /* If empty 'ACK-' message, wake up sleeping connect() */
1584 if (waitqueue_active(sk_sleep(sk)))
1585 wake_up_interruptible(sk_sleep(sk));
1587 /* 'ACK-' message is neither accepted nor rejected: */
1588 msg_set_dest_droppable(hdr, 1);
1592 case TIPC_DISCONNECTING:
1595 /* Accept only SYN message */
1596 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1599 case TIPC_ESTABLISHED:
1600 /* Accept only connection-based messages sent by peer */
1601 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1604 if (unlikely(msg_errcode(hdr))) {
1605 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1606 /* Let timer expire on it's own */
1607 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1609 sk->sk_state_change(sk);
1613 pr_err("Unknown sk_state %u\n", sk->sk_state);
1620 * rcvbuf_limit - get proper overload limit of socket receive queue
1624 * For connection oriented messages, irrespective of importance,
1625 * default queue limit is 2 MB.
1627 * For connectionless messages, queue limits are based on message
1628 * importance as follows:
1630 * TIPC_LOW_IMPORTANCE (2 MB)
1631 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1632 * TIPC_HIGH_IMPORTANCE (8 MB)
1633 * TIPC_CRITICAL_IMPORTANCE (16 MB)
1635 * Returns overload limit according to corresponding message importance
1637 static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
1639 struct tipc_sock *tsk = tipc_sk(sk);
1640 struct tipc_msg *hdr = buf_msg(skb);
1642 if (unlikely(!msg_connected(hdr)))
1643 return sk->sk_rcvbuf << msg_importance(hdr);
1645 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1646 return sk->sk_rcvbuf;
1648 return FLOWCTL_MSG_LIM;
1652 * filter_rcv - validate incoming message
1654 * @skb: pointer to message.
1656 * Enqueues message on receive queue if acceptable; optionally handles
1657 * disconnect indication for a connected socket.
1659 * Called with socket lock already taken
1661 * Returns true if message was added to socket receive queue, otherwise false
1663 static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1664 struct sk_buff_head *xmitq)
1666 struct tipc_sock *tsk = tipc_sk(sk);
1667 struct tipc_msg *hdr = buf_msg(skb);
1668 unsigned int limit = rcvbuf_limit(sk, skb);
1670 int usr = msg_user(hdr);
1673 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
1674 tipc_sk_proto_rcv(tsk, skb, xmitq);
1678 if (unlikely(usr == SOCK_WAKEUP)) {
1679 onode = msg_orignode(hdr);
1681 u32_del(&tsk->cong_links, onode);
1682 tsk->cong_link_cnt--;
1683 sk->sk_write_space(sk);
1687 /* Drop if illegal message type */
1688 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1693 /* Reject if wrong message type for current socket state */
1694 if (tipc_sk_type_connectionless(sk)) {
1695 if (msg_connected(hdr)) {
1696 err = TIPC_ERR_NO_PORT;
1699 } else if (unlikely(!filter_connect(tsk, skb))) {
1700 err = TIPC_ERR_NO_PORT;
1704 /* Reject message if there isn't room to queue it */
1705 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1706 err = TIPC_ERR_OVERLOAD;
1710 /* Enqueue message */
1711 TIPC_SKB_CB(skb)->bytes_read = 0;
1712 __skb_queue_tail(&sk->sk_receive_queue, skb);
1713 skb_set_owner_r(skb, sk);
1715 sk->sk_data_ready(sk);
1719 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1720 __skb_queue_tail(xmitq, skb);
1725 * tipc_backlog_rcv - handle incoming message from backlog queue
1729 * Caller must hold socket lock
1733 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
1735 unsigned int truesize = skb->truesize;
1736 struct sk_buff_head xmitq;
1737 u32 dnode, selector;
1739 __skb_queue_head_init(&xmitq);
1741 if (likely(filter_rcv(sk, skb, &xmitq))) {
1742 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
1746 if (skb_queue_empty(&xmitq))
1749 /* Send response/rejected message */
1750 skb = __skb_dequeue(&xmitq);
1751 dnode = msg_destnode(buf_msg(skb));
1752 selector = msg_origport(buf_msg(skb));
1753 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
1758 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1759 * inputq and try adding them to socket or backlog queue
1760 * @inputq: list of incoming buffers with potentially different destinations
1761 * @sk: socket where the buffers should be enqueued
1762 * @dport: port number for the socket
1764 * Caller must hold socket lock
1766 static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
1767 u32 dport, struct sk_buff_head *xmitq)
1769 unsigned long time_limit = jiffies + 2;
1770 struct sk_buff *skb;
1775 while (skb_queue_len(inputq)) {
1776 if (unlikely(time_after_eq(jiffies, time_limit)))
1779 skb = tipc_skb_dequeue(inputq, dport);
1783 /* Add message directly to receive queue if possible */
1784 if (!sock_owned_by_user(sk)) {
1785 filter_rcv(sk, skb, xmitq);
1789 /* Try backlog, compensating for double-counted bytes */
1790 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
1791 if (!sk->sk_backlog.len)
1792 atomic_set(dcnt, 0);
1793 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1794 if (likely(!sk_add_backlog(sk, skb, lim)))
1797 /* Overload => reject message back to sender */
1798 onode = tipc_own_addr(sock_net(sk));
1799 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1800 __skb_queue_tail(xmitq, skb);
1806 * tipc_sk_rcv - handle a chain of incoming buffers
1807 * @inputq: buffer list containing the buffers
1808 * Consumes all buffers in list until inputq is empty
1809 * Note: may be called in multiple threads referring to the same queue
1811 void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
1813 struct sk_buff_head xmitq;
1814 u32 dnode, dport = 0;
1816 struct tipc_sock *tsk;
1818 struct sk_buff *skb;
1820 __skb_queue_head_init(&xmitq);
1821 while (skb_queue_len(inputq)) {
1822 dport = tipc_skb_peek_port(inputq, dport);
1823 tsk = tipc_sk_lookup(net, dport);
1827 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
1828 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
1829 spin_unlock_bh(&sk->sk_lock.slock);
1831 /* Send pending response/rejected messages, if any */
1832 while ((skb = __skb_dequeue(&xmitq))) {
1833 dnode = msg_destnode(buf_msg(skb));
1834 tipc_node_xmit_skb(net, skb, dnode, dport);
1840 /* No destination socket => dequeue skb if still there */
1841 skb = tipc_skb_dequeue(inputq, dport);
1845 /* Try secondary lookup if unresolved named message */
1846 err = TIPC_ERR_NO_PORT;
1847 if (tipc_msg_lookup_dest(net, skb, &err))
1850 /* Prepare for message rejection */
1851 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
1854 dnode = msg_destnode(buf_msg(skb));
1855 tipc_node_xmit_skb(net, skb, dnode, dport);
1859 static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1861 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1862 struct sock *sk = sock->sk;
1866 int err = sock_error(sk);
1871 if (signal_pending(current))
1872 return sock_intr_errno(*timeo_p);
1874 add_wait_queue(sk_sleep(sk), &wait);
1875 done = sk_wait_event(sk, timeo_p,
1876 sk->sk_state != TIPC_CONNECTING, &wait);
1877 remove_wait_queue(sk_sleep(sk), &wait);
1883 * tipc_connect - establish a connection to another TIPC port
1884 * @sock: socket structure
1885 * @dest: socket address for destination port
1886 * @destlen: size of socket address data structure
1887 * @flags: file-related flags associated with socket
1889 * Returns 0 on success, errno otherwise
1891 static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1892 int destlen, int flags)
1894 struct sock *sk = sock->sk;
1895 struct tipc_sock *tsk = tipc_sk(sk);
1896 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1897 struct msghdr m = {NULL,};
1898 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
1904 /* DGRAM/RDM connect(), just save the destaddr */
1905 if (tipc_sk_type_connectionless(sk)) {
1906 if (dst->family == AF_UNSPEC) {
1907 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
1908 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1911 memcpy(&tsk->peer, dest, destlen);
1917 * Reject connection attempt using multicast address
1919 * Note: send_msg() validates the rest of the address fields,
1920 * so there's no need to do it here
1922 if (dst->addrtype == TIPC_ADDR_MCAST) {
1927 previous = sk->sk_state;
1929 switch (sk->sk_state) {
1931 /* Send a 'SYN-' to destination */
1933 m.msg_namelen = destlen;
1935 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1936 * indicate send_msg() is never blocked.
1939 m.msg_flags = MSG_DONTWAIT;
1941 res = __tipc_sendmsg(sock, &m, 0);
1942 if ((res < 0) && (res != -EWOULDBLOCK))
1945 /* Just entered TIPC_CONNECTING state; the only
1946 * difference is that return value in non-blocking
1947 * case is EINPROGRESS, rather than EALREADY.
1951 case TIPC_CONNECTING:
1953 if (previous == TIPC_CONNECTING)
1957 timeout = msecs_to_jiffies(timeout);
1958 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1959 res = tipc_wait_for_connect(sock, &timeout);
1961 case TIPC_ESTABLISHED:
1974 * tipc_listen - allow socket to listen for incoming connections
1975 * @sock: socket structure
1978 * Returns 0 on success, errno otherwise
1980 static int tipc_listen(struct socket *sock, int len)
1982 struct sock *sk = sock->sk;
1986 res = tipc_set_sk_state(sk, TIPC_LISTEN);
1992 static int tipc_wait_for_accept(struct socket *sock, long timeo)
1994 struct sock *sk = sock->sk;
1998 /* True wake-one mechanism for incoming connections: only
1999 * one process gets woken up, not the 'whole herd'.
2000 * Since we do not 'race & poll' for established sockets
2001 * anymore, the common case will execute the loop only once.
2004 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2005 TASK_INTERRUPTIBLE);
2006 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
2008 timeo = schedule_timeout(timeo);
2012 if (!skb_queue_empty(&sk->sk_receive_queue))
2017 err = sock_intr_errno(timeo);
2018 if (signal_pending(current))
2021 finish_wait(sk_sleep(sk), &wait);
2026 * tipc_accept - wait for connection request
2027 * @sock: listening socket
2028 * @newsock: new socket that is to be connected
2029 * @flags: file-related flags associated with socket
2031 * Returns 0 on success, errno otherwise
2033 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
2036 struct sock *new_sk, *sk = sock->sk;
2037 struct sk_buff *buf;
2038 struct tipc_sock *new_tsock;
2039 struct tipc_msg *msg;
2045 if (sk->sk_state != TIPC_LISTEN) {
2049 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2050 res = tipc_wait_for_accept(sock, timeo);
2054 buf = skb_peek(&sk->sk_receive_queue);
2056 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
2059 security_sk_clone(sock->sk, new_sock->sk);
2061 new_sk = new_sock->sk;
2062 new_tsock = tipc_sk(new_sk);
2065 /* we lock on new_sk; but lockdep sees the lock on sk */
2066 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
2069 * Reject any stray messages received by new socket
2070 * before the socket lock was taken (very, very unlikely)
2072 tsk_rej_rx_queue(new_sk);
2074 /* Connect new socket to it's peer */
2075 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
2077 tsk_set_importance(new_tsock, msg_importance(msg));
2078 if (msg_named(msg)) {
2079 new_tsock->conn_type = msg_nametype(msg);
2080 new_tsock->conn_instance = msg_nameinst(msg);
2084 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2085 * Respond to 'SYN+' by queuing it on new socket.
2087 if (!msg_data_sz(msg)) {
2088 struct msghdr m = {NULL,};
2090 tsk_advance_rx_queue(sk);
2091 __tipc_sendstream(new_sock, &m, 0);
2093 __skb_dequeue(&sk->sk_receive_queue);
2094 __skb_queue_head(&new_sk->sk_receive_queue, buf);
2095 skb_set_owner_r(buf, new_sk);
2097 release_sock(new_sk);
2104 * tipc_shutdown - shutdown socket connection
2105 * @sock: socket structure
2106 * @how: direction to close (must be SHUT_RDWR)
2108 * Terminates connection (if necessary), then purges socket's receive queue.
2110 * Returns 0 on success, errno otherwise
2112 static int tipc_shutdown(struct socket *sock, int how)
2114 struct sock *sk = sock->sk;
2117 if (how != SHUT_RDWR)
2122 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2123 sk->sk_shutdown = SEND_SHUTDOWN;
2125 if (sk->sk_state == TIPC_DISCONNECTING) {
2126 /* Discard any unreceived messages */
2127 __skb_queue_purge(&sk->sk_receive_queue);
2129 /* Wake up anyone sleeping in poll */
2130 sk->sk_state_change(sk);
2140 static void tipc_sk_timeout(unsigned long data)
2142 struct tipc_sock *tsk = (struct tipc_sock *)data;
2143 struct sock *sk = &tsk->sk;
2144 struct sk_buff *skb = NULL;
2145 u32 peer_port, peer_node;
2146 u32 own_node = tsk_own_node(tsk);
2149 if (!tipc_sk_connected(sk)) {
2153 peer_port = tsk_peer_port(tsk);
2154 peer_node = tsk_peer_node(tsk);
2156 if (tsk->probe_unacked) {
2157 if (!sock_owned_by_user(sk)) {
2158 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2159 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2160 tsk_peer_port(tsk));
2161 sk->sk_state_change(sk);
2163 /* Try again later */
2164 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2171 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2172 INT_H_SIZE, 0, peer_node, own_node,
2173 peer_port, tsk->portid, TIPC_OK);
2174 tsk->probe_unacked = true;
2175 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
2178 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
2183 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
2184 struct tipc_name_seq const *seq)
2186 struct sock *sk = &tsk->sk;
2187 struct net *net = sock_net(sk);
2188 struct publication *publ;
2191 if (tipc_sk_connected(sk))
2193 key = tsk->portid + tsk->pub_count + 1;
2194 if (key == tsk->portid)
2197 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
2198 scope, tsk->portid, key);
2199 if (unlikely(!publ))
2202 list_add(&publ->pport_list, &tsk->publications);
2208 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
2209 struct tipc_name_seq const *seq)
2211 struct net *net = sock_net(&tsk->sk);
2212 struct publication *publ;
2213 struct publication *safe;
2216 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
2218 if (publ->scope != scope)
2220 if (publ->type != seq->type)
2222 if (publ->lower != seq->lower)
2224 if (publ->upper != seq->upper)
2226 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2227 publ->ref, publ->key);
2231 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2232 publ->ref, publ->key);
2235 if (list_empty(&tsk->publications))
2240 /* tipc_sk_reinit: set non-zero address in all existing sockets
2241 * when we go from standalone to network mode.
2243 void tipc_sk_reinit(struct net *net)
2245 struct tipc_net *tn = net_generic(net, tipc_net_id);
2246 struct rhashtable_iter iter;
2247 struct tipc_sock *tsk;
2248 struct tipc_msg *msg;
2250 rhashtable_walk_enter(&tn->sk_rht, &iter);
2253 tsk = ERR_PTR(rhashtable_walk_start(&iter));
2257 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
2258 spin_lock_bh(&tsk->sk.sk_lock.slock);
2260 msg_set_prevnode(msg, tn->own_addr);
2261 msg_set_orignode(msg, tn->own_addr);
2262 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2265 rhashtable_walk_stop(&iter);
2266 } while (tsk == ERR_PTR(-EAGAIN));
2269 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
2271 struct tipc_net *tn = net_generic(net, tipc_net_id);
2272 struct tipc_sock *tsk;
2275 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
2277 sock_hold(&tsk->sk);
2283 static int tipc_sk_insert(struct tipc_sock *tsk)
2285 struct sock *sk = &tsk->sk;
2286 struct net *net = sock_net(sk);
2287 struct tipc_net *tn = net_generic(net, tipc_net_id);
2288 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2289 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2291 while (remaining--) {
2293 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2294 portid = TIPC_MIN_PORT;
2295 tsk->portid = portid;
2296 sock_hold(&tsk->sk);
2297 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2306 static void tipc_sk_remove(struct tipc_sock *tsk)
2308 struct sock *sk = &tsk->sk;
2309 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
2311 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
2312 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2317 static const struct rhashtable_params tsk_rht_params = {
2319 .head_offset = offsetof(struct tipc_sock, node),
2320 .key_offset = offsetof(struct tipc_sock, portid),
2321 .key_len = sizeof(u32), /* portid */
2322 .max_size = 1048576,
2324 .automatic_shrinking = true,
2327 int tipc_sk_rht_init(struct net *net)
2329 struct tipc_net *tn = net_generic(net, tipc_net_id);
2331 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
2334 void tipc_sk_rht_destroy(struct net *net)
2336 struct tipc_net *tn = net_generic(net, tipc_net_id);
2338 /* Wait for socket readers to complete */
2341 rhashtable_destroy(&tn->sk_rht);
2345 * tipc_setsockopt - set socket option
2346 * @sock: socket structure
2347 * @lvl: option level
2348 * @opt: option identifier
2349 * @ov: pointer to new option value
2350 * @ol: length of option value
2352 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
2353 * (to ease compatibility).
2355 * Returns 0 on success, errno otherwise
2357 static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2358 char __user *ov, unsigned int ol)
2360 struct sock *sk = sock->sk;
2361 struct tipc_sock *tsk = tipc_sk(sk);
2365 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2367 if (lvl != SOL_TIPC)
2368 return -ENOPROTOOPT;
2371 case TIPC_IMPORTANCE:
2372 case TIPC_SRC_DROPPABLE:
2373 case TIPC_DEST_DROPPABLE:
2374 case TIPC_CONN_TIMEOUT:
2375 if (ol < sizeof(value))
2377 res = get_user(value, (u32 __user *)ov);
2389 case TIPC_IMPORTANCE:
2390 res = tsk_set_importance(tsk, value);
2392 case TIPC_SRC_DROPPABLE:
2393 if (sock->type != SOCK_STREAM)
2394 tsk_set_unreliable(tsk, value);
2398 case TIPC_DEST_DROPPABLE:
2399 tsk_set_unreturnable(tsk, value);
2401 case TIPC_CONN_TIMEOUT:
2402 tipc_sk(sk)->conn_timeout = value;
2404 case TIPC_MCAST_BROADCAST:
2405 tsk->mc_method.rcast = false;
2406 tsk->mc_method.mandatory = true;
2408 case TIPC_MCAST_REPLICAST:
2409 tsk->mc_method.rcast = true;
2410 tsk->mc_method.mandatory = true;
2422 * tipc_getsockopt - get socket option
2423 * @sock: socket structure
2424 * @lvl: option level
2425 * @opt: option identifier
2426 * @ov: receptacle for option value
2427 * @ol: receptacle for length of option value
2429 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
2430 * (to ease compatibility).
2432 * Returns 0 on success, errno otherwise
2434 static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2435 char __user *ov, int __user *ol)
2437 struct sock *sk = sock->sk;
2438 struct tipc_sock *tsk = tipc_sk(sk);
2443 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2444 return put_user(0, ol);
2445 if (lvl != SOL_TIPC)
2446 return -ENOPROTOOPT;
2447 res = get_user(len, ol);
2454 case TIPC_IMPORTANCE:
2455 value = tsk_importance(tsk);
2457 case TIPC_SRC_DROPPABLE:
2458 value = tsk_unreliable(tsk);
2460 case TIPC_DEST_DROPPABLE:
2461 value = tsk_unreturnable(tsk);
2463 case TIPC_CONN_TIMEOUT:
2464 value = tsk->conn_timeout;
2465 /* no need to set "res", since already 0 at this point */
2467 case TIPC_NODE_RECVQ_DEPTH:
2468 value = 0; /* was tipc_queue_size, now obsolete */
2470 case TIPC_SOCK_RECVQ_DEPTH:
2471 value = skb_queue_len(&sk->sk_receive_queue);
2480 return res; /* "get" failed */
2482 if (len < sizeof(value))
2485 if (copy_to_user(ov, &value, sizeof(value)))
2488 return put_user(sizeof(value), ol);
2491 static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
2493 struct sock *sk = sock->sk;
2494 struct tipc_sioc_ln_req lnr;
2495 void __user *argp = (void __user *)arg;
2498 case SIOCGETLINKNAME:
2499 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2501 if (!tipc_node_get_linkname(sock_net(sk),
2502 lnr.bearer_id & 0xffff, lnr.peer,
2503 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2504 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2508 return -EADDRNOTAVAIL;
2510 return -ENOIOCTLCMD;
2514 /* Protocol switches for the various types of TIPC sockets */
2516 static const struct proto_ops msg_ops = {
2517 .owner = THIS_MODULE,
2519 .release = tipc_release,
2521 .connect = tipc_connect,
2522 .socketpair = sock_no_socketpair,
2523 .accept = sock_no_accept,
2524 .getname = tipc_getname,
2526 .ioctl = tipc_ioctl,
2527 .listen = sock_no_listen,
2528 .shutdown = tipc_shutdown,
2529 .setsockopt = tipc_setsockopt,
2530 .getsockopt = tipc_getsockopt,
2531 .sendmsg = tipc_sendmsg,
2532 .recvmsg = tipc_recvmsg,
2533 .mmap = sock_no_mmap,
2534 .sendpage = sock_no_sendpage
2537 static const struct proto_ops packet_ops = {
2538 .owner = THIS_MODULE,
2540 .release = tipc_release,
2542 .connect = tipc_connect,
2543 .socketpair = sock_no_socketpair,
2544 .accept = tipc_accept,
2545 .getname = tipc_getname,
2547 .ioctl = tipc_ioctl,
2548 .listen = tipc_listen,
2549 .shutdown = tipc_shutdown,
2550 .setsockopt = tipc_setsockopt,
2551 .getsockopt = tipc_getsockopt,
2552 .sendmsg = tipc_send_packet,
2553 .recvmsg = tipc_recvmsg,
2554 .mmap = sock_no_mmap,
2555 .sendpage = sock_no_sendpage
2558 static const struct proto_ops stream_ops = {
2559 .owner = THIS_MODULE,
2561 .release = tipc_release,
2563 .connect = tipc_connect,
2564 .socketpair = sock_no_socketpair,
2565 .accept = tipc_accept,
2566 .getname = tipc_getname,
2568 .ioctl = tipc_ioctl,
2569 .listen = tipc_listen,
2570 .shutdown = tipc_shutdown,
2571 .setsockopt = tipc_setsockopt,
2572 .getsockopt = tipc_getsockopt,
2573 .sendmsg = tipc_sendstream,
2574 .recvmsg = tipc_recv_stream,
2575 .mmap = sock_no_mmap,
2576 .sendpage = sock_no_sendpage
2579 static const struct net_proto_family tipc_family_ops = {
2580 .owner = THIS_MODULE,
2582 .create = tipc_sk_create
2585 static struct proto tipc_proto = {
2587 .owner = THIS_MODULE,
2588 .obj_size = sizeof(struct tipc_sock),
2589 .sysctl_rmem = sysctl_tipc_rmem
2593 * tipc_socket_init - initialize TIPC socket interface
2595 * Returns 0 on success, errno otherwise
2597 int tipc_socket_init(void)
2601 res = proto_register(&tipc_proto, 1);
2603 pr_err("Failed to register TIPC protocol type\n");
2607 res = sock_register(&tipc_family_ops);
2609 pr_err("Failed to register TIPC socket type\n");
2610 proto_unregister(&tipc_proto);
2618 * tipc_socket_stop - stop TIPC socket interface
2620 void tipc_socket_stop(void)
2622 sock_unregister(tipc_family_ops.family);
2623 proto_unregister(&tipc_proto);
2626 /* Caller should hold socket lock for the passed tipc socket. */
2627 static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
2631 struct nlattr *nest;
2633 peer_node = tsk_peer_node(tsk);
2634 peer_port = tsk_peer_port(tsk);
2636 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2638 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2640 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2643 if (tsk->conn_type != 0) {
2644 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2646 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2648 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2651 nla_nest_end(skb, nest);
2656 nla_nest_cancel(skb, nest);
2661 /* Caller should hold socket lock for the passed tipc socket. */
2662 static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2663 struct tipc_sock *tsk)
2667 struct nlattr *attrs;
2668 struct net *net = sock_net(skb->sk);
2669 struct tipc_net *tn = net_generic(net, tipc_net_id);
2670 struct sock *sk = &tsk->sk;
2672 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2673 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
2677 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2679 goto genlmsg_cancel;
2680 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
2681 goto attr_msg_cancel;
2682 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
2683 goto attr_msg_cancel;
2685 if (tipc_sk_connected(sk)) {
2686 err = __tipc_nl_add_sk_con(skb, tsk);
2688 goto attr_msg_cancel;
2689 } else if (!list_empty(&tsk->publications)) {
2690 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2691 goto attr_msg_cancel;
2693 nla_nest_end(skb, attrs);
2694 genlmsg_end(skb, hdr);
2699 nla_nest_cancel(skb, attrs);
2701 genlmsg_cancel(skb, hdr);
2706 int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2709 struct tipc_sock *tsk;
2710 const struct bucket_table *tbl;
2711 struct rhash_head *pos;
2712 struct net *net = sock_net(skb->sk);
2713 struct tipc_net *tn = net_generic(net, tipc_net_id);
2714 u32 tbl_id = cb->args[0];
2715 u32 prev_portid = cb->args[1];
2718 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
2719 for (; tbl_id < tbl->size; tbl_id++) {
2720 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
2721 spin_lock_bh(&tsk->sk.sk_lock.slock);
2722 if (prev_portid && prev_portid != tsk->portid) {
2723 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2727 err = __tipc_nl_add_sk(skb, cb, tsk);
2729 prev_portid = tsk->portid;
2730 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2734 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2739 cb->args[0] = tbl_id;
2740 cb->args[1] = prev_portid;
2745 /* Caller should hold socket lock for the passed tipc socket. */
2746 static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2747 struct netlink_callback *cb,
2748 struct publication *publ)
2751 struct nlattr *attrs;
2753 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2754 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
2758 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2760 goto genlmsg_cancel;
2762 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2763 goto attr_msg_cancel;
2764 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2765 goto attr_msg_cancel;
2766 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2767 goto attr_msg_cancel;
2768 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2769 goto attr_msg_cancel;
2771 nla_nest_end(skb, attrs);
2772 genlmsg_end(skb, hdr);
2777 nla_nest_cancel(skb, attrs);
2779 genlmsg_cancel(skb, hdr);
2784 /* Caller should hold socket lock for the passed tipc socket. */
2785 static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2786 struct netlink_callback *cb,
2787 struct tipc_sock *tsk, u32 *last_publ)
2790 struct publication *p;
2793 list_for_each_entry(p, &tsk->publications, pport_list) {
2794 if (p->key == *last_publ)
2797 if (p->key != *last_publ) {
2798 /* We never set seq or call nl_dump_check_consistent()
2799 * this means that setting prev_seq here will cause the
2800 * consistence check to fail in the netlink callback
2801 * handler. Resulting in the last NLMSG_DONE message
2802 * having the NLM_F_DUMP_INTR flag set.
2809 p = list_first_entry(&tsk->publications, struct publication,
2813 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2814 err = __tipc_nl_add_sk_publ(skb, cb, p);
2816 *last_publ = p->key;
2825 int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2828 u32 tsk_portid = cb->args[0];
2829 u32 last_publ = cb->args[1];
2830 u32 done = cb->args[2];
2831 struct net *net = sock_net(skb->sk);
2832 struct tipc_sock *tsk;
2835 struct nlattr **attrs;
2836 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2838 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2842 if (!attrs[TIPC_NLA_SOCK])
2845 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2846 attrs[TIPC_NLA_SOCK],
2847 tipc_nl_sock_policy);
2851 if (!sock[TIPC_NLA_SOCK_REF])
2854 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
2860 tsk = tipc_sk_lookup(net, tsk_portid);
2864 lock_sock(&tsk->sk);
2865 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2868 release_sock(&tsk->sk);
2871 cb->args[0] = tsk_portid;
2872 cb->args[1] = last_publ;