cfq-iosched: fix locking around ioc->ioc_data assignment
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / tipc / socket.c
blob1a5f62ab62e78ddacf38cfdde963bb5d29e98a90
1 /*
2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2001-2007, Ericsson AB
5 * Copyright (c) 2004-2008, Wind River Systems
6 * All rights reserved.
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>
42 #include <linux/mm.h>
43 #include <linux/poll.h>
44 #include <linux/fcntl.h>
45 #include <linux/gfp.h>
46 #include <asm/string.h>
47 #include <asm/atomic.h>
48 #include <net/sock.h>
50 #include <linux/tipc.h>
51 #include <linux/tipc_config.h>
52 #include <net/tipc/tipc_msg.h>
53 #include <net/tipc/tipc_port.h>
55 #include "core.h"
57 #define SS_LISTENING -1 /* socket is listening */
58 #define SS_READY -2 /* socket is connectionless */
60 #define OVERLOAD_LIMIT_BASE 5000
61 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
63 struct tipc_sock {
64 struct sock sk;
65 struct tipc_port *p;
66 struct tipc_portid peer_name;
69 #define tipc_sk(sk) ((struct tipc_sock *)(sk))
70 #define tipc_sk_port(sk) ((struct tipc_port *)(tipc_sk(sk)->p))
72 static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
73 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
74 static void wakeupdispatch(struct tipc_port *tport);
76 static const struct proto_ops packet_ops;
77 static const struct proto_ops stream_ops;
78 static const struct proto_ops msg_ops;
80 static struct proto tipc_proto;
82 static int sockets_enabled = 0;
84 static atomic_t tipc_queue_size = ATOMIC_INIT(0);
87 * Revised TIPC socket locking policy:
89 * Most socket operations take the standard socket lock when they start
90 * and hold it until they finish (or until they need to sleep). Acquiring
91 * this lock grants the owner exclusive access to the fields of the socket
92 * data structures, with the exception of the backlog queue. A few socket
93 * operations can be done without taking the socket lock because they only
94 * read socket information that never changes during the life of the socket.
96 * Socket operations may acquire the lock for the associated TIPC port if they
97 * need to perform an operation on the port. If any routine needs to acquire
98 * both the socket lock and the port lock it must take the socket lock first
99 * to avoid the risk of deadlock.
101 * The dispatcher handling incoming messages cannot grab the socket lock in
102 * the standard fashion, since invoked it runs at the BH level and cannot block.
103 * Instead, it checks to see if the socket lock is currently owned by someone,
104 * and either handles the message itself or adds it to the socket's backlog
105 * queue; in the latter case the queued message is processed once the process
106 * owning the socket lock releases it.
108 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
109 * the problem of a blocked socket operation preventing any other operations
110 * from occurring. However, applications must be careful if they have
111 * multiple threads trying to send (or receive) on the same socket, as these
112 * operations might interfere with each other. For example, doing a connect
113 * and a receive at the same time might allow the receive to consume the
114 * ACK message meant for the connect. While additional work could be done
115 * to try and overcome this, it doesn't seem to be worthwhile at the present.
117 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
118 * that another operation that must be performed in a non-blocking manner is
119 * not delayed for very long because the lock has already been taken.
121 * NOTE: This code assumes that certain fields of a port/socket pair are
122 * constant over its lifetime; such fields can be examined without taking
123 * the socket lock and/or port lock, and do not need to be re-read even
124 * after resuming processing after waiting. These fields include:
125 * - socket type
126 * - pointer to socket sk structure (aka tipc_sock structure)
127 * - pointer to port structure
128 * - port reference
132 * advance_rx_queue - discard first buffer in socket receive queue
134 * Caller must hold socket lock
137 static void advance_rx_queue(struct sock *sk)
139 buf_discard(__skb_dequeue(&sk->sk_receive_queue));
140 atomic_dec(&tipc_queue_size);
144 * discard_rx_queue - discard all buffers in socket receive queue
146 * Caller must hold socket lock
149 static void discard_rx_queue(struct sock *sk)
151 struct sk_buff *buf;
153 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
154 atomic_dec(&tipc_queue_size);
155 buf_discard(buf);
160 * reject_rx_queue - reject all buffers in socket receive queue
162 * Caller must hold socket lock
165 static void reject_rx_queue(struct sock *sk)
167 struct sk_buff *buf;
169 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
170 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
171 atomic_dec(&tipc_queue_size);
176 * tipc_create - create a TIPC socket
177 * @net: network namespace (must be default network)
178 * @sock: pre-allocated socket structure
179 * @protocol: protocol indicator (must be 0)
180 * @kern: caused by kernel or by userspace?
182 * This routine creates additional data structures used by the TIPC socket,
183 * initializes them, and links them together.
185 * Returns 0 on success, errno otherwise
188 static int tipc_create(struct net *net, struct socket *sock, int protocol,
189 int kern)
191 const struct proto_ops *ops;
192 socket_state state;
193 struct sock *sk;
194 struct tipc_port *tp_ptr;
196 /* Validate arguments */
198 if (!net_eq(net, &init_net))
199 return -EAFNOSUPPORT;
201 if (unlikely(protocol != 0))
202 return -EPROTONOSUPPORT;
204 switch (sock->type) {
205 case SOCK_STREAM:
206 ops = &stream_ops;
207 state = SS_UNCONNECTED;
208 break;
209 case SOCK_SEQPACKET:
210 ops = &packet_ops;
211 state = SS_UNCONNECTED;
212 break;
213 case SOCK_DGRAM:
214 case SOCK_RDM:
215 ops = &msg_ops;
216 state = SS_READY;
217 break;
218 default:
219 return -EPROTOTYPE;
222 /* Allocate socket's protocol area */
224 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
225 if (sk == NULL)
226 return -ENOMEM;
228 /* Allocate TIPC port for socket to use */
230 tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
231 TIPC_LOW_IMPORTANCE);
232 if (unlikely(!tp_ptr)) {
233 sk_free(sk);
234 return -ENOMEM;
237 /* Finish initializing socket data structures */
239 sock->ops = ops;
240 sock->state = state;
242 sock_init_data(sock, sk);
243 sk->sk_rcvtimeo = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
244 sk->sk_backlog_rcv = backlog_rcv;
245 tipc_sk(sk)->p = tp_ptr;
247 spin_unlock_bh(tp_ptr->lock);
249 if (sock->state == SS_READY) {
250 tipc_set_portunreturnable(tp_ptr->ref, 1);
251 if (sock->type == SOCK_DGRAM)
252 tipc_set_portunreliable(tp_ptr->ref, 1);
255 atomic_inc(&tipc_user_count);
256 return 0;
260 * release - destroy a TIPC socket
261 * @sock: socket to destroy
263 * This routine cleans up any messages that are still queued on the socket.
264 * For DGRAM and RDM socket types, all queued messages are rejected.
265 * For SEQPACKET and STREAM socket types, the first message is rejected
266 * and any others are discarded. (If the first message on a STREAM socket
267 * is partially-read, it is discarded and the next one is rejected instead.)
269 * NOTE: Rejected messages are not necessarily returned to the sender! They
270 * are returned or discarded according to the "destination droppable" setting
271 * specified for the message by the sender.
273 * Returns 0 on success, errno otherwise
276 static int release(struct socket *sock)
278 struct sock *sk = sock->sk;
279 struct tipc_port *tport;
280 struct sk_buff *buf;
281 int res;
284 * Exit if socket isn't fully initialized (occurs when a failed accept()
285 * releases a pre-allocated child socket that was never used)
288 if (sk == NULL)
289 return 0;
291 tport = tipc_sk_port(sk);
292 lock_sock(sk);
295 * Reject all unreceived messages, except on an active connection
296 * (which disconnects locally & sends a 'FIN+' to peer)
299 while (sock->state != SS_DISCONNECTING) {
300 buf = __skb_dequeue(&sk->sk_receive_queue);
301 if (buf == NULL)
302 break;
303 atomic_dec(&tipc_queue_size);
304 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
305 buf_discard(buf);
306 else {
307 if ((sock->state == SS_CONNECTING) ||
308 (sock->state == SS_CONNECTED)) {
309 sock->state = SS_DISCONNECTING;
310 tipc_disconnect(tport->ref);
312 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
317 * Delete TIPC port; this ensures no more messages are queued
318 * (also disconnects an active connection & sends a 'FIN-' to peer)
321 res = tipc_deleteport(tport->ref);
323 /* Discard any remaining (connection-based) messages in receive queue */
325 discard_rx_queue(sk);
327 /* Reject any messages that accumulated in backlog queue */
329 sock->state = SS_DISCONNECTING;
330 release_sock(sk);
332 sock_put(sk);
333 sock->sk = NULL;
335 atomic_dec(&tipc_user_count);
336 return res;
340 * bind - associate or disassocate TIPC name(s) with a socket
341 * @sock: socket structure
342 * @uaddr: socket address describing name(s) and desired operation
343 * @uaddr_len: size of socket address data structure
345 * Name and name sequence binding is indicated using a positive scope value;
346 * a negative scope value unbinds the specified name. Specifying no name
347 * (i.e. a socket address length of 0) unbinds all names from the socket.
349 * Returns 0 on success, errno otherwise
351 * NOTE: This routine doesn't need to take the socket lock since it doesn't
352 * access any non-constant socket information.
355 static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
357 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
358 u32 portref = tipc_sk_port(sock->sk)->ref;
360 if (unlikely(!uaddr_len))
361 return tipc_withdraw(portref, 0, NULL);
363 if (uaddr_len < sizeof(struct sockaddr_tipc))
364 return -EINVAL;
365 if (addr->family != AF_TIPC)
366 return -EAFNOSUPPORT;
368 if (addr->addrtype == TIPC_ADDR_NAME)
369 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
370 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
371 return -EAFNOSUPPORT;
373 return (addr->scope > 0) ?
374 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
375 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
379 * get_name - get port ID of socket or peer socket
380 * @sock: socket structure
381 * @uaddr: area for returned socket address
382 * @uaddr_len: area for returned length of socket address
383 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
385 * Returns 0 on success, errno otherwise
387 * NOTE: This routine doesn't need to take the socket lock since it only
388 * accesses socket information that is unchanging (or which changes in
389 * a completely predictable manner).
392 static int get_name(struct socket *sock, struct sockaddr *uaddr,
393 int *uaddr_len, int peer)
395 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
396 struct tipc_sock *tsock = tipc_sk(sock->sk);
398 memset(addr, 0, sizeof(*addr));
399 if (peer) {
400 if ((sock->state != SS_CONNECTED) &&
401 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
402 return -ENOTCONN;
403 addr->addr.id.ref = tsock->peer_name.ref;
404 addr->addr.id.node = tsock->peer_name.node;
405 } else {
406 tipc_ownidentity(tsock->p->ref, &addr->addr.id);
409 *uaddr_len = sizeof(*addr);
410 addr->addrtype = TIPC_ADDR_ID;
411 addr->family = AF_TIPC;
412 addr->scope = 0;
413 addr->addr.name.domain = 0;
415 return 0;
419 * poll - read and possibly block on pollmask
420 * @file: file structure associated with the socket
421 * @sock: socket for which to calculate the poll bits
422 * @wait: ???
424 * Returns pollmask value
426 * COMMENTARY:
427 * It appears that the usual socket locking mechanisms are not useful here
428 * since the pollmask info is potentially out-of-date the moment this routine
429 * exits. TCP and other protocols seem to rely on higher level poll routines
430 * to handle any preventable race conditions, so TIPC will do the same ...
432 * TIPC sets the returned events as follows:
433 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
434 * or if a connection-oriented socket is does not have an active connection
435 * (i.e. a read operation will not block).
436 * b) POLLOUT is set except when a socket's connection has been terminated
437 * (i.e. a write operation will not block).
438 * c) POLLHUP is set when a socket's connection has been terminated.
440 * IMPORTANT: The fact that a read or write operation will not block does NOT
441 * imply that the operation will succeed!
444 static unsigned int poll(struct file *file, struct socket *sock,
445 poll_table *wait)
447 struct sock *sk = sock->sk;
448 u32 mask;
450 poll_wait(file, sk_sleep(sk), wait);
452 if (!skb_queue_empty(&sk->sk_receive_queue) ||
453 (sock->state == SS_UNCONNECTED) ||
454 (sock->state == SS_DISCONNECTING))
455 mask = (POLLRDNORM | POLLIN);
456 else
457 mask = 0;
459 if (sock->state == SS_DISCONNECTING)
460 mask |= POLLHUP;
461 else
462 mask |= POLLOUT;
464 return mask;
468 * dest_name_check - verify user is permitted to send to specified port name
469 * @dest: destination address
470 * @m: descriptor for message to be sent
472 * Prevents restricted configuration commands from being issued by
473 * unauthorized users.
475 * Returns 0 if permission is granted, otherwise errno
478 static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
480 struct tipc_cfg_msg_hdr hdr;
482 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
483 return 0;
484 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
485 return 0;
486 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
487 return -EACCES;
489 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
490 return -EFAULT;
491 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
492 return -EACCES;
494 return 0;
498 * send_msg - send message in connectionless manner
499 * @iocb: if NULL, indicates that socket lock is already held
500 * @sock: socket structure
501 * @m: message to send
502 * @total_len: length of message
504 * Message must have an destination specified explicitly.
505 * Used for SOCK_RDM and SOCK_DGRAM messages,
506 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
507 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
509 * Returns the number of bytes sent on success, or errno otherwise
512 static int send_msg(struct kiocb *iocb, struct socket *sock,
513 struct msghdr *m, size_t total_len)
515 struct sock *sk = sock->sk;
516 struct tipc_port *tport = tipc_sk_port(sk);
517 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
518 int needs_conn;
519 int res = -EINVAL;
521 if (unlikely(!dest))
522 return -EDESTADDRREQ;
523 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
524 (dest->family != AF_TIPC)))
525 return -EINVAL;
527 if (iocb)
528 lock_sock(sk);
530 needs_conn = (sock->state != SS_READY);
531 if (unlikely(needs_conn)) {
532 if (sock->state == SS_LISTENING) {
533 res = -EPIPE;
534 goto exit;
536 if (sock->state != SS_UNCONNECTED) {
537 res = -EISCONN;
538 goto exit;
540 if ((tport->published) ||
541 ((sock->type == SOCK_STREAM) && (total_len != 0))) {
542 res = -EOPNOTSUPP;
543 goto exit;
545 if (dest->addrtype == TIPC_ADDR_NAME) {
546 tport->conn_type = dest->addr.name.name.type;
547 tport->conn_instance = dest->addr.name.name.instance;
550 /* Abort any pending connection attempts (very unlikely) */
552 reject_rx_queue(sk);
555 do {
556 if (dest->addrtype == TIPC_ADDR_NAME) {
557 if ((res = dest_name_check(dest, m)))
558 break;
559 res = tipc_send2name(tport->ref,
560 &dest->addr.name.name,
561 dest->addr.name.domain,
562 m->msg_iovlen,
563 m->msg_iov);
565 else if (dest->addrtype == TIPC_ADDR_ID) {
566 res = tipc_send2port(tport->ref,
567 &dest->addr.id,
568 m->msg_iovlen,
569 m->msg_iov);
571 else if (dest->addrtype == TIPC_ADDR_MCAST) {
572 if (needs_conn) {
573 res = -EOPNOTSUPP;
574 break;
576 if ((res = dest_name_check(dest, m)))
577 break;
578 res = tipc_multicast(tport->ref,
579 &dest->addr.nameseq,
581 m->msg_iovlen,
582 m->msg_iov);
584 if (likely(res != -ELINKCONG)) {
585 if (needs_conn && (res >= 0)) {
586 sock->state = SS_CONNECTING;
588 break;
590 if (m->msg_flags & MSG_DONTWAIT) {
591 res = -EWOULDBLOCK;
592 break;
594 release_sock(sk);
595 res = wait_event_interruptible(*sk_sleep(sk),
596 !tport->congested);
597 lock_sock(sk);
598 if (res)
599 break;
600 } while (1);
602 exit:
603 if (iocb)
604 release_sock(sk);
605 return res;
609 * send_packet - send a connection-oriented message
610 * @iocb: if NULL, indicates that socket lock is already held
611 * @sock: socket structure
612 * @m: message to send
613 * @total_len: length of message
615 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
617 * Returns the number of bytes sent on success, or errno otherwise
620 static int send_packet(struct kiocb *iocb, struct socket *sock,
621 struct msghdr *m, size_t total_len)
623 struct sock *sk = sock->sk;
624 struct tipc_port *tport = tipc_sk_port(sk);
625 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
626 int res;
628 /* Handle implied connection establishment */
630 if (unlikely(dest))
631 return send_msg(iocb, sock, m, total_len);
633 if (iocb)
634 lock_sock(sk);
636 do {
637 if (unlikely(sock->state != SS_CONNECTED)) {
638 if (sock->state == SS_DISCONNECTING)
639 res = -EPIPE;
640 else
641 res = -ENOTCONN;
642 break;
645 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov);
646 if (likely(res != -ELINKCONG)) {
647 break;
649 if (m->msg_flags & MSG_DONTWAIT) {
650 res = -EWOULDBLOCK;
651 break;
653 release_sock(sk);
654 res = wait_event_interruptible(*sk_sleep(sk),
655 (!tport->congested || !tport->connected));
656 lock_sock(sk);
657 if (res)
658 break;
659 } while (1);
661 if (iocb)
662 release_sock(sk);
663 return res;
667 * send_stream - send stream-oriented data
668 * @iocb: (unused)
669 * @sock: socket structure
670 * @m: data to send
671 * @total_len: total length of data to be sent
673 * Used for SOCK_STREAM data.
675 * Returns the number of bytes sent on success (or partial success),
676 * or errno if no data sent
679 static int send_stream(struct kiocb *iocb, struct socket *sock,
680 struct msghdr *m, size_t total_len)
682 struct sock *sk = sock->sk;
683 struct tipc_port *tport = tipc_sk_port(sk);
684 struct msghdr my_msg;
685 struct iovec my_iov;
686 struct iovec *curr_iov;
687 int curr_iovlen;
688 char __user *curr_start;
689 u32 hdr_size;
690 int curr_left;
691 int bytes_to_send;
692 int bytes_sent;
693 int res;
695 lock_sock(sk);
697 /* Handle special cases where there is no connection */
699 if (unlikely(sock->state != SS_CONNECTED)) {
700 if (sock->state == SS_UNCONNECTED) {
701 res = send_packet(NULL, sock, m, total_len);
702 goto exit;
703 } else if (sock->state == SS_DISCONNECTING) {
704 res = -EPIPE;
705 goto exit;
706 } else {
707 res = -ENOTCONN;
708 goto exit;
712 if (unlikely(m->msg_name)) {
713 res = -EISCONN;
714 goto exit;
718 * Send each iovec entry using one or more messages
720 * Note: This algorithm is good for the most likely case
721 * (i.e. one large iovec entry), but could be improved to pass sets
722 * of small iovec entries into send_packet().
725 curr_iov = m->msg_iov;
726 curr_iovlen = m->msg_iovlen;
727 my_msg.msg_iov = &my_iov;
728 my_msg.msg_iovlen = 1;
729 my_msg.msg_flags = m->msg_flags;
730 my_msg.msg_name = NULL;
731 bytes_sent = 0;
733 hdr_size = msg_hdr_sz(&tport->phdr);
735 while (curr_iovlen--) {
736 curr_start = curr_iov->iov_base;
737 curr_left = curr_iov->iov_len;
739 while (curr_left) {
740 bytes_to_send = tport->max_pkt - hdr_size;
741 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
742 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
743 if (curr_left < bytes_to_send)
744 bytes_to_send = curr_left;
745 my_iov.iov_base = curr_start;
746 my_iov.iov_len = bytes_to_send;
747 if ((res = send_packet(NULL, sock, &my_msg, 0)) < 0) {
748 if (bytes_sent)
749 res = bytes_sent;
750 goto exit;
752 curr_left -= bytes_to_send;
753 curr_start += bytes_to_send;
754 bytes_sent += bytes_to_send;
757 curr_iov++;
759 res = bytes_sent;
760 exit:
761 release_sock(sk);
762 return res;
766 * auto_connect - complete connection setup to a remote port
767 * @sock: socket structure
768 * @msg: peer's response message
770 * Returns 0 on success, errno otherwise
773 static int auto_connect(struct socket *sock, struct tipc_msg *msg)
775 struct tipc_sock *tsock = tipc_sk(sock->sk);
777 if (msg_errcode(msg)) {
778 sock->state = SS_DISCONNECTING;
779 return -ECONNREFUSED;
782 tsock->peer_name.ref = msg_origport(msg);
783 tsock->peer_name.node = msg_orignode(msg);
784 tipc_connect2port(tsock->p->ref, &tsock->peer_name);
785 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
786 sock->state = SS_CONNECTED;
787 return 0;
791 * set_orig_addr - capture sender's address for received message
792 * @m: descriptor for message info
793 * @msg: received message header
795 * Note: Address is not captured if not requested by receiver.
798 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
800 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
802 if (addr) {
803 addr->family = AF_TIPC;
804 addr->addrtype = TIPC_ADDR_ID;
805 addr->addr.id.ref = msg_origport(msg);
806 addr->addr.id.node = msg_orignode(msg);
807 addr->addr.name.domain = 0; /* could leave uninitialized */
808 addr->scope = 0; /* could leave uninitialized */
809 m->msg_namelen = sizeof(struct sockaddr_tipc);
814 * anc_data_recv - optionally capture ancillary data for received message
815 * @m: descriptor for message info
816 * @msg: received message header
817 * @tport: TIPC port associated with message
819 * Note: Ancillary data is not captured if not requested by receiver.
821 * Returns 0 if successful, otherwise errno
824 static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
825 struct tipc_port *tport)
827 u32 anc_data[3];
828 u32 err;
829 u32 dest_type;
830 int has_name;
831 int res;
833 if (likely(m->msg_controllen == 0))
834 return 0;
836 /* Optionally capture errored message object(s) */
838 err = msg ? msg_errcode(msg) : 0;
839 if (unlikely(err)) {
840 anc_data[0] = err;
841 anc_data[1] = msg_data_sz(msg);
842 if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data)))
843 return res;
844 if (anc_data[1] &&
845 (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
846 msg_data(msg))))
847 return res;
850 /* Optionally capture message destination object */
852 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
853 switch (dest_type) {
854 case TIPC_NAMED_MSG:
855 has_name = 1;
856 anc_data[0] = msg_nametype(msg);
857 anc_data[1] = msg_namelower(msg);
858 anc_data[2] = msg_namelower(msg);
859 break;
860 case TIPC_MCAST_MSG:
861 has_name = 1;
862 anc_data[0] = msg_nametype(msg);
863 anc_data[1] = msg_namelower(msg);
864 anc_data[2] = msg_nameupper(msg);
865 break;
866 case TIPC_CONN_MSG:
867 has_name = (tport->conn_type != 0);
868 anc_data[0] = tport->conn_type;
869 anc_data[1] = tport->conn_instance;
870 anc_data[2] = tport->conn_instance;
871 break;
872 default:
873 has_name = 0;
875 if (has_name &&
876 (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data)))
877 return res;
879 return 0;
883 * recv_msg - receive packet-oriented message
884 * @iocb: (unused)
885 * @m: descriptor for message info
886 * @buf_len: total size of user buffer area
887 * @flags: receive flags
889 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
890 * If the complete message doesn't fit in user area, truncate it.
892 * Returns size of returned message data, errno otherwise
895 static int recv_msg(struct kiocb *iocb, struct socket *sock,
896 struct msghdr *m, size_t buf_len, int flags)
898 struct sock *sk = sock->sk;
899 struct tipc_port *tport = tipc_sk_port(sk);
900 struct sk_buff *buf;
901 struct tipc_msg *msg;
902 unsigned int sz;
903 u32 err;
904 int res;
906 /* Catch invalid receive requests */
908 if (m->msg_iovlen != 1)
909 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
911 if (unlikely(!buf_len))
912 return -EINVAL;
914 lock_sock(sk);
916 if (unlikely(sock->state == SS_UNCONNECTED)) {
917 res = -ENOTCONN;
918 goto exit;
921 restart:
923 /* Look for a message in receive queue; wait if necessary */
925 while (skb_queue_empty(&sk->sk_receive_queue)) {
926 if (sock->state == SS_DISCONNECTING) {
927 res = -ENOTCONN;
928 goto exit;
930 if (flags & MSG_DONTWAIT) {
931 res = -EWOULDBLOCK;
932 goto exit;
934 release_sock(sk);
935 res = wait_event_interruptible(*sk_sleep(sk),
936 (!skb_queue_empty(&sk->sk_receive_queue) ||
937 (sock->state == SS_DISCONNECTING)));
938 lock_sock(sk);
939 if (res)
940 goto exit;
943 /* Look at first message in receive queue */
945 buf = skb_peek(&sk->sk_receive_queue);
946 msg = buf_msg(buf);
947 sz = msg_data_sz(msg);
948 err = msg_errcode(msg);
950 /* Complete connection setup for an implied connect */
952 if (unlikely(sock->state == SS_CONNECTING)) {
953 res = auto_connect(sock, msg);
954 if (res)
955 goto exit;
958 /* Discard an empty non-errored message & try again */
960 if ((!sz) && (!err)) {
961 advance_rx_queue(sk);
962 goto restart;
965 /* Capture sender's address (optional) */
967 set_orig_addr(m, msg);
969 /* Capture ancillary data (optional) */
971 res = anc_data_recv(m, msg, tport);
972 if (res)
973 goto exit;
975 /* Capture message data (if valid) & compute return value (always) */
977 if (!err) {
978 if (unlikely(buf_len < sz)) {
979 sz = buf_len;
980 m->msg_flags |= MSG_TRUNC;
982 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
983 sz))) {
984 res = -EFAULT;
985 goto exit;
987 res = sz;
988 } else {
989 if ((sock->state == SS_READY) ||
990 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
991 res = 0;
992 else
993 res = -ECONNRESET;
996 /* Consume received message (optional) */
998 if (likely(!(flags & MSG_PEEK))) {
999 if ((sock->state != SS_READY) &&
1000 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1001 tipc_acknowledge(tport->ref, tport->conn_unacked);
1002 advance_rx_queue(sk);
1004 exit:
1005 release_sock(sk);
1006 return res;
1010 * recv_stream - receive stream-oriented data
1011 * @iocb: (unused)
1012 * @m: descriptor for message info
1013 * @buf_len: total size of user buffer area
1014 * @flags: receive flags
1016 * Used for SOCK_STREAM messages only. If not enough data is available
1017 * will optionally wait for more; never truncates data.
1019 * Returns size of returned message data, errno otherwise
1022 static int recv_stream(struct kiocb *iocb, struct socket *sock,
1023 struct msghdr *m, size_t buf_len, int flags)
1025 struct sock *sk = sock->sk;
1026 struct tipc_port *tport = tipc_sk_port(sk);
1027 struct sk_buff *buf;
1028 struct tipc_msg *msg;
1029 unsigned int sz;
1030 int sz_to_copy;
1031 int sz_copied = 0;
1032 int needed;
1033 char __user *crs = m->msg_iov->iov_base;
1034 unsigned char *buf_crs;
1035 u32 err;
1036 int res = 0;
1038 /* Catch invalid receive attempts */
1040 if (m->msg_iovlen != 1)
1041 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
1043 if (unlikely(!buf_len))
1044 return -EINVAL;
1046 lock_sock(sk);
1048 if (unlikely((sock->state == SS_UNCONNECTED) ||
1049 (sock->state == SS_CONNECTING))) {
1050 res = -ENOTCONN;
1051 goto exit;
1054 restart:
1056 /* Look for a message in receive queue; wait if necessary */
1058 while (skb_queue_empty(&sk->sk_receive_queue)) {
1059 if (sock->state == SS_DISCONNECTING) {
1060 res = -ENOTCONN;
1061 goto exit;
1063 if (flags & MSG_DONTWAIT) {
1064 res = -EWOULDBLOCK;
1065 goto exit;
1067 release_sock(sk);
1068 res = wait_event_interruptible(*sk_sleep(sk),
1069 (!skb_queue_empty(&sk->sk_receive_queue) ||
1070 (sock->state == SS_DISCONNECTING)));
1071 lock_sock(sk);
1072 if (res)
1073 goto exit;
1076 /* Look at first message in receive queue */
1078 buf = skb_peek(&sk->sk_receive_queue);
1079 msg = buf_msg(buf);
1080 sz = msg_data_sz(msg);
1081 err = msg_errcode(msg);
1083 /* Discard an empty non-errored message & try again */
1085 if ((!sz) && (!err)) {
1086 advance_rx_queue(sk);
1087 goto restart;
1090 /* Optionally capture sender's address & ancillary data of first msg */
1092 if (sz_copied == 0) {
1093 set_orig_addr(m, msg);
1094 res = anc_data_recv(m, msg, tport);
1095 if (res)
1096 goto exit;
1099 /* Capture message data (if valid) & compute return value (always) */
1101 if (!err) {
1102 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
1103 sz = (unsigned char *)msg + msg_size(msg) - buf_crs;
1105 needed = (buf_len - sz_copied);
1106 sz_to_copy = (sz <= needed) ? sz : needed;
1107 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1108 res = -EFAULT;
1109 goto exit;
1111 sz_copied += sz_to_copy;
1113 if (sz_to_copy < sz) {
1114 if (!(flags & MSG_PEEK))
1115 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1116 goto exit;
1119 crs += sz_to_copy;
1120 } else {
1121 if (sz_copied != 0)
1122 goto exit; /* can't add error msg to valid data */
1124 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1125 res = 0;
1126 else
1127 res = -ECONNRESET;
1130 /* Consume received message (optional) */
1132 if (likely(!(flags & MSG_PEEK))) {
1133 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1134 tipc_acknowledge(tport->ref, tport->conn_unacked);
1135 advance_rx_queue(sk);
1138 /* Loop around if more data is required */
1140 if ((sz_copied < buf_len) && /* didn't get all requested data */
1141 (!skb_queue_empty(&sk->sk_receive_queue) ||
1142 (flags & MSG_WAITALL)) && /* and more is ready or required */
1143 (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */
1144 (!err)) /* and haven't reached a FIN */
1145 goto restart;
1147 exit:
1148 release_sock(sk);
1149 return sz_copied ? sz_copied : res;
1153 * rx_queue_full - determine if receive queue can accept another message
1154 * @msg: message to be added to queue
1155 * @queue_size: current size of queue
1156 * @base: nominal maximum size of queue
1158 * Returns 1 if queue is unable to accept message, 0 otherwise
1161 static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
1163 u32 threshold;
1164 u32 imp = msg_importance(msg);
1166 if (imp == TIPC_LOW_IMPORTANCE)
1167 threshold = base;
1168 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1169 threshold = base * 2;
1170 else if (imp == TIPC_HIGH_IMPORTANCE)
1171 threshold = base * 100;
1172 else
1173 return 0;
1175 if (msg_connected(msg))
1176 threshold *= 4;
1178 return (queue_size >= threshold);
1182 * filter_rcv - validate incoming message
1183 * @sk: socket
1184 * @buf: message
1186 * Enqueues message on receive queue if acceptable; optionally handles
1187 * disconnect indication for a connected socket.
1189 * Called with socket lock already taken; port lock may also be taken.
1191 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1194 static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
1196 struct socket *sock = sk->sk_socket;
1197 struct tipc_msg *msg = buf_msg(buf);
1198 u32 recv_q_len;
1200 /* Reject message if it is wrong sort of message for socket */
1203 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1204 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1205 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1208 if (sock->state == SS_READY) {
1209 if (msg_connected(msg)) {
1210 msg_dbg(msg, "dispatch filter 1\n");
1211 return TIPC_ERR_NO_PORT;
1213 } else {
1214 if (msg_mcast(msg)) {
1215 msg_dbg(msg, "dispatch filter 2\n");
1216 return TIPC_ERR_NO_PORT;
1218 if (sock->state == SS_CONNECTED) {
1219 if (!msg_connected(msg)) {
1220 msg_dbg(msg, "dispatch filter 3\n");
1221 return TIPC_ERR_NO_PORT;
1224 else if (sock->state == SS_CONNECTING) {
1225 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1226 msg_dbg(msg, "dispatch filter 4\n");
1227 return TIPC_ERR_NO_PORT;
1230 else if (sock->state == SS_LISTENING) {
1231 if (msg_connected(msg) || msg_errcode(msg)) {
1232 msg_dbg(msg, "dispatch filter 5\n");
1233 return TIPC_ERR_NO_PORT;
1236 else if (sock->state == SS_DISCONNECTING) {
1237 msg_dbg(msg, "dispatch filter 6\n");
1238 return TIPC_ERR_NO_PORT;
1240 else /* (sock->state == SS_UNCONNECTED) */ {
1241 if (msg_connected(msg) || msg_errcode(msg)) {
1242 msg_dbg(msg, "dispatch filter 7\n");
1243 return TIPC_ERR_NO_PORT;
1248 /* Reject message if there isn't room to queue it */
1250 recv_q_len = (u32)atomic_read(&tipc_queue_size);
1251 if (unlikely(recv_q_len >= OVERLOAD_LIMIT_BASE)) {
1252 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE))
1253 return TIPC_ERR_OVERLOAD;
1255 recv_q_len = skb_queue_len(&sk->sk_receive_queue);
1256 if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
1257 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
1258 return TIPC_ERR_OVERLOAD;
1261 /* Enqueue message (finally!) */
1263 msg_dbg(msg, "<DISP<: ");
1264 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1265 atomic_inc(&tipc_queue_size);
1266 __skb_queue_tail(&sk->sk_receive_queue, buf);
1268 /* Initiate connection termination for an incoming 'FIN' */
1270 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1271 sock->state = SS_DISCONNECTING;
1272 tipc_disconnect_port(tipc_sk_port(sk));
1275 if (waitqueue_active(sk_sleep(sk)))
1276 wake_up_interruptible(sk_sleep(sk));
1277 return TIPC_OK;
1281 * backlog_rcv - handle incoming message from backlog queue
1282 * @sk: socket
1283 * @buf: message
1285 * Caller must hold socket lock, but not port lock.
1287 * Returns 0
1290 static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1292 u32 res;
1294 res = filter_rcv(sk, buf);
1295 if (res)
1296 tipc_reject_msg(buf, res);
1297 return 0;
1301 * dispatch - handle incoming message
1302 * @tport: TIPC port that received message
1303 * @buf: message
1305 * Called with port lock already taken.
1307 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1310 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1312 struct sock *sk = (struct sock *)tport->usr_handle;
1313 u32 res;
1316 * Process message if socket is unlocked; otherwise add to backlog queue
1318 * This code is based on sk_receive_skb(), but must be distinct from it
1319 * since a TIPC-specific filter/reject mechanism is utilized
1322 bh_lock_sock(sk);
1323 if (!sock_owned_by_user(sk)) {
1324 res = filter_rcv(sk, buf);
1325 } else {
1326 if (sk_add_backlog(sk, buf))
1327 res = TIPC_ERR_OVERLOAD;
1328 else
1329 res = TIPC_OK;
1331 bh_unlock_sock(sk);
1333 return res;
1337 * wakeupdispatch - wake up port after congestion
1338 * @tport: port to wakeup
1340 * Called with port lock already taken.
1343 static void wakeupdispatch(struct tipc_port *tport)
1345 struct sock *sk = (struct sock *)tport->usr_handle;
1347 if (waitqueue_active(sk_sleep(sk)))
1348 wake_up_interruptible(sk_sleep(sk));
1352 * connect - establish a connection to another TIPC port
1353 * @sock: socket structure
1354 * @dest: socket address for destination port
1355 * @destlen: size of socket address data structure
1356 * @flags: file-related flags associated with socket
1358 * Returns 0 on success, errno otherwise
1361 static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
1362 int flags)
1364 struct sock *sk = sock->sk;
1365 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1366 struct msghdr m = {NULL,};
1367 struct sk_buff *buf;
1368 struct tipc_msg *msg;
1369 int res;
1371 lock_sock(sk);
1373 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
1375 if (sock->state == SS_READY) {
1376 res = -EOPNOTSUPP;
1377 goto exit;
1380 /* For now, TIPC does not support the non-blocking form of connect() */
1382 if (flags & O_NONBLOCK) {
1383 res = -EWOULDBLOCK;
1384 goto exit;
1387 /* Issue Posix-compliant error code if socket is in the wrong state */
1389 if (sock->state == SS_LISTENING) {
1390 res = -EOPNOTSUPP;
1391 goto exit;
1393 if (sock->state == SS_CONNECTING) {
1394 res = -EALREADY;
1395 goto exit;
1397 if (sock->state != SS_UNCONNECTED) {
1398 res = -EISCONN;
1399 goto exit;
1403 * Reject connection attempt using multicast address
1405 * Note: send_msg() validates the rest of the address fields,
1406 * so there's no need to do it here
1409 if (dst->addrtype == TIPC_ADDR_MCAST) {
1410 res = -EINVAL;
1411 goto exit;
1414 /* Reject any messages already in receive queue (very unlikely) */
1416 reject_rx_queue(sk);
1418 /* Send a 'SYN-' to destination */
1420 m.msg_name = dest;
1421 m.msg_namelen = destlen;
1422 res = send_msg(NULL, sock, &m, 0);
1423 if (res < 0) {
1424 goto exit;
1427 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1429 release_sock(sk);
1430 res = wait_event_interruptible_timeout(*sk_sleep(sk),
1431 (!skb_queue_empty(&sk->sk_receive_queue) ||
1432 (sock->state != SS_CONNECTING)),
1433 sk->sk_rcvtimeo);
1434 lock_sock(sk);
1436 if (res > 0) {
1437 buf = skb_peek(&sk->sk_receive_queue);
1438 if (buf != NULL) {
1439 msg = buf_msg(buf);
1440 res = auto_connect(sock, msg);
1441 if (!res) {
1442 if (!msg_data_sz(msg))
1443 advance_rx_queue(sk);
1445 } else {
1446 if (sock->state == SS_CONNECTED) {
1447 res = -EISCONN;
1448 } else {
1449 res = -ECONNREFUSED;
1452 } else {
1453 if (res == 0)
1454 res = -ETIMEDOUT;
1455 else
1456 ; /* leave "res" unchanged */
1457 sock->state = SS_DISCONNECTING;
1460 exit:
1461 release_sock(sk);
1462 return res;
1466 * listen - allow socket to listen for incoming connections
1467 * @sock: socket structure
1468 * @len: (unused)
1470 * Returns 0 on success, errno otherwise
1473 static int listen(struct socket *sock, int len)
1475 struct sock *sk = sock->sk;
1476 int res;
1478 lock_sock(sk);
1480 if (sock->state == SS_READY)
1481 res = -EOPNOTSUPP;
1482 else if (sock->state != SS_UNCONNECTED)
1483 res = -EINVAL;
1484 else {
1485 sock->state = SS_LISTENING;
1486 res = 0;
1489 release_sock(sk);
1490 return res;
1494 * accept - wait for connection request
1495 * @sock: listening socket
1496 * @newsock: new socket that is to be connected
1497 * @flags: file-related flags associated with socket
1499 * Returns 0 on success, errno otherwise
1502 static int accept(struct socket *sock, struct socket *new_sock, int flags)
1504 struct sock *sk = sock->sk;
1505 struct sk_buff *buf;
1506 int res;
1508 lock_sock(sk);
1510 if (sock->state == SS_READY) {
1511 res = -EOPNOTSUPP;
1512 goto exit;
1514 if (sock->state != SS_LISTENING) {
1515 res = -EINVAL;
1516 goto exit;
1519 while (skb_queue_empty(&sk->sk_receive_queue)) {
1520 if (flags & O_NONBLOCK) {
1521 res = -EWOULDBLOCK;
1522 goto exit;
1524 release_sock(sk);
1525 res = wait_event_interruptible(*sk_sleep(sk),
1526 (!skb_queue_empty(&sk->sk_receive_queue)));
1527 lock_sock(sk);
1528 if (res)
1529 goto exit;
1532 buf = skb_peek(&sk->sk_receive_queue);
1534 res = tipc_create(sock_net(sock->sk), new_sock, 0, 0);
1535 if (!res) {
1536 struct sock *new_sk = new_sock->sk;
1537 struct tipc_sock *new_tsock = tipc_sk(new_sk);
1538 struct tipc_port *new_tport = new_tsock->p;
1539 u32 new_ref = new_tport->ref;
1540 struct tipc_msg *msg = buf_msg(buf);
1542 lock_sock(new_sk);
1545 * Reject any stray messages received by new socket
1546 * before the socket lock was taken (very, very unlikely)
1549 reject_rx_queue(new_sk);
1551 /* Connect new socket to it's peer */
1553 new_tsock->peer_name.ref = msg_origport(msg);
1554 new_tsock->peer_name.node = msg_orignode(msg);
1555 tipc_connect2port(new_ref, &new_tsock->peer_name);
1556 new_sock->state = SS_CONNECTED;
1558 tipc_set_portimportance(new_ref, msg_importance(msg));
1559 if (msg_named(msg)) {
1560 new_tport->conn_type = msg_nametype(msg);
1561 new_tport->conn_instance = msg_nameinst(msg);
1565 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1566 * Respond to 'SYN+' by queuing it on new socket.
1569 msg_dbg(msg,"<ACC<: ");
1570 if (!msg_data_sz(msg)) {
1571 struct msghdr m = {NULL,};
1573 advance_rx_queue(sk);
1574 send_packet(NULL, new_sock, &m, 0);
1575 } else {
1576 __skb_dequeue(&sk->sk_receive_queue);
1577 __skb_queue_head(&new_sk->sk_receive_queue, buf);
1579 release_sock(new_sk);
1581 exit:
1582 release_sock(sk);
1583 return res;
1587 * shutdown - shutdown socket connection
1588 * @sock: socket structure
1589 * @how: direction to close (must be SHUT_RDWR)
1591 * Terminates connection (if necessary), then purges socket's receive queue.
1593 * Returns 0 on success, errno otherwise
1596 static int shutdown(struct socket *sock, int how)
1598 struct sock *sk = sock->sk;
1599 struct tipc_port *tport = tipc_sk_port(sk);
1600 struct sk_buff *buf;
1601 int res;
1603 if (how != SHUT_RDWR)
1604 return -EINVAL;
1606 lock_sock(sk);
1608 switch (sock->state) {
1609 case SS_CONNECTING:
1610 case SS_CONNECTED:
1612 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
1613 restart:
1614 buf = __skb_dequeue(&sk->sk_receive_queue);
1615 if (buf) {
1616 atomic_dec(&tipc_queue_size);
1617 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1618 buf_discard(buf);
1619 goto restart;
1621 tipc_disconnect(tport->ref);
1622 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
1623 } else {
1624 tipc_shutdown(tport->ref);
1627 sock->state = SS_DISCONNECTING;
1629 /* fall through */
1631 case SS_DISCONNECTING:
1633 /* Discard any unreceived messages; wake up sleeping tasks */
1635 discard_rx_queue(sk);
1636 if (waitqueue_active(sk_sleep(sk)))
1637 wake_up_interruptible(sk_sleep(sk));
1638 res = 0;
1639 break;
1641 default:
1642 res = -ENOTCONN;
1645 release_sock(sk);
1646 return res;
1650 * setsockopt - set socket option
1651 * @sock: socket structure
1652 * @lvl: option level
1653 * @opt: option identifier
1654 * @ov: pointer to new option value
1655 * @ol: length of option value
1657 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1658 * (to ease compatibility).
1660 * Returns 0 on success, errno otherwise
1663 static int setsockopt(struct socket *sock,
1664 int lvl, int opt, char __user *ov, unsigned int ol)
1666 struct sock *sk = sock->sk;
1667 struct tipc_port *tport = tipc_sk_port(sk);
1668 u32 value;
1669 int res;
1671 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1672 return 0;
1673 if (lvl != SOL_TIPC)
1674 return -ENOPROTOOPT;
1675 if (ol < sizeof(value))
1676 return -EINVAL;
1677 if ((res = get_user(value, (u32 __user *)ov)))
1678 return res;
1680 lock_sock(sk);
1682 switch (opt) {
1683 case TIPC_IMPORTANCE:
1684 res = tipc_set_portimportance(tport->ref, value);
1685 break;
1686 case TIPC_SRC_DROPPABLE:
1687 if (sock->type != SOCK_STREAM)
1688 res = tipc_set_portunreliable(tport->ref, value);
1689 else
1690 res = -ENOPROTOOPT;
1691 break;
1692 case TIPC_DEST_DROPPABLE:
1693 res = tipc_set_portunreturnable(tport->ref, value);
1694 break;
1695 case TIPC_CONN_TIMEOUT:
1696 sk->sk_rcvtimeo = msecs_to_jiffies(value);
1697 /* no need to set "res", since already 0 at this point */
1698 break;
1699 default:
1700 res = -EINVAL;
1703 release_sock(sk);
1705 return res;
1709 * getsockopt - get socket option
1710 * @sock: socket structure
1711 * @lvl: option level
1712 * @opt: option identifier
1713 * @ov: receptacle for option value
1714 * @ol: receptacle for length of option value
1716 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1717 * (to ease compatibility).
1719 * Returns 0 on success, errno otherwise
1722 static int getsockopt(struct socket *sock,
1723 int lvl, int opt, char __user *ov, int __user *ol)
1725 struct sock *sk = sock->sk;
1726 struct tipc_port *tport = tipc_sk_port(sk);
1727 int len;
1728 u32 value;
1729 int res;
1731 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1732 return put_user(0, ol);
1733 if (lvl != SOL_TIPC)
1734 return -ENOPROTOOPT;
1735 if ((res = get_user(len, ol)))
1736 return res;
1738 lock_sock(sk);
1740 switch (opt) {
1741 case TIPC_IMPORTANCE:
1742 res = tipc_portimportance(tport->ref, &value);
1743 break;
1744 case TIPC_SRC_DROPPABLE:
1745 res = tipc_portunreliable(tport->ref, &value);
1746 break;
1747 case TIPC_DEST_DROPPABLE:
1748 res = tipc_portunreturnable(tport->ref, &value);
1749 break;
1750 case TIPC_CONN_TIMEOUT:
1751 value = jiffies_to_msecs(sk->sk_rcvtimeo);
1752 /* no need to set "res", since already 0 at this point */
1753 break;
1754 case TIPC_NODE_RECVQ_DEPTH:
1755 value = (u32)atomic_read(&tipc_queue_size);
1756 break;
1757 case TIPC_SOCK_RECVQ_DEPTH:
1758 value = skb_queue_len(&sk->sk_receive_queue);
1759 break;
1760 default:
1761 res = -EINVAL;
1764 release_sock(sk);
1766 if (res) {
1767 /* "get" failed */
1769 else if (len < sizeof(value)) {
1770 res = -EINVAL;
1772 else if (copy_to_user(ov, &value, sizeof(value))) {
1773 res = -EFAULT;
1775 else {
1776 res = put_user(sizeof(value), ol);
1779 return res;
1783 * Protocol switches for the various types of TIPC sockets
1786 static const struct proto_ops msg_ops = {
1787 .owner = THIS_MODULE,
1788 .family = AF_TIPC,
1789 .release = release,
1790 .bind = bind,
1791 .connect = connect,
1792 .socketpair = sock_no_socketpair,
1793 .accept = accept,
1794 .getname = get_name,
1795 .poll = poll,
1796 .ioctl = sock_no_ioctl,
1797 .listen = listen,
1798 .shutdown = shutdown,
1799 .setsockopt = setsockopt,
1800 .getsockopt = getsockopt,
1801 .sendmsg = send_msg,
1802 .recvmsg = recv_msg,
1803 .mmap = sock_no_mmap,
1804 .sendpage = sock_no_sendpage
1807 static const struct proto_ops packet_ops = {
1808 .owner = THIS_MODULE,
1809 .family = AF_TIPC,
1810 .release = release,
1811 .bind = bind,
1812 .connect = connect,
1813 .socketpair = sock_no_socketpair,
1814 .accept = accept,
1815 .getname = get_name,
1816 .poll = poll,
1817 .ioctl = sock_no_ioctl,
1818 .listen = listen,
1819 .shutdown = shutdown,
1820 .setsockopt = setsockopt,
1821 .getsockopt = getsockopt,
1822 .sendmsg = send_packet,
1823 .recvmsg = recv_msg,
1824 .mmap = sock_no_mmap,
1825 .sendpage = sock_no_sendpage
1828 static const struct proto_ops stream_ops = {
1829 .owner = THIS_MODULE,
1830 .family = AF_TIPC,
1831 .release = release,
1832 .bind = bind,
1833 .connect = connect,
1834 .socketpair = sock_no_socketpair,
1835 .accept = accept,
1836 .getname = get_name,
1837 .poll = poll,
1838 .ioctl = sock_no_ioctl,
1839 .listen = listen,
1840 .shutdown = shutdown,
1841 .setsockopt = setsockopt,
1842 .getsockopt = getsockopt,
1843 .sendmsg = send_stream,
1844 .recvmsg = recv_stream,
1845 .mmap = sock_no_mmap,
1846 .sendpage = sock_no_sendpage
1849 static const struct net_proto_family tipc_family_ops = {
1850 .owner = THIS_MODULE,
1851 .family = AF_TIPC,
1852 .create = tipc_create
1855 static struct proto tipc_proto = {
1856 .name = "TIPC",
1857 .owner = THIS_MODULE,
1858 .obj_size = sizeof(struct tipc_sock)
1862 * tipc_socket_init - initialize TIPC socket interface
1864 * Returns 0 on success, errno otherwise
1866 int tipc_socket_init(void)
1868 int res;
1870 res = proto_register(&tipc_proto, 1);
1871 if (res) {
1872 err("Failed to register TIPC protocol type\n");
1873 goto out;
1876 res = sock_register(&tipc_family_ops);
1877 if (res) {
1878 err("Failed to register TIPC socket type\n");
1879 proto_unregister(&tipc_proto);
1880 goto out;
1883 sockets_enabled = 1;
1884 out:
1885 return res;
1889 * tipc_socket_stop - stop TIPC socket interface
1892 void tipc_socket_stop(void)
1894 if (!sockets_enabled)
1895 return;
1897 sockets_enabled = 0;
1898 sock_unregister(tipc_family_ops.family);
1899 proto_unregister(&tipc_proto);