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
69 #define tipc_sk(sk) ((struct tipc_sock*)sk)
71 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
72 static void wakeupdispatch(struct tipc_port *tport);
74 static const struct proto_ops packet_ops;
75 static const struct proto_ops stream_ops;
76 static const struct proto_ops msg_ops;
78 static struct proto tipc_proto;
80 static int sockets_enabled = 0;
82 static atomic_t tipc_queue_size = ATOMIC_INIT(0);
86 * sock_lock(): Lock a port/socket pair. lock_sock() can
87 * not be used here, since the same lock must protect ports
88 * with non-socket interfaces.
89 * See net.c for description of locking policy.
91 static void sock_lock(struct tipc_sock* tsock)
93 spin_lock_bh(tsock->p->lock);
97 * sock_unlock(): Unlock a port/socket pair
99 static void sock_unlock(struct tipc_sock* tsock)
101 spin_unlock_bh(tsock->p->lock);
105 * advance_queue - discard first buffer in queue
106 * @tsock: TIPC socket
109 static void advance_queue(struct tipc_sock *tsock)
112 buf_discard(skb_dequeue(&tsock->sk.sk_receive_queue));
114 atomic_dec(&tipc_queue_size);
118 * tipc_create - create a TIPC socket
119 * @sock: pre-allocated socket structure
120 * @protocol: protocol indicator (must be 0)
122 * This routine creates and attaches a 'struct sock' to the 'struct socket',
123 * then create and attaches a TIPC port to the 'struct sock' part.
125 * Returns 0 on success, errno otherwise
127 static int tipc_create(struct net *net, struct socket *sock, int protocol)
129 struct tipc_sock *tsock;
130 struct tipc_port *port;
134 if (net != &init_net)
135 return -EAFNOSUPPORT;
137 if (unlikely(protocol != 0))
138 return -EPROTONOSUPPORT;
140 ref = tipc_createport_raw(NULL, &dispatch, &wakeupdispatch, TIPC_LOW_IMPORTANCE);
144 sock->state = SS_UNCONNECTED;
146 switch (sock->type) {
148 sock->ops = &stream_ops;
151 sock->ops = &packet_ops;
154 tipc_set_portunreliable(ref, 1);
157 tipc_set_portunreturnable(ref, 1);
158 sock->ops = &msg_ops;
159 sock->state = SS_READY;
162 tipc_deleteport(ref);
166 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
168 tipc_deleteport(ref);
172 sock_init_data(sock, sk);
173 sk->sk_rcvtimeo = 8 * HZ; /* default connect timeout = 8s */
176 port = tipc_get_port(ref);
179 port->usr_handle = tsock;
181 mutex_init(&tsock->lock);
183 dbg("sock_create: %x\n",tsock);
185 atomic_inc(&tipc_user_count);
191 * release - destroy a TIPC socket
192 * @sock: socket to destroy
194 * This routine cleans up any messages that are still queued on the socket.
195 * For DGRAM and RDM socket types, all queued messages are rejected.
196 * For SEQPACKET and STREAM socket types, the first message is rejected
197 * and any others are discarded. (If the first message on a STREAM socket
198 * is partially-read, it is discarded and the next one is rejected instead.)
200 * NOTE: Rejected messages are not necessarily returned to the sender! They
201 * are returned or discarded according to the "destination droppable" setting
202 * specified for the message by the sender.
204 * Returns 0 on success, errno otherwise
207 static int release(struct socket *sock)
209 struct tipc_sock *tsock = tipc_sk(sock->sk);
210 struct sock *sk = sock->sk;
214 dbg("sock_delete: %x\n",tsock);
217 mutex_lock(&tsock->lock);
219 mutex_unlock(&tsock->lock);
223 /* Reject unreceived messages, unless no longer connected */
225 while (sock->state != SS_DISCONNECTING) {
227 buf = skb_dequeue(&sk->sk_receive_queue);
229 tsock->p->usr_handle = NULL;
233 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
236 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
237 atomic_dec(&tipc_queue_size);
240 /* Delete TIPC port */
242 res = tipc_deleteport(tsock->p->ref);
245 /* Discard any remaining messages */
247 while ((buf = skb_dequeue(&sk->sk_receive_queue))) {
249 atomic_dec(&tipc_queue_size);
252 mutex_unlock(&tsock->lock);
256 atomic_dec(&tipc_user_count);
261 * bind - associate or disassocate TIPC name(s) with a socket
262 * @sock: socket structure
263 * @uaddr: socket address describing name(s) and desired operation
264 * @uaddr_len: size of socket address data structure
266 * Name and name sequence binding is indicated using a positive scope value;
267 * a negative scope value unbinds the specified name. Specifying no name
268 * (i.e. a socket address length of 0) unbinds all names from the socket.
270 * Returns 0 on success, errno otherwise
273 static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
275 struct tipc_sock *tsock = tipc_sk(sock->sk);
276 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
279 if (mutex_lock_interruptible(&tsock->lock))
282 if (unlikely(!uaddr_len)) {
283 res = tipc_withdraw(tsock->p->ref, 0, NULL);
287 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
292 if (addr->family != AF_TIPC) {
296 if (addr->addrtype == TIPC_ADDR_NAME)
297 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
298 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
304 res = tipc_publish(tsock->p->ref, addr->scope,
305 &addr->addr.nameseq);
307 res = tipc_withdraw(tsock->p->ref, -addr->scope,
308 &addr->addr.nameseq);
310 mutex_unlock(&tsock->lock);
315 * get_name - get port ID of socket or peer socket
316 * @sock: socket structure
317 * @uaddr: area for returned socket address
318 * @uaddr_len: area for returned length of socket address
319 * @peer: 0 to obtain socket name, 1 to obtain peer socket name
321 * Returns 0 on success, errno otherwise
324 static int get_name(struct socket *sock, struct sockaddr *uaddr,
325 int *uaddr_len, int peer)
327 struct tipc_sock *tsock = tipc_sk(sock->sk);
328 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
331 if (mutex_lock_interruptible(&tsock->lock))
334 *uaddr_len = sizeof(*addr);
335 addr->addrtype = TIPC_ADDR_ID;
336 addr->family = AF_TIPC;
339 res = tipc_peer(tsock->p->ref, &addr->addr.id);
341 res = tipc_ownidentity(tsock->p->ref, &addr->addr.id);
342 addr->addr.name.domain = 0;
344 mutex_unlock(&tsock->lock);
349 * poll - read and possibly block on pollmask
350 * @file: file structure associated with the socket
351 * @sock: socket for which to calculate the poll bits
354 * Returns pollmask value
357 * It appears that the usual socket locking mechanisms are not useful here
358 * since the pollmask info is potentially out-of-date the moment this routine
359 * exits. TCP and other protocols seem to rely on higher level poll routines
360 * to handle any preventable race conditions, so TIPC will do the same ...
362 * TIPC sets the returned events as follows:
363 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
364 * or if a connection-oriented socket is does not have an active connection
365 * (i.e. a read operation will not block).
366 * b) POLLOUT is set except when a socket's connection has been terminated
367 * (i.e. a write operation will not block).
368 * c) POLLHUP is set when a socket's connection has been terminated.
370 * IMPORTANT: The fact that a read or write operation will not block does NOT
371 * imply that the operation will succeed!
374 static unsigned int poll(struct file *file, struct socket *sock,
377 struct sock *sk = sock->sk;
380 poll_wait(file, sk->sk_sleep, wait);
382 if (!skb_queue_empty(&sk->sk_receive_queue) ||
383 (sock->state == SS_UNCONNECTED) ||
384 (sock->state == SS_DISCONNECTING))
385 mask = (POLLRDNORM | POLLIN);
389 if (sock->state == SS_DISCONNECTING)
398 * dest_name_check - verify user is permitted to send to specified port name
399 * @dest: destination address
400 * @m: descriptor for message to be sent
402 * Prevents restricted configuration commands from being issued by
403 * unauthorized users.
405 * Returns 0 if permission is granted, otherwise errno
408 static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
410 struct tipc_cfg_msg_hdr hdr;
412 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
414 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
417 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
420 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
422 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
429 * send_msg - send message in connectionless manner
431 * @sock: socket structure
432 * @m: message to send
433 * @total_len: length of message
435 * Message must have an destination specified explicitly.
436 * Used for SOCK_RDM and SOCK_DGRAM messages,
437 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
438 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
440 * Returns the number of bytes sent on success, or errno otherwise
443 static int send_msg(struct kiocb *iocb, struct socket *sock,
444 struct msghdr *m, size_t total_len)
446 struct tipc_sock *tsock = tipc_sk(sock->sk);
447 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
453 return -EDESTADDRREQ;
454 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
455 (dest->family != AF_TIPC)))
458 needs_conn = (sock->state != SS_READY);
459 if (unlikely(needs_conn)) {
460 if (sock->state == SS_LISTENING)
462 if (sock->state != SS_UNCONNECTED)
464 if ((tsock->p->published) ||
465 ((sock->type == SOCK_STREAM) && (total_len != 0)))
467 if (dest->addrtype == TIPC_ADDR_NAME) {
468 tsock->p->conn_type = dest->addr.name.name.type;
469 tsock->p->conn_instance = dest->addr.name.name.instance;
473 if (mutex_lock_interruptible(&tsock->lock))
478 /* Abort any pending connection attempts (very unlikely) */
480 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
481 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
482 atomic_dec(&tipc_queue_size);
485 sock->state = SS_CONNECTING;
489 if (dest->addrtype == TIPC_ADDR_NAME) {
490 if ((res = dest_name_check(dest, m)))
492 res = tipc_send2name(tsock->p->ref,
493 &dest->addr.name.name,
494 dest->addr.name.domain,
498 else if (dest->addrtype == TIPC_ADDR_ID) {
499 res = tipc_send2port(tsock->p->ref,
504 else if (dest->addrtype == TIPC_ADDR_MCAST) {
509 if ((res = dest_name_check(dest, m)))
511 res = tipc_multicast(tsock->p->ref,
517 if (likely(res != -ELINKCONG)) {
519 mutex_unlock(&tsock->lock);
522 if (m->msg_flags & MSG_DONTWAIT) {
526 if (wait_event_interruptible(*sock->sk->sk_sleep,
527 !tsock->p->congested)) {
535 * send_packet - send a connection-oriented message
537 * @sock: socket structure
538 * @m: message to send
539 * @total_len: length of message
541 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
543 * Returns the number of bytes sent on success, or errno otherwise
546 static int send_packet(struct kiocb *iocb, struct socket *sock,
547 struct msghdr *m, size_t total_len)
549 struct tipc_sock *tsock = tipc_sk(sock->sk);
550 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
553 /* Handle implied connection establishment */
556 return send_msg(iocb, sock, m, total_len);
558 if (mutex_lock_interruptible(&tsock->lock)) {
563 if (unlikely(sock->state != SS_CONNECTED)) {
564 if (sock->state == SS_DISCONNECTING)
571 res = tipc_send(tsock->p->ref, m->msg_iovlen, m->msg_iov);
572 if (likely(res != -ELINKCONG)) {
574 mutex_unlock(&tsock->lock);
577 if (m->msg_flags & MSG_DONTWAIT) {
581 if (wait_event_interruptible(*sock->sk->sk_sleep,
582 !tsock->p->congested)) {
590 * send_stream - send stream-oriented data
592 * @sock: socket structure
594 * @total_len: total length of data to be sent
596 * Used for SOCK_STREAM data.
598 * Returns the number of bytes sent on success (or partial success),
599 * or errno if no data sent
603 static int send_stream(struct kiocb *iocb, struct socket *sock,
604 struct msghdr *m, size_t total_len)
606 struct tipc_port *tport;
607 struct msghdr my_msg;
609 struct iovec *curr_iov;
611 char __user *curr_start;
618 /* Handle special cases where there is no connection */
620 if (unlikely(sock->state != SS_CONNECTED)) {
621 if (sock->state == SS_UNCONNECTED)
622 return send_packet(iocb, sock, m, total_len);
623 else if (sock->state == SS_DISCONNECTING)
629 if (unlikely(m->msg_name))
633 * Send each iovec entry using one or more messages
635 * Note: This algorithm is good for the most likely case
636 * (i.e. one large iovec entry), but could be improved to pass sets
637 * of small iovec entries into send_packet().
640 curr_iov = m->msg_iov;
641 curr_iovlen = m->msg_iovlen;
642 my_msg.msg_iov = &my_iov;
643 my_msg.msg_iovlen = 1;
644 my_msg.msg_flags = m->msg_flags;
645 my_msg.msg_name = NULL;
648 tport = tipc_sk(sock->sk)->p;
649 hdr_size = msg_hdr_sz(&tport->phdr);
651 while (curr_iovlen--) {
652 curr_start = curr_iov->iov_base;
653 curr_left = curr_iov->iov_len;
656 bytes_to_send = tport->max_pkt - hdr_size;
657 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
658 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
659 if (curr_left < bytes_to_send)
660 bytes_to_send = curr_left;
661 my_iov.iov_base = curr_start;
662 my_iov.iov_len = bytes_to_send;
663 if ((res = send_packet(iocb, sock, &my_msg, 0)) < 0) {
668 curr_left -= bytes_to_send;
669 curr_start += bytes_to_send;
670 bytes_sent += bytes_to_send;
680 * auto_connect - complete connection setup to a remote port
681 * @sock: socket structure
682 * @tsock: TIPC-specific socket structure
683 * @msg: peer's response message
685 * Returns 0 on success, errno otherwise
688 static int auto_connect(struct socket *sock, struct tipc_sock *tsock,
689 struct tipc_msg *msg)
691 struct tipc_portid peer;
693 if (msg_errcode(msg)) {
694 sock->state = SS_DISCONNECTING;
695 return -ECONNREFUSED;
698 peer.ref = msg_origport(msg);
699 peer.node = msg_orignode(msg);
700 tipc_connect2port(tsock->p->ref, &peer);
701 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
702 sock->state = SS_CONNECTED;
707 * set_orig_addr - capture sender's address for received message
708 * @m: descriptor for message info
709 * @msg: received message header
711 * Note: Address is not captured if not requested by receiver.
714 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
716 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
719 addr->family = AF_TIPC;
720 addr->addrtype = TIPC_ADDR_ID;
721 addr->addr.id.ref = msg_origport(msg);
722 addr->addr.id.node = msg_orignode(msg);
723 addr->addr.name.domain = 0; /* could leave uninitialized */
724 addr->scope = 0; /* could leave uninitialized */
725 m->msg_namelen = sizeof(struct sockaddr_tipc);
730 * anc_data_recv - optionally capture ancillary data for received message
731 * @m: descriptor for message info
732 * @msg: received message header
733 * @tport: TIPC port associated with message
735 * Note: Ancillary data is not captured if not requested by receiver.
737 * Returns 0 if successful, otherwise errno
740 static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
741 struct tipc_port *tport)
749 if (likely(m->msg_controllen == 0))
752 /* Optionally capture errored message object(s) */
754 err = msg ? msg_errcode(msg) : 0;
757 anc_data[1] = msg_data_sz(msg);
758 if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data)))
761 (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
766 /* Optionally capture message destination object */
768 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
772 anc_data[0] = msg_nametype(msg);
773 anc_data[1] = msg_namelower(msg);
774 anc_data[2] = msg_namelower(msg);
778 anc_data[0] = msg_nametype(msg);
779 anc_data[1] = msg_namelower(msg);
780 anc_data[2] = msg_nameupper(msg);
783 has_name = (tport->conn_type != 0);
784 anc_data[0] = tport->conn_type;
785 anc_data[1] = tport->conn_instance;
786 anc_data[2] = tport->conn_instance;
792 (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data)))
799 * recv_msg - receive packet-oriented message
801 * @m: descriptor for message info
802 * @buf_len: total size of user buffer area
803 * @flags: receive flags
805 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
806 * If the complete message doesn't fit in user area, truncate it.
808 * Returns size of returned message data, errno otherwise
811 static int recv_msg(struct kiocb *iocb, struct socket *sock,
812 struct msghdr *m, size_t buf_len, int flags)
814 struct tipc_sock *tsock = tipc_sk(sock->sk);
816 struct tipc_msg *msg;
822 /* Currently doesn't support receiving into multiple iovec entries */
824 if (m->msg_iovlen != 1)
827 /* Catch invalid receive attempts */
829 if (unlikely(!buf_len))
832 if (sock->type == SOCK_SEQPACKET) {
833 if (unlikely(sock->state == SS_UNCONNECTED))
835 if (unlikely((sock->state == SS_DISCONNECTING) &&
836 (skb_queue_len(&sock->sk->sk_receive_queue) == 0)))
840 /* Look for a message in receive queue; wait if necessary */
842 if (unlikely(mutex_lock_interruptible(&tsock->lock)))
846 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
847 (flags & MSG_DONTWAIT))) {
852 if ((res = wait_event_interruptible(
854 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
855 (sock->state == SS_DISCONNECTING))) )) {
859 /* Catch attempt to receive on an already terminated connection */
860 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
867 /* Get access to first message in receive queue */
869 buf = skb_peek(&sock->sk->sk_receive_queue);
871 sz = msg_data_sz(msg);
872 err = msg_errcode(msg);
874 /* Complete connection setup for an implied connect */
876 if (unlikely(sock->state == SS_CONNECTING)) {
877 if ((res = auto_connect(sock, tsock, msg)))
881 /* Discard an empty non-errored message & try again */
883 if ((!sz) && (!err)) {
884 advance_queue(tsock);
888 /* Capture sender's address (optional) */
890 set_orig_addr(m, msg);
892 /* Capture ancillary data (optional) */
894 if ((res = anc_data_recv(m, msg, tsock->p)))
897 /* Capture message data (if valid) & compute return value (always) */
900 if (unlikely(buf_len < sz)) {
902 m->msg_flags |= MSG_TRUNC;
904 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
911 if ((sock->state == SS_READY) ||
912 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
918 /* Consume received message (optional) */
920 if (likely(!(flags & MSG_PEEK))) {
921 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
922 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
923 advance_queue(tsock);
926 mutex_unlock(&tsock->lock);
931 * recv_stream - receive stream-oriented data
933 * @m: descriptor for message info
934 * @buf_len: total size of user buffer area
935 * @flags: receive flags
937 * Used for SOCK_STREAM messages only. If not enough data is available
938 * will optionally wait for more; never truncates data.
940 * Returns size of returned message data, errno otherwise
943 static int recv_stream(struct kiocb *iocb, struct socket *sock,
944 struct msghdr *m, size_t buf_len, int flags)
946 struct tipc_sock *tsock = tipc_sk(sock->sk);
948 struct tipc_msg *msg;
954 char __user *crs = m->msg_iov->iov_base;
955 unsigned char *buf_crs;
959 /* Currently doesn't support receiving into multiple iovec entries */
961 if (m->msg_iovlen != 1)
964 /* Catch invalid receive attempts */
966 if (unlikely(!buf_len))
969 if (unlikely(sock->state == SS_DISCONNECTING)) {
970 if (skb_queue_len(&sock->sk->sk_receive_queue) == 0)
972 } else if (unlikely(sock->state != SS_CONNECTED))
975 /* Look for a message in receive queue; wait if necessary */
977 if (unlikely(mutex_lock_interruptible(&tsock->lock)))
981 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
982 (flags & MSG_DONTWAIT))) {
987 if ((res = wait_event_interruptible(
989 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
990 (sock->state == SS_DISCONNECTING))) )) {
994 /* Catch attempt to receive on an already terminated connection */
995 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
1002 /* Get access to first message in receive queue */
1004 buf = skb_peek(&sock->sk->sk_receive_queue);
1006 sz = msg_data_sz(msg);
1007 err = msg_errcode(msg);
1009 /* Discard an empty non-errored message & try again */
1011 if ((!sz) && (!err)) {
1012 advance_queue(tsock);
1016 /* Optionally capture sender's address & ancillary data of first msg */
1018 if (sz_copied == 0) {
1019 set_orig_addr(m, msg);
1020 if ((res = anc_data_recv(m, msg, tsock->p)))
1024 /* Capture message data (if valid) & compute return value (always) */
1027 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
1028 sz = skb_tail_pointer(buf) - buf_crs;
1030 needed = (buf_len - sz_copied);
1031 sz_to_copy = (sz <= needed) ? sz : needed;
1032 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1036 sz_copied += sz_to_copy;
1038 if (sz_to_copy < sz) {
1039 if (!(flags & MSG_PEEK))
1040 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1047 goto exit; /* can't add error msg to valid data */
1049 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1055 /* Consume received message (optional) */
1057 if (likely(!(flags & MSG_PEEK))) {
1058 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1059 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
1060 advance_queue(tsock);
1063 /* Loop around if more data is required */
1065 if ((sz_copied < buf_len) /* didn't get all requested data */
1066 && (flags & MSG_WAITALL) /* ... and need to wait for more */
1067 && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
1068 && (!err) /* ... and haven't reached a FIN */
1073 mutex_unlock(&tsock->lock);
1074 return sz_copied ? sz_copied : res;
1078 * queue_overloaded - test if queue overload condition exists
1079 * @queue_size: current size of queue
1080 * @base: nominal maximum size of queue
1081 * @msg: message to be added to queue
1083 * Returns 1 if queue is currently overloaded, 0 otherwise
1086 static int queue_overloaded(u32 queue_size, u32 base, struct tipc_msg *msg)
1089 u32 imp = msg_importance(msg);
1091 if (imp == TIPC_LOW_IMPORTANCE)
1093 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1094 threshold = base * 2;
1095 else if (imp == TIPC_HIGH_IMPORTANCE)
1096 threshold = base * 100;
1100 if (msg_connected(msg))
1103 return (queue_size > threshold);
1107 * async_disconnect - wrapper function used to disconnect port
1108 * @portref: TIPC port reference (passed as pointer-sized value)
1111 static void async_disconnect(unsigned long portref)
1113 tipc_disconnect((u32)portref);
1117 * dispatch - handle arriving message
1118 * @tport: TIPC port that received message
1121 * Called with port locked. Must not take socket lock to avoid deadlock risk.
1123 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1126 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1128 struct tipc_msg *msg = buf_msg(buf);
1129 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1130 struct socket *sock;
1133 /* Reject message if socket is closing */
1136 return TIPC_ERR_NO_PORT;
1138 /* Reject message if it is wrong sort of message for socket */
1141 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1142 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1143 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1145 sock = tsock->sk.sk_socket;
1146 if (sock->state == SS_READY) {
1147 if (msg_connected(msg)) {
1148 msg_dbg(msg, "dispatch filter 1\n");
1149 return TIPC_ERR_NO_PORT;
1152 if (msg_mcast(msg)) {
1153 msg_dbg(msg, "dispatch filter 2\n");
1154 return TIPC_ERR_NO_PORT;
1156 if (sock->state == SS_CONNECTED) {
1157 if (!msg_connected(msg)) {
1158 msg_dbg(msg, "dispatch filter 3\n");
1159 return TIPC_ERR_NO_PORT;
1162 else if (sock->state == SS_CONNECTING) {
1163 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1164 msg_dbg(msg, "dispatch filter 4\n");
1165 return TIPC_ERR_NO_PORT;
1168 else if (sock->state == SS_LISTENING) {
1169 if (msg_connected(msg) || msg_errcode(msg)) {
1170 msg_dbg(msg, "dispatch filter 5\n");
1171 return TIPC_ERR_NO_PORT;
1174 else if (sock->state == SS_DISCONNECTING) {
1175 msg_dbg(msg, "dispatch filter 6\n");
1176 return TIPC_ERR_NO_PORT;
1178 else /* (sock->state == SS_UNCONNECTED) */ {
1179 if (msg_connected(msg) || msg_errcode(msg)) {
1180 msg_dbg(msg, "dispatch filter 7\n");
1181 return TIPC_ERR_NO_PORT;
1186 /* Reject message if there isn't room to queue it */
1188 if (unlikely((u32)atomic_read(&tipc_queue_size) >
1189 OVERLOAD_LIMIT_BASE)) {
1190 if (queue_overloaded(atomic_read(&tipc_queue_size),
1191 OVERLOAD_LIMIT_BASE, msg))
1192 return TIPC_ERR_OVERLOAD;
1194 recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue);
1195 if (unlikely(recv_q_len > (OVERLOAD_LIMIT_BASE / 2))) {
1196 if (queue_overloaded(recv_q_len,
1197 OVERLOAD_LIMIT_BASE / 2, msg))
1198 return TIPC_ERR_OVERLOAD;
1201 /* Initiate connection termination for an incoming 'FIN' */
1203 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1204 sock->state = SS_DISCONNECTING;
1205 /* Note: Use signal since port lock is already taken! */
1206 tipc_k_signal((Handler)async_disconnect, tport->ref);
1209 /* Enqueue message (finally!) */
1211 msg_dbg(msg,"<DISP<: ");
1212 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1213 atomic_inc(&tipc_queue_size);
1214 skb_queue_tail(&sock->sk->sk_receive_queue, buf);
1216 if (waitqueue_active(sock->sk->sk_sleep))
1217 wake_up_interruptible(sock->sk->sk_sleep);
1222 * wakeupdispatch - wake up port after congestion
1223 * @tport: port to wakeup
1225 * Called with port lock on.
1228 static void wakeupdispatch(struct tipc_port *tport)
1230 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1232 if (waitqueue_active(tsock->sk.sk_sleep))
1233 wake_up_interruptible(tsock->sk.sk_sleep);
1237 * connect - establish a connection to another TIPC port
1238 * @sock: socket structure
1239 * @dest: socket address for destination port
1240 * @destlen: size of socket address data structure
1243 * Returns 0 on success, errno otherwise
1246 static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
1249 struct tipc_sock *tsock = tipc_sk(sock->sk);
1250 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1251 struct msghdr m = {NULL,};
1252 struct sk_buff *buf;
1253 struct tipc_msg *msg;
1256 /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */
1258 if (sock->state == SS_READY)
1261 /* Issue Posix-compliant error code if socket is in the wrong state */
1263 if (sock->state == SS_LISTENING)
1265 if (sock->state == SS_CONNECTING)
1267 if (sock->state != SS_UNCONNECTED)
1271 * Reject connection attempt using multicast address
1273 * Note: send_msg() validates the rest of the address fields,
1274 * so there's no need to do it here
1277 if (dst->addrtype == TIPC_ADDR_MCAST)
1280 /* Send a 'SYN-' to destination */
1283 m.msg_namelen = destlen;
1284 if ((res = send_msg(NULL, sock, &m, 0)) < 0) {
1285 sock->state = SS_DISCONNECTING;
1289 if (mutex_lock_interruptible(&tsock->lock))
1290 return -ERESTARTSYS;
1292 /* Wait for destination's 'ACK' response */
1294 res = wait_event_interruptible_timeout(*sock->sk->sk_sleep,
1295 skb_queue_len(&sock->sk->sk_receive_queue),
1296 sock->sk->sk_rcvtimeo);
1297 buf = skb_peek(&sock->sk->sk_receive_queue);
1300 res = auto_connect(sock, tsock, msg);
1302 if (!msg_data_sz(msg))
1303 advance_queue(tsock);
1309 { /* leave "res" unchanged */ }
1310 sock->state = SS_DISCONNECTING;
1313 mutex_unlock(&tsock->lock);
1318 * listen - allow socket to listen for incoming connections
1319 * @sock: socket structure
1322 * Returns 0 on success, errno otherwise
1325 static int listen(struct socket *sock, int len)
1327 /* REQUIRES SOCKET LOCKING OF SOME SORT? */
1329 if (sock->state == SS_READY)
1331 if (sock->state != SS_UNCONNECTED)
1333 sock->state = SS_LISTENING;
1338 * accept - wait for connection request
1339 * @sock: listening socket
1340 * @newsock: new socket that is to be connected
1341 * @flags: file-related flags associated with socket
1343 * Returns 0 on success, errno otherwise
1346 static int accept(struct socket *sock, struct socket *newsock, int flags)
1348 struct tipc_sock *tsock = tipc_sk(sock->sk);
1349 struct sk_buff *buf;
1352 if (sock->state == SS_READY)
1354 if (sock->state != SS_LISTENING)
1357 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
1358 (flags & O_NONBLOCK)))
1359 return -EWOULDBLOCK;
1361 if (mutex_lock_interruptible(&tsock->lock))
1362 return -ERESTARTSYS;
1364 if (wait_event_interruptible(*sock->sk->sk_sleep,
1365 skb_queue_len(&sock->sk->sk_receive_queue))) {
1369 buf = skb_peek(&sock->sk->sk_receive_queue);
1371 res = tipc_create(sock_net(sock->sk), newsock, 0);
1373 struct tipc_sock *new_tsock = tipc_sk(newsock->sk);
1374 struct tipc_portid id;
1375 struct tipc_msg *msg = buf_msg(buf);
1376 u32 new_ref = new_tsock->p->ref;
1378 id.ref = msg_origport(msg);
1379 id.node = msg_orignode(msg);
1380 tipc_connect2port(new_ref, &id);
1381 newsock->state = SS_CONNECTED;
1383 tipc_set_portimportance(new_ref, msg_importance(msg));
1384 if (msg_named(msg)) {
1385 new_tsock->p->conn_type = msg_nametype(msg);
1386 new_tsock->p->conn_instance = msg_nameinst(msg);
1390 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1391 * Respond to 'SYN+' by queuing it on new socket.
1394 msg_dbg(msg,"<ACC<: ");
1395 if (!msg_data_sz(msg)) {
1396 struct msghdr m = {NULL,};
1398 send_packet(NULL, newsock, &m, 0);
1399 advance_queue(tsock);
1402 skb_dequeue(&sock->sk->sk_receive_queue);
1404 skb_queue_head(&newsock->sk->sk_receive_queue, buf);
1408 mutex_unlock(&tsock->lock);
1413 * shutdown - shutdown socket connection
1414 * @sock: socket structure
1415 * @how: direction to close (must be SHUT_RDWR)
1417 * Terminates connection (if necessary), then purges socket's receive queue.
1419 * Returns 0 on success, errno otherwise
1422 static int shutdown(struct socket *sock, int how)
1424 struct tipc_sock* tsock = tipc_sk(sock->sk);
1425 struct sk_buff *buf;
1428 if (how != SHUT_RDWR)
1431 if (mutex_lock_interruptible(&tsock->lock))
1432 return -ERESTARTSYS;
1436 switch (sock->state) {
1439 /* Send 'FIN+' or 'FIN-' message to peer */
1443 if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1444 atomic_dec(&tipc_queue_size);
1445 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1449 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
1452 tipc_shutdown(tsock->p->ref);
1458 case SS_DISCONNECTING:
1460 /* Discard any unreceived messages */
1462 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1463 atomic_dec(&tipc_queue_size);
1466 tsock->p->conn_unacked = 0;
1471 sock->state = SS_DISCONNECTING;
1481 mutex_unlock(&tsock->lock);
1486 * setsockopt - set socket option
1487 * @sock: socket structure
1488 * @lvl: option level
1489 * @opt: option identifier
1490 * @ov: pointer to new option value
1491 * @ol: length of option value
1493 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1494 * (to ease compatibility).
1496 * Returns 0 on success, errno otherwise
1499 static int setsockopt(struct socket *sock,
1500 int lvl, int opt, char __user *ov, int ol)
1502 struct tipc_sock *tsock = tipc_sk(sock->sk);
1506 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1508 if (lvl != SOL_TIPC)
1509 return -ENOPROTOOPT;
1510 if (ol < sizeof(value))
1512 if ((res = get_user(value, (u32 __user *)ov)))
1515 if (mutex_lock_interruptible(&tsock->lock))
1516 return -ERESTARTSYS;
1519 case TIPC_IMPORTANCE:
1520 res = tipc_set_portimportance(tsock->p->ref, value);
1522 case TIPC_SRC_DROPPABLE:
1523 if (sock->type != SOCK_STREAM)
1524 res = tipc_set_portunreliable(tsock->p->ref, value);
1528 case TIPC_DEST_DROPPABLE:
1529 res = tipc_set_portunreturnable(tsock->p->ref, value);
1531 case TIPC_CONN_TIMEOUT:
1532 sock->sk->sk_rcvtimeo = (value * HZ / 1000);
1538 mutex_unlock(&tsock->lock);
1543 * getsockopt - get socket option
1544 * @sock: socket structure
1545 * @lvl: option level
1546 * @opt: option identifier
1547 * @ov: receptacle for option value
1548 * @ol: receptacle for length of option value
1550 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1551 * (to ease compatibility).
1553 * Returns 0 on success, errno otherwise
1556 static int getsockopt(struct socket *sock,
1557 int lvl, int opt, char __user *ov, int __user *ol)
1559 struct tipc_sock *tsock = tipc_sk(sock->sk);
1564 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1565 return put_user(0, ol);
1566 if (lvl != SOL_TIPC)
1567 return -ENOPROTOOPT;
1568 if ((res = get_user(len, ol)))
1571 if (mutex_lock_interruptible(&tsock->lock))
1572 return -ERESTARTSYS;
1575 case TIPC_IMPORTANCE:
1576 res = tipc_portimportance(tsock->p->ref, &value);
1578 case TIPC_SRC_DROPPABLE:
1579 res = tipc_portunreliable(tsock->p->ref, &value);
1581 case TIPC_DEST_DROPPABLE:
1582 res = tipc_portunreturnable(tsock->p->ref, &value);
1584 case TIPC_CONN_TIMEOUT:
1585 value = (sock->sk->sk_rcvtimeo * 1000) / HZ;
1594 else if (len < sizeof(value)) {
1597 else if ((res = copy_to_user(ov, &value, sizeof(value)))) {
1598 /* couldn't return value */
1601 res = put_user(sizeof(value), ol);
1604 mutex_unlock(&tsock->lock);
1609 * Protocol switches for the various types of TIPC sockets
1612 static const struct proto_ops msg_ops = {
1613 .owner = THIS_MODULE,
1618 .socketpair = sock_no_socketpair,
1620 .getname = get_name,
1622 .ioctl = sock_no_ioctl,
1624 .shutdown = shutdown,
1625 .setsockopt = setsockopt,
1626 .getsockopt = getsockopt,
1627 .sendmsg = send_msg,
1628 .recvmsg = recv_msg,
1629 .mmap = sock_no_mmap,
1630 .sendpage = sock_no_sendpage
1633 static const struct proto_ops packet_ops = {
1634 .owner = THIS_MODULE,
1639 .socketpair = sock_no_socketpair,
1641 .getname = get_name,
1643 .ioctl = sock_no_ioctl,
1645 .shutdown = shutdown,
1646 .setsockopt = setsockopt,
1647 .getsockopt = getsockopt,
1648 .sendmsg = send_packet,
1649 .recvmsg = recv_msg,
1650 .mmap = sock_no_mmap,
1651 .sendpage = sock_no_sendpage
1654 static const struct proto_ops stream_ops = {
1655 .owner = THIS_MODULE,
1660 .socketpair = sock_no_socketpair,
1662 .getname = get_name,
1664 .ioctl = sock_no_ioctl,
1666 .shutdown = shutdown,
1667 .setsockopt = setsockopt,
1668 .getsockopt = getsockopt,
1669 .sendmsg = send_stream,
1670 .recvmsg = recv_stream,
1671 .mmap = sock_no_mmap,
1672 .sendpage = sock_no_sendpage
1675 static const struct net_proto_family tipc_family_ops = {
1676 .owner = THIS_MODULE,
1678 .create = tipc_create
1681 static struct proto tipc_proto = {
1683 .owner = THIS_MODULE,
1684 .obj_size = sizeof(struct tipc_sock)
1688 * tipc_socket_init - initialize TIPC socket interface
1690 * Returns 0 on success, errno otherwise
1692 int tipc_socket_init(void)
1696 res = proto_register(&tipc_proto, 1);
1698 err("Failed to register TIPC protocol type\n");
1702 res = sock_register(&tipc_family_ops);
1704 err("Failed to register TIPC socket type\n");
1705 proto_unregister(&tipc_proto);
1709 sockets_enabled = 1;
1715 * tipc_socket_stop - stop TIPC socket interface
1717 void tipc_socket_stop(void)
1719 if (!sockets_enabled)
1722 sockets_enabled = 0;
1723 sock_unregister(tipc_family_ops.family);
1724 proto_unregister(&tipc_proto);