2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2001-2007, 2012-2015, 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/jhash.h>
40 #include "name_table.h"
43 #include "name_distr.h"
46 #define SS_LISTENING -1 /* socket is listening */
47 #define SS_READY -2 /* socket is connectionless */
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_CONN_OK 0
53 #define TIPC_CONN_PROBING 1
54 #define TIPC_MAX_PORT 0xffffffff
55 #define TIPC_MIN_PORT 1
58 * struct tipc_sock - TIPC socket structure
59 * @sk: socket - interacts with 'port' and with user via the socket API
60 * @connected: non-zero if port is currently connected to a peer port
61 * @conn_type: TIPC type used when connection was established
62 * @conn_instance: TIPC instance used when connection was established
63 * @published: non-zero if port has one or more associated names
64 * @max_pkt: maximum packet size "hint" used when building messages sent by port
65 * @portid: unique port identity in TIPC socket hash table
66 * @phdr: preformatted message header used when sending messages
67 * @port_list: adjacent ports in TIPC's global list of ports
68 * @publications: list of publications for port
69 * @pub_count: total # of publications port has made during its lifetime
72 * @conn_timeout: the time we can wait for an unresponded setup request
73 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
74 * @link_cong: non-zero if owner must sleep because of link congestion
75 * @sent_unacked: # messages sent by socket, and not yet acked by peer
76 * @rcv_unacked: # messages read by user, but not yet acked back to peer
77 * @node: hash table node
78 * @rcu: rcu struct for tipc_sock
89 struct list_head sock_list;
90 struct list_head publications;
93 unsigned long probing_intv;
99 struct rhash_head node;
103 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
104 static void tipc_data_ready(struct sock *sk);
105 static void tipc_write_space(struct sock *sk);
106 static int tipc_release(struct socket *sock);
107 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
108 static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
109 static void tipc_sk_timeout(unsigned long data);
110 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
111 struct tipc_name_seq const *seq);
112 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
113 struct tipc_name_seq const *seq);
114 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
115 static int tipc_sk_insert(struct tipc_sock *tsk);
116 static void tipc_sk_remove(struct tipc_sock *tsk);
117 static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
119 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
121 static const struct proto_ops packet_ops;
122 static const struct proto_ops stream_ops;
123 static const struct proto_ops msg_ops;
125 static struct proto tipc_proto;
126 static struct proto tipc_proto_kern;
128 static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = {
129 [TIPC_NLA_SOCK_UNSPEC] = { .type = NLA_UNSPEC },
130 [TIPC_NLA_SOCK_ADDR] = { .type = NLA_U32 },
131 [TIPC_NLA_SOCK_REF] = { .type = NLA_U32 },
132 [TIPC_NLA_SOCK_CON] = { .type = NLA_NESTED },
133 [TIPC_NLA_SOCK_HAS_PUBL] = { .type = NLA_FLAG }
137 * Revised TIPC socket locking policy:
139 * Most socket operations take the standard socket lock when they start
140 * and hold it until they finish (or until they need to sleep). Acquiring
141 * this lock grants the owner exclusive access to the fields of the socket
142 * data structures, with the exception of the backlog queue. A few socket
143 * operations can be done without taking the socket lock because they only
144 * read socket information that never changes during the life of the socket.
146 * Socket operations may acquire the lock for the associated TIPC port if they
147 * need to perform an operation on the port. If any routine needs to acquire
148 * both the socket lock and the port lock it must take the socket lock first
149 * to avoid the risk of deadlock.
151 * The dispatcher handling incoming messages cannot grab the socket lock in
152 * the standard fashion, since invoked it runs at the BH level and cannot block.
153 * Instead, it checks to see if the socket lock is currently owned by someone,
154 * and either handles the message itself or adds it to the socket's backlog
155 * queue; in the latter case the queued message is processed once the process
156 * owning the socket lock releases it.
158 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
159 * the problem of a blocked socket operation preventing any other operations
160 * from occurring. However, applications must be careful if they have
161 * multiple threads trying to send (or receive) on the same socket, as these
162 * operations might interfere with each other. For example, doing a connect
163 * and a receive at the same time might allow the receive to consume the
164 * ACK message meant for the connect. While additional work could be done
165 * to try and overcome this, it doesn't seem to be worthwhile at the present.
167 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
168 * that another operation that must be performed in a non-blocking manner is
169 * not delayed for very long because the lock has already been taken.
171 * NOTE: This code assumes that certain fields of a port/socket pair are
172 * constant over its lifetime; such fields can be examined without taking
173 * the socket lock and/or port lock, and do not need to be re-read even
174 * after resuming processing after waiting. These fields include:
176 * - pointer to socket sk structure (aka tipc_sock structure)
177 * - pointer to port structure
181 static u32 tsk_own_node(struct tipc_sock *tsk)
183 return msg_prevnode(&tsk->phdr);
186 static u32 tsk_peer_node(struct tipc_sock *tsk)
188 return msg_destnode(&tsk->phdr);
191 static u32 tsk_peer_port(struct tipc_sock *tsk)
193 return msg_destport(&tsk->phdr);
196 static bool tsk_unreliable(struct tipc_sock *tsk)
198 return msg_src_droppable(&tsk->phdr) != 0;
201 static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
203 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
206 static bool tsk_unreturnable(struct tipc_sock *tsk)
208 return msg_dest_droppable(&tsk->phdr) != 0;
211 static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
213 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
216 static int tsk_importance(struct tipc_sock *tsk)
218 return msg_importance(&tsk->phdr);
221 static int tsk_set_importance(struct tipc_sock *tsk, int imp)
223 if (imp > TIPC_CRITICAL_IMPORTANCE)
225 msg_set_importance(&tsk->phdr, (u32)imp);
229 static struct tipc_sock *tipc_sk(const struct sock *sk)
231 return container_of(sk, struct tipc_sock, sk);
234 static int tsk_conn_cong(struct tipc_sock *tsk)
236 return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN;
240 * tsk_advance_rx_queue - discard first buffer in socket receive queue
242 * Caller must hold socket lock
244 static void tsk_advance_rx_queue(struct sock *sk)
246 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
250 * tsk_rej_rx_queue - reject all buffers in socket receive queue
252 * Caller must hold socket lock
254 static void tsk_rej_rx_queue(struct sock *sk)
258 u32 own_node = tsk_own_node(tipc_sk(sk));
260 while ((skb = __skb_dequeue(&sk->sk_receive_queue))) {
261 if (tipc_msg_reverse(own_node, skb, &dnode, TIPC_ERR_NO_PORT))
262 tipc_link_xmit_skb(sock_net(sk), skb, dnode, 0);
266 /* tsk_peer_msg - verify if message was sent by connected port's peer
268 * Handles cases where the node's network address has changed from
269 * the default of <0.0.0> to its configured setting.
271 static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
273 struct tipc_net *tn = net_generic(sock_net(&tsk->sk), tipc_net_id);
274 u32 peer_port = tsk_peer_port(tsk);
278 if (unlikely(!tsk->connected))
281 if (unlikely(msg_origport(msg) != peer_port))
284 orig_node = msg_orignode(msg);
285 peer_node = tsk_peer_node(tsk);
287 if (likely(orig_node == peer_node))
290 if (!orig_node && (peer_node == tn->own_addr))
293 if (!peer_node && (orig_node == tn->own_addr))
300 * tipc_sk_create - create a TIPC socket
301 * @net: network namespace (must be default network)
302 * @sock: pre-allocated socket structure
303 * @protocol: protocol indicator (must be 0)
304 * @kern: caused by kernel or by userspace?
306 * This routine creates additional data structures used by the TIPC socket,
307 * initializes them, and links them together.
309 * Returns 0 on success, errno otherwise
311 static int tipc_sk_create(struct net *net, struct socket *sock,
312 int protocol, int kern)
315 const struct proto_ops *ops;
318 struct tipc_sock *tsk;
319 struct tipc_msg *msg;
321 /* Validate arguments */
322 if (unlikely(protocol != 0))
323 return -EPROTONOSUPPORT;
325 switch (sock->type) {
328 state = SS_UNCONNECTED;
332 state = SS_UNCONNECTED;
343 /* Allocate socket's protocol area */
345 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
347 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
353 tsk->max_pkt = MAX_PKT_DEFAULT;
354 INIT_LIST_HEAD(&tsk->publications);
356 tn = net_generic(sock_net(sk), tipc_net_id);
357 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
360 /* Finish initializing socket data structures */
363 sock_init_data(sock, sk);
364 if (tipc_sk_insert(tsk)) {
365 pr_warn("Socket create failed; port numbrer exhausted\n");
368 msg_set_origport(msg, tsk->portid);
369 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
370 sk->sk_backlog_rcv = tipc_backlog_rcv;
371 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
372 sk->sk_data_ready = tipc_data_ready;
373 sk->sk_write_space = tipc_write_space;
374 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
375 tsk->sent_unacked = 0;
376 atomic_set(&tsk->dupl_rcvcnt, 0);
378 if (sock->state == SS_READY) {
379 tsk_set_unreturnable(tsk, true);
380 if (sock->type == SOCK_DGRAM)
381 tsk_set_unreliable(tsk, true);
387 * tipc_sock_create_local - create TIPC socket from inside TIPC module
388 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
390 * We cannot use sock_creat_kern here because it bumps module user count.
391 * Since socket owner and creator is the same module we must make sure
392 * that module count remains zero for module local sockets, otherwise
393 * we cannot do rmmod.
395 * Returns 0 on success, errno otherwise
397 int tipc_sock_create_local(struct net *net, int type, struct socket **res)
401 rc = sock_create_lite(AF_TIPC, type, 0, res);
403 pr_err("Failed to create kernel socket\n");
406 tipc_sk_create(net, *res, 0, 1);
412 * tipc_sock_release_local - release socket created by tipc_sock_create_local
413 * @sock: the socket to be released.
415 * Module reference count is not incremented when such sockets are created,
416 * so we must keep it from being decremented when they are released.
418 void tipc_sock_release_local(struct socket *sock)
426 * tipc_sock_accept_local - accept a connection on a socket created
427 * with tipc_sock_create_local. Use this function to avoid that
428 * module reference count is inadvertently incremented.
430 * @sock: the accepting socket
431 * @newsock: reference to the new socket to be created
432 * @flags: socket flags
435 int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
438 struct sock *sk = sock->sk;
441 ret = sock_create_lite(sk->sk_family, sk->sk_type,
442 sk->sk_protocol, newsock);
446 ret = tipc_accept(sock, *newsock, flags);
448 sock_release(*newsock);
451 (*newsock)->ops = sock->ops;
455 static void tipc_sk_callback(struct rcu_head *head)
457 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
463 * tipc_release - destroy a TIPC socket
464 * @sock: socket to destroy
466 * This routine cleans up any messages that are still queued on the socket.
467 * For DGRAM and RDM socket types, all queued messages are rejected.
468 * For SEQPACKET and STREAM socket types, the first message is rejected
469 * and any others are discarded. (If the first message on a STREAM socket
470 * is partially-read, it is discarded and the next one is rejected instead.)
472 * NOTE: Rejected messages are not necessarily returned to the sender! They
473 * are returned or discarded according to the "destination droppable" setting
474 * specified for the message by the sender.
476 * Returns 0 on success, errno otherwise
478 static int tipc_release(struct socket *sock)
480 struct sock *sk = sock->sk;
482 struct tipc_sock *tsk;
484 u32 dnode, probing_state;
487 * Exit if socket isn't fully initialized (occurs when a failed accept()
488 * releases a pre-allocated child socket that was never used)
498 * Reject all unreceived messages, except on an active connection
499 * (which disconnects locally & sends a 'FIN+' to peer)
501 dnode = tsk_peer_node(tsk);
502 while (sock->state != SS_DISCONNECTING) {
503 skb = __skb_dequeue(&sk->sk_receive_queue);
506 if (TIPC_SKB_CB(skb)->handle != NULL)
509 if ((sock->state == SS_CONNECTING) ||
510 (sock->state == SS_CONNECTED)) {
511 sock->state = SS_DISCONNECTING;
513 tipc_node_remove_conn(net, dnode, tsk->portid);
515 if (tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode,
517 tipc_link_xmit_skb(net, skb, dnode, 0);
521 tipc_sk_withdraw(tsk, 0, NULL);
522 probing_state = tsk->probing_state;
523 if (del_timer_sync(&sk->sk_timer) &&
524 probing_state != TIPC_CONN_PROBING)
527 if (tsk->connected) {
528 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
529 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
530 tsk_own_node(tsk), tsk_peer_port(tsk),
531 tsk->portid, TIPC_ERR_NO_PORT);
533 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
534 tipc_node_remove_conn(net, dnode, tsk->portid);
537 /* Discard any remaining (connection-based) messages in receive queue */
538 __skb_queue_purge(&sk->sk_receive_queue);
540 /* Reject any messages that accumulated in backlog queue */
541 sock->state = SS_DISCONNECTING;
544 call_rcu(&tsk->rcu, tipc_sk_callback);
551 * tipc_bind - associate or disassocate TIPC name(s) with a socket
552 * @sock: socket structure
553 * @uaddr: socket address describing name(s) and desired operation
554 * @uaddr_len: size of socket address data structure
556 * Name and name sequence binding is indicated using a positive scope value;
557 * a negative scope value unbinds the specified name. Specifying no name
558 * (i.e. a socket address length of 0) unbinds all names from the socket.
560 * Returns 0 on success, errno otherwise
562 * NOTE: This routine doesn't need to take the socket lock since it doesn't
563 * access any non-constant socket information.
565 static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
568 struct sock *sk = sock->sk;
569 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
570 struct tipc_sock *tsk = tipc_sk(sk);
574 if (unlikely(!uaddr_len)) {
575 res = tipc_sk_withdraw(tsk, 0, NULL);
579 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
583 if (addr->family != AF_TIPC) {
588 if (addr->addrtype == TIPC_ADDR_NAME)
589 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
590 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
595 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
596 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
597 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
602 res = (addr->scope > 0) ?
603 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
604 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
611 * tipc_getname - get port ID of socket or peer socket
612 * @sock: socket structure
613 * @uaddr: area for returned socket address
614 * @uaddr_len: area for returned length of socket address
615 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
617 * Returns 0 on success, errno otherwise
619 * NOTE: This routine doesn't need to take the socket lock since it only
620 * accesses socket information that is unchanging (or which changes in
621 * a completely predictable manner).
623 static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
624 int *uaddr_len, int peer)
626 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
627 struct tipc_sock *tsk = tipc_sk(sock->sk);
628 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
630 memset(addr, 0, sizeof(*addr));
632 if ((sock->state != SS_CONNECTED) &&
633 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
635 addr->addr.id.ref = tsk_peer_port(tsk);
636 addr->addr.id.node = tsk_peer_node(tsk);
638 addr->addr.id.ref = tsk->portid;
639 addr->addr.id.node = tn->own_addr;
642 *uaddr_len = sizeof(*addr);
643 addr->addrtype = TIPC_ADDR_ID;
644 addr->family = AF_TIPC;
646 addr->addr.name.domain = 0;
652 * tipc_poll - read and possibly block on pollmask
653 * @file: file structure associated with the socket
654 * @sock: socket for which to calculate the poll bits
657 * Returns pollmask value
660 * It appears that the usual socket locking mechanisms are not useful here
661 * since the pollmask info is potentially out-of-date the moment this routine
662 * exits. TCP and other protocols seem to rely on higher level poll routines
663 * to handle any preventable race conditions, so TIPC will do the same ...
665 * TIPC sets the returned events as follows:
667 * socket state flags set
668 * ------------ ---------
669 * unconnected no read flags
670 * POLLOUT if port is not congested
672 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
675 * connected POLLIN/POLLRDNORM if data in rx queue
676 * POLLOUT if port is not congested
678 * disconnecting POLLIN/POLLRDNORM/POLLHUP
681 * listening POLLIN if SYN in rx queue
684 * ready POLLIN/POLLRDNORM if data in rx queue
685 * [connectionless] POLLOUT (since port cannot be congested)
687 * IMPORTANT: The fact that a read or write operation is indicated does NOT
688 * imply that the operation will succeed, merely that it should be performed
689 * and will not block.
691 static unsigned int tipc_poll(struct file *file, struct socket *sock,
694 struct sock *sk = sock->sk;
695 struct tipc_sock *tsk = tipc_sk(sk);
698 sock_poll_wait(file, sk_sleep(sk), wait);
700 switch ((int)sock->state) {
707 if (!tsk->link_cong && !tsk_conn_cong(tsk))
712 if (!skb_queue_empty(&sk->sk_receive_queue))
713 mask |= (POLLIN | POLLRDNORM);
715 case SS_DISCONNECTING:
716 mask = (POLLIN | POLLRDNORM | POLLHUP);
724 * tipc_sendmcast - send multicast message
725 * @sock: socket structure
726 * @seq: destination address
727 * @msg: message to send
728 * @dsz: total length of message data
729 * @timeo: timeout to wait for wakeup
731 * Called from function tipc_sendmsg(), which has done all sanity checks
732 * Returns the number of bytes sent on success, or errno
734 static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
735 struct msghdr *msg, size_t dsz, long timeo)
737 struct sock *sk = sock->sk;
738 struct tipc_sock *tsk = tipc_sk(sk);
739 struct net *net = sock_net(sk);
740 struct tipc_msg *mhdr = &tsk->phdr;
741 struct sk_buff_head *pktchain = &sk->sk_write_queue;
742 struct iov_iter save = msg->msg_iter;
746 msg_set_type(mhdr, TIPC_MCAST_MSG);
747 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
748 msg_set_destport(mhdr, 0);
749 msg_set_destnode(mhdr, 0);
750 msg_set_nametype(mhdr, seq->type);
751 msg_set_namelower(mhdr, seq->lower);
752 msg_set_nameupper(mhdr, seq->upper);
753 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
756 mtu = tipc_bclink_get_mtu();
757 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, pktchain);
758 if (unlikely(rc < 0))
762 rc = tipc_bclink_xmit(net, pktchain);
763 if (likely(rc >= 0)) {
767 if (rc == -EMSGSIZE) {
768 msg->msg_iter = save;
771 if (rc != -ELINKCONG)
773 tipc_sk(sk)->link_cong = 1;
774 rc = tipc_wait_for_sndmsg(sock, &timeo);
776 __skb_queue_purge(pktchain);
782 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
783 * @arrvq: queue with arriving messages, to be cloned after destination lookup
784 * @inputq: queue with cloned messages, delivered to socket after dest lookup
786 * Multi-threaded: parallel calls with reference to same queues may occur
788 void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
789 struct sk_buff_head *inputq)
791 struct tipc_msg *msg;
792 struct tipc_plist dports;
794 u32 scope = TIPC_CLUSTER_SCOPE;
795 struct sk_buff_head tmpq;
797 struct sk_buff *skb, *_skb;
799 __skb_queue_head_init(&tmpq);
800 tipc_plist_init(&dports);
802 skb = tipc_skb_peek(arrvq, &inputq->lock);
803 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
805 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
807 if (in_own_node(net, msg_orignode(msg)))
808 scope = TIPC_NODE_SCOPE;
810 /* Create destination port list and message clones: */
811 tipc_nametbl_mc_translate(net,
812 msg_nametype(msg), msg_namelower(msg),
813 msg_nameupper(msg), scope, &dports);
814 portid = tipc_plist_pop(&dports);
815 for (; portid; portid = tipc_plist_pop(&dports)) {
816 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
818 msg_set_destport(buf_msg(_skb), portid);
819 __skb_queue_tail(&tmpq, _skb);
822 pr_warn("Failed to clone mcast rcv buffer\n");
824 /* Append to inputq if not already done by other thread */
825 spin_lock_bh(&inputq->lock);
826 if (skb_peek(arrvq) == skb) {
827 skb_queue_splice_tail_init(&tmpq, inputq);
828 kfree_skb(__skb_dequeue(arrvq));
830 spin_unlock_bh(&inputq->lock);
831 __skb_queue_purge(&tmpq);
834 tipc_sk_rcv(net, inputq);
838 * tipc_sk_proto_rcv - receive a connection mng protocol message
839 * @tsk: receiving socket
840 * @skb: pointer to message buffer. Set to NULL if buffer is consumed.
842 static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff **skb)
844 struct tipc_msg *msg = buf_msg(*skb);
847 u32 own_node = tsk_own_node(tsk);
848 /* Ignore if connection cannot be validated: */
849 if (!tsk_peer_msg(tsk, msg))
852 tsk->probing_state = TIPC_CONN_OK;
854 if (msg_type(msg) == CONN_ACK) {
855 conn_cong = tsk_conn_cong(tsk);
856 tsk->sent_unacked -= msg_msgcnt(msg);
858 tsk->sk.sk_write_space(&tsk->sk);
859 } else if (msg_type(msg) == CONN_PROBE) {
860 if (tipc_msg_reverse(own_node, *skb, &dnode, TIPC_OK)) {
861 msg_set_type(msg, CONN_PROBE_REPLY);
865 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
871 static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
873 struct sock *sk = sock->sk;
874 struct tipc_sock *tsk = tipc_sk(sk);
879 int err = sock_error(sk);
882 if (sock->state == SS_DISCONNECTING)
886 if (signal_pending(current))
887 return sock_intr_errno(*timeo_p);
889 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
890 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
891 finish_wait(sk_sleep(sk), &wait);
897 * tipc_sendmsg - send message in connectionless manner
898 * @sock: socket structure
899 * @m: message to send
900 * @dsz: amount of user data to be sent
902 * Message must have an destination specified explicitly.
903 * Used for SOCK_RDM and SOCK_DGRAM messages,
904 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
905 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
907 * Returns the number of bytes sent on success, or errno otherwise
909 static int tipc_sendmsg(struct socket *sock,
910 struct msghdr *m, size_t dsz)
912 struct sock *sk = sock->sk;
916 ret = __tipc_sendmsg(sock, m, dsz);
922 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
924 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
925 struct sock *sk = sock->sk;
926 struct tipc_sock *tsk = tipc_sk(sk);
927 struct net *net = sock_net(sk);
928 struct tipc_msg *mhdr = &tsk->phdr;
930 struct sk_buff_head *pktchain = &sk->sk_write_queue;
932 struct tipc_name_seq *seq = &dest->addr.nameseq;
933 struct iov_iter save;
939 return -EDESTADDRREQ;
941 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
942 (dest->family != AF_TIPC)))
945 if (dsz > TIPC_MAX_USER_MSG_SIZE)
948 if (unlikely(sock->state != SS_READY)) {
949 if (sock->state == SS_LISTENING)
951 if (sock->state != SS_UNCONNECTED)
955 if (dest->addrtype == TIPC_ADDR_NAME) {
956 tsk->conn_type = dest->addr.name.name.type;
957 tsk->conn_instance = dest->addr.name.name.instance;
961 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
963 if (dest->addrtype == TIPC_ADDR_MCAST) {
964 return tipc_sendmcast(sock, seq, m, dsz, timeo);
965 } else if (dest->addrtype == TIPC_ADDR_NAME) {
966 u32 type = dest->addr.name.name.type;
967 u32 inst = dest->addr.name.name.instance;
968 u32 domain = dest->addr.name.domain;
971 msg_set_type(mhdr, TIPC_NAMED_MSG);
972 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
973 msg_set_nametype(mhdr, type);
974 msg_set_nameinst(mhdr, inst);
975 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
976 dport = tipc_nametbl_translate(net, type, inst, &dnode);
977 msg_set_destnode(mhdr, dnode);
978 msg_set_destport(mhdr, dport);
979 if (unlikely(!dport && !dnode))
980 return -EHOSTUNREACH;
981 } else if (dest->addrtype == TIPC_ADDR_ID) {
982 dnode = dest->addr.id.node;
983 msg_set_type(mhdr, TIPC_DIRECT_MSG);
984 msg_set_lookup_scope(mhdr, 0);
985 msg_set_destnode(mhdr, dnode);
986 msg_set_destport(mhdr, dest->addr.id.ref);
987 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
992 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
993 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, pktchain);
998 skb = skb_peek(pktchain);
999 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
1000 rc = tipc_link_xmit(net, pktchain, dnode, tsk->portid);
1001 if (likely(rc >= 0)) {
1002 if (sock->state != SS_READY)
1003 sock->state = SS_CONNECTING;
1007 if (rc == -EMSGSIZE) {
1011 if (rc != -ELINKCONG)
1014 rc = tipc_wait_for_sndmsg(sock, &timeo);
1016 __skb_queue_purge(pktchain);
1022 static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
1024 struct sock *sk = sock->sk;
1025 struct tipc_sock *tsk = tipc_sk(sk);
1030 int err = sock_error(sk);
1033 if (sock->state == SS_DISCONNECTING)
1035 else if (sock->state != SS_CONNECTED)
1039 if (signal_pending(current))
1040 return sock_intr_errno(*timeo_p);
1042 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1043 done = sk_wait_event(sk, timeo_p,
1045 !tsk_conn_cong(tsk)) ||
1047 finish_wait(sk_sleep(sk), &wait);
1053 * tipc_send_stream - send stream-oriented data
1054 * @sock: socket structure
1056 * @dsz: total length of data to be transmitted
1058 * Used for SOCK_STREAM data.
1060 * Returns the number of bytes sent on success (or partial success),
1061 * or errno if no data sent
1063 static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1065 struct sock *sk = sock->sk;
1069 ret = __tipc_send_stream(sock, m, dsz);
1075 static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1077 struct sock *sk = sock->sk;
1078 struct net *net = sock_net(sk);
1079 struct tipc_sock *tsk = tipc_sk(sk);
1080 struct tipc_msg *mhdr = &tsk->phdr;
1081 struct sk_buff_head *pktchain = &sk->sk_write_queue;
1082 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1083 u32 portid = tsk->portid;
1087 uint mtu, send, sent = 0;
1088 struct iov_iter save;
1090 /* Handle implied connection establishment */
1091 if (unlikely(dest)) {
1092 rc = __tipc_sendmsg(sock, m, dsz);
1093 if (dsz && (dsz == rc))
1094 tsk->sent_unacked = 1;
1097 if (dsz > (uint)INT_MAX)
1100 if (unlikely(sock->state != SS_CONNECTED)) {
1101 if (sock->state == SS_DISCONNECTING)
1107 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1108 dnode = tsk_peer_node(tsk);
1113 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
1114 rc = tipc_msg_build(mhdr, m, sent, send, mtu, pktchain);
1115 if (unlikely(rc < 0))
1118 if (likely(!tsk_conn_cong(tsk))) {
1119 rc = tipc_link_xmit(net, pktchain, dnode, portid);
1121 tsk->sent_unacked++;
1127 if (rc == -EMSGSIZE) {
1128 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1133 if (rc != -ELINKCONG)
1137 rc = tipc_wait_for_sndpkt(sock, &timeo);
1139 __skb_queue_purge(pktchain);
1142 return sent ? sent : rc;
1146 * tipc_send_packet - send a connection-oriented message
1147 * @sock: socket structure
1148 * @m: message to send
1149 * @dsz: length of data to be transmitted
1151 * Used for SOCK_SEQPACKET messages.
1153 * Returns the number of bytes sent on success, or errno otherwise
1155 static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
1157 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1160 return tipc_send_stream(sock, m, dsz);
1163 /* tipc_sk_finish_conn - complete the setup of a connection
1165 static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
1168 struct sock *sk = &tsk->sk;
1169 struct net *net = sock_net(sk);
1170 struct tipc_msg *msg = &tsk->phdr;
1172 msg_set_destnode(msg, peer_node);
1173 msg_set_destport(msg, peer_port);
1174 msg_set_type(msg, TIPC_CONN_MSG);
1175 msg_set_lookup_scope(msg, 0);
1176 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1178 tsk->probing_intv = CONN_PROBING_INTERVAL;
1179 tsk->probing_state = TIPC_CONN_OK;
1181 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
1182 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1183 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
1187 * set_orig_addr - capture sender's address for received message
1188 * @m: descriptor for message info
1189 * @msg: received message header
1191 * Note: Address is not captured if not requested by receiver.
1193 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
1195 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
1198 addr->family = AF_TIPC;
1199 addr->addrtype = TIPC_ADDR_ID;
1200 memset(&addr->addr, 0, sizeof(addr->addr));
1201 addr->addr.id.ref = msg_origport(msg);
1202 addr->addr.id.node = msg_orignode(msg);
1203 addr->addr.name.domain = 0; /* could leave uninitialized */
1204 addr->scope = 0; /* could leave uninitialized */
1205 m->msg_namelen = sizeof(struct sockaddr_tipc);
1210 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
1211 * @m: descriptor for message info
1212 * @msg: received message header
1213 * @tsk: TIPC port associated with message
1215 * Note: Ancillary data is not captured if not requested by receiver.
1217 * Returns 0 if successful, otherwise errno
1219 static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1220 struct tipc_sock *tsk)
1228 if (likely(m->msg_controllen == 0))
1231 /* Optionally capture errored message object(s) */
1232 err = msg ? msg_errcode(msg) : 0;
1233 if (unlikely(err)) {
1235 anc_data[1] = msg_data_sz(msg);
1236 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1240 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1247 /* Optionally capture message destination object */
1248 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1249 switch (dest_type) {
1250 case TIPC_NAMED_MSG:
1252 anc_data[0] = msg_nametype(msg);
1253 anc_data[1] = msg_namelower(msg);
1254 anc_data[2] = msg_namelower(msg);
1256 case TIPC_MCAST_MSG:
1258 anc_data[0] = msg_nametype(msg);
1259 anc_data[1] = msg_namelower(msg);
1260 anc_data[2] = msg_nameupper(msg);
1263 has_name = (tsk->conn_type != 0);
1264 anc_data[0] = tsk->conn_type;
1265 anc_data[1] = tsk->conn_instance;
1266 anc_data[2] = tsk->conn_instance;
1272 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1280 static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
1282 struct net *net = sock_net(&tsk->sk);
1283 struct sk_buff *skb = NULL;
1284 struct tipc_msg *msg;
1285 u32 peer_port = tsk_peer_port(tsk);
1286 u32 dnode = tsk_peer_node(tsk);
1288 if (!tsk->connected)
1290 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1291 dnode, tsk_own_node(tsk), peer_port,
1292 tsk->portid, TIPC_OK);
1296 msg_set_msgcnt(msg, ack);
1297 tipc_link_xmit_skb(net, skb, dnode, msg_link_selector(msg));
1300 static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1302 struct sock *sk = sock->sk;
1304 long timeo = *timeop;
1308 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1309 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
1310 if (sock->state == SS_DISCONNECTING) {
1315 timeo = schedule_timeout(timeo);
1319 if (!skb_queue_empty(&sk->sk_receive_queue))
1321 err = sock_intr_errno(timeo);
1322 if (signal_pending(current))
1328 finish_wait(sk_sleep(sk), &wait);
1334 * tipc_recvmsg - receive packet-oriented message
1335 * @m: descriptor for message info
1336 * @buf_len: total size of user buffer area
1337 * @flags: receive flags
1339 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1340 * If the complete message doesn't fit in user area, truncate it.
1342 * Returns size of returned message data, errno otherwise
1344 static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1347 struct sock *sk = sock->sk;
1348 struct tipc_sock *tsk = tipc_sk(sk);
1349 struct sk_buff *buf;
1350 struct tipc_msg *msg;
1356 /* Catch invalid receive requests */
1357 if (unlikely(!buf_len))
1362 if (unlikely(sock->state == SS_UNCONNECTED)) {
1367 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1370 /* Look for a message in receive queue; wait if necessary */
1371 res = tipc_wait_for_rcvmsg(sock, &timeo);
1375 /* Look at first message in receive queue */
1376 buf = skb_peek(&sk->sk_receive_queue);
1378 sz = msg_data_sz(msg);
1379 err = msg_errcode(msg);
1381 /* Discard an empty non-errored message & try again */
1382 if ((!sz) && (!err)) {
1383 tsk_advance_rx_queue(sk);
1387 /* Capture sender's address (optional) */
1388 set_orig_addr(m, msg);
1390 /* Capture ancillary data (optional) */
1391 res = tipc_sk_anc_data_recv(m, msg, tsk);
1395 /* Capture message data (if valid) & compute return value (always) */
1397 if (unlikely(buf_len < sz)) {
1399 m->msg_flags |= MSG_TRUNC;
1401 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
1406 if ((sock->state == SS_READY) ||
1407 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1413 /* Consume received message (optional) */
1414 if (likely(!(flags & MSG_PEEK))) {
1415 if ((sock->state != SS_READY) &&
1416 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1417 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
1418 tsk->rcv_unacked = 0;
1420 tsk_advance_rx_queue(sk);
1428 * tipc_recv_stream - receive stream-oriented data
1429 * @m: descriptor for message info
1430 * @buf_len: total size of user buffer area
1431 * @flags: receive flags
1433 * Used for SOCK_STREAM messages only. If not enough data is available
1434 * will optionally wait for more; never truncates data.
1436 * Returns size of returned message data, errno otherwise
1438 static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1439 size_t buf_len, int flags)
1441 struct sock *sk = sock->sk;
1442 struct tipc_sock *tsk = tipc_sk(sk);
1443 struct sk_buff *buf;
1444 struct tipc_msg *msg;
1447 int sz_to_copy, target, needed;
1452 /* Catch invalid receive attempts */
1453 if (unlikely(!buf_len))
1458 if (unlikely(sock->state == SS_UNCONNECTED)) {
1463 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
1464 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1467 /* Look for a message in receive queue; wait if necessary */
1468 res = tipc_wait_for_rcvmsg(sock, &timeo);
1472 /* Look at first message in receive queue */
1473 buf = skb_peek(&sk->sk_receive_queue);
1475 sz = msg_data_sz(msg);
1476 err = msg_errcode(msg);
1478 /* Discard an empty non-errored message & try again */
1479 if ((!sz) && (!err)) {
1480 tsk_advance_rx_queue(sk);
1484 /* Optionally capture sender's address & ancillary data of first msg */
1485 if (sz_copied == 0) {
1486 set_orig_addr(m, msg);
1487 res = tipc_sk_anc_data_recv(m, msg, tsk);
1492 /* Capture message data (if valid) & compute return value (always) */
1494 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
1497 needed = (buf_len - sz_copied);
1498 sz_to_copy = (sz <= needed) ? sz : needed;
1500 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
1505 sz_copied += sz_to_copy;
1507 if (sz_to_copy < sz) {
1508 if (!(flags & MSG_PEEK))
1509 TIPC_SKB_CB(buf)->handle =
1510 (void *)(unsigned long)(offset + sz_to_copy);
1515 goto exit; /* can't add error msg to valid data */
1517 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1523 /* Consume received message (optional) */
1524 if (likely(!(flags & MSG_PEEK))) {
1525 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1526 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
1527 tsk->rcv_unacked = 0;
1529 tsk_advance_rx_queue(sk);
1532 /* Loop around if more data is required */
1533 if ((sz_copied < buf_len) && /* didn't get all requested data */
1534 (!skb_queue_empty(&sk->sk_receive_queue) ||
1535 (sz_copied < target)) && /* and more is ready or required */
1536 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1537 (!err)) /* and haven't reached a FIN */
1542 return sz_copied ? sz_copied : res;
1546 * tipc_write_space - wake up thread if port congestion is released
1549 static void tipc_write_space(struct sock *sk)
1551 struct socket_wq *wq;
1554 wq = rcu_dereference(sk->sk_wq);
1555 if (wq_has_sleeper(wq))
1556 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1557 POLLWRNORM | POLLWRBAND);
1562 * tipc_data_ready - wake up threads to indicate messages have been received
1564 * @len: the length of messages
1566 static void tipc_data_ready(struct sock *sk)
1568 struct socket_wq *wq;
1571 wq = rcu_dereference(sk->sk_wq);
1572 if (wq_has_sleeper(wq))
1573 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1574 POLLRDNORM | POLLRDBAND);
1579 * filter_connect - Handle all incoming messages for a connection-based socket
1581 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
1583 * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
1585 static int filter_connect(struct tipc_sock *tsk, struct sk_buff **skb)
1587 struct sock *sk = &tsk->sk;
1588 struct net *net = sock_net(sk);
1589 struct socket *sock = sk->sk_socket;
1590 struct tipc_msg *msg = buf_msg(*skb);
1591 int retval = -TIPC_ERR_NO_PORT;
1596 switch ((int)sock->state) {
1598 /* Accept only connection-based messages sent by peer */
1599 if (tsk_peer_msg(tsk, msg)) {
1600 if (unlikely(msg_errcode(msg))) {
1601 sock->state = SS_DISCONNECTING;
1603 /* let timer expire on it's own */
1604 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1611 /* Accept only ACK or NACK message */
1613 if (unlikely(!msg_connected(msg)))
1616 if (unlikely(msg_errcode(msg))) {
1617 sock->state = SS_DISCONNECTING;
1618 sk->sk_err = ECONNREFUSED;
1623 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
1624 sock->state = SS_DISCONNECTING;
1625 sk->sk_err = EINVAL;
1630 tipc_sk_finish_conn(tsk, msg_origport(msg), msg_orignode(msg));
1631 msg_set_importance(&tsk->phdr, msg_importance(msg));
1632 sock->state = SS_CONNECTED;
1634 /* If an incoming message is an 'ACK-', it should be
1635 * discarded here because it doesn't contain useful
1636 * data. In addition, we should try to wake up
1637 * connect() routine if sleeping.
1639 if (msg_data_sz(msg) == 0) {
1642 if (waitqueue_active(sk_sleep(sk)))
1643 wake_up_interruptible(sk_sleep(sk));
1648 case SS_UNCONNECTED:
1649 /* Accept only SYN message */
1650 if (!msg_connected(msg) && !(msg_errcode(msg)))
1653 case SS_DISCONNECTING:
1656 pr_err("Unknown socket state %u\n", sock->state);
1662 * rcvbuf_limit - get proper overload limit of socket receive queue
1666 * For all connection oriented messages, irrespective of importance,
1667 * the default overload value (i.e. 67MB) is set as limit.
1669 * For all connectionless messages, by default new queue limits are
1672 * TIPC_LOW_IMPORTANCE (4 MB)
1673 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1674 * TIPC_HIGH_IMPORTANCE (16 MB)
1675 * TIPC_CRITICAL_IMPORTANCE (32 MB)
1677 * Returns overload limit according to corresponding message importance
1679 static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1681 struct tipc_msg *msg = buf_msg(buf);
1683 if (msg_connected(msg))
1684 return sysctl_tipc_rmem[2];
1686 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1687 msg_importance(msg);
1691 * filter_rcv - validate incoming message
1693 * @skb: pointer to message. Set to NULL if buffer is consumed.
1695 * Enqueues message on receive queue if acceptable; optionally handles
1696 * disconnect indication for a connected socket.
1698 * Called with socket lock already taken
1700 * Returns 0 (TIPC_OK) if message was ok, -TIPC error code if rejected
1702 static int filter_rcv(struct sock *sk, struct sk_buff **skb)
1704 struct socket *sock = sk->sk_socket;
1705 struct tipc_sock *tsk = tipc_sk(sk);
1706 struct tipc_msg *msg = buf_msg(*skb);
1707 unsigned int limit = rcvbuf_limit(sk, *skb);
1710 if (unlikely(msg_user(msg) == CONN_MANAGER)) {
1711 tipc_sk_proto_rcv(tsk, skb);
1715 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1718 sk->sk_write_space(sk);
1723 /* Reject message if it is wrong sort of message for socket */
1724 if (msg_type(msg) > TIPC_DIRECT_MSG)
1725 return -TIPC_ERR_NO_PORT;
1727 if (sock->state == SS_READY) {
1728 if (msg_connected(msg))
1729 return -TIPC_ERR_NO_PORT;
1731 rc = filter_connect(tsk, skb);
1732 if (rc != TIPC_OK || !*skb)
1736 /* Reject message if there isn't room to queue it */
1737 if (sk_rmem_alloc_get(sk) + (*skb)->truesize >= limit)
1738 return -TIPC_ERR_OVERLOAD;
1740 /* Enqueue message */
1741 TIPC_SKB_CB(*skb)->handle = NULL;
1742 __skb_queue_tail(&sk->sk_receive_queue, *skb);
1743 skb_set_owner_r(*skb, sk);
1745 sk->sk_data_ready(sk);
1751 * tipc_backlog_rcv - handle incoming message from backlog queue
1755 * Caller must hold socket lock
1759 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
1764 struct tipc_sock *tsk = tipc_sk(sk);
1765 struct net *net = sock_net(sk);
1766 uint truesize = skb->truesize;
1768 err = filter_rcv(sk, &skb);
1770 dcnt = &tsk->dupl_rcvcnt;
1771 if (atomic_read(dcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1772 atomic_add(truesize, dcnt);
1775 if (!err || tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode, -err))
1776 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
1781 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1782 * inputq and try adding them to socket or backlog queue
1783 * @inputq: list of incoming buffers with potentially different destinations
1784 * @sk: socket where the buffers should be enqueued
1785 * @dport: port number for the socket
1786 * @_skb: returned buffer to be forwarded or rejected, if applicable
1788 * Caller must hold socket lock
1790 * Returns TIPC_OK if all buffers enqueued, otherwise -TIPC_ERR_OVERLOAD
1791 * or -TIPC_ERR_NO_PORT
1793 static int tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
1794 u32 dport, struct sk_buff **_skb)
1799 struct sk_buff *skb;
1800 unsigned long time_limit = jiffies + 2;
1802 while (skb_queue_len(inputq)) {
1803 if (unlikely(time_after_eq(jiffies, time_limit)))
1805 skb = tipc_skb_dequeue(inputq, dport);
1808 if (!sock_owned_by_user(sk)) {
1809 err = filter_rcv(sk, &skb);
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 return -TIPC_ERR_OVERLOAD;
1828 * tipc_sk_rcv - handle a chain of incoming buffers
1829 * @inputq: buffer list containing the buffers
1830 * Consumes all buffers in list until inputq is empty
1831 * Note: may be called in multiple threads referring to the same queue
1832 * Returns 0 if last buffer was accepted, otherwise -EHOSTUNREACH
1833 * Only node local calls check the return value, sending single-buffer queues
1835 int tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
1837 u32 dnode, dport = 0;
1838 int err = -TIPC_ERR_NO_PORT;
1839 struct sk_buff *skb;
1840 struct tipc_sock *tsk;
1841 struct tipc_net *tn;
1844 while (skb_queue_len(inputq)) {
1846 dport = tipc_skb_peek_port(inputq, dport);
1847 tsk = tipc_sk_lookup(net, dport);
1850 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
1851 err = tipc_sk_enqueue(inputq, sk, dport, &skb);
1852 spin_unlock_bh(&sk->sk_lock.slock);
1857 skb = tipc_skb_dequeue(inputq, dport);
1861 if (tipc_msg_lookup_dest(net, skb, &dnode, &err))
1864 dnode = msg_destnode(buf_msg(skb));
1867 tn = net_generic(net, tipc_net_id);
1868 if (!tipc_msg_reverse(tn->own_addr, skb, &dnode, -err))
1871 tipc_link_xmit_skb(net, skb, dnode, dport);
1873 return err ? -EHOSTUNREACH : 0;
1876 static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1878 struct sock *sk = sock->sk;
1883 int err = sock_error(sk);
1888 if (signal_pending(current))
1889 return sock_intr_errno(*timeo_p);
1891 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1892 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1893 finish_wait(sk_sleep(sk), &wait);
1899 * tipc_connect - establish a connection to another TIPC port
1900 * @sock: socket structure
1901 * @dest: socket address for destination port
1902 * @destlen: size of socket address data structure
1903 * @flags: file-related flags associated with socket
1905 * Returns 0 on success, errno otherwise
1907 static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1908 int destlen, int flags)
1910 struct sock *sk = sock->sk;
1911 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1912 struct msghdr m = {NULL,};
1913 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1914 socket_state previous;
1919 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
1920 if (sock->state == SS_READY) {
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 = sock->state;
1937 switch (sock->state) {
1938 case SS_UNCONNECTED:
1939 /* Send a 'SYN-' to destination */
1941 m.msg_namelen = destlen;
1943 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1944 * indicate send_msg() is never blocked.
1947 m.msg_flags = MSG_DONTWAIT;
1949 res = __tipc_sendmsg(sock, &m, 0);
1950 if ((res < 0) && (res != -EWOULDBLOCK))
1953 /* Just entered SS_CONNECTING state; the only
1954 * difference is that return value in non-blocking
1955 * case is EINPROGRESS, rather than EALREADY.
1959 if (previous == SS_CONNECTING)
1963 timeout = msecs_to_jiffies(timeout);
1964 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1965 res = tipc_wait_for_connect(sock, &timeout);
1980 * tipc_listen - allow socket to listen for incoming connections
1981 * @sock: socket structure
1984 * Returns 0 on success, errno otherwise
1986 static int tipc_listen(struct socket *sock, int len)
1988 struct sock *sk = sock->sk;
1993 if (sock->state != SS_UNCONNECTED)
1996 sock->state = SS_LISTENING;
2004 static int tipc_wait_for_accept(struct socket *sock, long timeo)
2006 struct sock *sk = sock->sk;
2010 /* True wake-one mechanism for incoming connections: only
2011 * one process gets woken up, not the 'whole herd'.
2012 * Since we do not 'race & poll' for established sockets
2013 * anymore, the common case will execute the loop only once.
2016 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2017 TASK_INTERRUPTIBLE);
2018 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
2020 timeo = schedule_timeout(timeo);
2024 if (!skb_queue_empty(&sk->sk_receive_queue))
2027 if (sock->state != SS_LISTENING)
2029 err = sock_intr_errno(timeo);
2030 if (signal_pending(current))
2036 finish_wait(sk_sleep(sk), &wait);
2041 * tipc_accept - wait for connection request
2042 * @sock: listening socket
2043 * @newsock: new socket that is to be connected
2044 * @flags: file-related flags associated with socket
2046 * Returns 0 on success, errno otherwise
2048 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
2050 struct sock *new_sk, *sk = sock->sk;
2051 struct sk_buff *buf;
2052 struct tipc_sock *new_tsock;
2053 struct tipc_msg *msg;
2059 if (sock->state != SS_LISTENING) {
2063 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2064 res = tipc_wait_for_accept(sock, timeo);
2068 buf = skb_peek(&sk->sk_receive_queue);
2070 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
2074 new_sk = new_sock->sk;
2075 new_tsock = tipc_sk(new_sk);
2078 /* we lock on new_sk; but lockdep sees the lock on sk */
2079 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
2082 * Reject any stray messages received by new socket
2083 * before the socket lock was taken (very, very unlikely)
2085 tsk_rej_rx_queue(new_sk);
2087 /* Connect new socket to it's peer */
2088 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
2089 new_sock->state = SS_CONNECTED;
2091 tsk_set_importance(new_tsock, msg_importance(msg));
2092 if (msg_named(msg)) {
2093 new_tsock->conn_type = msg_nametype(msg);
2094 new_tsock->conn_instance = msg_nameinst(msg);
2098 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2099 * Respond to 'SYN+' by queuing it on new socket.
2101 if (!msg_data_sz(msg)) {
2102 struct msghdr m = {NULL,};
2104 tsk_advance_rx_queue(sk);
2105 __tipc_send_stream(new_sock, &m, 0);
2107 __skb_dequeue(&sk->sk_receive_queue);
2108 __skb_queue_head(&new_sk->sk_receive_queue, buf);
2109 skb_set_owner_r(buf, new_sk);
2111 release_sock(new_sk);
2118 * tipc_shutdown - shutdown socket connection
2119 * @sock: socket structure
2120 * @how: direction to close (must be SHUT_RDWR)
2122 * Terminates connection (if necessary), then purges socket's receive queue.
2124 * Returns 0 on success, errno otherwise
2126 static int tipc_shutdown(struct socket *sock, int how)
2128 struct sock *sk = sock->sk;
2129 struct net *net = sock_net(sk);
2130 struct tipc_sock *tsk = tipc_sk(sk);
2131 struct sk_buff *skb;
2135 if (how != SHUT_RDWR)
2140 switch (sock->state) {
2145 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
2146 skb = __skb_dequeue(&sk->sk_receive_queue);
2148 if (TIPC_SKB_CB(skb)->handle != NULL) {
2152 if (tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode,
2153 TIPC_CONN_SHUTDOWN))
2154 tipc_link_xmit_skb(net, skb, dnode,
2156 tipc_node_remove_conn(net, dnode, tsk->portid);
2158 dnode = tsk_peer_node(tsk);
2160 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
2161 TIPC_CONN_MSG, SHORT_H_SIZE,
2162 0, dnode, tsk_own_node(tsk),
2164 tsk->portid, TIPC_CONN_SHUTDOWN);
2165 tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
2168 sock->state = SS_DISCONNECTING;
2169 tipc_node_remove_conn(net, dnode, tsk->portid);
2172 case SS_DISCONNECTING:
2174 /* Discard any unreceived messages */
2175 __skb_queue_purge(&sk->sk_receive_queue);
2177 /* Wake up anyone sleeping in poll */
2178 sk->sk_state_change(sk);
2190 static void tipc_sk_timeout(unsigned long data)
2192 struct tipc_sock *tsk = (struct tipc_sock *)data;
2193 struct sock *sk = &tsk->sk;
2194 struct sk_buff *skb = NULL;
2195 u32 peer_port, peer_node;
2196 u32 own_node = tsk_own_node(tsk);
2199 if (!tsk->connected) {
2203 peer_port = tsk_peer_port(tsk);
2204 peer_node = tsk_peer_node(tsk);
2206 if (tsk->probing_state == TIPC_CONN_PROBING) {
2207 /* Previous probe not answered -> self abort */
2208 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
2209 TIPC_CONN_MSG, SHORT_H_SIZE, 0,
2210 own_node, peer_node, tsk->portid,
2211 peer_port, TIPC_ERR_NO_PORT);
2213 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2214 INT_H_SIZE, 0, peer_node, own_node,
2215 peer_port, tsk->portid, TIPC_OK);
2216 tsk->probing_state = TIPC_CONN_PROBING;
2217 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
2221 tipc_link_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
2226 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
2227 struct tipc_name_seq const *seq)
2229 struct net *net = sock_net(&tsk->sk);
2230 struct publication *publ;
2235 key = tsk->portid + tsk->pub_count + 1;
2236 if (key == tsk->portid)
2239 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
2240 scope, tsk->portid, key);
2241 if (unlikely(!publ))
2244 list_add(&publ->pport_list, &tsk->publications);
2250 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
2251 struct tipc_name_seq const *seq)
2253 struct net *net = sock_net(&tsk->sk);
2254 struct publication *publ;
2255 struct publication *safe;
2258 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
2260 if (publ->scope != scope)
2262 if (publ->type != seq->type)
2264 if (publ->lower != seq->lower)
2266 if (publ->upper != seq->upper)
2268 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2269 publ->ref, publ->key);
2273 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2274 publ->ref, publ->key);
2277 if (list_empty(&tsk->publications))
2282 /* tipc_sk_reinit: set non-zero address in all existing sockets
2283 * when we go from standalone to network mode.
2285 void tipc_sk_reinit(struct net *net)
2287 struct tipc_net *tn = net_generic(net, tipc_net_id);
2288 const struct bucket_table *tbl;
2289 struct rhash_head *pos;
2290 struct tipc_sock *tsk;
2291 struct tipc_msg *msg;
2295 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
2296 for (i = 0; i < tbl->size; i++) {
2297 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2298 spin_lock_bh(&tsk->sk.sk_lock.slock);
2300 msg_set_prevnode(msg, tn->own_addr);
2301 msg_set_orignode(msg, tn->own_addr);
2302 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2308 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
2310 struct tipc_net *tn = net_generic(net, tipc_net_id);
2311 struct tipc_sock *tsk;
2314 tsk = rhashtable_lookup(&tn->sk_rht, &portid);
2316 sock_hold(&tsk->sk);
2322 static int tipc_sk_insert(struct tipc_sock *tsk)
2324 struct sock *sk = &tsk->sk;
2325 struct net *net = sock_net(sk);
2326 struct tipc_net *tn = net_generic(net, tipc_net_id);
2327 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2328 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2330 while (remaining--) {
2332 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2333 portid = TIPC_MIN_PORT;
2334 tsk->portid = portid;
2335 sock_hold(&tsk->sk);
2336 if (rhashtable_lookup_insert(&tn->sk_rht, &tsk->node))
2344 static void tipc_sk_remove(struct tipc_sock *tsk)
2346 struct sock *sk = &tsk->sk;
2347 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
2349 if (rhashtable_remove(&tn->sk_rht, &tsk->node)) {
2350 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2355 int tipc_sk_rht_init(struct net *net)
2357 struct tipc_net *tn = net_generic(net, tipc_net_id);
2358 struct rhashtable_params rht_params = {
2360 .head_offset = offsetof(struct tipc_sock, node),
2361 .key_offset = offsetof(struct tipc_sock, portid),
2362 .key_len = sizeof(u32), /* portid */
2364 .max_shift = 20, /* 1M */
2365 .min_shift = 8, /* 256 */
2368 return rhashtable_init(&tn->sk_rht, &rht_params);
2371 void tipc_sk_rht_destroy(struct net *net)
2373 struct tipc_net *tn = net_generic(net, tipc_net_id);
2375 /* Wait for socket readers to complete */
2378 rhashtable_destroy(&tn->sk_rht);
2382 * tipc_setsockopt - set socket option
2383 * @sock: socket structure
2384 * @lvl: option level
2385 * @opt: option identifier
2386 * @ov: pointer to new option value
2387 * @ol: length of option value
2389 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
2390 * (to ease compatibility).
2392 * Returns 0 on success, errno otherwise
2394 static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2395 char __user *ov, unsigned int ol)
2397 struct sock *sk = sock->sk;
2398 struct tipc_sock *tsk = tipc_sk(sk);
2402 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2404 if (lvl != SOL_TIPC)
2405 return -ENOPROTOOPT;
2406 if (ol < sizeof(value))
2408 res = get_user(value, (u32 __user *)ov);
2415 case TIPC_IMPORTANCE:
2416 res = tsk_set_importance(tsk, value);
2418 case TIPC_SRC_DROPPABLE:
2419 if (sock->type != SOCK_STREAM)
2420 tsk_set_unreliable(tsk, value);
2424 case TIPC_DEST_DROPPABLE:
2425 tsk_set_unreturnable(tsk, value);
2427 case TIPC_CONN_TIMEOUT:
2428 tipc_sk(sk)->conn_timeout = value;
2429 /* no need to set "res", since already 0 at this point */
2441 * tipc_getsockopt - get socket option
2442 * @sock: socket structure
2443 * @lvl: option level
2444 * @opt: option identifier
2445 * @ov: receptacle for option value
2446 * @ol: receptacle for length of option value
2448 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
2449 * (to ease compatibility).
2451 * Returns 0 on success, errno otherwise
2453 static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2454 char __user *ov, int __user *ol)
2456 struct sock *sk = sock->sk;
2457 struct tipc_sock *tsk = tipc_sk(sk);
2462 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2463 return put_user(0, ol);
2464 if (lvl != SOL_TIPC)
2465 return -ENOPROTOOPT;
2466 res = get_user(len, ol);
2473 case TIPC_IMPORTANCE:
2474 value = tsk_importance(tsk);
2476 case TIPC_SRC_DROPPABLE:
2477 value = tsk_unreliable(tsk);
2479 case TIPC_DEST_DROPPABLE:
2480 value = tsk_unreturnable(tsk);
2482 case TIPC_CONN_TIMEOUT:
2483 value = tsk->conn_timeout;
2484 /* no need to set "res", since already 0 at this point */
2486 case TIPC_NODE_RECVQ_DEPTH:
2487 value = 0; /* was tipc_queue_size, now obsolete */
2489 case TIPC_SOCK_RECVQ_DEPTH:
2490 value = skb_queue_len(&sk->sk_receive_queue);
2499 return res; /* "get" failed */
2501 if (len < sizeof(value))
2504 if (copy_to_user(ov, &value, sizeof(value)))
2507 return put_user(sizeof(value), ol);
2510 static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
2512 struct sock *sk = sock->sk;
2513 struct tipc_sioc_ln_req lnr;
2514 void __user *argp = (void __user *)arg;
2517 case SIOCGETLINKNAME:
2518 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2520 if (!tipc_node_get_linkname(sock_net(sk),
2521 lnr.bearer_id & 0xffff, lnr.peer,
2522 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2523 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2527 return -EADDRNOTAVAIL;
2529 return -ENOIOCTLCMD;
2533 /* Protocol switches for the various types of TIPC sockets */
2535 static const struct proto_ops msg_ops = {
2536 .owner = THIS_MODULE,
2538 .release = tipc_release,
2540 .connect = tipc_connect,
2541 .socketpair = sock_no_socketpair,
2542 .accept = sock_no_accept,
2543 .getname = tipc_getname,
2545 .ioctl = tipc_ioctl,
2546 .listen = sock_no_listen,
2547 .shutdown = tipc_shutdown,
2548 .setsockopt = tipc_setsockopt,
2549 .getsockopt = tipc_getsockopt,
2550 .sendmsg = tipc_sendmsg,
2551 .recvmsg = tipc_recvmsg,
2552 .mmap = sock_no_mmap,
2553 .sendpage = sock_no_sendpage
2556 static const struct proto_ops packet_ops = {
2557 .owner = THIS_MODULE,
2559 .release = tipc_release,
2561 .connect = tipc_connect,
2562 .socketpair = sock_no_socketpair,
2563 .accept = tipc_accept,
2564 .getname = tipc_getname,
2566 .ioctl = tipc_ioctl,
2567 .listen = tipc_listen,
2568 .shutdown = tipc_shutdown,
2569 .setsockopt = tipc_setsockopt,
2570 .getsockopt = tipc_getsockopt,
2571 .sendmsg = tipc_send_packet,
2572 .recvmsg = tipc_recvmsg,
2573 .mmap = sock_no_mmap,
2574 .sendpage = sock_no_sendpage
2577 static const struct proto_ops stream_ops = {
2578 .owner = THIS_MODULE,
2580 .release = tipc_release,
2582 .connect = tipc_connect,
2583 .socketpair = sock_no_socketpair,
2584 .accept = tipc_accept,
2585 .getname = tipc_getname,
2587 .ioctl = tipc_ioctl,
2588 .listen = tipc_listen,
2589 .shutdown = tipc_shutdown,
2590 .setsockopt = tipc_setsockopt,
2591 .getsockopt = tipc_getsockopt,
2592 .sendmsg = tipc_send_stream,
2593 .recvmsg = tipc_recv_stream,
2594 .mmap = sock_no_mmap,
2595 .sendpage = sock_no_sendpage
2598 static const struct net_proto_family tipc_family_ops = {
2599 .owner = THIS_MODULE,
2601 .create = tipc_sk_create
2604 static struct proto tipc_proto = {
2606 .owner = THIS_MODULE,
2607 .obj_size = sizeof(struct tipc_sock),
2608 .sysctl_rmem = sysctl_tipc_rmem
2611 static struct proto tipc_proto_kern = {
2613 .obj_size = sizeof(struct tipc_sock),
2614 .sysctl_rmem = sysctl_tipc_rmem
2618 * tipc_socket_init - initialize TIPC socket interface
2620 * Returns 0 on success, errno otherwise
2622 int tipc_socket_init(void)
2626 res = proto_register(&tipc_proto, 1);
2628 pr_err("Failed to register TIPC protocol type\n");
2632 res = sock_register(&tipc_family_ops);
2634 pr_err("Failed to register TIPC socket type\n");
2635 proto_unregister(&tipc_proto);
2643 * tipc_socket_stop - stop TIPC socket interface
2645 void tipc_socket_stop(void)
2647 sock_unregister(tipc_family_ops.family);
2648 proto_unregister(&tipc_proto);
2651 /* Caller should hold socket lock for the passed tipc socket. */
2652 static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
2656 struct nlattr *nest;
2658 peer_node = tsk_peer_node(tsk);
2659 peer_port = tsk_peer_port(tsk);
2661 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2663 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2665 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2668 if (tsk->conn_type != 0) {
2669 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2671 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2673 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2676 nla_nest_end(skb, nest);
2681 nla_nest_cancel(skb, nest);
2686 /* Caller should hold socket lock for the passed tipc socket. */
2687 static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2688 struct tipc_sock *tsk)
2692 struct nlattr *attrs;
2693 struct net *net = sock_net(skb->sk);
2694 struct tipc_net *tn = net_generic(net, tipc_net_id);
2696 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2697 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
2701 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2703 goto genlmsg_cancel;
2704 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
2705 goto attr_msg_cancel;
2706 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
2707 goto attr_msg_cancel;
2709 if (tsk->connected) {
2710 err = __tipc_nl_add_sk_con(skb, tsk);
2712 goto attr_msg_cancel;
2713 } else if (!list_empty(&tsk->publications)) {
2714 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2715 goto attr_msg_cancel;
2717 nla_nest_end(skb, attrs);
2718 genlmsg_end(skb, hdr);
2723 nla_nest_cancel(skb, attrs);
2725 genlmsg_cancel(skb, hdr);
2730 int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2733 struct tipc_sock *tsk;
2734 const struct bucket_table *tbl;
2735 struct rhash_head *pos;
2736 struct net *net = sock_net(skb->sk);
2737 struct tipc_net *tn = net_generic(net, tipc_net_id);
2738 u32 tbl_id = cb->args[0];
2739 u32 prev_portid = cb->args[1];
2742 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
2743 for (; tbl_id < tbl->size; tbl_id++) {
2744 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
2745 spin_lock_bh(&tsk->sk.sk_lock.slock);
2746 if (prev_portid && prev_portid != tsk->portid) {
2747 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2751 err = __tipc_nl_add_sk(skb, cb, tsk);
2753 prev_portid = tsk->portid;
2754 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2758 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2763 cb->args[0] = tbl_id;
2764 cb->args[1] = prev_portid;
2769 /* Caller should hold socket lock for the passed tipc socket. */
2770 static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2771 struct netlink_callback *cb,
2772 struct publication *publ)
2775 struct nlattr *attrs;
2777 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2778 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
2782 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2784 goto genlmsg_cancel;
2786 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2787 goto attr_msg_cancel;
2788 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2789 goto attr_msg_cancel;
2790 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2791 goto attr_msg_cancel;
2792 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2793 goto attr_msg_cancel;
2795 nla_nest_end(skb, attrs);
2796 genlmsg_end(skb, hdr);
2801 nla_nest_cancel(skb, attrs);
2803 genlmsg_cancel(skb, hdr);
2808 /* Caller should hold socket lock for the passed tipc socket. */
2809 static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2810 struct netlink_callback *cb,
2811 struct tipc_sock *tsk, u32 *last_publ)
2814 struct publication *p;
2817 list_for_each_entry(p, &tsk->publications, pport_list) {
2818 if (p->key == *last_publ)
2821 if (p->key != *last_publ) {
2822 /* We never set seq or call nl_dump_check_consistent()
2823 * this means that setting prev_seq here will cause the
2824 * consistence check to fail in the netlink callback
2825 * handler. Resulting in the last NLMSG_DONE message
2826 * having the NLM_F_DUMP_INTR flag set.
2833 p = list_first_entry(&tsk->publications, struct publication,
2837 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2838 err = __tipc_nl_add_sk_publ(skb, cb, p);
2840 *last_publ = p->key;
2849 int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2852 u32 tsk_portid = cb->args[0];
2853 u32 last_publ = cb->args[1];
2854 u32 done = cb->args[2];
2855 struct net *net = sock_net(skb->sk);
2856 struct tipc_sock *tsk;
2859 struct nlattr **attrs;
2860 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2862 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2866 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2867 attrs[TIPC_NLA_SOCK],
2868 tipc_nl_sock_policy);
2872 if (!sock[TIPC_NLA_SOCK_REF])
2875 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
2881 tsk = tipc_sk_lookup(net, tsk_portid);
2885 lock_sock(&tsk->sk);
2886 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2889 release_sock(&tsk->sk);
2892 cb->args[0] = tsk_portid;
2893 cb->args[1] = last_publ;