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
54 #define TIPC_ACK_RATE 4 /* ACK at 1/4 of of rcv window size */
57 TIPC_LISTEN = TCP_LISTEN,
58 TIPC_ESTABLISHED = TCP_ESTABLISHED,
59 TIPC_OPEN = TCP_CLOSE,
60 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
61 TIPC_CONNECTING = TCP_SYN_SENT,
65 * struct tipc_sock - TIPC socket structure
66 * @sk: socket - interacts with 'port' and with user via the socket API
67 * @conn_type: TIPC type used when connection was established
68 * @conn_instance: TIPC instance used when connection was established
69 * @published: non-zero if port has one or more associated names
70 * @max_pkt: maximum packet size "hint" used when building messages sent by port
71 * @portid: unique port identity in TIPC socket hash table
72 * @phdr: preformatted message header used when sending messages
73 * #cong_links: list of congested links
74 * @publications: list of publications for port
75 * @blocking_link: address of the congested link we are currently sleeping on
76 * @pub_count: total # of publications port has made during its lifetime
78 * @conn_timeout: the time we can wait for an unresponded setup request
79 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
80 * @cong_link_cnt: number of congested links
81 * @sent_unacked: # messages sent by socket, and not yet acked by peer
82 * @rcv_unacked: # messages read by user, but not yet acked back to peer
83 * @peer: 'connected' peer for dgram/rdm
84 * @node: hash table node
85 * @mc_method: cookie for use between socket and broadcast layer
86 * @rcu: rcu struct for tipc_sock
96 struct list_head cong_links;
97 struct list_head publications;
100 atomic_t dupl_rcvcnt;
108 struct sockaddr_tipc peer;
109 struct rhash_head node;
110 struct tipc_mc_method mc_method;
114 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
115 static void tipc_data_ready(struct sock *sk);
116 static void tipc_write_space(struct sock *sk);
117 static void tipc_sock_destruct(struct sock *sk);
118 static int tipc_release(struct socket *sock);
119 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
121 static void tipc_sk_timeout(unsigned long data);
122 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
123 struct tipc_name_seq const *seq);
124 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
125 struct tipc_name_seq const *seq);
126 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
127 static int tipc_sk_insert(struct tipc_sock *tsk);
128 static void tipc_sk_remove(struct tipc_sock *tsk);
129 static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
130 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
132 static const struct proto_ops packet_ops;
133 static const struct proto_ops stream_ops;
134 static const struct proto_ops msg_ops;
135 static struct proto tipc_proto;
136 static const struct rhashtable_params tsk_rht_params;
138 static u32 tsk_own_node(struct tipc_sock *tsk)
140 return msg_prevnode(&tsk->phdr);
143 static u32 tsk_peer_node(struct tipc_sock *tsk)
145 return msg_destnode(&tsk->phdr);
148 static u32 tsk_peer_port(struct tipc_sock *tsk)
150 return msg_destport(&tsk->phdr);
153 static bool tsk_unreliable(struct tipc_sock *tsk)
155 return msg_src_droppable(&tsk->phdr) != 0;
158 static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
160 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
163 static bool tsk_unreturnable(struct tipc_sock *tsk)
165 return msg_dest_droppable(&tsk->phdr) != 0;
168 static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
170 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
173 static int tsk_importance(struct tipc_sock *tsk)
175 return msg_importance(&tsk->phdr);
178 static int tsk_set_importance(struct tipc_sock *tsk, int imp)
180 if (imp > TIPC_CRITICAL_IMPORTANCE)
182 msg_set_importance(&tsk->phdr, (u32)imp);
186 static struct tipc_sock *tipc_sk(const struct sock *sk)
188 return container_of(sk, struct tipc_sock, sk);
191 static bool tsk_conn_cong(struct tipc_sock *tsk)
193 return tsk->snt_unacked > tsk->snd_win;
196 /* tsk_blocks(): translate a buffer size in bytes to number of
197 * advertisable blocks, taking into account the ratio truesize(len)/len
198 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
200 static u16 tsk_adv_blocks(int len)
202 return len / FLOWCTL_BLK_SZ / 4;
205 /* tsk_inc(): increment counter for sent or received data
206 * - If block based flow control is not supported by peer we
207 * fall back to message based ditto, incrementing the counter
209 static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
211 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
212 return ((msglen / FLOWCTL_BLK_SZ) + 1);
217 * tsk_advance_rx_queue - discard first buffer in socket receive queue
219 * Caller must hold socket lock
221 static void tsk_advance_rx_queue(struct sock *sk)
223 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
226 /* tipc_sk_respond() : send response message back to sender
228 static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
232 u32 onode = tipc_own_addr(sock_net(sk));
234 if (!tipc_msg_reverse(onode, &skb, err))
237 dnode = msg_destnode(buf_msg(skb));
238 selector = msg_origport(buf_msg(skb));
239 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
243 * tsk_rej_rx_queue - reject all buffers in socket receive queue
245 * Caller must hold socket lock
247 static void tsk_rej_rx_queue(struct sock *sk)
251 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
252 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
255 static bool tipc_sk_connected(struct sock *sk)
257 return sk->sk_state == TIPC_ESTABLISHED;
260 /* tipc_sk_type_connectionless - check if the socket is datagram socket
263 * Returns true if connection less, false otherwise
265 static bool tipc_sk_type_connectionless(struct sock *sk)
267 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
270 /* tsk_peer_msg - verify if message was sent by connected port's peer
272 * Handles cases where the node's network address has changed from
273 * the default of <0.0.0> to its configured setting.
275 static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
277 struct sock *sk = &tsk->sk;
278 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
279 u32 peer_port = tsk_peer_port(tsk);
283 if (unlikely(!tipc_sk_connected(sk)))
286 if (unlikely(msg_origport(msg) != peer_port))
289 orig_node = msg_orignode(msg);
290 peer_node = tsk_peer_node(tsk);
292 if (likely(orig_node == peer_node))
295 if (!orig_node && (peer_node == tn->own_addr))
298 if (!peer_node && (orig_node == tn->own_addr))
304 /* tipc_set_sk_state - set the sk_state of the socket
307 * Caller must hold socket lock
309 * Returns 0 on success, errno otherwise
311 static int tipc_set_sk_state(struct sock *sk, int state)
313 int oldsk_state = sk->sk_state;
321 case TIPC_CONNECTING:
322 if (oldsk_state == TIPC_OPEN)
325 case TIPC_ESTABLISHED:
326 if (oldsk_state == TIPC_CONNECTING ||
327 oldsk_state == TIPC_OPEN)
330 case TIPC_DISCONNECTING:
331 if (oldsk_state == TIPC_CONNECTING ||
332 oldsk_state == TIPC_ESTABLISHED)
338 sk->sk_state = state;
343 static int tipc_sk_sock_err(struct socket *sock, long *timeout)
345 struct sock *sk = sock->sk;
346 int err = sock_error(sk);
347 int typ = sock->type;
351 if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
352 if (sk->sk_state == TIPC_DISCONNECTING)
354 else if (!tipc_sk_connected(sk))
359 if (signal_pending(current))
360 return sock_intr_errno(*timeout);
365 #define tipc_wait_for_cond(sock_, timeo_, condition_) \
370 while ((rc_ = !(condition_))) { \
371 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
373 rc_ = tipc_sk_sock_err((sock_), timeo_); \
376 prepare_to_wait(sk_sleep(sk_), &wait_, TASK_INTERRUPTIBLE); \
378 *(timeo_) = wait_woken(&wait_, TASK_INTERRUPTIBLE, *(timeo_)); \
379 sched_annotate_sleep(); \
381 remove_wait_queue(sk_sleep(sk_), &wait_); \
387 * tipc_sk_create - create a TIPC socket
388 * @net: network namespace (must be default network)
389 * @sock: pre-allocated socket structure
390 * @protocol: protocol indicator (must be 0)
391 * @kern: caused by kernel or by userspace?
393 * This routine creates additional data structures used by the TIPC socket,
394 * initializes them, and links them together.
396 * Returns 0 on success, errno otherwise
398 static int tipc_sk_create(struct net *net, struct socket *sock,
399 int protocol, int kern)
402 const struct proto_ops *ops;
404 struct tipc_sock *tsk;
405 struct tipc_msg *msg;
407 /* Validate arguments */
408 if (unlikely(protocol != 0))
409 return -EPROTONOSUPPORT;
411 switch (sock->type) {
426 /* Allocate socket's protocol area */
427 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
432 tsk->max_pkt = MAX_PKT_DEFAULT;
433 INIT_LIST_HEAD(&tsk->publications);
434 INIT_LIST_HEAD(&tsk->cong_links);
436 tn = net_generic(sock_net(sk), tipc_net_id);
438 /* Finish initializing socket data structures */
440 sock_init_data(sock, sk);
441 tipc_set_sk_state(sk, TIPC_OPEN);
442 if (tipc_sk_insert(tsk)) {
443 pr_warn("Socket create failed; port number exhausted\n");
447 /* Ensure tsk is visible before we read own_addr. */
450 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
453 msg_set_origport(msg, tsk->portid);
454 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
456 sk->sk_backlog_rcv = tipc_backlog_rcv;
457 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
458 sk->sk_data_ready = tipc_data_ready;
459 sk->sk_write_space = tipc_write_space;
460 sk->sk_destruct = tipc_sock_destruct;
461 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
462 atomic_set(&tsk->dupl_rcvcnt, 0);
464 /* Start out with safe limits until we receive an advertised window */
465 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
466 tsk->rcv_win = tsk->snd_win;
468 if (tipc_sk_type_connectionless(sk)) {
469 tsk_set_unreturnable(tsk, true);
470 if (sock->type == SOCK_DGRAM)
471 tsk_set_unreliable(tsk, true);
477 static void tipc_sk_callback(struct rcu_head *head)
479 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
484 /* Caller should hold socket lock for the socket. */
485 static void __tipc_shutdown(struct socket *sock, int error)
487 struct sock *sk = sock->sk;
488 struct tipc_sock *tsk = tipc_sk(sk);
489 struct net *net = sock_net(sk);
490 long timeout = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
491 u32 dnode = tsk_peer_node(tsk);
494 /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
495 tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
496 !tsk_conn_cong(tsk)));
498 /* Reject all unreceived messages, except on an active connection
499 * (which disconnects locally & sends a 'FIN+' to peer).
501 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
502 if (TIPC_SKB_CB(skb)->bytes_read) {
506 if (!tipc_sk_type_connectionless(sk) &&
507 sk->sk_state != TIPC_DISCONNECTING) {
508 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
509 tipc_node_remove_conn(net, dnode, tsk->portid);
511 tipc_sk_respond(sk, skb, error);
514 if (tipc_sk_type_connectionless(sk))
517 if (sk->sk_state != TIPC_DISCONNECTING) {
518 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
519 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
520 tsk_own_node(tsk), tsk_peer_port(tsk),
523 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
524 tipc_node_remove_conn(net, dnode, tsk->portid);
525 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
530 * tipc_release - destroy a TIPC socket
531 * @sock: socket to destroy
533 * This routine cleans up any messages that are still queued on the socket.
534 * For DGRAM and RDM socket types, all queued messages are rejected.
535 * For SEQPACKET and STREAM socket types, the first message is rejected
536 * and any others are discarded. (If the first message on a STREAM socket
537 * is partially-read, it is discarded and the next one is rejected instead.)
539 * NOTE: Rejected messages are not necessarily returned to the sender! They
540 * are returned or discarded according to the "destination droppable" setting
541 * specified for the message by the sender.
543 * Returns 0 on success, errno otherwise
545 static int tipc_release(struct socket *sock)
547 struct sock *sk = sock->sk;
548 struct tipc_sock *tsk;
551 * Exit if socket isn't fully initialized (occurs when a failed accept()
552 * releases a pre-allocated child socket that was never used)
560 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
561 sk->sk_shutdown = SHUTDOWN_MASK;
562 tipc_sk_withdraw(tsk, 0, NULL);
563 sk_stop_timer(sk, &sk->sk_timer);
566 /* Reject any messages that accumulated in backlog queue */
568 u32_list_purge(&tsk->cong_links);
569 tsk->cong_link_cnt = 0;
570 call_rcu(&tsk->rcu, tipc_sk_callback);
577 * tipc_bind - associate or disassocate TIPC name(s) with a socket
578 * @sock: socket structure
579 * @uaddr: socket address describing name(s) and desired operation
580 * @uaddr_len: size of socket address data structure
582 * Name and name sequence binding is indicated using a positive scope value;
583 * a negative scope value unbinds the specified name. Specifying no name
584 * (i.e. a socket address length of 0) unbinds all names from the socket.
586 * Returns 0 on success, errno otherwise
588 * NOTE: This routine doesn't need to take the socket lock since it doesn't
589 * access any non-constant socket information.
591 static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
594 struct sock *sk = sock->sk;
595 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
596 struct tipc_sock *tsk = tipc_sk(sk);
600 if (unlikely(!uaddr_len)) {
601 res = tipc_sk_withdraw(tsk, 0, NULL);
605 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
609 if (addr->family != AF_TIPC) {
614 if (addr->addrtype == TIPC_ADDR_NAME)
615 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
616 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
621 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
622 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
623 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
628 res = (addr->scope > 0) ?
629 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
630 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
637 * tipc_getname - get port ID of socket or peer socket
638 * @sock: socket structure
639 * @uaddr: area for returned socket address
640 * @uaddr_len: area for returned length of socket address
641 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
643 * Returns 0 on success, errno otherwise
645 * NOTE: This routine doesn't need to take the socket lock since it only
646 * accesses socket information that is unchanging (or which changes in
647 * a completely predictable manner).
649 static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
650 int *uaddr_len, int peer)
652 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
653 struct sock *sk = sock->sk;
654 struct tipc_sock *tsk = tipc_sk(sk);
655 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
657 memset(addr, 0, sizeof(*addr));
659 if ((!tipc_sk_connected(sk)) &&
660 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
662 addr->addr.id.ref = tsk_peer_port(tsk);
663 addr->addr.id.node = tsk_peer_node(tsk);
665 addr->addr.id.ref = tsk->portid;
666 addr->addr.id.node = tn->own_addr;
669 *uaddr_len = sizeof(*addr);
670 addr->addrtype = TIPC_ADDR_ID;
671 addr->family = AF_TIPC;
673 addr->addr.name.domain = 0;
679 * tipc_poll - read and possibly block on pollmask
680 * @file: file structure associated with the socket
681 * @sock: socket for which to calculate the poll bits
684 * Returns pollmask value
687 * It appears that the usual socket locking mechanisms are not useful here
688 * since the pollmask info is potentially out-of-date the moment this routine
689 * exits. TCP and other protocols seem to rely on higher level poll routines
690 * to handle any preventable race conditions, so TIPC will do the same ...
692 * IMPORTANT: The fact that a read or write operation is indicated does NOT
693 * imply that the operation will succeed, merely that it should be performed
694 * and will not block.
696 static unsigned int tipc_poll(struct file *file, struct socket *sock,
699 struct sock *sk = sock->sk;
700 struct tipc_sock *tsk = tipc_sk(sk);
703 sock_poll_wait(file, sk_sleep(sk), wait);
705 if (sk->sk_shutdown & RCV_SHUTDOWN)
706 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
707 if (sk->sk_shutdown == SHUTDOWN_MASK)
710 switch (sk->sk_state) {
711 case TIPC_ESTABLISHED:
712 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
716 case TIPC_CONNECTING:
717 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
718 mask |= (POLLIN | POLLRDNORM);
721 if (!tsk->cong_link_cnt)
723 if (tipc_sk_type_connectionless(sk) &&
724 (!skb_queue_empty_lockless(&sk->sk_receive_queue)))
725 mask |= (POLLIN | POLLRDNORM);
727 case TIPC_DISCONNECTING:
728 mask = (POLLIN | POLLRDNORM | POLLHUP);
736 * tipc_sendmcast - send multicast message
737 * @sock: socket structure
738 * @seq: destination address
739 * @msg: message to send
740 * @dlen: length of data to send
741 * @timeout: timeout to wait for wakeup
743 * Called from function tipc_sendmsg(), which has done all sanity checks
744 * Returns the number of bytes sent on success, or errno
746 static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
747 struct msghdr *msg, size_t dlen, long timeout)
749 struct sock *sk = sock->sk;
750 struct tipc_sock *tsk = tipc_sk(sk);
751 struct tipc_msg *hdr = &tsk->phdr;
752 struct net *net = sock_net(sk);
753 int mtu = tipc_bcast_get_mtu(net);
754 struct tipc_mc_method *method = &tsk->mc_method;
755 u32 domain = addr_domain(net, TIPC_CLUSTER_SCOPE);
756 struct sk_buff_head pkts;
757 struct tipc_nlist dsts;
760 /* Block or return if any destination link is congested */
761 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
765 /* Lookup destination nodes */
766 tipc_nlist_init(&dsts, tipc_own_addr(net));
767 tipc_nametbl_lookup_dst_nodes(net, seq->type, seq->lower,
768 seq->upper, domain, &dsts);
769 if (!dsts.local && !dsts.remote)
770 return -EHOSTUNREACH;
772 /* Build message header */
773 msg_set_type(hdr, TIPC_MCAST_MSG);
774 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
775 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
776 msg_set_destport(hdr, 0);
777 msg_set_destnode(hdr, 0);
778 msg_set_nametype(hdr, seq->type);
779 msg_set_namelower(hdr, seq->lower);
780 msg_set_nameupper(hdr, seq->upper);
782 /* Build message as chain of buffers */
783 skb_queue_head_init(&pkts);
784 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
786 /* Send message if build was successful */
787 if (unlikely(rc == dlen))
788 rc = tipc_mcast_xmit(net, &pkts, method, &dsts,
789 &tsk->cong_link_cnt);
791 tipc_nlist_purge(&dsts);
793 return rc ? rc : dlen;
797 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
798 * @arrvq: queue with arriving messages, to be cloned after destination lookup
799 * @inputq: queue with cloned messages, delivered to socket after dest lookup
801 * Multi-threaded: parallel calls with reference to same queues may occur
803 void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
804 struct sk_buff_head *inputq)
806 struct tipc_msg *msg;
807 struct list_head dports;
809 u32 scope = TIPC_CLUSTER_SCOPE;
810 struct sk_buff_head tmpq;
812 struct sk_buff *skb, *_skb;
814 __skb_queue_head_init(&tmpq);
815 INIT_LIST_HEAD(&dports);
817 skb = tipc_skb_peek(arrvq, &inputq->lock);
818 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
820 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
822 if (in_own_node(net, msg_orignode(msg)))
823 scope = TIPC_NODE_SCOPE;
825 /* Create destination port list and message clones: */
826 tipc_nametbl_mc_translate(net,
827 msg_nametype(msg), msg_namelower(msg),
828 msg_nameupper(msg), scope, &dports);
829 portid = u32_pop(&dports);
830 for (; portid; portid = u32_pop(&dports)) {
831 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
833 msg_set_destport(buf_msg(_skb), portid);
834 __skb_queue_tail(&tmpq, _skb);
837 pr_warn("Failed to clone mcast rcv buffer\n");
839 /* Append to inputq if not already done by other thread */
840 spin_lock_bh(&inputq->lock);
841 if (skb_peek(arrvq) == skb) {
842 skb_queue_splice_tail_init(&tmpq, inputq);
843 /* Decrease the skb's refcnt as increasing in the
844 * function tipc_skb_peek
846 kfree_skb(__skb_dequeue(arrvq));
848 spin_unlock_bh(&inputq->lock);
849 __skb_queue_purge(&tmpq);
852 tipc_sk_rcv(net, inputq);
856 * tipc_sk_proto_rcv - receive a connection mng protocol message
857 * @tsk: receiving socket
858 * @skb: pointer to message buffer.
860 static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
861 struct sk_buff_head *xmitq)
863 struct sock *sk = &tsk->sk;
864 u32 onode = tsk_own_node(tsk);
865 struct tipc_msg *hdr = buf_msg(skb);
866 int mtyp = msg_type(hdr);
869 /* Ignore if connection cannot be validated: */
870 if (!tsk_peer_msg(tsk, hdr))
873 if (unlikely(msg_errcode(hdr))) {
874 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
875 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
877 sk->sk_state_change(sk);
881 tsk->probe_unacked = false;
883 if (mtyp == CONN_PROBE) {
884 msg_set_type(hdr, CONN_PROBE_REPLY);
885 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
886 __skb_queue_tail(xmitq, skb);
888 } else if (mtyp == CONN_ACK) {
889 conn_cong = tsk_conn_cong(tsk);
890 tsk->snt_unacked -= msg_conn_ack(hdr);
891 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
892 tsk->snd_win = msg_adv_win(hdr);
894 sk->sk_write_space(sk);
895 } else if (mtyp != CONN_PROBE_REPLY) {
896 pr_warn("Received unknown CONN_PROTO msg\n");
903 * tipc_sendmsg - send message in connectionless manner
904 * @sock: socket structure
905 * @m: message to send
906 * @dsz: amount of user data to be sent
908 * Message must have an destination specified explicitly.
909 * Used for SOCK_RDM and SOCK_DGRAM messages,
910 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
911 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
913 * Returns the number of bytes sent on success, or errno otherwise
915 static int tipc_sendmsg(struct socket *sock,
916 struct msghdr *m, size_t dsz)
918 struct sock *sk = sock->sk;
922 ret = __tipc_sendmsg(sock, m, dsz);
928 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
930 struct sock *sk = sock->sk;
931 struct net *net = sock_net(sk);
932 struct tipc_sock *tsk = tipc_sk(sk);
933 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
934 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
935 struct list_head *clinks = &tsk->cong_links;
936 bool syn = !tipc_sk_type_connectionless(sk);
937 struct tipc_msg *hdr = &tsk->phdr;
938 struct tipc_name_seq *seq;
939 struct sk_buff_head pkts;
940 u32 type, inst, domain;
944 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
947 if (unlikely(!dest)) {
949 if (!syn && dest->family != AF_TIPC)
950 return -EDESTADDRREQ;
953 if (unlikely(m->msg_namelen < sizeof(*dest)))
956 if (unlikely(dest->family != AF_TIPC))
960 if (sk->sk_state == TIPC_LISTEN)
962 if (sk->sk_state != TIPC_OPEN)
966 if (dest->addrtype == TIPC_ADDR_NAME) {
967 tsk->conn_type = dest->addr.name.name.type;
968 tsk->conn_instance = dest->addr.name.name.instance;
972 seq = &dest->addr.nameseq;
973 if (dest->addrtype == TIPC_ADDR_MCAST)
974 return tipc_sendmcast(sock, seq, m, dlen, timeout);
976 if (dest->addrtype == TIPC_ADDR_NAME) {
977 type = dest->addr.name.name.type;
978 inst = dest->addr.name.name.instance;
979 domain = dest->addr.name.domain;
981 msg_set_type(hdr, TIPC_NAMED_MSG);
982 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
983 msg_set_nametype(hdr, type);
984 msg_set_nameinst(hdr, inst);
985 msg_set_lookup_scope(hdr, tipc_addr_scope(domain));
986 dport = tipc_nametbl_translate(net, type, inst, &dnode);
987 msg_set_destnode(hdr, dnode);
988 msg_set_destport(hdr, dport);
989 if (unlikely(!dport && !dnode))
990 return -EHOSTUNREACH;
992 } else if (dest->addrtype == TIPC_ADDR_ID) {
993 dnode = dest->addr.id.node;
994 msg_set_type(hdr, TIPC_DIRECT_MSG);
995 msg_set_lookup_scope(hdr, 0);
996 msg_set_destnode(hdr, dnode);
997 msg_set_destport(hdr, dest->addr.id.ref);
998 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
1001 /* Block or return if destination link is congested */
1002 rc = tipc_wait_for_cond(sock, &timeout, !u32_find(clinks, dnode));
1006 skb_queue_head_init(&pkts);
1007 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
1008 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1009 if (unlikely(rc != dlen))
1012 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1013 if (unlikely(rc == -ELINKCONG)) {
1014 u32_push(clinks, dnode);
1015 tsk->cong_link_cnt++;
1019 if (unlikely(syn && !rc))
1020 tipc_set_sk_state(sk, TIPC_CONNECTING);
1022 return rc ? rc : dlen;
1026 * tipc_sendstream - send stream-oriented data
1027 * @sock: socket structure
1029 * @dsz: total length of data to be transmitted
1031 * Used for SOCK_STREAM data.
1033 * Returns the number of bytes sent on success (or partial success),
1034 * or errno if no data sent
1036 static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
1038 struct sock *sk = sock->sk;
1042 ret = __tipc_sendstream(sock, m, dsz);
1048 static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
1050 struct sock *sk = sock->sk;
1051 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1052 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1053 struct tipc_sock *tsk = tipc_sk(sk);
1054 struct tipc_msg *hdr = &tsk->phdr;
1055 struct net *net = sock_net(sk);
1056 struct sk_buff_head pkts;
1057 u32 dnode = tsk_peer_node(tsk);
1061 skb_queue_head_init(&pkts);
1063 if (unlikely(dlen > INT_MAX))
1066 /* Handle implicit connection setup */
1067 if (unlikely(dest)) {
1068 rc = __tipc_sendmsg(sock, m, dlen);
1069 if (dlen && dlen == rc) {
1070 tsk->peer_caps = tipc_node_get_capabilities(net, dnode);
1071 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
1077 rc = tipc_wait_for_cond(sock, &timeout,
1078 (!tsk->cong_link_cnt &&
1079 !tsk_conn_cong(tsk) &&
1080 tipc_sk_connected(sk)));
1084 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
1085 rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
1086 if (unlikely(rc != send))
1089 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1090 if (unlikely(rc == -ELINKCONG)) {
1091 tsk->cong_link_cnt = 1;
1095 tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
1098 } while (sent < dlen && !rc);
1100 return sent ? sent : rc;
1104 * tipc_send_packet - send a connection-oriented message
1105 * @sock: socket structure
1106 * @m: message to send
1107 * @dsz: length of data to be transmitted
1109 * Used for SOCK_SEQPACKET messages.
1111 * Returns the number of bytes sent on success, or errno otherwise
1113 static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
1115 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1118 return tipc_sendstream(sock, m, dsz);
1121 /* tipc_sk_finish_conn - complete the setup of a connection
1123 static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
1126 struct sock *sk = &tsk->sk;
1127 struct net *net = sock_net(sk);
1128 struct tipc_msg *msg = &tsk->phdr;
1130 msg_set_destnode(msg, peer_node);
1131 msg_set_destport(msg, peer_port);
1132 msg_set_type(msg, TIPC_CONN_MSG);
1133 msg_set_lookup_scope(msg, 0);
1134 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1136 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
1137 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
1138 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1139 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
1140 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
1141 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1144 /* Fall back to message based flow control */
1145 tsk->rcv_win = FLOWCTL_MSG_WIN;
1146 tsk->snd_win = FLOWCTL_MSG_WIN;
1150 * set_orig_addr - capture sender's address for received message
1151 * @m: descriptor for message info
1152 * @msg: received message header
1154 * Note: Address is not captured if not requested by receiver.
1156 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
1158 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
1161 addr->family = AF_TIPC;
1162 addr->addrtype = TIPC_ADDR_ID;
1163 memset(&addr->addr, 0, sizeof(addr->addr));
1164 addr->addr.id.ref = msg_origport(msg);
1165 addr->addr.id.node = msg_orignode(msg);
1166 addr->addr.name.domain = 0; /* could leave uninitialized */
1167 addr->scope = 0; /* could leave uninitialized */
1168 m->msg_namelen = sizeof(struct sockaddr_tipc);
1173 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
1174 * @m: descriptor for message info
1175 * @msg: received message header
1176 * @tsk: TIPC port associated with message
1178 * Note: Ancillary data is not captured if not requested by receiver.
1180 * Returns 0 if successful, otherwise errno
1182 static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1183 struct tipc_sock *tsk)
1191 if (likely(m->msg_controllen == 0))
1194 /* Optionally capture errored message object(s) */
1195 err = msg ? msg_errcode(msg) : 0;
1196 if (unlikely(err)) {
1198 anc_data[1] = msg_data_sz(msg);
1199 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1203 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1210 /* Optionally capture message destination object */
1211 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1212 switch (dest_type) {
1213 case TIPC_NAMED_MSG:
1215 anc_data[0] = msg_nametype(msg);
1216 anc_data[1] = msg_namelower(msg);
1217 anc_data[2] = msg_namelower(msg);
1219 case TIPC_MCAST_MSG:
1221 anc_data[0] = msg_nametype(msg);
1222 anc_data[1] = msg_namelower(msg);
1223 anc_data[2] = msg_nameupper(msg);
1226 has_name = (tsk->conn_type != 0);
1227 anc_data[0] = tsk->conn_type;
1228 anc_data[1] = tsk->conn_instance;
1229 anc_data[2] = tsk->conn_instance;
1235 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1243 static void tipc_sk_send_ack(struct tipc_sock *tsk)
1245 struct sock *sk = &tsk->sk;
1246 struct net *net = sock_net(sk);
1247 struct sk_buff *skb = NULL;
1248 struct tipc_msg *msg;
1249 u32 peer_port = tsk_peer_port(tsk);
1250 u32 dnode = tsk_peer_node(tsk);
1252 if (!tipc_sk_connected(sk))
1254 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1255 dnode, tsk_own_node(tsk), peer_port,
1256 tsk->portid, TIPC_OK);
1260 msg_set_conn_ack(msg, tsk->rcv_unacked);
1261 tsk->rcv_unacked = 0;
1263 /* Adjust to and advertize the correct window limit */
1264 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1265 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1266 msg_set_adv_win(msg, tsk->rcv_win);
1268 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
1271 static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1273 struct sock *sk = sock->sk;
1275 long timeo = *timeop;
1276 int err = sock_error(sk);
1282 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1283 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
1284 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1289 timeo = schedule_timeout(timeo);
1293 if (!skb_queue_empty(&sk->sk_receive_queue))
1298 err = sock_intr_errno(timeo);
1299 if (signal_pending(current))
1302 err = sock_error(sk);
1306 finish_wait(sk_sleep(sk), &wait);
1312 * tipc_recvmsg - receive packet-oriented message
1313 * @m: descriptor for message info
1314 * @buflen: length of user buffer area
1315 * @flags: receive flags
1317 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1318 * If the complete message doesn't fit in user area, truncate it.
1320 * Returns size of returned message data, errno otherwise
1322 static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
1323 size_t buflen, int flags)
1325 struct sock *sk = sock->sk;
1326 struct tipc_sock *tsk = tipc_sk(sk);
1327 struct sk_buff *skb;
1328 struct tipc_msg *hdr;
1329 bool connected = !tipc_sk_type_connectionless(sk);
1330 int rc, err, hlen, dlen, copy;
1333 /* Catch invalid receive requests */
1334 if (unlikely(!buflen))
1338 if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
1342 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1345 /* Look at first msg in receive queue; wait if necessary */
1346 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1349 skb = skb_peek(&sk->sk_receive_queue);
1351 dlen = msg_data_sz(hdr);
1352 hlen = msg_hdr_sz(hdr);
1353 err = msg_errcode(hdr);
1354 if (likely(dlen || err))
1356 tsk_advance_rx_queue(sk);
1359 /* Collect msg meta data, including error code and rejected data */
1360 set_orig_addr(m, hdr);
1361 rc = tipc_sk_anc_data_recv(m, hdr, tsk);
1365 /* Capture data if non-error msg, otherwise just set return value */
1367 copy = min_t(int, dlen, buflen);
1368 if (unlikely(copy != dlen))
1369 m->msg_flags |= MSG_TRUNC;
1370 rc = skb_copy_datagram_msg(skb, hlen, m, copy);
1374 if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control)
1380 /* Caption of data or error code/rejected data was successful */
1381 if (unlikely(flags & MSG_PEEK))
1384 tsk_advance_rx_queue(sk);
1385 if (likely(!connected))
1388 /* Send connection flow control ack when applicable */
1389 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1390 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
1391 tipc_sk_send_ack(tsk);
1394 return rc ? rc : copy;
1398 * tipc_recvstream - receive stream-oriented data
1399 * @m: descriptor for message info
1400 * @buflen: total size of user buffer area
1401 * @flags: receive flags
1403 * Used for SOCK_STREAM messages only. If not enough data is available
1404 * will optionally wait for more; never truncates data.
1406 * Returns size of returned message data, errno otherwise
1408 static int tipc_recvstream(struct socket *sock, struct msghdr *m,
1409 size_t buflen, int flags)
1411 struct sock *sk = sock->sk;
1412 struct tipc_sock *tsk = tipc_sk(sk);
1413 struct sk_buff *skb;
1414 struct tipc_msg *hdr;
1415 struct tipc_skb_cb *skb_cb;
1416 bool peek = flags & MSG_PEEK;
1417 int offset, required, copy, copied = 0;
1418 int hlen, dlen, err, rc;
1421 /* Catch invalid receive attempts */
1422 if (unlikely(!buflen))
1427 if (unlikely(sk->sk_state == TIPC_OPEN)) {
1431 required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
1432 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1435 /* Look at first msg in receive queue; wait if necessary */
1436 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1439 skb = skb_peek(&sk->sk_receive_queue);
1440 skb_cb = TIPC_SKB_CB(skb);
1442 dlen = msg_data_sz(hdr);
1443 hlen = msg_hdr_sz(hdr);
1444 err = msg_errcode(hdr);
1446 /* Discard any empty non-errored (SYN-) message */
1447 if (unlikely(!dlen && !err)) {
1448 tsk_advance_rx_queue(sk);
1452 /* Collect msg meta data, incl. error code and rejected data */
1454 set_orig_addr(m, hdr);
1455 rc = tipc_sk_anc_data_recv(m, hdr, tsk);
1460 /* Copy data if msg ok, otherwise return error/partial data */
1462 offset = skb_cb->bytes_read;
1463 copy = min_t(int, dlen - offset, buflen - copied);
1464 rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
1469 if (unlikely(offset < dlen)) {
1471 skb_cb->bytes_read = offset;
1476 if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control)
1485 tsk_advance_rx_queue(sk);
1487 /* Send connection flow control advertisement when applicable */
1488 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1489 if (unlikely(tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE))
1490 tipc_sk_send_ack(tsk);
1492 /* Exit if all requested data or FIN/error received */
1493 if (copied == buflen || err)
1496 } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required);
1499 return copied ? copied : rc;
1503 * tipc_write_space - wake up thread if port congestion is released
1506 static void tipc_write_space(struct sock *sk)
1508 struct socket_wq *wq;
1511 wq = rcu_dereference(sk->sk_wq);
1512 if (skwq_has_sleeper(wq))
1513 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1514 POLLWRNORM | POLLWRBAND);
1519 * tipc_data_ready - wake up threads to indicate messages have been received
1521 * @len: the length of messages
1523 static void tipc_data_ready(struct sock *sk)
1525 struct socket_wq *wq;
1528 wq = rcu_dereference(sk->sk_wq);
1529 if (skwq_has_sleeper(wq))
1530 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1531 POLLRDNORM | POLLRDBAND);
1535 static void tipc_sock_destruct(struct sock *sk)
1537 __skb_queue_purge(&sk->sk_receive_queue);
1541 * filter_connect - Handle all incoming messages for a connection-based socket
1543 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
1545 * Returns true if everything ok, false otherwise
1547 static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
1549 struct sock *sk = &tsk->sk;
1550 struct net *net = sock_net(sk);
1551 struct tipc_msg *hdr = buf_msg(skb);
1552 u32 pport = msg_origport(hdr);
1553 u32 pnode = msg_orignode(hdr);
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))) {
1562 if (pport != tsk_peer_port(tsk) ||
1563 pnode != tsk_peer_node(tsk))
1566 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1567 sk->sk_err = ECONNREFUSED;
1568 sk->sk_state_change(sk);
1572 if (unlikely(msg_errcode(hdr))) {
1573 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1574 sk->sk_err = ECONNREFUSED;
1575 sk->sk_state_change(sk);
1579 if (unlikely(!msg_isdata(hdr))) {
1580 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1581 sk->sk_err = EINVAL;
1582 sk->sk_state_change(sk);
1586 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1587 msg_set_importance(&tsk->phdr, msg_importance(hdr));
1589 /* If 'ACK+' message, add to socket receive queue */
1590 if (msg_data_sz(hdr))
1593 /* If empty 'ACK-' message, wake up sleeping connect() */
1594 sk->sk_state_change(sk);
1596 /* 'ACK-' message is neither accepted nor rejected: */
1597 msg_set_dest_droppable(hdr, 1);
1601 case TIPC_DISCONNECTING:
1604 /* Accept only SYN message */
1605 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1608 case TIPC_ESTABLISHED:
1609 /* Accept only connection-based messages sent by peer */
1610 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1613 if (unlikely(msg_errcode(hdr))) {
1614 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1615 /* Let timer expire on it's own */
1616 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1618 sk->sk_state_change(sk);
1622 pr_err("Unknown sk_state %u\n", sk->sk_state);
1629 * rcvbuf_limit - get proper overload limit of socket receive queue
1633 * For connection oriented messages, irrespective of importance,
1634 * default queue limit is 2 MB.
1636 * For connectionless messages, queue limits are based on message
1637 * importance as follows:
1639 * TIPC_LOW_IMPORTANCE (2 MB)
1640 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1641 * TIPC_HIGH_IMPORTANCE (8 MB)
1642 * TIPC_CRITICAL_IMPORTANCE (16 MB)
1644 * Returns overload limit according to corresponding message importance
1646 static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
1648 struct tipc_sock *tsk = tipc_sk(sk);
1649 struct tipc_msg *hdr = buf_msg(skb);
1651 if (unlikely(!msg_connected(hdr)))
1652 return sk->sk_rcvbuf << msg_importance(hdr);
1654 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1655 return sk->sk_rcvbuf;
1657 return FLOWCTL_MSG_LIM;
1661 * filter_rcv - validate incoming message
1663 * @skb: pointer to message.
1665 * Enqueues message on receive queue if acceptable; optionally handles
1666 * disconnect indication for a connected socket.
1668 * Called with socket lock already taken
1670 * Returns true if message was added to socket receive queue, otherwise false
1672 static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1673 struct sk_buff_head *xmitq)
1675 struct tipc_sock *tsk = tipc_sk(sk);
1676 struct tipc_msg *hdr = buf_msg(skb);
1677 unsigned int limit = rcvbuf_limit(sk, skb);
1679 int usr = msg_user(hdr);
1682 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
1683 tipc_sk_proto_rcv(tsk, skb, xmitq);
1687 if (unlikely(usr == SOCK_WAKEUP)) {
1688 onode = msg_orignode(hdr);
1690 u32_del(&tsk->cong_links, onode);
1691 tsk->cong_link_cnt--;
1692 sk->sk_write_space(sk);
1696 /* Drop if illegal message type */
1697 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1702 /* Reject if wrong message type for current socket state */
1703 if (tipc_sk_type_connectionless(sk)) {
1704 if (msg_connected(hdr)) {
1705 err = TIPC_ERR_NO_PORT;
1708 } else if (unlikely(!filter_connect(tsk, skb))) {
1709 err = TIPC_ERR_NO_PORT;
1713 /* Reject message if there isn't room to queue it */
1714 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1715 err = TIPC_ERR_OVERLOAD;
1719 /* Enqueue message */
1720 TIPC_SKB_CB(skb)->bytes_read = 0;
1721 __skb_queue_tail(&sk->sk_receive_queue, skb);
1722 skb_set_owner_r(skb, sk);
1724 sk->sk_data_ready(sk);
1728 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1729 __skb_queue_tail(xmitq, skb);
1734 * tipc_backlog_rcv - handle incoming message from backlog queue
1738 * Caller must hold socket lock
1742 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
1744 unsigned int truesize = skb->truesize;
1745 struct sk_buff_head xmitq;
1746 u32 dnode, selector;
1748 __skb_queue_head_init(&xmitq);
1750 if (likely(filter_rcv(sk, skb, &xmitq))) {
1751 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
1755 if (skb_queue_empty(&xmitq))
1758 /* Send response/rejected message */
1759 skb = __skb_dequeue(&xmitq);
1760 dnode = msg_destnode(buf_msg(skb));
1761 selector = msg_origport(buf_msg(skb));
1762 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
1767 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1768 * inputq and try adding them to socket or backlog queue
1769 * @inputq: list of incoming buffers with potentially different destinations
1770 * @sk: socket where the buffers should be enqueued
1771 * @dport: port number for the socket
1773 * Caller must hold socket lock
1775 static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
1776 u32 dport, struct sk_buff_head *xmitq)
1778 unsigned long time_limit = jiffies + usecs_to_jiffies(20000);
1779 struct sk_buff *skb;
1784 while (skb_queue_len(inputq)) {
1785 if (unlikely(time_after_eq(jiffies, time_limit)))
1788 skb = tipc_skb_dequeue(inputq, dport);
1792 /* Add message directly to receive queue if possible */
1793 if (!sock_owned_by_user(sk)) {
1794 filter_rcv(sk, skb, xmitq);
1798 /* Try backlog, compensating for double-counted bytes */
1799 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
1800 if (!sk->sk_backlog.len)
1801 atomic_set(dcnt, 0);
1802 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1803 if (likely(!sk_add_backlog(sk, skb, lim)))
1806 /* Overload => reject message back to sender */
1807 onode = tipc_own_addr(sock_net(sk));
1808 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1809 __skb_queue_tail(xmitq, skb);
1815 * tipc_sk_rcv - handle a chain of incoming buffers
1816 * @inputq: buffer list containing the buffers
1817 * Consumes all buffers in list until inputq is empty
1818 * Note: may be called in multiple threads referring to the same queue
1820 void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
1822 struct sk_buff_head xmitq;
1823 u32 dnode, dport = 0;
1825 struct tipc_sock *tsk;
1827 struct sk_buff *skb;
1829 __skb_queue_head_init(&xmitq);
1830 while (skb_queue_len(inputq)) {
1831 dport = tipc_skb_peek_port(inputq, dport);
1832 tsk = tipc_sk_lookup(net, dport);
1836 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
1837 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
1838 spin_unlock_bh(&sk->sk_lock.slock);
1840 /* Send pending response/rejected messages, if any */
1841 while ((skb = __skb_dequeue(&xmitq))) {
1842 dnode = msg_destnode(buf_msg(skb));
1843 tipc_node_xmit_skb(net, skb, dnode, dport);
1849 /* No destination socket => dequeue skb if still there */
1850 skb = tipc_skb_dequeue(inputq, dport);
1854 /* Try secondary lookup if unresolved named message */
1855 err = TIPC_ERR_NO_PORT;
1856 if (tipc_msg_lookup_dest(net, skb, &err))
1859 /* Prepare for message rejection */
1860 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
1863 dnode = msg_destnode(buf_msg(skb));
1864 tipc_node_xmit_skb(net, skb, dnode, dport);
1868 static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1870 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1871 struct sock *sk = sock->sk;
1875 int err = sock_error(sk);
1880 if (signal_pending(current))
1881 return sock_intr_errno(*timeo_p);
1883 add_wait_queue(sk_sleep(sk), &wait);
1884 done = sk_wait_event(sk, timeo_p,
1885 sk->sk_state != TIPC_CONNECTING, &wait);
1886 remove_wait_queue(sk_sleep(sk), &wait);
1892 * tipc_connect - establish a connection to another TIPC port
1893 * @sock: socket structure
1894 * @dest: socket address for destination port
1895 * @destlen: size of socket address data structure
1896 * @flags: file-related flags associated with socket
1898 * Returns 0 on success, errno otherwise
1900 static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1901 int destlen, int flags)
1903 struct sock *sk = sock->sk;
1904 struct tipc_sock *tsk = tipc_sk(sk);
1905 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1906 struct msghdr m = {NULL,};
1907 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
1913 /* DGRAM/RDM connect(), just save the destaddr */
1914 if (tipc_sk_type_connectionless(sk)) {
1915 if (dst->family == AF_UNSPEC) {
1916 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
1917 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1920 memcpy(&tsk->peer, dest, destlen);
1926 * Reject connection attempt using multicast address
1928 * Note: send_msg() validates the rest of the address fields,
1929 * so there's no need to do it here
1931 if (dst->addrtype == TIPC_ADDR_MCAST) {
1936 previous = sk->sk_state;
1938 switch (sk->sk_state) {
1940 /* Send a 'SYN-' to destination */
1942 m.msg_namelen = destlen;
1944 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1945 * indicate send_msg() is never blocked.
1948 m.msg_flags = MSG_DONTWAIT;
1950 res = __tipc_sendmsg(sock, &m, 0);
1951 if ((res < 0) && (res != -EWOULDBLOCK))
1954 /* Just entered TIPC_CONNECTING state; the only
1955 * difference is that return value in non-blocking
1956 * case is EINPROGRESS, rather than EALREADY.
1960 case TIPC_CONNECTING:
1962 if (previous == TIPC_CONNECTING)
1966 timeout = msecs_to_jiffies(timeout);
1967 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1968 res = tipc_wait_for_connect(sock, &timeout);
1970 case TIPC_ESTABLISHED:
1983 * tipc_listen - allow socket to listen for incoming connections
1984 * @sock: socket structure
1987 * Returns 0 on success, errno otherwise
1989 static int tipc_listen(struct socket *sock, int len)
1991 struct sock *sk = sock->sk;
1995 res = tipc_set_sk_state(sk, TIPC_LISTEN);
2001 static int tipc_wait_for_accept(struct socket *sock, long timeo)
2003 struct sock *sk = sock->sk;
2004 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2007 /* True wake-one mechanism for incoming connections: only
2008 * one process gets woken up, not the 'whole herd'.
2009 * Since we do not 'race & poll' for established sockets
2010 * anymore, the common case will execute the loop only once.
2013 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
2014 add_wait_queue(sk_sleep(sk), &wait);
2016 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
2018 remove_wait_queue(sk_sleep(sk), &wait);
2021 if (!skb_queue_empty(&sk->sk_receive_queue))
2026 err = sock_intr_errno(timeo);
2027 if (signal_pending(current))
2034 * tipc_accept - wait for connection request
2035 * @sock: listening socket
2036 * @newsock: new socket that is to be connected
2037 * @flags: file-related flags associated with socket
2039 * Returns 0 on success, errno otherwise
2041 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
2044 struct sock *new_sk, *sk = sock->sk;
2045 struct sk_buff *buf;
2046 struct tipc_sock *new_tsock;
2047 struct tipc_msg *msg;
2053 if (sk->sk_state != TIPC_LISTEN) {
2057 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2058 res = tipc_wait_for_accept(sock, timeo);
2062 buf = skb_peek(&sk->sk_receive_queue);
2064 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
2067 security_sk_clone(sock->sk, new_sock->sk);
2069 new_sk = new_sock->sk;
2070 new_tsock = tipc_sk(new_sk);
2073 /* we lock on new_sk; but lockdep sees the lock on sk */
2074 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
2077 * Reject any stray messages received by new socket
2078 * before the socket lock was taken (very, very unlikely)
2080 tsk_rej_rx_queue(new_sk);
2082 /* Connect new socket to it's peer */
2083 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
2085 tsk_set_importance(new_tsock, msg_importance(msg));
2086 if (msg_named(msg)) {
2087 new_tsock->conn_type = msg_nametype(msg);
2088 new_tsock->conn_instance = msg_nameinst(msg);
2092 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2093 * Respond to 'SYN+' by queuing it on new socket.
2095 if (!msg_data_sz(msg)) {
2096 struct msghdr m = {NULL,};
2098 tsk_advance_rx_queue(sk);
2099 __tipc_sendstream(new_sock, &m, 0);
2101 __skb_dequeue(&sk->sk_receive_queue);
2102 __skb_queue_head(&new_sk->sk_receive_queue, buf);
2103 skb_set_owner_r(buf, new_sk);
2105 release_sock(new_sk);
2112 * tipc_shutdown - shutdown socket connection
2113 * @sock: socket structure
2114 * @how: direction to close (must be SHUT_RDWR)
2116 * Terminates connection (if necessary), then purges socket's receive queue.
2118 * Returns 0 on success, errno otherwise
2120 static int tipc_shutdown(struct socket *sock, int how)
2122 struct sock *sk = sock->sk;
2125 if (how != SHUT_RDWR)
2130 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2131 sk->sk_shutdown = SHUTDOWN_MASK;
2133 if (sk->sk_state == TIPC_DISCONNECTING) {
2134 /* Discard any unreceived messages */
2135 __skb_queue_purge(&sk->sk_receive_queue);
2141 /* Wake up anyone sleeping in poll. */
2142 sk->sk_state_change(sk);
2148 static void tipc_sk_timeout(unsigned long data)
2150 struct tipc_sock *tsk = (struct tipc_sock *)data;
2151 struct sock *sk = &tsk->sk;
2152 struct sk_buff *skb = NULL;
2153 u32 peer_port, peer_node;
2154 u32 own_node = tsk_own_node(tsk);
2157 if (!tipc_sk_connected(sk)) {
2161 peer_port = tsk_peer_port(tsk);
2162 peer_node = tsk_peer_node(tsk);
2164 if (tsk->probe_unacked) {
2165 if (!sock_owned_by_user(sk)) {
2166 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2167 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2168 tsk_peer_port(tsk));
2169 sk->sk_state_change(sk);
2171 /* Try again later */
2172 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2179 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2180 INT_H_SIZE, 0, peer_node, own_node,
2181 peer_port, tsk->portid, TIPC_OK);
2182 tsk->probe_unacked = true;
2183 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
2186 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
2191 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
2192 struct tipc_name_seq const *seq)
2194 struct sock *sk = &tsk->sk;
2195 struct net *net = sock_net(sk);
2196 struct publication *publ;
2199 if (tipc_sk_connected(sk))
2201 key = tsk->portid + tsk->pub_count + 1;
2202 if (key == tsk->portid)
2205 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
2206 scope, tsk->portid, key);
2207 if (unlikely(!publ))
2210 list_add(&publ->pport_list, &tsk->publications);
2216 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
2217 struct tipc_name_seq const *seq)
2219 struct net *net = sock_net(&tsk->sk);
2220 struct publication *publ;
2221 struct publication *safe;
2224 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
2226 if (publ->scope != scope)
2228 if (publ->type != seq->type)
2230 if (publ->lower != seq->lower)
2232 if (publ->upper != seq->upper)
2234 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2235 publ->ref, publ->key);
2239 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2240 publ->ref, publ->key);
2243 if (list_empty(&tsk->publications))
2248 /* tipc_sk_reinit: set non-zero address in all existing sockets
2249 * when we go from standalone to network mode.
2251 void tipc_sk_reinit(struct net *net)
2253 struct tipc_net *tn = net_generic(net, tipc_net_id);
2254 struct rhashtable_iter iter;
2255 struct tipc_sock *tsk;
2256 struct tipc_msg *msg;
2258 rhashtable_walk_enter(&tn->sk_rht, &iter);
2261 tsk = ERR_PTR(rhashtable_walk_start(&iter));
2265 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
2266 sock_hold(&tsk->sk);
2267 rhashtable_walk_stop(&iter);
2268 lock_sock(&tsk->sk);
2270 msg_set_prevnode(msg, tn->own_addr);
2271 msg_set_orignode(msg, tn->own_addr);
2272 release_sock(&tsk->sk);
2273 rhashtable_walk_start(&iter);
2277 rhashtable_walk_stop(&iter);
2278 } while (tsk == ERR_PTR(-EAGAIN));
2280 rhashtable_walk_exit(&iter);
2283 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
2285 struct tipc_net *tn = net_generic(net, tipc_net_id);
2286 struct tipc_sock *tsk;
2289 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
2291 sock_hold(&tsk->sk);
2297 static int tipc_sk_insert(struct tipc_sock *tsk)
2299 struct sock *sk = &tsk->sk;
2300 struct net *net = sock_net(sk);
2301 struct tipc_net *tn = net_generic(net, tipc_net_id);
2302 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2303 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2305 while (remaining--) {
2307 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2308 portid = TIPC_MIN_PORT;
2309 tsk->portid = portid;
2310 sock_hold(&tsk->sk);
2311 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2320 static void tipc_sk_remove(struct tipc_sock *tsk)
2322 struct sock *sk = &tsk->sk;
2323 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
2325 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
2326 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
2331 static const struct rhashtable_params tsk_rht_params = {
2333 .head_offset = offsetof(struct tipc_sock, node),
2334 .key_offset = offsetof(struct tipc_sock, portid),
2335 .key_len = sizeof(u32), /* portid */
2336 .max_size = 1048576,
2338 .automatic_shrinking = true,
2341 int tipc_sk_rht_init(struct net *net)
2343 struct tipc_net *tn = net_generic(net, tipc_net_id);
2345 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
2348 void tipc_sk_rht_destroy(struct net *net)
2350 struct tipc_net *tn = net_generic(net, tipc_net_id);
2352 /* Wait for socket readers to complete */
2355 rhashtable_destroy(&tn->sk_rht);
2359 * tipc_setsockopt - set socket option
2360 * @sock: socket structure
2361 * @lvl: option level
2362 * @opt: option identifier
2363 * @ov: pointer to new option value
2364 * @ol: length of option value
2366 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
2367 * (to ease compatibility).
2369 * Returns 0 on success, errno otherwise
2371 static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2372 char __user *ov, unsigned int ol)
2374 struct sock *sk = sock->sk;
2375 struct tipc_sock *tsk = tipc_sk(sk);
2379 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2381 if (lvl != SOL_TIPC)
2382 return -ENOPROTOOPT;
2385 case TIPC_IMPORTANCE:
2386 case TIPC_SRC_DROPPABLE:
2387 case TIPC_DEST_DROPPABLE:
2388 case TIPC_CONN_TIMEOUT:
2389 if (ol < sizeof(value))
2391 res = get_user(value, (u32 __user *)ov);
2403 case TIPC_IMPORTANCE:
2404 res = tsk_set_importance(tsk, value);
2406 case TIPC_SRC_DROPPABLE:
2407 if (sock->type != SOCK_STREAM)
2408 tsk_set_unreliable(tsk, value);
2412 case TIPC_DEST_DROPPABLE:
2413 tsk_set_unreturnable(tsk, value);
2415 case TIPC_CONN_TIMEOUT:
2416 tipc_sk(sk)->conn_timeout = value;
2418 case TIPC_MCAST_BROADCAST:
2419 tsk->mc_method.rcast = false;
2420 tsk->mc_method.mandatory = true;
2422 case TIPC_MCAST_REPLICAST:
2423 tsk->mc_method.rcast = true;
2424 tsk->mc_method.mandatory = true;
2436 * tipc_getsockopt - get socket option
2437 * @sock: socket structure
2438 * @lvl: option level
2439 * @opt: option identifier
2440 * @ov: receptacle for option value
2441 * @ol: receptacle for length of option value
2443 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
2444 * (to ease compatibility).
2446 * Returns 0 on success, errno otherwise
2448 static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2449 char __user *ov, int __user *ol)
2451 struct sock *sk = sock->sk;
2452 struct tipc_sock *tsk = tipc_sk(sk);
2457 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2458 return put_user(0, ol);
2459 if (lvl != SOL_TIPC)
2460 return -ENOPROTOOPT;
2461 res = get_user(len, ol);
2468 case TIPC_IMPORTANCE:
2469 value = tsk_importance(tsk);
2471 case TIPC_SRC_DROPPABLE:
2472 value = tsk_unreliable(tsk);
2474 case TIPC_DEST_DROPPABLE:
2475 value = tsk_unreturnable(tsk);
2477 case TIPC_CONN_TIMEOUT:
2478 value = tsk->conn_timeout;
2479 /* no need to set "res", since already 0 at this point */
2481 case TIPC_NODE_RECVQ_DEPTH:
2482 value = 0; /* was tipc_queue_size, now obsolete */
2484 case TIPC_SOCK_RECVQ_DEPTH:
2485 value = skb_queue_len(&sk->sk_receive_queue);
2494 return res; /* "get" failed */
2496 if (len < sizeof(value))
2499 if (copy_to_user(ov, &value, sizeof(value)))
2502 return put_user(sizeof(value), ol);
2505 static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
2507 struct sock *sk = sock->sk;
2508 struct tipc_sioc_ln_req lnr;
2509 void __user *argp = (void __user *)arg;
2512 case SIOCGETLINKNAME:
2513 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2515 if (!tipc_node_get_linkname(sock_net(sk),
2516 lnr.bearer_id & 0xffff, lnr.peer,
2517 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2518 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2522 return -EADDRNOTAVAIL;
2524 return -ENOIOCTLCMD;
2528 static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
2530 struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
2531 struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
2532 u32 onode = tipc_own_addr(sock_net(sock1->sk));
2534 tsk1->peer.family = AF_TIPC;
2535 tsk1->peer.addrtype = TIPC_ADDR_ID;
2536 tsk1->peer.scope = TIPC_NODE_SCOPE;
2537 tsk1->peer.addr.id.ref = tsk2->portid;
2538 tsk1->peer.addr.id.node = onode;
2539 tsk2->peer.family = AF_TIPC;
2540 tsk2->peer.addrtype = TIPC_ADDR_ID;
2541 tsk2->peer.scope = TIPC_NODE_SCOPE;
2542 tsk2->peer.addr.id.ref = tsk1->portid;
2543 tsk2->peer.addr.id.node = onode;
2545 tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
2546 tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
2550 /* Protocol switches for the various types of TIPC sockets */
2552 static const struct proto_ops msg_ops = {
2553 .owner = THIS_MODULE,
2555 .release = tipc_release,
2557 .connect = tipc_connect,
2558 .socketpair = tipc_socketpair,
2559 .accept = sock_no_accept,
2560 .getname = tipc_getname,
2562 .ioctl = tipc_ioctl,
2563 .listen = sock_no_listen,
2564 .shutdown = tipc_shutdown,
2565 .setsockopt = tipc_setsockopt,
2566 .getsockopt = tipc_getsockopt,
2567 .sendmsg = tipc_sendmsg,
2568 .recvmsg = tipc_recvmsg,
2569 .mmap = sock_no_mmap,
2570 .sendpage = sock_no_sendpage
2573 static const struct proto_ops packet_ops = {
2574 .owner = THIS_MODULE,
2576 .release = tipc_release,
2578 .connect = tipc_connect,
2579 .socketpair = tipc_socketpair,
2580 .accept = tipc_accept,
2581 .getname = tipc_getname,
2583 .ioctl = tipc_ioctl,
2584 .listen = tipc_listen,
2585 .shutdown = tipc_shutdown,
2586 .setsockopt = tipc_setsockopt,
2587 .getsockopt = tipc_getsockopt,
2588 .sendmsg = tipc_send_packet,
2589 .recvmsg = tipc_recvmsg,
2590 .mmap = sock_no_mmap,
2591 .sendpage = sock_no_sendpage
2594 static const struct proto_ops stream_ops = {
2595 .owner = THIS_MODULE,
2597 .release = tipc_release,
2599 .connect = tipc_connect,
2600 .socketpair = tipc_socketpair,
2601 .accept = tipc_accept,
2602 .getname = tipc_getname,
2604 .ioctl = tipc_ioctl,
2605 .listen = tipc_listen,
2606 .shutdown = tipc_shutdown,
2607 .setsockopt = tipc_setsockopt,
2608 .getsockopt = tipc_getsockopt,
2609 .sendmsg = tipc_sendstream,
2610 .recvmsg = tipc_recvstream,
2611 .mmap = sock_no_mmap,
2612 .sendpage = sock_no_sendpage
2615 static const struct net_proto_family tipc_family_ops = {
2616 .owner = THIS_MODULE,
2618 .create = tipc_sk_create
2621 static struct proto tipc_proto = {
2623 .owner = THIS_MODULE,
2624 .obj_size = sizeof(struct tipc_sock),
2625 .sysctl_rmem = sysctl_tipc_rmem
2629 * tipc_socket_init - initialize TIPC socket interface
2631 * Returns 0 on success, errno otherwise
2633 int tipc_socket_init(void)
2637 res = proto_register(&tipc_proto, 1);
2639 pr_err("Failed to register TIPC protocol type\n");
2643 res = sock_register(&tipc_family_ops);
2645 pr_err("Failed to register TIPC socket type\n");
2646 proto_unregister(&tipc_proto);
2654 * tipc_socket_stop - stop TIPC socket interface
2656 void tipc_socket_stop(void)
2658 sock_unregister(tipc_family_ops.family);
2659 proto_unregister(&tipc_proto);
2662 /* Caller should hold socket lock for the passed tipc socket. */
2663 static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
2667 struct nlattr *nest;
2669 peer_node = tsk_peer_node(tsk);
2670 peer_port = tsk_peer_port(tsk);
2672 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2674 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2676 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2679 if (tsk->conn_type != 0) {
2680 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2682 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2684 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2687 nla_nest_end(skb, nest);
2692 nla_nest_cancel(skb, nest);
2697 /* Caller should hold socket lock for the passed tipc socket. */
2698 static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2699 struct tipc_sock *tsk)
2703 struct nlattr *attrs;
2704 struct net *net = sock_net(skb->sk);
2705 struct tipc_net *tn = net_generic(net, tipc_net_id);
2706 struct sock *sk = &tsk->sk;
2708 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2709 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
2713 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2715 goto genlmsg_cancel;
2716 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
2717 goto attr_msg_cancel;
2718 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
2719 goto attr_msg_cancel;
2721 if (tipc_sk_connected(sk)) {
2722 err = __tipc_nl_add_sk_con(skb, tsk);
2724 goto attr_msg_cancel;
2725 } else if (!list_empty(&tsk->publications)) {
2726 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2727 goto attr_msg_cancel;
2729 nla_nest_end(skb, attrs);
2730 genlmsg_end(skb, hdr);
2735 nla_nest_cancel(skb, attrs);
2737 genlmsg_cancel(skb, hdr);
2742 int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2745 struct tipc_sock *tsk;
2746 const struct bucket_table *tbl;
2747 struct rhash_head *pos;
2748 struct net *net = sock_net(skb->sk);
2749 struct tipc_net *tn = net_generic(net, tipc_net_id);
2750 u32 tbl_id = cb->args[0];
2751 u32 prev_portid = cb->args[1];
2754 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
2755 for (; tbl_id < tbl->size; tbl_id++) {
2756 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
2757 spin_lock_bh(&tsk->sk.sk_lock.slock);
2758 if (prev_portid && prev_portid != tsk->portid) {
2759 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2763 err = __tipc_nl_add_sk(skb, cb, tsk);
2765 prev_portid = tsk->portid;
2766 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2770 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2775 cb->args[0] = tbl_id;
2776 cb->args[1] = prev_portid;
2781 /* Caller should hold socket lock for the passed tipc socket. */
2782 static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2783 struct netlink_callback *cb,
2784 struct publication *publ)
2787 struct nlattr *attrs;
2789 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2790 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
2794 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2796 goto genlmsg_cancel;
2798 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2799 goto attr_msg_cancel;
2800 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2801 goto attr_msg_cancel;
2802 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2803 goto attr_msg_cancel;
2804 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2805 goto attr_msg_cancel;
2807 nla_nest_end(skb, attrs);
2808 genlmsg_end(skb, hdr);
2813 nla_nest_cancel(skb, attrs);
2815 genlmsg_cancel(skb, hdr);
2820 /* Caller should hold socket lock for the passed tipc socket. */
2821 static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2822 struct netlink_callback *cb,
2823 struct tipc_sock *tsk, u32 *last_publ)
2826 struct publication *p;
2829 list_for_each_entry(p, &tsk->publications, pport_list) {
2830 if (p->key == *last_publ)
2833 if (p->key != *last_publ) {
2834 /* We never set seq or call nl_dump_check_consistent()
2835 * this means that setting prev_seq here will cause the
2836 * consistence check to fail in the netlink callback
2837 * handler. Resulting in the last NLMSG_DONE message
2838 * having the NLM_F_DUMP_INTR flag set.
2845 p = list_first_entry(&tsk->publications, struct publication,
2849 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2850 err = __tipc_nl_add_sk_publ(skb, cb, p);
2852 *last_publ = p->key;
2861 int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2864 u32 tsk_portid = cb->args[0];
2865 u32 last_publ = cb->args[1];
2866 u32 done = cb->args[2];
2867 struct net *net = sock_net(skb->sk);
2868 struct tipc_sock *tsk;
2871 struct nlattr **attrs;
2872 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2874 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2878 if (!attrs[TIPC_NLA_SOCK])
2881 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2882 attrs[TIPC_NLA_SOCK],
2883 tipc_nl_sock_policy, NULL);
2887 if (!sock[TIPC_NLA_SOCK_REF])
2890 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
2896 tsk = tipc_sk_lookup(net, tsk_portid);
2900 lock_sock(&tsk->sk);
2901 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2904 release_sock(&tsk->sk);
2907 cb->args[0] = tsk_portid;
2908 cb->args[1] = last_publ;