2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2001-2007, 2012-2016, Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
37 #include <linux/rhashtable.h>
39 #include "name_table.h"
42 #include "name_distr.h"
47 #define SS_LISTENING -1 /* socket is listening */
48 #define SS_READY -2 /* socket is connectionless */
50 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
51 #define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
52 #define TIPC_FWD_MSG 1
53 #define TIPC_CONN_OK 0
54 #define TIPC_CONN_PROBING 1
55 #define TIPC_MAX_PORT 0xffffffff
56 #define TIPC_MIN_PORT 1
59 * struct tipc_sock - TIPC socket structure
60 * @sk: socket - interacts with 'port' and with user via the socket API
61 * @connected: non-zero if port is currently connected to a peer port
62 * @conn_type: TIPC type used when connection was established
63 * @conn_instance: TIPC instance used when connection was established
64 * @published: non-zero if port has one or more associated names
65 * @max_pkt: maximum packet size "hint" used when building messages sent by port
66 * @portid: unique port identity in TIPC socket hash table
67 * @phdr: preformatted message header used when sending messages
68 * @port_list: adjacent ports in TIPC's global list of ports
69 * @publications: list of publications for port
70 * @pub_count: total # of publications port has made during its lifetime
73 * @conn_timeout: the time we can wait for an unresponded setup request
74 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
75 * @link_cong: non-zero if owner must sleep because of link congestion
76 * @sent_unacked: # messages sent by socket, and not yet acked by peer
77 * @rcv_unacked: # messages read by user, but not yet acked back to peer
78 * @remote: 'connected' peer for dgram/rdm
79 * @node: hash table node
80 * @rcu: rcu struct for tipc_sock
91 struct list_head sock_list;
92 struct list_head publications;
95 unsigned long probing_intv;
104 struct sockaddr_tipc remote;
105 struct rhash_head node;
109 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
110 static void tipc_data_ready(struct sock *sk);
111 static void tipc_write_space(struct sock *sk);
112 static void tipc_sock_destruct(struct sock *sk);
113 static int tipc_release(struct socket *sock);
114 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
115 static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
116 static void tipc_sk_timeout(unsigned long data);
117 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
118 struct tipc_name_seq const *seq);
119 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
120 struct tipc_name_seq const *seq);
121 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
122 static int tipc_sk_insert(struct tipc_sock *tsk);
123 static void tipc_sk_remove(struct tipc_sock *tsk);
124 static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
126 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
128 static const struct proto_ops packet_ops;
129 static const struct proto_ops stream_ops;
130 static const struct proto_ops msg_ops;
131 static struct proto tipc_proto;
132 static const struct rhashtable_params tsk_rht_params;
134 static u32 tsk_own_node(struct tipc_sock *tsk)
136 return msg_prevnode(&tsk->phdr);
139 static u32 tsk_peer_node(struct tipc_sock *tsk)
141 return msg_destnode(&tsk->phdr);
144 static u32 tsk_peer_port(struct tipc_sock *tsk)
146 return msg_destport(&tsk->phdr);
149 static bool tsk_unreliable(struct tipc_sock *tsk)
151 return msg_src_droppable(&tsk->phdr) != 0;
154 static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
156 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
159 static bool tsk_unreturnable(struct tipc_sock *tsk)
161 return msg_dest_droppable(&tsk->phdr) != 0;
164 static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
166 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
169 static int tsk_importance(struct tipc_sock *tsk)
171 return msg_importance(&tsk->phdr);
174 static int tsk_set_importance(struct tipc_sock *tsk, int imp)
176 if (imp > TIPC_CRITICAL_IMPORTANCE)
178 msg_set_importance(&tsk->phdr, (u32)imp);
182 static struct tipc_sock *tipc_sk(const struct sock *sk)
184 return container_of(sk, struct tipc_sock, sk);
187 static bool tsk_conn_cong(struct tipc_sock *tsk)
189 return tsk->snt_unacked > tsk->snd_win;
192 /* tsk_blocks(): translate a buffer size in bytes to number of
193 * advertisable blocks, taking into account the ratio truesize(len)/len
194 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
196 static u16 tsk_adv_blocks(int len)
198 return len / FLOWCTL_BLK_SZ / 4;
201 /* tsk_inc(): increment counter for sent or received data
202 * - If block based flow control is not supported by peer we
203 * fall back to message based ditto, incrementing the counter
205 static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
207 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
208 return ((msglen / FLOWCTL_BLK_SZ) + 1);
213 * tsk_advance_rx_queue - discard first buffer in socket receive queue
215 * Caller must hold socket lock
217 static void tsk_advance_rx_queue(struct sock *sk)
219 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
222 /* tipc_sk_respond() : send response message back to sender
224 static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
228 u32 onode = tipc_own_addr(sock_net(sk));
230 if (!tipc_msg_reverse(onode, &skb, err))
233 dnode = msg_destnode(buf_msg(skb));
234 selector = msg_origport(buf_msg(skb));
235 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
239 * tsk_rej_rx_queue - reject all buffers in socket receive queue
241 * Caller must hold socket lock
243 static void tsk_rej_rx_queue(struct sock *sk)
247 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
248 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
251 /* tsk_peer_msg - verify if message was sent by connected port's peer
253 * Handles cases where the node's network address has changed from
254 * the default of <0.0.0> to its configured setting.
256 static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
258 struct tipc_net *tn = net_generic(sock_net(&tsk->sk), tipc_net_id);
259 u32 peer_port = tsk_peer_port(tsk);
263 if (unlikely(!tsk->connected))
266 if (unlikely(msg_origport(msg) != peer_port))
269 orig_node = msg_orignode(msg);
270 peer_node = tsk_peer_node(tsk);
272 if (likely(orig_node == peer_node))
275 if (!orig_node && (peer_node == tn->own_addr))
278 if (!peer_node && (orig_node == tn->own_addr))
285 * tipc_sk_create - create a TIPC socket
286 * @net: network namespace (must be default network)
287 * @sock: pre-allocated socket structure
288 * @protocol: protocol indicator (must be 0)
289 * @kern: caused by kernel or by userspace?
291 * This routine creates additional data structures used by the TIPC socket,
292 * initializes them, and links them together.
294 * Returns 0 on success, errno otherwise
296 static int tipc_sk_create(struct net *net, struct socket *sock,
297 int protocol, int kern)
300 const struct proto_ops *ops;
303 struct tipc_sock *tsk;
304 struct tipc_msg *msg;
306 /* Validate arguments */
307 if (unlikely(protocol != 0))
308 return -EPROTONOSUPPORT;
310 switch (sock->type) {
313 state = SS_UNCONNECTED;
317 state = SS_UNCONNECTED;
328 /* Allocate socket's protocol area */
329 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
334 tsk->max_pkt = MAX_PKT_DEFAULT;
335 INIT_LIST_HEAD(&tsk->publications);
337 tn = net_generic(sock_net(sk), tipc_net_id);
339 /* Finish initializing socket data structures */
342 sock_init_data(sock, sk);
343 if (tipc_sk_insert(tsk)) {
344 pr_warn("Socket create failed; port number exhausted\n");
348 /* Ensure tsk is visible before we read own_addr. */
351 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
354 msg_set_origport(msg, tsk->portid);
355 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
356 sk->sk_backlog_rcv = tipc_backlog_rcv;
357 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
358 sk->sk_data_ready = tipc_data_ready;
359 sk->sk_write_space = tipc_write_space;
360 sk->sk_destruct = tipc_sock_destruct;
361 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
362 atomic_set(&tsk->dupl_rcvcnt, 0);
364 /* Start out with safe limits until we receive an advertised window */
365 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
366 tsk->rcv_win = tsk->snd_win;
368 if (sock->state == SS_READY) {
369 tsk_set_unreturnable(tsk, true);
370 if (sock->type == SOCK_DGRAM)
371 tsk_set_unreliable(tsk, true);
376 static void tipc_sk_callback(struct rcu_head *head)
378 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
384 * tipc_release - destroy a TIPC socket
385 * @sock: socket to destroy
387 * This routine cleans up any messages that are still queued on the socket.
388 * For DGRAM and RDM socket types, all queued messages are rejected.
389 * For SEQPACKET and STREAM socket types, the first message is rejected
390 * and any others are discarded. (If the first message on a STREAM socket
391 * is partially-read, it is discarded and the next one is rejected instead.)
393 * NOTE: Rejected messages are not necessarily returned to the sender! They
394 * are returned or discarded according to the "destination droppable" setting
395 * specified for the message by the sender.
397 * Returns 0 on success, errno otherwise
399 static int tipc_release(struct socket *sock)
401 struct sock *sk = sock->sk;
403 struct tipc_sock *tsk;
408 * Exit if socket isn't fully initialized (occurs when a failed accept()
409 * releases a pre-allocated child socket that was never used)
419 * Reject all unreceived messages, except on an active connection
420 * (which disconnects locally & sends a 'FIN+' to peer)
422 dnode = tsk_peer_node(tsk);
423 while (sock->state != SS_DISCONNECTING) {
424 skb = __skb_dequeue(&sk->sk_receive_queue);
427 if (TIPC_SKB_CB(skb)->handle != NULL)
430 if ((sock->state == SS_CONNECTING) ||
431 (sock->state == SS_CONNECTED)) {
432 sock->state = SS_DISCONNECTING;
434 tipc_node_remove_conn(net, dnode, tsk->portid);
436 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
440 tipc_sk_withdraw(tsk, 0, NULL);
441 sk_stop_timer(sk, &sk->sk_timer);
443 if (tsk->connected) {
444 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
445 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
446 tsk_own_node(tsk), tsk_peer_port(tsk),
447 tsk->portid, TIPC_ERR_NO_PORT);
449 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
450 tipc_node_remove_conn(net, dnode, tsk->portid);
453 /* Reject any messages that accumulated in backlog queue */
454 sock->state = SS_DISCONNECTING;
457 call_rcu(&tsk->rcu, tipc_sk_callback);
464 * tipc_bind - associate or disassocate TIPC name(s) with a socket
465 * @sock: socket structure
466 * @uaddr: socket address describing name(s) and desired operation
467 * @uaddr_len: size of socket address data structure
469 * Name and name sequence binding is indicated using a positive scope value;
470 * a negative scope value unbinds the specified name. Specifying no name
471 * (i.e. a socket address length of 0) unbinds all names from the socket.
473 * Returns 0 on success, errno otherwise
475 * NOTE: This routine doesn't need to take the socket lock since it doesn't
476 * access any non-constant socket information.
478 static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
481 struct sock *sk = sock->sk;
482 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
483 struct tipc_sock *tsk = tipc_sk(sk);
487 if (unlikely(!uaddr_len)) {
488 res = tipc_sk_withdraw(tsk, 0, NULL);
492 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
496 if (addr->family != AF_TIPC) {
501 if (addr->addrtype == TIPC_ADDR_NAME)
502 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
503 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
508 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
509 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
510 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
515 res = (addr->scope > 0) ?
516 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
517 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
524 * tipc_getname - get port ID of socket or peer socket
525 * @sock: socket structure
526 * @uaddr: area for returned socket address
527 * @uaddr_len: area for returned length of socket address
528 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
530 * Returns 0 on success, errno otherwise
532 * NOTE: This routine doesn't need to take the socket lock since it only
533 * accesses socket information that is unchanging (or which changes in
534 * a completely predictable manner).
536 static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
537 int *uaddr_len, int peer)
539 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
540 struct tipc_sock *tsk = tipc_sk(sock->sk);
541 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
543 memset(addr, 0, sizeof(*addr));
545 if ((sock->state != SS_CONNECTED) &&
546 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
548 addr->addr.id.ref = tsk_peer_port(tsk);
549 addr->addr.id.node = tsk_peer_node(tsk);
551 addr->addr.id.ref = tsk->portid;
552 addr->addr.id.node = tn->own_addr;
555 *uaddr_len = sizeof(*addr);
556 addr->addrtype = TIPC_ADDR_ID;
557 addr->family = AF_TIPC;
559 addr->addr.name.domain = 0;
565 * tipc_poll - read and possibly block on pollmask
566 * @file: file structure associated with the socket
567 * @sock: socket for which to calculate the poll bits
570 * Returns pollmask value
573 * It appears that the usual socket locking mechanisms are not useful here
574 * since the pollmask info is potentially out-of-date the moment this routine
575 * exits. TCP and other protocols seem to rely on higher level poll routines
576 * to handle any preventable race conditions, so TIPC will do the same ...
578 * TIPC sets the returned events as follows:
580 * socket state flags set
581 * ------------ ---------
582 * unconnected no read flags
583 * POLLOUT if port is not congested
585 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
588 * connected POLLIN/POLLRDNORM if data in rx queue
589 * POLLOUT if port is not congested
591 * disconnecting POLLIN/POLLRDNORM/POLLHUP
594 * listening POLLIN if SYN in rx queue
597 * ready POLLIN/POLLRDNORM if data in rx queue
598 * [connectionless] POLLOUT (since port cannot be congested)
600 * IMPORTANT: The fact that a read or write operation is indicated does NOT
601 * imply that the operation will succeed, merely that it should be performed
602 * and will not block.
604 static unsigned int tipc_poll(struct file *file, struct socket *sock,
607 struct sock *sk = sock->sk;
608 struct tipc_sock *tsk = tipc_sk(sk);
611 sock_poll_wait(file, sk_sleep(sk), wait);
613 switch ((int)sock->state) {
620 if (!tsk->link_cong && !tsk_conn_cong(tsk))
625 if (!skb_queue_empty(&sk->sk_receive_queue))
626 mask |= (POLLIN | POLLRDNORM);
628 case SS_DISCONNECTING:
629 mask = (POLLIN | POLLRDNORM | POLLHUP);
637 * tipc_sendmcast - send multicast message
638 * @sock: socket structure
639 * @seq: destination address
640 * @msg: message to send
641 * @dsz: total length of message data
642 * @timeo: timeout to wait for wakeup
644 * Called from function tipc_sendmsg(), which has done all sanity checks
645 * Returns the number of bytes sent on success, or errno
647 static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
648 struct msghdr *msg, size_t dsz, long timeo)
650 struct sock *sk = sock->sk;
651 struct tipc_sock *tsk = tipc_sk(sk);
652 struct net *net = sock_net(sk);
653 struct tipc_msg *mhdr = &tsk->phdr;
654 struct sk_buff_head pktchain;
655 struct iov_iter save = msg->msg_iter;
659 msg_set_type(mhdr, TIPC_MCAST_MSG);
660 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
661 msg_set_destport(mhdr, 0);
662 msg_set_destnode(mhdr, 0);
663 msg_set_nametype(mhdr, seq->type);
664 msg_set_namelower(mhdr, seq->lower);
665 msg_set_nameupper(mhdr, seq->upper);
666 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
668 skb_queue_head_init(&pktchain);
671 mtu = tipc_bcast_get_mtu(net);
672 rc = tipc_msg_build(mhdr, msg, 0, dsz, mtu, &pktchain);
673 if (unlikely(rc < 0))
677 rc = tipc_bcast_xmit(net, &pktchain);
681 if (rc == -ELINKCONG) {
683 rc = tipc_wait_for_sndmsg(sock, &timeo);
687 __skb_queue_purge(&pktchain);
688 if (rc == -EMSGSIZE) {
689 msg->msg_iter = save;
698 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
699 * @arrvq: queue with arriving messages, to be cloned after destination lookup
700 * @inputq: queue with cloned messages, delivered to socket after dest lookup
702 * Multi-threaded: parallel calls with reference to same queues may occur
704 void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
705 struct sk_buff_head *inputq)
707 struct tipc_msg *msg;
708 struct tipc_plist dports;
710 u32 scope = TIPC_CLUSTER_SCOPE;
711 struct sk_buff_head tmpq;
713 struct sk_buff *skb, *_skb;
715 __skb_queue_head_init(&tmpq);
716 tipc_plist_init(&dports);
718 skb = tipc_skb_peek(arrvq, &inputq->lock);
719 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
721 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
723 if (in_own_node(net, msg_orignode(msg)))
724 scope = TIPC_NODE_SCOPE;
726 /* Create destination port list and message clones: */
727 tipc_nametbl_mc_translate(net,
728 msg_nametype(msg), msg_namelower(msg),
729 msg_nameupper(msg), scope, &dports);
730 portid = tipc_plist_pop(&dports);
731 for (; portid; portid = tipc_plist_pop(&dports)) {
732 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
734 msg_set_destport(buf_msg(_skb), portid);
735 __skb_queue_tail(&tmpq, _skb);
738 pr_warn("Failed to clone mcast rcv buffer\n");
740 /* Append to inputq if not already done by other thread */
741 spin_lock_bh(&inputq->lock);
742 if (skb_peek(arrvq) == skb) {
743 skb_queue_splice_tail_init(&tmpq, inputq);
744 /* Decrease the skb's refcnt as increasing in the
745 * function tipc_skb_peek
747 kfree_skb(__skb_dequeue(arrvq));
749 spin_unlock_bh(&inputq->lock);
750 __skb_queue_purge(&tmpq);
753 tipc_sk_rcv(net, inputq);
757 * tipc_sk_proto_rcv - receive a connection mng protocol message
758 * @tsk: receiving socket
759 * @skb: pointer to message buffer.
761 static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
762 struct sk_buff_head *xmitq)
764 struct sock *sk = &tsk->sk;
765 u32 onode = tsk_own_node(tsk);
766 struct tipc_msg *hdr = buf_msg(skb);
767 int mtyp = msg_type(hdr);
770 /* Ignore if connection cannot be validated: */
771 if (!tsk_peer_msg(tsk, hdr))
774 tsk->probing_state = TIPC_CONN_OK;
776 if (mtyp == CONN_PROBE) {
777 msg_set_type(hdr, CONN_PROBE_REPLY);
778 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
779 __skb_queue_tail(xmitq, skb);
781 } else if (mtyp == CONN_ACK) {
782 conn_cong = tsk_conn_cong(tsk);
783 tsk->snt_unacked -= msg_conn_ack(hdr);
784 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
785 tsk->snd_win = msg_adv_win(hdr);
787 sk->sk_write_space(sk);
788 } else if (mtyp != CONN_PROBE_REPLY) {
789 pr_warn("Received unknown CONN_PROTO msg\n");
795 static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
797 struct sock *sk = sock->sk;
798 struct tipc_sock *tsk = tipc_sk(sk);
803 int err = sock_error(sk);
806 if (sock->state == SS_DISCONNECTING)
810 if (signal_pending(current))
811 return sock_intr_errno(*timeo_p);
813 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
814 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
815 finish_wait(sk_sleep(sk), &wait);
821 * tipc_sendmsg - send message in connectionless manner
822 * @sock: socket structure
823 * @m: message to send
824 * @dsz: amount of user data to be sent
826 * Message must have an destination specified explicitly.
827 * Used for SOCK_RDM and SOCK_DGRAM messages,
828 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
829 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
831 * Returns the number of bytes sent on success, or errno otherwise
833 static int tipc_sendmsg(struct socket *sock,
834 struct msghdr *m, size_t dsz)
836 struct sock *sk = sock->sk;
840 ret = __tipc_sendmsg(sock, m, dsz);
846 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
848 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
849 struct sock *sk = sock->sk;
850 struct tipc_sock *tsk = tipc_sk(sk);
851 struct net *net = sock_net(sk);
852 struct tipc_msg *mhdr = &tsk->phdr;
854 struct sk_buff_head pktchain;
856 struct tipc_name_seq *seq;
857 struct iov_iter save;
862 if (dsz > TIPC_MAX_USER_MSG_SIZE)
864 if (unlikely(!dest)) {
865 if (tsk->connected && sock->state == SS_READY)
868 return -EDESTADDRREQ;
869 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
870 dest->family != AF_TIPC) {
873 if (unlikely(sock->state != SS_READY)) {
874 if (sock->state == SS_LISTENING)
876 if (sock->state != SS_UNCONNECTED)
880 if (dest->addrtype == TIPC_ADDR_NAME) {
881 tsk->conn_type = dest->addr.name.name.type;
882 tsk->conn_instance = dest->addr.name.name.instance;
885 seq = &dest->addr.nameseq;
886 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
888 if (dest->addrtype == TIPC_ADDR_MCAST) {
889 return tipc_sendmcast(sock, seq, m, dsz, timeo);
890 } else if (dest->addrtype == TIPC_ADDR_NAME) {
891 u32 type = dest->addr.name.name.type;
892 u32 inst = dest->addr.name.name.instance;
893 u32 domain = dest->addr.name.domain;
896 msg_set_type(mhdr, TIPC_NAMED_MSG);
897 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
898 msg_set_nametype(mhdr, type);
899 msg_set_nameinst(mhdr, inst);
900 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
901 dport = tipc_nametbl_translate(net, type, inst, &dnode);
902 msg_set_destnode(mhdr, dnode);
903 msg_set_destport(mhdr, dport);
904 if (unlikely(!dport && !dnode))
905 return -EHOSTUNREACH;
906 } else if (dest->addrtype == TIPC_ADDR_ID) {
907 dnode = dest->addr.id.node;
908 msg_set_type(mhdr, TIPC_DIRECT_MSG);
909 msg_set_lookup_scope(mhdr, 0);
910 msg_set_destnode(mhdr, dnode);
911 msg_set_destport(mhdr, dest->addr.id.ref);
912 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
915 skb_queue_head_init(&pktchain);
918 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
919 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, &pktchain);
924 skb = skb_peek(&pktchain);
925 TIPC_SKB_CB(skb)->wakeup_pending = tsk->link_cong;
926 rc = tipc_node_xmit(net, &pktchain, dnode, tsk->portid);
928 if (sock->state != SS_READY)
929 sock->state = SS_CONNECTING;
932 if (rc == -ELINKCONG) {
934 rc = tipc_wait_for_sndmsg(sock, &timeo);
938 __skb_queue_purge(&pktchain);
939 if (rc == -EMSGSIZE) {
949 static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
951 struct sock *sk = sock->sk;
952 struct tipc_sock *tsk = tipc_sk(sk);
957 int err = sock_error(sk);
960 if (sock->state == SS_DISCONNECTING)
962 else if (sock->state != SS_CONNECTED)
966 if (signal_pending(current))
967 return sock_intr_errno(*timeo_p);
969 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
970 done = sk_wait_event(sk, timeo_p,
972 !tsk_conn_cong(tsk)) ||
974 finish_wait(sk_sleep(sk), &wait);
980 * tipc_send_stream - send stream-oriented data
981 * @sock: socket structure
983 * @dsz: total length of data to be transmitted
985 * Used for SOCK_STREAM data.
987 * Returns the number of bytes sent on success (or partial success),
988 * or errno if no data sent
990 static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
992 struct sock *sk = sock->sk;
996 ret = __tipc_send_stream(sock, m, dsz);
1002 static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1004 struct sock *sk = sock->sk;
1005 struct net *net = sock_net(sk);
1006 struct tipc_sock *tsk = tipc_sk(sk);
1007 struct tipc_msg *mhdr = &tsk->phdr;
1008 struct sk_buff_head pktchain;
1009 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1010 u32 portid = tsk->portid;
1014 uint mtu, send, sent = 0;
1015 struct iov_iter save;
1016 int hlen = MIN_H_SIZE;
1018 /* Handle implied connection establishment */
1019 if (unlikely(dest)) {
1020 rc = __tipc_sendmsg(sock, m, dsz);
1021 hlen = msg_hdr_sz(mhdr);
1022 if (dsz && (dsz == rc))
1023 tsk->snt_unacked = tsk_inc(tsk, dsz + hlen);
1026 if (dsz > (uint)INT_MAX)
1029 if (unlikely(sock->state != SS_CONNECTED)) {
1030 if (sock->state == SS_DISCONNECTING)
1036 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1037 dnode = tsk_peer_node(tsk);
1038 skb_queue_head_init(&pktchain);
1043 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
1044 rc = tipc_msg_build(mhdr, m, sent, send, mtu, &pktchain);
1045 if (unlikely(rc < 0))
1049 if (likely(!tsk_conn_cong(tsk))) {
1050 rc = tipc_node_xmit(net, &pktchain, dnode, portid);
1052 tsk->snt_unacked += tsk_inc(tsk, send + hlen);
1058 if (rc == -EMSGSIZE) {
1059 __skb_queue_purge(&pktchain);
1060 tsk->max_pkt = tipc_node_get_mtu(net, dnode,
1065 if (rc != -ELINKCONG)
1070 rc = tipc_wait_for_sndpkt(sock, &timeo);
1073 __skb_queue_purge(&pktchain);
1074 return sent ? sent : rc;
1078 * tipc_send_packet - send a connection-oriented message
1079 * @sock: socket structure
1080 * @m: message to send
1081 * @dsz: length of data to be transmitted
1083 * Used for SOCK_SEQPACKET messages.
1085 * Returns the number of bytes sent on success, or errno otherwise
1087 static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
1089 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1092 return tipc_send_stream(sock, m, dsz);
1095 /* tipc_sk_finish_conn - complete the setup of a connection
1097 static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
1100 struct sock *sk = &tsk->sk;
1101 struct net *net = sock_net(sk);
1102 struct tipc_msg *msg = &tsk->phdr;
1104 msg_set_destnode(msg, peer_node);
1105 msg_set_destport(msg, peer_port);
1106 msg_set_type(msg, TIPC_CONN_MSG);
1107 msg_set_lookup_scope(msg, 0);
1108 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1110 tsk->probing_intv = CONN_PROBING_INTERVAL;
1111 tsk->probing_state = TIPC_CONN_OK;
1113 sk_reset_timer(sk, &sk->sk_timer, jiffies + tsk->probing_intv);
1114 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1115 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
1116 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
1117 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1120 /* Fall back to message based flow control */
1121 tsk->rcv_win = FLOWCTL_MSG_WIN;
1122 tsk->snd_win = FLOWCTL_MSG_WIN;
1126 * set_orig_addr - capture sender's address for received message
1127 * @m: descriptor for message info
1128 * @msg: received message header
1130 * Note: Address is not captured if not requested by receiver.
1132 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
1134 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
1137 addr->family = AF_TIPC;
1138 addr->addrtype = TIPC_ADDR_ID;
1139 memset(&addr->addr, 0, sizeof(addr->addr));
1140 addr->addr.id.ref = msg_origport(msg);
1141 addr->addr.id.node = msg_orignode(msg);
1142 addr->addr.name.domain = 0; /* could leave uninitialized */
1143 addr->scope = 0; /* could leave uninitialized */
1144 m->msg_namelen = sizeof(struct sockaddr_tipc);
1149 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
1150 * @m: descriptor for message info
1151 * @msg: received message header
1152 * @tsk: TIPC port associated with message
1154 * Note: Ancillary data is not captured if not requested by receiver.
1156 * Returns 0 if successful, otherwise errno
1158 static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1159 struct tipc_sock *tsk)
1167 if (likely(m->msg_controllen == 0))
1170 /* Optionally capture errored message object(s) */
1171 err = msg ? msg_errcode(msg) : 0;
1172 if (unlikely(err)) {
1174 anc_data[1] = msg_data_sz(msg);
1175 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1179 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1186 /* Optionally capture message destination object */
1187 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1188 switch (dest_type) {
1189 case TIPC_NAMED_MSG:
1191 anc_data[0] = msg_nametype(msg);
1192 anc_data[1] = msg_namelower(msg);
1193 anc_data[2] = msg_namelower(msg);
1195 case TIPC_MCAST_MSG:
1197 anc_data[0] = msg_nametype(msg);
1198 anc_data[1] = msg_namelower(msg);
1199 anc_data[2] = msg_nameupper(msg);
1202 has_name = (tsk->conn_type != 0);
1203 anc_data[0] = tsk->conn_type;
1204 anc_data[1] = tsk->conn_instance;
1205 anc_data[2] = tsk->conn_instance;
1211 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1219 static void tipc_sk_send_ack(struct tipc_sock *tsk)
1221 struct net *net = sock_net(&tsk->sk);
1222 struct sk_buff *skb = NULL;
1223 struct tipc_msg *msg;
1224 u32 peer_port = tsk_peer_port(tsk);
1225 u32 dnode = tsk_peer_node(tsk);
1227 if (!tsk->connected)
1229 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1230 dnode, tsk_own_node(tsk), peer_port,
1231 tsk->portid, TIPC_OK);
1235 msg_set_conn_ack(msg, tsk->rcv_unacked);
1236 tsk->rcv_unacked = 0;
1238 /* Adjust to and advertize the correct window limit */
1239 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1240 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1241 msg_set_adv_win(msg, tsk->rcv_win);
1243 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
1246 static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1248 struct sock *sk = sock->sk;
1250 long timeo = *timeop;
1254 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1255 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
1256 if (sock->state == SS_DISCONNECTING) {
1261 timeo = schedule_timeout(timeo);
1265 if (!skb_queue_empty(&sk->sk_receive_queue))
1270 err = sock_intr_errno(timeo);
1271 if (signal_pending(current))
1274 finish_wait(sk_sleep(sk), &wait);
1280 * tipc_recvmsg - receive packet-oriented message
1281 * @m: descriptor for message info
1282 * @buf_len: total size of user buffer area
1283 * @flags: receive flags
1285 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1286 * If the complete message doesn't fit in user area, truncate it.
1288 * Returns size of returned message data, errno otherwise
1290 static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1293 struct sock *sk = sock->sk;
1294 struct tipc_sock *tsk = tipc_sk(sk);
1295 struct sk_buff *buf;
1296 struct tipc_msg *msg;
1302 /* Catch invalid receive requests */
1303 if (unlikely(!buf_len))
1308 if (unlikely(sock->state == SS_UNCONNECTED)) {
1313 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1316 /* Look for a message in receive queue; wait if necessary */
1317 res = tipc_wait_for_rcvmsg(sock, &timeo);
1321 /* Look at first message in receive queue */
1322 buf = skb_peek(&sk->sk_receive_queue);
1324 sz = msg_data_sz(msg);
1325 hlen = msg_hdr_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, hlen, m, sz);
1353 if ((sock->state == SS_READY) ||
1354 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1360 if (unlikely(flags & MSG_PEEK))
1363 if (likely(sock->state != SS_READY)) {
1364 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1365 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1366 tipc_sk_send_ack(tsk);
1368 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 hlen = msg_hdr_sz(msg);
1424 err = msg_errcode(msg);
1426 /* Discard an empty non-errored message & try again */
1427 if ((!sz) && (!err)) {
1428 tsk_advance_rx_queue(sk);
1432 /* Optionally capture sender's address & ancillary data of first msg */
1433 if (sz_copied == 0) {
1434 set_orig_addr(m, msg);
1435 res = tipc_sk_anc_data_recv(m, msg, tsk);
1440 /* Capture message data (if valid) & compute return value (always) */
1442 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
1445 needed = (buf_len - sz_copied);
1446 sz_to_copy = (sz <= needed) ? sz : needed;
1448 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy);
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 if (unlikely(flags & MSG_PEEK))
1473 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1474 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1475 tipc_sk_send_ack(tsk);
1476 tsk_advance_rx_queue(sk);
1478 /* Loop around if more data is required */
1479 if ((sz_copied < buf_len) && /* didn't get all requested data */
1480 (!skb_queue_empty(&sk->sk_receive_queue) ||
1481 (sz_copied < target)) && /* and more is ready or required */
1482 (!err)) /* and haven't reached a FIN */
1487 return sz_copied ? sz_copied : res;
1491 * tipc_write_space - wake up thread if port congestion is released
1494 static void tipc_write_space(struct sock *sk)
1496 struct socket_wq *wq;
1499 wq = rcu_dereference(sk->sk_wq);
1500 if (skwq_has_sleeper(wq))
1501 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1502 POLLWRNORM | POLLWRBAND);
1507 * tipc_data_ready - wake up threads to indicate messages have been received
1509 * @len: the length of messages
1511 static void tipc_data_ready(struct sock *sk)
1513 struct socket_wq *wq;
1516 wq = rcu_dereference(sk->sk_wq);
1517 if (skwq_has_sleeper(wq))
1518 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1519 POLLRDNORM | POLLRDBAND);
1523 static void tipc_sock_destruct(struct sock *sk)
1525 __skb_queue_purge(&sk->sk_receive_queue);
1529 * filter_connect - Handle all incoming messages for a connection-based socket
1531 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
1533 * Returns true if everything ok, false otherwise
1535 static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
1537 struct sock *sk = &tsk->sk;
1538 struct net *net = sock_net(sk);
1539 struct socket *sock = sk->sk_socket;
1540 struct tipc_msg *hdr = buf_msg(skb);
1542 if (unlikely(msg_mcast(hdr)))
1545 switch ((int)sock->state) {
1548 /* Accept only connection-based messages sent by peer */
1549 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1552 if (unlikely(msg_errcode(hdr))) {
1553 sock->state = SS_DISCONNECTING;
1555 /* Let timer expire on it's own */
1556 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1563 /* Accept only ACK or NACK message */
1564 if (unlikely(!msg_connected(hdr)))
1567 if (unlikely(msg_errcode(hdr))) {
1568 sock->state = SS_DISCONNECTING;
1569 sk->sk_err = ECONNREFUSED;
1573 if (unlikely(!msg_isdata(hdr))) {
1574 sock->state = SS_DISCONNECTING;
1575 sk->sk_err = EINVAL;
1579 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1580 msg_set_importance(&tsk->phdr, msg_importance(hdr));
1581 sock->state = SS_CONNECTED;
1583 /* If 'ACK+' message, add to socket receive queue */
1584 if (msg_data_sz(hdr))
1587 /* If empty 'ACK-' message, wake up sleeping connect() */
1588 if (waitqueue_active(sk_sleep(sk)))
1589 wake_up_interruptible(sk_sleep(sk));
1591 /* 'ACK-' message is neither accepted nor rejected: */
1592 msg_set_dest_droppable(hdr, 1);
1596 case SS_UNCONNECTED:
1598 /* Accept only SYN message */
1599 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1602 case SS_DISCONNECTING:
1605 pr_err("Unknown socket state %u\n", sock->state);
1611 * rcvbuf_limit - get proper overload limit of socket receive queue
1615 * For connection oriented messages, irrespective of importance,
1616 * default queue limit is 2 MB.
1618 * For connectionless messages, queue limits are based on message
1619 * importance as follows:
1621 * TIPC_LOW_IMPORTANCE (2 MB)
1622 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1623 * TIPC_HIGH_IMPORTANCE (8 MB)
1624 * TIPC_CRITICAL_IMPORTANCE (16 MB)
1626 * Returns overload limit according to corresponding message importance
1628 static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
1630 struct tipc_sock *tsk = tipc_sk(sk);
1631 struct tipc_msg *hdr = buf_msg(skb);
1633 if (unlikely(!msg_connected(hdr)))
1634 return sk->sk_rcvbuf << msg_importance(hdr);
1636 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1637 return sk->sk_rcvbuf;
1639 return FLOWCTL_MSG_LIM;
1643 * filter_rcv - validate incoming message
1645 * @skb: pointer to message.
1647 * Enqueues message on receive queue if acceptable; optionally handles
1648 * disconnect indication for a connected socket.
1650 * Called with socket lock already taken
1652 * Returns true if message was added to socket receive queue, otherwise false
1654 static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1655 struct sk_buff_head *xmitq)
1657 struct socket *sock = sk->sk_socket;
1658 struct tipc_sock *tsk = tipc_sk(sk);
1659 struct tipc_msg *hdr = buf_msg(skb);
1660 unsigned int limit = rcvbuf_limit(sk, skb);
1662 int usr = msg_user(hdr);
1664 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
1665 tipc_sk_proto_rcv(tsk, skb, xmitq);
1669 if (unlikely(usr == SOCK_WAKEUP)) {
1672 sk->sk_write_space(sk);
1676 /* Drop if illegal message type */
1677 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1682 /* Reject if wrong message type for current socket state */
1683 if (unlikely(sock->state == SS_READY)) {
1684 if (msg_connected(hdr)) {
1685 err = TIPC_ERR_NO_PORT;
1688 } else if (unlikely(!filter_connect(tsk, skb))) {
1689 err = TIPC_ERR_NO_PORT;
1693 /* Reject message if there isn't room to queue it */
1694 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1695 err = TIPC_ERR_OVERLOAD;
1699 /* Enqueue message */
1700 TIPC_SKB_CB(skb)->handle = NULL;
1701 __skb_queue_tail(&sk->sk_receive_queue, skb);
1702 skb_set_owner_r(skb, sk);
1704 sk->sk_data_ready(sk);
1708 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1709 __skb_queue_tail(xmitq, skb);
1714 * tipc_backlog_rcv - handle incoming message from backlog queue
1718 * Caller must hold socket lock
1722 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
1724 unsigned int truesize = skb->truesize;
1725 struct sk_buff_head xmitq;
1726 u32 dnode, selector;
1728 __skb_queue_head_init(&xmitq);
1730 if (likely(filter_rcv(sk, skb, &xmitq))) {
1731 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
1735 if (skb_queue_empty(&xmitq))
1738 /* Send response/rejected message */
1739 skb = __skb_dequeue(&xmitq);
1740 dnode = msg_destnode(buf_msg(skb));
1741 selector = msg_origport(buf_msg(skb));
1742 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
1747 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1748 * inputq and try adding them to socket or backlog queue
1749 * @inputq: list of incoming buffers with potentially different destinations
1750 * @sk: socket where the buffers should be enqueued
1751 * @dport: port number for the socket
1753 * Caller must hold socket lock
1755 static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
1756 u32 dport, struct sk_buff_head *xmitq)
1758 unsigned long time_limit = jiffies + usecs_to_jiffies(20000);
1759 struct sk_buff *skb;
1764 while (skb_queue_len(inputq)) {
1765 if (unlikely(time_after_eq(jiffies, time_limit)))
1768 skb = tipc_skb_dequeue(inputq, dport);
1772 /* Add message directly to receive queue if possible */
1773 if (!sock_owned_by_user(sk)) {
1774 filter_rcv(sk, skb, xmitq);
1778 /* Try backlog, compensating for double-counted bytes */
1779 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
1780 if (!sk->sk_backlog.len)
1781 atomic_set(dcnt, 0);
1782 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1783 if (likely(!sk_add_backlog(sk, skb, lim)))
1786 /* Overload => reject message back to sender */
1787 onode = tipc_own_addr(sock_net(sk));
1788 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1789 __skb_queue_tail(xmitq, skb);
1795 * tipc_sk_rcv - handle a chain of incoming buffers
1796 * @inputq: buffer list containing the buffers
1797 * Consumes all buffers in list until inputq is empty
1798 * Note: may be called in multiple threads referring to the same queue
1800 void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
1802 struct sk_buff_head xmitq;
1803 u32 dnode, dport = 0;
1805 struct tipc_sock *tsk;
1807 struct sk_buff *skb;
1809 __skb_queue_head_init(&xmitq);
1810 while (skb_queue_len(inputq)) {
1811 dport = tipc_skb_peek_port(inputq, dport);
1812 tsk = tipc_sk_lookup(net, dport);
1816 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
1817 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
1818 spin_unlock_bh(&sk->sk_lock.slock);
1820 /* Send pending response/rejected messages, if any */
1821 while ((skb = __skb_dequeue(&xmitq))) {
1822 dnode = msg_destnode(buf_msg(skb));
1823 tipc_node_xmit_skb(net, skb, dnode, dport);
1829 /* No destination socket => dequeue skb if still there */
1830 skb = tipc_skb_dequeue(inputq, dport);
1834 /* Try secondary lookup if unresolved named message */
1835 err = TIPC_ERR_NO_PORT;
1836 if (tipc_msg_lookup_dest(net, skb, &err))
1839 /* Prepare for message rejection */
1840 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
1843 dnode = msg_destnode(buf_msg(skb));
1844 tipc_node_xmit_skb(net, skb, dnode, dport);
1848 static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1850 struct sock *sk = sock->sk;
1855 int err = sock_error(sk);
1860 if (signal_pending(current))
1861 return sock_intr_errno(*timeo_p);
1863 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1864 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1865 finish_wait(sk_sleep(sk), &wait);
1871 * tipc_connect - establish a connection to another TIPC port
1872 * @sock: socket structure
1873 * @dest: socket address for destination port
1874 * @destlen: size of socket address data structure
1875 * @flags: file-related flags associated with socket
1877 * Returns 0 on success, errno otherwise
1879 static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1880 int destlen, int flags)
1882 struct sock *sk = sock->sk;
1883 struct tipc_sock *tsk = tipc_sk(sk);
1884 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1885 struct msghdr m = {NULL,};
1886 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
1887 socket_state previous;
1892 /* DGRAM/RDM connect(), just save the destaddr */
1893 if (sock->state == SS_READY) {
1894 if (dst->family == AF_UNSPEC) {
1895 memset(&tsk->remote, 0, sizeof(struct sockaddr_tipc));
1897 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1900 memcpy(&tsk->remote, dest, destlen);
1907 * Reject connection attempt using multicast address
1909 * Note: send_msg() validates the rest of the address fields,
1910 * so there's no need to do it here
1912 if (dst->addrtype == TIPC_ADDR_MCAST) {
1917 previous = sock->state;
1918 switch (sock->state) {
1919 case SS_UNCONNECTED:
1920 /* Send a 'SYN-' to destination */
1922 m.msg_namelen = destlen;
1924 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1925 * indicate send_msg() is never blocked.
1928 m.msg_flags = MSG_DONTWAIT;
1930 res = __tipc_sendmsg(sock, &m, 0);
1931 if ((res < 0) && (res != -EWOULDBLOCK))
1934 /* Just entered SS_CONNECTING state; the only
1935 * difference is that return value in non-blocking
1936 * case is EINPROGRESS, rather than EALREADY.
1940 if (previous == SS_CONNECTING)
1944 timeout = msecs_to_jiffies(timeout);
1945 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1946 res = tipc_wait_for_connect(sock, &timeout);
1961 * tipc_listen - allow socket to listen for incoming connections
1962 * @sock: socket structure
1965 * Returns 0 on success, errno otherwise
1967 static int tipc_listen(struct socket *sock, int len)
1969 struct sock *sk = sock->sk;
1974 if (sock->state != SS_UNCONNECTED)
1977 sock->state = SS_LISTENING;
1985 static int tipc_wait_for_accept(struct socket *sock, long timeo)
1987 struct sock *sk = sock->sk;
1988 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1991 /* True wake-one mechanism for incoming connections: only
1992 * one process gets woken up, not the 'whole herd'.
1993 * Since we do not 'race & poll' for established sockets
1994 * anymore, the common case will execute the loop only once.
1997 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
1998 add_wait_queue(sk_sleep(sk), &wait);
2000 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
2002 remove_wait_queue(sk_sleep(sk), &wait);
2005 if (!skb_queue_empty(&sk->sk_receive_queue))
2008 if (sock->state != SS_LISTENING)
2013 err = sock_intr_errno(timeo);
2014 if (signal_pending(current))
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 struct rhashtable_iter iter;
2275 struct tipc_sock *tsk;
2276 struct tipc_msg *msg;
2278 rhashtable_walk_enter(&tn->sk_rht, &iter);
2281 tsk = ERR_PTR(rhashtable_walk_start(&iter));
2285 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
2286 sock_hold(&tsk->sk);
2287 rhashtable_walk_stop(&iter);
2288 lock_sock(&tsk->sk);
2290 msg_set_prevnode(msg, tn->own_addr);
2291 msg_set_orignode(msg, tn->own_addr);
2292 release_sock(&tsk->sk);
2293 rhashtable_walk_start(&iter);
2297 rhashtable_walk_stop(&iter);
2298 } while (tsk == ERR_PTR(-EAGAIN));
2301 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
2303 struct tipc_net *tn = net_generic(net, tipc_net_id);
2304 struct tipc_sock *tsk;
2307 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
2309 sock_hold(&tsk->sk);
2315 static int tipc_sk_insert(struct tipc_sock *tsk)
2317 struct sock *sk = &tsk->sk;
2318 struct net *net = sock_net(sk);
2319 struct tipc_net *tn = net_generic(net, tipc_net_id);
2320 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2321 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2323 while (remaining--) {
2325 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2326 portid = TIPC_MIN_PORT;
2327 tsk->portid = portid;
2328 sock_hold(&tsk->sk);
2329 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2338 static void tipc_sk_remove(struct tipc_sock *tsk)
2340 struct sock *sk = &tsk->sk;
2341 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
2343 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
2344 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2349 static const struct rhashtable_params tsk_rht_params = {
2351 .head_offset = offsetof(struct tipc_sock, node),
2352 .key_offset = offsetof(struct tipc_sock, portid),
2353 .key_len = sizeof(u32), /* portid */
2354 .max_size = 1048576,
2356 .automatic_shrinking = true,
2359 int tipc_sk_rht_init(struct net *net)
2361 struct tipc_net *tn = net_generic(net, tipc_net_id);
2363 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
2366 void tipc_sk_rht_destroy(struct net *net)
2368 struct tipc_net *tn = net_generic(net, tipc_net_id);
2370 /* Wait for socket readers to complete */
2373 rhashtable_destroy(&tn->sk_rht);
2377 * tipc_setsockopt - set socket option
2378 * @sock: socket structure
2379 * @lvl: option level
2380 * @opt: option identifier
2381 * @ov: pointer to new option value
2382 * @ol: length of option value
2384 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
2385 * (to ease compatibility).
2387 * Returns 0 on success, errno otherwise
2389 static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2390 char __user *ov, unsigned int ol)
2392 struct sock *sk = sock->sk;
2393 struct tipc_sock *tsk = tipc_sk(sk);
2397 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2399 if (lvl != SOL_TIPC)
2400 return -ENOPROTOOPT;
2401 if (ol < sizeof(value))
2403 res = get_user(value, (u32 __user *)ov);
2410 case TIPC_IMPORTANCE:
2411 res = tsk_set_importance(tsk, value);
2413 case TIPC_SRC_DROPPABLE:
2414 if (sock->type != SOCK_STREAM)
2415 tsk_set_unreliable(tsk, value);
2419 case TIPC_DEST_DROPPABLE:
2420 tsk_set_unreturnable(tsk, value);
2422 case TIPC_CONN_TIMEOUT:
2423 tipc_sk(sk)->conn_timeout = value;
2424 /* no need to set "res", since already 0 at this point */
2436 * tipc_getsockopt - get socket option
2437 * @sock: socket structure
2438 * @lvl: option level
2439 * @opt: option identifier
2440 * @ov: receptacle for option value
2441 * @ol: receptacle for length of option value
2443 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
2444 * (to ease compatibility).
2446 * Returns 0 on success, errno otherwise
2448 static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2449 char __user *ov, int __user *ol)
2451 struct sock *sk = sock->sk;
2452 struct tipc_sock *tsk = tipc_sk(sk);
2457 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2458 return put_user(0, ol);
2459 if (lvl != SOL_TIPC)
2460 return -ENOPROTOOPT;
2461 res = get_user(len, ol);
2468 case TIPC_IMPORTANCE:
2469 value = tsk_importance(tsk);
2471 case TIPC_SRC_DROPPABLE:
2472 value = tsk_unreliable(tsk);
2474 case TIPC_DEST_DROPPABLE:
2475 value = tsk_unreturnable(tsk);
2477 case TIPC_CONN_TIMEOUT:
2478 value = tsk->conn_timeout;
2479 /* no need to set "res", since already 0 at this point */
2481 case TIPC_NODE_RECVQ_DEPTH:
2482 value = 0; /* was tipc_queue_size, now obsolete */
2484 case TIPC_SOCK_RECVQ_DEPTH:
2485 value = skb_queue_len(&sk->sk_receive_queue);
2494 return res; /* "get" failed */
2496 if (len < sizeof(value))
2499 if (copy_to_user(ov, &value, sizeof(value)))
2502 return put_user(sizeof(value), ol);
2505 static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
2507 struct sock *sk = sock->sk;
2508 struct tipc_sioc_ln_req lnr;
2509 void __user *argp = (void __user *)arg;
2512 case SIOCGETLINKNAME:
2513 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2515 if (!tipc_node_get_linkname(sock_net(sk),
2516 lnr.bearer_id & 0xffff, lnr.peer,
2517 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2518 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2522 return -EADDRNOTAVAIL;
2524 return -ENOIOCTLCMD;
2528 /* Protocol switches for the various types of TIPC sockets */
2530 static const struct proto_ops msg_ops = {
2531 .owner = THIS_MODULE,
2533 .release = tipc_release,
2535 .connect = tipc_connect,
2536 .socketpair = sock_no_socketpair,
2537 .accept = sock_no_accept,
2538 .getname = tipc_getname,
2540 .ioctl = tipc_ioctl,
2541 .listen = sock_no_listen,
2542 .shutdown = tipc_shutdown,
2543 .setsockopt = tipc_setsockopt,
2544 .getsockopt = tipc_getsockopt,
2545 .sendmsg = tipc_sendmsg,
2546 .recvmsg = tipc_recvmsg,
2547 .mmap = sock_no_mmap,
2548 .sendpage = sock_no_sendpage
2551 static const struct proto_ops packet_ops = {
2552 .owner = THIS_MODULE,
2554 .release = tipc_release,
2556 .connect = tipc_connect,
2557 .socketpair = sock_no_socketpair,
2558 .accept = tipc_accept,
2559 .getname = tipc_getname,
2561 .ioctl = tipc_ioctl,
2562 .listen = tipc_listen,
2563 .shutdown = tipc_shutdown,
2564 .setsockopt = tipc_setsockopt,
2565 .getsockopt = tipc_getsockopt,
2566 .sendmsg = tipc_send_packet,
2567 .recvmsg = tipc_recvmsg,
2568 .mmap = sock_no_mmap,
2569 .sendpage = sock_no_sendpage
2572 static const struct proto_ops stream_ops = {
2573 .owner = THIS_MODULE,
2575 .release = tipc_release,
2577 .connect = tipc_connect,
2578 .socketpair = sock_no_socketpair,
2579 .accept = tipc_accept,
2580 .getname = tipc_getname,
2582 .ioctl = tipc_ioctl,
2583 .listen = tipc_listen,
2584 .shutdown = tipc_shutdown,
2585 .setsockopt = tipc_setsockopt,
2586 .getsockopt = tipc_getsockopt,
2587 .sendmsg = tipc_send_stream,
2588 .recvmsg = tipc_recv_stream,
2589 .mmap = sock_no_mmap,
2590 .sendpage = sock_no_sendpage
2593 static const struct net_proto_family tipc_family_ops = {
2594 .owner = THIS_MODULE,
2596 .create = tipc_sk_create
2599 static struct proto tipc_proto = {
2601 .owner = THIS_MODULE,
2602 .obj_size = sizeof(struct tipc_sock),
2603 .sysctl_rmem = sysctl_tipc_rmem
2607 * tipc_socket_init - initialize TIPC socket interface
2609 * Returns 0 on success, errno otherwise
2611 int tipc_socket_init(void)
2615 res = proto_register(&tipc_proto, 1);
2617 pr_err("Failed to register TIPC protocol type\n");
2621 res = sock_register(&tipc_family_ops);
2623 pr_err("Failed to register TIPC socket type\n");
2624 proto_unregister(&tipc_proto);
2632 * tipc_socket_stop - stop TIPC socket interface
2634 void tipc_socket_stop(void)
2636 sock_unregister(tipc_family_ops.family);
2637 proto_unregister(&tipc_proto);
2640 /* Caller should hold socket lock for the passed tipc socket. */
2641 static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
2645 struct nlattr *nest;
2647 peer_node = tsk_peer_node(tsk);
2648 peer_port = tsk_peer_port(tsk);
2650 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2652 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2654 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2657 if (tsk->conn_type != 0) {
2658 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2660 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2662 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2665 nla_nest_end(skb, nest);
2670 nla_nest_cancel(skb, nest);
2675 /* Caller should hold socket lock for the passed tipc socket. */
2676 static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2677 struct tipc_sock *tsk)
2681 struct nlattr *attrs;
2682 struct net *net = sock_net(skb->sk);
2683 struct tipc_net *tn = net_generic(net, tipc_net_id);
2685 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2686 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
2690 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2692 goto genlmsg_cancel;
2693 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
2694 goto attr_msg_cancel;
2695 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
2696 goto attr_msg_cancel;
2698 if (tsk->connected) {
2699 err = __tipc_nl_add_sk_con(skb, tsk);
2701 goto attr_msg_cancel;
2702 } else if (!list_empty(&tsk->publications)) {
2703 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2704 goto attr_msg_cancel;
2706 nla_nest_end(skb, attrs);
2707 genlmsg_end(skb, hdr);
2712 nla_nest_cancel(skb, attrs);
2714 genlmsg_cancel(skb, hdr);
2719 int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2722 struct tipc_sock *tsk;
2723 const struct bucket_table *tbl;
2724 struct rhash_head *pos;
2725 struct net *net = sock_net(skb->sk);
2726 struct tipc_net *tn = net_generic(net, tipc_net_id);
2727 u32 tbl_id = cb->args[0];
2728 u32 prev_portid = cb->args[1];
2731 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
2732 for (; tbl_id < tbl->size; tbl_id++) {
2733 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
2734 spin_lock_bh(&tsk->sk.sk_lock.slock);
2735 if (prev_portid && prev_portid != tsk->portid) {
2736 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2740 err = __tipc_nl_add_sk(skb, cb, tsk);
2742 prev_portid = tsk->portid;
2743 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2747 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2752 cb->args[0] = tbl_id;
2753 cb->args[1] = prev_portid;
2758 /* Caller should hold socket lock for the passed tipc socket. */
2759 static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2760 struct netlink_callback *cb,
2761 struct publication *publ)
2764 struct nlattr *attrs;
2766 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
2767 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
2771 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2773 goto genlmsg_cancel;
2775 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2776 goto attr_msg_cancel;
2777 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2778 goto attr_msg_cancel;
2779 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2780 goto attr_msg_cancel;
2781 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2782 goto attr_msg_cancel;
2784 nla_nest_end(skb, attrs);
2785 genlmsg_end(skb, hdr);
2790 nla_nest_cancel(skb, attrs);
2792 genlmsg_cancel(skb, hdr);
2797 /* Caller should hold socket lock for the passed tipc socket. */
2798 static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2799 struct netlink_callback *cb,
2800 struct tipc_sock *tsk, u32 *last_publ)
2803 struct publication *p;
2806 list_for_each_entry(p, &tsk->publications, pport_list) {
2807 if (p->key == *last_publ)
2810 if (p->key != *last_publ) {
2811 /* We never set seq or call nl_dump_check_consistent()
2812 * this means that setting prev_seq here will cause the
2813 * consistence check to fail in the netlink callback
2814 * handler. Resulting in the last NLMSG_DONE message
2815 * having the NLM_F_DUMP_INTR flag set.
2822 p = list_first_entry(&tsk->publications, struct publication,
2826 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2827 err = __tipc_nl_add_sk_publ(skb, cb, p);
2829 *last_publ = p->key;
2838 int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2841 u32 tsk_portid = cb->args[0];
2842 u32 last_publ = cb->args[1];
2843 u32 done = cb->args[2];
2844 struct net *net = sock_net(skb->sk);
2845 struct tipc_sock *tsk;
2848 struct nlattr **attrs;
2849 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2851 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2855 if (!attrs[TIPC_NLA_SOCK])
2858 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2859 attrs[TIPC_NLA_SOCK],
2860 tipc_nl_sock_policy);
2864 if (!sock[TIPC_NLA_SOCK_REF])
2867 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
2873 tsk = tipc_sk_lookup(net, tsk_portid);
2877 lock_sock(&tsk->sk);
2878 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2881 release_sock(&tsk->sk);
2884 cb->args[0] = tsk_portid;
2885 cb->args[1] = last_publ;