[PATCH] Remove #if 0 and other long dead code from rio_tty
[usb.git] / net / tipc / socket.c
blobd21f8c0cd25a2e7ec9c0d688db0fb067db471978
1 /*
2 * net/tipc/socket.c: TIPC socket API
3 *
4 * Copyright (c) 2001-2006, Ericsson AB
5 * Copyright (c) 2004-2005, 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/slab.h>
44 #include <linux/poll.h>
45 #include <linux/version.h>
46 #include <linux/fcntl.h>
47 #include <linux/version.h>
48 #include <asm/semaphore.h>
49 #include <asm/string.h>
50 #include <asm/atomic.h>
51 #include <net/sock.h>
53 #include <linux/tipc.h>
54 #include <linux/tipc_config.h>
55 #include <net/tipc/tipc_msg.h>
56 #include <net/tipc/tipc_port.h>
58 #include "core.h"
60 #define SS_LISTENING -1 /* socket is listening */
61 #define SS_READY -2 /* socket is connectionless */
63 #define OVERLOAD_LIMIT_BASE 5000
65 struct tipc_sock {
66 struct sock sk;
67 struct tipc_port *p;
68 struct semaphore sem;
71 #define tipc_sk(sk) ((struct tipc_sock*)sk)
73 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
74 static void wakeupdispatch(struct tipc_port *tport);
76 static struct proto_ops packet_ops;
77 static struct proto_ops stream_ops;
78 static 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 /*
88 * sock_lock(): Lock a port/socket pair. lock_sock() can
89 * not be used here, since the same lock must protect ports
90 * with non-socket interfaces.
91 * See net.c for description of locking policy.
93 static inline void sock_lock(struct tipc_sock* tsock)
95 spin_lock_bh(tsock->p->lock);
98 /*
99 * sock_unlock(): Unlock a port/socket pair
101 static inline void sock_unlock(struct tipc_sock* tsock)
103 spin_unlock_bh(tsock->p->lock);
107 * pollmask - determine the current set of poll() events for a socket
108 * @sock: socket structure
110 * TIPC sets the returned events as follows:
111 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
112 * or if a connection-oriented socket is does not have an active connection
113 * (i.e. a read operation will not block).
114 * b) POLLOUT is set except when a socket's connection has been terminated
115 * (i.e. a write operation will not block).
116 * c) POLLHUP is set when a socket's connection has been terminated.
118 * IMPORTANT: The fact that a read or write operation will not block does NOT
119 * imply that the operation will succeed!
121 * Returns pollmask value
124 static inline u32 pollmask(struct socket *sock)
126 u32 mask;
128 if ((skb_queue_len(&sock->sk->sk_receive_queue) != 0) ||
129 (sock->state == SS_UNCONNECTED) ||
130 (sock->state == SS_DISCONNECTING))
131 mask = (POLLRDNORM | POLLIN);
132 else
133 mask = 0;
135 if (sock->state == SS_DISCONNECTING)
136 mask |= POLLHUP;
137 else
138 mask |= POLLOUT;
140 return mask;
145 * advance_queue - discard first buffer in queue
146 * @tsock: TIPC socket
149 static inline void advance_queue(struct tipc_sock *tsock)
151 sock_lock(tsock);
152 buf_discard(skb_dequeue(&tsock->sk.sk_receive_queue));
153 sock_unlock(tsock);
154 atomic_dec(&tipc_queue_size);
158 * tipc_create - create a TIPC socket
159 * @sock: pre-allocated socket structure
160 * @protocol: protocol indicator (must be 0)
162 * This routine creates and attaches a 'struct sock' to the 'struct socket',
163 * then create and attaches a TIPC port to the 'struct sock' part.
165 * Returns 0 on success, errno otherwise
167 static int tipc_create(struct socket *sock, int protocol)
169 struct tipc_sock *tsock;
170 struct tipc_port *port;
171 struct sock *sk;
172 u32 ref;
174 if ((sock->type != SOCK_STREAM) &&
175 (sock->type != SOCK_SEQPACKET) &&
176 (sock->type != SOCK_DGRAM) &&
177 (sock->type != SOCK_RDM))
178 return -EPROTOTYPE;
180 if (unlikely(protocol != 0))
181 return -EPROTONOSUPPORT;
183 ref = tipc_createport_raw(0, &dispatch, &wakeupdispatch, TIPC_LOW_IMPORTANCE);
184 if (unlikely(!ref))
185 return -ENOMEM;
187 sock->state = SS_UNCONNECTED;
189 switch (sock->type) {
190 case SOCK_STREAM:
191 sock->ops = &stream_ops;
192 break;
193 case SOCK_SEQPACKET:
194 sock->ops = &packet_ops;
195 break;
196 case SOCK_DGRAM:
197 tipc_set_portunreliable(ref, 1);
198 /* fall through */
199 case SOCK_RDM:
200 tipc_set_portunreturnable(ref, 1);
201 sock->ops = &msg_ops;
202 sock->state = SS_READY;
203 break;
206 sk = sk_alloc(AF_TIPC, GFP_KERNEL, &tipc_proto, 1);
207 if (!sk) {
208 tipc_deleteport(ref);
209 return -ENOMEM;
212 sock_init_data(sock, sk);
213 init_waitqueue_head(sk->sk_sleep);
214 sk->sk_rcvtimeo = 8 * HZ; /* default connect timeout = 8s */
216 tsock = tipc_sk(sk);
217 port = tipc_get_port(ref);
219 tsock->p = port;
220 port->usr_handle = tsock;
222 init_MUTEX(&tsock->sem);
224 dbg("sock_create: %x\n",tsock);
226 atomic_inc(&tipc_user_count);
228 return 0;
232 * release - destroy a TIPC socket
233 * @sock: socket to destroy
235 * This routine cleans up any messages that are still queued on the socket.
236 * For DGRAM and RDM socket types, all queued messages are rejected.
237 * For SEQPACKET and STREAM socket types, the first message is rejected
238 * and any others are discarded. (If the first message on a STREAM socket
239 * is partially-read, it is discarded and the next one is rejected instead.)
241 * NOTE: Rejected messages are not necessarily returned to the sender! They
242 * are returned or discarded according to the "destination droppable" setting
243 * specified for the message by the sender.
245 * Returns 0 on success, errno otherwise
248 static int release(struct socket *sock)
250 struct tipc_sock *tsock = tipc_sk(sock->sk);
251 struct sock *sk = sock->sk;
252 int res = TIPC_OK;
253 struct sk_buff *buf;
255 dbg("sock_delete: %x\n",tsock);
256 if (!tsock)
257 return 0;
258 down_interruptible(&tsock->sem);
259 if (!sock->sk) {
260 up(&tsock->sem);
261 return 0;
264 /* Reject unreceived messages, unless no longer connected */
266 while (sock->state != SS_DISCONNECTING) {
267 sock_lock(tsock);
268 buf = skb_dequeue(&sk->sk_receive_queue);
269 if (!buf)
270 tsock->p->usr_handle = 0;
271 sock_unlock(tsock);
272 if (!buf)
273 break;
274 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
275 buf_discard(buf);
276 else
277 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
278 atomic_dec(&tipc_queue_size);
281 /* Delete TIPC port */
283 res = tipc_deleteport(tsock->p->ref);
284 sock->sk = NULL;
286 /* Discard any remaining messages */
288 while ((buf = skb_dequeue(&sk->sk_receive_queue))) {
289 buf_discard(buf);
290 atomic_dec(&tipc_queue_size);
293 up(&tsock->sem);
295 sock_put(sk);
297 atomic_dec(&tipc_user_count);
298 return res;
302 * bind - associate or disassocate TIPC name(s) with a socket
303 * @sock: socket structure
304 * @uaddr: socket address describing name(s) and desired operation
305 * @uaddr_len: size of socket address data structure
307 * Name and name sequence binding is indicated using a positive scope value;
308 * a negative scope value unbinds the specified name. Specifying no name
309 * (i.e. a socket address length of 0) unbinds all names from the socket.
311 * Returns 0 on success, errno otherwise
314 static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
316 struct tipc_sock *tsock = tipc_sk(sock->sk);
317 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
318 int res;
320 if (down_interruptible(&tsock->sem))
321 return -ERESTARTSYS;
323 if (unlikely(!uaddr_len)) {
324 res = tipc_withdraw(tsock->p->ref, 0, 0);
325 goto exit;
328 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
329 res = -EINVAL;
330 goto exit;
333 if (addr->family != AF_TIPC) {
334 res = -EAFNOSUPPORT;
335 goto exit;
337 if (addr->addrtype == TIPC_ADDR_NAME)
338 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
339 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
340 res = -EAFNOSUPPORT;
341 goto exit;
344 if (addr->scope > 0)
345 res = tipc_publish(tsock->p->ref, addr->scope,
346 &addr->addr.nameseq);
347 else
348 res = tipc_withdraw(tsock->p->ref, -addr->scope,
349 &addr->addr.nameseq);
350 exit:
351 up(&tsock->sem);
352 return res;
355 /**
356 * get_name - get port ID of socket or peer socket
357 * @sock: socket structure
358 * @uaddr: area for returned socket address
359 * @uaddr_len: area for returned length of socket address
360 * @peer: 0 to obtain socket name, 1 to obtain peer socket name
362 * Returns 0 on success, errno otherwise
365 static int get_name(struct socket *sock, struct sockaddr *uaddr,
366 int *uaddr_len, int peer)
368 struct tipc_sock *tsock = tipc_sk(sock->sk);
369 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
370 u32 res;
372 if (down_interruptible(&tsock->sem))
373 return -ERESTARTSYS;
375 *uaddr_len = sizeof(*addr);
376 addr->addrtype = TIPC_ADDR_ID;
377 addr->family = AF_TIPC;
378 addr->scope = 0;
379 if (peer)
380 res = tipc_peer(tsock->p->ref, &addr->addr.id);
381 else
382 res = tipc_ownidentity(tsock->p->ref, &addr->addr.id);
383 addr->addr.name.domain = 0;
385 up(&tsock->sem);
386 return res;
390 * poll - read and possibly block on pollmask
391 * @file: file structure associated with the socket
392 * @sock: socket for which to calculate the poll bits
393 * @wait: ???
395 * Returns the pollmask
398 static unsigned int poll(struct file *file, struct socket *sock,
399 poll_table *wait)
401 poll_wait(file, sock->sk->sk_sleep, wait);
402 /* NEED LOCK HERE? */
403 return pollmask(sock);
406 /**
407 * dest_name_check - verify user is permitted to send to specified port name
408 * @dest: destination address
409 * @m: descriptor for message to be sent
411 * Prevents restricted configuration commands from being issued by
412 * unauthorized users.
414 * Returns 0 if permission is granted, otherwise errno
417 static inline int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
419 struct tipc_cfg_msg_hdr hdr;
421 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
422 return 0;
423 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
424 return 0;
426 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
427 return -EACCES;
429 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
430 return -EFAULT;
431 if ((ntohs(hdr.tcm_type) & 0xC000) & (!capable(CAP_NET_ADMIN)))
432 return -EACCES;
434 return 0;
438 * send_msg - send message in connectionless manner
439 * @iocb: (unused)
440 * @sock: socket structure
441 * @m: message to send
442 * @total_len: (unused)
444 * Message must have an destination specified explicitly.
445 * Used for SOCK_RDM and SOCK_DGRAM messages,
446 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
447 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
449 * Returns the number of bytes sent on success, or errno otherwise
452 static int send_msg(struct kiocb *iocb, struct socket *sock,
453 struct msghdr *m, size_t total_len)
455 struct tipc_sock *tsock = tipc_sk(sock->sk);
456 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
457 struct sk_buff *buf;
458 int needs_conn;
459 int res = -EINVAL;
461 if (unlikely(!dest))
462 return -EDESTADDRREQ;
463 if (unlikely(dest->family != AF_TIPC))
464 return -EINVAL;
466 needs_conn = (sock->state != SS_READY);
467 if (unlikely(needs_conn)) {
468 if (sock->state == SS_LISTENING)
469 return -EPIPE;
470 if (sock->state != SS_UNCONNECTED)
471 return -EISCONN;
472 if ((tsock->p->published) ||
473 ((sock->type == SOCK_STREAM) && (total_len != 0)))
474 return -EOPNOTSUPP;
477 if (down_interruptible(&tsock->sem))
478 return -ERESTARTSYS;
480 if (needs_conn) {
482 /* Abort any pending connection attempts (very unlikely) */
484 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
485 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
486 atomic_dec(&tipc_queue_size);
489 sock->state = SS_CONNECTING;
492 do {
493 if (dest->addrtype == TIPC_ADDR_NAME) {
494 if ((res = dest_name_check(dest, m)))
495 goto exit;
496 res = tipc_send2name(tsock->p->ref,
497 &dest->addr.name.name,
498 dest->addr.name.domain,
499 m->msg_iovlen,
500 m->msg_iov);
502 else if (dest->addrtype == TIPC_ADDR_ID) {
503 res = tipc_send2port(tsock->p->ref,
504 &dest->addr.id,
505 m->msg_iovlen,
506 m->msg_iov);
508 else if (dest->addrtype == TIPC_ADDR_MCAST) {
509 if (needs_conn) {
510 res = -EOPNOTSUPP;
511 goto exit;
513 if ((res = dest_name_check(dest, m)))
514 goto exit;
515 res = tipc_multicast(tsock->p->ref,
516 &dest->addr.nameseq,
518 m->msg_iovlen,
519 m->msg_iov);
521 if (likely(res != -ELINKCONG)) {
522 exit:
523 up(&tsock->sem);
524 return res;
526 if (m->msg_flags & MSG_DONTWAIT) {
527 res = -EWOULDBLOCK;
528 goto exit;
530 if (wait_event_interruptible(*sock->sk->sk_sleep,
531 !tsock->p->congested)) {
532 res = -ERESTARTSYS;
533 goto exit;
535 } while (1);
538 /**
539 * send_packet - send a connection-oriented message
540 * @iocb: (unused)
541 * @sock: socket structure
542 * @m: message to send
543 * @total_len: (unused)
545 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
547 * Returns the number of bytes sent on success, or errno otherwise
550 static int send_packet(struct kiocb *iocb, struct socket *sock,
551 struct msghdr *m, size_t total_len)
553 struct tipc_sock *tsock = tipc_sk(sock->sk);
554 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
555 int res;
557 /* Handle implied connection establishment */
559 if (unlikely(dest))
560 return send_msg(iocb, sock, m, total_len);
562 if (down_interruptible(&tsock->sem)) {
563 return -ERESTARTSYS;
566 if (unlikely(sock->state != SS_CONNECTED)) {
567 if (sock->state == SS_DISCONNECTING)
568 res = -EPIPE;
569 else
570 res = -ENOTCONN;
571 goto exit;
574 do {
575 res = tipc_send(tsock->p->ref, m->msg_iovlen, m->msg_iov);
576 if (likely(res != -ELINKCONG)) {
577 exit:
578 up(&tsock->sem);
579 return res;
581 if (m->msg_flags & MSG_DONTWAIT) {
582 res = -EWOULDBLOCK;
583 goto exit;
585 if (wait_event_interruptible(*sock->sk->sk_sleep,
586 !tsock->p->congested)) {
587 res = -ERESTARTSYS;
588 goto exit;
590 } while (1);
593 /**
594 * send_stream - send stream-oriented data
595 * @iocb: (unused)
596 * @sock: socket structure
597 * @m: data to send
598 * @total_len: total length of data to be sent
600 * Used for SOCK_STREAM data.
602 * Returns the number of bytes sent on success, or errno otherwise
606 static int send_stream(struct kiocb *iocb, struct socket *sock,
607 struct msghdr *m, size_t total_len)
609 struct msghdr my_msg;
610 struct iovec my_iov;
611 struct iovec *curr_iov;
612 int curr_iovlen;
613 char __user *curr_start;
614 int curr_left;
615 int bytes_to_send;
616 int res;
618 if (likely(total_len <= TIPC_MAX_USER_MSG_SIZE))
619 return send_packet(iocb, sock, m, total_len);
621 /* Can only send large data streams if already connected */
623 if (unlikely(sock->state != SS_CONNECTED)) {
624 if (sock->state == SS_DISCONNECTING)
625 return -EPIPE;
626 else
627 return -ENOTCONN;
631 * Send each iovec entry using one or more messages
633 * Note: This algorithm is good for the most likely case
634 * (i.e. one large iovec entry), but could be improved to pass sets
635 * of small iovec entries into send_packet().
638 my_msg = *m;
639 curr_iov = my_msg.msg_iov;
640 curr_iovlen = my_msg.msg_iovlen;
641 my_msg.msg_iov = &my_iov;
642 my_msg.msg_iovlen = 1;
644 while (curr_iovlen--) {
645 curr_start = curr_iov->iov_base;
646 curr_left = curr_iov->iov_len;
648 while (curr_left) {
649 bytes_to_send = (curr_left < TIPC_MAX_USER_MSG_SIZE)
650 ? curr_left : TIPC_MAX_USER_MSG_SIZE;
651 my_iov.iov_base = curr_start;
652 my_iov.iov_len = bytes_to_send;
653 if ((res = send_packet(iocb, sock, &my_msg, 0)) < 0)
654 return res;
655 curr_left -= bytes_to_send;
656 curr_start += bytes_to_send;
659 curr_iov++;
662 return total_len;
666 * auto_connect - complete connection setup to a remote port
667 * @sock: socket structure
668 * @tsock: TIPC-specific socket structure
669 * @msg: peer's response message
671 * Returns 0 on success, errno otherwise
674 static int auto_connect(struct socket *sock, struct tipc_sock *tsock,
675 struct tipc_msg *msg)
677 struct tipc_portid peer;
679 if (msg_errcode(msg)) {
680 sock->state = SS_DISCONNECTING;
681 return -ECONNREFUSED;
684 peer.ref = msg_origport(msg);
685 peer.node = msg_orignode(msg);
686 tipc_connect2port(tsock->p->ref, &peer);
687 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
688 sock->state = SS_CONNECTED;
689 return 0;
693 * set_orig_addr - capture sender's address for received message
694 * @m: descriptor for message info
695 * @msg: received message header
697 * Note: Address is not captured if not requested by receiver.
700 static inline void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
702 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
704 if (addr) {
705 addr->family = AF_TIPC;
706 addr->addrtype = TIPC_ADDR_ID;
707 addr->addr.id.ref = msg_origport(msg);
708 addr->addr.id.node = msg_orignode(msg);
709 addr->addr.name.domain = 0; /* could leave uninitialized */
710 addr->scope = 0; /* could leave uninitialized */
711 m->msg_namelen = sizeof(struct sockaddr_tipc);
716 * anc_data_recv - optionally capture ancillary data for received message
717 * @m: descriptor for message info
718 * @msg: received message header
719 * @tport: TIPC port associated with message
721 * Note: Ancillary data is not captured if not requested by receiver.
723 * Returns 0 if successful, otherwise errno
726 static inline int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
727 struct tipc_port *tport)
729 u32 anc_data[3];
730 u32 err;
731 u32 dest_type;
732 int res;
734 if (likely(m->msg_controllen == 0))
735 return 0;
737 /* Optionally capture errored message object(s) */
739 err = msg ? msg_errcode(msg) : 0;
740 if (unlikely(err)) {
741 anc_data[0] = err;
742 anc_data[1] = msg_data_sz(msg);
743 if ((res = put_cmsg(m, SOL_SOCKET, TIPC_ERRINFO, 8, anc_data)))
744 return res;
745 if (anc_data[1] &&
746 (res = put_cmsg(m, SOL_SOCKET, TIPC_RETDATA, anc_data[1],
747 msg_data(msg))))
748 return res;
751 /* Optionally capture message destination object */
753 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
754 switch (dest_type) {
755 case TIPC_NAMED_MSG:
756 anc_data[0] = msg_nametype(msg);
757 anc_data[1] = msg_namelower(msg);
758 anc_data[2] = msg_namelower(msg);
759 break;
760 case TIPC_MCAST_MSG:
761 anc_data[0] = msg_nametype(msg);
762 anc_data[1] = msg_namelower(msg);
763 anc_data[2] = msg_nameupper(msg);
764 break;
765 case TIPC_CONN_MSG:
766 anc_data[0] = tport->conn_type;
767 anc_data[1] = tport->conn_instance;
768 anc_data[2] = tport->conn_instance;
769 break;
770 default:
771 anc_data[0] = 0;
773 if (anc_data[0] &&
774 (res = put_cmsg(m, SOL_SOCKET, TIPC_DESTNAME, 12, anc_data)))
775 return res;
777 return 0;
780 /**
781 * recv_msg - receive packet-oriented message
782 * @iocb: (unused)
783 * @m: descriptor for message info
784 * @buf_len: total size of user buffer area
785 * @flags: receive flags
787 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
788 * If the complete message doesn't fit in user area, truncate it.
790 * Returns size of returned message data, errno otherwise
793 static int recv_msg(struct kiocb *iocb, struct socket *sock,
794 struct msghdr *m, size_t buf_len, int flags)
796 struct tipc_sock *tsock = tipc_sk(sock->sk);
797 struct sk_buff *buf;
798 struct tipc_msg *msg;
799 unsigned int q_len;
800 unsigned int sz;
801 u32 err;
802 int res;
804 /* Currently doesn't support receiving into multiple iovec entries */
806 if (m->msg_iovlen != 1)
807 return -EOPNOTSUPP;
809 /* Catch invalid receive attempts */
811 if (unlikely(!buf_len))
812 return -EINVAL;
814 if (sock->type == SOCK_SEQPACKET) {
815 if (unlikely(sock->state == SS_UNCONNECTED))
816 return -ENOTCONN;
817 if (unlikely((sock->state == SS_DISCONNECTING) &&
818 (skb_queue_len(&sock->sk->sk_receive_queue) == 0)))
819 return -ENOTCONN;
822 /* Look for a message in receive queue; wait if necessary */
824 if (unlikely(down_interruptible(&tsock->sem)))
825 return -ERESTARTSYS;
827 restart:
828 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
829 (flags & MSG_DONTWAIT))) {
830 res = -EWOULDBLOCK;
831 goto exit;
834 if ((res = wait_event_interruptible(
835 *sock->sk->sk_sleep,
836 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
837 (sock->state == SS_DISCONNECTING))) )) {
838 goto exit;
841 /* Catch attempt to receive on an already terminated connection */
842 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
844 if (!q_len) {
845 res = -ENOTCONN;
846 goto exit;
849 /* Get access to first message in receive queue */
851 buf = skb_peek(&sock->sk->sk_receive_queue);
852 msg = buf_msg(buf);
853 sz = msg_data_sz(msg);
854 err = msg_errcode(msg);
856 /* Complete connection setup for an implied connect */
858 if (unlikely(sock->state == SS_CONNECTING)) {
859 if ((res = auto_connect(sock, tsock, msg)))
860 goto exit;
863 /* Discard an empty non-errored message & try again */
865 if ((!sz) && (!err)) {
866 advance_queue(tsock);
867 goto restart;
870 /* Capture sender's address (optional) */
872 set_orig_addr(m, msg);
874 /* Capture ancillary data (optional) */
876 if ((res = anc_data_recv(m, msg, tsock->p)))
877 goto exit;
879 /* Capture message data (if valid) & compute return value (always) */
881 if (!err) {
882 if (unlikely(buf_len < sz)) {
883 sz = buf_len;
884 m->msg_flags |= MSG_TRUNC;
886 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
887 sz))) {
888 res = -EFAULT;
889 goto exit;
891 res = sz;
892 } else {
893 if ((sock->state == SS_READY) ||
894 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
895 res = 0;
896 else
897 res = -ECONNRESET;
900 /* Consume received message (optional) */
902 if (likely(!(flags & MSG_PEEK))) {
903 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
904 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
905 advance_queue(tsock);
907 exit:
908 up(&tsock->sem);
909 return res;
912 /**
913 * recv_stream - receive stream-oriented data
914 * @iocb: (unused)
915 * @m: descriptor for message info
916 * @buf_len: total size of user buffer area
917 * @flags: receive flags
919 * Used for SOCK_STREAM messages only. If not enough data is available
920 * will optionally wait for more; never truncates data.
922 * Returns size of returned message data, errno otherwise
925 static int recv_stream(struct kiocb *iocb, struct socket *sock,
926 struct msghdr *m, size_t buf_len, int flags)
928 struct tipc_sock *tsock = tipc_sk(sock->sk);
929 struct sk_buff *buf;
930 struct tipc_msg *msg;
931 unsigned int q_len;
932 unsigned int sz;
933 int sz_to_copy;
934 int sz_copied = 0;
935 int needed;
936 char *crs = m->msg_iov->iov_base;
937 unsigned char *buf_crs;
938 u32 err;
939 int res;
941 /* Currently doesn't support receiving into multiple iovec entries */
943 if (m->msg_iovlen != 1)
944 return -EOPNOTSUPP;
946 /* Catch invalid receive attempts */
948 if (unlikely(!buf_len))
949 return -EINVAL;
951 if (unlikely(sock->state == SS_DISCONNECTING)) {
952 if (skb_queue_len(&sock->sk->sk_receive_queue) == 0)
953 return -ENOTCONN;
954 } else if (unlikely(sock->state != SS_CONNECTED))
955 return -ENOTCONN;
957 /* Look for a message in receive queue; wait if necessary */
959 if (unlikely(down_interruptible(&tsock->sem)))
960 return -ERESTARTSYS;
962 restart:
963 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
964 (flags & MSG_DONTWAIT))) {
965 res = (sz_copied == 0) ? -EWOULDBLOCK : 0;
966 goto exit;
969 if ((res = wait_event_interruptible(
970 *sock->sk->sk_sleep,
971 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
972 (sock->state == SS_DISCONNECTING))) )) {
973 goto exit;
976 /* Catch attempt to receive on an already terminated connection */
977 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
979 if (!q_len) {
980 res = -ENOTCONN;
981 goto exit;
984 /* Get access to first message in receive queue */
986 buf = skb_peek(&sock->sk->sk_receive_queue);
987 msg = buf_msg(buf);
988 sz = msg_data_sz(msg);
989 err = msg_errcode(msg);
991 /* Discard an empty non-errored message & try again */
993 if ((!sz) && (!err)) {
994 advance_queue(tsock);
995 goto restart;
998 /* Optionally capture sender's address & ancillary data of first msg */
1000 if (sz_copied == 0) {
1001 set_orig_addr(m, msg);
1002 if ((res = anc_data_recv(m, msg, tsock->p)))
1003 goto exit;
1006 /* Capture message data (if valid) & compute return value (always) */
1008 if (!err) {
1009 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
1010 sz = buf->tail - buf_crs;
1012 needed = (buf_len - sz_copied);
1013 sz_to_copy = (sz <= needed) ? sz : needed;
1014 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1015 res = -EFAULT;
1016 goto exit;
1018 sz_copied += sz_to_copy;
1020 if (sz_to_copy < sz) {
1021 if (!(flags & MSG_PEEK))
1022 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1023 goto exit;
1026 crs += sz_to_copy;
1027 } else {
1028 if (sz_copied != 0)
1029 goto exit; /* can't add error msg to valid data */
1031 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1032 res = 0;
1033 else
1034 res = -ECONNRESET;
1037 /* Consume received message (optional) */
1039 if (likely(!(flags & MSG_PEEK))) {
1040 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1041 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
1042 advance_queue(tsock);
1045 /* Loop around if more data is required */
1047 if ((sz_copied < buf_len) /* didn't get all requested data */
1048 && (flags & MSG_WAITALL) /* ... and need to wait for more */
1049 && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
1050 && (!err) /* ... and haven't reached a FIN */
1052 goto restart;
1054 exit:
1055 up(&tsock->sem);
1056 return res ? res : sz_copied;
1060 * queue_overloaded - test if queue overload condition exists
1061 * @queue_size: current size of queue
1062 * @base: nominal maximum size of queue
1063 * @msg: message to be added to queue
1065 * Returns 1 if queue is currently overloaded, 0 otherwise
1068 static int queue_overloaded(u32 queue_size, u32 base, struct tipc_msg *msg)
1070 u32 threshold;
1071 u32 imp = msg_importance(msg);
1073 if (imp == TIPC_LOW_IMPORTANCE)
1074 threshold = base;
1075 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1076 threshold = base * 2;
1077 else if (imp == TIPC_HIGH_IMPORTANCE)
1078 threshold = base * 100;
1079 else
1080 return 0;
1082 if (msg_connected(msg))
1083 threshold *= 4;
1085 return (queue_size > threshold);
1088 /**
1089 * async_disconnect - wrapper function used to disconnect port
1090 * @portref: TIPC port reference (passed as pointer-sized value)
1093 static void async_disconnect(unsigned long portref)
1095 tipc_disconnect((u32)portref);
1098 /**
1099 * dispatch - handle arriving message
1100 * @tport: TIPC port that received message
1101 * @buf: message
1103 * Called with port locked. Must not take socket lock to avoid deadlock risk.
1105 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1108 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1110 struct tipc_msg *msg = buf_msg(buf);
1111 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1112 struct socket *sock;
1113 u32 recv_q_len;
1115 /* Reject message if socket is closing */
1117 if (!tsock)
1118 return TIPC_ERR_NO_PORT;
1120 /* Reject message if it is wrong sort of message for socket */
1123 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1124 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1125 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1127 sock = tsock->sk.sk_socket;
1128 if (sock->state == SS_READY) {
1129 if (msg_connected(msg)) {
1130 msg_dbg(msg, "dispatch filter 1\n");
1131 return TIPC_ERR_NO_PORT;
1133 } else {
1134 if (msg_mcast(msg)) {
1135 msg_dbg(msg, "dispatch filter 2\n");
1136 return TIPC_ERR_NO_PORT;
1138 if (sock->state == SS_CONNECTED) {
1139 if (!msg_connected(msg)) {
1140 msg_dbg(msg, "dispatch filter 3\n");
1141 return TIPC_ERR_NO_PORT;
1144 else if (sock->state == SS_CONNECTING) {
1145 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1146 msg_dbg(msg, "dispatch filter 4\n");
1147 return TIPC_ERR_NO_PORT;
1150 else if (sock->state == SS_LISTENING) {
1151 if (msg_connected(msg) || msg_errcode(msg)) {
1152 msg_dbg(msg, "dispatch filter 5\n");
1153 return TIPC_ERR_NO_PORT;
1156 else if (sock->state == SS_DISCONNECTING) {
1157 msg_dbg(msg, "dispatch filter 6\n");
1158 return TIPC_ERR_NO_PORT;
1160 else /* (sock->state == SS_UNCONNECTED) */ {
1161 if (msg_connected(msg) || msg_errcode(msg)) {
1162 msg_dbg(msg, "dispatch filter 7\n");
1163 return TIPC_ERR_NO_PORT;
1168 /* Reject message if there isn't room to queue it */
1170 if (unlikely((u32)atomic_read(&tipc_queue_size) >
1171 OVERLOAD_LIMIT_BASE)) {
1172 if (queue_overloaded(atomic_read(&tipc_queue_size),
1173 OVERLOAD_LIMIT_BASE, msg))
1174 return TIPC_ERR_OVERLOAD;
1176 recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue);
1177 if (unlikely(recv_q_len > (OVERLOAD_LIMIT_BASE / 2))) {
1178 if (queue_overloaded(recv_q_len,
1179 OVERLOAD_LIMIT_BASE / 2, msg))
1180 return TIPC_ERR_OVERLOAD;
1183 /* Initiate connection termination for an incoming 'FIN' */
1185 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1186 sock->state = SS_DISCONNECTING;
1187 /* Note: Use signal since port lock is already taken! */
1188 k_signal((Handler)async_disconnect, tport->ref);
1191 /* Enqueue message (finally!) */
1193 msg_dbg(msg,"<DISP<: ");
1194 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1195 atomic_inc(&tipc_queue_size);
1196 skb_queue_tail(&sock->sk->sk_receive_queue, buf);
1198 wake_up_interruptible(sock->sk->sk_sleep);
1199 return TIPC_OK;
1202 /**
1203 * wakeupdispatch - wake up port after congestion
1204 * @tport: port to wakeup
1206 * Called with port lock on.
1209 static void wakeupdispatch(struct tipc_port *tport)
1211 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1213 wake_up_interruptible(tsock->sk.sk_sleep);
1217 * connect - establish a connection to another TIPC port
1218 * @sock: socket structure
1219 * @dest: socket address for destination port
1220 * @destlen: size of socket address data structure
1221 * @flags: (unused)
1223 * Returns 0 on success, errno otherwise
1226 static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
1227 int flags)
1229 struct tipc_sock *tsock = tipc_sk(sock->sk);
1230 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1231 struct msghdr m = {0,};
1232 struct sk_buff *buf;
1233 struct tipc_msg *msg;
1234 int res;
1236 /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */
1238 if (sock->state == SS_READY)
1239 return -EOPNOTSUPP;
1241 /* MOVE THE REST OF THIS ERROR CHECKING TO send_msg()? */
1242 if (sock->state == SS_LISTENING)
1243 return -EOPNOTSUPP;
1244 if (sock->state == SS_CONNECTING)
1245 return -EALREADY;
1246 if (sock->state != SS_UNCONNECTED)
1247 return -EISCONN;
1249 if ((dst->family != AF_TIPC) ||
1250 ((dst->addrtype != TIPC_ADDR_NAME) && (dst->addrtype != TIPC_ADDR_ID)))
1251 return -EINVAL;
1253 /* Send a 'SYN-' to destination */
1255 m.msg_name = dest;
1256 if ((res = send_msg(0, sock, &m, 0)) < 0) {
1257 sock->state = SS_DISCONNECTING;
1258 return res;
1261 if (down_interruptible(&tsock->sem))
1262 return -ERESTARTSYS;
1264 /* Wait for destination's 'ACK' response */
1266 res = wait_event_interruptible_timeout(*sock->sk->sk_sleep,
1267 skb_queue_len(&sock->sk->sk_receive_queue),
1268 sock->sk->sk_rcvtimeo);
1269 buf = skb_peek(&sock->sk->sk_receive_queue);
1270 if (res > 0) {
1271 msg = buf_msg(buf);
1272 res = auto_connect(sock, tsock, msg);
1273 if (!res) {
1274 if (dst->addrtype == TIPC_ADDR_NAME) {
1275 tsock->p->conn_type = dst->addr.name.name.type;
1276 tsock->p->conn_instance = dst->addr.name.name.instance;
1278 if (!msg_data_sz(msg))
1279 advance_queue(tsock);
1281 } else {
1282 if (res == 0) {
1283 res = -ETIMEDOUT;
1284 } else
1285 { /* leave "res" unchanged */ }
1286 sock->state = SS_DISCONNECTING;
1289 up(&tsock->sem);
1290 return res;
1293 /**
1294 * listen - allow socket to listen for incoming connections
1295 * @sock: socket structure
1296 * @len: (unused)
1298 * Returns 0 on success, errno otherwise
1301 static int listen(struct socket *sock, int len)
1303 /* REQUIRES SOCKET LOCKING OF SOME SORT? */
1305 if (sock->state == SS_READY)
1306 return -EOPNOTSUPP;
1307 if (sock->state != SS_UNCONNECTED)
1308 return -EINVAL;
1309 sock->state = SS_LISTENING;
1310 return 0;
1313 /**
1314 * accept - wait for connection request
1315 * @sock: listening socket
1316 * @newsock: new socket that is to be connected
1317 * @flags: file-related flags associated with socket
1319 * Returns 0 on success, errno otherwise
1322 static int accept(struct socket *sock, struct socket *newsock, int flags)
1324 struct tipc_sock *tsock = tipc_sk(sock->sk);
1325 struct sk_buff *buf;
1326 int res = -EFAULT;
1328 if (sock->state == SS_READY)
1329 return -EOPNOTSUPP;
1330 if (sock->state != SS_LISTENING)
1331 return -EINVAL;
1333 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
1334 (flags & O_NONBLOCK)))
1335 return -EWOULDBLOCK;
1337 if (down_interruptible(&tsock->sem))
1338 return -ERESTARTSYS;
1340 if (wait_event_interruptible(*sock->sk->sk_sleep,
1341 skb_queue_len(&sock->sk->sk_receive_queue))) {
1342 res = -ERESTARTSYS;
1343 goto exit;
1345 buf = skb_peek(&sock->sk->sk_receive_queue);
1347 res = tipc_create(newsock, 0);
1348 if (!res) {
1349 struct tipc_sock *new_tsock = tipc_sk(newsock->sk);
1350 struct tipc_portid id;
1351 struct tipc_msg *msg = buf_msg(buf);
1352 u32 new_ref = new_tsock->p->ref;
1354 id.ref = msg_origport(msg);
1355 id.node = msg_orignode(msg);
1356 tipc_connect2port(new_ref, &id);
1357 newsock->state = SS_CONNECTED;
1359 tipc_set_portimportance(new_ref, msg_importance(msg));
1360 if (msg_named(msg)) {
1361 new_tsock->p->conn_type = msg_nametype(msg);
1362 new_tsock->p->conn_instance = msg_nameinst(msg);
1366 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1367 * Respond to 'SYN+' by queuing it on new socket.
1370 msg_dbg(msg,"<ACC<: ");
1371 if (!msg_data_sz(msg)) {
1372 struct msghdr m = {0,};
1374 send_packet(0, newsock, &m, 0);
1375 advance_queue(tsock);
1376 } else {
1377 sock_lock(tsock);
1378 skb_dequeue(&sock->sk->sk_receive_queue);
1379 sock_unlock(tsock);
1380 skb_queue_head(&newsock->sk->sk_receive_queue, buf);
1383 exit:
1384 up(&tsock->sem);
1385 return res;
1389 * shutdown - shutdown socket connection
1390 * @sock: socket structure
1391 * @how: direction to close (always treated as read + write)
1393 * Terminates connection (if necessary), then purges socket's receive queue.
1395 * Returns 0 on success, errno otherwise
1398 static int shutdown(struct socket *sock, int how)
1400 struct tipc_sock* tsock = tipc_sk(sock->sk);
1401 struct sk_buff *buf;
1402 int res;
1404 /* Could return -EINVAL for an invalid "how", but why bother? */
1406 if (down_interruptible(&tsock->sem))
1407 return -ERESTARTSYS;
1409 sock_lock(tsock);
1411 switch (sock->state) {
1412 case SS_CONNECTED:
1414 /* Send 'FIN+' or 'FIN-' message to peer */
1416 sock_unlock(tsock);
1417 restart:
1418 if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1419 atomic_dec(&tipc_queue_size);
1420 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1421 buf_discard(buf);
1422 goto restart;
1424 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
1426 else {
1427 tipc_shutdown(tsock->p->ref);
1429 sock_lock(tsock);
1431 /* fall through */
1433 case SS_DISCONNECTING:
1435 /* Discard any unreceived messages */
1437 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1438 atomic_dec(&tipc_queue_size);
1439 buf_discard(buf);
1441 tsock->p->conn_unacked = 0;
1443 /* fall through */
1445 case SS_CONNECTING:
1446 sock->state = SS_DISCONNECTING;
1447 res = 0;
1448 break;
1450 default:
1451 res = -ENOTCONN;
1454 sock_unlock(tsock);
1456 up(&tsock->sem);
1457 return res;
1461 * setsockopt - set socket option
1462 * @sock: socket structure
1463 * @lvl: option level
1464 * @opt: option identifier
1465 * @ov: pointer to new option value
1466 * @ol: length of option value
1468 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1469 * (to ease compatibility).
1471 * Returns 0 on success, errno otherwise
1474 static int setsockopt(struct socket *sock, int lvl, int opt, char *ov, int ol)
1476 struct tipc_sock *tsock = tipc_sk(sock->sk);
1477 u32 value;
1478 int res;
1480 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1481 return 0;
1482 if (lvl != SOL_TIPC)
1483 return -ENOPROTOOPT;
1484 if (ol < sizeof(value))
1485 return -EINVAL;
1486 if ((res = get_user(value, (u32 *)ov)))
1487 return res;
1489 if (down_interruptible(&tsock->sem))
1490 return -ERESTARTSYS;
1492 switch (opt) {
1493 case TIPC_IMPORTANCE:
1494 res = tipc_set_portimportance(tsock->p->ref, value);
1495 break;
1496 case TIPC_SRC_DROPPABLE:
1497 if (sock->type != SOCK_STREAM)
1498 res = tipc_set_portunreliable(tsock->p->ref, value);
1499 else
1500 res = -ENOPROTOOPT;
1501 break;
1502 case TIPC_DEST_DROPPABLE:
1503 res = tipc_set_portunreturnable(tsock->p->ref, value);
1504 break;
1505 case TIPC_CONN_TIMEOUT:
1506 sock->sk->sk_rcvtimeo = (value * HZ / 1000);
1507 break;
1508 default:
1509 res = -EINVAL;
1512 up(&tsock->sem);
1513 return res;
1517 * getsockopt - get socket option
1518 * @sock: socket structure
1519 * @lvl: option level
1520 * @opt: option identifier
1521 * @ov: receptacle for option value
1522 * @ol: receptacle for length of option value
1524 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1525 * (to ease compatibility).
1527 * Returns 0 on success, errno otherwise
1530 static int getsockopt(struct socket *sock, int lvl, int opt, char *ov, int *ol)
1532 struct tipc_sock *tsock = tipc_sk(sock->sk);
1533 int len;
1534 u32 value;
1535 int res;
1537 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1538 return put_user(0, ol);
1539 if (lvl != SOL_TIPC)
1540 return -ENOPROTOOPT;
1541 if ((res = get_user(len, ol)))
1542 return res;
1544 if (down_interruptible(&tsock->sem))
1545 return -ERESTARTSYS;
1547 switch (opt) {
1548 case TIPC_IMPORTANCE:
1549 res = tipc_portimportance(tsock->p->ref, &value);
1550 break;
1551 case TIPC_SRC_DROPPABLE:
1552 res = tipc_portunreliable(tsock->p->ref, &value);
1553 break;
1554 case TIPC_DEST_DROPPABLE:
1555 res = tipc_portunreturnable(tsock->p->ref, &value);
1556 break;
1557 case TIPC_CONN_TIMEOUT:
1558 value = (sock->sk->sk_rcvtimeo * 1000) / HZ;
1559 break;
1560 default:
1561 res = -EINVAL;
1564 if (res) {
1565 /* "get" failed */
1567 else if (len < sizeof(value)) {
1568 res = -EINVAL;
1570 else if ((res = copy_to_user(ov, &value, sizeof(value)))) {
1571 /* couldn't return value */
1573 else {
1574 res = put_user(sizeof(value), ol);
1577 up(&tsock->sem);
1578 return res;
1582 * Placeholders for non-implemented functionality
1584 * Returns error code (POSIX-compliant where defined)
1587 static int ioctl(struct socket *s, u32 cmd, unsigned long arg)
1589 return -EINVAL;
1592 static int no_mmap(struct file *file, struct socket *sock,
1593 struct vm_area_struct *vma)
1595 return -EINVAL;
1597 static ssize_t no_sendpage(struct socket *sock, struct page *page,
1598 int offset, size_t size, int flags)
1600 return -EINVAL;
1603 static int no_skpair(struct socket *s1, struct socket *s2)
1605 return -EOPNOTSUPP;
1609 * Protocol switches for the various types of TIPC sockets
1612 static struct proto_ops msg_ops = {
1613 .owner = THIS_MODULE,
1614 .family = AF_TIPC,
1615 .release = release,
1616 .bind = bind,
1617 .connect = connect,
1618 .socketpair = no_skpair,
1619 .accept = accept,
1620 .getname = get_name,
1621 .poll = poll,
1622 .ioctl = ioctl,
1623 .listen = listen,
1624 .shutdown = shutdown,
1625 .setsockopt = setsockopt,
1626 .getsockopt = getsockopt,
1627 .sendmsg = send_msg,
1628 .recvmsg = recv_msg,
1629 .mmap = no_mmap,
1630 .sendpage = no_sendpage
1633 static struct proto_ops packet_ops = {
1634 .owner = THIS_MODULE,
1635 .family = AF_TIPC,
1636 .release = release,
1637 .bind = bind,
1638 .connect = connect,
1639 .socketpair = no_skpair,
1640 .accept = accept,
1641 .getname = get_name,
1642 .poll = poll,
1643 .ioctl = ioctl,
1644 .listen = listen,
1645 .shutdown = shutdown,
1646 .setsockopt = setsockopt,
1647 .getsockopt = getsockopt,
1648 .sendmsg = send_packet,
1649 .recvmsg = recv_msg,
1650 .mmap = no_mmap,
1651 .sendpage = no_sendpage
1654 static struct proto_ops stream_ops = {
1655 .owner = THIS_MODULE,
1656 .family = AF_TIPC,
1657 .release = release,
1658 .bind = bind,
1659 .connect = connect,
1660 .socketpair = no_skpair,
1661 .accept = accept,
1662 .getname = get_name,
1663 .poll = poll,
1664 .ioctl = ioctl,
1665 .listen = listen,
1666 .shutdown = shutdown,
1667 .setsockopt = setsockopt,
1668 .getsockopt = getsockopt,
1669 .sendmsg = send_stream,
1670 .recvmsg = recv_stream,
1671 .mmap = no_mmap,
1672 .sendpage = no_sendpage
1675 static struct net_proto_family tipc_family_ops = {
1676 .owner = THIS_MODULE,
1677 .family = AF_TIPC,
1678 .create = tipc_create
1681 static struct proto tipc_proto = {
1682 .name = "TIPC",
1683 .owner = THIS_MODULE,
1684 .obj_size = sizeof(struct tipc_sock)
1688 * socket_init - initialize TIPC socket interface
1690 * Returns 0 on success, errno otherwise
1692 int socket_init(void)
1694 int res;
1696 res = proto_register(&tipc_proto, 1);
1697 if (res) {
1698 err("Failed to register TIPC protocol type\n");
1699 goto out;
1702 res = sock_register(&tipc_family_ops);
1703 if (res) {
1704 err("Failed to register TIPC socket type\n");
1705 proto_unregister(&tipc_proto);
1706 goto out;
1709 sockets_enabled = 1;
1710 out:
1711 return res;
1715 * sock_stop - stop TIPC socket interface
1717 void socket_stop(void)
1719 if (!sockets_enabled)
1720 return;
1722 sockets_enabled = 0;
1723 sock_unregister(tipc_family_ops.family);
1724 proto_unregister(&tipc_proto);