2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2001-2007, 2012-2014, 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.
39 #include "name_table.h"
42 #include <linux/export.h>
45 #define SS_LISTENING -1 /* socket is listening */
46 #define SS_READY -2 /* socket is connectionless */
48 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
49 #define CONN_PROBING_INTERVAL 3600000 /* [ms] => 1 h */
50 #define TIPC_FWD_MSG 1
52 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
53 static void tipc_data_ready(struct sock *sk);
54 static void tipc_write_space(struct sock *sk);
55 static int tipc_release(struct socket *sock);
56 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
57 static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p);
58 static void tipc_sk_timeout(unsigned long ref);
60 static const struct proto_ops packet_ops;
61 static const struct proto_ops stream_ops;
62 static const struct proto_ops msg_ops;
64 static struct proto tipc_proto;
65 static struct proto tipc_proto_kern;
68 * Revised TIPC socket locking policy:
70 * Most socket operations take the standard socket lock when they start
71 * and hold it until they finish (or until they need to sleep). Acquiring
72 * this lock grants the owner exclusive access to the fields of the socket
73 * data structures, with the exception of the backlog queue. A few socket
74 * operations can be done without taking the socket lock because they only
75 * read socket information that never changes during the life of the socket.
77 * Socket operations may acquire the lock for the associated TIPC port if they
78 * need to perform an operation on the port. If any routine needs to acquire
79 * both the socket lock and the port lock it must take the socket lock first
80 * to avoid the risk of deadlock.
82 * The dispatcher handling incoming messages cannot grab the socket lock in
83 * the standard fashion, since invoked it runs at the BH level and cannot block.
84 * Instead, it checks to see if the socket lock is currently owned by someone,
85 * and either handles the message itself or adds it to the socket's backlog
86 * queue; in the latter case the queued message is processed once the process
87 * owning the socket lock releases it.
89 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
90 * the problem of a blocked socket operation preventing any other operations
91 * from occurring. However, applications must be careful if they have
92 * multiple threads trying to send (or receive) on the same socket, as these
93 * operations might interfere with each other. For example, doing a connect
94 * and a receive at the same time might allow the receive to consume the
95 * ACK message meant for the connect. While additional work could be done
96 * to try and overcome this, it doesn't seem to be worthwhile at the present.
98 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
99 * that another operation that must be performed in a non-blocking manner is
100 * not delayed for very long because the lock has already been taken.
102 * NOTE: This code assumes that certain fields of a port/socket pair are
103 * constant over its lifetime; such fields can be examined without taking
104 * the socket lock and/or port lock, and do not need to be re-read even
105 * after resuming processing after waiting. These fields include:
107 * - pointer to socket sk structure (aka tipc_sock structure)
108 * - pointer to port structure
114 /* tipc_sk_lock_next: find & lock next socket in registry from given port number
116 static struct tipc_sock *tipc_sk_lock_next(u32 *ref)
118 struct tipc_port *port = (struct tipc_port *)tipc_ref_lock_next(ref);
122 return tipc_port_to_sock(port);
126 * advance_rx_queue - discard first buffer in socket receive queue
128 * Caller must hold socket lock
130 static void advance_rx_queue(struct sock *sk)
132 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
136 * reject_rx_queue - reject all buffers in socket receive queue
138 * Caller must hold socket lock
140 static void reject_rx_queue(struct sock *sk)
145 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
146 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
147 tipc_link_xmit(buf, dnode, 0);
152 * tipc_sk_create - create a TIPC socket
153 * @net: network namespace (must be default network)
154 * @sock: pre-allocated socket structure
155 * @protocol: protocol indicator (must be 0)
156 * @kern: caused by kernel or by userspace?
158 * This routine creates additional data structures used by the TIPC socket,
159 * initializes them, and links them together.
161 * Returns 0 on success, errno otherwise
163 static int tipc_sk_create(struct net *net, struct socket *sock,
164 int protocol, int kern)
166 const struct proto_ops *ops;
169 struct tipc_sock *tsk;
170 struct tipc_port *port;
171 struct tipc_msg *msg;
174 /* Validate arguments */
175 if (unlikely(protocol != 0))
176 return -EPROTONOSUPPORT;
178 switch (sock->type) {
181 state = SS_UNCONNECTED;
185 state = SS_UNCONNECTED;
196 /* Allocate socket's protocol area */
198 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
200 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
207 ref = tipc_ref_acquire(port, &port->lock);
209 pr_warn("Socket create failed; reference table exhausted\n");
212 port->max_pkt = MAX_PKT_DEFAULT;
214 INIT_LIST_HEAD(&port->publications);
217 tipc_msg_init(msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
219 msg_set_origport(msg, ref);
221 /* Finish initializing socket data structures */
224 sock_init_data(sock, sk);
225 k_init_timer(&port->timer, (Handler)tipc_sk_timeout, ref);
226 sk->sk_backlog_rcv = tipc_backlog_rcv;
227 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
228 sk->sk_data_ready = tipc_data_ready;
229 sk->sk_write_space = tipc_write_space;
230 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
231 tsk->sent_unacked = 0;
232 atomic_set(&tsk->dupl_rcvcnt, 0);
233 tipc_port_unlock(port);
235 if (sock->state == SS_READY) {
236 tipc_port_set_unreturnable(port, true);
237 if (sock->type == SOCK_DGRAM)
238 tipc_port_set_unreliable(port, true);
244 * tipc_sock_create_local - create TIPC socket from inside TIPC module
245 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
247 * We cannot use sock_creat_kern here because it bumps module user count.
248 * Since socket owner and creator is the same module we must make sure
249 * that module count remains zero for module local sockets, otherwise
250 * we cannot do rmmod.
252 * Returns 0 on success, errno otherwise
254 int tipc_sock_create_local(int type, struct socket **res)
258 rc = sock_create_lite(AF_TIPC, type, 0, res);
260 pr_err("Failed to create kernel socket\n");
263 tipc_sk_create(&init_net, *res, 0, 1);
269 * tipc_sock_release_local - release socket created by tipc_sock_create_local
270 * @sock: the socket to be released.
272 * Module reference count is not incremented when such sockets are created,
273 * so we must keep it from being decremented when they are released.
275 void tipc_sock_release_local(struct socket *sock)
283 * tipc_sock_accept_local - accept a connection on a socket created
284 * with tipc_sock_create_local. Use this function to avoid that
285 * module reference count is inadvertently incremented.
287 * @sock: the accepting socket
288 * @newsock: reference to the new socket to be created
289 * @flags: socket flags
292 int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
295 struct sock *sk = sock->sk;
298 ret = sock_create_lite(sk->sk_family, sk->sk_type,
299 sk->sk_protocol, newsock);
303 ret = tipc_accept(sock, *newsock, flags);
305 sock_release(*newsock);
308 (*newsock)->ops = sock->ops;
313 * tipc_release - destroy a TIPC socket
314 * @sock: socket to destroy
316 * This routine cleans up any messages that are still queued on the socket.
317 * For DGRAM and RDM socket types, all queued messages are rejected.
318 * For SEQPACKET and STREAM socket types, the first message is rejected
319 * and any others are discarded. (If the first message on a STREAM socket
320 * is partially-read, it is discarded and the next one is rejected instead.)
322 * NOTE: Rejected messages are not necessarily returned to the sender! They
323 * are returned or discarded according to the "destination droppable" setting
324 * specified for the message by the sender.
326 * Returns 0 on success, errno otherwise
328 static int tipc_release(struct socket *sock)
330 struct sock *sk = sock->sk;
331 struct tipc_sock *tsk;
332 struct tipc_port *port;
337 * Exit if socket isn't fully initialized (occurs when a failed accept()
338 * releases a pre-allocated child socket that was never used)
348 * Reject all unreceived messages, except on an active connection
349 * (which disconnects locally & sends a 'FIN+' to peer)
351 dnode = tipc_port_peernode(port);
352 while (sock->state != SS_DISCONNECTING) {
353 buf = __skb_dequeue(&sk->sk_receive_queue);
356 if (TIPC_SKB_CB(buf)->handle != NULL)
359 if ((sock->state == SS_CONNECTING) ||
360 (sock->state == SS_CONNECTED)) {
361 sock->state = SS_DISCONNECTING;
363 tipc_node_remove_conn(dnode, port->ref);
365 if (tipc_msg_reverse(buf, &dnode, TIPC_ERR_NO_PORT))
366 tipc_link_xmit(buf, dnode, 0);
370 tipc_withdraw(port, 0, NULL);
371 spin_lock_bh(port->lock);
372 tipc_ref_discard(port->ref);
373 spin_unlock_bh(port->lock);
374 k_cancel_timer(&port->timer);
375 if (port->connected) {
376 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
377 SHORT_H_SIZE, 0, dnode, tipc_own_addr,
378 tipc_port_peerport(port),
379 port->ref, TIPC_ERR_NO_PORT);
381 tipc_link_xmit(buf, dnode, port->ref);
382 tipc_node_remove_conn(dnode, port->ref);
384 k_term_timer(&port->timer);
386 /* Discard any remaining (connection-based) messages in receive queue */
387 __skb_queue_purge(&sk->sk_receive_queue);
389 /* Reject any messages that accumulated in backlog queue */
390 sock->state = SS_DISCONNECTING;
399 * tipc_bind - associate or disassocate TIPC name(s) with a socket
400 * @sock: socket structure
401 * @uaddr: socket address describing name(s) and desired operation
402 * @uaddr_len: size of socket address data structure
404 * Name and name sequence binding is indicated using a positive scope value;
405 * a negative scope value unbinds the specified name. Specifying no name
406 * (i.e. a socket address length of 0) unbinds all names from the socket.
408 * Returns 0 on success, errno otherwise
410 * NOTE: This routine doesn't need to take the socket lock since it doesn't
411 * access any non-constant socket information.
413 static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
416 struct sock *sk = sock->sk;
417 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
418 struct tipc_sock *tsk = tipc_sk(sk);
422 if (unlikely(!uaddr_len)) {
423 res = tipc_withdraw(&tsk->port, 0, NULL);
427 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
431 if (addr->family != AF_TIPC) {
436 if (addr->addrtype == TIPC_ADDR_NAME)
437 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
438 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
443 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
444 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
445 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
450 res = (addr->scope > 0) ?
451 tipc_publish(&tsk->port, addr->scope, &addr->addr.nameseq) :
452 tipc_withdraw(&tsk->port, -addr->scope, &addr->addr.nameseq);
459 * tipc_getname - get port ID of socket or peer socket
460 * @sock: socket structure
461 * @uaddr: area for returned socket address
462 * @uaddr_len: area for returned length of socket address
463 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
465 * Returns 0 on success, errno otherwise
467 * NOTE: This routine doesn't need to take the socket lock since it only
468 * accesses socket information that is unchanging (or which changes in
469 * a completely predictable manner).
471 static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
472 int *uaddr_len, int peer)
474 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
475 struct tipc_sock *tsk = tipc_sk(sock->sk);
477 memset(addr, 0, sizeof(*addr));
479 if ((sock->state != SS_CONNECTED) &&
480 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
482 addr->addr.id.ref = tipc_port_peerport(&tsk->port);
483 addr->addr.id.node = tipc_port_peernode(&tsk->port);
485 addr->addr.id.ref = tsk->port.ref;
486 addr->addr.id.node = tipc_own_addr;
489 *uaddr_len = sizeof(*addr);
490 addr->addrtype = TIPC_ADDR_ID;
491 addr->family = AF_TIPC;
493 addr->addr.name.domain = 0;
499 * tipc_poll - read and possibly block on pollmask
500 * @file: file structure associated with the socket
501 * @sock: socket for which to calculate the poll bits
504 * Returns pollmask value
507 * It appears that the usual socket locking mechanisms are not useful here
508 * since the pollmask info is potentially out-of-date the moment this routine
509 * exits. TCP and other protocols seem to rely on higher level poll routines
510 * to handle any preventable race conditions, so TIPC will do the same ...
512 * TIPC sets the returned events as follows:
514 * socket state flags set
515 * ------------ ---------
516 * unconnected no read flags
517 * POLLOUT if port is not congested
519 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
522 * connected POLLIN/POLLRDNORM if data in rx queue
523 * POLLOUT if port is not congested
525 * disconnecting POLLIN/POLLRDNORM/POLLHUP
528 * listening POLLIN if SYN in rx queue
531 * ready POLLIN/POLLRDNORM if data in rx queue
532 * [connectionless] POLLOUT (since port cannot be congested)
534 * IMPORTANT: The fact that a read or write operation is indicated does NOT
535 * imply that the operation will succeed, merely that it should be performed
536 * and will not block.
538 static unsigned int tipc_poll(struct file *file, struct socket *sock,
541 struct sock *sk = sock->sk;
542 struct tipc_sock *tsk = tipc_sk(sk);
545 sock_poll_wait(file, sk_sleep(sk), wait);
547 switch ((int)sock->state) {
554 if (!tsk->link_cong && !tipc_sk_conn_cong(tsk))
559 if (!skb_queue_empty(&sk->sk_receive_queue))
560 mask |= (POLLIN | POLLRDNORM);
562 case SS_DISCONNECTING:
563 mask = (POLLIN | POLLRDNORM | POLLHUP);
571 * tipc_sendmcast - send multicast message
572 * @sock: socket structure
573 * @seq: destination address
574 * @iov: message data to send
575 * @dsz: total length of message data
576 * @timeo: timeout to wait for wakeup
578 * Called from function tipc_sendmsg(), which has done all sanity checks
579 * Returns the number of bytes sent on success, or errno
581 static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
582 struct iovec *iov, size_t dsz, long timeo)
584 struct sock *sk = sock->sk;
585 struct tipc_msg *mhdr = &tipc_sk(sk)->port.phdr;
590 msg_set_type(mhdr, TIPC_MCAST_MSG);
591 msg_set_lookup_scope(mhdr, TIPC_CLUSTER_SCOPE);
592 msg_set_destport(mhdr, 0);
593 msg_set_destnode(mhdr, 0);
594 msg_set_nametype(mhdr, seq->type);
595 msg_set_namelower(mhdr, seq->lower);
596 msg_set_nameupper(mhdr, seq->upper);
597 msg_set_hdr_sz(mhdr, MCAST_H_SIZE);
600 mtu = tipc_bclink_get_mtu();
601 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
602 if (unlikely(rc < 0))
606 rc = tipc_bclink_xmit(buf);
607 if (likely(rc >= 0)) {
613 if (rc != -ELINKCONG)
615 tipc_sk(sk)->link_cong = 1;
616 rc = tipc_wait_for_sndmsg(sock, &timeo);
623 /* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
625 void tipc_sk_mcast_rcv(struct sk_buff *buf)
627 struct tipc_msg *msg = buf_msg(buf);
628 struct tipc_port_list dports = {0, NULL, };
629 struct tipc_port_list *item;
631 uint i, last, dst = 0;
632 u32 scope = TIPC_CLUSTER_SCOPE;
634 if (in_own_node(msg_orignode(msg)))
635 scope = TIPC_NODE_SCOPE;
637 /* Create destination port list: */
638 tipc_nametbl_mc_translate(msg_nametype(msg),
649 for (item = &dports; item; item = item->next) {
650 for (i = 0; i < PLSIZE && ++dst <= last; i++) {
651 b = (dst != last) ? skb_clone(buf, GFP_ATOMIC) : buf;
653 pr_warn("Failed do clone mcast rcv buffer\n");
656 msg_set_destport(msg, item->ports[i]);
660 tipc_port_list_free(&dports);
664 * tipc_sk_proto_rcv - receive a connection mng protocol message
665 * @tsk: receiving socket
666 * @dnode: node to send response message to, if any
667 * @buf: buffer containing protocol message
668 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
669 * (CONN_PROBE_REPLY) message should be forwarded.
671 static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
674 struct tipc_msg *msg = buf_msg(buf);
675 struct tipc_port *port = &tsk->port;
678 /* Ignore if connection cannot be validated: */
679 if (!port->connected || !tipc_port_peer_msg(port, msg))
682 port->probing_state = TIPC_CONN_OK;
684 if (msg_type(msg) == CONN_ACK) {
685 conn_cong = tipc_sk_conn_cong(tsk);
686 tsk->sent_unacked -= msg_msgcnt(msg);
688 tsk->sk.sk_write_space(&tsk->sk);
689 } else if (msg_type(msg) == CONN_PROBE) {
690 if (!tipc_msg_reverse(buf, dnode, TIPC_OK))
692 msg_set_type(msg, CONN_PROBE_REPLY);
695 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
702 * dest_name_check - verify user is permitted to send to specified port name
703 * @dest: destination address
704 * @m: descriptor for message to be sent
706 * Prevents restricted configuration commands from being issued by
707 * unauthorized users.
709 * Returns 0 if permission is granted, otherwise errno
711 static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
713 struct tipc_cfg_msg_hdr hdr;
715 if (unlikely(dest->addrtype == TIPC_ADDR_ID))
717 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
719 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
721 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
724 if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr)))
726 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
728 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
734 static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
736 struct sock *sk = sock->sk;
737 struct tipc_sock *tsk = tipc_sk(sk);
742 int err = sock_error(sk);
745 if (sock->state == SS_DISCONNECTING)
749 if (signal_pending(current))
750 return sock_intr_errno(*timeo_p);
752 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
753 done = sk_wait_event(sk, timeo_p, !tsk->link_cong);
754 finish_wait(sk_sleep(sk), &wait);
760 * tipc_sendmsg - send message in connectionless manner
761 * @iocb: if NULL, indicates that socket lock is already held
762 * @sock: socket structure
763 * @m: message to send
764 * @dsz: amount of user data to be sent
766 * Message must have an destination specified explicitly.
767 * Used for SOCK_RDM and SOCK_DGRAM messages,
768 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
769 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
771 * Returns the number of bytes sent on success, or errno otherwise
773 static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
774 struct msghdr *m, size_t dsz)
776 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
777 struct sock *sk = sock->sk;
778 struct tipc_sock *tsk = tipc_sk(sk);
779 struct tipc_port *port = &tsk->port;
780 struct tipc_msg *mhdr = &port->phdr;
781 struct iovec *iov = m->msg_iov;
784 struct tipc_name_seq *seq = &dest->addr.nameseq;
790 return -EDESTADDRREQ;
792 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
793 (dest->family != AF_TIPC)))
796 if (dsz > TIPC_MAX_USER_MSG_SIZE)
802 if (unlikely(sock->state != SS_READY)) {
803 if (sock->state == SS_LISTENING) {
807 if (sock->state != SS_UNCONNECTED) {
811 if (tsk->port.published) {
815 if (dest->addrtype == TIPC_ADDR_NAME) {
816 tsk->port.conn_type = dest->addr.name.name.type;
817 tsk->port.conn_instance = dest->addr.name.name.instance;
820 rc = dest_name_check(dest, m);
824 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
826 if (dest->addrtype == TIPC_ADDR_MCAST) {
827 rc = tipc_sendmcast(sock, seq, iov, dsz, timeo);
829 } else if (dest->addrtype == TIPC_ADDR_NAME) {
830 u32 type = dest->addr.name.name.type;
831 u32 inst = dest->addr.name.name.instance;
832 u32 domain = dest->addr.name.domain;
835 msg_set_type(mhdr, TIPC_NAMED_MSG);
836 msg_set_hdr_sz(mhdr, NAMED_H_SIZE);
837 msg_set_nametype(mhdr, type);
838 msg_set_nameinst(mhdr, inst);
839 msg_set_lookup_scope(mhdr, tipc_addr_scope(domain));
840 dport = tipc_nametbl_translate(type, inst, &dnode);
841 msg_set_destnode(mhdr, dnode);
842 msg_set_destport(mhdr, dport);
843 if (unlikely(!dport && !dnode)) {
847 } else if (dest->addrtype == TIPC_ADDR_ID) {
848 dnode = dest->addr.id.node;
849 msg_set_type(mhdr, TIPC_DIRECT_MSG);
850 msg_set_lookup_scope(mhdr, 0);
851 msg_set_destnode(mhdr, dnode);
852 msg_set_destport(mhdr, dest->addr.id.ref);
853 msg_set_hdr_sz(mhdr, BASIC_H_SIZE);
857 mtu = tipc_node_get_mtu(dnode, tsk->port.ref);
858 rc = tipc_msg_build(mhdr, iov, 0, dsz, mtu, &buf);
863 TIPC_SKB_CB(buf)->wakeup_pending = tsk->link_cong;
864 rc = tipc_link_xmit(buf, dnode, tsk->port.ref);
865 if (likely(rc >= 0)) {
866 if (sock->state != SS_READY)
867 sock->state = SS_CONNECTING;
873 if (rc != -ELINKCONG)
876 rc = tipc_wait_for_sndmsg(sock, &timeo);
887 static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
889 struct sock *sk = sock->sk;
890 struct tipc_sock *tsk = tipc_sk(sk);
895 int err = sock_error(sk);
898 if (sock->state == SS_DISCONNECTING)
900 else if (sock->state != SS_CONNECTED)
904 if (signal_pending(current))
905 return sock_intr_errno(*timeo_p);
907 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
908 done = sk_wait_event(sk, timeo_p,
910 !tipc_sk_conn_cong(tsk)) ||
911 !tsk->port.connected);
912 finish_wait(sk_sleep(sk), &wait);
918 * tipc_send_stream - send stream-oriented data
920 * @sock: socket structure
922 * @dsz: total length of data to be transmitted
924 * Used for SOCK_STREAM data.
926 * Returns the number of bytes sent on success (or partial success),
927 * or errno if no data sent
929 static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
930 struct msghdr *m, size_t dsz)
932 struct sock *sk = sock->sk;
933 struct tipc_sock *tsk = tipc_sk(sk);
934 struct tipc_port *port = &tsk->port;
935 struct tipc_msg *mhdr = &port->phdr;
937 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
942 uint mtu, send, sent = 0;
944 /* Handle implied connection establishment */
945 if (unlikely(dest)) {
946 rc = tipc_sendmsg(iocb, sock, m, dsz);
947 if (dsz && (dsz == rc))
948 tsk->sent_unacked = 1;
951 if (dsz > (uint)INT_MAX)
957 if (unlikely(sock->state != SS_CONNECTED)) {
958 if (sock->state == SS_DISCONNECTING)
965 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
966 dnode = tipc_port_peernode(port);
970 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
971 rc = tipc_msg_build(mhdr, m->msg_iov, sent, send, mtu, &buf);
972 if (unlikely(rc < 0))
975 if (likely(!tipc_sk_conn_cong(tsk))) {
976 rc = tipc_link_xmit(buf, dnode, ref);
984 if (rc == -EMSGSIZE) {
985 port->max_pkt = tipc_node_get_mtu(dnode, ref);
988 if (rc != -ELINKCONG)
992 rc = tipc_wait_for_sndpkt(sock, &timeo);
999 return sent ? sent : rc;
1003 * tipc_send_packet - send a connection-oriented message
1004 * @iocb: if NULL, indicates that socket lock is already held
1005 * @sock: socket structure
1006 * @m: message to send
1007 * @dsz: length of data to be transmitted
1009 * Used for SOCK_SEQPACKET messages.
1011 * Returns the number of bytes sent on success, or errno otherwise
1013 static int tipc_send_packet(struct kiocb *iocb, struct socket *sock,
1014 struct msghdr *m, size_t dsz)
1016 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1019 return tipc_send_stream(iocb, sock, m, dsz);
1022 /* tipc_sk_finish_conn - complete the setup of a connection
1024 static void tipc_sk_finish_conn(struct tipc_port *port, u32 peer_port,
1027 struct tipc_msg *msg = &port->phdr;
1029 msg_set_destnode(msg, peer_node);
1030 msg_set_destport(msg, peer_port);
1031 msg_set_type(msg, TIPC_CONN_MSG);
1032 msg_set_lookup_scope(msg, 0);
1033 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1035 port->probing_interval = CONN_PROBING_INTERVAL;
1036 port->probing_state = TIPC_CONN_OK;
1037 port->connected = 1;
1038 k_start_timer(&port->timer, port->probing_interval);
1039 tipc_node_add_conn(peer_node, port->ref, peer_port);
1040 port->max_pkt = tipc_node_get_mtu(peer_node, port->ref);
1044 * set_orig_addr - capture sender's address for received message
1045 * @m: descriptor for message info
1046 * @msg: received message header
1048 * Note: Address is not captured if not requested by receiver.
1050 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
1052 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
1055 addr->family = AF_TIPC;
1056 addr->addrtype = TIPC_ADDR_ID;
1057 memset(&addr->addr, 0, sizeof(addr->addr));
1058 addr->addr.id.ref = msg_origport(msg);
1059 addr->addr.id.node = msg_orignode(msg);
1060 addr->addr.name.domain = 0; /* could leave uninitialized */
1061 addr->scope = 0; /* could leave uninitialized */
1062 m->msg_namelen = sizeof(struct sockaddr_tipc);
1067 * anc_data_recv - optionally capture ancillary data for received message
1068 * @m: descriptor for message info
1069 * @msg: received message header
1070 * @tport: TIPC port associated with message
1072 * Note: Ancillary data is not captured if not requested by receiver.
1074 * Returns 0 if successful, otherwise errno
1076 static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1077 struct tipc_port *tport)
1085 if (likely(m->msg_controllen == 0))
1088 /* Optionally capture errored message object(s) */
1089 err = msg ? msg_errcode(msg) : 0;
1090 if (unlikely(err)) {
1092 anc_data[1] = msg_data_sz(msg);
1093 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1097 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1104 /* Optionally capture message destination object */
1105 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1106 switch (dest_type) {
1107 case TIPC_NAMED_MSG:
1109 anc_data[0] = msg_nametype(msg);
1110 anc_data[1] = msg_namelower(msg);
1111 anc_data[2] = msg_namelower(msg);
1113 case TIPC_MCAST_MSG:
1115 anc_data[0] = msg_nametype(msg);
1116 anc_data[1] = msg_namelower(msg);
1117 anc_data[2] = msg_nameupper(msg);
1120 has_name = (tport->conn_type != 0);
1121 anc_data[0] = tport->conn_type;
1122 anc_data[1] = tport->conn_instance;
1123 anc_data[2] = tport->conn_instance;
1129 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1137 static void tipc_sk_send_ack(struct tipc_port *port, uint ack)
1139 struct sk_buff *buf = NULL;
1140 struct tipc_msg *msg;
1141 u32 peer_port = tipc_port_peerport(port);
1142 u32 dnode = tipc_port_peernode(port);
1144 if (!port->connected)
1146 buf = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, dnode,
1147 tipc_own_addr, peer_port, port->ref, TIPC_OK);
1151 msg_set_msgcnt(msg, ack);
1152 tipc_link_xmit(buf, dnode, msg_link_selector(msg));
1155 static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1157 struct sock *sk = sock->sk;
1159 long timeo = *timeop;
1163 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1164 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
1165 if (sock->state == SS_DISCONNECTING) {
1170 timeo = schedule_timeout(timeo);
1174 if (!skb_queue_empty(&sk->sk_receive_queue))
1176 err = sock_intr_errno(timeo);
1177 if (signal_pending(current))
1183 finish_wait(sk_sleep(sk), &wait);
1189 * tipc_recvmsg - receive packet-oriented message
1191 * @m: descriptor for message info
1192 * @buf_len: total size of user buffer area
1193 * @flags: receive flags
1195 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1196 * If the complete message doesn't fit in user area, truncate it.
1198 * Returns size of returned message data, errno otherwise
1200 static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock,
1201 struct msghdr *m, size_t buf_len, int flags)
1203 struct sock *sk = sock->sk;
1204 struct tipc_sock *tsk = tipc_sk(sk);
1205 struct tipc_port *port = &tsk->port;
1206 struct sk_buff *buf;
1207 struct tipc_msg *msg;
1213 /* Catch invalid receive requests */
1214 if (unlikely(!buf_len))
1219 if (unlikely(sock->state == SS_UNCONNECTED)) {
1224 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1227 /* Look for a message in receive queue; wait if necessary */
1228 res = tipc_wait_for_rcvmsg(sock, &timeo);
1232 /* Look at first message in receive queue */
1233 buf = skb_peek(&sk->sk_receive_queue);
1235 sz = msg_data_sz(msg);
1236 err = msg_errcode(msg);
1238 /* Discard an empty non-errored message & try again */
1239 if ((!sz) && (!err)) {
1240 advance_rx_queue(sk);
1244 /* Capture sender's address (optional) */
1245 set_orig_addr(m, msg);
1247 /* Capture ancillary data (optional) */
1248 res = anc_data_recv(m, msg, port);
1252 /* Capture message data (if valid) & compute return value (always) */
1254 if (unlikely(buf_len < sz)) {
1256 m->msg_flags |= MSG_TRUNC;
1258 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg),
1264 if ((sock->state == SS_READY) ||
1265 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
1271 /* Consume received message (optional) */
1272 if (likely(!(flags & MSG_PEEK))) {
1273 if ((sock->state != SS_READY) &&
1274 (++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1275 tipc_sk_send_ack(port, tsk->rcv_unacked);
1276 tsk->rcv_unacked = 0;
1278 advance_rx_queue(sk);
1286 * tipc_recv_stream - receive stream-oriented data
1288 * @m: descriptor for message info
1289 * @buf_len: total size of user buffer area
1290 * @flags: receive flags
1292 * Used for SOCK_STREAM messages only. If not enough data is available
1293 * will optionally wait for more; never truncates data.
1295 * Returns size of returned message data, errno otherwise
1297 static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock,
1298 struct msghdr *m, size_t buf_len, int flags)
1300 struct sock *sk = sock->sk;
1301 struct tipc_sock *tsk = tipc_sk(sk);
1302 struct tipc_port *port = &tsk->port;
1303 struct sk_buff *buf;
1304 struct tipc_msg *msg;
1307 int sz_to_copy, target, needed;
1312 /* Catch invalid receive attempts */
1313 if (unlikely(!buf_len))
1318 if (unlikely(sock->state == SS_UNCONNECTED)) {
1323 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
1324 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1327 /* Look for a message in receive queue; wait if necessary */
1328 res = tipc_wait_for_rcvmsg(sock, &timeo);
1332 /* Look at first message in receive queue */
1333 buf = skb_peek(&sk->sk_receive_queue);
1335 sz = msg_data_sz(msg);
1336 err = msg_errcode(msg);
1338 /* Discard an empty non-errored message & try again */
1339 if ((!sz) && (!err)) {
1340 advance_rx_queue(sk);
1344 /* Optionally capture sender's address & ancillary data of first msg */
1345 if (sz_copied == 0) {
1346 set_orig_addr(m, msg);
1347 res = anc_data_recv(m, msg, port);
1352 /* Capture message data (if valid) & compute return value (always) */
1354 u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle);
1357 needed = (buf_len - sz_copied);
1358 sz_to_copy = (sz <= needed) ? sz : needed;
1360 res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset,
1361 m->msg_iov, sz_to_copy);
1365 sz_copied += sz_to_copy;
1367 if (sz_to_copy < sz) {
1368 if (!(flags & MSG_PEEK))
1369 TIPC_SKB_CB(buf)->handle =
1370 (void *)(unsigned long)(offset + sz_to_copy);
1375 goto exit; /* can't add error msg to valid data */
1377 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1383 /* Consume received message (optional) */
1384 if (likely(!(flags & MSG_PEEK))) {
1385 if (unlikely(++tsk->rcv_unacked >= TIPC_CONNACK_INTV)) {
1386 tipc_sk_send_ack(port, tsk->rcv_unacked);
1387 tsk->rcv_unacked = 0;
1389 advance_rx_queue(sk);
1392 /* Loop around if more data is required */
1393 if ((sz_copied < buf_len) && /* didn't get all requested data */
1394 (!skb_queue_empty(&sk->sk_receive_queue) ||
1395 (sz_copied < target)) && /* and more is ready or required */
1396 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1397 (!err)) /* and haven't reached a FIN */
1402 return sz_copied ? sz_copied : res;
1406 * tipc_write_space - wake up thread if port congestion is released
1409 static void tipc_write_space(struct sock *sk)
1411 struct socket_wq *wq;
1414 wq = rcu_dereference(sk->sk_wq);
1415 if (wq_has_sleeper(wq))
1416 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1417 POLLWRNORM | POLLWRBAND);
1422 * tipc_data_ready - wake up threads to indicate messages have been received
1424 * @len: the length of messages
1426 static void tipc_data_ready(struct sock *sk)
1428 struct socket_wq *wq;
1431 wq = rcu_dereference(sk->sk_wq);
1432 if (wq_has_sleeper(wq))
1433 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1434 POLLRDNORM | POLLRDBAND);
1439 * filter_connect - Handle all incoming messages for a connection-based socket
1443 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
1445 static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
1447 struct sock *sk = &tsk->sk;
1448 struct tipc_port *port = &tsk->port;
1449 struct socket *sock = sk->sk_socket;
1450 struct tipc_msg *msg = buf_msg(*buf);
1452 int retval = -TIPC_ERR_NO_PORT;
1457 switch ((int)sock->state) {
1459 /* Accept only connection-based messages sent by peer */
1460 if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
1461 if (unlikely(msg_errcode(msg))) {
1462 sock->state = SS_DISCONNECTING;
1463 port->connected = 0;
1464 /* let timer expire on it's own */
1465 tipc_node_remove_conn(tipc_port_peernode(port),
1472 /* Accept only ACK or NACK message */
1474 if (unlikely(!msg_connected(msg)))
1477 if (unlikely(msg_errcode(msg))) {
1478 sock->state = SS_DISCONNECTING;
1479 sk->sk_err = ECONNREFUSED;
1484 if (unlikely(msg_importance(msg) > TIPC_CRITICAL_IMPORTANCE)) {
1485 sock->state = SS_DISCONNECTING;
1486 sk->sk_err = EINVAL;
1491 tipc_sk_finish_conn(port, msg_origport(msg), msg_orignode(msg));
1492 msg_set_importance(&port->phdr, msg_importance(msg));
1493 sock->state = SS_CONNECTED;
1495 /* If an incoming message is an 'ACK-', it should be
1496 * discarded here because it doesn't contain useful
1497 * data. In addition, we should try to wake up
1498 * connect() routine if sleeping.
1500 if (msg_data_sz(msg) == 0) {
1503 if (waitqueue_active(sk_sleep(sk)))
1504 wake_up_interruptible(sk_sleep(sk));
1509 case SS_UNCONNECTED:
1510 /* Accept only SYN message */
1511 if (!msg_connected(msg) && !(msg_errcode(msg)))
1514 case SS_DISCONNECTING:
1517 pr_err("Unknown socket state %u\n", sock->state);
1523 * rcvbuf_limit - get proper overload limit of socket receive queue
1527 * For all connection oriented messages, irrespective of importance,
1528 * the default overload value (i.e. 67MB) is set as limit.
1530 * For all connectionless messages, by default new queue limits are
1533 * TIPC_LOW_IMPORTANCE (4 MB)
1534 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1535 * TIPC_HIGH_IMPORTANCE (16 MB)
1536 * TIPC_CRITICAL_IMPORTANCE (32 MB)
1538 * Returns overload limit according to corresponding message importance
1540 static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
1542 struct tipc_msg *msg = buf_msg(buf);
1544 if (msg_connected(msg))
1545 return sysctl_tipc_rmem[2];
1547 return sk->sk_rcvbuf >> TIPC_CRITICAL_IMPORTANCE <<
1548 msg_importance(msg);
1552 * filter_rcv - validate incoming message
1556 * Enqueues message on receive queue if acceptable; optionally handles
1557 * disconnect indication for a connected socket.
1559 * Called with socket lock already taken; port lock may also be taken.
1561 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
1562 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
1564 static int filter_rcv(struct sock *sk, struct sk_buff *buf)
1566 struct socket *sock = sk->sk_socket;
1567 struct tipc_sock *tsk = tipc_sk(sk);
1568 struct tipc_msg *msg = buf_msg(buf);
1569 unsigned int limit = rcvbuf_limit(sk, buf);
1573 if (unlikely(msg_user(msg) == CONN_MANAGER))
1574 return tipc_sk_proto_rcv(tsk, &onode, buf);
1576 if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1579 sk->sk_write_space(sk);
1583 /* Reject message if it is wrong sort of message for socket */
1584 if (msg_type(msg) > TIPC_DIRECT_MSG)
1585 return -TIPC_ERR_NO_PORT;
1587 if (sock->state == SS_READY) {
1588 if (msg_connected(msg))
1589 return -TIPC_ERR_NO_PORT;
1591 rc = filter_connect(tsk, &buf);
1592 if (rc != TIPC_OK || buf == NULL)
1596 /* Reject message if there isn't room to queue it */
1597 if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1598 return -TIPC_ERR_OVERLOAD;
1600 /* Enqueue message */
1601 TIPC_SKB_CB(buf)->handle = NULL;
1602 __skb_queue_tail(&sk->sk_receive_queue, buf);
1603 skb_set_owner_r(buf, sk);
1605 sk->sk_data_ready(sk);
1610 * tipc_backlog_rcv - handle incoming message from backlog queue
1614 * Caller must hold socket lock, but not port lock.
1618 static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *buf)
1622 struct tipc_sock *tsk = tipc_sk(sk);
1623 uint truesize = buf->truesize;
1625 rc = filter_rcv(sk, buf);
1628 if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1629 atomic_add(truesize, &tsk->dupl_rcvcnt);
1633 if ((rc < 0) && !tipc_msg_reverse(buf, &onode, -rc))
1636 tipc_link_xmit(buf, onode, 0);
1642 * tipc_sk_rcv - handle incoming message
1643 * @buf: buffer containing arriving message
1645 * Returns 0 if success, or errno: -EHOSTUNREACH
1647 int tipc_sk_rcv(struct sk_buff *buf)
1649 struct tipc_sock *tsk;
1650 struct tipc_port *port;
1652 u32 dport = msg_destport(buf_msg(buf));
1657 /* Validate destination and message */
1658 port = tipc_port_lock(dport);
1659 if (unlikely(!port)) {
1660 rc = tipc_msg_eval(buf, &dnode);
1664 tsk = tipc_port_to_sock(port);
1670 if (!sock_owned_by_user(sk)) {
1671 rc = filter_rcv(sk, buf);
1673 if (sk->sk_backlog.len == 0)
1674 atomic_set(&tsk->dupl_rcvcnt, 0);
1675 limit = rcvbuf_limit(sk, buf) + atomic_read(&tsk->dupl_rcvcnt);
1676 if (sk_add_backlog(sk, buf, limit))
1677 rc = -TIPC_ERR_OVERLOAD;
1680 tipc_port_unlock(port);
1685 if ((rc < 0) && !tipc_msg_reverse(buf, &dnode, -rc))
1686 return -EHOSTUNREACH;
1688 tipc_link_xmit(buf, dnode, 0);
1689 return (rc < 0) ? -EHOSTUNREACH : 0;
1692 static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1694 struct sock *sk = sock->sk;
1699 int err = sock_error(sk);
1704 if (signal_pending(current))
1705 return sock_intr_errno(*timeo_p);
1707 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1708 done = sk_wait_event(sk, timeo_p, sock->state != SS_CONNECTING);
1709 finish_wait(sk_sleep(sk), &wait);
1715 * tipc_connect - establish a connection to another TIPC port
1716 * @sock: socket structure
1717 * @dest: socket address for destination port
1718 * @destlen: size of socket address data structure
1719 * @flags: file-related flags associated with socket
1721 * Returns 0 on success, errno otherwise
1723 static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1724 int destlen, int flags)
1726 struct sock *sk = sock->sk;
1727 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1728 struct msghdr m = {NULL,};
1729 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout;
1730 socket_state previous;
1735 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
1736 if (sock->state == SS_READY) {
1742 * Reject connection attempt using multicast address
1744 * Note: send_msg() validates the rest of the address fields,
1745 * so there's no need to do it here
1747 if (dst->addrtype == TIPC_ADDR_MCAST) {
1752 previous = sock->state;
1753 switch (sock->state) {
1754 case SS_UNCONNECTED:
1755 /* Send a 'SYN-' to destination */
1757 m.msg_namelen = destlen;
1759 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1760 * indicate send_msg() is never blocked.
1763 m.msg_flags = MSG_DONTWAIT;
1765 res = tipc_sendmsg(NULL, sock, &m, 0);
1766 if ((res < 0) && (res != -EWOULDBLOCK))
1769 /* Just entered SS_CONNECTING state; the only
1770 * difference is that return value in non-blocking
1771 * case is EINPROGRESS, rather than EALREADY.
1775 if (previous == SS_CONNECTING)
1779 timeout = msecs_to_jiffies(timeout);
1780 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1781 res = tipc_wait_for_connect(sock, &timeout);
1796 * tipc_listen - allow socket to listen for incoming connections
1797 * @sock: socket structure
1800 * Returns 0 on success, errno otherwise
1802 static int tipc_listen(struct socket *sock, int len)
1804 struct sock *sk = sock->sk;
1809 if (sock->state != SS_UNCONNECTED)
1812 sock->state = SS_LISTENING;
1820 static int tipc_wait_for_accept(struct socket *sock, long timeo)
1822 struct sock *sk = sock->sk;
1826 /* True wake-one mechanism for incoming connections: only
1827 * one process gets woken up, not the 'whole herd'.
1828 * Since we do not 'race & poll' for established sockets
1829 * anymore, the common case will execute the loop only once.
1832 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1833 TASK_INTERRUPTIBLE);
1834 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
1836 timeo = schedule_timeout(timeo);
1840 if (!skb_queue_empty(&sk->sk_receive_queue))
1843 if (sock->state != SS_LISTENING)
1845 err = sock_intr_errno(timeo);
1846 if (signal_pending(current))
1852 finish_wait(sk_sleep(sk), &wait);
1857 * tipc_accept - wait for connection request
1858 * @sock: listening socket
1859 * @newsock: new socket that is to be connected
1860 * @flags: file-related flags associated with socket
1862 * Returns 0 on success, errno otherwise
1864 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
1866 struct sock *new_sk, *sk = sock->sk;
1867 struct sk_buff *buf;
1868 struct tipc_port *new_port;
1869 struct tipc_msg *msg;
1875 if (sock->state != SS_LISTENING) {
1879 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1880 res = tipc_wait_for_accept(sock, timeo);
1884 buf = skb_peek(&sk->sk_receive_queue);
1886 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 1);
1890 new_sk = new_sock->sk;
1891 new_port = &tipc_sk(new_sk)->port;
1894 /* we lock on new_sk; but lockdep sees the lock on sk */
1895 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
1898 * Reject any stray messages received by new socket
1899 * before the socket lock was taken (very, very unlikely)
1901 reject_rx_queue(new_sk);
1903 /* Connect new socket to it's peer */
1904 tipc_sk_finish_conn(new_port, msg_origport(msg), msg_orignode(msg));
1905 new_sock->state = SS_CONNECTED;
1907 tipc_port_set_importance(new_port, msg_importance(msg));
1908 if (msg_named(msg)) {
1909 new_port->conn_type = msg_nametype(msg);
1910 new_port->conn_instance = msg_nameinst(msg);
1914 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1915 * Respond to 'SYN+' by queuing it on new socket.
1917 if (!msg_data_sz(msg)) {
1918 struct msghdr m = {NULL,};
1920 advance_rx_queue(sk);
1921 tipc_send_packet(NULL, new_sock, &m, 0);
1923 __skb_dequeue(&sk->sk_receive_queue);
1924 __skb_queue_head(&new_sk->sk_receive_queue, buf);
1925 skb_set_owner_r(buf, new_sk);
1927 release_sock(new_sk);
1934 * tipc_shutdown - shutdown socket connection
1935 * @sock: socket structure
1936 * @how: direction to close (must be SHUT_RDWR)
1938 * Terminates connection (if necessary), then purges socket's receive queue.
1940 * Returns 0 on success, errno otherwise
1942 static int tipc_shutdown(struct socket *sock, int how)
1944 struct sock *sk = sock->sk;
1945 struct tipc_sock *tsk = tipc_sk(sk);
1946 struct tipc_port *port = &tsk->port;
1947 struct sk_buff *buf;
1951 if (how != SHUT_RDWR)
1956 switch (sock->state) {
1961 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
1962 buf = __skb_dequeue(&sk->sk_receive_queue);
1964 if (TIPC_SKB_CB(buf)->handle != NULL) {
1968 if (tipc_msg_reverse(buf, &dnode, TIPC_CONN_SHUTDOWN))
1969 tipc_link_xmit(buf, dnode, port->ref);
1970 tipc_node_remove_conn(dnode, port->ref);
1972 dnode = tipc_port_peernode(port);
1973 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
1974 TIPC_CONN_MSG, SHORT_H_SIZE,
1975 0, dnode, tipc_own_addr,
1976 tipc_port_peerport(port),
1977 port->ref, TIPC_CONN_SHUTDOWN);
1978 tipc_link_xmit(buf, dnode, port->ref);
1980 port->connected = 0;
1981 sock->state = SS_DISCONNECTING;
1982 tipc_node_remove_conn(dnode, port->ref);
1985 case SS_DISCONNECTING:
1987 /* Discard any unreceived messages */
1988 __skb_queue_purge(&sk->sk_receive_queue);
1990 /* Wake up anyone sleeping in poll */
1991 sk->sk_state_change(sk);
2003 static void tipc_sk_timeout(unsigned long ref)
2005 struct tipc_port *port = tipc_port_lock(ref);
2006 struct tipc_sock *tsk;
2008 struct sk_buff *buf = NULL;
2009 struct tipc_msg *msg = NULL;
2010 u32 peer_port, peer_node;
2015 if (!port->connected) {
2016 tipc_port_unlock(port);
2019 tsk = tipc_port_to_sock(port);
2022 peer_port = tipc_port_peerport(port);
2023 peer_node = tipc_port_peernode(port);
2025 if (port->probing_state == TIPC_CONN_PROBING) {
2026 /* Previous probe not answered -> self abort */
2027 buf = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, TIPC_CONN_MSG,
2028 SHORT_H_SIZE, 0, tipc_own_addr,
2029 peer_node, ref, peer_port,
2032 buf = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE,
2033 0, peer_node, tipc_own_addr,
2034 peer_port, ref, TIPC_OK);
2035 port->probing_state = TIPC_CONN_PROBING;
2036 k_start_timer(&port->timer, port->probing_interval);
2039 tipc_port_unlock(port);
2044 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
2047 static int tipc_sk_show(struct tipc_port *port, char *buf,
2048 int len, int full_id)
2050 struct publication *publ;
2054 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
2055 tipc_zone(tipc_own_addr),
2056 tipc_cluster(tipc_own_addr),
2057 tipc_node(tipc_own_addr), port->ref);
2059 ret = tipc_snprintf(buf, len, "%-10u:", port->ref);
2061 if (port->connected) {
2062 u32 dport = tipc_port_peerport(port);
2063 u32 destnode = tipc_port_peernode(port);
2065 ret += tipc_snprintf(buf + ret, len - ret,
2066 " connected to <%u.%u.%u:%u>",
2067 tipc_zone(destnode),
2068 tipc_cluster(destnode),
2069 tipc_node(destnode), dport);
2070 if (port->conn_type != 0)
2071 ret += tipc_snprintf(buf + ret, len - ret,
2072 " via {%u,%u}", port->conn_type,
2073 port->conn_instance);
2074 } else if (port->published) {
2075 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
2076 list_for_each_entry(publ, &port->publications, pport_list) {
2077 if (publ->lower == publ->upper)
2078 ret += tipc_snprintf(buf + ret, len - ret,
2079 " {%u,%u}", publ->type,
2082 ret += tipc_snprintf(buf + ret, len - ret,
2083 " {%u,%u,%u}", publ->type,
2084 publ->lower, publ->upper);
2087 ret += tipc_snprintf(buf + ret, len - ret, "\n");
2091 struct sk_buff *tipc_sk_socks_show(void)
2093 struct sk_buff *buf;
2094 struct tlv_desc *rep_tlv;
2097 struct tipc_sock *tsk;
2101 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
2104 rep_tlv = (struct tlv_desc *)buf->data;
2105 pb = TLV_DATA(rep_tlv);
2106 pb_len = ULTRA_STRING_MAX_LEN;
2108 tsk = tipc_sk_lock_next(&ref);
2109 for (; tsk; tsk = tipc_sk_lock_next(&ref)) {
2110 bh_lock_sock(&tsk->sk);
2111 str_len += tipc_sk_show(&tsk->port, pb + str_len,
2112 pb_len - str_len, 0);
2113 bh_unlock_sock(&tsk->sk);
2114 tipc_port_unlock(&tsk->port);
2116 str_len += 1; /* for "\0" */
2117 skb_put(buf, TLV_SPACE(str_len));
2118 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
2123 /* tipc_sk_reinit: set non-zero address in all existing sockets
2124 * when we go from standalone to network mode.
2126 void tipc_sk_reinit(void)
2128 struct tipc_msg *msg;
2130 struct tipc_sock *tsk = tipc_sk_lock_next(&ref);
2132 for (; tsk; tsk = tipc_sk_lock_next(&ref)) {
2133 bh_lock_sock(&tsk->sk);
2134 msg = &tsk->port.phdr;
2135 msg_set_prevnode(msg, tipc_own_addr);
2136 msg_set_orignode(msg, tipc_own_addr);
2137 bh_unlock_sock(&tsk->sk);
2138 tipc_port_unlock(&tsk->port);
2143 * tipc_setsockopt - set socket option
2144 * @sock: socket structure
2145 * @lvl: option level
2146 * @opt: option identifier
2147 * @ov: pointer to new option value
2148 * @ol: length of option value
2150 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
2151 * (to ease compatibility).
2153 * Returns 0 on success, errno otherwise
2155 static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2156 char __user *ov, unsigned int ol)
2158 struct sock *sk = sock->sk;
2159 struct tipc_sock *tsk = tipc_sk(sk);
2160 struct tipc_port *port = &tsk->port;
2164 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2166 if (lvl != SOL_TIPC)
2167 return -ENOPROTOOPT;
2168 if (ol < sizeof(value))
2170 res = get_user(value, (u32 __user *)ov);
2177 case TIPC_IMPORTANCE:
2178 res = tipc_port_set_importance(port, value);
2180 case TIPC_SRC_DROPPABLE:
2181 if (sock->type != SOCK_STREAM)
2182 tipc_port_set_unreliable(port, value);
2186 case TIPC_DEST_DROPPABLE:
2187 tipc_port_set_unreturnable(port, value);
2189 case TIPC_CONN_TIMEOUT:
2190 tipc_sk(sk)->conn_timeout = value;
2191 /* no need to set "res", since already 0 at this point */
2203 * tipc_getsockopt - get socket option
2204 * @sock: socket structure
2205 * @lvl: option level
2206 * @opt: option identifier
2207 * @ov: receptacle for option value
2208 * @ol: receptacle for length of option value
2210 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
2211 * (to ease compatibility).
2213 * Returns 0 on success, errno otherwise
2215 static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2216 char __user *ov, int __user *ol)
2218 struct sock *sk = sock->sk;
2219 struct tipc_sock *tsk = tipc_sk(sk);
2220 struct tipc_port *port = &tsk->port;
2225 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2226 return put_user(0, ol);
2227 if (lvl != SOL_TIPC)
2228 return -ENOPROTOOPT;
2229 res = get_user(len, ol);
2236 case TIPC_IMPORTANCE:
2237 value = tipc_port_importance(port);
2239 case TIPC_SRC_DROPPABLE:
2240 value = tipc_port_unreliable(port);
2242 case TIPC_DEST_DROPPABLE:
2243 value = tipc_port_unreturnable(port);
2245 case TIPC_CONN_TIMEOUT:
2246 value = tipc_sk(sk)->conn_timeout;
2247 /* no need to set "res", since already 0 at this point */
2249 case TIPC_NODE_RECVQ_DEPTH:
2250 value = 0; /* was tipc_queue_size, now obsolete */
2252 case TIPC_SOCK_RECVQ_DEPTH:
2253 value = skb_queue_len(&sk->sk_receive_queue);
2262 return res; /* "get" failed */
2264 if (len < sizeof(value))
2267 if (copy_to_user(ov, &value, sizeof(value)))
2270 return put_user(sizeof(value), ol);
2273 static int tipc_ioctl(struct socket *sk, unsigned int cmd, unsigned long arg)
2275 struct tipc_sioc_ln_req lnr;
2276 void __user *argp = (void __user *)arg;
2279 case SIOCGETLINKNAME:
2280 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2282 if (!tipc_node_get_linkname(lnr.bearer_id, lnr.peer,
2283 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2284 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2288 return -EADDRNOTAVAIL;
2290 return -ENOIOCTLCMD;
2294 /* Protocol switches for the various types of TIPC sockets */
2296 static const struct proto_ops msg_ops = {
2297 .owner = THIS_MODULE,
2299 .release = tipc_release,
2301 .connect = tipc_connect,
2302 .socketpair = sock_no_socketpair,
2303 .accept = sock_no_accept,
2304 .getname = tipc_getname,
2306 .ioctl = tipc_ioctl,
2307 .listen = sock_no_listen,
2308 .shutdown = tipc_shutdown,
2309 .setsockopt = tipc_setsockopt,
2310 .getsockopt = tipc_getsockopt,
2311 .sendmsg = tipc_sendmsg,
2312 .recvmsg = tipc_recvmsg,
2313 .mmap = sock_no_mmap,
2314 .sendpage = sock_no_sendpage
2317 static const struct proto_ops packet_ops = {
2318 .owner = THIS_MODULE,
2320 .release = tipc_release,
2322 .connect = tipc_connect,
2323 .socketpair = sock_no_socketpair,
2324 .accept = tipc_accept,
2325 .getname = tipc_getname,
2327 .ioctl = tipc_ioctl,
2328 .listen = tipc_listen,
2329 .shutdown = tipc_shutdown,
2330 .setsockopt = tipc_setsockopt,
2331 .getsockopt = tipc_getsockopt,
2332 .sendmsg = tipc_send_packet,
2333 .recvmsg = tipc_recvmsg,
2334 .mmap = sock_no_mmap,
2335 .sendpage = sock_no_sendpage
2338 static const struct proto_ops stream_ops = {
2339 .owner = THIS_MODULE,
2341 .release = tipc_release,
2343 .connect = tipc_connect,
2344 .socketpair = sock_no_socketpair,
2345 .accept = tipc_accept,
2346 .getname = tipc_getname,
2348 .ioctl = tipc_ioctl,
2349 .listen = tipc_listen,
2350 .shutdown = tipc_shutdown,
2351 .setsockopt = tipc_setsockopt,
2352 .getsockopt = tipc_getsockopt,
2353 .sendmsg = tipc_send_stream,
2354 .recvmsg = tipc_recv_stream,
2355 .mmap = sock_no_mmap,
2356 .sendpage = sock_no_sendpage
2359 static const struct net_proto_family tipc_family_ops = {
2360 .owner = THIS_MODULE,
2362 .create = tipc_sk_create
2365 static struct proto tipc_proto = {
2367 .owner = THIS_MODULE,
2368 .obj_size = sizeof(struct tipc_sock),
2369 .sysctl_rmem = sysctl_tipc_rmem
2372 static struct proto tipc_proto_kern = {
2374 .obj_size = sizeof(struct tipc_sock),
2375 .sysctl_rmem = sysctl_tipc_rmem
2379 * tipc_socket_init - initialize TIPC socket interface
2381 * Returns 0 on success, errno otherwise
2383 int tipc_socket_init(void)
2387 res = proto_register(&tipc_proto, 1);
2389 pr_err("Failed to register TIPC protocol type\n");
2393 res = sock_register(&tipc_family_ops);
2395 pr_err("Failed to register TIPC socket type\n");
2396 proto_unregister(&tipc_proto);
2404 * tipc_socket_stop - stop TIPC socket interface
2406 void tipc_socket_stop(void)
2408 sock_unregister(tipc_family_ops.family);
2409 proto_unregister(&tipc_proto);