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>
39 #include "name_table.h"
42 #include "name_distr.h"
47 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
48 #define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
49 #define TIPC_FWD_MSG 1
50 #define TIPC_MAX_PORT 0xffffffff
51 #define TIPC_MIN_PORT 1
54 TIPC_LISTEN = TCP_LISTEN,
55 TIPC_ESTABLISHED = TCP_ESTABLISHED,
56 TIPC_OPEN = TCP_CLOSE,
57 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
58 TIPC_CONNECTING = TCP_SYN_SENT,
62 * struct tipc_sock - TIPC socket structure
63 * @sk: socket - interacts with 'port' and with user via the socket API
64 * @conn_type: TIPC type used when connection was established
65 * @conn_instance: TIPC instance used when connection was established
66 * @published: non-zero if port has one or more associated names
67 * @max_pkt: maximum packet size "hint" used when building messages sent by port
68 * @portid: unique port identity in TIPC socket hash table
69 * @phdr: preformatted message header used when sending messages
70 * @publications: list of publications for port
71 * @pub_count: total # of publications port has made during its lifetime
73 * @conn_timeout: the time we can wait for an unresponded setup request
74 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
75 * @link_cong: non-zero if owner must sleep because of link congestion
76 * @sent_unacked: # messages sent by socket, and not yet acked by peer
77 * @rcv_unacked: # messages read by user, but not yet acked back to peer
78 * @peer: 'connected' peer for dgram/rdm
79 * @node: hash table node
80 * @rcu: rcu struct for tipc_sock
90 struct list_head sock_list;
91 struct list_head publications;
102 struct sockaddr_tipc peer;
103 struct rhash_head node;
107 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
108 static void tipc_data_ready(struct sock *sk);
109 static void tipc_write_space(struct sock *sk);
110 static void tipc_sock_destruct(struct sock *sk);
111 static int tipc_release(struct socket *sock);
112 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
113 static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
114 static void tipc_sk_timeout(unsigned long data);
115 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
116 struct tipc_name_seq const *seq);
117 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
118 struct tipc_name_seq const *seq);
119 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
120 static int tipc_sk_insert(struct tipc_sock *tsk);
121 static void tipc_sk_remove(struct tipc_sock *tsk);
122 static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
124 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
126 static const struct proto_ops packet_ops;
127 static const struct proto_ops stream_ops;
128 static const struct proto_ops msg_ops;
129 static struct proto tipc_proto;
130 static const struct rhashtable_params tsk_rht_params;
132 static u32 tsk_own_node(struct tipc_sock *tsk)
134 return msg_prevnode(&tsk->phdr);
137 static u32 tsk_peer_node(struct tipc_sock *tsk)
139 return msg_destnode(&tsk->phdr);
142 static u32 tsk_peer_port(struct tipc_sock *tsk)
144 return msg_destport(&tsk->phdr);
147 static bool tsk_unreliable(struct tipc_sock *tsk)
149 return msg_src_droppable(&tsk->phdr) != 0;
152 static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
154 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
157 static bool tsk_unreturnable(struct tipc_sock *tsk)
159 return msg_dest_droppable(&tsk->phdr) != 0;
162 static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
164 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
167 static int tsk_importance(struct tipc_sock *tsk)
169 return msg_importance(&tsk->phdr);
172 static int tsk_set_importance(struct tipc_sock *tsk, int imp)
174 if (imp > TIPC_CRITICAL_IMPORTANCE)
176 msg_set_importance(&tsk->phdr, (u32)imp);
180 static struct tipc_sock *tipc_sk(const struct sock *sk)
182 return container_of(sk, struct tipc_sock, sk);
185 static bool tsk_conn_cong(struct tipc_sock *tsk)
187 return tsk->snt_unacked > tsk->snd_win;
190 /* tsk_blocks(): translate a buffer size in bytes to number of
191 * advertisable blocks, taking into account the ratio truesize(len)/len
192 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
194 static u16 tsk_adv_blocks(int len)
196 return len / FLOWCTL_BLK_SZ / 4;
199 /* tsk_inc(): increment counter for sent or received data
200 * - If block based flow control is not supported by peer we
201 * fall back to message based ditto, incrementing the counter
203 static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
205 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
206 return ((msglen / FLOWCTL_BLK_SZ) + 1);
211 * tsk_advance_rx_queue - discard first buffer in socket receive queue
213 * Caller must hold socket lock
215 static void tsk_advance_rx_queue(struct sock *sk)
217 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
220 /* tipc_sk_respond() : send response message back to sender
222 static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
226 u32 onode = tipc_own_addr(sock_net(sk));
228 if (!tipc_msg_reverse(onode, &skb, err))
231 dnode = msg_destnode(buf_msg(skb));
232 selector = msg_origport(buf_msg(skb));
233 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
237 * tsk_rej_rx_queue - reject all buffers in socket receive queue
239 * Caller must hold socket lock
241 static void tsk_rej_rx_queue(struct sock *sk)
245 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
246 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
249 static bool tipc_sk_connected(struct sock *sk)
251 return sk->sk_state == TIPC_ESTABLISHED;
254 /* tipc_sk_type_connectionless - check if the socket is datagram socket
257 * Returns true if connection less, false otherwise
259 static bool tipc_sk_type_connectionless(struct sock *sk)
261 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
264 /* tsk_peer_msg - verify if message was sent by connected port's peer
266 * Handles cases where the node's network address has changed from
267 * the default of <0.0.0> to its configured setting.
269 static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
271 struct sock *sk = &tsk->sk;
272 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
273 u32 peer_port = tsk_peer_port(tsk);
277 if (unlikely(!tipc_sk_connected(sk)))
280 if (unlikely(msg_origport(msg) != peer_port))
283 orig_node = msg_orignode(msg);
284 peer_node = tsk_peer_node(tsk);
286 if (likely(orig_node == peer_node))
289 if (!orig_node && (peer_node == tn->own_addr))
292 if (!peer_node && (orig_node == tn->own_addr))
298 /* tipc_set_sk_state - set the sk_state of the socket
301 * Caller must hold socket lock
303 * Returns 0 on success, errno otherwise
305 static int tipc_set_sk_state(struct sock *sk, int state)
307 int oldsk_state = sk->sk_state;
315 case TIPC_CONNECTING:
316 if (oldsk_state == TIPC_OPEN)
319 case TIPC_ESTABLISHED:
320 if (oldsk_state == TIPC_CONNECTING ||
321 oldsk_state == TIPC_OPEN)
324 case TIPC_DISCONNECTING:
325 if (oldsk_state == TIPC_CONNECTING ||
326 oldsk_state == TIPC_ESTABLISHED)
332 sk->sk_state = state;
338 * tipc_sk_create - create a TIPC socket
339 * @net: network namespace (must be default network)
340 * @sock: pre-allocated socket structure
341 * @protocol: protocol indicator (must be 0)
342 * @kern: caused by kernel or by userspace?
344 * This routine creates additional data structures used by the TIPC socket,
345 * initializes them, and links them together.
347 * Returns 0 on success, errno otherwise
349 static int tipc_sk_create(struct net *net, struct socket *sock,
350 int protocol, int kern)
353 const struct proto_ops *ops;
355 struct tipc_sock *tsk;
356 struct tipc_msg *msg;
358 /* Validate arguments */
359 if (unlikely(protocol != 0))
360 return -EPROTONOSUPPORT;
362 switch (sock->type) {
377 /* Allocate socket's protocol area */
378 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
383 tsk->max_pkt = MAX_PKT_DEFAULT;
384 INIT_LIST_HEAD(&tsk->publications);
386 tn = net_generic(sock_net(sk), tipc_net_id);
387 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
390 /* Finish initializing socket data structures */
392 sock_init_data(sock, sk);
393 tipc_set_sk_state(sk, TIPC_OPEN);
394 if (tipc_sk_insert(tsk)) {
395 pr_warn("Socket create failed; port number exhausted\n");
398 msg_set_origport(msg, tsk->portid);
399 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
401 sk->sk_backlog_rcv = tipc_backlog_rcv;
402 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
403 sk->sk_data_ready = tipc_data_ready;
404 sk->sk_write_space = tipc_write_space;
405 sk->sk_destruct = tipc_sock_destruct;
406 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
407 atomic_set(&tsk->dupl_rcvcnt, 0);
409 /* Start out with safe limits until we receive an advertised window */
410 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
411 tsk->rcv_win = tsk->snd_win;
413 if (tipc_sk_type_connectionless(sk)) {
414 tsk_set_unreturnable(tsk, true);
415 if (sock->type == SOCK_DGRAM)
416 tsk_set_unreliable(tsk, true);
422 static void tipc_sk_callback(struct rcu_head *head)
424 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
429 /* Caller should hold socket lock for the socket. */
430 static void __tipc_shutdown(struct socket *sock, int error)
432 struct sock *sk = sock->sk;
433 struct tipc_sock *tsk = tipc_sk(sk);
434 struct net *net = sock_net(sk);
435 u32 dnode = tsk_peer_node(tsk);
438 /* Reject all unreceived messages, except on an active connection
439 * (which disconnects locally & sends a 'FIN+' to peer).
441 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
442 if (TIPC_SKB_CB(skb)->bytes_read) {
445 if (!tipc_sk_type_connectionless(sk) &&
446 sk->sk_state != TIPC_DISCONNECTING) {
447 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
448 tipc_node_remove_conn(net, dnode, tsk->portid);
450 tipc_sk_respond(sk, skb, error);
453 if (sk->sk_state != TIPC_DISCONNECTING) {
454 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
455 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
456 tsk_own_node(tsk), tsk_peer_port(tsk),
459 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
460 if (!tipc_sk_type_connectionless(sk)) {
461 tipc_node_remove_conn(net, dnode, tsk->portid);
462 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
468 * tipc_release - destroy a TIPC socket
469 * @sock: socket to destroy
471 * This routine cleans up any messages that are still queued on the socket.
472 * For DGRAM and RDM socket types, all queued messages are rejected.
473 * For SEQPACKET and STREAM socket types, the first message is rejected
474 * and any others are discarded. (If the first message on a STREAM socket
475 * is partially-read, it is discarded and the next one is rejected instead.)
477 * NOTE: Rejected messages are not necessarily returned to the sender! They
478 * are returned or discarded according to the "destination droppable" setting
479 * specified for the message by the sender.
481 * Returns 0 on success, errno otherwise
483 static int tipc_release(struct socket *sock)
485 struct sock *sk = sock->sk;
486 struct tipc_sock *tsk;
489 * Exit if socket isn't fully initialized (occurs when a failed accept()
490 * releases a pre-allocated child socket that was never used)
498 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
499 sk->sk_shutdown = SHUTDOWN_MASK;
500 tipc_sk_withdraw(tsk, 0, NULL);
501 sk_stop_timer(sk, &sk->sk_timer);
504 /* Reject any messages that accumulated in backlog queue */
507 call_rcu(&tsk->rcu, tipc_sk_callback);
514 * tipc_bind - associate or disassocate TIPC name(s) with a socket
515 * @sock: socket structure
516 * @uaddr: socket address describing name(s) and desired operation
517 * @uaddr_len: size of socket address data structure
519 * Name and name sequence binding is indicated using a positive scope value;
520 * a negative scope value unbinds the specified name. Specifying no name
521 * (i.e. a socket address length of 0) unbinds all names from the socket.
523 * Returns 0 on success, errno otherwise
525 * NOTE: This routine doesn't need to take the socket lock since it doesn't
526 * access any non-constant socket information.
528 static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
531 struct sock *sk = sock->sk;
532 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
533 struct tipc_sock *tsk = tipc_sk(sk);
537 if (unlikely(!uaddr_len)) {
538 res = tipc_sk_withdraw(tsk, 0, NULL);
542 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
546 if (addr->family != AF_TIPC) {
551 if (addr->addrtype == TIPC_ADDR_NAME)
552 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
553 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
558 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
559 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
560 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
565 res = (addr->scope > 0) ?
566 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
567 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
574 * tipc_getname - get port ID of socket or peer socket
575 * @sock: socket structure
576 * @uaddr: area for returned socket address
577 * @uaddr_len: area for returned length of socket address
578 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
580 * Returns 0 on success, errno otherwise
582 * NOTE: This routine doesn't need to take the socket lock since it only
583 * accesses socket information that is unchanging (or which changes in
584 * a completely predictable manner).
586 static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
587 int *uaddr_len, int peer)
589 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
590 struct sock *sk = sock->sk;
591 struct tipc_sock *tsk = tipc_sk(sk);
592 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
594 memset(addr, 0, sizeof(*addr));
596 if ((!tipc_sk_connected(sk)) &&
597 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
599 addr->addr.id.ref = tsk_peer_port(tsk);
600 addr->addr.id.node = tsk_peer_node(tsk);
602 addr->addr.id.ref = tsk->portid;
603 addr->addr.id.node = tn->own_addr;
606 *uaddr_len = sizeof(*addr);
607 addr->addrtype = TIPC_ADDR_ID;
608 addr->family = AF_TIPC;
610 addr->addr.name.domain = 0;
616 * tipc_poll - read and possibly block on pollmask
617 * @file: file structure associated with the socket
618 * @sock: socket for which to calculate the poll bits
621 * Returns pollmask value
624 * It appears that the usual socket locking mechanisms are not useful here
625 * since the pollmask info is potentially out-of-date the moment this routine
626 * exits. TCP and other protocols seem to rely on higher level poll routines
627 * to handle any preventable race conditions, so TIPC will do the same ...
629 * IMPORTANT: The fact that a read or write operation is indicated does NOT
630 * imply that the operation will succeed, merely that it should be performed
631 * and will not block.
633 static unsigned int tipc_poll(struct file *file, struct socket *sock,
636 struct sock *sk = sock->sk;
637 struct tipc_sock *tsk = tipc_sk(sk);
640 sock_poll_wait(file, sk_sleep(sk), wait);
642 if (sk->sk_shutdown & RCV_SHUTDOWN)
643 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
644 if (sk->sk_shutdown == SHUTDOWN_MASK)
647 switch (sk->sk_state) {
648 case TIPC_ESTABLISHED:
649 if (!tsk->link_cong && !tsk_conn_cong(tsk))
653 case TIPC_CONNECTING:
654 if (!skb_queue_empty(&sk->sk_receive_queue))
655 mask |= (POLLIN | POLLRDNORM);
660 if (tipc_sk_type_connectionless(sk) &&
661 (!skb_queue_empty(&sk->sk_receive_queue)))
662 mask |= (POLLIN | POLLRDNORM);
664 case TIPC_DISCONNECTING:
665 mask = (POLLIN | POLLRDNORM | POLLHUP);
673 * tipc_sendmcast - send multicast message
674 * @sock: socket structure
675 * @seq: destination address
676 * @msg: message to send
677 * @dsz: total length of message data
678 * @timeo: timeout to wait for wakeup
680 * Called from function tipc_sendmsg(), which has done all sanity checks
681 * Returns the number of bytes sent on success, or errno
683 static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
684 struct msghdr *msg, size_t dsz, long timeo)
686 struct sock *sk = sock->sk;
687 struct tipc_sock *tsk = tipc_sk(sk);
688 struct net *net = sock_net(sk);
689 struct tipc_msg *mhdr = &tsk->phdr;
690 struct sk_buff_head pktchain;
691 struct iov_iter save = msg->msg_iter;
695 if (!timeo && tsk->link_cong)
698 msg_set_type(mhdr, TIPC_MCAST_MSG);
699 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
700 msg_set_destport(mhdr, 0);
701 msg_set_destnode(mhdr, 0);
702 msg_set_nametype(mhdr, seq->type);
703 msg_set_namelower(mhdr, seq->lower);
704 msg_set_nameupper(mhdr, seq->upper);
705 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
707 skb_queue_head_init(&pktchain);
710 mtu = tipc_bcast_get_mtu(net);
711 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &pktchain);
712 if (unlikely(rc < 0))
716 rc = tipc_bcast_xmit(net, &pktchain);
720 if (rc == -ELINKCONG) {
722 rc = tipc_wait_for_sndmsg(sock, &timeo);
726 __skb_queue_purge(&pktchain);
727 if (rc == -EMSGSIZE) {
728 msg->msg_iter = save;
737 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
738 * @arrvq: queue with arriving messages, to be cloned after destination lookup
739 * @inputq: queue with cloned messages, delivered to socket after dest lookup
741 * Multi-threaded: parallel calls with reference to same queues may occur
743 void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
744 struct sk_buff_head *inputq)
746 struct tipc_msg *msg;
747 struct tipc_plist dports;
749 u32 scope = TIPC_CLUSTER_SCOPE;
750 struct sk_buff_head tmpq;
752 struct sk_buff *skb, *_skb;
754 __skb_queue_head_init(&tmpq);
755 tipc_plist_init(&dports);
757 skb = tipc_skb_peek(arrvq, &inputq->lock);
758 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
760 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
762 if (in_own_node(net, msg_orignode(msg)))
763 scope = TIPC_NODE_SCOPE;
765 /* Create destination port list and message clones: */
766 tipc_nametbl_mc_translate(net,
767 msg_nametype(msg), msg_namelower(msg),
768 msg_nameupper(msg), scope, &dports);
769 portid = tipc_plist_pop(&dports);
770 for (; portid; portid = tipc_plist_pop(&dports)) {
771 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
773 msg_set_destport(buf_msg(_skb), portid);
774 __skb_queue_tail(&tmpq, _skb);
777 pr_warn("Failed to clone mcast rcv buffer\n");
779 /* Append to inputq if not already done by other thread */
780 spin_lock_bh(&inputq->lock);
781 if (skb_peek(arrvq) == skb) {
782 skb_queue_splice_tail_init(&tmpq, inputq);
783 kfree_skb(__skb_dequeue(arrvq));
785 spin_unlock_bh(&inputq->lock);
786 __skb_queue_purge(&tmpq);
789 tipc_sk_rcv(net, inputq);
793 * tipc_sk_proto_rcv - receive a connection mng protocol message
794 * @tsk: receiving socket
795 * @skb: pointer to message buffer.
797 static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
798 struct sk_buff_head *xmitq)
800 struct sock *sk = &tsk->sk;
801 u32 onode = tsk_own_node(tsk);
802 struct tipc_msg *hdr = buf_msg(skb);
803 int mtyp = msg_type(hdr);
806 /* Ignore if connection cannot be validated: */
807 if (!tsk_peer_msg(tsk, hdr))
810 tsk->probe_unacked = false;
812 if (mtyp == CONN_PROBE) {
813 msg_set_type(hdr, CONN_PROBE_REPLY);
814 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
815 __skb_queue_tail(xmitq, skb);
817 } else if (mtyp == CONN_ACK) {
818 conn_cong = tsk_conn_cong(tsk);
819 tsk->snt_unacked -= msg_conn_ack(hdr);
820 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
821 tsk->snd_win = msg_adv_win(hdr);
823 sk->sk_write_space(sk);
824 } else if (mtyp != CONN_PROBE_REPLY) {
825 pr_warn("Received unknown CONN_PROTO msg\n");
831 static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
833 DEFINE_WAIT_FUNC(wait, woken_wake_function);
834 struct sock *sk = sock->sk;
835 struct tipc_sock *tsk = tipc_sk(sk);
839 int err = sock_error(sk);
842 if (sk->sk_shutdown & SEND_SHUTDOWN)
846 if (signal_pending(current))
847 return sock_intr_errno(*timeo_p);
849 add_wait_queue(sk_sleep(sk), &wait);
850 done = sk_wait_event(sk, timeo_p, !tsk->link_cong, &wait);
851 remove_wait_queue(sk_sleep(sk), &wait);
857 * tipc_sendmsg - send message in connectionless manner
858 * @sock: socket structure
859 * @m: message to send
860 * @dsz: amount of user data to be sent
862 * Message must have an destination specified explicitly.
863 * Used for SOCK_RDM and SOCK_DGRAM messages,
864 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
865 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
867 * Returns the number of bytes sent on success, or errno otherwise
869 static int tipc_sendmsg(struct socket *sock,
870 struct msghdr *m, size_t dsz)
872 struct sock *sk = sock->sk;
876 ret = __tipc_sendmsg(sock, m, dsz);
882 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
884 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
885 struct sock *sk = sock->sk;
886 struct tipc_sock *tsk = tipc_sk(sk);
887 struct net *net = sock_net(sk);
888 struct tipc_msg *mhdr = &tsk->phdr;
890 struct sk_buff_head pktchain;
891 bool is_connectionless = tipc_sk_type_connectionless(sk);
893 struct tipc_name_seq *seq;
894 struct iov_iter save;
899 if (dsz > TIPC_MAX_USER_MSG_SIZE)
901 if (unlikely(!dest)) {
902 if (is_connectionless && tsk->peer.family == AF_TIPC)
905 return -EDESTADDRREQ;
906 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
907 dest->family != AF_TIPC) {
910 if (!is_connectionless) {
911 if (sk->sk_state == TIPC_LISTEN)
913 if (sk->sk_state != TIPC_OPEN)
917 if (dest->addrtype == TIPC_ADDR_NAME) {
918 tsk->conn_type = dest->addr.name.name.type;
919 tsk->conn_instance = dest->addr.name.name.instance;
922 seq = &dest->addr.nameseq;
923 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
925 if (dest->addrtype == TIPC_ADDR_MCAST) {
926 return tipc_sendmcast(sock, seq, m, dsz, timeo);
927 } else if (dest->addrtype == TIPC_ADDR_NAME) {
928 u32 type = dest->addr.name.name.type;
929 u32 inst = dest->addr.name.name.instance;
930 u32 domain = dest->addr.name.domain;
933 msg_set_type(mhdr, TIPC_NAMED_MSG);
934 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
935 msg_set_nametype(mhdr, type);
936 msg_set_nameinst(mhdr, inst);
937 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
938 dport = tipc_nametbl_translate(net, type, inst, &dnode);
939 msg_set_destnode(mhdr, dnode);
940 msg_set_destport(mhdr, dport);
941 if (unlikely(!dport && !dnode))
942 return -EHOSTUNREACH;
943 } else if (dest->addrtype == TIPC_ADDR_ID) {
944 dnode = dest->addr.id.node;
945 msg_set_type(mhdr, TIPC_DIRECT_MSG);
946 msg_set_lookup_scope(mhdr, 0);
947 msg_set_destnode(mhdr, dnode);
948 msg_set_destport(mhdr, dest->addr.id.ref);
949 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
952 skb_queue_head_init(&pktchain);
955 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
956 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &pktchain);
961 skb = skb_peek(&pktchain);
962 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
963 rc = tipc_node_xmit(net, &pktchain, dnode, tsk->portid);
965 if (!is_connectionless)
966 tipc_set_sk_state(sk, TIPC_CONNECTING);
969 if (rc == -ELINKCONG) {
971 rc = tipc_wait_for_sndmsg(sock, &timeo);
975 __skb_queue_purge(&pktchain);
976 if (rc == -EMSGSIZE) {
986 static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
988 DEFINE_WAIT_FUNC(wait, woken_wake_function);
989 struct sock *sk = sock->sk;
990 struct tipc_sock *tsk = tipc_sk(sk);
994 int err = sock_error(sk);
997 if (sk->sk_state == TIPC_DISCONNECTING)
999 else if (!tipc_sk_connected(sk))
1003 if (signal_pending(current))
1004 return sock_intr_errno(*timeo_p);
1006 add_wait_queue(sk_sleep(sk), &wait);
1007 done = sk_wait_event(sk, timeo_p,
1009 !tsk_conn_cong(tsk)) ||
1010 !tipc_sk_connected(sk), &wait);
1011 remove_wait_queue(sk_sleep(sk), &wait);
1017 * tipc_send_stream - send stream-oriented data
1018 * @sock: socket structure
1020 * @dsz: total length of data to be transmitted
1022 * Used for SOCK_STREAM data.
1024 * Returns the number of bytes sent on success (or partial success),
1025 * or errno if no data sent
1027 static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1029 struct sock *sk = sock->sk;
1033 ret = __tipc_send_stream(sock, m, dsz);
1039 static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1041 struct sock *sk = sock->sk;
1042 struct net *net = sock_net(sk);
1043 struct tipc_sock *tsk = tipc_sk(sk);
1044 struct tipc_msg *mhdr = &tsk->phdr;
1045 struct sk_buff_head pktchain;
1046 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1047 u32 portid = tsk->portid;
1051 uint mtu, send, sent = 0;
1052 struct iov_iter save;
1053 int hlen = MIN_H_SIZE;
1055 /* Handle implied connection establishment */
1056 if (unlikely(dest)) {
1057 rc = __tipc_sendmsg(sock, m, dsz);
1058 hlen = msg_hdr_sz(mhdr);
1059 if (dsz && (dsz == rc))
1060 tsk->snt_unacked = tsk_inc(tsk, dsz + hlen);
1063 if (dsz > (uint)INT_MAX)
1066 if (unlikely(!tipc_sk_connected(sk))) {
1067 if (sk->sk_state == TIPC_DISCONNECTING)
1073 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1074 if (!timeo && tsk->link_cong)
1077 dnode = tsk_peer_node(tsk);
1078 skb_queue_head_init(&pktchain);
1083 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
1084 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &pktchain);
1085 if (unlikely(rc < 0))
1089 if (likely(!tsk_conn_cong(tsk))) {
1090 rc = tipc_node_xmit(net, &pktchain, dnode, portid);
1092 tsk->snt_unacked += tsk_inc(tsk, send + hlen);
1098 if (rc == -EMSGSIZE) {
1099 __skb_queue_purge(&pktchain);
1100 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1105 if (rc != -ELINKCONG)
1110 rc = tipc_wait_for_sndpkt(sock, &timeo);
1113 __skb_queue_purge(&pktchain);
1114 return sent ? sent : rc;
1118 * tipc_send_packet - send a connection-oriented message
1119 * @sock: socket structure
1120 * @m: message to send
1121 * @dsz: length of data to be transmitted
1123 * Used for SOCK_SEQPACKET messages.
1125 * Returns the number of bytes sent on success, or errno otherwise
1127 static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
1129 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1132 return tipc_send_stream(sock, m, dsz);
1135 /* tipc_sk_finish_conn - complete the setup of a connection
1137 static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
1140 struct sock *sk = &tsk->sk;
1141 struct net *net = sock_net(sk);
1142 struct tipc_msg *msg = &tsk->phdr;
1144 msg_set_destnode(msg, peer_node);
1145 msg_set_destport(msg, peer_port);
1146 msg_set_type(msg, TIPC_CONN_MSG);
1147 msg_set_lookup_scope(msg, 0);
1148 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1150 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
1151 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
1152 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1153 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
1154 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
1155 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1158 /* Fall back to message based flow control */
1159 tsk->rcv_win = FLOWCTL_MSG_WIN;
1160 tsk->snd_win = FLOWCTL_MSG_WIN;
1164 * set_orig_addr - capture sender's address for received message
1165 * @m: descriptor for message info
1166 * @msg: received message header
1168 * Note: Address is not captured if not requested by receiver.
1170 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
1172 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
1175 addr->family = AF_TIPC;
1176 addr->addrtype = TIPC_ADDR_ID;
1177 memset(&addr->addr, 0, sizeof(addr->addr));
1178 addr->addr.id.ref = msg_origport(msg);
1179 addr->addr.id.node = msg_orignode(msg);
1180 addr->addr.name.domain = 0; /* could leave uninitialized */
1181 addr->scope = 0; /* could leave uninitialized */
1182 m->msg_namelen = sizeof(struct sockaddr_tipc);
1187 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
1188 * @m: descriptor for message info
1189 * @msg: received message header
1190 * @tsk: TIPC port associated with message
1192 * Note: Ancillary data is not captured if not requested by receiver.
1194 * Returns 0 if successful, otherwise errno
1196 static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1197 struct tipc_sock *tsk)
1205 if (likely(m->msg_controllen == 0))
1208 /* Optionally capture errored message object(s) */
1209 err = msg ? msg_errcode(msg) : 0;
1210 if (unlikely(err)) {
1212 anc_data[1] = msg_data_sz(msg);
1213 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1217 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1224 /* Optionally capture message destination object */
1225 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1226 switch (dest_type) {
1227 case TIPC_NAMED_MSG:
1229 anc_data[0] = msg_nametype(msg);
1230 anc_data[1] = msg_namelower(msg);
1231 anc_data[2] = msg_namelower(msg);
1233 case TIPC_MCAST_MSG:
1235 anc_data[0] = msg_nametype(msg);
1236 anc_data[1] = msg_namelower(msg);
1237 anc_data[2] = msg_nameupper(msg);
1240 has_name = (tsk->conn_type != 0);
1241 anc_data[0] = tsk->conn_type;
1242 anc_data[1] = tsk->conn_instance;
1243 anc_data[2] = tsk->conn_instance;
1249 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1257 static void tipc_sk_send_ack(struct tipc_sock *tsk)
1259 struct sock *sk = &tsk->sk;
1260 struct net *net = sock_net(sk);
1261 struct sk_buff *skb = NULL;
1262 struct tipc_msg *msg;
1263 u32 peer_port = tsk_peer_port(tsk);
1264 u32 dnode = tsk_peer_node(tsk);
1266 if (!tipc_sk_connected(sk))
1268 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1269 dnode, tsk_own_node(tsk), peer_port,
1270 tsk->portid, TIPC_OK);
1274 msg_set_conn_ack(msg, tsk->rcv_unacked);
1275 tsk->rcv_unacked = 0;
1277 /* Adjust to and advertize the correct window limit */
1278 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1279 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1280 msg_set_adv_win(msg, tsk->rcv_win);
1282 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
1285 static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1287 struct sock *sk = sock->sk;
1289 long timeo = *timeop;
1293 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1294 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
1295 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1300 timeo = schedule_timeout(timeo);
1304 if (!skb_queue_empty(&sk->sk_receive_queue))
1309 err = sock_intr_errno(timeo);
1310 if (signal_pending(current))
1313 finish_wait(sk_sleep(sk), &wait);
1319 * tipc_recvmsg - receive packet-oriented message
1320 * @m: descriptor for message info
1321 * @buf_len: total size of user buffer area
1322 * @flags: receive flags
1324 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1325 * If the complete message doesn't fit in user area, truncate it.
1327 * Returns size of returned message data, errno otherwise
1329 static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1332 struct sock *sk = sock->sk;
1333 struct tipc_sock *tsk = tipc_sk(sk);
1334 struct sk_buff *buf;
1335 struct tipc_msg *msg;
1336 bool is_connectionless = tipc_sk_type_connectionless(sk);
1342 /* Catch invalid receive requests */
1343 if (unlikely(!buf_len))
1348 if (!is_connectionless && unlikely(sk->sk_state == TIPC_OPEN)) {
1353 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1356 /* Look for a message in receive queue; wait if necessary */
1357 res = tipc_wait_for_rcvmsg(sock, &timeo);
1361 /* Look at first message in receive queue */
1362 buf = skb_peek(&sk->sk_receive_queue);
1364 sz = msg_data_sz(msg);
1365 hlen = msg_hdr_sz(msg);
1366 err = msg_errcode(msg);
1368 /* Discard an empty non-errored message & try again */
1369 if ((!sz) && (!err)) {
1370 tsk_advance_rx_queue(sk);
1374 /* Capture sender's address (optional) */
1375 set_orig_addr(m, msg);
1377 /* Capture ancillary data (optional) */
1378 res = tipc_sk_anc_data_recv(m, msg, tsk);
1382 /* Capture message data (if valid) & compute return value (always) */
1384 if (unlikely(buf_len < sz)) {
1386 m->msg_flags |= MSG_TRUNC;
1388 res = skb_copy_datagram_msg(buf, hlen, m, sz);
1393 if (is_connectionless || err == TIPC_CONN_SHUTDOWN ||
1400 if (unlikely(flags & MSG_PEEK))
1403 if (likely(!is_connectionless)) {
1404 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1405 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1406 tipc_sk_send_ack(tsk);
1408 tsk_advance_rx_queue(sk);
1415 * tipc_recv_stream - receive stream-oriented data
1416 * @m: descriptor for message info
1417 * @buf_len: total size of user buffer area
1418 * @flags: receive flags
1420 * Used for SOCK_STREAM messages only. If not enough data is available
1421 * will optionally wait for more; never truncates data.
1423 * Returns size of returned message data, errno otherwise
1425 static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1426 size_t buf_len, int flags)
1428 struct sock *sk = sock->sk;
1429 struct tipc_sock *tsk = tipc_sk(sk);
1430 struct sk_buff *buf;
1431 struct tipc_msg *msg;
1439 /* Catch invalid receive attempts */
1440 if (unlikely(!buf_len))
1445 if (unlikely(sk->sk_state == TIPC_OPEN)) {
1450 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
1451 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1454 /* Look for a message in receive queue; wait if necessary */
1455 res = tipc_wait_for_rcvmsg(sock, &timeo);
1459 /* Look at first message in receive queue */
1460 buf = skb_peek(&sk->sk_receive_queue);
1462 sz = msg_data_sz(msg);
1463 hlen = msg_hdr_sz(msg);
1464 err = msg_errcode(msg);
1466 /* Discard an empty non-errored message & try again */
1467 if ((!sz) && (!err)) {
1468 tsk_advance_rx_queue(sk);
1472 /* Optionally capture sender's address & ancillary data of first msg */
1473 if (sz_copied == 0) {
1474 set_orig_addr(m, msg);
1475 res = tipc_sk_anc_data_recv(m, msg, tsk);
1480 /* Capture message data (if valid) & compute return value (always) */
1482 u32 offset = TIPC_SKB_CB(buf)->bytes_read;
1487 needed = (buf_len - sz_copied);
1488 sz_to_copy = min(sz, needed);
1490 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy);
1494 sz_copied += sz_to_copy;
1496 if (sz_to_copy < sz) {
1497 if (!(flags & MSG_PEEK))
1498 TIPC_SKB_CB(buf)->bytes_read =
1499 offset + sz_to_copy;
1504 goto exit; /* can't add error msg to valid data */
1506 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1512 if (unlikely(flags & MSG_PEEK))
1515 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1516 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1517 tipc_sk_send_ack(tsk);
1518 tsk_advance_rx_queue(sk);
1520 /* Loop around if more data is required */
1521 if ((sz_copied < buf_len) && /* didn't get all requested data */
1522 (!skb_queue_empty(&sk->sk_receive_queue) ||
1523 (sz_copied < target)) && /* and more is ready or required */
1524 (!err)) /* and haven't reached a FIN */
1529 return sz_copied ? sz_copied : res;
1533 * tipc_write_space - wake up thread if port congestion is released
1536 static void tipc_write_space(struct sock *sk)
1538 struct socket_wq *wq;
1541 wq = rcu_dereference(sk->sk_wq);
1542 if (skwq_has_sleeper(wq))
1543 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1544 POLLWRNORM | POLLWRBAND);
1549 * tipc_data_ready - wake up threads to indicate messages have been received
1551 * @len: the length of messages
1553 static void tipc_data_ready(struct sock *sk)
1555 struct socket_wq *wq;
1558 wq = rcu_dereference(sk->sk_wq);
1559 if (skwq_has_sleeper(wq))
1560 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1561 POLLRDNORM | POLLRDBAND);
1565 static void tipc_sock_destruct(struct sock *sk)
1567 __skb_queue_purge(&sk->sk_receive_queue);
1571 * filter_connect - Handle all incoming messages for a connection-based socket
1573 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
1575 * Returns true if everything ok, false otherwise
1577 static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
1579 struct sock *sk = &tsk->sk;
1580 struct net *net = sock_net(sk);
1581 struct tipc_msg *hdr = buf_msg(skb);
1583 if (unlikely(msg_mcast(hdr)))
1586 switch (sk->sk_state) {
1587 case TIPC_CONNECTING:
1588 /* Accept only ACK or NACK message */
1589 if (unlikely(!msg_connected(hdr)))
1592 if (unlikely(msg_errcode(hdr))) {
1593 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1594 sk->sk_err = ECONNREFUSED;
1598 if (unlikely(!msg_isdata(hdr))) {
1599 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1600 sk->sk_err = EINVAL;
1604 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1605 msg_set_importance(&tsk->phdr, msg_importance(hdr));
1607 /* If 'ACK+' message, add to socket receive queue */
1608 if (msg_data_sz(hdr))
1611 /* If empty 'ACK-' message, wake up sleeping connect() */
1612 if (waitqueue_active(sk_sleep(sk)))
1613 wake_up_interruptible(sk_sleep(sk));
1615 /* 'ACK-' message is neither accepted nor rejected: */
1616 msg_set_dest_droppable(hdr, 1);
1620 case TIPC_DISCONNECTING:
1623 /* Accept only SYN message */
1624 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1627 case TIPC_ESTABLISHED:
1628 /* Accept only connection-based messages sent by peer */
1629 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1632 if (unlikely(msg_errcode(hdr))) {
1633 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1634 /* Let timer expire on it's own */
1635 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1637 sk->sk_state_change(sk);
1641 pr_err("Unknown sk_state %u\n", sk->sk_state);
1648 * rcvbuf_limit - get proper overload limit of socket receive queue
1652 * For connection oriented messages, irrespective of importance,
1653 * default queue limit is 2 MB.
1655 * For connectionless messages, queue limits are based on message
1656 * importance as follows:
1658 * TIPC_LOW_IMPORTANCE (2 MB)
1659 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1660 * TIPC_HIGH_IMPORTANCE (8 MB)
1661 * TIPC_CRITICAL_IMPORTANCE (16 MB)
1663 * Returns overload limit according to corresponding message importance
1665 static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
1667 struct tipc_sock *tsk = tipc_sk(sk);
1668 struct tipc_msg *hdr = buf_msg(skb);
1670 if (unlikely(!msg_connected(hdr)))
1671 return sk->sk_rcvbuf << msg_importance(hdr);
1673 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1674 return sk->sk_rcvbuf;
1676 return FLOWCTL_MSG_LIM;
1680 * filter_rcv - validate incoming message
1682 * @skb: pointer to message.
1684 * Enqueues message on receive queue if acceptable; optionally handles
1685 * disconnect indication for a connected socket.
1687 * Called with socket lock already taken
1689 * Returns true if message was added to socket receive queue, otherwise false
1691 static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1692 struct sk_buff_head *xmitq)
1694 struct tipc_sock *tsk = tipc_sk(sk);
1695 struct tipc_msg *hdr = buf_msg(skb);
1696 unsigned int limit = rcvbuf_limit(sk, skb);
1698 int usr = msg_user(hdr);
1700 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
1701 tipc_sk_proto_rcv(tsk, skb, xmitq);
1705 if (unlikely(usr == SOCK_WAKEUP)) {
1708 sk->sk_write_space(sk);
1712 /* Drop if illegal message type */
1713 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1718 /* Reject if wrong message type for current socket state */
1719 if (tipc_sk_type_connectionless(sk)) {
1720 if (msg_connected(hdr)) {
1721 err = TIPC_ERR_NO_PORT;
1724 } else if (unlikely(!filter_connect(tsk, skb))) {
1725 err = TIPC_ERR_NO_PORT;
1729 /* Reject message if there isn't room to queue it */
1730 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1731 err = TIPC_ERR_OVERLOAD;
1735 /* Enqueue message */
1736 TIPC_SKB_CB(skb)->bytes_read = 0;
1737 __skb_queue_tail(&sk->sk_receive_queue, skb);
1738 skb_set_owner_r(skb, sk);
1740 sk->sk_data_ready(sk);
1744 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1745 __skb_queue_tail(xmitq, skb);
1750 * tipc_backlog_rcv - handle incoming message from backlog queue
1754 * Caller must hold socket lock
1758 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
1760 unsigned int truesize = skb->truesize;
1761 struct sk_buff_head xmitq;
1762 u32 dnode, selector;
1764 __skb_queue_head_init(&xmitq);
1766 if (likely(filter_rcv(sk, skb, &xmitq))) {
1767 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
1771 if (skb_queue_empty(&xmitq))
1774 /* Send response/rejected message */
1775 skb = __skb_dequeue(&xmitq);
1776 dnode = msg_destnode(buf_msg(skb));
1777 selector = msg_origport(buf_msg(skb));
1778 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
1783 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1784 * inputq and try adding them to socket or backlog queue
1785 * @inputq: list of incoming buffers with potentially different destinations
1786 * @sk: socket where the buffers should be enqueued
1787 * @dport: port number for the socket
1789 * Caller must hold socket lock
1791 static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
1792 u32 dport, struct sk_buff_head *xmitq)
1794 unsigned long time_limit = jiffies + 2;
1795 struct sk_buff *skb;
1800 while (skb_queue_len(inputq)) {
1801 if (unlikely(time_after_eq(jiffies, time_limit)))
1804 skb = tipc_skb_dequeue(inputq, dport);
1808 /* Add message directly to receive queue if possible */
1809 if (!sock_owned_by_user(sk)) {
1810 filter_rcv(sk, skb, xmitq);
1814 /* Try backlog, compensating for double-counted bytes */
1815 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
1816 if (!sk->sk_backlog.len)
1817 atomic_set(dcnt, 0);
1818 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1819 if (likely(!sk_add_backlog(sk, skb, lim)))
1822 /* Overload => reject message back to sender */
1823 onode = tipc_own_addr(sock_net(sk));
1824 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1825 __skb_queue_tail(xmitq, skb);
1831 * tipc_sk_rcv - handle a chain of incoming buffers
1832 * @inputq: buffer list containing the buffers
1833 * Consumes all buffers in list until inputq is empty
1834 * Note: may be called in multiple threads referring to the same queue
1836 void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
1838 struct sk_buff_head xmitq;
1839 u32 dnode, dport = 0;
1841 struct tipc_sock *tsk;
1843 struct sk_buff *skb;
1845 __skb_queue_head_init(&xmitq);
1846 while (skb_queue_len(inputq)) {
1847 dport = tipc_skb_peek_port(inputq, dport);
1848 tsk = tipc_sk_lookup(net, dport);
1852 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
1853 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
1854 spin_unlock_bh(&sk->sk_lock.slock);
1856 /* Send pending response/rejected messages, if any */
1857 while ((skb = __skb_dequeue(&xmitq))) {
1858 dnode = msg_destnode(buf_msg(skb));
1859 tipc_node_xmit_skb(net, skb, dnode, dport);
1865 /* No destination socket => dequeue skb if still there */
1866 skb = tipc_skb_dequeue(inputq, dport);
1870 /* Try secondary lookup if unresolved named message */
1871 err = TIPC_ERR_NO_PORT;
1872 if (tipc_msg_lookup_dest(net, skb, &err))
1875 /* Prepare for message rejection */
1876 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
1879 dnode = msg_destnode(buf_msg(skb));
1880 tipc_node_xmit_skb(net, skb, dnode, dport);
1884 static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1886 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1887 struct sock *sk = sock->sk;
1891 int err = sock_error(sk);
1896 if (signal_pending(current))
1897 return sock_intr_errno(*timeo_p);
1899 add_wait_queue(sk_sleep(sk), &wait);
1900 done = sk_wait_event(sk, timeo_p,
1901 sk->sk_state != TIPC_CONNECTING, &wait);
1902 remove_wait_queue(sk_sleep(sk), &wait);
1908 * tipc_connect - establish a connection to another TIPC port
1909 * @sock: socket structure
1910 * @dest: socket address for destination port
1911 * @destlen: size of socket address data structure
1912 * @flags: file-related flags associated with socket
1914 * Returns 0 on success, errno otherwise
1916 static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1917 int destlen, int flags)
1919 struct sock *sk = sock->sk;
1920 struct tipc_sock *tsk = tipc_sk(sk);
1921 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1922 struct msghdr m = {NULL,};
1923 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
1929 /* DGRAM/RDM connect(), just save the destaddr */
1930 if (tipc_sk_type_connectionless(sk)) {
1931 if (dst->family == AF_UNSPEC) {
1932 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
1933 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1936 memcpy(&tsk->peer, dest, destlen);
1942 * Reject connection attempt using multicast address
1944 * Note: send_msg() validates the rest of the address fields,
1945 * so there's no need to do it here
1947 if (dst->addrtype == TIPC_ADDR_MCAST) {
1952 previous = sk->sk_state;
1954 switch (sk->sk_state) {
1956 /* Send a 'SYN-' to destination */
1958 m.msg_namelen = destlen;
1960 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1961 * indicate send_msg() is never blocked.
1964 m.msg_flags = MSG_DONTWAIT;
1966 res = __tipc_sendmsg(sock, &m, 0);
1967 if ((res < 0) && (res != -EWOULDBLOCK))
1970 /* Just entered TIPC_CONNECTING state; the only
1971 * difference is that return value in non-blocking
1972 * case is EINPROGRESS, rather than EALREADY.
1976 case TIPC_CONNECTING:
1978 if (previous == TIPC_CONNECTING)
1982 timeout = msecs_to_jiffies(timeout);
1983 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1984 res = tipc_wait_for_connect(sock, &timeout);
1986 case TIPC_ESTABLISHED:
1999 * tipc_listen - allow socket to listen for incoming connections
2000 * @sock: socket structure
2003 * Returns 0 on success, errno otherwise
2005 static int tipc_listen(struct socket *sock, int len)
2007 struct sock *sk = sock->sk;
2011 res = tipc_set_sk_state(sk, TIPC_LISTEN);
2017 static int tipc_wait_for_accept(struct socket *sock, long timeo)
2019 struct sock *sk = sock->sk;
2023 /* True wake-one mechanism for incoming connections: only
2024 * one process gets woken up, not the 'whole herd'.
2025 * Since we do not 'race & poll' for established sockets
2026 * anymore, the common case will execute the loop only once.
2029 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2030 TASK_INTERRUPTIBLE);
2031 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
2033 timeo = schedule_timeout(timeo);
2037 if (!skb_queue_empty(&sk->sk_receive_queue))
2042 err = sock_intr_errno(timeo);
2043 if (signal_pending(current))
2046 finish_wait(sk_sleep(sk), &wait);
2051 * tipc_accept - wait for connection request
2052 * @sock: listening socket
2053 * @newsock: new socket that is to be connected
2054 * @flags: file-related flags associated with socket
2056 * Returns 0 on success, errno otherwise
2058 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
2060 struct sock *new_sk, *sk = sock->sk;
2061 struct sk_buff *buf;
2062 struct tipc_sock *new_tsock;
2063 struct tipc_msg *msg;
2069 if (sk->sk_state != TIPC_LISTEN) {
2073 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2074 res = tipc_wait_for_accept(sock, timeo);
2078 buf = skb_peek(&sk->sk_receive_queue);
2080 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 0);
2083 security_sk_clone(sock->sk, new_sock->sk);
2085 new_sk = new_sock->sk;
2086 new_tsock = tipc_sk(new_sk);
2089 /* we lock on new_sk; but lockdep sees the lock on sk */
2090 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
2093 * Reject any stray messages received by new socket
2094 * before the socket lock was taken (very, very unlikely)
2096 tsk_rej_rx_queue(new_sk);
2098 /* Connect new socket to it's peer */
2099 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
2101 tsk_set_importance(new_tsock, msg_importance(msg));
2102 if (msg_named(msg)) {
2103 new_tsock->conn_type = msg_nametype(msg);
2104 new_tsock->conn_instance = msg_nameinst(msg);
2108 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2109 * Respond to 'SYN+' by queuing it on new socket.
2111 if (!msg_data_sz(msg)) {
2112 struct msghdr m = {NULL,};
2114 tsk_advance_rx_queue(sk);
2115 __tipc_send_stream(new_sock, &m, 0);
2117 __skb_dequeue(&sk->sk_receive_queue);
2118 __skb_queue_head(&new_sk->sk_receive_queue, buf);
2119 skb_set_owner_r(buf, new_sk);
2121 release_sock(new_sk);
2128 * tipc_shutdown - shutdown socket connection
2129 * @sock: socket structure
2130 * @how: direction to close (must be SHUT_RDWR)
2132 * Terminates connection (if necessary), then purges socket's receive queue.
2134 * Returns 0 on success, errno otherwise
2136 static int tipc_shutdown(struct socket *sock, int how)
2138 struct sock *sk = sock->sk;
2141 if (how != SHUT_RDWR)
2146 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2147 sk->sk_shutdown = SEND_SHUTDOWN;
2149 if (sk->sk_state == TIPC_DISCONNECTING) {
2150 /* Discard any unreceived messages */
2151 __skb_queue_purge(&sk->sk_receive_queue);
2153 /* Wake up anyone sleeping in poll */
2154 sk->sk_state_change(sk);
2164 static void tipc_sk_timeout(unsigned long data)
2166 struct tipc_sock *tsk = (struct tipc_sock *)data;
2167 struct sock *sk = &tsk->sk;
2168 struct sk_buff *skb = NULL;
2169 u32 peer_port, peer_node;
2170 u32 own_node = tsk_own_node(tsk);
2173 if (!tipc_sk_connected(sk)) {
2177 peer_port = tsk_peer_port(tsk);
2178 peer_node = tsk_peer_node(tsk);
2180 if (tsk->probe_unacked) {
2181 if (!sock_owned_by_user(sk)) {
2182 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2183 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2184 tsk_peer_port(tsk));
2185 sk->sk_state_change(sk);
2187 /* Try again later */
2188 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2195 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2196 INT_H_SIZE, 0, peer_node, own_node,
2197 peer_port, tsk->portid, TIPC_OK);
2198 tsk->probe_unacked = true;
2199 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
2202 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
2207 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
2208 struct tipc_name_seq const *seq)
2210 struct sock *sk = &tsk->sk;
2211 struct net *net = sock_net(sk);
2212 struct publication *publ;
2215 if (tipc_sk_connected(sk))
2217 key = tsk->portid + tsk->pub_count + 1;
2218 if (key == tsk->portid)
2221 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
2222 scope, tsk->portid, key);
2223 if (unlikely(!publ))
2226 list_add(&publ->pport_list, &tsk->publications);
2232 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
2233 struct tipc_name_seq const *seq)
2235 struct net *net = sock_net(&tsk->sk);
2236 struct publication *publ;
2237 struct publication *safe;
2240 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
2242 if (publ->scope != scope)
2244 if (publ->type != seq->type)
2246 if (publ->lower != seq->lower)
2248 if (publ->upper != seq->upper)
2250 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2251 publ->ref, publ->key);
2255 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2256 publ->ref, publ->key);
2259 if (list_empty(&tsk->publications))
2264 /* tipc_sk_reinit: set non-zero address in all existing sockets
2265 * when we go from standalone to network mode.
2267 void tipc_sk_reinit(struct net *net)
2269 struct tipc_net *tn = net_generic(net, tipc_net_id);
2270 const struct bucket_table *tbl;
2271 struct rhash_head *pos;
2272 struct tipc_sock *tsk;
2273 struct tipc_msg *msg;
2277 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
2278 for (i = 0; i < tbl->size; i++) {
2279 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2280 spin_lock_bh(&tsk->sk.sk_lock.slock);
2282 msg_set_prevnode(msg, tn->own_addr);
2283 msg_set_orignode(msg, tn->own_addr);
2284 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2290 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
2292 struct tipc_net *tn = net_generic(net, tipc_net_id);
2293 struct tipc_sock *tsk;
2296 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
2298 sock_hold(&tsk->sk);
2304 static int tipc_sk_insert(struct tipc_sock *tsk)
2306 struct sock *sk = &tsk->sk;
2307 struct net *net = sock_net(sk);
2308 struct tipc_net *tn = net_generic(net, tipc_net_id);
2309 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2310 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2312 while (remaining--) {
2314 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2315 portid = TIPC_MIN_PORT;
2316 tsk->portid = portid;
2317 sock_hold(&tsk->sk);
2318 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2327 static void tipc_sk_remove(struct tipc_sock *tsk)
2329 struct sock *sk = &tsk->sk;
2330 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
2332 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
2333 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2338 static const struct rhashtable_params tsk_rht_params = {
2340 .head_offset = offsetof(struct tipc_sock, node),
2341 .key_offset = offsetof(struct tipc_sock, portid),
2342 .key_len = sizeof(u32), /* portid */
2343 .max_size = 1048576,
2345 .automatic_shrinking = true,
2348 int tipc_sk_rht_init(struct net *net)
2350 struct tipc_net *tn = net_generic(net, tipc_net_id);
2352 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
2355 void tipc_sk_rht_destroy(struct net *net)
2357 struct tipc_net *tn = net_generic(net, tipc_net_id);
2359 /* Wait for socket readers to complete */
2362 rhashtable_destroy(&tn->sk_rht);
2366 * tipc_setsockopt - set socket option
2367 * @sock: socket structure
2368 * @lvl: option level
2369 * @opt: option identifier
2370 * @ov: pointer to new option value
2371 * @ol: length of option value
2373 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
2374 * (to ease compatibility).
2376 * Returns 0 on success, errno otherwise
2378 static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2379 char __user *ov, unsigned int ol)
2381 struct sock *sk = sock->sk;
2382 struct tipc_sock *tsk = tipc_sk(sk);
2386 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2388 if (lvl != SOL_TIPC)
2389 return -ENOPROTOOPT;
2390 if (ol < sizeof(value))
2392 res = get_user(value, (u32 __user *)ov);
2399 case TIPC_IMPORTANCE:
2400 res = tsk_set_importance(tsk, value);
2402 case TIPC_SRC_DROPPABLE:
2403 if (sock->type != SOCK_STREAM)
2404 tsk_set_unreliable(tsk, value);
2408 case TIPC_DEST_DROPPABLE:
2409 tsk_set_unreturnable(tsk, value);
2411 case TIPC_CONN_TIMEOUT:
2412 tipc_sk(sk)->conn_timeout = value;
2413 /* no need to set "res", since already 0 at this point */
2425 * tipc_getsockopt - get socket option
2426 * @sock: socket structure
2427 * @lvl: option level
2428 * @opt: option identifier
2429 * @ov: receptacle for option value
2430 * @ol: receptacle for length of option value
2432 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
2433 * (to ease compatibility).
2435 * Returns 0 on success, errno otherwise
2437 static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2438 char __user *ov, int __user *ol)
2440 struct sock *sk = sock->sk;
2441 struct tipc_sock *tsk = tipc_sk(sk);
2446 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2447 return put_user(0, ol);
2448 if (lvl != SOL_TIPC)
2449 return -ENOPROTOOPT;
2450 res = get_user(len, ol);
2457 case TIPC_IMPORTANCE:
2458 value = tsk_importance(tsk);
2460 case TIPC_SRC_DROPPABLE:
2461 value = tsk_unreliable(tsk);
2463 case TIPC_DEST_DROPPABLE:
2464 value = tsk_unreturnable(tsk);
2466 case TIPC_CONN_TIMEOUT:
2467 value = tsk->conn_timeout;
2468 /* no need to set "res", since already 0 at this point */
2470 case TIPC_NODE_RECVQ_DEPTH:
2471 value = 0; /* was tipc_queue_size, now obsolete */
2473 case TIPC_SOCK_RECVQ_DEPTH:
2474 value = skb_queue_len(&sk->sk_receive_queue);
2483 return res; /* "get" failed */
2485 if (len < sizeof(value))
2488 if (copy_to_user(ov, &value, sizeof(value)))
2491 return put_user(sizeof(value), ol);
2494 static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
2496 struct sock *sk = sock->sk;
2497 struct tipc_sioc_ln_req lnr;
2498 void __user *argp = (void __user *)arg;
2501 case SIOCGETLINKNAME:
2502 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2504 if (!tipc_node_get_linkname(sock_net(sk),
2505 lnr.bearer_id & 0xffff, lnr.peer,
2506 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2507 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2511 return -EADDRNOTAVAIL;
2513 return -ENOIOCTLCMD;
2517 /* Protocol switches for the various types of TIPC sockets */
2519 static const struct proto_ops msg_ops = {
2520 .owner = THIS_MODULE,
2522 .release = tipc_release,
2524 .connect = tipc_connect,
2525 .socketpair = sock_no_socketpair,
2526 .accept = sock_no_accept,
2527 .getname = tipc_getname,
2529 .ioctl = tipc_ioctl,
2530 .listen = sock_no_listen,
2531 .shutdown = tipc_shutdown,
2532 .setsockopt = tipc_setsockopt,
2533 .getsockopt = tipc_getsockopt,
2534 .sendmsg = tipc_sendmsg,
2535 .recvmsg = tipc_recvmsg,
2536 .mmap = sock_no_mmap,
2537 .sendpage = sock_no_sendpage
2540 static const struct proto_ops packet_ops = {
2541 .owner = THIS_MODULE,
2543 .release = tipc_release,
2545 .connect = tipc_connect,
2546 .socketpair = sock_no_socketpair,
2547 .accept = tipc_accept,
2548 .getname = tipc_getname,
2550 .ioctl = tipc_ioctl,
2551 .listen = tipc_listen,
2552 .shutdown = tipc_shutdown,
2553 .setsockopt = tipc_setsockopt,
2554 .getsockopt = tipc_getsockopt,
2555 .sendmsg = tipc_send_packet,
2556 .recvmsg = tipc_recvmsg,
2557 .mmap = sock_no_mmap,
2558 .sendpage = sock_no_sendpage
2561 static const struct proto_ops stream_ops = {
2562 .owner = THIS_MODULE,
2564 .release = tipc_release,
2566 .connect = tipc_connect,
2567 .socketpair = sock_no_socketpair,
2568 .accept = tipc_accept,
2569 .getname = tipc_getname,
2571 .ioctl = tipc_ioctl,
2572 .listen = tipc_listen,
2573 .shutdown = tipc_shutdown,
2574 .setsockopt = tipc_setsockopt,
2575 .getsockopt = tipc_getsockopt,
2576 .sendmsg = tipc_send_stream,
2577 .recvmsg = tipc_recv_stream,
2578 .mmap = sock_no_mmap,
2579 .sendpage = sock_no_sendpage
2582 static const struct net_proto_family tipc_family_ops = {
2583 .owner = THIS_MODULE,
2585 .create = tipc_sk_create
2588 static struct proto tipc_proto = {
2590 .owner = THIS_MODULE,
2591 .obj_size = sizeof(struct tipc_sock),
2592 .sysctl_rmem = sysctl_tipc_rmem
2596 * tipc_socket_init - initialize TIPC socket interface
2598 * Returns 0 on success, errno otherwise
2600 int tipc_socket_init(void)
2604 res = proto_register(&tipc_proto, 1);
2606 pr_err("Failed to register TIPC protocol type\n");
2610 res = sock_register(&tipc_family_ops);
2612 pr_err("Failed to register TIPC socket type\n");
2613 proto_unregister(&tipc_proto);
2621 * tipc_socket_stop - stop TIPC socket interface
2623 void tipc_socket_stop(void)
2625 sock_unregister(tipc_family_ops.family);
2626 proto_unregister(&tipc_proto);
2629 /* Caller should hold socket lock for the passed tipc socket. */
2630 static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
2634 struct nlattr *nest;
2636 peer_node = tsk_peer_node(tsk);
2637 peer_port = tsk_peer_port(tsk);
2639 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2641 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2643 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2646 if (tsk->conn_type != 0) {
2647 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2649 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2651 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2654 nla_nest_end(skb, nest);
2659 nla_nest_cancel(skb, nest);
2664 /* Caller should hold socket lock for the passed tipc socket. */
2665 static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2666 struct tipc_sock *tsk)
2670 struct nlattr *attrs;
2671 struct net *net = sock_net(skb->sk);
2672 struct tipc_net *tn = net_generic(net, tipc_net_id);
2673 struct sock *sk = &tsk->sk;
2675 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2676 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
2680 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2682 goto genlmsg_cancel;
2683 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
2684 goto attr_msg_cancel;
2685 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
2686 goto attr_msg_cancel;
2688 if (tipc_sk_connected(sk)) {
2689 err = __tipc_nl_add_sk_con(skb, tsk);
2691 goto attr_msg_cancel;
2692 } else if (!list_empty(&tsk->publications)) {
2693 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2694 goto attr_msg_cancel;
2696 nla_nest_end(skb, attrs);
2697 genlmsg_end(skb, hdr);
2702 nla_nest_cancel(skb, attrs);
2704 genlmsg_cancel(skb, hdr);
2709 int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2712 struct tipc_sock *tsk;
2713 const struct bucket_table *tbl;
2714 struct rhash_head *pos;
2715 struct net *net = sock_net(skb->sk);
2716 struct tipc_net *tn = net_generic(net, tipc_net_id);
2717 u32 tbl_id = cb->args[0];
2718 u32 prev_portid = cb->args[1];
2721 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
2722 for (; tbl_id < tbl->size; tbl_id++) {
2723 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
2724 spin_lock_bh(&tsk->sk.sk_lock.slock);
2725 if (prev_portid && prev_portid != tsk->portid) {
2726 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2730 err = __tipc_nl_add_sk(skb, cb, tsk);
2732 prev_portid = tsk->portid;
2733 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2737 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2742 cb->args[0] = tbl_id;
2743 cb->args[1] = prev_portid;
2748 /* Caller should hold socket lock for the passed tipc socket. */
2749 static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2750 struct netlink_callback *cb,
2751 struct publication *publ)
2754 struct nlattr *attrs;
2756 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2757 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
2761 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2763 goto genlmsg_cancel;
2765 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2766 goto attr_msg_cancel;
2767 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2768 goto attr_msg_cancel;
2769 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2770 goto attr_msg_cancel;
2771 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2772 goto attr_msg_cancel;
2774 nla_nest_end(skb, attrs);
2775 genlmsg_end(skb, hdr);
2780 nla_nest_cancel(skb, attrs);
2782 genlmsg_cancel(skb, hdr);
2787 /* Caller should hold socket lock for the passed tipc socket. */
2788 static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2789 struct netlink_callback *cb,
2790 struct tipc_sock *tsk, u32 *last_publ)
2793 struct publication *p;
2796 list_for_each_entry(p, &tsk->publications, pport_list) {
2797 if (p->key == *last_publ)
2800 if (p->key != *last_publ) {
2801 /* We never set seq or call nl_dump_check_consistent()
2802 * this means that setting prev_seq here will cause the
2803 * consistence check to fail in the netlink callback
2804 * handler. Resulting in the last NLMSG_DONE message
2805 * having the NLM_F_DUMP_INTR flag set.
2812 p = list_first_entry(&tsk->publications, struct publication,
2816 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2817 err = __tipc_nl_add_sk_publ(skb, cb, p);
2819 *last_publ = p->key;
2828 int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2831 u32 tsk_portid = cb->args[0];
2832 u32 last_publ = cb->args[1];
2833 u32 done = cb->args[2];
2834 struct net *net = sock_net(skb->sk);
2835 struct tipc_sock *tsk;
2838 struct nlattr **attrs;
2839 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2841 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2845 if (!attrs[TIPC_NLA_SOCK])
2848 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2849 attrs[TIPC_NLA_SOCK],
2850 tipc_nl_sock_policy);
2854 if (!sock[TIPC_NLA_SOCK_REF])
2857 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
2863 tsk = tipc_sk_lookup(net, tsk_portid);
2867 lock_sock(&tsk->sk);
2868 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2871 release_sock(&tsk->sk);
2874 cb->args[0] = tsk_portid;
2875 cb->args[1] = last_publ;