2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2013, 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.
38 #include "name_table.h"
41 #include <linux/export.h>
45 #define SS_LISTENING -1 /* socket is listening */
46 #define SS_READY -2 /* socket is connectionless */
48 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
49 #define CONN_PROBING_INTERVAL 3600000 /* [ms] => 1 h */
50 #define TIPC_FWD_MSG 1
51 #define TIPC_CONN_OK 0
52 #define TIPC_CONN_PROBING 1
55 * struct tipc_sock - TIPC socket structure
56 * @sk: socket - interacts with 'port' and with user via the socket API
57 * @connected: non-zero if port is currently connected to a peer port
58 * @conn_type: TIPC type used when connection was established
59 * @conn_instance: TIPC instance used when connection was established
60 * @published: non-zero if port has one or more associated names
61 * @max_pkt: maximum packet size "hint" used when building messages sent by port
62 * @ref: unique reference to port in TIPC object registry
63 * @phdr: preformatted message header used when sending messages
64 * @port_list: adjacent ports in TIPC's global list of ports
65 * @publications: list of publications for port
66 * @pub_count: total # of publications port has made during its lifetime
70 * @port: port - interacts with 'sk' and with the rest of the TIPC stack
71 * @peer_name: the peer of the connection, if any
72 * @conn_timeout: the time we can wait for an unresponded setup request
73 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
74 * @link_cong: non-zero if owner must sleep because of link congestion
75 * @sent_unacked: # messages sent by socket, and not yet acked by peer
76 * @rcv_unacked: # messages read by user, but not yet acked back to peer
87 struct list_head sock_list
;
88 struct list_head publications
;
92 struct timer_list timer
;
100 static int tipc_backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
);
101 static void tipc_data_ready(struct sock
*sk
);
102 static void tipc_write_space(struct sock
*sk
);
103 static int tipc_release(struct socket
*sock
);
104 static int tipc_accept(struct socket
*sock
, struct socket
*new_sock
, int flags
);
105 static int tipc_wait_for_sndmsg(struct socket
*sock
, long *timeo_p
);
106 static void tipc_sk_timeout(unsigned long ref
);
107 static int tipc_sk_publish(struct tipc_sock
*tsk
, uint scope
,
108 struct tipc_name_seq
const *seq
);
109 static int tipc_sk_withdraw(struct tipc_sock
*tsk
, uint scope
,
110 struct tipc_name_seq
const *seq
);
111 static u32
tipc_sk_ref_acquire(struct tipc_sock
*tsk
);
112 static void tipc_sk_ref_discard(u32 ref
);
113 static struct tipc_sock
*tipc_sk_get(u32 ref
);
114 static struct tipc_sock
*tipc_sk_get_next(u32
*ref
);
115 static void tipc_sk_put(struct tipc_sock
*tsk
);
117 static const struct proto_ops packet_ops
;
118 static const struct proto_ops stream_ops
;
119 static const struct proto_ops msg_ops
;
121 static struct proto tipc_proto
;
122 static struct proto tipc_proto_kern
;
125 * Revised TIPC socket locking policy:
127 * Most socket operations take the standard socket lock when they start
128 * and hold it until they finish (or until they need to sleep). Acquiring
129 * this lock grants the owner exclusive access to the fields of the socket
130 * data structures, with the exception of the backlog queue. A few socket
131 * operations can be done without taking the socket lock because they only
132 * read socket information that never changes during the life of the socket.
134 * Socket operations may acquire the lock for the associated TIPC port if they
135 * need to perform an operation on the port. If any routine needs to acquire
136 * both the socket lock and the port lock it must take the socket lock first
137 * to avoid the risk of deadlock.
139 * The dispatcher handling incoming messages cannot grab the socket lock in
140 * the standard fashion, since invoked it runs at the BH level and cannot block.
141 * Instead, it checks to see if the socket lock is currently owned by someone,
142 * and either handles the message itself or adds it to the socket's backlog
143 * queue; in the latter case the queued message is processed once the process
144 * owning the socket lock releases it.
146 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
147 * the problem of a blocked socket operation preventing any other operations
148 * from occurring. However, applications must be careful if they have
149 * multiple threads trying to send (or receive) on the same socket, as these
150 * operations might interfere with each other. For example, doing a connect
151 * and a receive at the same time might allow the receive to consume the
152 * ACK message meant for the connect. While additional work could be done
153 * to try and overcome this, it doesn't seem to be worthwhile at the present.
155 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
156 * that another operation that must be performed in a non-blocking manner is
157 * not delayed for very long because the lock has already been taken.
159 * NOTE: This code assumes that certain fields of a port/socket pair are
160 * constant over its lifetime; such fields can be examined without taking
161 * the socket lock and/or port lock, and do not need to be re-read even
162 * after resuming processing after waiting. These fields include:
164 * - pointer to socket sk structure (aka tipc_sock structure)
165 * - pointer to port structure
169 static u32
tsk_peer_node(struct tipc_sock
*tsk
)
171 return msg_destnode(&tsk
->phdr
);
174 static u32
tsk_peer_port(struct tipc_sock
*tsk
)
176 return msg_destport(&tsk
->phdr
);
179 static bool tsk_unreliable(struct tipc_sock
*tsk
)
181 return msg_src_droppable(&tsk
->phdr
) != 0;
184 static void tsk_set_unreliable(struct tipc_sock
*tsk
, bool unreliable
)
186 msg_set_src_droppable(&tsk
->phdr
, unreliable
? 1 : 0);
189 static bool tsk_unreturnable(struct tipc_sock
*tsk
)
191 return msg_dest_droppable(&tsk
->phdr
) != 0;
194 static void tsk_set_unreturnable(struct tipc_sock
*tsk
, bool unreturnable
)
196 msg_set_dest_droppable(&tsk
->phdr
, unreturnable
? 1 : 0);
199 static int tsk_importance(struct tipc_sock
*tsk
)
201 return msg_importance(&tsk
->phdr
);
204 static int tsk_set_importance(struct tipc_sock
*tsk
, int imp
)
206 if (imp
> TIPC_CRITICAL_IMPORTANCE
)
208 msg_set_importance(&tsk
->phdr
, (u32
)imp
);
212 static struct tipc_sock
*tipc_sk(const struct sock
*sk
)
214 return container_of(sk
, struct tipc_sock
, sk
);
217 static int tsk_conn_cong(struct tipc_sock
*tsk
)
219 return tsk
->sent_unacked
>= TIPC_FLOWCTRL_WIN
;
223 * tsk_advance_rx_queue - discard first buffer in socket receive queue
225 * Caller must hold socket lock
227 static void tsk_advance_rx_queue(struct sock
*sk
)
229 kfree_skb(__skb_dequeue(&sk
->sk_receive_queue
));
233 * tsk_rej_rx_queue - reject all buffers in socket receive queue
235 * Caller must hold socket lock
237 static void tsk_rej_rx_queue(struct sock
*sk
)
242 while ((buf
= __skb_dequeue(&sk
->sk_receive_queue
))) {
243 if (tipc_msg_reverse(buf
, &dnode
, TIPC_ERR_NO_PORT
))
244 tipc_link_xmit(buf
, dnode
, 0);
248 /* tsk_peer_msg - verify if message was sent by connected port's peer
250 * Handles cases where the node's network address has changed from
251 * the default of <0.0.0> to its configured setting.
253 static bool tsk_peer_msg(struct tipc_sock
*tsk
, struct tipc_msg
*msg
)
255 u32 peer_port
= tsk_peer_port(tsk
);
259 if (unlikely(!tsk
->connected
))
262 if (unlikely(msg_origport(msg
) != peer_port
))
265 orig_node
= msg_orignode(msg
);
266 peer_node
= tsk_peer_node(tsk
);
268 if (likely(orig_node
== peer_node
))
271 if (!orig_node
&& (peer_node
== tipc_own_addr
))
274 if (!peer_node
&& (orig_node
== tipc_own_addr
))
281 * tipc_sk_create - create a TIPC socket
282 * @net: network namespace (must be default network)
283 * @sock: pre-allocated socket structure
284 * @protocol: protocol indicator (must be 0)
285 * @kern: caused by kernel or by userspace?
287 * This routine creates additional data structures used by the TIPC socket,
288 * initializes them, and links them together.
290 * Returns 0 on success, errno otherwise
292 static int tipc_sk_create(struct net
*net
, struct socket
*sock
,
293 int protocol
, int kern
)
295 const struct proto_ops
*ops
;
298 struct tipc_sock
*tsk
;
299 struct tipc_msg
*msg
;
302 /* Validate arguments */
303 if (unlikely(protocol
!= 0))
304 return -EPROTONOSUPPORT
;
306 switch (sock
->type
) {
309 state
= SS_UNCONNECTED
;
313 state
= SS_UNCONNECTED
;
324 /* Allocate socket's protocol area */
326 sk
= sk_alloc(net
, AF_TIPC
, GFP_KERNEL
, &tipc_proto
);
328 sk
= sk_alloc(net
, AF_TIPC
, GFP_KERNEL
, &tipc_proto_kern
);
334 ref
= tipc_sk_ref_acquire(tsk
);
336 pr_warn("Socket create failed; reference table exhausted\n");
339 tsk
->max_pkt
= MAX_PKT_DEFAULT
;
341 INIT_LIST_HEAD(&tsk
->publications
);
343 tipc_msg_init(msg
, TIPC_LOW_IMPORTANCE
, TIPC_NAMED_MSG
,
345 msg_set_origport(msg
, ref
);
347 /* Finish initializing socket data structures */
350 sock_init_data(sock
, sk
);
351 k_init_timer(&tsk
->timer
, (Handler
)tipc_sk_timeout
, ref
);
352 sk
->sk_backlog_rcv
= tipc_backlog_rcv
;
353 sk
->sk_rcvbuf
= sysctl_tipc_rmem
[1];
354 sk
->sk_data_ready
= tipc_data_ready
;
355 sk
->sk_write_space
= tipc_write_space
;
356 tsk
->conn_timeout
= CONN_TIMEOUT_DEFAULT
;
357 tsk
->sent_unacked
= 0;
358 atomic_set(&tsk
->dupl_rcvcnt
, 0);
360 if (sock
->state
== SS_READY
) {
361 tsk_set_unreturnable(tsk
, true);
362 if (sock
->type
== SOCK_DGRAM
)
363 tsk_set_unreliable(tsk
, true);
369 * tipc_sock_create_local - create TIPC socket from inside TIPC module
370 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
372 * We cannot use sock_creat_kern here because it bumps module user count.
373 * Since socket owner and creator is the same module we must make sure
374 * that module count remains zero for module local sockets, otherwise
375 * we cannot do rmmod.
377 * Returns 0 on success, errno otherwise
379 int tipc_sock_create_local(int type
, struct socket
**res
)
383 rc
= sock_create_lite(AF_TIPC
, type
, 0, res
);
385 pr_err("Failed to create kernel socket\n");
388 tipc_sk_create(&init_net
, *res
, 0, 1);
394 * tipc_sock_release_local - release socket created by tipc_sock_create_local
395 * @sock: the socket to be released.
397 * Module reference count is not incremented when such sockets are created,
398 * so we must keep it from being decremented when they are released.
400 void tipc_sock_release_local(struct socket
*sock
)
408 * tipc_sock_accept_local - accept a connection on a socket created
409 * with tipc_sock_create_local. Use this function to avoid that
410 * module reference count is inadvertently incremented.
412 * @sock: the accepting socket
413 * @newsock: reference to the new socket to be created
414 * @flags: socket flags
417 int tipc_sock_accept_local(struct socket
*sock
, struct socket
**newsock
,
420 struct sock
*sk
= sock
->sk
;
423 ret
= sock_create_lite(sk
->sk_family
, sk
->sk_type
,
424 sk
->sk_protocol
, newsock
);
428 ret
= tipc_accept(sock
, *newsock
, flags
);
430 sock_release(*newsock
);
433 (*newsock
)->ops
= sock
->ops
;
438 * tipc_release - destroy a TIPC socket
439 * @sock: socket to destroy
441 * This routine cleans up any messages that are still queued on the socket.
442 * For DGRAM and RDM socket types, all queued messages are rejected.
443 * For SEQPACKET and STREAM socket types, the first message is rejected
444 * and any others are discarded. (If the first message on a STREAM socket
445 * is partially-read, it is discarded and the next one is rejected instead.)
447 * NOTE: Rejected messages are not necessarily returned to the sender! They
448 * are returned or discarded according to the "destination droppable" setting
449 * specified for the message by the sender.
451 * Returns 0 on success, errno otherwise
453 static int tipc_release(struct socket
*sock
)
455 struct sock
*sk
= sock
->sk
;
456 struct tipc_sock
*tsk
;
461 * Exit if socket isn't fully initialized (occurs when a failed accept()
462 * releases a pre-allocated child socket that was never used)
471 * Reject all unreceived messages, except on an active connection
472 * (which disconnects locally & sends a 'FIN+' to peer)
474 dnode
= tsk_peer_node(tsk
);
475 while (sock
->state
!= SS_DISCONNECTING
) {
476 buf
= __skb_dequeue(&sk
->sk_receive_queue
);
479 if (TIPC_SKB_CB(buf
)->handle
!= NULL
)
482 if ((sock
->state
== SS_CONNECTING
) ||
483 (sock
->state
== SS_CONNECTED
)) {
484 sock
->state
= SS_DISCONNECTING
;
486 tipc_node_remove_conn(dnode
, tsk
->ref
);
488 if (tipc_msg_reverse(buf
, &dnode
, TIPC_ERR_NO_PORT
))
489 tipc_link_xmit(buf
, dnode
, 0);
493 tipc_sk_withdraw(tsk
, 0, NULL
);
494 tipc_sk_ref_discard(tsk
->ref
);
495 k_cancel_timer(&tsk
->timer
);
496 if (tsk
->connected
) {
497 buf
= tipc_msg_create(TIPC_CRITICAL_IMPORTANCE
, TIPC_CONN_MSG
,
498 SHORT_H_SIZE
, 0, dnode
, tipc_own_addr
,
500 tsk
->ref
, TIPC_ERR_NO_PORT
);
502 tipc_link_xmit(buf
, dnode
, tsk
->ref
);
503 tipc_node_remove_conn(dnode
, tsk
->ref
);
505 k_term_timer(&tsk
->timer
);
507 /* Discard any remaining (connection-based) messages in receive queue */
508 __skb_queue_purge(&sk
->sk_receive_queue
);
510 /* Reject any messages that accumulated in backlog queue */
511 sock
->state
= SS_DISCONNECTING
;
520 * tipc_bind - associate or disassocate TIPC name(s) with a socket
521 * @sock: socket structure
522 * @uaddr: socket address describing name(s) and desired operation
523 * @uaddr_len: size of socket address data structure
525 * Name and name sequence binding is indicated using a positive scope value;
526 * a negative scope value unbinds the specified name. Specifying no name
527 * (i.e. a socket address length of 0) unbinds all names from the socket.
529 * Returns 0 on success, errno otherwise
531 * NOTE: This routine doesn't need to take the socket lock since it doesn't
532 * access any non-constant socket information.
534 static int tipc_bind(struct socket
*sock
, struct sockaddr
*uaddr
,
537 struct sock
*sk
= sock
->sk
;
538 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
539 struct tipc_sock
*tsk
= tipc_sk(sk
);
543 if (unlikely(!uaddr_len
)) {
544 res
= tipc_sk_withdraw(tsk
, 0, NULL
);
548 if (uaddr_len
< sizeof(struct sockaddr_tipc
)) {
552 if (addr
->family
!= AF_TIPC
) {
557 if (addr
->addrtype
== TIPC_ADDR_NAME
)
558 addr
->addr
.nameseq
.upper
= addr
->addr
.nameseq
.lower
;
559 else if (addr
->addrtype
!= TIPC_ADDR_NAMESEQ
) {
564 if ((addr
->addr
.nameseq
.type
< TIPC_RESERVED_TYPES
) &&
565 (addr
->addr
.nameseq
.type
!= TIPC_TOP_SRV
) &&
566 (addr
->addr
.nameseq
.type
!= TIPC_CFG_SRV
)) {
571 res
= (addr
->scope
> 0) ?
572 tipc_sk_publish(tsk
, addr
->scope
, &addr
->addr
.nameseq
) :
573 tipc_sk_withdraw(tsk
, -addr
->scope
, &addr
->addr
.nameseq
);
580 * tipc_getname - get port ID of socket or peer socket
581 * @sock: socket structure
582 * @uaddr: area for returned socket address
583 * @uaddr_len: area for returned length of socket address
584 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
586 * Returns 0 on success, errno otherwise
588 * NOTE: This routine doesn't need to take the socket lock since it only
589 * accesses socket information that is unchanging (or which changes in
590 * a completely predictable manner).
592 static int tipc_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
593 int *uaddr_len
, int peer
)
595 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
596 struct tipc_sock
*tsk
= tipc_sk(sock
->sk
);
598 memset(addr
, 0, sizeof(*addr
));
600 if ((sock
->state
!= SS_CONNECTED
) &&
601 ((peer
!= 2) || (sock
->state
!= SS_DISCONNECTING
)))
603 addr
->addr
.id
.ref
= tsk_peer_port(tsk
);
604 addr
->addr
.id
.node
= tsk_peer_node(tsk
);
606 addr
->addr
.id
.ref
= tsk
->ref
;
607 addr
->addr
.id
.node
= tipc_own_addr
;
610 *uaddr_len
= sizeof(*addr
);
611 addr
->addrtype
= TIPC_ADDR_ID
;
612 addr
->family
= AF_TIPC
;
614 addr
->addr
.name
.domain
= 0;
620 * tipc_poll - read and possibly block on pollmask
621 * @file: file structure associated with the socket
622 * @sock: socket for which to calculate the poll bits
625 * Returns pollmask value
628 * It appears that the usual socket locking mechanisms are not useful here
629 * since the pollmask info is potentially out-of-date the moment this routine
630 * exits. TCP and other protocols seem to rely on higher level poll routines
631 * to handle any preventable race conditions, so TIPC will do the same ...
633 * TIPC sets the returned events as follows:
635 * socket state flags set
636 * ------------ ---------
637 * unconnected no read flags
638 * POLLOUT if port is not congested
640 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
643 * connected POLLIN/POLLRDNORM if data in rx queue
644 * POLLOUT if port is not congested
646 * disconnecting POLLIN/POLLRDNORM/POLLHUP
649 * listening POLLIN if SYN in rx queue
652 * ready POLLIN/POLLRDNORM if data in rx queue
653 * [connectionless] POLLOUT (since port cannot be congested)
655 * IMPORTANT: The fact that a read or write operation is indicated does NOT
656 * imply that the operation will succeed, merely that it should be performed
657 * and will not block.
659 static unsigned int tipc_poll(struct file
*file
, struct socket
*sock
,
662 struct sock
*sk
= sock
->sk
;
663 struct tipc_sock
*tsk
= tipc_sk(sk
);
666 sock_poll_wait(file
, sk_sleep(sk
), wait
);
668 switch ((int)sock
->state
) {
675 if (!tsk
->link_cong
&& !tsk_conn_cong(tsk
))
680 if (!skb_queue_empty(&sk
->sk_receive_queue
))
681 mask
|= (POLLIN
| POLLRDNORM
);
683 case SS_DISCONNECTING
:
684 mask
= (POLLIN
| POLLRDNORM
| POLLHUP
);
692 * tipc_sendmcast - send multicast message
693 * @sock: socket structure
694 * @seq: destination address
695 * @iov: message data to send
696 * @dsz: total length of message data
697 * @timeo: timeout to wait for wakeup
699 * Called from function tipc_sendmsg(), which has done all sanity checks
700 * Returns the number of bytes sent on success, or errno
702 static int tipc_sendmcast(struct socket
*sock
, struct tipc_name_seq
*seq
,
703 struct iovec
*iov
, size_t dsz
, long timeo
)
705 struct sock
*sk
= sock
->sk
;
706 struct tipc_msg
*mhdr
= &tipc_sk(sk
)->phdr
;
711 msg_set_type(mhdr
, TIPC_MCAST_MSG
);
712 msg_set_lookup_scope(mhdr
, TIPC_CLUSTER_SCOPE
);
713 msg_set_destport(mhdr
, 0);
714 msg_set_destnode(mhdr
, 0);
715 msg_set_nametype(mhdr
, seq
->type
);
716 msg_set_namelower(mhdr
, seq
->lower
);
717 msg_set_nameupper(mhdr
, seq
->upper
);
718 msg_set_hdr_sz(mhdr
, MCAST_H_SIZE
);
721 mtu
= tipc_bclink_get_mtu();
722 rc
= tipc_msg_build(mhdr
, iov
, 0, dsz
, mtu
, &buf
);
723 if (unlikely(rc
< 0))
727 rc
= tipc_bclink_xmit(buf
);
728 if (likely(rc
>= 0)) {
734 if (rc
!= -ELINKCONG
)
736 tipc_sk(sk
)->link_cong
= 1;
737 rc
= tipc_wait_for_sndmsg(sock
, &timeo
);
744 /* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
746 void tipc_sk_mcast_rcv(struct sk_buff
*buf
)
748 struct tipc_msg
*msg
= buf_msg(buf
);
749 struct tipc_port_list dports
= {0, NULL
, };
750 struct tipc_port_list
*item
;
752 uint i
, last
, dst
= 0;
753 u32 scope
= TIPC_CLUSTER_SCOPE
;
755 if (in_own_node(msg_orignode(msg
)))
756 scope
= TIPC_NODE_SCOPE
;
758 /* Create destination port list: */
759 tipc_nametbl_mc_translate(msg_nametype(msg
),
770 for (item
= &dports
; item
; item
= item
->next
) {
771 for (i
= 0; i
< PLSIZE
&& ++dst
<= last
; i
++) {
772 b
= (dst
!= last
) ? skb_clone(buf
, GFP_ATOMIC
) : buf
;
774 pr_warn("Failed do clone mcast rcv buffer\n");
777 msg_set_destport(msg
, item
->ports
[i
]);
781 tipc_port_list_free(&dports
);
785 * tipc_sk_proto_rcv - receive a connection mng protocol message
786 * @tsk: receiving socket
787 * @dnode: node to send response message to, if any
788 * @buf: buffer containing protocol message
789 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
790 * (CONN_PROBE_REPLY) message should be forwarded.
792 static int tipc_sk_proto_rcv(struct tipc_sock
*tsk
, u32
*dnode
,
795 struct tipc_msg
*msg
= buf_msg(buf
);
798 /* Ignore if connection cannot be validated: */
799 if (!tsk_peer_msg(tsk
, msg
))
802 tsk
->probing_state
= TIPC_CONN_OK
;
804 if (msg_type(msg
) == CONN_ACK
) {
805 conn_cong
= tsk_conn_cong(tsk
);
806 tsk
->sent_unacked
-= msg_msgcnt(msg
);
808 tsk
->sk
.sk_write_space(&tsk
->sk
);
809 } else if (msg_type(msg
) == CONN_PROBE
) {
810 if (!tipc_msg_reverse(buf
, dnode
, TIPC_OK
))
812 msg_set_type(msg
, CONN_PROBE_REPLY
);
815 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
822 * dest_name_check - verify user is permitted to send to specified port name
823 * @dest: destination address
824 * @m: descriptor for message to be sent
826 * Prevents restricted configuration commands from being issued by
827 * unauthorized users.
829 * Returns 0 if permission is granted, otherwise errno
831 static int dest_name_check(struct sockaddr_tipc
*dest
, struct msghdr
*m
)
833 struct tipc_cfg_msg_hdr hdr
;
835 if (unlikely(dest
->addrtype
== TIPC_ADDR_ID
))
837 if (likely(dest
->addr
.name
.name
.type
>= TIPC_RESERVED_TYPES
))
839 if (likely(dest
->addr
.name
.name
.type
== TIPC_TOP_SRV
))
841 if (likely(dest
->addr
.name
.name
.type
!= TIPC_CFG_SRV
))
844 if (!m
->msg_iovlen
|| (m
->msg_iov
[0].iov_len
< sizeof(hdr
)))
846 if (copy_from_user(&hdr
, m
->msg_iov
[0].iov_base
, sizeof(hdr
)))
848 if ((ntohs(hdr
.tcm_type
) & 0xC000) && (!capable(CAP_NET_ADMIN
)))
854 static int tipc_wait_for_sndmsg(struct socket
*sock
, long *timeo_p
)
856 struct sock
*sk
= sock
->sk
;
857 struct tipc_sock
*tsk
= tipc_sk(sk
);
862 int err
= sock_error(sk
);
865 if (sock
->state
== SS_DISCONNECTING
)
869 if (signal_pending(current
))
870 return sock_intr_errno(*timeo_p
);
872 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
873 done
= sk_wait_event(sk
, timeo_p
, !tsk
->link_cong
);
874 finish_wait(sk_sleep(sk
), &wait
);
880 * tipc_sendmsg - send message in connectionless manner
881 * @iocb: if NULL, indicates that socket lock is already held
882 * @sock: socket structure
883 * @m: message to send
884 * @dsz: amount of user data to be sent
886 * Message must have an destination specified explicitly.
887 * Used for SOCK_RDM and SOCK_DGRAM messages,
888 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
889 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
891 * Returns the number of bytes sent on success, or errno otherwise
893 static int tipc_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
894 struct msghdr
*m
, size_t dsz
)
896 DECLARE_SOCKADDR(struct sockaddr_tipc
*, dest
, m
->msg_name
);
897 struct sock
*sk
= sock
->sk
;
898 struct tipc_sock
*tsk
= tipc_sk(sk
);
899 struct tipc_msg
*mhdr
= &tsk
->phdr
;
900 struct iovec
*iov
= m
->msg_iov
;
903 struct tipc_name_seq
*seq
= &dest
->addr
.nameseq
;
909 return -EDESTADDRREQ
;
911 if (unlikely((m
->msg_namelen
< sizeof(*dest
)) ||
912 (dest
->family
!= AF_TIPC
)))
915 if (dsz
> TIPC_MAX_USER_MSG_SIZE
)
921 if (unlikely(sock
->state
!= SS_READY
)) {
922 if (sock
->state
== SS_LISTENING
) {
926 if (sock
->state
!= SS_UNCONNECTED
) {
930 if (tsk
->published
) {
934 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
935 tsk
->conn_type
= dest
->addr
.name
.name
.type
;
936 tsk
->conn_instance
= dest
->addr
.name
.name
.instance
;
939 rc
= dest_name_check(dest
, m
);
943 timeo
= sock_sndtimeo(sk
, m
->msg_flags
& MSG_DONTWAIT
);
945 if (dest
->addrtype
== TIPC_ADDR_MCAST
) {
946 rc
= tipc_sendmcast(sock
, seq
, iov
, dsz
, timeo
);
948 } else if (dest
->addrtype
== TIPC_ADDR_NAME
) {
949 u32 type
= dest
->addr
.name
.name
.type
;
950 u32 inst
= dest
->addr
.name
.name
.instance
;
951 u32 domain
= dest
->addr
.name
.domain
;
954 msg_set_type(mhdr
, TIPC_NAMED_MSG
);
955 msg_set_hdr_sz(mhdr
, NAMED_H_SIZE
);
956 msg_set_nametype(mhdr
, type
);
957 msg_set_nameinst(mhdr
, inst
);
958 msg_set_lookup_scope(mhdr
, tipc_addr_scope(domain
));
959 dport
= tipc_nametbl_translate(type
, inst
, &dnode
);
960 msg_set_destnode(mhdr
, dnode
);
961 msg_set_destport(mhdr
, dport
);
962 if (unlikely(!dport
&& !dnode
)) {
966 } else if (dest
->addrtype
== TIPC_ADDR_ID
) {
967 dnode
= dest
->addr
.id
.node
;
968 msg_set_type(mhdr
, TIPC_DIRECT_MSG
);
969 msg_set_lookup_scope(mhdr
, 0);
970 msg_set_destnode(mhdr
, dnode
);
971 msg_set_destport(mhdr
, dest
->addr
.id
.ref
);
972 msg_set_hdr_sz(mhdr
, BASIC_H_SIZE
);
976 mtu
= tipc_node_get_mtu(dnode
, tsk
->ref
);
977 rc
= tipc_msg_build(mhdr
, iov
, 0, dsz
, mtu
, &buf
);
982 TIPC_SKB_CB(buf
)->wakeup_pending
= tsk
->link_cong
;
983 rc
= tipc_link_xmit(buf
, dnode
, tsk
->ref
);
984 if (likely(rc
>= 0)) {
985 if (sock
->state
!= SS_READY
)
986 sock
->state
= SS_CONNECTING
;
992 if (rc
!= -ELINKCONG
)
995 rc
= tipc_wait_for_sndmsg(sock
, &timeo
);
1006 static int tipc_wait_for_sndpkt(struct socket
*sock
, long *timeo_p
)
1008 struct sock
*sk
= sock
->sk
;
1009 struct tipc_sock
*tsk
= tipc_sk(sk
);
1014 int err
= sock_error(sk
);
1017 if (sock
->state
== SS_DISCONNECTING
)
1019 else if (sock
->state
!= SS_CONNECTED
)
1023 if (signal_pending(current
))
1024 return sock_intr_errno(*timeo_p
);
1026 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1027 done
= sk_wait_event(sk
, timeo_p
,
1029 !tsk_conn_cong(tsk
)) ||
1031 finish_wait(sk_sleep(sk
), &wait
);
1037 * tipc_send_stream - send stream-oriented data
1039 * @sock: socket structure
1041 * @dsz: total length of data to be transmitted
1043 * Used for SOCK_STREAM data.
1045 * Returns the number of bytes sent on success (or partial success),
1046 * or errno if no data sent
1048 static int tipc_send_stream(struct kiocb
*iocb
, struct socket
*sock
,
1049 struct msghdr
*m
, size_t dsz
)
1051 struct sock
*sk
= sock
->sk
;
1052 struct tipc_sock
*tsk
= tipc_sk(sk
);
1053 struct tipc_msg
*mhdr
= &tsk
->phdr
;
1054 struct sk_buff
*buf
;
1055 DECLARE_SOCKADDR(struct sockaddr_tipc
*, dest
, m
->msg_name
);
1060 uint mtu
, send
, sent
= 0;
1062 /* Handle implied connection establishment */
1063 if (unlikely(dest
)) {
1064 rc
= tipc_sendmsg(iocb
, sock
, m
, dsz
);
1065 if (dsz
&& (dsz
== rc
))
1066 tsk
->sent_unacked
= 1;
1069 if (dsz
> (uint
)INT_MAX
)
1075 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
1076 if (sock
->state
== SS_DISCONNECTING
)
1083 timeo
= sock_sndtimeo(sk
, m
->msg_flags
& MSG_DONTWAIT
);
1084 dnode
= tsk_peer_node(tsk
);
1088 send
= min_t(uint
, dsz
- sent
, TIPC_MAX_USER_MSG_SIZE
);
1089 rc
= tipc_msg_build(mhdr
, m
->msg_iov
, sent
, send
, mtu
, &buf
);
1090 if (unlikely(rc
< 0))
1093 if (likely(!tsk_conn_cong(tsk
))) {
1094 rc
= tipc_link_xmit(buf
, dnode
, ref
);
1096 tsk
->sent_unacked
++;
1102 if (rc
== -EMSGSIZE
) {
1103 tsk
->max_pkt
= tipc_node_get_mtu(dnode
, ref
);
1106 if (rc
!= -ELINKCONG
)
1110 rc
= tipc_wait_for_sndpkt(sock
, &timeo
);
1112 kfree_skb_list(buf
);
1117 return sent
? sent
: rc
;
1121 * tipc_send_packet - send a connection-oriented message
1122 * @iocb: if NULL, indicates that socket lock is already held
1123 * @sock: socket structure
1124 * @m: message to send
1125 * @dsz: length of data to be transmitted
1127 * Used for SOCK_SEQPACKET messages.
1129 * Returns the number of bytes sent on success, or errno otherwise
1131 static int tipc_send_packet(struct kiocb
*iocb
, struct socket
*sock
,
1132 struct msghdr
*m
, size_t dsz
)
1134 if (dsz
> TIPC_MAX_USER_MSG_SIZE
)
1137 return tipc_send_stream(iocb
, sock
, m
, dsz
);
1140 /* tipc_sk_finish_conn - complete the setup of a connection
1142 static void tipc_sk_finish_conn(struct tipc_sock
*tsk
, u32 peer_port
,
1145 struct tipc_msg
*msg
= &tsk
->phdr
;
1147 msg_set_destnode(msg
, peer_node
);
1148 msg_set_destport(msg
, peer_port
);
1149 msg_set_type(msg
, TIPC_CONN_MSG
);
1150 msg_set_lookup_scope(msg
, 0);
1151 msg_set_hdr_sz(msg
, SHORT_H_SIZE
);
1153 tsk
->probing_interval
= CONN_PROBING_INTERVAL
;
1154 tsk
->probing_state
= TIPC_CONN_OK
;
1156 k_start_timer(&tsk
->timer
, tsk
->probing_interval
);
1157 tipc_node_add_conn(peer_node
, tsk
->ref
, peer_port
);
1158 tsk
->max_pkt
= tipc_node_get_mtu(peer_node
, tsk
->ref
);
1162 * set_orig_addr - capture sender's address for received message
1163 * @m: descriptor for message info
1164 * @msg: received message header
1166 * Note: Address is not captured if not requested by receiver.
1168 static void set_orig_addr(struct msghdr
*m
, struct tipc_msg
*msg
)
1170 DECLARE_SOCKADDR(struct sockaddr_tipc
*, addr
, m
->msg_name
);
1173 addr
->family
= AF_TIPC
;
1174 addr
->addrtype
= TIPC_ADDR_ID
;
1175 memset(&addr
->addr
, 0, sizeof(addr
->addr
));
1176 addr
->addr
.id
.ref
= msg_origport(msg
);
1177 addr
->addr
.id
.node
= msg_orignode(msg
);
1178 addr
->addr
.name
.domain
= 0; /* could leave uninitialized */
1179 addr
->scope
= 0; /* could leave uninitialized */
1180 m
->msg_namelen
= sizeof(struct sockaddr_tipc
);
1185 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
1186 * @m: descriptor for message info
1187 * @msg: received message header
1188 * @tsk: TIPC port associated with message
1190 * Note: Ancillary data is not captured if not requested by receiver.
1192 * Returns 0 if successful, otherwise errno
1194 static int tipc_sk_anc_data_recv(struct msghdr
*m
, struct tipc_msg
*msg
,
1195 struct tipc_sock
*tsk
)
1203 if (likely(m
->msg_controllen
== 0))
1206 /* Optionally capture errored message object(s) */
1207 err
= msg
? msg_errcode(msg
) : 0;
1208 if (unlikely(err
)) {
1210 anc_data
[1] = msg_data_sz(msg
);
1211 res
= put_cmsg(m
, SOL_TIPC
, TIPC_ERRINFO
, 8, anc_data
);
1215 res
= put_cmsg(m
, SOL_TIPC
, TIPC_RETDATA
, anc_data
[1],
1222 /* Optionally capture message destination object */
1223 dest_type
= msg
? msg_type(msg
) : TIPC_DIRECT_MSG
;
1224 switch (dest_type
) {
1225 case TIPC_NAMED_MSG
:
1227 anc_data
[0] = msg_nametype(msg
);
1228 anc_data
[1] = msg_namelower(msg
);
1229 anc_data
[2] = msg_namelower(msg
);
1231 case TIPC_MCAST_MSG
:
1233 anc_data
[0] = msg_nametype(msg
);
1234 anc_data
[1] = msg_namelower(msg
);
1235 anc_data
[2] = msg_nameupper(msg
);
1238 has_name
= (tsk
->conn_type
!= 0);
1239 anc_data
[0] = tsk
->conn_type
;
1240 anc_data
[1] = tsk
->conn_instance
;
1241 anc_data
[2] = tsk
->conn_instance
;
1247 res
= put_cmsg(m
, SOL_TIPC
, TIPC_DESTNAME
, 12, anc_data
);
1255 static void tipc_sk_send_ack(struct tipc_sock
*tsk
, uint ack
)
1257 struct sk_buff
*buf
= NULL
;
1258 struct tipc_msg
*msg
;
1259 u32 peer_port
= tsk_peer_port(tsk
);
1260 u32 dnode
= tsk_peer_node(tsk
);
1262 if (!tsk
->connected
)
1264 buf
= tipc_msg_create(CONN_MANAGER
, CONN_ACK
, INT_H_SIZE
, 0, dnode
,
1265 tipc_own_addr
, peer_port
, tsk
->ref
, TIPC_OK
);
1269 msg_set_msgcnt(msg
, ack
);
1270 tipc_link_xmit(buf
, dnode
, msg_link_selector(msg
));
1273 static int tipc_wait_for_rcvmsg(struct socket
*sock
, long *timeop
)
1275 struct sock
*sk
= sock
->sk
;
1277 long timeo
= *timeop
;
1281 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1282 if (timeo
&& skb_queue_empty(&sk
->sk_receive_queue
)) {
1283 if (sock
->state
== SS_DISCONNECTING
) {
1288 timeo
= schedule_timeout(timeo
);
1292 if (!skb_queue_empty(&sk
->sk_receive_queue
))
1294 err
= sock_intr_errno(timeo
);
1295 if (signal_pending(current
))
1301 finish_wait(sk_sleep(sk
), &wait
);
1307 * tipc_recvmsg - receive packet-oriented message
1309 * @m: descriptor for message info
1310 * @buf_len: total size of user buffer area
1311 * @flags: receive flags
1313 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1314 * If the complete message doesn't fit in user area, truncate it.
1316 * Returns size of returned message data, errno otherwise
1318 static int tipc_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1319 struct msghdr
*m
, size_t buf_len
, int flags
)
1321 struct sock
*sk
= sock
->sk
;
1322 struct tipc_sock
*tsk
= tipc_sk(sk
);
1323 struct sk_buff
*buf
;
1324 struct tipc_msg
*msg
;
1330 /* Catch invalid receive requests */
1331 if (unlikely(!buf_len
))
1336 if (unlikely(sock
->state
== SS_UNCONNECTED
)) {
1341 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
1344 /* Look for a message in receive queue; wait if necessary */
1345 res
= tipc_wait_for_rcvmsg(sock
, &timeo
);
1349 /* Look at first message in receive queue */
1350 buf
= skb_peek(&sk
->sk_receive_queue
);
1352 sz
= msg_data_sz(msg
);
1353 err
= msg_errcode(msg
);
1355 /* Discard an empty non-errored message & try again */
1356 if ((!sz
) && (!err
)) {
1357 tsk_advance_rx_queue(sk
);
1361 /* Capture sender's address (optional) */
1362 set_orig_addr(m
, msg
);
1364 /* Capture ancillary data (optional) */
1365 res
= tipc_sk_anc_data_recv(m
, msg
, tsk
);
1369 /* Capture message data (if valid) & compute return value (always) */
1371 if (unlikely(buf_len
< sz
)) {
1373 m
->msg_flags
|= MSG_TRUNC
;
1375 res
= skb_copy_datagram_msg(buf
, msg_hdr_sz(msg
), m
, sz
);
1380 if ((sock
->state
== SS_READY
) ||
1381 ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
))
1387 /* Consume received message (optional) */
1388 if (likely(!(flags
& MSG_PEEK
))) {
1389 if ((sock
->state
!= SS_READY
) &&
1390 (++tsk
->rcv_unacked
>= TIPC_CONNACK_INTV
)) {
1391 tipc_sk_send_ack(tsk
, tsk
->rcv_unacked
);
1392 tsk
->rcv_unacked
= 0;
1394 tsk_advance_rx_queue(sk
);
1402 * tipc_recv_stream - receive stream-oriented data
1404 * @m: descriptor for message info
1405 * @buf_len: total size of user buffer area
1406 * @flags: receive flags
1408 * Used for SOCK_STREAM messages only. If not enough data is available
1409 * will optionally wait for more; never truncates data.
1411 * Returns size of returned message data, errno otherwise
1413 static int tipc_recv_stream(struct kiocb
*iocb
, struct socket
*sock
,
1414 struct msghdr
*m
, size_t buf_len
, int flags
)
1416 struct sock
*sk
= sock
->sk
;
1417 struct tipc_sock
*tsk
= tipc_sk(sk
);
1418 struct sk_buff
*buf
;
1419 struct tipc_msg
*msg
;
1422 int sz_to_copy
, target
, needed
;
1427 /* Catch invalid receive attempts */
1428 if (unlikely(!buf_len
))
1433 if (unlikely(sock
->state
== SS_UNCONNECTED
)) {
1438 target
= sock_rcvlowat(sk
, flags
& MSG_WAITALL
, buf_len
);
1439 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
1442 /* Look for a message in receive queue; wait if necessary */
1443 res
= tipc_wait_for_rcvmsg(sock
, &timeo
);
1447 /* Look at first message in receive queue */
1448 buf
= skb_peek(&sk
->sk_receive_queue
);
1450 sz
= msg_data_sz(msg
);
1451 err
= msg_errcode(msg
);
1453 /* Discard an empty non-errored message & try again */
1454 if ((!sz
) && (!err
)) {
1455 tsk_advance_rx_queue(sk
);
1459 /* Optionally capture sender's address & ancillary data of first msg */
1460 if (sz_copied
== 0) {
1461 set_orig_addr(m
, msg
);
1462 res
= tipc_sk_anc_data_recv(m
, msg
, tsk
);
1467 /* Capture message data (if valid) & compute return value (always) */
1469 u32 offset
= (u32
)(unsigned long)(TIPC_SKB_CB(buf
)->handle
);
1472 needed
= (buf_len
- sz_copied
);
1473 sz_to_copy
= (sz
<= needed
) ? sz
: needed
;
1475 res
= skb_copy_datagram_msg(buf
, msg_hdr_sz(msg
) + offset
,
1480 sz_copied
+= sz_to_copy
;
1482 if (sz_to_copy
< sz
) {
1483 if (!(flags
& MSG_PEEK
))
1484 TIPC_SKB_CB(buf
)->handle
=
1485 (void *)(unsigned long)(offset
+ sz_to_copy
);
1490 goto exit
; /* can't add error msg to valid data */
1492 if ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
)
1498 /* Consume received message (optional) */
1499 if (likely(!(flags
& MSG_PEEK
))) {
1500 if (unlikely(++tsk
->rcv_unacked
>= TIPC_CONNACK_INTV
)) {
1501 tipc_sk_send_ack(tsk
, tsk
->rcv_unacked
);
1502 tsk
->rcv_unacked
= 0;
1504 tsk_advance_rx_queue(sk
);
1507 /* Loop around if more data is required */
1508 if ((sz_copied
< buf_len
) && /* didn't get all requested data */
1509 (!skb_queue_empty(&sk
->sk_receive_queue
) ||
1510 (sz_copied
< target
)) && /* and more is ready or required */
1511 (!(flags
& MSG_PEEK
)) && /* and aren't just peeking at data */
1512 (!err
)) /* and haven't reached a FIN */
1517 return sz_copied
? sz_copied
: res
;
1521 * tipc_write_space - wake up thread if port congestion is released
1524 static void tipc_write_space(struct sock
*sk
)
1526 struct socket_wq
*wq
;
1529 wq
= rcu_dereference(sk
->sk_wq
);
1530 if (wq_has_sleeper(wq
))
1531 wake_up_interruptible_sync_poll(&wq
->wait
, POLLOUT
|
1532 POLLWRNORM
| POLLWRBAND
);
1537 * tipc_data_ready - wake up threads to indicate messages have been received
1539 * @len: the length of messages
1541 static void tipc_data_ready(struct sock
*sk
)
1543 struct socket_wq
*wq
;
1546 wq
= rcu_dereference(sk
->sk_wq
);
1547 if (wq_has_sleeper(wq
))
1548 wake_up_interruptible_sync_poll(&wq
->wait
, POLLIN
|
1549 POLLRDNORM
| POLLRDBAND
);
1554 * filter_connect - Handle all incoming messages for a connection-based socket
1558 * Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
1560 static int filter_connect(struct tipc_sock
*tsk
, struct sk_buff
**buf
)
1562 struct sock
*sk
= &tsk
->sk
;
1563 struct socket
*sock
= sk
->sk_socket
;
1564 struct tipc_msg
*msg
= buf_msg(*buf
);
1565 int retval
= -TIPC_ERR_NO_PORT
;
1570 switch ((int)sock
->state
) {
1572 /* Accept only connection-based messages sent by peer */
1573 if (tsk_peer_msg(tsk
, msg
)) {
1574 if (unlikely(msg_errcode(msg
))) {
1575 sock
->state
= SS_DISCONNECTING
;
1577 /* let timer expire on it's own */
1578 tipc_node_remove_conn(tsk_peer_node(tsk
),
1585 /* Accept only ACK or NACK message */
1587 if (unlikely(!msg_connected(msg
)))
1590 if (unlikely(msg_errcode(msg
))) {
1591 sock
->state
= SS_DISCONNECTING
;
1592 sk
->sk_err
= ECONNREFUSED
;
1597 if (unlikely(msg_importance(msg
) > TIPC_CRITICAL_IMPORTANCE
)) {
1598 sock
->state
= SS_DISCONNECTING
;
1599 sk
->sk_err
= EINVAL
;
1604 tipc_sk_finish_conn(tsk
, msg_origport(msg
), msg_orignode(msg
));
1605 msg_set_importance(&tsk
->phdr
, msg_importance(msg
));
1606 sock
->state
= SS_CONNECTED
;
1608 /* If an incoming message is an 'ACK-', it should be
1609 * discarded here because it doesn't contain useful
1610 * data. In addition, we should try to wake up
1611 * connect() routine if sleeping.
1613 if (msg_data_sz(msg
) == 0) {
1616 if (waitqueue_active(sk_sleep(sk
)))
1617 wake_up_interruptible(sk_sleep(sk
));
1622 case SS_UNCONNECTED
:
1623 /* Accept only SYN message */
1624 if (!msg_connected(msg
) && !(msg_errcode(msg
)))
1627 case SS_DISCONNECTING
:
1630 pr_err("Unknown socket state %u\n", sock
->state
);
1636 * rcvbuf_limit - get proper overload limit of socket receive queue
1640 * For all connection oriented messages, irrespective of importance,
1641 * the default overload value (i.e. 67MB) is set as limit.
1643 * For all connectionless messages, by default new queue limits are
1646 * TIPC_LOW_IMPORTANCE (4 MB)
1647 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1648 * TIPC_HIGH_IMPORTANCE (16 MB)
1649 * TIPC_CRITICAL_IMPORTANCE (32 MB)
1651 * Returns overload limit according to corresponding message importance
1653 static unsigned int rcvbuf_limit(struct sock
*sk
, struct sk_buff
*buf
)
1655 struct tipc_msg
*msg
= buf_msg(buf
);
1657 if (msg_connected(msg
))
1658 return sysctl_tipc_rmem
[2];
1660 return sk
->sk_rcvbuf
>> TIPC_CRITICAL_IMPORTANCE
<<
1661 msg_importance(msg
);
1665 * filter_rcv - validate incoming message
1669 * Enqueues message on receive queue if acceptable; optionally handles
1670 * disconnect indication for a connected socket.
1672 * Called with socket lock already taken; port lock may also be taken.
1674 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
1675 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
1677 static int filter_rcv(struct sock
*sk
, struct sk_buff
*buf
)
1679 struct socket
*sock
= sk
->sk_socket
;
1680 struct tipc_sock
*tsk
= tipc_sk(sk
);
1681 struct tipc_msg
*msg
= buf_msg(buf
);
1682 unsigned int limit
= rcvbuf_limit(sk
, buf
);
1686 if (unlikely(msg_user(msg
) == CONN_MANAGER
))
1687 return tipc_sk_proto_rcv(tsk
, &onode
, buf
);
1689 if (unlikely(msg_user(msg
) == SOCK_WAKEUP
)) {
1692 sk
->sk_write_space(sk
);
1696 /* Reject message if it is wrong sort of message for socket */
1697 if (msg_type(msg
) > TIPC_DIRECT_MSG
)
1698 return -TIPC_ERR_NO_PORT
;
1700 if (sock
->state
== SS_READY
) {
1701 if (msg_connected(msg
))
1702 return -TIPC_ERR_NO_PORT
;
1704 rc
= filter_connect(tsk
, &buf
);
1705 if (rc
!= TIPC_OK
|| buf
== NULL
)
1709 /* Reject message if there isn't room to queue it */
1710 if (sk_rmem_alloc_get(sk
) + buf
->truesize
>= limit
)
1711 return -TIPC_ERR_OVERLOAD
;
1713 /* Enqueue message */
1714 TIPC_SKB_CB(buf
)->handle
= NULL
;
1715 __skb_queue_tail(&sk
->sk_receive_queue
, buf
);
1716 skb_set_owner_r(buf
, sk
);
1718 sk
->sk_data_ready(sk
);
1723 * tipc_backlog_rcv - handle incoming message from backlog queue
1727 * Caller must hold socket lock, but not port lock.
1731 static int tipc_backlog_rcv(struct sock
*sk
, struct sk_buff
*buf
)
1735 struct tipc_sock
*tsk
= tipc_sk(sk
);
1736 uint truesize
= buf
->truesize
;
1738 rc
= filter_rcv(sk
, buf
);
1741 if (atomic_read(&tsk
->dupl_rcvcnt
) < TIPC_CONN_OVERLOAD_LIMIT
)
1742 atomic_add(truesize
, &tsk
->dupl_rcvcnt
);
1746 if ((rc
< 0) && !tipc_msg_reverse(buf
, &onode
, -rc
))
1749 tipc_link_xmit(buf
, onode
, 0);
1755 * tipc_sk_rcv - handle incoming message
1756 * @buf: buffer containing arriving message
1758 * Returns 0 if success, or errno: -EHOSTUNREACH
1760 int tipc_sk_rcv(struct sk_buff
*buf
)
1762 struct tipc_sock
*tsk
;
1764 u32 dport
= msg_destport(buf_msg(buf
));
1769 /* Validate destination and message */
1770 tsk
= tipc_sk_get(dport
);
1771 if (unlikely(!tsk
)) {
1772 rc
= tipc_msg_eval(buf
, &dnode
);
1778 spin_lock_bh(&sk
->sk_lock
.slock
);
1780 if (!sock_owned_by_user(sk
)) {
1781 rc
= filter_rcv(sk
, buf
);
1783 if (sk
->sk_backlog
.len
== 0)
1784 atomic_set(&tsk
->dupl_rcvcnt
, 0);
1785 limit
= rcvbuf_limit(sk
, buf
) + atomic_read(&tsk
->dupl_rcvcnt
);
1786 if (sk_add_backlog(sk
, buf
, limit
))
1787 rc
= -TIPC_ERR_OVERLOAD
;
1789 spin_unlock_bh(&sk
->sk_lock
.slock
);
1794 if ((rc
< 0) && !tipc_msg_reverse(buf
, &dnode
, -rc
))
1795 return -EHOSTUNREACH
;
1797 tipc_link_xmit(buf
, dnode
, 0);
1798 return (rc
< 0) ? -EHOSTUNREACH
: 0;
1801 static int tipc_wait_for_connect(struct socket
*sock
, long *timeo_p
)
1803 struct sock
*sk
= sock
->sk
;
1808 int err
= sock_error(sk
);
1813 if (signal_pending(current
))
1814 return sock_intr_errno(*timeo_p
);
1816 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1817 done
= sk_wait_event(sk
, timeo_p
, sock
->state
!= SS_CONNECTING
);
1818 finish_wait(sk_sleep(sk
), &wait
);
1824 * tipc_connect - establish a connection to another TIPC port
1825 * @sock: socket structure
1826 * @dest: socket address for destination port
1827 * @destlen: size of socket address data structure
1828 * @flags: file-related flags associated with socket
1830 * Returns 0 on success, errno otherwise
1832 static int tipc_connect(struct socket
*sock
, struct sockaddr
*dest
,
1833 int destlen
, int flags
)
1835 struct sock
*sk
= sock
->sk
;
1836 struct sockaddr_tipc
*dst
= (struct sockaddr_tipc
*)dest
;
1837 struct msghdr m
= {NULL
,};
1838 long timeout
= (flags
& O_NONBLOCK
) ? 0 : tipc_sk(sk
)->conn_timeout
;
1839 socket_state previous
;
1844 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
1845 if (sock
->state
== SS_READY
) {
1851 * Reject connection attempt using multicast address
1853 * Note: send_msg() validates the rest of the address fields,
1854 * so there's no need to do it here
1856 if (dst
->addrtype
== TIPC_ADDR_MCAST
) {
1861 previous
= sock
->state
;
1862 switch (sock
->state
) {
1863 case SS_UNCONNECTED
:
1864 /* Send a 'SYN-' to destination */
1866 m
.msg_namelen
= destlen
;
1868 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1869 * indicate send_msg() is never blocked.
1872 m
.msg_flags
= MSG_DONTWAIT
;
1874 res
= tipc_sendmsg(NULL
, sock
, &m
, 0);
1875 if ((res
< 0) && (res
!= -EWOULDBLOCK
))
1878 /* Just entered SS_CONNECTING state; the only
1879 * difference is that return value in non-blocking
1880 * case is EINPROGRESS, rather than EALREADY.
1884 if (previous
== SS_CONNECTING
)
1888 timeout
= msecs_to_jiffies(timeout
);
1889 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1890 res
= tipc_wait_for_connect(sock
, &timeout
);
1905 * tipc_listen - allow socket to listen for incoming connections
1906 * @sock: socket structure
1909 * Returns 0 on success, errno otherwise
1911 static int tipc_listen(struct socket
*sock
, int len
)
1913 struct sock
*sk
= sock
->sk
;
1918 if (sock
->state
!= SS_UNCONNECTED
)
1921 sock
->state
= SS_LISTENING
;
1929 static int tipc_wait_for_accept(struct socket
*sock
, long timeo
)
1931 struct sock
*sk
= sock
->sk
;
1935 /* True wake-one mechanism for incoming connections: only
1936 * one process gets woken up, not the 'whole herd'.
1937 * Since we do not 'race & poll' for established sockets
1938 * anymore, the common case will execute the loop only once.
1941 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
,
1942 TASK_INTERRUPTIBLE
);
1943 if (timeo
&& skb_queue_empty(&sk
->sk_receive_queue
)) {
1945 timeo
= schedule_timeout(timeo
);
1949 if (!skb_queue_empty(&sk
->sk_receive_queue
))
1952 if (sock
->state
!= SS_LISTENING
)
1954 err
= sock_intr_errno(timeo
);
1955 if (signal_pending(current
))
1961 finish_wait(sk_sleep(sk
), &wait
);
1966 * tipc_accept - wait for connection request
1967 * @sock: listening socket
1968 * @newsock: new socket that is to be connected
1969 * @flags: file-related flags associated with socket
1971 * Returns 0 on success, errno otherwise
1973 static int tipc_accept(struct socket
*sock
, struct socket
*new_sock
, int flags
)
1975 struct sock
*new_sk
, *sk
= sock
->sk
;
1976 struct sk_buff
*buf
;
1977 struct tipc_sock
*new_tsock
;
1978 struct tipc_msg
*msg
;
1984 if (sock
->state
!= SS_LISTENING
) {
1988 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
1989 res
= tipc_wait_for_accept(sock
, timeo
);
1993 buf
= skb_peek(&sk
->sk_receive_queue
);
1995 res
= tipc_sk_create(sock_net(sock
->sk
), new_sock
, 0, 1);
1999 new_sk
= new_sock
->sk
;
2000 new_tsock
= tipc_sk(new_sk
);
2003 /* we lock on new_sk; but lockdep sees the lock on sk */
2004 lock_sock_nested(new_sk
, SINGLE_DEPTH_NESTING
);
2007 * Reject any stray messages received by new socket
2008 * before the socket lock was taken (very, very unlikely)
2010 tsk_rej_rx_queue(new_sk
);
2012 /* Connect new socket to it's peer */
2013 tipc_sk_finish_conn(new_tsock
, msg_origport(msg
), msg_orignode(msg
));
2014 new_sock
->state
= SS_CONNECTED
;
2016 tsk_set_importance(new_tsock
, msg_importance(msg
));
2017 if (msg_named(msg
)) {
2018 new_tsock
->conn_type
= msg_nametype(msg
);
2019 new_tsock
->conn_instance
= msg_nameinst(msg
);
2023 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2024 * Respond to 'SYN+' by queuing it on new socket.
2026 if (!msg_data_sz(msg
)) {
2027 struct msghdr m
= {NULL
,};
2029 tsk_advance_rx_queue(sk
);
2030 tipc_send_packet(NULL
, new_sock
, &m
, 0);
2032 __skb_dequeue(&sk
->sk_receive_queue
);
2033 __skb_queue_head(&new_sk
->sk_receive_queue
, buf
);
2034 skb_set_owner_r(buf
, new_sk
);
2036 release_sock(new_sk
);
2043 * tipc_shutdown - shutdown socket connection
2044 * @sock: socket structure
2045 * @how: direction to close (must be SHUT_RDWR)
2047 * Terminates connection (if necessary), then purges socket's receive queue.
2049 * Returns 0 on success, errno otherwise
2051 static int tipc_shutdown(struct socket
*sock
, int how
)
2053 struct sock
*sk
= sock
->sk
;
2054 struct tipc_sock
*tsk
= tipc_sk(sk
);
2055 struct sk_buff
*buf
;
2059 if (how
!= SHUT_RDWR
)
2064 switch (sock
->state
) {
2069 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
2070 buf
= __skb_dequeue(&sk
->sk_receive_queue
);
2072 if (TIPC_SKB_CB(buf
)->handle
!= NULL
) {
2076 if (tipc_msg_reverse(buf
, &dnode
, TIPC_CONN_SHUTDOWN
))
2077 tipc_link_xmit(buf
, dnode
, tsk
->ref
);
2078 tipc_node_remove_conn(dnode
, tsk
->ref
);
2080 dnode
= tsk_peer_node(tsk
);
2081 buf
= tipc_msg_create(TIPC_CRITICAL_IMPORTANCE
,
2082 TIPC_CONN_MSG
, SHORT_H_SIZE
,
2083 0, dnode
, tipc_own_addr
,
2085 tsk
->ref
, TIPC_CONN_SHUTDOWN
);
2086 tipc_link_xmit(buf
, dnode
, tsk
->ref
);
2089 sock
->state
= SS_DISCONNECTING
;
2090 tipc_node_remove_conn(dnode
, tsk
->ref
);
2093 case SS_DISCONNECTING
:
2095 /* Discard any unreceived messages */
2096 __skb_queue_purge(&sk
->sk_receive_queue
);
2098 /* Wake up anyone sleeping in poll */
2099 sk
->sk_state_change(sk
);
2111 static void tipc_sk_timeout(unsigned long ref
)
2113 struct tipc_sock
*tsk
;
2115 struct sk_buff
*buf
= NULL
;
2116 u32 peer_port
, peer_node
;
2118 tsk
= tipc_sk_get(ref
);
2124 if (!tsk
->connected
) {
2128 peer_port
= tsk_peer_port(tsk
);
2129 peer_node
= tsk_peer_node(tsk
);
2131 if (tsk
->probing_state
== TIPC_CONN_PROBING
) {
2132 /* Previous probe not answered -> self abort */
2133 buf
= tipc_msg_create(TIPC_CRITICAL_IMPORTANCE
, TIPC_CONN_MSG
,
2134 SHORT_H_SIZE
, 0, tipc_own_addr
,
2135 peer_node
, ref
, peer_port
,
2138 buf
= tipc_msg_create(CONN_MANAGER
, CONN_PROBE
, INT_H_SIZE
,
2139 0, peer_node
, tipc_own_addr
,
2140 peer_port
, ref
, TIPC_OK
);
2141 tsk
->probing_state
= TIPC_CONN_PROBING
;
2142 k_start_timer(&tsk
->timer
, tsk
->probing_interval
);
2146 tipc_link_xmit(buf
, peer_node
, ref
);
2151 static int tipc_sk_publish(struct tipc_sock
*tsk
, uint scope
,
2152 struct tipc_name_seq
const *seq
)
2154 struct publication
*publ
;
2159 key
= tsk
->ref
+ tsk
->pub_count
+ 1;
2160 if (key
== tsk
->ref
)
2163 publ
= tipc_nametbl_publish(seq
->type
, seq
->lower
, seq
->upper
,
2164 scope
, tsk
->ref
, key
);
2165 if (unlikely(!publ
))
2168 list_add(&publ
->pport_list
, &tsk
->publications
);
2174 static int tipc_sk_withdraw(struct tipc_sock
*tsk
, uint scope
,
2175 struct tipc_name_seq
const *seq
)
2177 struct publication
*publ
;
2178 struct publication
*safe
;
2181 list_for_each_entry_safe(publ
, safe
, &tsk
->publications
, pport_list
) {
2183 if (publ
->scope
!= scope
)
2185 if (publ
->type
!= seq
->type
)
2187 if (publ
->lower
!= seq
->lower
)
2189 if (publ
->upper
!= seq
->upper
)
2191 tipc_nametbl_withdraw(publ
->type
, publ
->lower
,
2192 publ
->ref
, publ
->key
);
2196 tipc_nametbl_withdraw(publ
->type
, publ
->lower
,
2197 publ
->ref
, publ
->key
);
2200 if (list_empty(&tsk
->publications
))
2205 static int tipc_sk_show(struct tipc_sock
*tsk
, char *buf
,
2206 int len
, int full_id
)
2208 struct publication
*publ
;
2212 ret
= tipc_snprintf(buf
, len
, "<%u.%u.%u:%u>:",
2213 tipc_zone(tipc_own_addr
),
2214 tipc_cluster(tipc_own_addr
),
2215 tipc_node(tipc_own_addr
), tsk
->ref
);
2217 ret
= tipc_snprintf(buf
, len
, "%-10u:", tsk
->ref
);
2219 if (tsk
->connected
) {
2220 u32 dport
= tsk_peer_port(tsk
);
2221 u32 destnode
= tsk_peer_node(tsk
);
2223 ret
+= tipc_snprintf(buf
+ ret
, len
- ret
,
2224 " connected to <%u.%u.%u:%u>",
2225 tipc_zone(destnode
),
2226 tipc_cluster(destnode
),
2227 tipc_node(destnode
), dport
);
2228 if (tsk
->conn_type
!= 0)
2229 ret
+= tipc_snprintf(buf
+ ret
, len
- ret
,
2230 " via {%u,%u}", tsk
->conn_type
,
2231 tsk
->conn_instance
);
2232 } else if (tsk
->published
) {
2233 ret
+= tipc_snprintf(buf
+ ret
, len
- ret
, " bound to");
2234 list_for_each_entry(publ
, &tsk
->publications
, pport_list
) {
2235 if (publ
->lower
== publ
->upper
)
2236 ret
+= tipc_snprintf(buf
+ ret
, len
- ret
,
2237 " {%u,%u}", publ
->type
,
2240 ret
+= tipc_snprintf(buf
+ ret
, len
- ret
,
2241 " {%u,%u,%u}", publ
->type
,
2242 publ
->lower
, publ
->upper
);
2245 ret
+= tipc_snprintf(buf
+ ret
, len
- ret
, "\n");
2249 struct sk_buff
*tipc_sk_socks_show(void)
2251 struct sk_buff
*buf
;
2252 struct tlv_desc
*rep_tlv
;
2255 struct tipc_sock
*tsk
;
2259 buf
= tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN
));
2262 rep_tlv
= (struct tlv_desc
*)buf
->data
;
2263 pb
= TLV_DATA(rep_tlv
);
2264 pb_len
= ULTRA_STRING_MAX_LEN
;
2266 tsk
= tipc_sk_get_next(&ref
);
2267 for (; tsk
; tsk
= tipc_sk_get_next(&ref
)) {
2268 lock_sock(&tsk
->sk
);
2269 str_len
+= tipc_sk_show(tsk
, pb
+ str_len
,
2270 pb_len
- str_len
, 0);
2271 release_sock(&tsk
->sk
);
2274 str_len
+= 1; /* for "\0" */
2275 skb_put(buf
, TLV_SPACE(str_len
));
2276 TLV_SET(rep_tlv
, TIPC_TLV_ULTRA_STRING
, NULL
, str_len
);
2281 /* tipc_sk_reinit: set non-zero address in all existing sockets
2282 * when we go from standalone to network mode.
2284 void tipc_sk_reinit(void)
2286 struct tipc_msg
*msg
;
2288 struct tipc_sock
*tsk
= tipc_sk_get_next(&ref
);
2290 for (; tsk
; tsk
= tipc_sk_get_next(&ref
)) {
2291 lock_sock(&tsk
->sk
);
2293 msg_set_prevnode(msg
, tipc_own_addr
);
2294 msg_set_orignode(msg
, tipc_own_addr
);
2295 release_sock(&tsk
->sk
);
2301 * struct reference - TIPC socket reference entry
2302 * @tsk: pointer to socket associated with reference entry
2303 * @ref: reference value for socket (combines instance & array index info)
2306 struct tipc_sock
*tsk
;
2311 * struct tipc_ref_table - table of TIPC socket reference entries
2312 * @entries: pointer to array of reference entries
2313 * @capacity: array index of first unusable entry
2314 * @init_point: array index of first uninitialized entry
2315 * @first_free: array index of first unused socket reference entry
2316 * @last_free: array index of last unused socket reference entry
2317 * @index_mask: bitmask for array index portion of reference values
2318 * @start_mask: initial value for instance value portion of reference values
2321 struct reference
*entries
;
2330 /* Socket reference table consists of 2**N entries.
2332 * State Socket ptr Reference
2333 * ----- ---------- ---------
2334 * In use non-NULL XXXX|own index
2335 * (XXXX changes each time entry is acquired)
2336 * Free NULL YYYY|next free index
2337 * (YYYY is one more than last used XXXX)
2338 * Uninitialized NULL 0
2340 * Entry 0 is not used; this allows index 0 to denote the end of the free list.
2342 * Note that a reference value of 0 does not necessarily indicate that an
2343 * entry is uninitialized, since the last entry in the free list could also
2344 * have a reference value of 0 (although this is unlikely).
2347 static struct ref_table tipc_ref_table
;
2349 static DEFINE_RWLOCK(ref_table_lock
);
2352 * tipc_ref_table_init - create reference table for sockets
2354 int tipc_sk_ref_table_init(u32 req_sz
, u32 start
)
2356 struct reference
*table
;
2359 /* account for unused entry, then round up size to a power of 2 */
2362 for (actual_sz
= 16; actual_sz
< req_sz
; actual_sz
<<= 1) {
2366 /* allocate table & mark all entries as uninitialized */
2367 table
= vzalloc(actual_sz
* sizeof(struct reference
));
2371 tipc_ref_table
.entries
= table
;
2372 tipc_ref_table
.capacity
= req_sz
;
2373 tipc_ref_table
.init_point
= 1;
2374 tipc_ref_table
.first_free
= 0;
2375 tipc_ref_table
.last_free
= 0;
2376 tipc_ref_table
.index_mask
= actual_sz
- 1;
2377 tipc_ref_table
.start_mask
= start
& ~tipc_ref_table
.index_mask
;
2383 * tipc_ref_table_stop - destroy reference table for sockets
2385 void tipc_sk_ref_table_stop(void)
2387 if (!tipc_ref_table
.entries
)
2389 vfree(tipc_ref_table
.entries
);
2390 tipc_ref_table
.entries
= NULL
;
2393 /* tipc_ref_acquire - create reference to a socket
2395 * Register an socket pointer in the reference table.
2396 * Returns a unique reference value that is used from then on to retrieve the
2397 * socket pointer, or to determine if the socket has been deregistered.
2399 u32
tipc_sk_ref_acquire(struct tipc_sock
*tsk
)
2403 u32 next_plus_upper
;
2405 struct reference
*entry
;
2407 if (unlikely(!tsk
)) {
2408 pr_err("Attempt to acquire ref. to non-existent obj\n");
2411 if (unlikely(!tipc_ref_table
.entries
)) {
2412 pr_err("Ref. table not found in acquisition attempt\n");
2416 /* Take a free entry, if available; otherwise initialize a new one */
2417 write_lock_bh(&ref_table_lock
);
2418 index
= tipc_ref_table
.first_free
;
2419 entry
= &tipc_ref_table
.entries
[index
];
2421 if (likely(index
)) {
2422 index
= tipc_ref_table
.first_free
;
2423 entry
= &tipc_ref_table
.entries
[index
];
2424 index_mask
= tipc_ref_table
.index_mask
;
2425 next_plus_upper
= entry
->ref
;
2426 tipc_ref_table
.first_free
= next_plus_upper
& index_mask
;
2427 ref
= (next_plus_upper
& ~index_mask
) + index
;
2429 } else if (tipc_ref_table
.init_point
< tipc_ref_table
.capacity
) {
2430 index
= tipc_ref_table
.init_point
++;
2431 entry
= &tipc_ref_table
.entries
[index
];
2432 ref
= tipc_ref_table
.start_mask
+ index
;
2439 write_unlock_bh(&ref_table_lock
);
2443 /* tipc_sk_ref_discard - invalidate reference to an socket
2445 * Disallow future references to an socket and free up the entry for re-use.
2447 void tipc_sk_ref_discard(u32 ref
)
2449 struct reference
*entry
;
2453 if (unlikely(!tipc_ref_table
.entries
)) {
2454 pr_err("Ref. table not found during discard attempt\n");
2458 index_mask
= tipc_ref_table
.index_mask
;
2459 index
= ref
& index_mask
;
2460 entry
= &tipc_ref_table
.entries
[index
];
2462 write_lock_bh(&ref_table_lock
);
2464 if (unlikely(!entry
->tsk
)) {
2465 pr_err("Attempt to discard ref. to non-existent socket\n");
2468 if (unlikely(entry
->ref
!= ref
)) {
2469 pr_err("Attempt to discard non-existent reference\n");
2473 /* Mark entry as unused; increment instance part of entry's
2474 * reference to invalidate any subsequent references
2478 entry
->ref
= (ref
& ~index_mask
) + (index_mask
+ 1);
2480 /* Append entry to free entry list */
2481 if (unlikely(tipc_ref_table
.first_free
== 0))
2482 tipc_ref_table
.first_free
= index
;
2484 tipc_ref_table
.entries
[tipc_ref_table
.last_free
].ref
|= index
;
2485 tipc_ref_table
.last_free
= index
;
2487 write_unlock_bh(&ref_table_lock
);
2490 /* tipc_sk_get - find referenced socket and return pointer to it
2492 struct tipc_sock
*tipc_sk_get(u32 ref
)
2494 struct reference
*entry
;
2495 struct tipc_sock
*tsk
;
2497 if (unlikely(!tipc_ref_table
.entries
))
2499 read_lock_bh(&ref_table_lock
);
2500 entry
= &tipc_ref_table
.entries
[ref
& tipc_ref_table
.index_mask
];
2502 if (likely(tsk
&& (entry
->ref
== ref
)))
2503 sock_hold(&tsk
->sk
);
2506 read_unlock_bh(&ref_table_lock
);
2510 /* tipc_sk_get_next - lock & return next socket after referenced one
2512 struct tipc_sock
*tipc_sk_get_next(u32
*ref
)
2514 struct reference
*entry
;
2515 struct tipc_sock
*tsk
= NULL
;
2516 uint index
= *ref
& tipc_ref_table
.index_mask
;
2518 read_lock_bh(&ref_table_lock
);
2519 while (++index
< tipc_ref_table
.capacity
) {
2520 entry
= &tipc_ref_table
.entries
[index
];
2524 sock_hold(&tsk
->sk
);
2528 read_unlock_bh(&ref_table_lock
);
2532 static void tipc_sk_put(struct tipc_sock
*tsk
)
2538 * tipc_setsockopt - set socket option
2539 * @sock: socket structure
2540 * @lvl: option level
2541 * @opt: option identifier
2542 * @ov: pointer to new option value
2543 * @ol: length of option value
2545 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
2546 * (to ease compatibility).
2548 * Returns 0 on success, errno otherwise
2550 static int tipc_setsockopt(struct socket
*sock
, int lvl
, int opt
,
2551 char __user
*ov
, unsigned int ol
)
2553 struct sock
*sk
= sock
->sk
;
2554 struct tipc_sock
*tsk
= tipc_sk(sk
);
2558 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
2560 if (lvl
!= SOL_TIPC
)
2561 return -ENOPROTOOPT
;
2562 if (ol
< sizeof(value
))
2564 res
= get_user(value
, (u32 __user
*)ov
);
2571 case TIPC_IMPORTANCE
:
2572 res
= tsk_set_importance(tsk
, value
);
2574 case TIPC_SRC_DROPPABLE
:
2575 if (sock
->type
!= SOCK_STREAM
)
2576 tsk_set_unreliable(tsk
, value
);
2580 case TIPC_DEST_DROPPABLE
:
2581 tsk_set_unreturnable(tsk
, value
);
2583 case TIPC_CONN_TIMEOUT
:
2584 tipc_sk(sk
)->conn_timeout
= value
;
2585 /* no need to set "res", since already 0 at this point */
2597 * tipc_getsockopt - get socket option
2598 * @sock: socket structure
2599 * @lvl: option level
2600 * @opt: option identifier
2601 * @ov: receptacle for option value
2602 * @ol: receptacle for length of option value
2604 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
2605 * (to ease compatibility).
2607 * Returns 0 on success, errno otherwise
2609 static int tipc_getsockopt(struct socket
*sock
, int lvl
, int opt
,
2610 char __user
*ov
, int __user
*ol
)
2612 struct sock
*sk
= sock
->sk
;
2613 struct tipc_sock
*tsk
= tipc_sk(sk
);
2618 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
2619 return put_user(0, ol
);
2620 if (lvl
!= SOL_TIPC
)
2621 return -ENOPROTOOPT
;
2622 res
= get_user(len
, ol
);
2629 case TIPC_IMPORTANCE
:
2630 value
= tsk_importance(tsk
);
2632 case TIPC_SRC_DROPPABLE
:
2633 value
= tsk_unreliable(tsk
);
2635 case TIPC_DEST_DROPPABLE
:
2636 value
= tsk_unreturnable(tsk
);
2638 case TIPC_CONN_TIMEOUT
:
2639 value
= tsk
->conn_timeout
;
2640 /* no need to set "res", since already 0 at this point */
2642 case TIPC_NODE_RECVQ_DEPTH
:
2643 value
= 0; /* was tipc_queue_size, now obsolete */
2645 case TIPC_SOCK_RECVQ_DEPTH
:
2646 value
= skb_queue_len(&sk
->sk_receive_queue
);
2655 return res
; /* "get" failed */
2657 if (len
< sizeof(value
))
2660 if (copy_to_user(ov
, &value
, sizeof(value
)))
2663 return put_user(sizeof(value
), ol
);
2666 static int tipc_ioctl(struct socket
*sk
, unsigned int cmd
, unsigned long arg
)
2668 struct tipc_sioc_ln_req lnr
;
2669 void __user
*argp
= (void __user
*)arg
;
2672 case SIOCGETLINKNAME
:
2673 if (copy_from_user(&lnr
, argp
, sizeof(lnr
)))
2675 if (!tipc_node_get_linkname(lnr
.bearer_id
& 0xffff, lnr
.peer
,
2676 lnr
.linkname
, TIPC_MAX_LINK_NAME
)) {
2677 if (copy_to_user(argp
, &lnr
, sizeof(lnr
)))
2681 return -EADDRNOTAVAIL
;
2683 return -ENOIOCTLCMD
;
2687 /* Protocol switches for the various types of TIPC sockets */
2689 static const struct proto_ops msg_ops
= {
2690 .owner
= THIS_MODULE
,
2692 .release
= tipc_release
,
2694 .connect
= tipc_connect
,
2695 .socketpair
= sock_no_socketpair
,
2696 .accept
= sock_no_accept
,
2697 .getname
= tipc_getname
,
2699 .ioctl
= tipc_ioctl
,
2700 .listen
= sock_no_listen
,
2701 .shutdown
= tipc_shutdown
,
2702 .setsockopt
= tipc_setsockopt
,
2703 .getsockopt
= tipc_getsockopt
,
2704 .sendmsg
= tipc_sendmsg
,
2705 .recvmsg
= tipc_recvmsg
,
2706 .mmap
= sock_no_mmap
,
2707 .sendpage
= sock_no_sendpage
2710 static const struct proto_ops packet_ops
= {
2711 .owner
= THIS_MODULE
,
2713 .release
= tipc_release
,
2715 .connect
= tipc_connect
,
2716 .socketpair
= sock_no_socketpair
,
2717 .accept
= tipc_accept
,
2718 .getname
= tipc_getname
,
2720 .ioctl
= tipc_ioctl
,
2721 .listen
= tipc_listen
,
2722 .shutdown
= tipc_shutdown
,
2723 .setsockopt
= tipc_setsockopt
,
2724 .getsockopt
= tipc_getsockopt
,
2725 .sendmsg
= tipc_send_packet
,
2726 .recvmsg
= tipc_recvmsg
,
2727 .mmap
= sock_no_mmap
,
2728 .sendpage
= sock_no_sendpage
2731 static const struct proto_ops stream_ops
= {
2732 .owner
= THIS_MODULE
,
2734 .release
= tipc_release
,
2736 .connect
= tipc_connect
,
2737 .socketpair
= sock_no_socketpair
,
2738 .accept
= tipc_accept
,
2739 .getname
= tipc_getname
,
2741 .ioctl
= tipc_ioctl
,
2742 .listen
= tipc_listen
,
2743 .shutdown
= tipc_shutdown
,
2744 .setsockopt
= tipc_setsockopt
,
2745 .getsockopt
= tipc_getsockopt
,
2746 .sendmsg
= tipc_send_stream
,
2747 .recvmsg
= tipc_recv_stream
,
2748 .mmap
= sock_no_mmap
,
2749 .sendpage
= sock_no_sendpage
2752 static const struct net_proto_family tipc_family_ops
= {
2753 .owner
= THIS_MODULE
,
2755 .create
= tipc_sk_create
2758 static struct proto tipc_proto
= {
2760 .owner
= THIS_MODULE
,
2761 .obj_size
= sizeof(struct tipc_sock
),
2762 .sysctl_rmem
= sysctl_tipc_rmem
2765 static struct proto tipc_proto_kern
= {
2767 .obj_size
= sizeof(struct tipc_sock
),
2768 .sysctl_rmem
= sysctl_tipc_rmem
2772 * tipc_socket_init - initialize TIPC socket interface
2774 * Returns 0 on success, errno otherwise
2776 int tipc_socket_init(void)
2780 res
= proto_register(&tipc_proto
, 1);
2782 pr_err("Failed to register TIPC protocol type\n");
2786 res
= sock_register(&tipc_family_ops
);
2788 pr_err("Failed to register TIPC socket type\n");
2789 proto_unregister(&tipc_proto
);
2797 * tipc_socket_stop - stop TIPC socket interface
2799 void tipc_socket_stop(void)
2801 sock_unregister(tipc_family_ops
.family
);
2802 proto_unregister(&tipc_proto
);