2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2001-2007, Ericsson AB
5 * Copyright (c) 2004-2007, 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/module.h>
38 #include <linux/types.h>
39 #include <linux/net.h>
40 #include <linux/socket.h>
41 #include <linux/errno.h>
43 #include <linux/slab.h>
44 #include <linux/poll.h>
45 #include <linux/fcntl.h>
46 #include <linux/mutex.h>
47 #include <asm/string.h>
48 #include <asm/atomic.h>
51 #include <linux/tipc.h>
52 #include <linux/tipc_config.h>
53 #include <net/tipc/tipc_msg.h>
54 #include <net/tipc/tipc_port.h>
58 #define SS_LISTENING -1 /* socket is listening */
59 #define SS_READY -2 /* socket is connectionless */
61 #define OVERLOAD_LIMIT_BASE 5000
62 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
70 #define tipc_sk(sk) ((struct tipc_sock*)sk)
72 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
73 static void wakeupdispatch(struct tipc_port *tport);
75 static const struct proto_ops packet_ops;
76 static const struct proto_ops stream_ops;
77 static const struct proto_ops msg_ops;
79 static struct proto tipc_proto;
81 static int sockets_enabled = 0;
83 static atomic_t tipc_queue_size = ATOMIC_INIT(0);
87 * sock_lock(): Lock a port/socket pair. lock_sock() can
88 * not be used here, since the same lock must protect ports
89 * with non-socket interfaces.
90 * See net.c for description of locking policy.
92 static void sock_lock(struct tipc_sock* tsock)
94 spin_lock_bh(tsock->p->lock);
98 * sock_unlock(): Unlock a port/socket pair
100 static void sock_unlock(struct tipc_sock* tsock)
102 spin_unlock_bh(tsock->p->lock);
106 * advance_queue - discard first buffer in queue
107 * @tsock: TIPC socket
110 static void advance_queue(struct tipc_sock *tsock)
113 buf_discard(skb_dequeue(&tsock->sk.sk_receive_queue));
115 atomic_dec(&tipc_queue_size);
119 * tipc_create - create a TIPC socket
120 * @sock: pre-allocated socket structure
121 * @protocol: protocol indicator (must be 0)
123 * This routine creates and attaches a 'struct sock' to the 'struct socket',
124 * then create and attaches a TIPC port to the 'struct sock' part.
126 * Returns 0 on success, errno otherwise
128 static int tipc_create(struct net *net, struct socket *sock, int protocol)
130 struct tipc_sock *tsock;
131 struct tipc_port *port;
135 if (net != &init_net)
136 return -EAFNOSUPPORT;
138 if (unlikely(protocol != 0))
139 return -EPROTONOSUPPORT;
141 ref = tipc_createport_raw(NULL, &dispatch, &wakeupdispatch, TIPC_LOW_IMPORTANCE);
145 sock->state = SS_UNCONNECTED;
147 switch (sock->type) {
149 sock->ops = &stream_ops;
152 sock->ops = &packet_ops;
155 tipc_set_portunreliable(ref, 1);
158 tipc_set_portunreturnable(ref, 1);
159 sock->ops = &msg_ops;
160 sock->state = SS_READY;
163 tipc_deleteport(ref);
167 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
169 tipc_deleteport(ref);
173 sock_init_data(sock, sk);
174 sk->sk_rcvtimeo = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
177 port = tipc_get_port(ref);
180 port->usr_handle = tsock;
182 mutex_init(&tsock->lock);
184 dbg("sock_create: %x\n",tsock);
186 atomic_inc(&tipc_user_count);
192 * release - destroy a TIPC socket
193 * @sock: socket to destroy
195 * This routine cleans up any messages that are still queued on the socket.
196 * For DGRAM and RDM socket types, all queued messages are rejected.
197 * For SEQPACKET and STREAM socket types, the first message is rejected
198 * and any others are discarded. (If the first message on a STREAM socket
199 * is partially-read, it is discarded and the next one is rejected instead.)
201 * NOTE: Rejected messages are not necessarily returned to the sender! They
202 * are returned or discarded according to the "destination droppable" setting
203 * specified for the message by the sender.
205 * Returns 0 on success, errno otherwise
208 static int release(struct socket *sock)
210 struct tipc_sock *tsock = tipc_sk(sock->sk);
211 struct sock *sk = sock->sk;
215 dbg("sock_delete: %x\n",tsock);
218 mutex_lock(&tsock->lock);
220 mutex_unlock(&tsock->lock);
224 /* Reject unreceived messages, unless no longer connected */
226 while (sock->state != SS_DISCONNECTING) {
228 buf = skb_dequeue(&sk->sk_receive_queue);
230 tsock->p->usr_handle = NULL;
234 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
237 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
238 atomic_dec(&tipc_queue_size);
241 /* Delete TIPC port */
243 res = tipc_deleteport(tsock->p->ref);
246 /* Discard any remaining messages */
248 while ((buf = skb_dequeue(&sk->sk_receive_queue))) {
250 atomic_dec(&tipc_queue_size);
253 mutex_unlock(&tsock->lock);
257 atomic_dec(&tipc_user_count);
262 * bind - associate or disassocate TIPC name(s) with a socket
263 * @sock: socket structure
264 * @uaddr: socket address describing name(s) and desired operation
265 * @uaddr_len: size of socket address data structure
267 * Name and name sequence binding is indicated using a positive scope value;
268 * a negative scope value unbinds the specified name. Specifying no name
269 * (i.e. a socket address length of 0) unbinds all names from the socket.
271 * Returns 0 on success, errno otherwise
274 static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
276 struct tipc_sock *tsock = tipc_sk(sock->sk);
277 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
280 if (mutex_lock_interruptible(&tsock->lock))
283 if (unlikely(!uaddr_len)) {
284 res = tipc_withdraw(tsock->p->ref, 0, NULL);
288 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
293 if (addr->family != AF_TIPC) {
297 if (addr->addrtype == TIPC_ADDR_NAME)
298 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
299 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
305 res = tipc_publish(tsock->p->ref, addr->scope,
306 &addr->addr.nameseq);
308 res = tipc_withdraw(tsock->p->ref, -addr->scope,
309 &addr->addr.nameseq);
311 mutex_unlock(&tsock->lock);
316 * get_name - get port ID of socket or peer socket
317 * @sock: socket structure
318 * @uaddr: area for returned socket address
319 * @uaddr_len: area for returned length of socket address
320 * @peer: 0 to obtain socket name, 1 to obtain peer socket name
322 * Returns 0 on success, errno otherwise
325 static int get_name(struct socket *sock, struct sockaddr *uaddr,
326 int *uaddr_len, int peer)
328 struct tipc_sock *tsock = tipc_sk(sock->sk);
329 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
332 if (mutex_lock_interruptible(&tsock->lock))
335 *uaddr_len = sizeof(*addr);
336 addr->addrtype = TIPC_ADDR_ID;
337 addr->family = AF_TIPC;
340 res = tipc_peer(tsock->p->ref, &addr->addr.id);
342 res = tipc_ownidentity(tsock->p->ref, &addr->addr.id);
343 addr->addr.name.domain = 0;
345 mutex_unlock(&tsock->lock);
350 * poll - read and possibly block on pollmask
351 * @file: file structure associated with the socket
352 * @sock: socket for which to calculate the poll bits
355 * Returns pollmask value
358 * It appears that the usual socket locking mechanisms are not useful here
359 * since the pollmask info is potentially out-of-date the moment this routine
360 * exits. TCP and other protocols seem to rely on higher level poll routines
361 * to handle any preventable race conditions, so TIPC will do the same ...
363 * TIPC sets the returned events as follows:
364 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
365 * or if a connection-oriented socket is does not have an active connection
366 * (i.e. a read operation will not block).
367 * b) POLLOUT is set except when a socket's connection has been terminated
368 * (i.e. a write operation will not block).
369 * c) POLLHUP is set when a socket's connection has been terminated.
371 * IMPORTANT: The fact that a read or write operation will not block does NOT
372 * imply that the operation will succeed!
375 static unsigned int poll(struct file *file, struct socket *sock,
378 struct sock *sk = sock->sk;
381 poll_wait(file, sk->sk_sleep, wait);
383 if (!skb_queue_empty(&sk->sk_receive_queue) ||
384 (sock->state == SS_UNCONNECTED) ||
385 (sock->state == SS_DISCONNECTING))
386 mask = (POLLRDNORM | POLLIN);
390 if (sock->state == SS_DISCONNECTING)
399 * dest_name_check - verify user is permitted to send to specified port name
400 * @dest: destination address
401 * @m: descriptor for message to be sent
403 * Prevents restricted configuration commands from being issued by
404 * unauthorized users.
406 * Returns 0 if permission is granted, otherwise errno
409 static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
411 struct tipc_cfg_msg_hdr hdr;
413 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
415 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
418 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
421 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
423 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
430 * send_msg - send message in connectionless manner
432 * @sock: socket structure
433 * @m: message to send
434 * @total_len: length of message
436 * Message must have an destination specified explicitly.
437 * Used for SOCK_RDM and SOCK_DGRAM messages,
438 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
439 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
441 * Returns the number of bytes sent on success, or errno otherwise
444 static int send_msg(struct kiocb *iocb, struct socket *sock,
445 struct msghdr *m, size_t total_len)
447 struct tipc_sock *tsock = tipc_sk(sock->sk);
448 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
454 return -EDESTADDRREQ;
455 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
456 (dest->family != AF_TIPC)))
459 needs_conn = (sock->state != SS_READY);
460 if (unlikely(needs_conn)) {
461 if (sock->state == SS_LISTENING)
463 if (sock->state != SS_UNCONNECTED)
465 if ((tsock->p->published) ||
466 ((sock->type == SOCK_STREAM) && (total_len != 0)))
468 if (dest->addrtype == TIPC_ADDR_NAME) {
469 tsock->p->conn_type = dest->addr.name.name.type;
470 tsock->p->conn_instance = dest->addr.name.name.instance;
474 if (mutex_lock_interruptible(&tsock->lock))
479 /* Abort any pending connection attempts (very unlikely) */
481 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
482 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
483 atomic_dec(&tipc_queue_size);
486 sock->state = SS_CONNECTING;
490 if (dest->addrtype == TIPC_ADDR_NAME) {
491 if ((res = dest_name_check(dest, m)))
493 res = tipc_send2name(tsock->p->ref,
494 &dest->addr.name.name,
495 dest->addr.name.domain,
499 else if (dest->addrtype == TIPC_ADDR_ID) {
500 res = tipc_send2port(tsock->p->ref,
505 else if (dest->addrtype == TIPC_ADDR_MCAST) {
510 if ((res = dest_name_check(dest, m)))
512 res = tipc_multicast(tsock->p->ref,
518 if (likely(res != -ELINKCONG)) {
520 mutex_unlock(&tsock->lock);
523 if (m->msg_flags & MSG_DONTWAIT) {
527 if (wait_event_interruptible(*sock->sk->sk_sleep,
528 !tsock->p->congested)) {
536 * send_packet - send a connection-oriented message
538 * @sock: socket structure
539 * @m: message to send
540 * @total_len: length of message
542 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
544 * Returns the number of bytes sent on success, or errno otherwise
547 static int send_packet(struct kiocb *iocb, struct socket *sock,
548 struct msghdr *m, size_t total_len)
550 struct tipc_sock *tsock = tipc_sk(sock->sk);
551 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
554 /* Handle implied connection establishment */
557 return send_msg(iocb, sock, m, total_len);
559 if (mutex_lock_interruptible(&tsock->lock)) {
564 if (unlikely(sock->state != SS_CONNECTED)) {
565 if (sock->state == SS_DISCONNECTING)
572 res = tipc_send(tsock->p->ref, m->msg_iovlen, m->msg_iov);
573 if (likely(res != -ELINKCONG)) {
575 mutex_unlock(&tsock->lock);
578 if (m->msg_flags & MSG_DONTWAIT) {
582 if (wait_event_interruptible(*sock->sk->sk_sleep,
583 !tsock->p->congested)) {
591 * send_stream - send stream-oriented data
593 * @sock: socket structure
595 * @total_len: total length of data to be sent
597 * Used for SOCK_STREAM data.
599 * Returns the number of bytes sent on success (or partial success),
600 * or errno if no data sent
604 static int send_stream(struct kiocb *iocb, struct socket *sock,
605 struct msghdr *m, size_t total_len)
607 struct tipc_port *tport;
608 struct msghdr my_msg;
610 struct iovec *curr_iov;
612 char __user *curr_start;
619 /* Handle special cases where there is no connection */
621 if (unlikely(sock->state != SS_CONNECTED)) {
622 if (sock->state == SS_UNCONNECTED)
623 return send_packet(iocb, sock, m, total_len);
624 else if (sock->state == SS_DISCONNECTING)
630 if (unlikely(m->msg_name))
634 * Send each iovec entry using one or more messages
636 * Note: This algorithm is good for the most likely case
637 * (i.e. one large iovec entry), but could be improved to pass sets
638 * of small iovec entries into send_packet().
641 curr_iov = m->msg_iov;
642 curr_iovlen = m->msg_iovlen;
643 my_msg.msg_iov = &my_iov;
644 my_msg.msg_iovlen = 1;
645 my_msg.msg_flags = m->msg_flags;
646 my_msg.msg_name = NULL;
649 tport = tipc_sk(sock->sk)->p;
650 hdr_size = msg_hdr_sz(&tport->phdr);
652 while (curr_iovlen--) {
653 curr_start = curr_iov->iov_base;
654 curr_left = curr_iov->iov_len;
657 bytes_to_send = tport->max_pkt - hdr_size;
658 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
659 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
660 if (curr_left < bytes_to_send)
661 bytes_to_send = curr_left;
662 my_iov.iov_base = curr_start;
663 my_iov.iov_len = bytes_to_send;
664 if ((res = send_packet(iocb, sock, &my_msg, 0)) < 0) {
669 curr_left -= bytes_to_send;
670 curr_start += bytes_to_send;
671 bytes_sent += bytes_to_send;
681 * auto_connect - complete connection setup to a remote port
682 * @sock: socket structure
683 * @tsock: TIPC-specific socket structure
684 * @msg: peer's response message
686 * Returns 0 on success, errno otherwise
689 static int auto_connect(struct socket *sock, struct tipc_sock *tsock,
690 struct tipc_msg *msg)
692 struct tipc_portid peer;
694 if (msg_errcode(msg)) {
695 sock->state = SS_DISCONNECTING;
696 return -ECONNREFUSED;
699 peer.ref = msg_origport(msg);
700 peer.node = msg_orignode(msg);
701 tipc_connect2port(tsock->p->ref, &peer);
702 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
703 sock->state = SS_CONNECTED;
708 * set_orig_addr - capture sender's address for received message
709 * @m: descriptor for message info
710 * @msg: received message header
712 * Note: Address is not captured if not requested by receiver.
715 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
717 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
720 addr->family = AF_TIPC;
721 addr->addrtype = TIPC_ADDR_ID;
722 addr->addr.id.ref = msg_origport(msg);
723 addr->addr.id.node = msg_orignode(msg);
724 addr->addr.name.domain = 0; /* could leave uninitialized */
725 addr->scope = 0; /* could leave uninitialized */
726 m->msg_namelen = sizeof(struct sockaddr_tipc);
731 * anc_data_recv - optionally capture ancillary data for received message
732 * @m: descriptor for message info
733 * @msg: received message header
734 * @tport: TIPC port associated with message
736 * Note: Ancillary data is not captured if not requested by receiver.
738 * Returns 0 if successful, otherwise errno
741 static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
742 struct tipc_port *tport)
750 if (likely(m->msg_controllen == 0))
753 /* Optionally capture errored message object(s) */
755 err = msg ? msg_errcode(msg) : 0;
758 anc_data[1] = msg_data_sz(msg);
759 if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data)))
762 (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
767 /* Optionally capture message destination object */
769 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
773 anc_data[0] = msg_nametype(msg);
774 anc_data[1] = msg_namelower(msg);
775 anc_data[2] = msg_namelower(msg);
779 anc_data[0] = msg_nametype(msg);
780 anc_data[1] = msg_namelower(msg);
781 anc_data[2] = msg_nameupper(msg);
784 has_name = (tport->conn_type != 0);
785 anc_data[0] = tport->conn_type;
786 anc_data[1] = tport->conn_instance;
787 anc_data[2] = tport->conn_instance;
793 (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data)))
800 * recv_msg - receive packet-oriented message
802 * @m: descriptor for message info
803 * @buf_len: total size of user buffer area
804 * @flags: receive flags
806 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
807 * If the complete message doesn't fit in user area, truncate it.
809 * Returns size of returned message data, errno otherwise
812 static int recv_msg(struct kiocb *iocb, struct socket *sock,
813 struct msghdr *m, size_t buf_len, int flags)
815 struct tipc_sock *tsock = tipc_sk(sock->sk);
817 struct tipc_msg *msg;
823 /* Currently doesn't support receiving into multiple iovec entries */
825 if (m->msg_iovlen != 1)
828 /* Catch invalid receive attempts */
830 if (unlikely(!buf_len))
833 if (sock->type == SOCK_SEQPACKET) {
834 if (unlikely(sock->state == SS_UNCONNECTED))
836 if (unlikely((sock->state == SS_DISCONNECTING) &&
837 (skb_queue_len(&sock->sk->sk_receive_queue) == 0)))
841 /* Look for a message in receive queue; wait if necessary */
843 if (unlikely(mutex_lock_interruptible(&tsock->lock)))
847 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
848 (flags & MSG_DONTWAIT))) {
853 if ((res = wait_event_interruptible(
855 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
856 (sock->state == SS_DISCONNECTING))) )) {
860 /* Catch attempt to receive on an already terminated connection */
861 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
868 /* Get access to first message in receive queue */
870 buf = skb_peek(&sock->sk->sk_receive_queue);
872 sz = msg_data_sz(msg);
873 err = msg_errcode(msg);
875 /* Complete connection setup for an implied connect */
877 if (unlikely(sock->state == SS_CONNECTING)) {
878 if ((res = auto_connect(sock, tsock, msg)))
882 /* Discard an empty non-errored message & try again */
884 if ((!sz) && (!err)) {
885 advance_queue(tsock);
889 /* Capture sender's address (optional) */
891 set_orig_addr(m, msg);
893 /* Capture ancillary data (optional) */
895 if ((res = anc_data_recv(m, msg, tsock->p)))
898 /* Capture message data (if valid) & compute return value (always) */
901 if (unlikely(buf_len < sz)) {
903 m->msg_flags |= MSG_TRUNC;
905 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
912 if ((sock->state == SS_READY) ||
913 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
919 /* Consume received message (optional) */
921 if (likely(!(flags & MSG_PEEK))) {
922 if ((sock->state != SS_READY) &&
923 (++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
924 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
925 advance_queue(tsock);
928 mutex_unlock(&tsock->lock);
933 * recv_stream - receive stream-oriented data
935 * @m: descriptor for message info
936 * @buf_len: total size of user buffer area
937 * @flags: receive flags
939 * Used for SOCK_STREAM messages only. If not enough data is available
940 * will optionally wait for more; never truncates data.
942 * Returns size of returned message data, errno otherwise
945 static int recv_stream(struct kiocb *iocb, struct socket *sock,
946 struct msghdr *m, size_t buf_len, int flags)
948 struct tipc_sock *tsock = tipc_sk(sock->sk);
950 struct tipc_msg *msg;
956 char __user *crs = m->msg_iov->iov_base;
957 unsigned char *buf_crs;
961 /* Currently doesn't support receiving into multiple iovec entries */
963 if (m->msg_iovlen != 1)
966 /* Catch invalid receive attempts */
968 if (unlikely(!buf_len))
971 if (unlikely(sock->state == SS_DISCONNECTING)) {
972 if (skb_queue_len(&sock->sk->sk_receive_queue) == 0)
974 } else if (unlikely(sock->state != SS_CONNECTED))
977 /* Look for a message in receive queue; wait if necessary */
979 if (unlikely(mutex_lock_interruptible(&tsock->lock)))
983 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
984 (flags & MSG_DONTWAIT))) {
989 if ((res = wait_event_interruptible(
991 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
992 (sock->state == SS_DISCONNECTING))) )) {
996 /* Catch attempt to receive on an already terminated connection */
997 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
1004 /* Get access to first message in receive queue */
1006 buf = skb_peek(&sock->sk->sk_receive_queue);
1008 sz = msg_data_sz(msg);
1009 err = msg_errcode(msg);
1011 /* Discard an empty non-errored message & try again */
1013 if ((!sz) && (!err)) {
1014 advance_queue(tsock);
1018 /* Optionally capture sender's address & ancillary data of first msg */
1020 if (sz_copied == 0) {
1021 set_orig_addr(m, msg);
1022 if ((res = anc_data_recv(m, msg, tsock->p)))
1026 /* Capture message data (if valid) & compute return value (always) */
1029 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
1030 sz = (unsigned char *)msg + msg_size(msg) - buf_crs;
1032 needed = (buf_len - sz_copied);
1033 sz_to_copy = (sz <= needed) ? sz : needed;
1034 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1038 sz_copied += sz_to_copy;
1040 if (sz_to_copy < sz) {
1041 if (!(flags & MSG_PEEK))
1042 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1049 goto exit; /* can't add error msg to valid data */
1051 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1057 /* Consume received message (optional) */
1059 if (likely(!(flags & MSG_PEEK))) {
1060 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1061 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
1062 advance_queue(tsock);
1065 /* Loop around if more data is required */
1067 if ((sz_copied < buf_len) /* didn't get all requested data */
1068 && (!skb_queue_empty(&sock->sk->sk_receive_queue) ||
1069 (flags & MSG_WAITALL))
1070 /* ... and more is ready or required */
1071 && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
1072 && (!err) /* ... and haven't reached a FIN */
1077 mutex_unlock(&tsock->lock);
1078 return sz_copied ? sz_copied : res;
1082 * rx_queue_full - determine if receive queue can accept another message
1083 * @msg: message to be added to queue
1084 * @queue_size: current size of queue
1085 * @base: nominal maximum size of queue
1087 * Returns 1 if queue is unable to accept message, 0 otherwise
1090 static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
1093 u32 imp = msg_importance(msg);
1095 if (imp == TIPC_LOW_IMPORTANCE)
1097 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1098 threshold = base * 2;
1099 else if (imp == TIPC_HIGH_IMPORTANCE)
1100 threshold = base * 100;
1104 if (msg_connected(msg))
1107 return (queue_size >= threshold);
1111 * async_disconnect - wrapper function used to disconnect port
1112 * @portref: TIPC port reference (passed as pointer-sized value)
1115 static void async_disconnect(unsigned long portref)
1117 tipc_disconnect((u32)portref);
1121 * dispatch - handle arriving message
1122 * @tport: TIPC port that received message
1125 * Called with port locked. Must not take socket lock to avoid deadlock risk.
1127 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1130 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1132 struct tipc_msg *msg = buf_msg(buf);
1133 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1134 struct socket *sock;
1137 /* Reject message if socket is closing */
1140 return TIPC_ERR_NO_PORT;
1142 /* Reject message if it is wrong sort of message for socket */
1145 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1146 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1147 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1149 sock = tsock->sk.sk_socket;
1150 if (sock->state == SS_READY) {
1151 if (msg_connected(msg)) {
1152 msg_dbg(msg, "dispatch filter 1\n");
1153 return TIPC_ERR_NO_PORT;
1156 if (msg_mcast(msg)) {
1157 msg_dbg(msg, "dispatch filter 2\n");
1158 return TIPC_ERR_NO_PORT;
1160 if (sock->state == SS_CONNECTED) {
1161 if (!msg_connected(msg)) {
1162 msg_dbg(msg, "dispatch filter 3\n");
1163 return TIPC_ERR_NO_PORT;
1166 else if (sock->state == SS_CONNECTING) {
1167 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1168 msg_dbg(msg, "dispatch filter 4\n");
1169 return TIPC_ERR_NO_PORT;
1172 else if (sock->state == SS_LISTENING) {
1173 if (msg_connected(msg) || msg_errcode(msg)) {
1174 msg_dbg(msg, "dispatch filter 5\n");
1175 return TIPC_ERR_NO_PORT;
1178 else if (sock->state == SS_DISCONNECTING) {
1179 msg_dbg(msg, "dispatch filter 6\n");
1180 return TIPC_ERR_NO_PORT;
1182 else /* (sock->state == SS_UNCONNECTED) */ {
1183 if (msg_connected(msg) || msg_errcode(msg)) {
1184 msg_dbg(msg, "dispatch filter 7\n");
1185 return TIPC_ERR_NO_PORT;
1190 /* Reject message if there isn't room to queue it */
1192 recv_q_len = (u32)atomic_read(&tipc_queue_size);
1193 if (unlikely(recv_q_len >= OVERLOAD_LIMIT_BASE)) {
1194 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE))
1195 return TIPC_ERR_OVERLOAD;
1197 recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue);
1198 if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
1199 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
1200 return TIPC_ERR_OVERLOAD;
1203 /* Initiate connection termination for an incoming 'FIN' */
1205 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1206 sock->state = SS_DISCONNECTING;
1207 /* Note: Use signal since port lock is already taken! */
1208 tipc_k_signal((Handler)async_disconnect, tport->ref);
1211 /* Enqueue message (finally!) */
1213 msg_dbg(msg,"<DISP<: ");
1214 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1215 atomic_inc(&tipc_queue_size);
1216 skb_queue_tail(&sock->sk->sk_receive_queue, buf);
1218 if (waitqueue_active(sock->sk->sk_sleep))
1219 wake_up_interruptible(sock->sk->sk_sleep);
1224 * wakeupdispatch - wake up port after congestion
1225 * @tport: port to wakeup
1227 * Called with port lock on.
1230 static void wakeupdispatch(struct tipc_port *tport)
1232 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1234 if (waitqueue_active(tsock->sk.sk_sleep))
1235 wake_up_interruptible(tsock->sk.sk_sleep);
1239 * connect - establish a connection to another TIPC port
1240 * @sock: socket structure
1241 * @dest: socket address for destination port
1242 * @destlen: size of socket address data structure
1245 * Returns 0 on success, errno otherwise
1248 static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
1251 struct tipc_sock *tsock = tipc_sk(sock->sk);
1252 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1253 struct msghdr m = {NULL,};
1254 struct sk_buff *buf;
1255 struct tipc_msg *msg;
1258 /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */
1260 if (sock->state == SS_READY)
1263 /* Issue Posix-compliant error code if socket is in the wrong state */
1265 if (sock->state == SS_LISTENING)
1267 if (sock->state == SS_CONNECTING)
1269 if (sock->state != SS_UNCONNECTED)
1273 * Reject connection attempt using multicast address
1275 * Note: send_msg() validates the rest of the address fields,
1276 * so there's no need to do it here
1279 if (dst->addrtype == TIPC_ADDR_MCAST)
1282 /* Send a 'SYN-' to destination */
1285 m.msg_namelen = destlen;
1286 if ((res = send_msg(NULL, sock, &m, 0)) < 0) {
1287 sock->state = SS_DISCONNECTING;
1291 if (mutex_lock_interruptible(&tsock->lock))
1292 return -ERESTARTSYS;
1294 /* Wait for destination's 'ACK' response */
1296 res = wait_event_interruptible_timeout(*sock->sk->sk_sleep,
1297 skb_queue_len(&sock->sk->sk_receive_queue),
1298 sock->sk->sk_rcvtimeo);
1299 buf = skb_peek(&sock->sk->sk_receive_queue);
1302 res = auto_connect(sock, tsock, msg);
1304 if (!msg_data_sz(msg))
1305 advance_queue(tsock);
1311 { /* leave "res" unchanged */ }
1312 sock->state = SS_DISCONNECTING;
1315 mutex_unlock(&tsock->lock);
1320 * listen - allow socket to listen for incoming connections
1321 * @sock: socket structure
1324 * Returns 0 on success, errno otherwise
1327 static int listen(struct socket *sock, int len)
1329 /* REQUIRES SOCKET LOCKING OF SOME SORT? */
1331 if (sock->state == SS_READY)
1333 if (sock->state != SS_UNCONNECTED)
1335 sock->state = SS_LISTENING;
1340 * accept - wait for connection request
1341 * @sock: listening socket
1342 * @newsock: new socket that is to be connected
1343 * @flags: file-related flags associated with socket
1345 * Returns 0 on success, errno otherwise
1348 static int accept(struct socket *sock, struct socket *newsock, int flags)
1350 struct tipc_sock *tsock = tipc_sk(sock->sk);
1351 struct sk_buff *buf;
1354 if (sock->state == SS_READY)
1356 if (sock->state != SS_LISTENING)
1359 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
1360 (flags & O_NONBLOCK)))
1361 return -EWOULDBLOCK;
1363 if (mutex_lock_interruptible(&tsock->lock))
1364 return -ERESTARTSYS;
1366 if (wait_event_interruptible(*sock->sk->sk_sleep,
1367 skb_queue_len(&sock->sk->sk_receive_queue))) {
1371 buf = skb_peek(&sock->sk->sk_receive_queue);
1373 res = tipc_create(sock_net(sock->sk), newsock, 0);
1375 struct tipc_sock *new_tsock = tipc_sk(newsock->sk);
1376 struct tipc_portid id;
1377 struct tipc_msg *msg = buf_msg(buf);
1378 u32 new_ref = new_tsock->p->ref;
1380 id.ref = msg_origport(msg);
1381 id.node = msg_orignode(msg);
1382 tipc_connect2port(new_ref, &id);
1383 newsock->state = SS_CONNECTED;
1385 tipc_set_portimportance(new_ref, msg_importance(msg));
1386 if (msg_named(msg)) {
1387 new_tsock->p->conn_type = msg_nametype(msg);
1388 new_tsock->p->conn_instance = msg_nameinst(msg);
1392 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1393 * Respond to 'SYN+' by queuing it on new socket.
1396 msg_dbg(msg,"<ACC<: ");
1397 if (!msg_data_sz(msg)) {
1398 struct msghdr m = {NULL,};
1400 send_packet(NULL, newsock, &m, 0);
1401 advance_queue(tsock);
1404 skb_dequeue(&sock->sk->sk_receive_queue);
1406 skb_queue_head(&newsock->sk->sk_receive_queue, buf);
1410 mutex_unlock(&tsock->lock);
1415 * shutdown - shutdown socket connection
1416 * @sock: socket structure
1417 * @how: direction to close (must be SHUT_RDWR)
1419 * Terminates connection (if necessary), then purges socket's receive queue.
1421 * Returns 0 on success, errno otherwise
1424 static int shutdown(struct socket *sock, int how)
1426 struct tipc_sock* tsock = tipc_sk(sock->sk);
1427 struct sk_buff *buf;
1430 if (how != SHUT_RDWR)
1433 if (mutex_lock_interruptible(&tsock->lock))
1434 return -ERESTARTSYS;
1438 switch (sock->state) {
1441 /* Send 'FIN+' or 'FIN-' message to peer */
1445 if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1446 atomic_dec(&tipc_queue_size);
1447 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1451 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
1454 tipc_shutdown(tsock->p->ref);
1460 case SS_DISCONNECTING:
1462 /* Discard any unreceived messages */
1464 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1465 atomic_dec(&tipc_queue_size);
1468 tsock->p->conn_unacked = 0;
1473 sock->state = SS_DISCONNECTING;
1483 mutex_unlock(&tsock->lock);
1488 * setsockopt - set socket option
1489 * @sock: socket structure
1490 * @lvl: option level
1491 * @opt: option identifier
1492 * @ov: pointer to new option value
1493 * @ol: length of option value
1495 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1496 * (to ease compatibility).
1498 * Returns 0 on success, errno otherwise
1501 static int setsockopt(struct socket *sock,
1502 int lvl, int opt, char __user *ov, int ol)
1504 struct tipc_sock *tsock = tipc_sk(sock->sk);
1508 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1510 if (lvl != SOL_TIPC)
1511 return -ENOPROTOOPT;
1512 if (ol < sizeof(value))
1514 if ((res = get_user(value, (u32 __user *)ov)))
1517 if (mutex_lock_interruptible(&tsock->lock))
1518 return -ERESTARTSYS;
1521 case TIPC_IMPORTANCE:
1522 res = tipc_set_portimportance(tsock->p->ref, value);
1524 case TIPC_SRC_DROPPABLE:
1525 if (sock->type != SOCK_STREAM)
1526 res = tipc_set_portunreliable(tsock->p->ref, value);
1530 case TIPC_DEST_DROPPABLE:
1531 res = tipc_set_portunreturnable(tsock->p->ref, value);
1533 case TIPC_CONN_TIMEOUT:
1534 sock->sk->sk_rcvtimeo = msecs_to_jiffies(value);
1540 mutex_unlock(&tsock->lock);
1545 * getsockopt - get socket option
1546 * @sock: socket structure
1547 * @lvl: option level
1548 * @opt: option identifier
1549 * @ov: receptacle for option value
1550 * @ol: receptacle for length of option value
1552 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1553 * (to ease compatibility).
1555 * Returns 0 on success, errno otherwise
1558 static int getsockopt(struct socket *sock,
1559 int lvl, int opt, char __user *ov, int __user *ol)
1561 struct tipc_sock *tsock = tipc_sk(sock->sk);
1566 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1567 return put_user(0, ol);
1568 if (lvl != SOL_TIPC)
1569 return -ENOPROTOOPT;
1570 if ((res = get_user(len, ol)))
1573 if (mutex_lock_interruptible(&tsock->lock))
1574 return -ERESTARTSYS;
1577 case TIPC_IMPORTANCE:
1578 res = tipc_portimportance(tsock->p->ref, &value);
1580 case TIPC_SRC_DROPPABLE:
1581 res = tipc_portunreliable(tsock->p->ref, &value);
1583 case TIPC_DEST_DROPPABLE:
1584 res = tipc_portunreturnable(tsock->p->ref, &value);
1586 case TIPC_CONN_TIMEOUT:
1587 value = jiffies_to_msecs(sock->sk->sk_rcvtimeo);
1596 else if (len < sizeof(value)) {
1599 else if ((res = copy_to_user(ov, &value, sizeof(value)))) {
1600 /* couldn't return value */
1603 res = put_user(sizeof(value), ol);
1606 mutex_unlock(&tsock->lock);
1611 * Protocol switches for the various types of TIPC sockets
1614 static const struct proto_ops msg_ops = {
1615 .owner = THIS_MODULE,
1620 .socketpair = sock_no_socketpair,
1622 .getname = get_name,
1624 .ioctl = sock_no_ioctl,
1626 .shutdown = shutdown,
1627 .setsockopt = setsockopt,
1628 .getsockopt = getsockopt,
1629 .sendmsg = send_msg,
1630 .recvmsg = recv_msg,
1631 .mmap = sock_no_mmap,
1632 .sendpage = sock_no_sendpage
1635 static const struct proto_ops packet_ops = {
1636 .owner = THIS_MODULE,
1641 .socketpair = sock_no_socketpair,
1643 .getname = get_name,
1645 .ioctl = sock_no_ioctl,
1647 .shutdown = shutdown,
1648 .setsockopt = setsockopt,
1649 .getsockopt = getsockopt,
1650 .sendmsg = send_packet,
1651 .recvmsg = recv_msg,
1652 .mmap = sock_no_mmap,
1653 .sendpage = sock_no_sendpage
1656 static const struct proto_ops stream_ops = {
1657 .owner = THIS_MODULE,
1662 .socketpair = sock_no_socketpair,
1664 .getname = get_name,
1666 .ioctl = sock_no_ioctl,
1668 .shutdown = shutdown,
1669 .setsockopt = setsockopt,
1670 .getsockopt = getsockopt,
1671 .sendmsg = send_stream,
1672 .recvmsg = recv_stream,
1673 .mmap = sock_no_mmap,
1674 .sendpage = sock_no_sendpage
1677 static const struct net_proto_family tipc_family_ops = {
1678 .owner = THIS_MODULE,
1680 .create = tipc_create
1683 static struct proto tipc_proto = {
1685 .owner = THIS_MODULE,
1686 .obj_size = sizeof(struct tipc_sock)
1690 * tipc_socket_init - initialize TIPC socket interface
1692 * Returns 0 on success, errno otherwise
1694 int tipc_socket_init(void)
1698 res = proto_register(&tipc_proto, 1);
1700 err("Failed to register TIPC protocol type\n");
1704 res = sock_register(&tipc_family_ops);
1706 err("Failed to register TIPC socket type\n");
1707 proto_unregister(&tipc_proto);
1711 sockets_enabled = 1;
1717 * tipc_socket_stop - stop TIPC socket interface
1719 void tipc_socket_stop(void)
1721 if (!sockets_enabled)
1724 sockets_enabled = 0;
1725 sock_unregister(tipc_family_ops.family);
1726 proto_unregister(&tipc_proto);