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>
39 #include "name_table.h"
42 #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 * @remote: 'connected' peer for dgram/rdm
78 * @node: hash table node
79 * @rcu: rcu struct for tipc_sock
90 struct list_head sock_list;
91 struct list_head publications;
94 unsigned long probing_intv;
100 struct sockaddr_tipc remote;
101 struct rhash_head node;
105 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
106 static void tipc_data_ready(struct sock *sk);
107 static void tipc_write_space(struct sock *sk);
108 static void tipc_sock_destruct(struct sock *sk);
109 static int tipc_release(struct socket *sock);
110 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
111 static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
112 static void tipc_sk_timeout(unsigned long data);
113 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
114 struct tipc_name_seq const *seq);
115 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
116 struct tipc_name_seq const *seq);
117 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
118 static int tipc_sk_insert(struct tipc_sock *tsk);
119 static void tipc_sk_remove(struct tipc_sock *tsk);
120 static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
122 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
124 static const struct proto_ops packet_ops;
125 static const struct proto_ops stream_ops;
126 static const struct proto_ops msg_ops;
127 static struct proto tipc_proto;
129 static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = {
130 [TIPC_NLA_SOCK_UNSPEC] = { .type = NLA_UNSPEC },
131 [TIPC_NLA_SOCK_ADDR] = { .type = NLA_U32 },
132 [TIPC_NLA_SOCK_REF] = { .type = NLA_U32 },
133 [TIPC_NLA_SOCK_CON] = { .type = NLA_NESTED },
134 [TIPC_NLA_SOCK_HAS_PUBL] = { .type = NLA_FLAG }
137 static const struct rhashtable_params tsk_rht_params;
140 * Revised TIPC socket locking policy:
142 * Most socket operations take the standard socket lock when they start
143 * and hold it until they finish (or until they need to sleep). Acquiring
144 * this lock grants the owner exclusive access to the fields of the socket
145 * data structures, with the exception of the backlog queue. A few socket
146 * operations can be done without taking the socket lock because they only
147 * read socket information that never changes during the life of the socket.
149 * Socket operations may acquire the lock for the associated TIPC port if they
150 * need to perform an operation on the port. If any routine needs to acquire
151 * both the socket lock and the port lock it must take the socket lock first
152 * to avoid the risk of deadlock.
154 * The dispatcher handling incoming messages cannot grab the socket lock in
155 * the standard fashion, since invoked it runs at the BH level and cannot block.
156 * Instead, it checks to see if the socket lock is currently owned by someone,
157 * and either handles the message itself or adds it to the socket's backlog
158 * queue; in the latter case the queued message is processed once the process
159 * owning the socket lock releases it.
161 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
162 * the problem of a blocked socket operation preventing any other operations
163 * from occurring. However, applications must be careful if they have
164 * multiple threads trying to send (or receive) on the same socket, as these
165 * operations might interfere with each other. For example, doing a connect
166 * and a receive at the same time might allow the receive to consume the
167 * ACK message meant for the connect. While additional work could be done
168 * to try and overcome this, it doesn't seem to be worthwhile at the present.
170 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
171 * that another operation that must be performed in a non-blocking manner is
172 * not delayed for very long because the lock has already been taken.
174 * NOTE: This code assumes that certain fields of a port/socket pair are
175 * constant over its lifetime; such fields can be examined without taking
176 * the socket lock and/or port lock, and do not need to be re-read even
177 * after resuming processing after waiting. These fields include:
179 * - pointer to socket sk structure (aka tipc_sock structure)
180 * - pointer to port structure
184 static u32 tsk_own_node(struct tipc_sock *tsk)
186 return msg_prevnode(&tsk->phdr);
189 static u32 tsk_peer_node(struct tipc_sock *tsk)
191 return msg_destnode(&tsk->phdr);
194 static u32 tsk_peer_port(struct tipc_sock *tsk)
196 return msg_destport(&tsk->phdr);
199 static bool tsk_unreliable(struct tipc_sock *tsk)
201 return msg_src_droppable(&tsk->phdr) != 0;
204 static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
206 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
209 static bool tsk_unreturnable(struct tipc_sock *tsk)
211 return msg_dest_droppable(&tsk->phdr) != 0;
214 static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
216 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
219 static int tsk_importance(struct tipc_sock *tsk)
221 return msg_importance(&tsk->phdr);
224 static int tsk_set_importance(struct tipc_sock *tsk, int imp)
226 if (imp > TIPC_CRITICAL_IMPORTANCE)
228 msg_set_importance(&tsk->phdr, (u32)imp);
232 static struct tipc_sock *tipc_sk(const struct sock *sk)
234 return container_of(sk, struct tipc_sock, sk);
237 static int tsk_conn_cong(struct tipc_sock *tsk)
239 return tsk->sent_unacked >= TIPC_FLOWCTRL_WIN;
243 * tsk_advance_rx_queue - discard first buffer in socket receive queue
245 * Caller must hold socket lock
247 static void tsk_advance_rx_queue(struct sock *sk)
249 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
252 /* tipc_sk_respond() : send response message back to sender
254 static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
258 u32 onode = tipc_own_addr(sock_net(sk));
260 if (!tipc_msg_reverse(onode, &skb, err))
263 dnode = msg_destnode(buf_msg(skb));
264 selector = msg_origport(buf_msg(skb));
265 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
269 * tsk_rej_rx_queue - reject all buffers in socket receive queue
271 * Caller must hold socket lock
273 static void tsk_rej_rx_queue(struct sock *sk)
277 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
278 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
281 /* tsk_peer_msg - verify if message was sent by connected port's peer
283 * Handles cases where the node's network address has changed from
284 * the default of <0.0.0> to its configured setting.
286 static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
288 struct tipc_net *tn = net_generic(sock_net(&tsk->sk), tipc_net_id);
289 u32 peer_port = tsk_peer_port(tsk);
293 if (unlikely(!tsk->connected))
296 if (unlikely(msg_origport(msg) != peer_port))
299 orig_node = msg_orignode(msg);
300 peer_node = tsk_peer_node(tsk);
302 if (likely(orig_node == peer_node))
305 if (!orig_node && (peer_node == tn->own_addr))
308 if (!peer_node && (orig_node == tn->own_addr))
315 * tipc_sk_create - create a TIPC socket
316 * @net: network namespace (must be default network)
317 * @sock: pre-allocated socket structure
318 * @protocol: protocol indicator (must be 0)
319 * @kern: caused by kernel or by userspace?
321 * This routine creates additional data structures used by the TIPC socket,
322 * initializes them, and links them together.
324 * Returns 0 on success, errno otherwise
326 static int tipc_sk_create(struct net *net, struct socket *sock,
327 int protocol, int kern)
330 const struct proto_ops *ops;
333 struct tipc_sock *tsk;
334 struct tipc_msg *msg;
336 /* Validate arguments */
337 if (unlikely(protocol != 0))
338 return -EPROTONOSUPPORT;
340 switch (sock->type) {
343 state = SS_UNCONNECTED;
347 state = SS_UNCONNECTED;
358 /* Allocate socket's protocol area */
359 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
364 tsk->max_pkt = MAX_PKT_DEFAULT;
365 INIT_LIST_HEAD(&tsk->publications);
367 tn = net_generic(sock_net(sk), tipc_net_id);
368 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
371 /* Finish initializing socket data structures */
374 sock_init_data(sock, sk);
375 if (tipc_sk_insert(tsk)) {
376 pr_warn("Socket create failed; port numbrer exhausted\n");
379 msg_set_origport(msg, tsk->portid);
380 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
381 sk->sk_backlog_rcv = tipc_backlog_rcv;
382 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
383 sk->sk_data_ready = tipc_data_ready;
384 sk->sk_write_space = tipc_write_space;
385 sk->sk_destruct = tipc_sock_destruct;
386 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
387 tsk->sent_unacked = 0;
388 atomic_set(&tsk->dupl_rcvcnt, 0);
390 if (sock->state == SS_READY) {
391 tsk_set_unreturnable(tsk, true);
392 if (sock->type == SOCK_DGRAM)
393 tsk_set_unreliable(tsk, true);
398 static void tipc_sk_callback(struct rcu_head *head)
400 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
406 * tipc_release - destroy a TIPC socket
407 * @sock: socket to destroy
409 * This routine cleans up any messages that are still queued on the socket.
410 * For DGRAM and RDM socket types, all queued messages are rejected.
411 * For SEQPACKET and STREAM socket types, the first message is rejected
412 * and any others are discarded. (If the first message on a STREAM socket
413 * is partially-read, it is discarded and the next one is rejected instead.)
415 * NOTE: Rejected messages are not necessarily returned to the sender! They
416 * are returned or discarded according to the "destination droppable" setting
417 * specified for the message by the sender.
419 * Returns 0 on success, errno otherwise
421 static int tipc_release(struct socket *sock)
423 struct sock *sk = sock->sk;
425 struct tipc_sock *tsk;
430 * Exit if socket isn't fully initialized (occurs when a failed accept()
431 * releases a pre-allocated child socket that was never used)
441 * Reject all unreceived messages, except on an active connection
442 * (which disconnects locally & sends a 'FIN+' to peer)
444 dnode = tsk_peer_node(tsk);
445 while (sock->state != SS_DISCONNECTING) {
446 skb = __skb_dequeue(&sk->sk_receive_queue);
449 if (TIPC_SKB_CB(skb)->handle != NULL)
452 if ((sock->state == SS_CONNECTING) ||
453 (sock->state == SS_CONNECTED)) {
454 sock->state = SS_DISCONNECTING;
456 tipc_node_remove_conn(net, dnode, tsk->portid);
458 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
462 tipc_sk_withdraw(tsk, 0, NULL);
463 sk_stop_timer(sk, &sk->sk_timer);
465 if (tsk->connected) {
466 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
467 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
468 tsk_own_node(tsk), tsk_peer_port(tsk),
469 tsk->portid, TIPC_ERR_NO_PORT);
471 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
472 tipc_node_remove_conn(net, dnode, tsk->portid);
475 /* Reject any messages that accumulated in backlog queue */
476 sock->state = SS_DISCONNECTING;
479 call_rcu(&tsk->rcu, tipc_sk_callback);
486 * tipc_bind - associate or disassocate TIPC name(s) with a socket
487 * @sock: socket structure
488 * @uaddr: socket address describing name(s) and desired operation
489 * @uaddr_len: size of socket address data structure
491 * Name and name sequence binding is indicated using a positive scope value;
492 * a negative scope value unbinds the specified name. Specifying no name
493 * (i.e. a socket address length of 0) unbinds all names from the socket.
495 * Returns 0 on success, errno otherwise
497 * NOTE: This routine doesn't need to take the socket lock since it doesn't
498 * access any non-constant socket information.
500 static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
503 struct sock *sk = sock->sk;
504 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
505 struct tipc_sock *tsk = tipc_sk(sk);
509 if (unlikely(!uaddr_len)) {
510 res = tipc_sk_withdraw(tsk, 0, NULL);
514 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
518 if (addr->family != AF_TIPC) {
523 if (addr->addrtype == TIPC_ADDR_NAME)
524 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
525 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
530 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
531 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
532 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
537 res = (addr->scope > 0) ?
538 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
539 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
546 * tipc_getname - get port ID of socket or peer socket
547 * @sock: socket structure
548 * @uaddr: area for returned socket address
549 * @uaddr_len: area for returned length of socket address
550 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
552 * Returns 0 on success, errno otherwise
554 * NOTE: This routine doesn't need to take the socket lock since it only
555 * accesses socket information that is unchanging (or which changes in
556 * a completely predictable manner).
558 static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
559 int *uaddr_len, int peer)
561 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
562 struct tipc_sock *tsk = tipc_sk(sock->sk);
563 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
565 memset(addr, 0, sizeof(*addr));
567 if ((sock->state != SS_CONNECTED) &&
568 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
570 addr->addr.id.ref = tsk_peer_port(tsk);
571 addr->addr.id.node = tsk_peer_node(tsk);
573 addr->addr.id.ref = tsk->portid;
574 addr->addr.id.node = tn->own_addr;
577 *uaddr_len = sizeof(*addr);
578 addr->addrtype = TIPC_ADDR_ID;
579 addr->family = AF_TIPC;
581 addr->addr.name.domain = 0;
587 * tipc_poll - read and possibly block on pollmask
588 * @file: file structure associated with the socket
589 * @sock: socket for which to calculate the poll bits
592 * Returns pollmask value
595 * It appears that the usual socket locking mechanisms are not useful here
596 * since the pollmask info is potentially out-of-date the moment this routine
597 * exits. TCP and other protocols seem to rely on higher level poll routines
598 * to handle any preventable race conditions, so TIPC will do the same ...
600 * TIPC sets the returned events as follows:
602 * socket state flags set
603 * ------------ ---------
604 * unconnected no read flags
605 * POLLOUT if port is not congested
607 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
610 * connected POLLIN/POLLRDNORM if data in rx queue
611 * POLLOUT if port is not congested
613 * disconnecting POLLIN/POLLRDNORM/POLLHUP
616 * listening POLLIN if SYN in rx queue
619 * ready POLLIN/POLLRDNORM if data in rx queue
620 * [connectionless] POLLOUT (since port cannot be congested)
622 * IMPORTANT: The fact that a read or write operation is indicated does NOT
623 * imply that the operation will succeed, merely that it should be performed
624 * and will not block.
626 static unsigned int tipc_poll(struct file *file, struct socket *sock,
629 struct sock *sk = sock->sk;
630 struct tipc_sock *tsk = tipc_sk(sk);
633 sock_poll_wait(file, sk_sleep(sk), wait);
635 switch ((int)sock->state) {
642 if (!tsk->link_cong && !tsk_conn_cong(tsk))
647 if (!skb_queue_empty(&sk->sk_receive_queue))
648 mask |= (POLLIN | POLLRDNORM);
650 case SS_DISCONNECTING:
651 mask = (POLLIN | POLLRDNORM | POLLHUP);
659 * tipc_sendmcast - send multicast message
660 * @sock: socket structure
661 * @seq: destination address
662 * @msg: message to send
663 * @dsz: total length of message data
664 * @timeo: timeout to wait for wakeup
666 * Called from function tipc_sendmsg(), which has done all sanity checks
667 * Returns the number of bytes sent on success, or errno
669 static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
670 struct msghdr *msg, size_t dsz, long timeo)
672 struct sock *sk = sock->sk;
673 struct tipc_sock *tsk = tipc_sk(sk);
674 struct net *net = sock_net(sk);
675 struct tipc_msg *mhdr = &tsk->phdr;
676 struct sk_buff_head pktchain;
677 struct iov_iter save = msg->msg_iter;
681 msg_set_type(mhdr, TIPC_MCAST_MSG);
682 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
683 msg_set_destport(mhdr, 0);
684 msg_set_destnode(mhdr, 0);
685 msg_set_nametype(mhdr, seq->type);
686 msg_set_namelower(mhdr, seq->lower);
687 msg_set_nameupper(mhdr, seq->upper);
688 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
690 skb_queue_head_init(&pktchain);
693 mtu = tipc_bcast_get_mtu(net);
694 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &pktchain);
695 if (unlikely(rc < 0))
699 rc = tipc_bcast_xmit(net, &pktchain);
703 if (rc == -ELINKCONG) {
705 rc = tipc_wait_for_sndmsg(sock, &timeo);
709 __skb_queue_purge(&pktchain);
710 if (rc == -EMSGSIZE) {
711 msg->msg_iter = save;
720 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
721 * @arrvq: queue with arriving messages, to be cloned after destination lookup
722 * @inputq: queue with cloned messages, delivered to socket after dest lookup
724 * Multi-threaded: parallel calls with reference to same queues may occur
726 void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
727 struct sk_buff_head *inputq)
729 struct tipc_msg *msg;
730 struct tipc_plist dports;
732 u32 scope = TIPC_CLUSTER_SCOPE;
733 struct sk_buff_head tmpq;
735 struct sk_buff *skb, *_skb;
737 __skb_queue_head_init(&tmpq);
738 tipc_plist_init(&dports);
740 skb = tipc_skb_peek(arrvq, &inputq->lock);
741 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
743 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
745 if (in_own_node(net, msg_orignode(msg)))
746 scope = TIPC_NODE_SCOPE;
748 /* Create destination port list and message clones: */
749 tipc_nametbl_mc_translate(net,
750 msg_nametype(msg), msg_namelower(msg),
751 msg_nameupper(msg), scope, &dports);
752 portid = tipc_plist_pop(&dports);
753 for (; portid; portid = tipc_plist_pop(&dports)) {
754 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
756 msg_set_destport(buf_msg(_skb), portid);
757 __skb_queue_tail(&tmpq, _skb);
760 pr_warn("Failed to clone mcast rcv buffer\n");
762 /* Append to inputq if not already done by other thread */
763 spin_lock_bh(&inputq->lock);
764 if (skb_peek(arrvq) == skb) {
765 skb_queue_splice_tail_init(&tmpq, inputq);
766 kfree_skb(__skb_dequeue(arrvq));
768 spin_unlock_bh(&inputq->lock);
769 __skb_queue_purge(&tmpq);
772 tipc_sk_rcv(net, inputq);
776 * tipc_sk_proto_rcv - receive a connection mng protocol message
777 * @tsk: receiving socket
778 * @skb: pointer to message buffer.
780 static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
781 struct sk_buff_head *xmitq)
783 struct sock *sk = &tsk->sk;
784 u32 onode = tsk_own_node(tsk);
785 struct tipc_msg *hdr = buf_msg(skb);
786 int mtyp = msg_type(hdr);
789 /* Ignore if connection cannot be validated: */
790 if (!tsk_peer_msg(tsk, hdr))
793 tsk->probing_state = TIPC_CONN_OK;
795 if (mtyp == CONN_PROBE) {
796 msg_set_type(hdr, CONN_PROBE_REPLY);
797 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
798 __skb_queue_tail(xmitq, skb);
800 } else if (mtyp == CONN_ACK) {
801 conn_cong = tsk_conn_cong(tsk);
802 tsk->sent_unacked -= msg_msgcnt(hdr);
804 sk->sk_write_space(sk);
805 } else if (mtyp != CONN_PROBE_REPLY) {
806 pr_warn("Received unknown CONN_PROTO msg\n");
812 static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
814 struct sock *sk = sock->sk;
815 struct tipc_sock *tsk = tipc_sk(sk);
820 int err = sock_error(sk);
823 if (sock->state == SS_DISCONNECTING)
827 if (signal_pending(current))
828 return sock_intr_errno(*timeo_p);
830 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
831 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
832 finish_wait(sk_sleep(sk), &wait);
838 * tipc_sendmsg - send message in connectionless manner
839 * @sock: socket structure
840 * @m: message to send
841 * @dsz: amount of user data to be sent
843 * Message must have an destination specified explicitly.
844 * Used for SOCK_RDM and SOCK_DGRAM messages,
845 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
846 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
848 * Returns the number of bytes sent on success, or errno otherwise
850 static int tipc_sendmsg(struct socket *sock,
851 struct msghdr *m, size_t dsz)
853 struct sock *sk = sock->sk;
857 ret = __tipc_sendmsg(sock, m, dsz);
863 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
865 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
866 struct sock *sk = sock->sk;
867 struct tipc_sock *tsk = tipc_sk(sk);
868 struct net *net = sock_net(sk);
869 struct tipc_msg *mhdr = &tsk->phdr;
871 struct sk_buff_head pktchain;
873 struct tipc_name_seq *seq;
874 struct iov_iter save;
879 if (dsz > TIPC_MAX_USER_MSG_SIZE)
881 if (unlikely(!dest)) {
882 if (tsk->connected && sock->state == SS_READY)
885 return -EDESTADDRREQ;
886 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
887 dest->family != AF_TIPC) {
890 if (unlikely(sock->state != SS_READY)) {
891 if (sock->state == SS_LISTENING)
893 if (sock->state != SS_UNCONNECTED)
897 if (dest->addrtype == TIPC_ADDR_NAME) {
898 tsk->conn_type = dest->addr.name.name.type;
899 tsk->conn_instance = dest->addr.name.name.instance;
902 seq = &dest->addr.nameseq;
903 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
905 if (dest->addrtype == TIPC_ADDR_MCAST) {
906 return tipc_sendmcast(sock, seq, m, dsz, timeo);
907 } else if (dest->addrtype == TIPC_ADDR_NAME) {
908 u32 type = dest->addr.name.name.type;
909 u32 inst = dest->addr.name.name.instance;
910 u32 domain = dest->addr.name.domain;
913 msg_set_type(mhdr, TIPC_NAMED_MSG);
914 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
915 msg_set_nametype(mhdr, type);
916 msg_set_nameinst(mhdr, inst);
917 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
918 dport = tipc_nametbl_translate(net, type, inst, &dnode);
919 msg_set_destnode(mhdr, dnode);
920 msg_set_destport(mhdr, dport);
921 if (unlikely(!dport && !dnode))
922 return -EHOSTUNREACH;
923 } else if (dest->addrtype == TIPC_ADDR_ID) {
924 dnode = dest->addr.id.node;
925 msg_set_type(mhdr, TIPC_DIRECT_MSG);
926 msg_set_lookup_scope(mhdr, 0);
927 msg_set_destnode(mhdr, dnode);
928 msg_set_destport(mhdr, dest->addr.id.ref);
929 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
932 skb_queue_head_init(&pktchain);
935 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
936 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &pktchain);
941 skb = skb_peek(&pktchain);
942 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
943 rc = tipc_node_xmit(net, &pktchain, dnode, tsk->portid);
945 if (sock->state != SS_READY)
946 sock->state = SS_CONNECTING;
949 if (rc == -ELINKCONG) {
951 rc = tipc_wait_for_sndmsg(sock, &timeo);
955 __skb_queue_purge(&pktchain);
956 if (rc == -EMSGSIZE) {
966 static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
968 struct sock *sk = sock->sk;
969 struct tipc_sock *tsk = tipc_sk(sk);
974 int err = sock_error(sk);
977 if (sock->state == SS_DISCONNECTING)
979 else if (sock->state != SS_CONNECTED)
983 if (signal_pending(current))
984 return sock_intr_errno(*timeo_p);
986 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
987 done = sk_wait_event(sk, timeo_p,
989 !tsk_conn_cong(tsk)) ||
991 finish_wait(sk_sleep(sk), &wait);
997 * tipc_send_stream - send stream-oriented data
998 * @sock: socket structure
1000 * @dsz: total length of data to be transmitted
1002 * Used for SOCK_STREAM data.
1004 * Returns the number of bytes sent on success (or partial success),
1005 * or errno if no data sent
1007 static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1009 struct sock *sk = sock->sk;
1013 ret = __tipc_send_stream(sock, m, dsz);
1019 static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1021 struct sock *sk = sock->sk;
1022 struct net *net = sock_net(sk);
1023 struct tipc_sock *tsk = tipc_sk(sk);
1024 struct tipc_msg *mhdr = &tsk->phdr;
1025 struct sk_buff_head pktchain;
1026 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1027 u32 portid = tsk->portid;
1031 uint mtu, send, sent = 0;
1032 struct iov_iter save;
1034 /* Handle implied connection establishment */
1035 if (unlikely(dest)) {
1036 rc = __tipc_sendmsg(sock, m, dsz);
1037 if (dsz && (dsz == rc))
1038 tsk->sent_unacked = 1;
1041 if (dsz > (uint)INT_MAX)
1044 if (unlikely(sock->state != SS_CONNECTED)) {
1045 if (sock->state == SS_DISCONNECTING)
1051 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1052 dnode = tsk_peer_node(tsk);
1053 skb_queue_head_init(&pktchain);
1058 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
1059 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &pktchain);
1060 if (unlikely(rc < 0))
1064 if (likely(!tsk_conn_cong(tsk))) {
1065 rc = tipc_node_xmit(net, &pktchain, dnode, portid);
1067 tsk->sent_unacked++;
1073 if (rc == -EMSGSIZE) {
1074 __skb_queue_purge(&pktchain);
1075 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1080 if (rc != -ELINKCONG)
1085 rc = tipc_wait_for_sndpkt(sock, &timeo);
1088 __skb_queue_purge(&pktchain);
1089 return sent ? sent : rc;
1093 * tipc_send_packet - send a connection-oriented message
1094 * @sock: socket structure
1095 * @m: message to send
1096 * @dsz: length of data to be transmitted
1098 * Used for SOCK_SEQPACKET messages.
1100 * Returns the number of bytes sent on success, or errno otherwise
1102 static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
1104 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1107 return tipc_send_stream(sock, m, dsz);
1110 /* tipc_sk_finish_conn - complete the setup of a connection
1112 static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
1115 struct sock *sk = &tsk->sk;
1116 struct net *net = sock_net(sk);
1117 struct tipc_msg *msg = &tsk->phdr;
1119 msg_set_destnode(msg, peer_node);
1120 msg_set_destport(msg, peer_port);
1121 msg_set_type(msg, TIPC_CONN_MSG);
1122 msg_set_lookup_scope(msg, 0);
1123 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1125 tsk->probing_intv = CONN_PROBING_INTERVAL;
1126 tsk->probing_state = TIPC_CONN_OK;
1128 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
1129 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1130 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
1134 * set_orig_addr - capture sender's address for received message
1135 * @m: descriptor for message info
1136 * @msg: received message header
1138 * Note: Address is not captured if not requested by receiver.
1140 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
1142 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
1145 addr->family = AF_TIPC;
1146 addr->addrtype = TIPC_ADDR_ID;
1147 memset(&addr->addr, 0, sizeof(addr->addr));
1148 addr->addr.id.ref = msg_origport(msg);
1149 addr->addr.id.node = msg_orignode(msg);
1150 addr->addr.name.domain = 0; /* could leave uninitialized */
1151 addr->scope = 0; /* could leave uninitialized */
1152 m->msg_namelen = sizeof(struct sockaddr_tipc);
1157 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
1158 * @m: descriptor for message info
1159 * @msg: received message header
1160 * @tsk: TIPC port associated with message
1162 * Note: Ancillary data is not captured if not requested by receiver.
1164 * Returns 0 if successful, otherwise errno
1166 static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1167 struct tipc_sock *tsk)
1175 if (likely(m->msg_controllen == 0))
1178 /* Optionally capture errored message object(s) */
1179 err = msg ? msg_errcode(msg) : 0;
1180 if (unlikely(err)) {
1182 anc_data[1] = msg_data_sz(msg);
1183 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1187 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1194 /* Optionally capture message destination object */
1195 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1196 switch (dest_type) {
1197 case TIPC_NAMED_MSG:
1199 anc_data[0] = msg_nametype(msg);
1200 anc_data[1] = msg_namelower(msg);
1201 anc_data[2] = msg_namelower(msg);
1203 case TIPC_MCAST_MSG:
1205 anc_data[0] = msg_nametype(msg);
1206 anc_data[1] = msg_namelower(msg);
1207 anc_data[2] = msg_nameupper(msg);
1210 has_name = (tsk->conn_type != 0);
1211 anc_data[0] = tsk->conn_type;
1212 anc_data[1] = tsk->conn_instance;
1213 anc_data[2] = tsk->conn_instance;
1219 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1227 static void tipc_sk_send_ack(struct tipc_sock *tsk, uint ack)
1229 struct net *net = sock_net(&tsk->sk);
1230 struct sk_buff *skb = NULL;
1231 struct tipc_msg *msg;
1232 u32 peer_port = tsk_peer_port(tsk);
1233 u32 dnode = tsk_peer_node(tsk);
1235 if (!tsk->connected)
1237 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1238 dnode, tsk_own_node(tsk), peer_port,
1239 tsk->portid, TIPC_OK);
1243 msg_set_msgcnt(msg, ack);
1244 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
1247 static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1249 struct sock *sk = sock->sk;
1251 long timeo = *timeop;
1255 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1256 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
1257 if (sock->state == SS_DISCONNECTING) {
1262 timeo = schedule_timeout(timeo);
1266 if (!skb_queue_empty(&sk->sk_receive_queue))
1271 err = sock_intr_errno(timeo);
1272 if (signal_pending(current))
1275 finish_wait(sk_sleep(sk), &wait);
1281 * tipc_recvmsg - receive packet-oriented message
1282 * @m: descriptor for message info
1283 * @buf_len: total size of user buffer area
1284 * @flags: receive flags
1286 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1287 * If the complete message doesn't fit in user area, truncate it.
1289 * Returns size of returned message data, errno otherwise
1291 static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1294 struct sock *sk = sock->sk;
1295 struct tipc_sock *tsk = tipc_sk(sk);
1296 struct sk_buff *buf;
1297 struct tipc_msg *msg;
1303 /* Catch invalid receive requests */
1304 if (unlikely(!buf_len))
1309 if (unlikely(sock->state == SS_UNCONNECTED)) {
1314 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1317 /* Look for a message in receive queue; wait if necessary */
1318 res = tipc_wait_for_rcvmsg(sock, &timeo);
1322 /* Look at first message in receive queue */
1323 buf = skb_peek(&sk->sk_receive_queue);
1325 sz = msg_data_sz(msg);
1326 err = msg_errcode(msg);
1328 /* Discard an empty non-errored message & try again */
1329 if ((!sz) && (!err)) {
1330 tsk_advance_rx_queue(sk);
1334 /* Capture sender's address (optional) */
1335 set_orig_addr(m, msg);
1337 /* Capture ancillary data (optional) */
1338 res = tipc_sk_anc_data_recv(m, msg, tsk);
1342 /* Capture message data (if valid) & compute return value (always) */
1344 if (unlikely(buf_len < sz)) {
1346 m->msg_flags |= MSG_TRUNC;
1348 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg), m, sz);
1353 if ((sock->state == SS_READY) ||
1354 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1360 /* Consume received message (optional) */
1361 if (likely(!(flags & MSG_PEEK))) {
1362 if ((sock->state != SS_READY) &&
1363 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1364 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
1365 tsk->rcv_unacked = 0;
1367 tsk_advance_rx_queue(sk);
1375 * tipc_recv_stream - receive stream-oriented data
1376 * @m: descriptor for message info
1377 * @buf_len: total size of user buffer area
1378 * @flags: receive flags
1380 * Used for SOCK_STREAM messages only. If not enough data is available
1381 * will optionally wait for more; never truncates data.
1383 * Returns size of returned message data, errno otherwise
1385 static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1386 size_t buf_len, int flags)
1388 struct sock *sk = sock->sk;
1389 struct tipc_sock *tsk = tipc_sk(sk);
1390 struct sk_buff *buf;
1391 struct tipc_msg *msg;
1394 int sz_to_copy, target, needed;
1399 /* Catch invalid receive attempts */
1400 if (unlikely(!buf_len))
1405 if (unlikely(sock->state == SS_UNCONNECTED)) {
1410 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
1411 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1414 /* Look for a message in receive queue; wait if necessary */
1415 res = tipc_wait_for_rcvmsg(sock, &timeo);
1419 /* Look at first message in receive queue */
1420 buf = skb_peek(&sk->sk_receive_queue);
1422 sz = msg_data_sz(msg);
1423 err = msg_errcode(msg);
1425 /* Discard an empty non-errored message & try again */
1426 if ((!sz) && (!err)) {
1427 tsk_advance_rx_queue(sk);
1431 /* Optionally capture sender's address & ancillary data of first msg */
1432 if (sz_copied == 0) {
1433 set_orig_addr(m, msg);
1434 res = tipc_sk_anc_data_recv(m, msg, tsk);
1439 /* Capture message data (if valid) & compute return value (always) */
1441 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
1444 needed = (buf_len - sz_copied);
1445 sz_to_copy = (sz <= needed) ? sz : needed;
1447 res = skb_copy_datagram_msg(buf, msg_hdr_sz(msg) + offset,
1452 sz_copied += sz_to_copy;
1454 if (sz_to_copy < sz) {
1455 if (!(flags & MSG_PEEK))
1456 TIPC_SKB_CB(buf)->handle =
1457 (void *)(unsigned long)(offset + sz_to_copy);
1462 goto exit; /* can't add error msg to valid data */
1464 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1470 /* Consume received message (optional) */
1471 if (likely(!(flags & MSG_PEEK))) {
1472 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1473 tipc_sk_send_ack(tsk, tsk->rcv_unacked);
1474 tsk->rcv_unacked = 0;
1476 tsk_advance_rx_queue(sk);
1479 /* Loop around if more data is required */
1480 if ((sz_copied < buf_len) && /* didn't get all requested data */
1481 (!skb_queue_empty(&sk->sk_receive_queue) ||
1482 (sz_copied < target)) && /* and more is ready or required */
1483 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1484 (!err)) /* and haven't reached a FIN */
1489 return sz_copied ? sz_copied : res;
1493 * tipc_write_space - wake up thread if port congestion is released
1496 static void tipc_write_space(struct sock *sk)
1498 struct socket_wq *wq;
1501 wq = rcu_dereference(sk->sk_wq);
1502 if (wq_has_sleeper(wq))
1503 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1504 POLLWRNORM | POLLWRBAND);
1509 * tipc_data_ready - wake up threads to indicate messages have been received
1511 * @len: the length of messages
1513 static void tipc_data_ready(struct sock *sk)
1515 struct socket_wq *wq;
1518 wq = rcu_dereference(sk->sk_wq);
1519 if (wq_has_sleeper(wq))
1520 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1521 POLLRDNORM | POLLRDBAND);
1525 static void tipc_sock_destruct(struct sock *sk)
1527 __skb_queue_purge(&sk->sk_receive_queue);
1531 * filter_connect - Handle all incoming messages for a connection-based socket
1533 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
1535 * Returns true if everything ok, false otherwise
1537 static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
1539 struct sock *sk = &tsk->sk;
1540 struct net *net = sock_net(sk);
1541 struct socket *sock = sk->sk_socket;
1542 struct tipc_msg *hdr = buf_msg(skb);
1544 if (unlikely(msg_mcast(hdr)))
1547 switch ((int)sock->state) {
1550 /* Accept only connection-based messages sent by peer */
1551 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1554 if (unlikely(msg_errcode(hdr))) {
1555 sock->state = SS_DISCONNECTING;
1557 /* Let timer expire on it's own */
1558 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1565 /* Accept only ACK or NACK message */
1566 if (unlikely(!msg_connected(hdr)))
1569 if (unlikely(msg_errcode(hdr))) {
1570 sock->state = SS_DISCONNECTING;
1571 sk->sk_err = ECONNREFUSED;
1575 if (unlikely(!msg_isdata(hdr))) {
1576 sock->state = SS_DISCONNECTING;
1577 sk->sk_err = EINVAL;
1581 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1582 msg_set_importance(&tsk->phdr, msg_importance(hdr));
1583 sock->state = SS_CONNECTED;
1585 /* If 'ACK+' message, add to socket receive queue */
1586 if (msg_data_sz(hdr))
1589 /* If empty 'ACK-' message, wake up sleeping connect() */
1590 if (waitqueue_active(sk_sleep(sk)))
1591 wake_up_interruptible(sk_sleep(sk));
1593 /* 'ACK-' message is neither accepted nor rejected: */
1594 msg_set_dest_droppable(hdr, 1);
1598 case SS_UNCONNECTED:
1600 /* Accept only SYN message */
1601 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1604 case SS_DISCONNECTING:
1607 pr_err("Unknown socket state %u\n", sock->state);
1613 * rcvbuf_limit - get proper overload limit of socket receive queue
1617 * For all connection oriented messages, irrespective of importance,
1618 * the default overload value (i.e. 67MB) is set as limit.
1620 * For all connectionless messages, by default new queue limits are
1623 * TIPC_LOW_IMPORTANCE (4 MB)
1624 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1625 * TIPC_HIGH_IMPORTANCE (16 MB)
1626 * TIPC_CRITICAL_IMPORTANCE (32 MB)
1628 * Returns overload limit according to corresponding message importance
1630 static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1632 struct tipc_msg *msg = buf_msg(buf);
1634 if (msg_connected(msg))
1635 return sysctl_tipc_rmem[2];
1637 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1638 msg_importance(msg);
1642 * filter_rcv - validate incoming message
1644 * @skb: pointer to message.
1646 * Enqueues message on receive queue if acceptable; optionally handles
1647 * disconnect indication for a connected socket.
1649 * Called with socket lock already taken
1651 * Returns true if message was added to socket receive queue, otherwise false
1653 static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1654 struct sk_buff_head *xmitq)
1656 struct socket *sock = sk->sk_socket;
1657 struct tipc_sock *tsk = tipc_sk(sk);
1658 struct tipc_msg *hdr = buf_msg(skb);
1659 unsigned int limit = rcvbuf_limit(sk, skb);
1661 int usr = msg_user(hdr);
1663 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
1664 tipc_sk_proto_rcv(tsk, skb, xmitq);
1668 if (unlikely(usr == SOCK_WAKEUP)) {
1671 sk->sk_write_space(sk);
1675 /* Drop if illegal message type */
1676 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1681 /* Reject if wrong message type for current socket state */
1682 if (unlikely(sock->state == SS_READY)) {
1683 if (msg_connected(hdr)) {
1684 err = TIPC_ERR_NO_PORT;
1687 } else if (unlikely(!filter_connect(tsk, skb))) {
1688 err = TIPC_ERR_NO_PORT;
1692 /* Reject message if there isn't room to queue it */
1693 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1694 err = TIPC_ERR_OVERLOAD;
1698 /* Enqueue message */
1699 TIPC_SKB_CB(skb)->handle = NULL;
1700 __skb_queue_tail(&sk->sk_receive_queue, skb);
1701 skb_set_owner_r(skb, sk);
1703 sk->sk_data_ready(sk);
1707 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1708 __skb_queue_tail(xmitq, skb);
1713 * tipc_backlog_rcv - handle incoming message from backlog queue
1717 * Caller must hold socket lock
1721 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
1723 unsigned int truesize = skb->truesize;
1724 struct sk_buff_head xmitq;
1725 u32 dnode, selector;
1727 __skb_queue_head_init(&xmitq);
1729 if (likely(filter_rcv(sk, skb, &xmitq))) {
1730 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
1734 if (skb_queue_empty(&xmitq))
1737 /* Send response/rejected message */
1738 skb = __skb_dequeue(&xmitq);
1739 dnode = msg_destnode(buf_msg(skb));
1740 selector = msg_origport(buf_msg(skb));
1741 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
1746 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1747 * inputq and try adding them to socket or backlog queue
1748 * @inputq: list of incoming buffers with potentially different destinations
1749 * @sk: socket where the buffers should be enqueued
1750 * @dport: port number for the socket
1752 * Caller must hold socket lock
1754 static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
1755 u32 dport, struct sk_buff_head *xmitq)
1757 unsigned long time_limit = jiffies + 2;
1758 struct sk_buff *skb;
1763 while (skb_queue_len(inputq)) {
1764 if (unlikely(time_after_eq(jiffies, time_limit)))
1767 skb = tipc_skb_dequeue(inputq, dport);
1771 /* Add message directly to receive queue if possible */
1772 if (!sock_owned_by_user(sk)) {
1773 filter_rcv(sk, skb, xmitq);
1777 /* Try backlog, compensating for double-counted bytes */
1778 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
1779 if (!sk->sk_backlog.len)
1780 atomic_set(dcnt, 0);
1781 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1782 if (likely(!sk_add_backlog(sk, skb, lim)))
1785 /* Overload => reject message back to sender */
1786 onode = tipc_own_addr(sock_net(sk));
1787 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1788 __skb_queue_tail(xmitq, skb);
1794 * tipc_sk_rcv - handle a chain of incoming buffers
1795 * @inputq: buffer list containing the buffers
1796 * Consumes all buffers in list until inputq is empty
1797 * Note: may be called in multiple threads referring to the same queue
1799 void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
1801 struct sk_buff_head xmitq;
1802 u32 dnode, dport = 0;
1804 struct tipc_sock *tsk;
1806 struct sk_buff *skb;
1808 __skb_queue_head_init(&xmitq);
1809 while (skb_queue_len(inputq)) {
1810 dport = tipc_skb_peek_port(inputq, dport);
1811 tsk = tipc_sk_lookup(net, dport);
1815 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
1816 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
1817 spin_unlock_bh(&sk->sk_lock.slock);
1819 /* Send pending response/rejected messages, if any */
1820 while ((skb = __skb_dequeue(&xmitq))) {
1821 dnode = msg_destnode(buf_msg(skb));
1822 tipc_node_xmit_skb(net, skb, dnode, dport);
1828 /* No destination socket => dequeue skb if still there */
1829 skb = tipc_skb_dequeue(inputq, dport);
1833 /* Try secondary lookup if unresolved named message */
1834 err = TIPC_ERR_NO_PORT;
1835 if (tipc_msg_lookup_dest(net, skb, &err))
1838 /* Prepare for message rejection */
1839 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
1842 dnode = msg_destnode(buf_msg(skb));
1843 tipc_node_xmit_skb(net, skb, dnode, dport);
1847 static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1849 struct sock *sk = sock->sk;
1854 int err = sock_error(sk);
1859 if (signal_pending(current))
1860 return sock_intr_errno(*timeo_p);
1862 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1863 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1864 finish_wait(sk_sleep(sk), &wait);
1870 * tipc_connect - establish a connection to another TIPC port
1871 * @sock: socket structure
1872 * @dest: socket address for destination port
1873 * @destlen: size of socket address data structure
1874 * @flags: file-related flags associated with socket
1876 * Returns 0 on success, errno otherwise
1878 static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1879 int destlen, int flags)
1881 struct sock *sk = sock->sk;
1882 struct tipc_sock *tsk = tipc_sk(sk);
1883 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1884 struct msghdr m = {NULL,};
1885 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
1886 socket_state previous;
1891 /* DGRAM/RDM connect(), just save the destaddr */
1892 if (sock->state == SS_READY) {
1893 if (dst->family == AF_UNSPEC) {
1894 memset(&tsk->remote, 0, sizeof(struct sockaddr_tipc));
1896 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1899 memcpy(&tsk->remote, dest, destlen);
1906 * Reject connection attempt using multicast address
1908 * Note: send_msg() validates the rest of the address fields,
1909 * so there's no need to do it here
1911 if (dst->addrtype == TIPC_ADDR_MCAST) {
1916 previous = sock->state;
1917 switch (sock->state) {
1918 case SS_UNCONNECTED:
1919 /* Send a 'SYN-' to destination */
1921 m.msg_namelen = destlen;
1923 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1924 * indicate send_msg() is never blocked.
1927 m.msg_flags = MSG_DONTWAIT;
1929 res = __tipc_sendmsg(sock, &m, 0);
1930 if ((res < 0) && (res != -EWOULDBLOCK))
1933 /* Just entered SS_CONNECTING state; the only
1934 * difference is that return value in non-blocking
1935 * case is EINPROGRESS, rather than EALREADY.
1939 if (previous == SS_CONNECTING)
1943 timeout = msecs_to_jiffies(timeout);
1944 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1945 res = tipc_wait_for_connect(sock, &timeout);
1960 * tipc_listen - allow socket to listen for incoming connections
1961 * @sock: socket structure
1964 * Returns 0 on success, errno otherwise
1966 static int tipc_listen(struct socket *sock, int len)
1968 struct sock *sk = sock->sk;
1973 if (sock->state != SS_UNCONNECTED)
1976 sock->state = SS_LISTENING;
1984 static int tipc_wait_for_accept(struct socket *sock, long timeo)
1986 struct sock *sk = sock->sk;
1990 /* True wake-one mechanism for incoming connections: only
1991 * one process gets woken up, not the 'whole herd'.
1992 * Since we do not 'race & poll' for established sockets
1993 * anymore, the common case will execute the loop only once.
1996 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1997 TASK_INTERRUPTIBLE);
1998 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
2000 timeo = schedule_timeout(timeo);
2004 if (!skb_queue_empty(&sk->sk_receive_queue))
2007 if (sock->state != SS_LISTENING)
2012 err = sock_intr_errno(timeo);
2013 if (signal_pending(current))
2016 finish_wait(sk_sleep(sk), &wait);
2021 * tipc_accept - wait for connection request
2022 * @sock: listening socket
2023 * @newsock: new socket that is to be connected
2024 * @flags: file-related flags associated with socket
2026 * Returns 0 on success, errno otherwise
2028 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
2030 struct sock *new_sk, *sk = sock->sk;
2031 struct sk_buff *buf;
2032 struct tipc_sock *new_tsock;
2033 struct tipc_msg *msg;
2039 if (sock->state != SS_LISTENING) {
2043 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2044 res = tipc_wait_for_accept(sock, timeo);
2048 buf = skb_peek(&sk->sk_receive_queue);
2050 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
2053 security_sk_clone(sock->sk, new_sock->sk);
2055 new_sk = new_sock->sk;
2056 new_tsock = tipc_sk(new_sk);
2059 /* we lock on new_sk; but lockdep sees the lock on sk */
2060 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
2063 * Reject any stray messages received by new socket
2064 * before the socket lock was taken (very, very unlikely)
2066 tsk_rej_rx_queue(new_sk);
2068 /* Connect new socket to it's peer */
2069 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
2070 new_sock->state = SS_CONNECTED;
2072 tsk_set_importance(new_tsock, msg_importance(msg));
2073 if (msg_named(msg)) {
2074 new_tsock->conn_type = msg_nametype(msg);
2075 new_tsock->conn_instance = msg_nameinst(msg);
2079 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2080 * Respond to 'SYN+' by queuing it on new socket.
2082 if (!msg_data_sz(msg)) {
2083 struct msghdr m = {NULL,};
2085 tsk_advance_rx_queue(sk);
2086 __tipc_send_stream(new_sock, &m, 0);
2088 __skb_dequeue(&sk->sk_receive_queue);
2089 __skb_queue_head(&new_sk->sk_receive_queue, buf);
2090 skb_set_owner_r(buf, new_sk);
2092 release_sock(new_sk);
2099 * tipc_shutdown - shutdown socket connection
2100 * @sock: socket structure
2101 * @how: direction to close (must be SHUT_RDWR)
2103 * Terminates connection (if necessary), then purges socket's receive queue.
2105 * Returns 0 on success, errno otherwise
2107 static int tipc_shutdown(struct socket *sock, int how)
2109 struct sock *sk = sock->sk;
2110 struct net *net = sock_net(sk);
2111 struct tipc_sock *tsk = tipc_sk(sk);
2112 struct sk_buff *skb;
2113 u32 dnode = tsk_peer_node(tsk);
2114 u32 dport = tsk_peer_port(tsk);
2115 u32 onode = tipc_own_addr(net);
2116 u32 oport = tsk->portid;
2119 if (how != SHUT_RDWR)
2124 switch (sock->state) {
2129 dnode = tsk_peer_node(tsk);
2131 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
2132 skb = __skb_dequeue(&sk->sk_receive_queue);
2134 if (TIPC_SKB_CB(skb)->handle != NULL) {
2138 tipc_sk_respond(sk, skb, TIPC_CONN_SHUTDOWN);
2140 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
2141 TIPC_CONN_MSG, SHORT_H_SIZE,
2142 0, dnode, onode, dport, oport,
2143 TIPC_CONN_SHUTDOWN);
2145 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
2148 sock->state = SS_DISCONNECTING;
2149 tipc_node_remove_conn(net, dnode, tsk->portid);
2152 case SS_DISCONNECTING:
2154 /* Discard any unreceived messages */
2155 __skb_queue_purge(&sk->sk_receive_queue);
2157 /* Wake up anyone sleeping in poll */
2158 sk->sk_state_change(sk);
2170 static void tipc_sk_timeout(unsigned long data)
2172 struct tipc_sock *tsk = (struct tipc_sock *)data;
2173 struct sock *sk = &tsk->sk;
2174 struct sk_buff *skb = NULL;
2175 u32 peer_port, peer_node;
2176 u32 own_node = tsk_own_node(tsk);
2179 if (!tsk->connected) {
2183 peer_port = tsk_peer_port(tsk);
2184 peer_node = tsk_peer_node(tsk);
2186 if (tsk->probing_state == TIPC_CONN_PROBING) {
2187 if (!sock_owned_by_user(sk)) {
2188 sk->sk_socket->state = SS_DISCONNECTING;
2190 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2191 tsk_peer_port(tsk));
2192 sk->sk_state_change(sk);
2194 /* Try again later */
2195 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2199 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2200 INT_H_SIZE, 0, peer_node, own_node,
2201 peer_port, tsk->portid, TIPC_OK);
2202 tsk->probing_state = TIPC_CONN_PROBING;
2203 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
2207 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
2212 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
2213 struct tipc_name_seq const *seq)
2215 struct net *net = sock_net(&tsk->sk);
2216 struct publication *publ;
2221 key = tsk->portid + tsk->pub_count + 1;
2222 if (key == tsk->portid)
2225 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
2226 scope, tsk->portid, key);
2227 if (unlikely(!publ))
2230 list_add(&publ->pport_list, &tsk->publications);
2236 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
2237 struct tipc_name_seq const *seq)
2239 struct net *net = sock_net(&tsk->sk);
2240 struct publication *publ;
2241 struct publication *safe;
2244 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
2246 if (publ->scope != scope)
2248 if (publ->type != seq->type)
2250 if (publ->lower != seq->lower)
2252 if (publ->upper != seq->upper)
2254 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2255 publ->ref, publ->key);
2259 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2260 publ->ref, publ->key);
2263 if (list_empty(&tsk->publications))
2268 /* tipc_sk_reinit: set non-zero address in all existing sockets
2269 * when we go from standalone to network mode.
2271 void tipc_sk_reinit(struct net *net)
2273 struct tipc_net *tn = net_generic(net, tipc_net_id);
2274 const struct bucket_table *tbl;
2275 struct rhash_head *pos;
2276 struct tipc_sock *tsk;
2277 struct tipc_msg *msg;
2281 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
2282 for (i = 0; i < tbl->size; i++) {
2283 rht_for_each_entry_rcu(tsk, pos, tbl, i, node) {
2284 spin_lock_bh(&tsk->sk.sk_lock.slock);
2286 msg_set_prevnode(msg, tn->own_addr);
2287 msg_set_orignode(msg, tn->own_addr);
2288 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2294 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
2296 struct tipc_net *tn = net_generic(net, tipc_net_id);
2297 struct tipc_sock *tsk;
2300 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
2302 sock_hold(&tsk->sk);
2308 static int tipc_sk_insert(struct tipc_sock *tsk)
2310 struct sock *sk = &tsk->sk;
2311 struct net *net = sock_net(sk);
2312 struct tipc_net *tn = net_generic(net, tipc_net_id);
2313 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2314 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2316 while (remaining--) {
2318 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2319 portid = TIPC_MIN_PORT;
2320 tsk->portid = portid;
2321 sock_hold(&tsk->sk);
2322 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2331 static void tipc_sk_remove(struct tipc_sock *tsk)
2333 struct sock *sk = &tsk->sk;
2334 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
2336 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
2337 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2342 static const struct rhashtable_params tsk_rht_params = {
2344 .head_offset = offsetof(struct tipc_sock, node),
2345 .key_offset = offsetof(struct tipc_sock, portid),
2346 .key_len = sizeof(u32), /* portid */
2347 .max_size = 1048576,
2349 .automatic_shrinking = true,
2352 int tipc_sk_rht_init(struct net *net)
2354 struct tipc_net *tn = net_generic(net, tipc_net_id);
2356 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
2359 void tipc_sk_rht_destroy(struct net *net)
2361 struct tipc_net *tn = net_generic(net, tipc_net_id);
2363 /* Wait for socket readers to complete */
2366 rhashtable_destroy(&tn->sk_rht);
2370 * tipc_setsockopt - set socket option
2371 * @sock: socket structure
2372 * @lvl: option level
2373 * @opt: option identifier
2374 * @ov: pointer to new option value
2375 * @ol: length of option value
2377 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
2378 * (to ease compatibility).
2380 * Returns 0 on success, errno otherwise
2382 static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2383 char __user *ov, unsigned int ol)
2385 struct sock *sk = sock->sk;
2386 struct tipc_sock *tsk = tipc_sk(sk);
2390 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2392 if (lvl != SOL_TIPC)
2393 return -ENOPROTOOPT;
2394 if (ol < sizeof(value))
2396 res = get_user(value, (u32 __user *)ov);
2403 case TIPC_IMPORTANCE:
2404 res = tsk_set_importance(tsk, value);
2406 case TIPC_SRC_DROPPABLE:
2407 if (sock->type != SOCK_STREAM)
2408 tsk_set_unreliable(tsk, value);
2412 case TIPC_DEST_DROPPABLE:
2413 tsk_set_unreturnable(tsk, value);
2415 case TIPC_CONN_TIMEOUT:
2416 tipc_sk(sk)->conn_timeout = value;
2417 /* no need to set "res", since already 0 at this point */
2429 * tipc_getsockopt - get socket option
2430 * @sock: socket structure
2431 * @lvl: option level
2432 * @opt: option identifier
2433 * @ov: receptacle for option value
2434 * @ol: receptacle for length of option value
2436 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
2437 * (to ease compatibility).
2439 * Returns 0 on success, errno otherwise
2441 static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2442 char __user *ov, int __user *ol)
2444 struct sock *sk = sock->sk;
2445 struct tipc_sock *tsk = tipc_sk(sk);
2450 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2451 return put_user(0, ol);
2452 if (lvl != SOL_TIPC)
2453 return -ENOPROTOOPT;
2454 res = get_user(len, ol);
2461 case TIPC_IMPORTANCE:
2462 value = tsk_importance(tsk);
2464 case TIPC_SRC_DROPPABLE:
2465 value = tsk_unreliable(tsk);
2467 case TIPC_DEST_DROPPABLE:
2468 value = tsk_unreturnable(tsk);
2470 case TIPC_CONN_TIMEOUT:
2471 value = tsk->conn_timeout;
2472 /* no need to set "res", since already 0 at this point */
2474 case TIPC_NODE_RECVQ_DEPTH:
2475 value = 0; /* was tipc_queue_size, now obsolete */
2477 case TIPC_SOCK_RECVQ_DEPTH:
2478 value = skb_queue_len(&sk->sk_receive_queue);
2487 return res; /* "get" failed */
2489 if (len < sizeof(value))
2492 if (copy_to_user(ov, &value, sizeof(value)))
2495 return put_user(sizeof(value), ol);
2498 static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
2500 struct sock *sk = sock->sk;
2501 struct tipc_sioc_ln_req lnr;
2502 void __user *argp = (void __user *)arg;
2505 case SIOCGETLINKNAME:
2506 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2508 if (!tipc_node_get_linkname(sock_net(sk),
2509 lnr.bearer_id & 0xffff, lnr.peer,
2510 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2511 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2515 return -EADDRNOTAVAIL;
2517 return -ENOIOCTLCMD;
2521 /* Protocol switches for the various types of TIPC sockets */
2523 static const struct proto_ops msg_ops = {
2524 .owner = THIS_MODULE,
2526 .release = tipc_release,
2528 .connect = tipc_connect,
2529 .socketpair = sock_no_socketpair,
2530 .accept = sock_no_accept,
2531 .getname = tipc_getname,
2533 .ioctl = tipc_ioctl,
2534 .listen = sock_no_listen,
2535 .shutdown = tipc_shutdown,
2536 .setsockopt = tipc_setsockopt,
2537 .getsockopt = tipc_getsockopt,
2538 .sendmsg = tipc_sendmsg,
2539 .recvmsg = tipc_recvmsg,
2540 .mmap = sock_no_mmap,
2541 .sendpage = sock_no_sendpage
2544 static const struct proto_ops packet_ops = {
2545 .owner = THIS_MODULE,
2547 .release = tipc_release,
2549 .connect = tipc_connect,
2550 .socketpair = sock_no_socketpair,
2551 .accept = tipc_accept,
2552 .getname = tipc_getname,
2554 .ioctl = tipc_ioctl,
2555 .listen = tipc_listen,
2556 .shutdown = tipc_shutdown,
2557 .setsockopt = tipc_setsockopt,
2558 .getsockopt = tipc_getsockopt,
2559 .sendmsg = tipc_send_packet,
2560 .recvmsg = tipc_recvmsg,
2561 .mmap = sock_no_mmap,
2562 .sendpage = sock_no_sendpage
2565 static const struct proto_ops stream_ops = {
2566 .owner = THIS_MODULE,
2568 .release = tipc_release,
2570 .connect = tipc_connect,
2571 .socketpair = sock_no_socketpair,
2572 .accept = tipc_accept,
2573 .getname = tipc_getname,
2575 .ioctl = tipc_ioctl,
2576 .listen = tipc_listen,
2577 .shutdown = tipc_shutdown,
2578 .setsockopt = tipc_setsockopt,
2579 .getsockopt = tipc_getsockopt,
2580 .sendmsg = tipc_send_stream,
2581 .recvmsg = tipc_recv_stream,
2582 .mmap = sock_no_mmap,
2583 .sendpage = sock_no_sendpage
2586 static const struct net_proto_family tipc_family_ops = {
2587 .owner = THIS_MODULE,
2589 .create = tipc_sk_create
2592 static struct proto tipc_proto = {
2594 .owner = THIS_MODULE,
2595 .obj_size = sizeof(struct tipc_sock),
2596 .sysctl_rmem = sysctl_tipc_rmem
2600 * tipc_socket_init - initialize TIPC socket interface
2602 * Returns 0 on success, errno otherwise
2604 int tipc_socket_init(void)
2608 res = proto_register(&tipc_proto, 1);
2610 pr_err("Failed to register TIPC protocol type\n");
2614 res = sock_register(&tipc_family_ops);
2616 pr_err("Failed to register TIPC socket type\n");
2617 proto_unregister(&tipc_proto);
2625 * tipc_socket_stop - stop TIPC socket interface
2627 void tipc_socket_stop(void)
2629 sock_unregister(tipc_family_ops.family);
2630 proto_unregister(&tipc_proto);
2633 /* Caller should hold socket lock for the passed tipc socket. */
2634 static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
2638 struct nlattr *nest;
2640 peer_node = tsk_peer_node(tsk);
2641 peer_port = tsk_peer_port(tsk);
2643 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2645 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2647 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2650 if (tsk->conn_type != 0) {
2651 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2653 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2655 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2658 nla_nest_end(skb, nest);
2663 nla_nest_cancel(skb, nest);
2668 /* Caller should hold socket lock for the passed tipc socket. */
2669 static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2670 struct tipc_sock *tsk)
2674 struct nlattr *attrs;
2675 struct net *net = sock_net(skb->sk);
2676 struct tipc_net *tn = net_generic(net, tipc_net_id);
2678 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2679 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
2683 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2685 goto genlmsg_cancel;
2686 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
2687 goto attr_msg_cancel;
2688 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
2689 goto attr_msg_cancel;
2691 if (tsk->connected) {
2692 err = __tipc_nl_add_sk_con(skb, tsk);
2694 goto attr_msg_cancel;
2695 } else if (!list_empty(&tsk->publications)) {
2696 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2697 goto attr_msg_cancel;
2699 nla_nest_end(skb, attrs);
2700 genlmsg_end(skb, hdr);
2705 nla_nest_cancel(skb, attrs);
2707 genlmsg_cancel(skb, hdr);
2712 int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2715 struct tipc_sock *tsk;
2716 const struct bucket_table *tbl;
2717 struct rhash_head *pos;
2718 struct net *net = sock_net(skb->sk);
2719 struct tipc_net *tn = net_generic(net, tipc_net_id);
2720 u32 tbl_id = cb->args[0];
2721 u32 prev_portid = cb->args[1];
2724 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
2725 for (; tbl_id < tbl->size; tbl_id++) {
2726 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
2727 spin_lock_bh(&tsk->sk.sk_lock.slock);
2728 if (prev_portid && prev_portid != tsk->portid) {
2729 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2733 err = __tipc_nl_add_sk(skb, cb, tsk);
2735 prev_portid = tsk->portid;
2736 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2740 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2745 cb->args[0] = tbl_id;
2746 cb->args[1] = prev_portid;
2751 /* Caller should hold socket lock for the passed tipc socket. */
2752 static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2753 struct netlink_callback *cb,
2754 struct publication *publ)
2757 struct nlattr *attrs;
2759 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2760 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
2764 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2766 goto genlmsg_cancel;
2768 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2769 goto attr_msg_cancel;
2770 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2771 goto attr_msg_cancel;
2772 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2773 goto attr_msg_cancel;
2774 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2775 goto attr_msg_cancel;
2777 nla_nest_end(skb, attrs);
2778 genlmsg_end(skb, hdr);
2783 nla_nest_cancel(skb, attrs);
2785 genlmsg_cancel(skb, hdr);
2790 /* Caller should hold socket lock for the passed tipc socket. */
2791 static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2792 struct netlink_callback *cb,
2793 struct tipc_sock *tsk, u32 *last_publ)
2796 struct publication *p;
2799 list_for_each_entry(p, &tsk->publications, pport_list) {
2800 if (p->key == *last_publ)
2803 if (p->key != *last_publ) {
2804 /* We never set seq or call nl_dump_check_consistent()
2805 * this means that setting prev_seq here will cause the
2806 * consistence check to fail in the netlink callback
2807 * handler. Resulting in the last NLMSG_DONE message
2808 * having the NLM_F_DUMP_INTR flag set.
2815 p = list_first_entry(&tsk->publications, struct publication,
2819 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2820 err = __tipc_nl_add_sk_publ(skb, cb, p);
2822 *last_publ = p->key;
2831 int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2834 u32 tsk_portid = cb->args[0];
2835 u32 last_publ = cb->args[1];
2836 u32 done = cb->args[2];
2837 struct net *net = sock_net(skb->sk);
2838 struct tipc_sock *tsk;
2841 struct nlattr **attrs;
2842 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2844 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2848 if (!attrs[TIPC_NLA_SOCK])
2851 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2852 attrs[TIPC_NLA_SOCK],
2853 tipc_nl_sock_policy);
2857 if (!sock[TIPC_NLA_SOCK_REF])
2860 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
2866 tsk = tipc_sk_lookup(net, tsk_portid);
2870 lock_sock(&tsk->sk);
2871 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2874 release_sock(&tsk->sk);
2877 cb->args[0] = tsk_portid;
2878 cb->args[1] = last_publ;