2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2001-2007, Ericsson AB
5 * Copyright (c) 2004-2008, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/net.h>
40 #include <linux/socket.h>
41 #include <linux/errno.h>
43 #include <linux/poll.h>
44 #include <linux/fcntl.h>
45 #include <linux/gfp.h>
46 #include <asm/string.h>
47 #include <asm/atomic.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>
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 */
66 struct tipc_portid peer_name
;
70 #define tipc_sk(sk) ((struct tipc_sock *)(sk))
71 #define tipc_sk_port(sk) ((struct tipc_port *)(tipc_sk(sk)->p))
73 static int backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
);
74 static u32
dispatch(struct tipc_port
*tport
, struct sk_buff
*buf
);
75 static void wakeupdispatch(struct tipc_port
*tport
);
77 static const struct proto_ops packet_ops
;
78 static const struct proto_ops stream_ops
;
79 static const struct proto_ops msg_ops
;
81 static struct proto tipc_proto
;
83 static int sockets_enabled
= 0;
85 static atomic_t tipc_queue_size
= ATOMIC_INIT(0);
88 * Revised TIPC socket locking policy:
90 * Most socket operations take the standard socket lock when they start
91 * and hold it until they finish (or until they need to sleep). Acquiring
92 * this lock grants the owner exclusive access to the fields of the socket
93 * data structures, with the exception of the backlog queue. A few socket
94 * operations can be done without taking the socket lock because they only
95 * read socket information that never changes during the life of the socket.
97 * Socket operations may acquire the lock for the associated TIPC port if they
98 * need to perform an operation on the port. If any routine needs to acquire
99 * both the socket lock and the port lock it must take the socket lock first
100 * to avoid the risk of deadlock.
102 * The dispatcher handling incoming messages cannot grab the socket lock in
103 * the standard fashion, since invoked it runs at the BH level and cannot block.
104 * Instead, it checks to see if the socket lock is currently owned by someone,
105 * and either handles the message itself or adds it to the socket's backlog
106 * queue; in the latter case the queued message is processed once the process
107 * owning the socket lock releases it.
109 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
110 * the problem of a blocked socket operation preventing any other operations
111 * from occurring. However, applications must be careful if they have
112 * multiple threads trying to send (or receive) on the same socket, as these
113 * operations might interfere with each other. For example, doing a connect
114 * and a receive at the same time might allow the receive to consume the
115 * ACK message meant for the connect. While additional work could be done
116 * to try and overcome this, it doesn't seem to be worthwhile at the present.
118 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
119 * that another operation that must be performed in a non-blocking manner is
120 * not delayed for very long because the lock has already been taken.
122 * NOTE: This code assumes that certain fields of a port/socket pair are
123 * constant over its lifetime; such fields can be examined without taking
124 * the socket lock and/or port lock, and do not need to be re-read even
125 * after resuming processing after waiting. These fields include:
127 * - pointer to socket sk structure (aka tipc_sock structure)
128 * - pointer to port structure
133 * advance_rx_queue - discard first buffer in socket receive queue
135 * Caller must hold socket lock
138 static void advance_rx_queue(struct sock
*sk
)
140 buf_discard(__skb_dequeue(&sk
->sk_receive_queue
));
141 atomic_dec(&tipc_queue_size
);
145 * discard_rx_queue - discard all buffers in socket receive queue
147 * Caller must hold socket lock
150 static void discard_rx_queue(struct sock
*sk
)
154 while ((buf
= __skb_dequeue(&sk
->sk_receive_queue
))) {
155 atomic_dec(&tipc_queue_size
);
161 * reject_rx_queue - reject all buffers in socket receive queue
163 * Caller must hold socket lock
166 static void reject_rx_queue(struct sock
*sk
)
170 while ((buf
= __skb_dequeue(&sk
->sk_receive_queue
))) {
171 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
172 atomic_dec(&tipc_queue_size
);
177 * tipc_create - create a TIPC socket
178 * @net: network namespace (must be default network)
179 * @sock: pre-allocated socket structure
180 * @protocol: protocol indicator (must be 0)
181 * @kern: caused by kernel or by userspace?
183 * This routine creates additional data structures used by the TIPC socket,
184 * initializes them, and links them together.
186 * Returns 0 on success, errno otherwise
189 static int tipc_create(struct net
*net
, struct socket
*sock
, int protocol
,
192 const struct proto_ops
*ops
;
195 struct tipc_port
*tp_ptr
;
197 /* Validate arguments */
199 if (!net_eq(net
, &init_net
))
200 return -EAFNOSUPPORT
;
202 if (unlikely(protocol
!= 0))
203 return -EPROTONOSUPPORT
;
205 switch (sock
->type
) {
208 state
= SS_UNCONNECTED
;
212 state
= SS_UNCONNECTED
;
223 /* Allocate socket's protocol area */
225 sk
= sk_alloc(net
, AF_TIPC
, GFP_KERNEL
, &tipc_proto
);
229 /* Allocate TIPC port for socket to use */
231 tp_ptr
= tipc_createport_raw(sk
, &dispatch
, &wakeupdispatch
,
232 TIPC_LOW_IMPORTANCE
);
233 if (unlikely(!tp_ptr
)) {
238 /* Finish initializing socket data structures */
243 sock_init_data(sock
, sk
);
244 sk
->sk_backlog_rcv
= backlog_rcv
;
245 tipc_sk(sk
)->p
= tp_ptr
;
246 tipc_sk(sk
)->conn_timeout
= msecs_to_jiffies(CONN_TIMEOUT_DEFAULT
);
248 spin_unlock_bh(tp_ptr
->lock
);
250 if (sock
->state
== SS_READY
) {
251 tipc_set_portunreturnable(tp_ptr
->ref
, 1);
252 if (sock
->type
== SOCK_DGRAM
)
253 tipc_set_portunreliable(tp_ptr
->ref
, 1);
256 atomic_inc(&tipc_user_count
);
261 * release - destroy a TIPC socket
262 * @sock: socket to destroy
264 * This routine cleans up any messages that are still queued on the socket.
265 * For DGRAM and RDM socket types, all queued messages are rejected.
266 * For SEQPACKET and STREAM socket types, the first message is rejected
267 * and any others are discarded. (If the first message on a STREAM socket
268 * is partially-read, it is discarded and the next one is rejected instead.)
270 * NOTE: Rejected messages are not necessarily returned to the sender! They
271 * are returned or discarded according to the "destination droppable" setting
272 * specified for the message by the sender.
274 * Returns 0 on success, errno otherwise
277 static int release(struct socket
*sock
)
279 struct sock
*sk
= sock
->sk
;
280 struct tipc_port
*tport
;
285 * Exit if socket isn't fully initialized (occurs when a failed accept()
286 * releases a pre-allocated child socket that was never used)
292 tport
= tipc_sk_port(sk
);
296 * Reject all unreceived messages, except on an active connection
297 * (which disconnects locally & sends a 'FIN+' to peer)
300 while (sock
->state
!= SS_DISCONNECTING
) {
301 buf
= __skb_dequeue(&sk
->sk_receive_queue
);
304 atomic_dec(&tipc_queue_size
);
305 if (TIPC_SKB_CB(buf
)->handle
!= msg_data(buf_msg(buf
)))
308 if ((sock
->state
== SS_CONNECTING
) ||
309 (sock
->state
== SS_CONNECTED
)) {
310 sock
->state
= SS_DISCONNECTING
;
311 tipc_disconnect(tport
->ref
);
313 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
318 * Delete TIPC port; this ensures no more messages are queued
319 * (also disconnects an active connection & sends a 'FIN-' to peer)
322 res
= tipc_deleteport(tport
->ref
);
324 /* Discard any remaining (connection-based) messages in receive queue */
326 discard_rx_queue(sk
);
328 /* Reject any messages that accumulated in backlog queue */
330 sock
->state
= SS_DISCONNECTING
;
336 atomic_dec(&tipc_user_count
);
341 * bind - associate or disassocate TIPC name(s) with a socket
342 * @sock: socket structure
343 * @uaddr: socket address describing name(s) and desired operation
344 * @uaddr_len: size of socket address data structure
346 * Name and name sequence binding is indicated using a positive scope value;
347 * a negative scope value unbinds the specified name. Specifying no name
348 * (i.e. a socket address length of 0) unbinds all names from the socket.
350 * Returns 0 on success, errno otherwise
352 * NOTE: This routine doesn't need to take the socket lock since it doesn't
353 * access any non-constant socket information.
356 static int bind(struct socket
*sock
, struct sockaddr
*uaddr
, int uaddr_len
)
358 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
359 u32 portref
= tipc_sk_port(sock
->sk
)->ref
;
361 if (unlikely(!uaddr_len
))
362 return tipc_withdraw(portref
, 0, NULL
);
364 if (uaddr_len
< sizeof(struct sockaddr_tipc
))
366 if (addr
->family
!= AF_TIPC
)
367 return -EAFNOSUPPORT
;
369 if (addr
->addrtype
== TIPC_ADDR_NAME
)
370 addr
->addr
.nameseq
.upper
= addr
->addr
.nameseq
.lower
;
371 else if (addr
->addrtype
!= TIPC_ADDR_NAMESEQ
)
372 return -EAFNOSUPPORT
;
374 return (addr
->scope
> 0) ?
375 tipc_publish(portref
, addr
->scope
, &addr
->addr
.nameseq
) :
376 tipc_withdraw(portref
, -addr
->scope
, &addr
->addr
.nameseq
);
380 * get_name - get port ID of socket or peer socket
381 * @sock: socket structure
382 * @uaddr: area for returned socket address
383 * @uaddr_len: area for returned length of socket address
384 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
386 * Returns 0 on success, errno otherwise
388 * NOTE: This routine doesn't need to take the socket lock since it only
389 * accesses socket information that is unchanging (or which changes in
390 * a completely predictable manner).
393 static int get_name(struct socket
*sock
, struct sockaddr
*uaddr
,
394 int *uaddr_len
, int peer
)
396 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
397 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
399 memset(addr
, 0, sizeof(*addr
));
401 if ((sock
->state
!= SS_CONNECTED
) &&
402 ((peer
!= 2) || (sock
->state
!= SS_DISCONNECTING
)))
404 addr
->addr
.id
.ref
= tsock
->peer_name
.ref
;
405 addr
->addr
.id
.node
= tsock
->peer_name
.node
;
407 tipc_ownidentity(tsock
->p
->ref
, &addr
->addr
.id
);
410 *uaddr_len
= sizeof(*addr
);
411 addr
->addrtype
= TIPC_ADDR_ID
;
412 addr
->family
= AF_TIPC
;
414 addr
->addr
.name
.domain
= 0;
420 * poll - read and possibly block on pollmask
421 * @file: file structure associated with the socket
422 * @sock: socket for which to calculate the poll bits
425 * Returns pollmask value
428 * It appears that the usual socket locking mechanisms are not useful here
429 * since the pollmask info is potentially out-of-date the moment this routine
430 * exits. TCP and other protocols seem to rely on higher level poll routines
431 * to handle any preventable race conditions, so TIPC will do the same ...
433 * TIPC sets the returned events as follows:
435 * socket state flags set
436 * ------------ ---------
437 * unconnected no read flags
440 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
443 * connected POLLIN/POLLRDNORM if data in rx queue
444 * POLLOUT if port is not congested
446 * disconnecting POLLIN/POLLRDNORM/POLLHUP
449 * listening POLLIN if SYN in rx queue
452 * ready POLLIN/POLLRDNORM if data in rx queue
453 * [connectionless] POLLOUT (since port cannot be congested)
455 * IMPORTANT: The fact that a read or write operation is indicated does NOT
456 * imply that the operation will succeed, merely that it should be performed
457 * and will not block.
460 static unsigned int poll(struct file
*file
, struct socket
*sock
,
463 struct sock
*sk
= sock
->sk
;
466 poll_wait(file
, sk_sleep(sk
), wait
);
468 switch ((int)sock
->state
) {
471 if (!tipc_sk_port(sk
)->congested
)
476 if (!skb_queue_empty(&sk
->sk_receive_queue
))
477 mask
|= (POLLIN
| POLLRDNORM
);
479 case SS_DISCONNECTING
:
480 mask
= (POLLIN
| POLLRDNORM
| POLLHUP
);
488 * dest_name_check - verify user is permitted to send to specified port name
489 * @dest: destination address
490 * @m: descriptor for message to be sent
492 * Prevents restricted configuration commands from being issued by
493 * unauthorized users.
495 * Returns 0 if permission is granted, otherwise errno
498 static int dest_name_check(struct sockaddr_tipc
*dest
, struct msghdr
*m
)
500 struct tipc_cfg_msg_hdr hdr
;
502 if (likely(dest
->addr
.name
.name
.type
>= TIPC_RESERVED_TYPES
))
504 if (likely(dest
->addr
.name
.name
.type
== TIPC_TOP_SRV
))
506 if (likely(dest
->addr
.name
.name
.type
!= TIPC_CFG_SRV
))
509 if (copy_from_user(&hdr
, m
->msg_iov
[0].iov_base
, sizeof(hdr
)))
511 if ((ntohs(hdr
.tcm_type
) & 0xC000) && (!capable(CAP_NET_ADMIN
)))
518 * send_msg - send message in connectionless manner
519 * @iocb: if NULL, indicates that socket lock is already held
520 * @sock: socket structure
521 * @m: message to send
522 * @total_len: length of message
524 * Message must have an destination specified explicitly.
525 * Used for SOCK_RDM and SOCK_DGRAM messages,
526 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
527 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
529 * Returns the number of bytes sent on success, or errno otherwise
532 static int send_msg(struct kiocb
*iocb
, struct socket
*sock
,
533 struct msghdr
*m
, size_t total_len
)
535 struct sock
*sk
= sock
->sk
;
536 struct tipc_port
*tport
= tipc_sk_port(sk
);
537 struct sockaddr_tipc
*dest
= (struct sockaddr_tipc
*)m
->msg_name
;
542 return -EDESTADDRREQ
;
543 if (unlikely((m
->msg_namelen
< sizeof(*dest
)) ||
544 (dest
->family
!= AF_TIPC
)))
550 needs_conn
= (sock
->state
!= SS_READY
);
551 if (unlikely(needs_conn
)) {
552 if (sock
->state
== SS_LISTENING
) {
556 if (sock
->state
!= SS_UNCONNECTED
) {
560 if ((tport
->published
) ||
561 ((sock
->type
== SOCK_STREAM
) && (total_len
!= 0))) {
565 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
566 tport
->conn_type
= dest
->addr
.name
.name
.type
;
567 tport
->conn_instance
= dest
->addr
.name
.name
.instance
;
570 /* Abort any pending connection attempts (very unlikely) */
576 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
577 if ((res
= dest_name_check(dest
, m
)))
579 res
= tipc_send2name(tport
->ref
,
580 &dest
->addr
.name
.name
,
581 dest
->addr
.name
.domain
,
585 else if (dest
->addrtype
== TIPC_ADDR_ID
) {
586 res
= tipc_send2port(tport
->ref
,
591 else if (dest
->addrtype
== TIPC_ADDR_MCAST
) {
596 if ((res
= dest_name_check(dest
, m
)))
598 res
= tipc_multicast(tport
->ref
,
604 if (likely(res
!= -ELINKCONG
)) {
605 if (needs_conn
&& (res
>= 0)) {
606 sock
->state
= SS_CONNECTING
;
610 if (m
->msg_flags
& MSG_DONTWAIT
) {
615 res
= wait_event_interruptible(*sk_sleep(sk
),
629 * send_packet - send a connection-oriented message
630 * @iocb: if NULL, indicates that socket lock is already held
631 * @sock: socket structure
632 * @m: message to send
633 * @total_len: length of message
635 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
637 * Returns the number of bytes sent on success, or errno otherwise
640 static int send_packet(struct kiocb
*iocb
, struct socket
*sock
,
641 struct msghdr
*m
, size_t total_len
)
643 struct sock
*sk
= sock
->sk
;
644 struct tipc_port
*tport
= tipc_sk_port(sk
);
645 struct sockaddr_tipc
*dest
= (struct sockaddr_tipc
*)m
->msg_name
;
648 /* Handle implied connection establishment */
651 return send_msg(iocb
, sock
, m
, total_len
);
657 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
658 if (sock
->state
== SS_DISCONNECTING
)
665 res
= tipc_send(tport
->ref
, m
->msg_iovlen
, m
->msg_iov
);
666 if (likely(res
!= -ELINKCONG
)) {
669 if (m
->msg_flags
& MSG_DONTWAIT
) {
674 res
= wait_event_interruptible(*sk_sleep(sk
),
675 (!tport
->congested
|| !tport
->connected
));
687 * send_stream - send stream-oriented data
689 * @sock: socket structure
691 * @total_len: total length of data to be sent
693 * Used for SOCK_STREAM data.
695 * Returns the number of bytes sent on success (or partial success),
696 * or errno if no data sent
699 static int send_stream(struct kiocb
*iocb
, struct socket
*sock
,
700 struct msghdr
*m
, size_t total_len
)
702 struct sock
*sk
= sock
->sk
;
703 struct tipc_port
*tport
= tipc_sk_port(sk
);
704 struct msghdr my_msg
;
706 struct iovec
*curr_iov
;
708 char __user
*curr_start
;
717 /* Handle special cases where there is no connection */
719 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
720 if (sock
->state
== SS_UNCONNECTED
) {
721 res
= send_packet(NULL
, sock
, m
, total_len
);
723 } else if (sock
->state
== SS_DISCONNECTING
) {
732 if (unlikely(m
->msg_name
)) {
738 * Send each iovec entry using one or more messages
740 * Note: This algorithm is good for the most likely case
741 * (i.e. one large iovec entry), but could be improved to pass sets
742 * of small iovec entries into send_packet().
745 curr_iov
= m
->msg_iov
;
746 curr_iovlen
= m
->msg_iovlen
;
747 my_msg
.msg_iov
= &my_iov
;
748 my_msg
.msg_iovlen
= 1;
749 my_msg
.msg_flags
= m
->msg_flags
;
750 my_msg
.msg_name
= NULL
;
753 hdr_size
= msg_hdr_sz(&tport
->phdr
);
755 while (curr_iovlen
--) {
756 curr_start
= curr_iov
->iov_base
;
757 curr_left
= curr_iov
->iov_len
;
760 bytes_to_send
= tport
->max_pkt
- hdr_size
;
761 if (bytes_to_send
> TIPC_MAX_USER_MSG_SIZE
)
762 bytes_to_send
= TIPC_MAX_USER_MSG_SIZE
;
763 if (curr_left
< bytes_to_send
)
764 bytes_to_send
= curr_left
;
765 my_iov
.iov_base
= curr_start
;
766 my_iov
.iov_len
= bytes_to_send
;
767 if ((res
= send_packet(NULL
, sock
, &my_msg
, 0)) < 0) {
772 curr_left
-= bytes_to_send
;
773 curr_start
+= bytes_to_send
;
774 bytes_sent
+= bytes_to_send
;
786 * auto_connect - complete connection setup to a remote port
787 * @sock: socket structure
788 * @msg: peer's response message
790 * Returns 0 on success, errno otherwise
793 static int auto_connect(struct socket
*sock
, struct tipc_msg
*msg
)
795 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
797 if (msg_errcode(msg
)) {
798 sock
->state
= SS_DISCONNECTING
;
799 return -ECONNREFUSED
;
802 tsock
->peer_name
.ref
= msg_origport(msg
);
803 tsock
->peer_name
.node
= msg_orignode(msg
);
804 tipc_connect2port(tsock
->p
->ref
, &tsock
->peer_name
);
805 tipc_set_portimportance(tsock
->p
->ref
, msg_importance(msg
));
806 sock
->state
= SS_CONNECTED
;
811 * set_orig_addr - capture sender's address for received message
812 * @m: descriptor for message info
813 * @msg: received message header
815 * Note: Address is not captured if not requested by receiver.
818 static void set_orig_addr(struct msghdr
*m
, struct tipc_msg
*msg
)
820 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)m
->msg_name
;
823 addr
->family
= AF_TIPC
;
824 addr
->addrtype
= TIPC_ADDR_ID
;
825 addr
->addr
.id
.ref
= msg_origport(msg
);
826 addr
->addr
.id
.node
= msg_orignode(msg
);
827 addr
->addr
.name
.domain
= 0; /* could leave uninitialized */
828 addr
->scope
= 0; /* could leave uninitialized */
829 m
->msg_namelen
= sizeof(struct sockaddr_tipc
);
834 * anc_data_recv - optionally capture ancillary data for received message
835 * @m: descriptor for message info
836 * @msg: received message header
837 * @tport: TIPC port associated with message
839 * Note: Ancillary data is not captured if not requested by receiver.
841 * Returns 0 if successful, otherwise errno
844 static int anc_data_recv(struct msghdr
*m
, struct tipc_msg
*msg
,
845 struct tipc_port
*tport
)
853 if (likely(m
->msg_controllen
== 0))
856 /* Optionally capture errored message object(s) */
858 err
= msg
? msg_errcode(msg
) : 0;
861 anc_data
[1] = msg_data_sz(msg
);
862 if ((res
= put_cmsg(m
, SOL_TIPC
, TIPC_ERRINFO
, 8, anc_data
)))
865 (res
= put_cmsg(m
, SOL_TIPC
, TIPC_RETDATA
, anc_data
[1],
870 /* Optionally capture message destination object */
872 dest_type
= msg
? msg_type(msg
) : TIPC_DIRECT_MSG
;
876 anc_data
[0] = msg_nametype(msg
);
877 anc_data
[1] = msg_namelower(msg
);
878 anc_data
[2] = msg_namelower(msg
);
882 anc_data
[0] = msg_nametype(msg
);
883 anc_data
[1] = msg_namelower(msg
);
884 anc_data
[2] = msg_nameupper(msg
);
887 has_name
= (tport
->conn_type
!= 0);
888 anc_data
[0] = tport
->conn_type
;
889 anc_data
[1] = tport
->conn_instance
;
890 anc_data
[2] = tport
->conn_instance
;
896 (res
= put_cmsg(m
, SOL_TIPC
, TIPC_DESTNAME
, 12, anc_data
)))
903 * recv_msg - receive packet-oriented message
905 * @m: descriptor for message info
906 * @buf_len: total size of user buffer area
907 * @flags: receive flags
909 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
910 * If the complete message doesn't fit in user area, truncate it.
912 * Returns size of returned message data, errno otherwise
915 static int recv_msg(struct kiocb
*iocb
, struct socket
*sock
,
916 struct msghdr
*m
, size_t buf_len
, int flags
)
918 struct sock
*sk
= sock
->sk
;
919 struct tipc_port
*tport
= tipc_sk_port(sk
);
921 struct tipc_msg
*msg
;
926 /* Catch invalid receive requests */
928 if (m
->msg_iovlen
!= 1)
929 return -EOPNOTSUPP
; /* Don't do multiple iovec entries yet */
931 if (unlikely(!buf_len
))
936 if (unlikely(sock
->state
== SS_UNCONNECTED
)) {
943 /* Look for a message in receive queue; wait if necessary */
945 while (skb_queue_empty(&sk
->sk_receive_queue
)) {
946 if (sock
->state
== SS_DISCONNECTING
) {
950 if (flags
& MSG_DONTWAIT
) {
955 res
= wait_event_interruptible(*sk_sleep(sk
),
956 (!skb_queue_empty(&sk
->sk_receive_queue
) ||
957 (sock
->state
== SS_DISCONNECTING
)));
963 /* Look at first message in receive queue */
965 buf
= skb_peek(&sk
->sk_receive_queue
);
967 sz
= msg_data_sz(msg
);
968 err
= msg_errcode(msg
);
970 /* Complete connection setup for an implied connect */
972 if (unlikely(sock
->state
== SS_CONNECTING
)) {
973 res
= auto_connect(sock
, msg
);
978 /* Discard an empty non-errored message & try again */
980 if ((!sz
) && (!err
)) {
981 advance_rx_queue(sk
);
985 /* Capture sender's address (optional) */
987 set_orig_addr(m
, msg
);
989 /* Capture ancillary data (optional) */
991 res
= anc_data_recv(m
, msg
, tport
);
995 /* Capture message data (if valid) & compute return value (always) */
998 if (unlikely(buf_len
< sz
)) {
1000 m
->msg_flags
|= MSG_TRUNC
;
1002 if (unlikely(copy_to_user(m
->msg_iov
->iov_base
, msg_data(msg
),
1009 if ((sock
->state
== SS_READY
) ||
1010 ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
))
1016 /* Consume received message (optional) */
1018 if (likely(!(flags
& MSG_PEEK
))) {
1019 if ((sock
->state
!= SS_READY
) &&
1020 (++tport
->conn_unacked
>= TIPC_FLOW_CONTROL_WIN
))
1021 tipc_acknowledge(tport
->ref
, tport
->conn_unacked
);
1022 advance_rx_queue(sk
);
1030 * recv_stream - receive stream-oriented data
1032 * @m: descriptor for message info
1033 * @buf_len: total size of user buffer area
1034 * @flags: receive flags
1036 * Used for SOCK_STREAM messages only. If not enough data is available
1037 * will optionally wait for more; never truncates data.
1039 * Returns size of returned message data, errno otherwise
1042 static int recv_stream(struct kiocb
*iocb
, struct socket
*sock
,
1043 struct msghdr
*m
, size_t buf_len
, int flags
)
1045 struct sock
*sk
= sock
->sk
;
1046 struct tipc_port
*tport
= tipc_sk_port(sk
);
1047 struct sk_buff
*buf
;
1048 struct tipc_msg
*msg
;
1050 int sz_to_copy
, target
, needed
;
1052 char __user
*crs
= m
->msg_iov
->iov_base
;
1053 unsigned char *buf_crs
;
1057 /* Catch invalid receive attempts */
1059 if (m
->msg_iovlen
!= 1)
1060 return -EOPNOTSUPP
; /* Don't do multiple iovec entries yet */
1062 if (unlikely(!buf_len
))
1067 if (unlikely((sock
->state
== SS_UNCONNECTED
) ||
1068 (sock
->state
== SS_CONNECTING
))) {
1073 target
= sock_rcvlowat(sk
, flags
& MSG_WAITALL
, buf_len
);
1077 /* Look for a message in receive queue; wait if necessary */
1079 while (skb_queue_empty(&sk
->sk_receive_queue
)) {
1080 if (sock
->state
== SS_DISCONNECTING
) {
1084 if (flags
& MSG_DONTWAIT
) {
1089 res
= wait_event_interruptible(*sk_sleep(sk
),
1090 (!skb_queue_empty(&sk
->sk_receive_queue
) ||
1091 (sock
->state
== SS_DISCONNECTING
)));
1097 /* Look at first message in receive queue */
1099 buf
= skb_peek(&sk
->sk_receive_queue
);
1101 sz
= msg_data_sz(msg
);
1102 err
= msg_errcode(msg
);
1104 /* Discard an empty non-errored message & try again */
1106 if ((!sz
) && (!err
)) {
1107 advance_rx_queue(sk
);
1111 /* Optionally capture sender's address & ancillary data of first msg */
1113 if (sz_copied
== 0) {
1114 set_orig_addr(m
, msg
);
1115 res
= anc_data_recv(m
, msg
, tport
);
1120 /* Capture message data (if valid) & compute return value (always) */
1123 buf_crs
= (unsigned char *)(TIPC_SKB_CB(buf
)->handle
);
1124 sz
= (unsigned char *)msg
+ msg_size(msg
) - buf_crs
;
1126 needed
= (buf_len
- sz_copied
);
1127 sz_to_copy
= (sz
<= needed
) ? sz
: needed
;
1128 if (unlikely(copy_to_user(crs
, buf_crs
, sz_to_copy
))) {
1132 sz_copied
+= sz_to_copy
;
1134 if (sz_to_copy
< sz
) {
1135 if (!(flags
& MSG_PEEK
))
1136 TIPC_SKB_CB(buf
)->handle
= buf_crs
+ sz_to_copy
;
1143 goto exit
; /* can't add error msg to valid data */
1145 if ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
)
1151 /* Consume received message (optional) */
1153 if (likely(!(flags
& MSG_PEEK
))) {
1154 if (unlikely(++tport
->conn_unacked
>= TIPC_FLOW_CONTROL_WIN
))
1155 tipc_acknowledge(tport
->ref
, tport
->conn_unacked
);
1156 advance_rx_queue(sk
);
1159 /* Loop around if more data is required */
1161 if ((sz_copied
< buf_len
) && /* didn't get all requested data */
1162 (!skb_queue_empty(&sk
->sk_receive_queue
) ||
1163 (sz_copied
< target
)) && /* and more is ready or required */
1164 (!(flags
& MSG_PEEK
)) && /* and aren't just peeking at data */
1165 (!err
)) /* and haven't reached a FIN */
1170 return sz_copied
? sz_copied
: res
;
1174 * rx_queue_full - determine if receive queue can accept another message
1175 * @msg: message to be added to queue
1176 * @queue_size: current size of queue
1177 * @base: nominal maximum size of queue
1179 * Returns 1 if queue is unable to accept message, 0 otherwise
1182 static int rx_queue_full(struct tipc_msg
*msg
, u32 queue_size
, u32 base
)
1185 u32 imp
= msg_importance(msg
);
1187 if (imp
== TIPC_LOW_IMPORTANCE
)
1189 else if (imp
== TIPC_MEDIUM_IMPORTANCE
)
1190 threshold
= base
* 2;
1191 else if (imp
== TIPC_HIGH_IMPORTANCE
)
1192 threshold
= base
* 100;
1196 if (msg_connected(msg
))
1199 return queue_size
>= threshold
;
1203 * filter_rcv - validate incoming message
1207 * Enqueues message on receive queue if acceptable; optionally handles
1208 * disconnect indication for a connected socket.
1210 * Called with socket lock already taken; port lock may also be taken.
1212 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1215 static u32
filter_rcv(struct sock
*sk
, struct sk_buff
*buf
)
1217 struct socket
*sock
= sk
->sk_socket
;
1218 struct tipc_msg
*msg
= buf_msg(buf
);
1221 /* Reject message if it is wrong sort of message for socket */
1224 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1225 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1226 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1229 if (sock
->state
== SS_READY
) {
1230 if (msg_connected(msg
)) {
1231 msg_dbg(msg
, "dispatch filter 1\n");
1232 return TIPC_ERR_NO_PORT
;
1235 if (msg_mcast(msg
)) {
1236 msg_dbg(msg
, "dispatch filter 2\n");
1237 return TIPC_ERR_NO_PORT
;
1239 if (sock
->state
== SS_CONNECTED
) {
1240 if (!msg_connected(msg
)) {
1241 msg_dbg(msg
, "dispatch filter 3\n");
1242 return TIPC_ERR_NO_PORT
;
1245 else if (sock
->state
== SS_CONNECTING
) {
1246 if (!msg_connected(msg
) && (msg_errcode(msg
) == 0)) {
1247 msg_dbg(msg
, "dispatch filter 4\n");
1248 return TIPC_ERR_NO_PORT
;
1251 else if (sock
->state
== SS_LISTENING
) {
1252 if (msg_connected(msg
) || msg_errcode(msg
)) {
1253 msg_dbg(msg
, "dispatch filter 5\n");
1254 return TIPC_ERR_NO_PORT
;
1257 else if (sock
->state
== SS_DISCONNECTING
) {
1258 msg_dbg(msg
, "dispatch filter 6\n");
1259 return TIPC_ERR_NO_PORT
;
1261 else /* (sock->state == SS_UNCONNECTED) */ {
1262 if (msg_connected(msg
) || msg_errcode(msg
)) {
1263 msg_dbg(msg
, "dispatch filter 7\n");
1264 return TIPC_ERR_NO_PORT
;
1269 /* Reject message if there isn't room to queue it */
1271 recv_q_len
= (u32
)atomic_read(&tipc_queue_size
);
1272 if (unlikely(recv_q_len
>= OVERLOAD_LIMIT_BASE
)) {
1273 if (rx_queue_full(msg
, recv_q_len
, OVERLOAD_LIMIT_BASE
))
1274 return TIPC_ERR_OVERLOAD
;
1276 recv_q_len
= skb_queue_len(&sk
->sk_receive_queue
);
1277 if (unlikely(recv_q_len
>= (OVERLOAD_LIMIT_BASE
/ 2))) {
1278 if (rx_queue_full(msg
, recv_q_len
, OVERLOAD_LIMIT_BASE
/ 2))
1279 return TIPC_ERR_OVERLOAD
;
1282 /* Enqueue message (finally!) */
1284 msg_dbg(msg
, "<DISP<: ");
1285 TIPC_SKB_CB(buf
)->handle
= msg_data(msg
);
1286 atomic_inc(&tipc_queue_size
);
1287 __skb_queue_tail(&sk
->sk_receive_queue
, buf
);
1289 /* Initiate connection termination for an incoming 'FIN' */
1291 if (unlikely(msg_errcode(msg
) && (sock
->state
== SS_CONNECTED
))) {
1292 sock
->state
= SS_DISCONNECTING
;
1293 tipc_disconnect_port(tipc_sk_port(sk
));
1296 if (waitqueue_active(sk_sleep(sk
)))
1297 wake_up_interruptible(sk_sleep(sk
));
1302 * backlog_rcv - handle incoming message from backlog queue
1306 * Caller must hold socket lock, but not port lock.
1311 static int backlog_rcv(struct sock
*sk
, struct sk_buff
*buf
)
1315 res
= filter_rcv(sk
, buf
);
1317 tipc_reject_msg(buf
, res
);
1322 * dispatch - handle incoming message
1323 * @tport: TIPC port that received message
1326 * Called with port lock already taken.
1328 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1331 static u32
dispatch(struct tipc_port
*tport
, struct sk_buff
*buf
)
1333 struct sock
*sk
= (struct sock
*)tport
->usr_handle
;
1337 * Process message if socket is unlocked; otherwise add to backlog queue
1339 * This code is based on sk_receive_skb(), but must be distinct from it
1340 * since a TIPC-specific filter/reject mechanism is utilized
1344 if (!sock_owned_by_user(sk
)) {
1345 res
= filter_rcv(sk
, buf
);
1347 if (sk_add_backlog(sk
, buf
))
1348 res
= TIPC_ERR_OVERLOAD
;
1358 * wakeupdispatch - wake up port after congestion
1359 * @tport: port to wakeup
1361 * Called with port lock already taken.
1364 static void wakeupdispatch(struct tipc_port
*tport
)
1366 struct sock
*sk
= (struct sock
*)tport
->usr_handle
;
1368 if (waitqueue_active(sk_sleep(sk
)))
1369 wake_up_interruptible(sk_sleep(sk
));
1373 * connect - establish a connection to another TIPC port
1374 * @sock: socket structure
1375 * @dest: socket address for destination port
1376 * @destlen: size of socket address data structure
1377 * @flags: file-related flags associated with socket
1379 * Returns 0 on success, errno otherwise
1382 static int connect(struct socket
*sock
, struct sockaddr
*dest
, int destlen
,
1385 struct sock
*sk
= sock
->sk
;
1386 struct sockaddr_tipc
*dst
= (struct sockaddr_tipc
*)dest
;
1387 struct msghdr m
= {NULL
,};
1388 struct sk_buff
*buf
;
1389 struct tipc_msg
*msg
;
1395 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
1397 if (sock
->state
== SS_READY
) {
1402 /* For now, TIPC does not support the non-blocking form of connect() */
1404 if (flags
& O_NONBLOCK
) {
1409 /* Issue Posix-compliant error code if socket is in the wrong state */
1411 if (sock
->state
== SS_LISTENING
) {
1415 if (sock
->state
== SS_CONNECTING
) {
1419 if (sock
->state
!= SS_UNCONNECTED
) {
1425 * Reject connection attempt using multicast address
1427 * Note: send_msg() validates the rest of the address fields,
1428 * so there's no need to do it here
1431 if (dst
->addrtype
== TIPC_ADDR_MCAST
) {
1436 /* Reject any messages already in receive queue (very unlikely) */
1438 reject_rx_queue(sk
);
1440 /* Send a 'SYN-' to destination */
1443 m
.msg_namelen
= destlen
;
1444 res
= send_msg(NULL
, sock
, &m
, 0);
1449 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1451 timeout
= tipc_sk(sk
)->conn_timeout
;
1453 res
= wait_event_interruptible_timeout(*sk_sleep(sk
),
1454 (!skb_queue_empty(&sk
->sk_receive_queue
) ||
1455 (sock
->state
!= SS_CONNECTING
)),
1456 timeout
? timeout
: MAX_SCHEDULE_TIMEOUT
);
1460 buf
= skb_peek(&sk
->sk_receive_queue
);
1463 res
= auto_connect(sock
, msg
);
1465 if (!msg_data_sz(msg
))
1466 advance_rx_queue(sk
);
1469 if (sock
->state
== SS_CONNECTED
) {
1472 res
= -ECONNREFUSED
;
1479 ; /* leave "res" unchanged */
1480 sock
->state
= SS_DISCONNECTING
;
1489 * listen - allow socket to listen for incoming connections
1490 * @sock: socket structure
1493 * Returns 0 on success, errno otherwise
1496 static int listen(struct socket
*sock
, int len
)
1498 struct sock
*sk
= sock
->sk
;
1503 if (sock
->state
== SS_READY
)
1505 else if (sock
->state
!= SS_UNCONNECTED
)
1508 sock
->state
= SS_LISTENING
;
1517 * accept - wait for connection request
1518 * @sock: listening socket
1519 * @newsock: new socket that is to be connected
1520 * @flags: file-related flags associated with socket
1522 * Returns 0 on success, errno otherwise
1525 static int accept(struct socket
*sock
, struct socket
*new_sock
, int flags
)
1527 struct sock
*sk
= sock
->sk
;
1528 struct sk_buff
*buf
;
1533 if (sock
->state
== SS_READY
) {
1537 if (sock
->state
!= SS_LISTENING
) {
1542 while (skb_queue_empty(&sk
->sk_receive_queue
)) {
1543 if (flags
& O_NONBLOCK
) {
1548 res
= wait_event_interruptible(*sk_sleep(sk
),
1549 (!skb_queue_empty(&sk
->sk_receive_queue
)));
1555 buf
= skb_peek(&sk
->sk_receive_queue
);
1557 res
= tipc_create(sock_net(sock
->sk
), new_sock
, 0, 0);
1559 struct sock
*new_sk
= new_sock
->sk
;
1560 struct tipc_sock
*new_tsock
= tipc_sk(new_sk
);
1561 struct tipc_port
*new_tport
= new_tsock
->p
;
1562 u32 new_ref
= new_tport
->ref
;
1563 struct tipc_msg
*msg
= buf_msg(buf
);
1568 * Reject any stray messages received by new socket
1569 * before the socket lock was taken (very, very unlikely)
1572 reject_rx_queue(new_sk
);
1574 /* Connect new socket to it's peer */
1576 new_tsock
->peer_name
.ref
= msg_origport(msg
);
1577 new_tsock
->peer_name
.node
= msg_orignode(msg
);
1578 tipc_connect2port(new_ref
, &new_tsock
->peer_name
);
1579 new_sock
->state
= SS_CONNECTED
;
1581 tipc_set_portimportance(new_ref
, msg_importance(msg
));
1582 if (msg_named(msg
)) {
1583 new_tport
->conn_type
= msg_nametype(msg
);
1584 new_tport
->conn_instance
= msg_nameinst(msg
);
1588 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1589 * Respond to 'SYN+' by queuing it on new socket.
1592 msg_dbg(msg
,"<ACC<: ");
1593 if (!msg_data_sz(msg
)) {
1594 struct msghdr m
= {NULL
,};
1596 advance_rx_queue(sk
);
1597 send_packet(NULL
, new_sock
, &m
, 0);
1599 __skb_dequeue(&sk
->sk_receive_queue
);
1600 __skb_queue_head(&new_sk
->sk_receive_queue
, buf
);
1602 release_sock(new_sk
);
1610 * shutdown - shutdown socket connection
1611 * @sock: socket structure
1612 * @how: direction to close (must be SHUT_RDWR)
1614 * Terminates connection (if necessary), then purges socket's receive queue.
1616 * Returns 0 on success, errno otherwise
1619 static int shutdown(struct socket
*sock
, int how
)
1621 struct sock
*sk
= sock
->sk
;
1622 struct tipc_port
*tport
= tipc_sk_port(sk
);
1623 struct sk_buff
*buf
;
1626 if (how
!= SHUT_RDWR
)
1631 switch (sock
->state
) {
1635 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
1637 buf
= __skb_dequeue(&sk
->sk_receive_queue
);
1639 atomic_dec(&tipc_queue_size
);
1640 if (TIPC_SKB_CB(buf
)->handle
!= msg_data(buf_msg(buf
))) {
1644 tipc_disconnect(tport
->ref
);
1645 tipc_reject_msg(buf
, TIPC_CONN_SHUTDOWN
);
1647 tipc_shutdown(tport
->ref
);
1650 sock
->state
= SS_DISCONNECTING
;
1654 case SS_DISCONNECTING
:
1656 /* Discard any unreceived messages; wake up sleeping tasks */
1658 discard_rx_queue(sk
);
1659 if (waitqueue_active(sk_sleep(sk
)))
1660 wake_up_interruptible(sk_sleep(sk
));
1673 * setsockopt - set socket option
1674 * @sock: socket structure
1675 * @lvl: option level
1676 * @opt: option identifier
1677 * @ov: pointer to new option value
1678 * @ol: length of option value
1680 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1681 * (to ease compatibility).
1683 * Returns 0 on success, errno otherwise
1686 static int setsockopt(struct socket
*sock
,
1687 int lvl
, int opt
, char __user
*ov
, unsigned int ol
)
1689 struct sock
*sk
= sock
->sk
;
1690 struct tipc_port
*tport
= tipc_sk_port(sk
);
1694 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1696 if (lvl
!= SOL_TIPC
)
1697 return -ENOPROTOOPT
;
1698 if (ol
< sizeof(value
))
1700 if ((res
= get_user(value
, (u32 __user
*)ov
)))
1706 case TIPC_IMPORTANCE
:
1707 res
= tipc_set_portimportance(tport
->ref
, value
);
1709 case TIPC_SRC_DROPPABLE
:
1710 if (sock
->type
!= SOCK_STREAM
)
1711 res
= tipc_set_portunreliable(tport
->ref
, value
);
1715 case TIPC_DEST_DROPPABLE
:
1716 res
= tipc_set_portunreturnable(tport
->ref
, value
);
1718 case TIPC_CONN_TIMEOUT
:
1719 tipc_sk(sk
)->conn_timeout
= msecs_to_jiffies(value
);
1720 /* no need to set "res", since already 0 at this point */
1732 * getsockopt - get socket option
1733 * @sock: socket structure
1734 * @lvl: option level
1735 * @opt: option identifier
1736 * @ov: receptacle for option value
1737 * @ol: receptacle for length of option value
1739 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1740 * (to ease compatibility).
1742 * Returns 0 on success, errno otherwise
1745 static int getsockopt(struct socket
*sock
,
1746 int lvl
, int opt
, char __user
*ov
, int __user
*ol
)
1748 struct sock
*sk
= sock
->sk
;
1749 struct tipc_port
*tport
= tipc_sk_port(sk
);
1754 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1755 return put_user(0, ol
);
1756 if (lvl
!= SOL_TIPC
)
1757 return -ENOPROTOOPT
;
1758 if ((res
= get_user(len
, ol
)))
1764 case TIPC_IMPORTANCE
:
1765 res
= tipc_portimportance(tport
->ref
, &value
);
1767 case TIPC_SRC_DROPPABLE
:
1768 res
= tipc_portunreliable(tport
->ref
, &value
);
1770 case TIPC_DEST_DROPPABLE
:
1771 res
= tipc_portunreturnable(tport
->ref
, &value
);
1773 case TIPC_CONN_TIMEOUT
:
1774 value
= jiffies_to_msecs(tipc_sk(sk
)->conn_timeout
);
1775 /* no need to set "res", since already 0 at this point */
1777 case TIPC_NODE_RECVQ_DEPTH
:
1778 value
= (u32
)atomic_read(&tipc_queue_size
);
1780 case TIPC_SOCK_RECVQ_DEPTH
:
1781 value
= skb_queue_len(&sk
->sk_receive_queue
);
1792 else if (len
< sizeof(value
)) {
1795 else if (copy_to_user(ov
, &value
, sizeof(value
))) {
1799 res
= put_user(sizeof(value
), ol
);
1806 * Protocol switches for the various types of TIPC sockets
1809 static const struct proto_ops msg_ops
= {
1810 .owner
= THIS_MODULE
,
1815 .socketpair
= sock_no_socketpair
,
1817 .getname
= get_name
,
1819 .ioctl
= sock_no_ioctl
,
1821 .shutdown
= shutdown
,
1822 .setsockopt
= setsockopt
,
1823 .getsockopt
= getsockopt
,
1824 .sendmsg
= send_msg
,
1825 .recvmsg
= recv_msg
,
1826 .mmap
= sock_no_mmap
,
1827 .sendpage
= sock_no_sendpage
1830 static const struct proto_ops packet_ops
= {
1831 .owner
= THIS_MODULE
,
1836 .socketpair
= sock_no_socketpair
,
1838 .getname
= get_name
,
1840 .ioctl
= sock_no_ioctl
,
1842 .shutdown
= shutdown
,
1843 .setsockopt
= setsockopt
,
1844 .getsockopt
= getsockopt
,
1845 .sendmsg
= send_packet
,
1846 .recvmsg
= recv_msg
,
1847 .mmap
= sock_no_mmap
,
1848 .sendpage
= sock_no_sendpage
1851 static const struct proto_ops stream_ops
= {
1852 .owner
= THIS_MODULE
,
1857 .socketpair
= sock_no_socketpair
,
1859 .getname
= get_name
,
1861 .ioctl
= sock_no_ioctl
,
1863 .shutdown
= shutdown
,
1864 .setsockopt
= setsockopt
,
1865 .getsockopt
= getsockopt
,
1866 .sendmsg
= send_stream
,
1867 .recvmsg
= recv_stream
,
1868 .mmap
= sock_no_mmap
,
1869 .sendpage
= sock_no_sendpage
1872 static const struct net_proto_family tipc_family_ops
= {
1873 .owner
= THIS_MODULE
,
1875 .create
= tipc_create
1878 static struct proto tipc_proto
= {
1880 .owner
= THIS_MODULE
,
1881 .obj_size
= sizeof(struct tipc_sock
)
1885 * tipc_socket_init - initialize TIPC socket interface
1887 * Returns 0 on success, errno otherwise
1889 int tipc_socket_init(void)
1893 res
= proto_register(&tipc_proto
, 1);
1895 err("Failed to register TIPC protocol type\n");
1899 res
= sock_register(&tipc_family_ops
);
1901 err("Failed to register TIPC socket type\n");
1902 proto_unregister(&tipc_proto
);
1906 sockets_enabled
= 1;
1912 * tipc_socket_stop - stop TIPC socket interface
1915 void tipc_socket_stop(void)
1917 if (!sockets_enabled
)
1920 sockets_enabled
= 0;
1921 sock_unregister(tipc_family_ops
.family
);
1922 proto_unregister(&tipc_proto
);