Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / net / tipc / socket.c
blob7dfa9fc99ec3d442f89f5fc1a7471f6c00e2b6fd
1 /*
2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2001-2007, 2012-2017, Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
37 #include <linux/rhashtable.h>
38 #include <linux/sched/signal.h>
40 #include "core.h"
41 #include "name_table.h"
42 #include "node.h"
43 #include "link.h"
44 #include "name_distr.h"
45 #include "socket.h"
46 #include "bcast.h"
47 #include "netlink.h"
48 #include "group.h"
50 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
51 #define CONN_PROBING_INTV msecs_to_jiffies(3600000) /* [ms] => 1 h */
52 #define TIPC_FWD_MSG 1
53 #define TIPC_MAX_PORT 0xffffffff
54 #define TIPC_MIN_PORT 1
55 #define TIPC_ACK_RATE 4 /* ACK at 1/4 of of rcv window size */
57 enum {
58 TIPC_LISTEN = TCP_LISTEN,
59 TIPC_ESTABLISHED = TCP_ESTABLISHED,
60 TIPC_OPEN = TCP_CLOSE,
61 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
62 TIPC_CONNECTING = TCP_SYN_SENT,
65 struct sockaddr_pair {
66 struct sockaddr_tipc sock;
67 struct sockaddr_tipc member;
70 /**
71 * struct tipc_sock - TIPC socket structure
72 * @sk: socket - interacts with 'port' and with user via the socket API
73 * @conn_type: TIPC type used when connection was established
74 * @conn_instance: TIPC instance used when connection was established
75 * @published: non-zero if port has one or more associated names
76 * @max_pkt: maximum packet size "hint" used when building messages sent by port
77 * @portid: unique port identity in TIPC socket hash table
78 * @phdr: preformatted message header used when sending messages
79 * #cong_links: list of congested links
80 * @publications: list of publications for port
81 * @blocking_link: address of the congested link we are currently sleeping on
82 * @pub_count: total # of publications port has made during its lifetime
83 * @probing_state:
84 * @conn_timeout: the time we can wait for an unresponded setup request
85 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
86 * @cong_link_cnt: number of congested links
87 * @snt_unacked: # messages sent by socket, and not yet acked by peer
88 * @rcv_unacked: # messages read by user, but not yet acked back to peer
89 * @peer: 'connected' peer for dgram/rdm
90 * @node: hash table node
91 * @mc_method: cookie for use between socket and broadcast layer
92 * @rcu: rcu struct for tipc_sock
94 struct tipc_sock {
95 struct sock sk;
96 u32 conn_type;
97 u32 conn_instance;
98 int published;
99 u32 max_pkt;
100 u32 portid;
101 struct tipc_msg phdr;
102 struct list_head cong_links;
103 struct list_head publications;
104 u32 pub_count;
105 uint conn_timeout;
106 atomic_t dupl_rcvcnt;
107 bool probe_unacked;
108 u16 cong_link_cnt;
109 u16 snt_unacked;
110 u16 snd_win;
111 u16 peer_caps;
112 u16 rcv_unacked;
113 u16 rcv_win;
114 struct sockaddr_tipc peer;
115 struct rhash_head node;
116 struct tipc_mc_method mc_method;
117 struct rcu_head rcu;
118 struct tipc_group *group;
119 bool group_is_open;
122 static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
123 static void tipc_data_ready(struct sock *sk);
124 static void tipc_write_space(struct sock *sk);
125 static void tipc_sock_destruct(struct sock *sk);
126 static int tipc_release(struct socket *sock);
127 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
128 bool kern);
129 static void tipc_sk_timeout(struct timer_list *t);
130 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
131 struct tipc_name_seq const *seq);
132 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
133 struct tipc_name_seq const *seq);
134 static int tipc_sk_leave(struct tipc_sock *tsk);
135 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
136 static int tipc_sk_insert(struct tipc_sock *tsk);
137 static void tipc_sk_remove(struct tipc_sock *tsk);
138 static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
139 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
141 static const struct proto_ops packet_ops;
142 static const struct proto_ops stream_ops;
143 static const struct proto_ops msg_ops;
144 static struct proto tipc_proto;
145 static const struct rhashtable_params tsk_rht_params;
147 static u32 tsk_own_node(struct tipc_sock *tsk)
149 return msg_prevnode(&tsk->phdr);
152 static u32 tsk_peer_node(struct tipc_sock *tsk)
154 return msg_destnode(&tsk->phdr);
157 static u32 tsk_peer_port(struct tipc_sock *tsk)
159 return msg_destport(&tsk->phdr);
162 static bool tsk_unreliable(struct tipc_sock *tsk)
164 return msg_src_droppable(&tsk->phdr) != 0;
167 static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
169 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
172 static bool tsk_unreturnable(struct tipc_sock *tsk)
174 return msg_dest_droppable(&tsk->phdr) != 0;
177 static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
179 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
182 static int tsk_importance(struct tipc_sock *tsk)
184 return msg_importance(&tsk->phdr);
187 static int tsk_set_importance(struct tipc_sock *tsk, int imp)
189 if (imp > TIPC_CRITICAL_IMPORTANCE)
190 return -EINVAL;
191 msg_set_importance(&tsk->phdr, (u32)imp);
192 return 0;
195 static struct tipc_sock *tipc_sk(const struct sock *sk)
197 return container_of(sk, struct tipc_sock, sk);
200 static bool tsk_conn_cong(struct tipc_sock *tsk)
202 return tsk->snt_unacked > tsk->snd_win;
205 static u16 tsk_blocks(int len)
207 return ((len / FLOWCTL_BLK_SZ) + 1);
210 /* tsk_blocks(): translate a buffer size in bytes to number of
211 * advertisable blocks, taking into account the ratio truesize(len)/len
212 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
214 static u16 tsk_adv_blocks(int len)
216 return len / FLOWCTL_BLK_SZ / 4;
219 /* tsk_inc(): increment counter for sent or received data
220 * - If block based flow control is not supported by peer we
221 * fall back to message based ditto, incrementing the counter
223 static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
225 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
226 return ((msglen / FLOWCTL_BLK_SZ) + 1);
227 return 1;
231 * tsk_advance_rx_queue - discard first buffer in socket receive queue
233 * Caller must hold socket lock
235 static void tsk_advance_rx_queue(struct sock *sk)
237 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
240 /* tipc_sk_respond() : send response message back to sender
242 static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
244 u32 selector;
245 u32 dnode;
246 u32 onode = tipc_own_addr(sock_net(sk));
248 if (!tipc_msg_reverse(onode, &skb, err))
249 return;
251 dnode = msg_destnode(buf_msg(skb));
252 selector = msg_origport(buf_msg(skb));
253 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
257 * tsk_rej_rx_queue - reject all buffers in socket receive queue
259 * Caller must hold socket lock
261 static void tsk_rej_rx_queue(struct sock *sk)
263 struct sk_buff *skb;
265 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
266 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
269 static bool tipc_sk_connected(struct sock *sk)
271 return sk->sk_state == TIPC_ESTABLISHED;
274 /* tipc_sk_type_connectionless - check if the socket is datagram socket
275 * @sk: socket
277 * Returns true if connection less, false otherwise
279 static bool tipc_sk_type_connectionless(struct sock *sk)
281 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
284 /* tsk_peer_msg - verify if message was sent by connected port's peer
286 * Handles cases where the node's network address has changed from
287 * the default of <0.0.0> to its configured setting.
289 static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
291 struct sock *sk = &tsk->sk;
292 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
293 u32 peer_port = tsk_peer_port(tsk);
294 u32 orig_node;
295 u32 peer_node;
297 if (unlikely(!tipc_sk_connected(sk)))
298 return false;
300 if (unlikely(msg_origport(msg) != peer_port))
301 return false;
303 orig_node = msg_orignode(msg);
304 peer_node = tsk_peer_node(tsk);
306 if (likely(orig_node == peer_node))
307 return true;
309 if (!orig_node && (peer_node == tn->own_addr))
310 return true;
312 if (!peer_node && (orig_node == tn->own_addr))
313 return true;
315 return false;
318 /* tipc_set_sk_state - set the sk_state of the socket
319 * @sk: socket
321 * Caller must hold socket lock
323 * Returns 0 on success, errno otherwise
325 static int tipc_set_sk_state(struct sock *sk, int state)
327 int oldsk_state = sk->sk_state;
328 int res = -EINVAL;
330 switch (state) {
331 case TIPC_OPEN:
332 res = 0;
333 break;
334 case TIPC_LISTEN:
335 case TIPC_CONNECTING:
336 if (oldsk_state == TIPC_OPEN)
337 res = 0;
338 break;
339 case TIPC_ESTABLISHED:
340 if (oldsk_state == TIPC_CONNECTING ||
341 oldsk_state == TIPC_OPEN)
342 res = 0;
343 break;
344 case TIPC_DISCONNECTING:
345 if (oldsk_state == TIPC_CONNECTING ||
346 oldsk_state == TIPC_ESTABLISHED)
347 res = 0;
348 break;
351 if (!res)
352 sk->sk_state = state;
354 return res;
357 static int tipc_sk_sock_err(struct socket *sock, long *timeout)
359 struct sock *sk = sock->sk;
360 int err = sock_error(sk);
361 int typ = sock->type;
363 if (err)
364 return err;
365 if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
366 if (sk->sk_state == TIPC_DISCONNECTING)
367 return -EPIPE;
368 else if (!tipc_sk_connected(sk))
369 return -ENOTCONN;
371 if (!*timeout)
372 return -EAGAIN;
373 if (signal_pending(current))
374 return sock_intr_errno(*timeout);
376 return 0;
379 #define tipc_wait_for_cond(sock_, timeo_, condition_) \
380 ({ \
381 struct sock *sk_; \
382 int rc_; \
384 while ((rc_ = !(condition_))) { \
385 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
386 sk_ = (sock_)->sk; \
387 rc_ = tipc_sk_sock_err((sock_), timeo_); \
388 if (rc_) \
389 break; \
390 prepare_to_wait(sk_sleep(sk_), &wait_, TASK_INTERRUPTIBLE); \
391 release_sock(sk_); \
392 *(timeo_) = wait_woken(&wait_, TASK_INTERRUPTIBLE, *(timeo_)); \
393 sched_annotate_sleep(); \
394 lock_sock(sk_); \
395 remove_wait_queue(sk_sleep(sk_), &wait_); \
397 rc_; \
401 * tipc_sk_create - create a TIPC socket
402 * @net: network namespace (must be default network)
403 * @sock: pre-allocated socket structure
404 * @protocol: protocol indicator (must be 0)
405 * @kern: caused by kernel or by userspace?
407 * This routine creates additional data structures used by the TIPC socket,
408 * initializes them, and links them together.
410 * Returns 0 on success, errno otherwise
412 static int tipc_sk_create(struct net *net, struct socket *sock,
413 int protocol, int kern)
415 struct tipc_net *tn;
416 const struct proto_ops *ops;
417 struct sock *sk;
418 struct tipc_sock *tsk;
419 struct tipc_msg *msg;
421 /* Validate arguments */
422 if (unlikely(protocol != 0))
423 return -EPROTONOSUPPORT;
425 switch (sock->type) {
426 case SOCK_STREAM:
427 ops = &stream_ops;
428 break;
429 case SOCK_SEQPACKET:
430 ops = &packet_ops;
431 break;
432 case SOCK_DGRAM:
433 case SOCK_RDM:
434 ops = &msg_ops;
435 break;
436 default:
437 return -EPROTOTYPE;
440 /* Allocate socket's protocol area */
441 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
442 if (sk == NULL)
443 return -ENOMEM;
445 tsk = tipc_sk(sk);
446 tsk->max_pkt = MAX_PKT_DEFAULT;
447 INIT_LIST_HEAD(&tsk->publications);
448 INIT_LIST_HEAD(&tsk->cong_links);
449 msg = &tsk->phdr;
450 tn = net_generic(sock_net(sk), tipc_net_id);
452 /* Finish initializing socket data structures */
453 sock->ops = ops;
454 sock_init_data(sock, sk);
455 tipc_set_sk_state(sk, TIPC_OPEN);
456 if (tipc_sk_insert(tsk)) {
457 pr_warn("Socket create failed; port number exhausted\n");
458 return -EINVAL;
461 /* Ensure tsk is visible before we read own_addr. */
462 smp_mb();
464 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
465 NAMED_H_SIZE, 0);
467 msg_set_origport(msg, tsk->portid);
468 timer_setup(&sk->sk_timer, tipc_sk_timeout, 0);
469 sk->sk_shutdown = 0;
470 sk->sk_backlog_rcv = tipc_sk_backlog_rcv;
471 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
472 sk->sk_data_ready = tipc_data_ready;
473 sk->sk_write_space = tipc_write_space;
474 sk->sk_destruct = tipc_sock_destruct;
475 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
476 tsk->group_is_open = true;
477 atomic_set(&tsk->dupl_rcvcnt, 0);
479 /* Start out with safe limits until we receive an advertised window */
480 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
481 tsk->rcv_win = tsk->snd_win;
483 if (tipc_sk_type_connectionless(sk)) {
484 tsk_set_unreturnable(tsk, true);
485 if (sock->type == SOCK_DGRAM)
486 tsk_set_unreliable(tsk, true);
489 return 0;
492 static void tipc_sk_callback(struct rcu_head *head)
494 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
496 sock_put(&tsk->sk);
499 /* Caller should hold socket lock for the socket. */
500 static void __tipc_shutdown(struct socket *sock, int error)
502 struct sock *sk = sock->sk;
503 struct tipc_sock *tsk = tipc_sk(sk);
504 struct net *net = sock_net(sk);
505 long timeout = CONN_TIMEOUT_DEFAULT;
506 u32 dnode = tsk_peer_node(tsk);
507 struct sk_buff *skb;
509 /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
510 tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
511 !tsk_conn_cong(tsk)));
513 /* Reject all unreceived messages, except on an active connection
514 * (which disconnects locally & sends a 'FIN+' to peer).
516 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
517 if (TIPC_SKB_CB(skb)->bytes_read) {
518 kfree_skb(skb);
519 continue;
521 if (!tipc_sk_type_connectionless(sk) &&
522 sk->sk_state != TIPC_DISCONNECTING) {
523 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
524 tipc_node_remove_conn(net, dnode, tsk->portid);
526 tipc_sk_respond(sk, skb, error);
529 if (tipc_sk_type_connectionless(sk))
530 return;
532 if (sk->sk_state != TIPC_DISCONNECTING) {
533 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
534 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
535 tsk_own_node(tsk), tsk_peer_port(tsk),
536 tsk->portid, error);
537 if (skb)
538 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
539 tipc_node_remove_conn(net, dnode, tsk->portid);
540 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
545 * tipc_release - destroy a TIPC socket
546 * @sock: socket to destroy
548 * This routine cleans up any messages that are still queued on the socket.
549 * For DGRAM and RDM socket types, all queued messages are rejected.
550 * For SEQPACKET and STREAM socket types, the first message is rejected
551 * and any others are discarded. (If the first message on a STREAM socket
552 * is partially-read, it is discarded and the next one is rejected instead.)
554 * NOTE: Rejected messages are not necessarily returned to the sender! They
555 * are returned or discarded according to the "destination droppable" setting
556 * specified for the message by the sender.
558 * Returns 0 on success, errno otherwise
560 static int tipc_release(struct socket *sock)
562 struct sock *sk = sock->sk;
563 struct tipc_sock *tsk;
566 * Exit if socket isn't fully initialized (occurs when a failed accept()
567 * releases a pre-allocated child socket that was never used)
569 if (sk == NULL)
570 return 0;
572 tsk = tipc_sk(sk);
573 lock_sock(sk);
575 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
576 sk->sk_shutdown = SHUTDOWN_MASK;
577 tipc_sk_leave(tsk);
578 tipc_sk_withdraw(tsk, 0, NULL);
579 sk_stop_timer(sk, &sk->sk_timer);
580 tipc_sk_remove(tsk);
582 /* Reject any messages that accumulated in backlog queue */
583 release_sock(sk);
584 tipc_dest_list_purge(&tsk->cong_links);
585 tsk->cong_link_cnt = 0;
586 call_rcu(&tsk->rcu, tipc_sk_callback);
587 sock->sk = NULL;
589 return 0;
593 * tipc_bind - associate or disassocate TIPC name(s) with a socket
594 * @sock: socket structure
595 * @uaddr: socket address describing name(s) and desired operation
596 * @uaddr_len: size of socket address data structure
598 * Name and name sequence binding is indicated using a positive scope value;
599 * a negative scope value unbinds the specified name. Specifying no name
600 * (i.e. a socket address length of 0) unbinds all names from the socket.
602 * Returns 0 on success, errno otherwise
604 * NOTE: This routine doesn't need to take the socket lock since it doesn't
605 * access any non-constant socket information.
607 static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
608 int uaddr_len)
610 struct sock *sk = sock->sk;
611 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
612 struct tipc_sock *tsk = tipc_sk(sk);
613 int res = -EINVAL;
615 lock_sock(sk);
616 if (unlikely(!uaddr_len)) {
617 res = tipc_sk_withdraw(tsk, 0, NULL);
618 goto exit;
620 if (tsk->group) {
621 res = -EACCES;
622 goto exit;
624 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
625 res = -EINVAL;
626 goto exit;
628 if (addr->family != AF_TIPC) {
629 res = -EAFNOSUPPORT;
630 goto exit;
633 if (addr->addrtype == TIPC_ADDR_NAME)
634 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
635 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
636 res = -EAFNOSUPPORT;
637 goto exit;
640 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
641 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
642 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
643 res = -EACCES;
644 goto exit;
647 res = (addr->scope > 0) ?
648 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
649 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
650 exit:
651 release_sock(sk);
652 return res;
656 * tipc_getname - get port ID of socket or peer socket
657 * @sock: socket structure
658 * @uaddr: area for returned socket address
659 * @uaddr_len: area for returned length of socket address
660 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
662 * Returns 0 on success, errno otherwise
664 * NOTE: This routine doesn't need to take the socket lock since it only
665 * accesses socket information that is unchanging (or which changes in
666 * a completely predictable manner).
668 static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
669 int *uaddr_len, int peer)
671 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
672 struct sock *sk = sock->sk;
673 struct tipc_sock *tsk = tipc_sk(sk);
674 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
676 memset(addr, 0, sizeof(*addr));
677 if (peer) {
678 if ((!tipc_sk_connected(sk)) &&
679 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
680 return -ENOTCONN;
681 addr->addr.id.ref = tsk_peer_port(tsk);
682 addr->addr.id.node = tsk_peer_node(tsk);
683 } else {
684 addr->addr.id.ref = tsk->portid;
685 addr->addr.id.node = tn->own_addr;
688 *uaddr_len = sizeof(*addr);
689 addr->addrtype = TIPC_ADDR_ID;
690 addr->family = AF_TIPC;
691 addr->scope = 0;
692 addr->addr.name.domain = 0;
694 return 0;
698 * tipc_poll - read and possibly block on pollmask
699 * @file: file structure associated with the socket
700 * @sock: socket for which to calculate the poll bits
701 * @wait: ???
703 * Returns pollmask value
705 * COMMENTARY:
706 * It appears that the usual socket locking mechanisms are not useful here
707 * since the pollmask info is potentially out-of-date the moment this routine
708 * exits. TCP and other protocols seem to rely on higher level poll routines
709 * to handle any preventable race conditions, so TIPC will do the same ...
711 * IMPORTANT: The fact that a read or write operation is indicated does NOT
712 * imply that the operation will succeed, merely that it should be performed
713 * and will not block.
715 static __poll_t tipc_poll(struct file *file, struct socket *sock,
716 poll_table *wait)
718 struct sock *sk = sock->sk;
719 struct tipc_sock *tsk = tipc_sk(sk);
720 __poll_t revents = 0;
722 sock_poll_wait(file, sk_sleep(sk), wait);
724 if (sk->sk_shutdown & RCV_SHUTDOWN)
725 revents |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
726 if (sk->sk_shutdown == SHUTDOWN_MASK)
727 revents |= EPOLLHUP;
729 switch (sk->sk_state) {
730 case TIPC_ESTABLISHED:
731 case TIPC_CONNECTING:
732 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
733 revents |= EPOLLOUT;
734 /* fall thru' */
735 case TIPC_LISTEN:
736 if (!skb_queue_empty(&sk->sk_receive_queue))
737 revents |= EPOLLIN | EPOLLRDNORM;
738 break;
739 case TIPC_OPEN:
740 if (tsk->group_is_open && !tsk->cong_link_cnt)
741 revents |= EPOLLOUT;
742 if (!tipc_sk_type_connectionless(sk))
743 break;
744 if (skb_queue_empty(&sk->sk_receive_queue))
745 break;
746 revents |= EPOLLIN | EPOLLRDNORM;
747 break;
748 case TIPC_DISCONNECTING:
749 revents = EPOLLIN | EPOLLRDNORM | EPOLLHUP;
750 break;
752 return revents;
756 * tipc_sendmcast - send multicast message
757 * @sock: socket structure
758 * @seq: destination address
759 * @msg: message to send
760 * @dlen: length of data to send
761 * @timeout: timeout to wait for wakeup
763 * Called from function tipc_sendmsg(), which has done all sanity checks
764 * Returns the number of bytes sent on success, or errno
766 static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
767 struct msghdr *msg, size_t dlen, long timeout)
769 struct sock *sk = sock->sk;
770 struct tipc_sock *tsk = tipc_sk(sk);
771 struct tipc_msg *hdr = &tsk->phdr;
772 struct net *net = sock_net(sk);
773 int mtu = tipc_bcast_get_mtu(net);
774 struct tipc_mc_method *method = &tsk->mc_method;
775 struct sk_buff_head pkts;
776 struct tipc_nlist dsts;
777 int rc;
779 if (tsk->group)
780 return -EACCES;
782 /* Block or return if any destination link is congested */
783 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
784 if (unlikely(rc))
785 return rc;
787 /* Lookup destination nodes */
788 tipc_nlist_init(&dsts, tipc_own_addr(net));
789 tipc_nametbl_lookup_dst_nodes(net, seq->type, seq->lower,
790 seq->upper, &dsts);
791 if (!dsts.local && !dsts.remote)
792 return -EHOSTUNREACH;
794 /* Build message header */
795 msg_set_type(hdr, TIPC_MCAST_MSG);
796 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
797 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
798 msg_set_destport(hdr, 0);
799 msg_set_destnode(hdr, 0);
800 msg_set_nametype(hdr, seq->type);
801 msg_set_namelower(hdr, seq->lower);
802 msg_set_nameupper(hdr, seq->upper);
804 /* Build message as chain of buffers */
805 skb_queue_head_init(&pkts);
806 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
808 /* Send message if build was successful */
809 if (unlikely(rc == dlen))
810 rc = tipc_mcast_xmit(net, &pkts, method, &dsts,
811 &tsk->cong_link_cnt);
813 tipc_nlist_purge(&dsts);
815 return rc ? rc : dlen;
819 * tipc_send_group_msg - send a message to a member in the group
820 * @net: network namespace
821 * @m: message to send
822 * @mb: group member
823 * @dnode: destination node
824 * @dport: destination port
825 * @dlen: total length of message data
827 static int tipc_send_group_msg(struct net *net, struct tipc_sock *tsk,
828 struct msghdr *m, struct tipc_member *mb,
829 u32 dnode, u32 dport, int dlen)
831 u16 bc_snd_nxt = tipc_group_bc_snd_nxt(tsk->group);
832 struct tipc_mc_method *method = &tsk->mc_method;
833 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
834 struct tipc_msg *hdr = &tsk->phdr;
835 struct sk_buff_head pkts;
836 int mtu, rc;
838 /* Complete message header */
839 msg_set_type(hdr, TIPC_GRP_UCAST_MSG);
840 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
841 msg_set_destport(hdr, dport);
842 msg_set_destnode(hdr, dnode);
843 msg_set_grp_bc_seqno(hdr, bc_snd_nxt);
845 /* Build message as chain of buffers */
846 skb_queue_head_init(&pkts);
847 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
848 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
849 if (unlikely(rc != dlen))
850 return rc;
852 /* Send message */
853 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
854 if (unlikely(rc == -ELINKCONG)) {
855 tipc_dest_push(&tsk->cong_links, dnode, 0);
856 tsk->cong_link_cnt++;
859 /* Update send window */
860 tipc_group_update_member(mb, blks);
862 /* A broadcast sent within next EXPIRE period must follow same path */
863 method->rcast = true;
864 method->mandatory = true;
865 return dlen;
869 * tipc_send_group_unicast - send message to a member in the group
870 * @sock: socket structure
871 * @m: message to send
872 * @dlen: total length of message data
873 * @timeout: timeout to wait for wakeup
875 * Called from function tipc_sendmsg(), which has done all sanity checks
876 * Returns the number of bytes sent on success, or errno
878 static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m,
879 int dlen, long timeout)
881 struct sock *sk = sock->sk;
882 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
883 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
884 struct tipc_sock *tsk = tipc_sk(sk);
885 struct tipc_group *grp = tsk->group;
886 struct net *net = sock_net(sk);
887 struct tipc_member *mb = NULL;
888 u32 node, port;
889 int rc;
891 node = dest->addr.id.node;
892 port = dest->addr.id.ref;
893 if (!port && !node)
894 return -EHOSTUNREACH;
896 /* Block or return if destination link or member is congested */
897 rc = tipc_wait_for_cond(sock, &timeout,
898 !tipc_dest_find(&tsk->cong_links, node, 0) &&
899 !tipc_group_cong(grp, node, port, blks, &mb));
900 if (unlikely(rc))
901 return rc;
903 if (unlikely(!mb))
904 return -EHOSTUNREACH;
906 rc = tipc_send_group_msg(net, tsk, m, mb, node, port, dlen);
908 return rc ? rc : dlen;
912 * tipc_send_group_anycast - send message to any member with given identity
913 * @sock: socket structure
914 * @m: message to send
915 * @dlen: total length of message data
916 * @timeout: timeout to wait for wakeup
918 * Called from function tipc_sendmsg(), which has done all sanity checks
919 * Returns the number of bytes sent on success, or errno
921 static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m,
922 int dlen, long timeout)
924 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
925 struct sock *sk = sock->sk;
926 struct tipc_sock *tsk = tipc_sk(sk);
927 struct list_head *cong_links = &tsk->cong_links;
928 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
929 struct tipc_group *grp = tsk->group;
930 struct tipc_msg *hdr = &tsk->phdr;
931 struct tipc_member *first = NULL;
932 struct tipc_member *mbr = NULL;
933 struct net *net = sock_net(sk);
934 u32 node, port, exclude;
935 struct list_head dsts;
936 u32 type, inst, scope;
937 int lookups = 0;
938 int dstcnt, rc;
939 bool cong;
941 INIT_LIST_HEAD(&dsts);
943 type = msg_nametype(hdr);
944 inst = dest->addr.name.name.instance;
945 scope = msg_lookup_scope(hdr);
946 exclude = tipc_group_exclude(grp);
948 while (++lookups < 4) {
949 first = NULL;
951 /* Look for a non-congested destination member, if any */
952 while (1) {
953 if (!tipc_nametbl_lookup(net, type, inst, scope, &dsts,
954 &dstcnt, exclude, false))
955 return -EHOSTUNREACH;
956 tipc_dest_pop(&dsts, &node, &port);
957 cong = tipc_group_cong(grp, node, port, blks, &mbr);
958 if (!cong)
959 break;
960 if (mbr == first)
961 break;
962 if (!first)
963 first = mbr;
966 /* Start over if destination was not in member list */
967 if (unlikely(!mbr))
968 continue;
970 if (likely(!cong && !tipc_dest_find(cong_links, node, 0)))
971 break;
973 /* Block or return if destination link or member is congested */
974 rc = tipc_wait_for_cond(sock, &timeout,
975 !tipc_dest_find(cong_links, node, 0) &&
976 !tipc_group_cong(grp, node, port,
977 blks, &mbr));
978 if (unlikely(rc))
979 return rc;
981 /* Send, unless destination disappeared while waiting */
982 if (likely(mbr))
983 break;
986 if (unlikely(lookups >= 4))
987 return -EHOSTUNREACH;
989 rc = tipc_send_group_msg(net, tsk, m, mbr, node, port, dlen);
991 return rc ? rc : dlen;
995 * tipc_send_group_bcast - send message to all members in communication group
996 * @sk: socket structure
997 * @m: message to send
998 * @dlen: total length of message data
999 * @timeout: timeout to wait for wakeup
1001 * Called from function tipc_sendmsg(), which has done all sanity checks
1002 * Returns the number of bytes sent on success, or errno
1004 static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m,
1005 int dlen, long timeout)
1007 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1008 struct sock *sk = sock->sk;
1009 struct net *net = sock_net(sk);
1010 struct tipc_sock *tsk = tipc_sk(sk);
1011 struct tipc_group *grp = tsk->group;
1012 struct tipc_nlist *dsts = tipc_group_dests(grp);
1013 struct tipc_mc_method *method = &tsk->mc_method;
1014 bool ack = method->mandatory && method->rcast;
1015 int blks = tsk_blocks(MCAST_H_SIZE + dlen);
1016 struct tipc_msg *hdr = &tsk->phdr;
1017 int mtu = tipc_bcast_get_mtu(net);
1018 struct sk_buff_head pkts;
1019 int rc = -EHOSTUNREACH;
1021 if (!dsts->local && !dsts->remote)
1022 return -EHOSTUNREACH;
1024 /* Block or return if any destination link or member is congested */
1025 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt &&
1026 !tipc_group_bc_cong(grp, blks));
1027 if (unlikely(rc))
1028 return rc;
1030 /* Complete message header */
1031 if (dest) {
1032 msg_set_type(hdr, TIPC_GRP_MCAST_MSG);
1033 msg_set_nameinst(hdr, dest->addr.name.name.instance);
1034 } else {
1035 msg_set_type(hdr, TIPC_GRP_BCAST_MSG);
1036 msg_set_nameinst(hdr, 0);
1038 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
1039 msg_set_destport(hdr, 0);
1040 msg_set_destnode(hdr, 0);
1041 msg_set_grp_bc_seqno(hdr, tipc_group_bc_snd_nxt(grp));
1043 /* Avoid getting stuck with repeated forced replicasts */
1044 msg_set_grp_bc_ack_req(hdr, ack);
1046 /* Build message as chain of buffers */
1047 skb_queue_head_init(&pkts);
1048 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1049 if (unlikely(rc != dlen))
1050 return rc;
1052 /* Send message */
1053 rc = tipc_mcast_xmit(net, &pkts, method, dsts, &tsk->cong_link_cnt);
1054 if (unlikely(rc))
1055 return rc;
1057 /* Update broadcast sequence number and send windows */
1058 tipc_group_update_bc_members(tsk->group, blks, ack);
1060 /* Broadcast link is now free to choose method for next broadcast */
1061 method->mandatory = false;
1062 method->expires = jiffies;
1064 return dlen;
1068 * tipc_send_group_mcast - send message to all members with given identity
1069 * @sock: socket structure
1070 * @m: message to send
1071 * @dlen: total length of message data
1072 * @timeout: timeout to wait for wakeup
1074 * Called from function tipc_sendmsg(), which has done all sanity checks
1075 * Returns the number of bytes sent on success, or errno
1077 static int tipc_send_group_mcast(struct socket *sock, struct msghdr *m,
1078 int dlen, long timeout)
1080 struct sock *sk = sock->sk;
1081 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1082 struct tipc_sock *tsk = tipc_sk(sk);
1083 struct tipc_group *grp = tsk->group;
1084 struct tipc_msg *hdr = &tsk->phdr;
1085 struct net *net = sock_net(sk);
1086 u32 type, inst, scope, exclude;
1087 struct list_head dsts;
1088 u32 dstcnt;
1090 INIT_LIST_HEAD(&dsts);
1092 type = msg_nametype(hdr);
1093 inst = dest->addr.name.name.instance;
1094 scope = msg_lookup_scope(hdr);
1095 exclude = tipc_group_exclude(grp);
1097 if (!tipc_nametbl_lookup(net, type, inst, scope, &dsts,
1098 &dstcnt, exclude, true))
1099 return -EHOSTUNREACH;
1101 if (dstcnt == 1) {
1102 tipc_dest_pop(&dsts, &dest->addr.id.node, &dest->addr.id.ref);
1103 return tipc_send_group_unicast(sock, m, dlen, timeout);
1106 tipc_dest_list_purge(&dsts);
1107 return tipc_send_group_bcast(sock, m, dlen, timeout);
1111 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
1112 * @arrvq: queue with arriving messages, to be cloned after destination lookup
1113 * @inputq: queue with cloned messages, delivered to socket after dest lookup
1115 * Multi-threaded: parallel calls with reference to same queues may occur
1117 void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
1118 struct sk_buff_head *inputq)
1120 u32 self = tipc_own_addr(net);
1121 u32 type, lower, upper, scope;
1122 struct sk_buff *skb, *_skb;
1123 u32 portid, oport, onode;
1124 struct sk_buff_head tmpq;
1125 struct list_head dports;
1126 struct tipc_msg *hdr;
1127 int user, mtyp, hlen;
1128 bool exact;
1130 __skb_queue_head_init(&tmpq);
1131 INIT_LIST_HEAD(&dports);
1133 skb = tipc_skb_peek(arrvq, &inputq->lock);
1134 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
1135 hdr = buf_msg(skb);
1136 user = msg_user(hdr);
1137 mtyp = msg_type(hdr);
1138 hlen = skb_headroom(skb) + msg_hdr_sz(hdr);
1139 oport = msg_origport(hdr);
1140 onode = msg_orignode(hdr);
1141 type = msg_nametype(hdr);
1143 if (mtyp == TIPC_GRP_UCAST_MSG || user == GROUP_PROTOCOL) {
1144 spin_lock_bh(&inputq->lock);
1145 if (skb_peek(arrvq) == skb) {
1146 __skb_dequeue(arrvq);
1147 __skb_queue_tail(inputq, skb);
1149 kfree_skb(skb);
1150 spin_unlock_bh(&inputq->lock);
1151 continue;
1154 /* Group messages require exact scope match */
1155 if (msg_in_group(hdr)) {
1156 lower = 0;
1157 upper = ~0;
1158 scope = msg_lookup_scope(hdr);
1159 exact = true;
1160 } else {
1161 /* TIPC_NODE_SCOPE means "any scope" in this context */
1162 if (onode == self)
1163 scope = TIPC_NODE_SCOPE;
1164 else
1165 scope = TIPC_CLUSTER_SCOPE;
1166 exact = false;
1167 lower = msg_namelower(hdr);
1168 upper = msg_nameupper(hdr);
1171 /* Create destination port list: */
1172 tipc_nametbl_mc_lookup(net, type, lower, upper,
1173 scope, exact, &dports);
1175 /* Clone message per destination */
1176 while (tipc_dest_pop(&dports, NULL, &portid)) {
1177 _skb = __pskb_copy(skb, hlen, GFP_ATOMIC);
1178 if (_skb) {
1179 msg_set_destport(buf_msg(_skb), portid);
1180 __skb_queue_tail(&tmpq, _skb);
1181 continue;
1183 pr_warn("Failed to clone mcast rcv buffer\n");
1185 /* Append to inputq if not already done by other thread */
1186 spin_lock_bh(&inputq->lock);
1187 if (skb_peek(arrvq) == skb) {
1188 skb_queue_splice_tail_init(&tmpq, inputq);
1189 kfree_skb(__skb_dequeue(arrvq));
1191 spin_unlock_bh(&inputq->lock);
1192 __skb_queue_purge(&tmpq);
1193 kfree_skb(skb);
1195 tipc_sk_rcv(net, inputq);
1199 * tipc_sk_conn_proto_rcv - receive a connection mng protocol message
1200 * @tsk: receiving socket
1201 * @skb: pointer to message buffer.
1203 static void tipc_sk_conn_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
1204 struct sk_buff_head *xmitq)
1206 struct tipc_msg *hdr = buf_msg(skb);
1207 u32 onode = tsk_own_node(tsk);
1208 struct sock *sk = &tsk->sk;
1209 int mtyp = msg_type(hdr);
1210 bool conn_cong;
1212 /* Ignore if connection cannot be validated: */
1213 if (!tsk_peer_msg(tsk, hdr))
1214 goto exit;
1216 if (unlikely(msg_errcode(hdr))) {
1217 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1218 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
1219 tsk_peer_port(tsk));
1220 sk->sk_state_change(sk);
1221 goto exit;
1224 tsk->probe_unacked = false;
1226 if (mtyp == CONN_PROBE) {
1227 msg_set_type(hdr, CONN_PROBE_REPLY);
1228 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
1229 __skb_queue_tail(xmitq, skb);
1230 return;
1231 } else if (mtyp == CONN_ACK) {
1232 conn_cong = tsk_conn_cong(tsk);
1233 tsk->snt_unacked -= msg_conn_ack(hdr);
1234 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1235 tsk->snd_win = msg_adv_win(hdr);
1236 if (conn_cong)
1237 sk->sk_write_space(sk);
1238 } else if (mtyp != CONN_PROBE_REPLY) {
1239 pr_warn("Received unknown CONN_PROTO msg\n");
1241 exit:
1242 kfree_skb(skb);
1246 * tipc_sendmsg - send message in connectionless manner
1247 * @sock: socket structure
1248 * @m: message to send
1249 * @dsz: amount of user data to be sent
1251 * Message must have an destination specified explicitly.
1252 * Used for SOCK_RDM and SOCK_DGRAM messages,
1253 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
1254 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
1256 * Returns the number of bytes sent on success, or errno otherwise
1258 static int tipc_sendmsg(struct socket *sock,
1259 struct msghdr *m, size_t dsz)
1261 struct sock *sk = sock->sk;
1262 int ret;
1264 lock_sock(sk);
1265 ret = __tipc_sendmsg(sock, m, dsz);
1266 release_sock(sk);
1268 return ret;
1271 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
1273 struct sock *sk = sock->sk;
1274 struct net *net = sock_net(sk);
1275 struct tipc_sock *tsk = tipc_sk(sk);
1276 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1277 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1278 struct list_head *clinks = &tsk->cong_links;
1279 bool syn = !tipc_sk_type_connectionless(sk);
1280 struct tipc_group *grp = tsk->group;
1281 struct tipc_msg *hdr = &tsk->phdr;
1282 struct tipc_name_seq *seq;
1283 struct sk_buff_head pkts;
1284 u32 type, inst, domain;
1285 u32 dnode, dport;
1286 int mtu, rc;
1288 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
1289 return -EMSGSIZE;
1291 if (likely(dest)) {
1292 if (unlikely(m->msg_namelen < sizeof(*dest)))
1293 return -EINVAL;
1294 if (unlikely(dest->family != AF_TIPC))
1295 return -EINVAL;
1298 if (grp) {
1299 if (!dest)
1300 return tipc_send_group_bcast(sock, m, dlen, timeout);
1301 if (dest->addrtype == TIPC_ADDR_NAME)
1302 return tipc_send_group_anycast(sock, m, dlen, timeout);
1303 if (dest->addrtype == TIPC_ADDR_ID)
1304 return tipc_send_group_unicast(sock, m, dlen, timeout);
1305 if (dest->addrtype == TIPC_ADDR_MCAST)
1306 return tipc_send_group_mcast(sock, m, dlen, timeout);
1307 return -EINVAL;
1310 if (unlikely(!dest)) {
1311 dest = &tsk->peer;
1312 if (!syn || dest->family != AF_TIPC)
1313 return -EDESTADDRREQ;
1316 if (unlikely(syn)) {
1317 if (sk->sk_state == TIPC_LISTEN)
1318 return -EPIPE;
1319 if (sk->sk_state != TIPC_OPEN)
1320 return -EISCONN;
1321 if (tsk->published)
1322 return -EOPNOTSUPP;
1323 if (dest->addrtype == TIPC_ADDR_NAME) {
1324 tsk->conn_type = dest->addr.name.name.type;
1325 tsk->conn_instance = dest->addr.name.name.instance;
1329 seq = &dest->addr.nameseq;
1330 if (dest->addrtype == TIPC_ADDR_MCAST)
1331 return tipc_sendmcast(sock, seq, m, dlen, timeout);
1333 if (dest->addrtype == TIPC_ADDR_NAME) {
1334 type = dest->addr.name.name.type;
1335 inst = dest->addr.name.name.instance;
1336 domain = dest->addr.name.domain;
1337 dnode = domain;
1338 msg_set_type(hdr, TIPC_NAMED_MSG);
1339 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
1340 msg_set_nametype(hdr, type);
1341 msg_set_nameinst(hdr, inst);
1342 msg_set_lookup_scope(hdr, tipc_addr_scope(domain));
1343 dport = tipc_nametbl_translate(net, type, inst, &dnode);
1344 msg_set_destnode(hdr, dnode);
1345 msg_set_destport(hdr, dport);
1346 if (unlikely(!dport && !dnode))
1347 return -EHOSTUNREACH;
1348 } else if (dest->addrtype == TIPC_ADDR_ID) {
1349 dnode = dest->addr.id.node;
1350 msg_set_type(hdr, TIPC_DIRECT_MSG);
1351 msg_set_lookup_scope(hdr, 0);
1352 msg_set_destnode(hdr, dnode);
1353 msg_set_destport(hdr, dest->addr.id.ref);
1354 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
1357 /* Block or return if destination link is congested */
1358 rc = tipc_wait_for_cond(sock, &timeout,
1359 !tipc_dest_find(clinks, dnode, 0));
1360 if (unlikely(rc))
1361 return rc;
1363 skb_queue_head_init(&pkts);
1364 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
1365 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1366 if (unlikely(rc != dlen))
1367 return rc;
1369 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1370 if (unlikely(rc == -ELINKCONG)) {
1371 tipc_dest_push(clinks, dnode, 0);
1372 tsk->cong_link_cnt++;
1373 rc = 0;
1376 if (unlikely(syn && !rc))
1377 tipc_set_sk_state(sk, TIPC_CONNECTING);
1379 return rc ? rc : dlen;
1383 * tipc_sendstream - send stream-oriented data
1384 * @sock: socket structure
1385 * @m: data to send
1386 * @dsz: total length of data to be transmitted
1388 * Used for SOCK_STREAM data.
1390 * Returns the number of bytes sent on success (or partial success),
1391 * or errno if no data sent
1393 static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
1395 struct sock *sk = sock->sk;
1396 int ret;
1398 lock_sock(sk);
1399 ret = __tipc_sendstream(sock, m, dsz);
1400 release_sock(sk);
1402 return ret;
1405 static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
1407 struct sock *sk = sock->sk;
1408 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1409 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1410 struct tipc_sock *tsk = tipc_sk(sk);
1411 struct tipc_msg *hdr = &tsk->phdr;
1412 struct net *net = sock_net(sk);
1413 struct sk_buff_head pkts;
1414 u32 dnode = tsk_peer_node(tsk);
1415 int send, sent = 0;
1416 int rc = 0;
1418 skb_queue_head_init(&pkts);
1420 if (unlikely(dlen > INT_MAX))
1421 return -EMSGSIZE;
1423 /* Handle implicit connection setup */
1424 if (unlikely(dest)) {
1425 rc = __tipc_sendmsg(sock, m, dlen);
1426 if (dlen && (dlen == rc))
1427 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
1428 return rc;
1431 do {
1432 rc = tipc_wait_for_cond(sock, &timeout,
1433 (!tsk->cong_link_cnt &&
1434 !tsk_conn_cong(tsk) &&
1435 tipc_sk_connected(sk)));
1436 if (unlikely(rc))
1437 break;
1439 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
1440 rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
1441 if (unlikely(rc != send))
1442 break;
1444 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1445 if (unlikely(rc == -ELINKCONG)) {
1446 tsk->cong_link_cnt = 1;
1447 rc = 0;
1449 if (likely(!rc)) {
1450 tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
1451 sent += send;
1453 } while (sent < dlen && !rc);
1455 return sent ? sent : rc;
1459 * tipc_send_packet - send a connection-oriented message
1460 * @sock: socket structure
1461 * @m: message to send
1462 * @dsz: length of data to be transmitted
1464 * Used for SOCK_SEQPACKET messages.
1466 * Returns the number of bytes sent on success, or errno otherwise
1468 static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
1470 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1471 return -EMSGSIZE;
1473 return tipc_sendstream(sock, m, dsz);
1476 /* tipc_sk_finish_conn - complete the setup of a connection
1478 static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
1479 u32 peer_node)
1481 struct sock *sk = &tsk->sk;
1482 struct net *net = sock_net(sk);
1483 struct tipc_msg *msg = &tsk->phdr;
1485 msg_set_destnode(msg, peer_node);
1486 msg_set_destport(msg, peer_port);
1487 msg_set_type(msg, TIPC_CONN_MSG);
1488 msg_set_lookup_scope(msg, 0);
1489 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1491 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
1492 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
1493 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1494 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
1495 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
1496 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1497 return;
1499 /* Fall back to message based flow control */
1500 tsk->rcv_win = FLOWCTL_MSG_WIN;
1501 tsk->snd_win = FLOWCTL_MSG_WIN;
1505 * tipc_sk_set_orig_addr - capture sender's address for received message
1506 * @m: descriptor for message info
1507 * @hdr: received message header
1509 * Note: Address is not captured if not requested by receiver.
1511 static void tipc_sk_set_orig_addr(struct msghdr *m, struct sk_buff *skb)
1513 DECLARE_SOCKADDR(struct sockaddr_pair *, srcaddr, m->msg_name);
1514 struct tipc_msg *hdr = buf_msg(skb);
1516 if (!srcaddr)
1517 return;
1519 srcaddr->sock.family = AF_TIPC;
1520 srcaddr->sock.addrtype = TIPC_ADDR_ID;
1521 srcaddr->sock.addr.id.ref = msg_origport(hdr);
1522 srcaddr->sock.addr.id.node = msg_orignode(hdr);
1523 srcaddr->sock.addr.name.domain = 0;
1524 srcaddr->sock.scope = 0;
1525 m->msg_namelen = sizeof(struct sockaddr_tipc);
1527 if (!msg_in_group(hdr))
1528 return;
1530 /* Group message users may also want to know sending member's id */
1531 srcaddr->member.family = AF_TIPC;
1532 srcaddr->member.addrtype = TIPC_ADDR_NAME;
1533 srcaddr->member.addr.name.name.type = msg_nametype(hdr);
1534 srcaddr->member.addr.name.name.instance = TIPC_SKB_CB(skb)->orig_member;
1535 srcaddr->member.addr.name.domain = 0;
1536 m->msg_namelen = sizeof(*srcaddr);
1540 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
1541 * @m: descriptor for message info
1542 * @msg: received message header
1543 * @tsk: TIPC port associated with message
1545 * Note: Ancillary data is not captured if not requested by receiver.
1547 * Returns 0 if successful, otherwise errno
1549 static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1550 struct tipc_sock *tsk)
1552 u32 anc_data[3];
1553 u32 err;
1554 u32 dest_type;
1555 int has_name;
1556 int res;
1558 if (likely(m->msg_controllen == 0))
1559 return 0;
1561 /* Optionally capture errored message object(s) */
1562 err = msg ? msg_errcode(msg) : 0;
1563 if (unlikely(err)) {
1564 anc_data[0] = err;
1565 anc_data[1] = msg_data_sz(msg);
1566 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1567 if (res)
1568 return res;
1569 if (anc_data[1]) {
1570 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1571 msg_data(msg));
1572 if (res)
1573 return res;
1577 /* Optionally capture message destination object */
1578 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1579 switch (dest_type) {
1580 case TIPC_NAMED_MSG:
1581 has_name = 1;
1582 anc_data[0] = msg_nametype(msg);
1583 anc_data[1] = msg_namelower(msg);
1584 anc_data[2] = msg_namelower(msg);
1585 break;
1586 case TIPC_MCAST_MSG:
1587 has_name = 1;
1588 anc_data[0] = msg_nametype(msg);
1589 anc_data[1] = msg_namelower(msg);
1590 anc_data[2] = msg_nameupper(msg);
1591 break;
1592 case TIPC_CONN_MSG:
1593 has_name = (tsk->conn_type != 0);
1594 anc_data[0] = tsk->conn_type;
1595 anc_data[1] = tsk->conn_instance;
1596 anc_data[2] = tsk->conn_instance;
1597 break;
1598 default:
1599 has_name = 0;
1601 if (has_name) {
1602 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1603 if (res)
1604 return res;
1607 return 0;
1610 static void tipc_sk_send_ack(struct tipc_sock *tsk)
1612 struct sock *sk = &tsk->sk;
1613 struct net *net = sock_net(sk);
1614 struct sk_buff *skb = NULL;
1615 struct tipc_msg *msg;
1616 u32 peer_port = tsk_peer_port(tsk);
1617 u32 dnode = tsk_peer_node(tsk);
1619 if (!tipc_sk_connected(sk))
1620 return;
1621 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1622 dnode, tsk_own_node(tsk), peer_port,
1623 tsk->portid, TIPC_OK);
1624 if (!skb)
1625 return;
1626 msg = buf_msg(skb);
1627 msg_set_conn_ack(msg, tsk->rcv_unacked);
1628 tsk->rcv_unacked = 0;
1630 /* Adjust to and advertize the correct window limit */
1631 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1632 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1633 msg_set_adv_win(msg, tsk->rcv_win);
1635 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
1638 static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1640 struct sock *sk = sock->sk;
1641 DEFINE_WAIT(wait);
1642 long timeo = *timeop;
1643 int err = sock_error(sk);
1645 if (err)
1646 return err;
1648 for (;;) {
1649 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
1650 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
1651 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1652 err = -ENOTCONN;
1653 break;
1655 release_sock(sk);
1656 timeo = schedule_timeout(timeo);
1657 lock_sock(sk);
1659 err = 0;
1660 if (!skb_queue_empty(&sk->sk_receive_queue))
1661 break;
1662 err = -EAGAIN;
1663 if (!timeo)
1664 break;
1665 err = sock_intr_errno(timeo);
1666 if (signal_pending(current))
1667 break;
1669 err = sock_error(sk);
1670 if (err)
1671 break;
1673 finish_wait(sk_sleep(sk), &wait);
1674 *timeop = timeo;
1675 return err;
1679 * tipc_recvmsg - receive packet-oriented message
1680 * @m: descriptor for message info
1681 * @buflen: length of user buffer area
1682 * @flags: receive flags
1684 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1685 * If the complete message doesn't fit in user area, truncate it.
1687 * Returns size of returned message data, errno otherwise
1689 static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
1690 size_t buflen, int flags)
1692 struct sock *sk = sock->sk;
1693 bool connected = !tipc_sk_type_connectionless(sk);
1694 struct tipc_sock *tsk = tipc_sk(sk);
1695 int rc, err, hlen, dlen, copy;
1696 struct sk_buff_head xmitq;
1697 struct tipc_msg *hdr;
1698 struct sk_buff *skb;
1699 bool grp_evt;
1700 long timeout;
1702 /* Catch invalid receive requests */
1703 if (unlikely(!buflen))
1704 return -EINVAL;
1706 lock_sock(sk);
1707 if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
1708 rc = -ENOTCONN;
1709 goto exit;
1711 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1713 /* Step rcv queue to first msg with data or error; wait if necessary */
1714 do {
1715 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1716 if (unlikely(rc))
1717 goto exit;
1718 skb = skb_peek(&sk->sk_receive_queue);
1719 hdr = buf_msg(skb);
1720 dlen = msg_data_sz(hdr);
1721 hlen = msg_hdr_sz(hdr);
1722 err = msg_errcode(hdr);
1723 grp_evt = msg_is_grp_evt(hdr);
1724 if (likely(dlen || err))
1725 break;
1726 tsk_advance_rx_queue(sk);
1727 } while (1);
1729 /* Collect msg meta data, including error code and rejected data */
1730 tipc_sk_set_orig_addr(m, skb);
1731 rc = tipc_sk_anc_data_recv(m, hdr, tsk);
1732 if (unlikely(rc))
1733 goto exit;
1735 /* Capture data if non-error msg, otherwise just set return value */
1736 if (likely(!err)) {
1737 copy = min_t(int, dlen, buflen);
1738 if (unlikely(copy != dlen))
1739 m->msg_flags |= MSG_TRUNC;
1740 rc = skb_copy_datagram_msg(skb, hlen, m, copy);
1741 } else {
1742 copy = 0;
1743 rc = 0;
1744 if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control)
1745 rc = -ECONNRESET;
1747 if (unlikely(rc))
1748 goto exit;
1750 /* Mark message as group event if applicable */
1751 if (unlikely(grp_evt)) {
1752 if (msg_grp_evt(hdr) == TIPC_WITHDRAWN)
1753 m->msg_flags |= MSG_EOR;
1754 m->msg_flags |= MSG_OOB;
1755 copy = 0;
1758 /* Caption of data or error code/rejected data was successful */
1759 if (unlikely(flags & MSG_PEEK))
1760 goto exit;
1762 /* Send group flow control advertisement when applicable */
1763 if (tsk->group && msg_in_group(hdr) && !grp_evt) {
1764 skb_queue_head_init(&xmitq);
1765 tipc_group_update_rcv_win(tsk->group, tsk_blocks(hlen + dlen),
1766 msg_orignode(hdr), msg_origport(hdr),
1767 &xmitq);
1768 tipc_node_distr_xmit(sock_net(sk), &xmitq);
1771 tsk_advance_rx_queue(sk);
1773 if (likely(!connected))
1774 goto exit;
1776 /* Send connection flow control advertisement when applicable */
1777 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1778 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
1779 tipc_sk_send_ack(tsk);
1780 exit:
1781 release_sock(sk);
1782 return rc ? rc : copy;
1786 * tipc_recvstream - receive stream-oriented data
1787 * @m: descriptor for message info
1788 * @buflen: total size of user buffer area
1789 * @flags: receive flags
1791 * Used for SOCK_STREAM messages only. If not enough data is available
1792 * will optionally wait for more; never truncates data.
1794 * Returns size of returned message data, errno otherwise
1796 static int tipc_recvstream(struct socket *sock, struct msghdr *m,
1797 size_t buflen, int flags)
1799 struct sock *sk = sock->sk;
1800 struct tipc_sock *tsk = tipc_sk(sk);
1801 struct sk_buff *skb;
1802 struct tipc_msg *hdr;
1803 struct tipc_skb_cb *skb_cb;
1804 bool peek = flags & MSG_PEEK;
1805 int offset, required, copy, copied = 0;
1806 int hlen, dlen, err, rc;
1807 long timeout;
1809 /* Catch invalid receive attempts */
1810 if (unlikely(!buflen))
1811 return -EINVAL;
1813 lock_sock(sk);
1815 if (unlikely(sk->sk_state == TIPC_OPEN)) {
1816 rc = -ENOTCONN;
1817 goto exit;
1819 required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
1820 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1822 do {
1823 /* Look at first msg in receive queue; wait if necessary */
1824 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1825 if (unlikely(rc))
1826 break;
1827 skb = skb_peek(&sk->sk_receive_queue);
1828 skb_cb = TIPC_SKB_CB(skb);
1829 hdr = buf_msg(skb);
1830 dlen = msg_data_sz(hdr);
1831 hlen = msg_hdr_sz(hdr);
1832 err = msg_errcode(hdr);
1834 /* Discard any empty non-errored (SYN-) message */
1835 if (unlikely(!dlen && !err)) {
1836 tsk_advance_rx_queue(sk);
1837 continue;
1840 /* Collect msg meta data, incl. error code and rejected data */
1841 if (!copied) {
1842 tipc_sk_set_orig_addr(m, skb);
1843 rc = tipc_sk_anc_data_recv(m, hdr, tsk);
1844 if (rc)
1845 break;
1848 /* Copy data if msg ok, otherwise return error/partial data */
1849 if (likely(!err)) {
1850 offset = skb_cb->bytes_read;
1851 copy = min_t(int, dlen - offset, buflen - copied);
1852 rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
1853 if (unlikely(rc))
1854 break;
1855 copied += copy;
1856 offset += copy;
1857 if (unlikely(offset < dlen)) {
1858 if (!peek)
1859 skb_cb->bytes_read = offset;
1860 break;
1862 } else {
1863 rc = 0;
1864 if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control)
1865 rc = -ECONNRESET;
1866 if (copied || rc)
1867 break;
1870 if (unlikely(peek))
1871 break;
1873 tsk_advance_rx_queue(sk);
1875 /* Send connection flow control advertisement when applicable */
1876 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1877 if (unlikely(tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE))
1878 tipc_sk_send_ack(tsk);
1880 /* Exit if all requested data or FIN/error received */
1881 if (copied == buflen || err)
1882 break;
1884 } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required);
1885 exit:
1886 release_sock(sk);
1887 return copied ? copied : rc;
1891 * tipc_write_space - wake up thread if port congestion is released
1892 * @sk: socket
1894 static void tipc_write_space(struct sock *sk)
1896 struct socket_wq *wq;
1898 rcu_read_lock();
1899 wq = rcu_dereference(sk->sk_wq);
1900 if (skwq_has_sleeper(wq))
1901 wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
1902 EPOLLWRNORM | EPOLLWRBAND);
1903 rcu_read_unlock();
1907 * tipc_data_ready - wake up threads to indicate messages have been received
1908 * @sk: socket
1909 * @len: the length of messages
1911 static void tipc_data_ready(struct sock *sk)
1913 struct socket_wq *wq;
1915 rcu_read_lock();
1916 wq = rcu_dereference(sk->sk_wq);
1917 if (skwq_has_sleeper(wq))
1918 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
1919 EPOLLRDNORM | EPOLLRDBAND);
1920 rcu_read_unlock();
1923 static void tipc_sock_destruct(struct sock *sk)
1925 __skb_queue_purge(&sk->sk_receive_queue);
1928 static void tipc_sk_proto_rcv(struct sock *sk,
1929 struct sk_buff_head *inputq,
1930 struct sk_buff_head *xmitq)
1932 struct sk_buff *skb = __skb_dequeue(inputq);
1933 struct tipc_sock *tsk = tipc_sk(sk);
1934 struct tipc_msg *hdr = buf_msg(skb);
1935 struct tipc_group *grp = tsk->group;
1936 bool wakeup = false;
1938 switch (msg_user(hdr)) {
1939 case CONN_MANAGER:
1940 tipc_sk_conn_proto_rcv(tsk, skb, xmitq);
1941 return;
1942 case SOCK_WAKEUP:
1943 tipc_dest_del(&tsk->cong_links, msg_orignode(hdr), 0);
1944 tsk->cong_link_cnt--;
1945 wakeup = true;
1946 break;
1947 case GROUP_PROTOCOL:
1948 tipc_group_proto_rcv(grp, &wakeup, hdr, inputq, xmitq);
1949 break;
1950 case TOP_SRV:
1951 tipc_group_member_evt(tsk->group, &wakeup, &sk->sk_rcvbuf,
1952 hdr, inputq, xmitq);
1953 break;
1954 default:
1955 break;
1958 if (wakeup)
1959 sk->sk_write_space(sk);
1961 kfree_skb(skb);
1965 * tipc_filter_connect - Handle incoming message for a connection-based socket
1966 * @tsk: TIPC socket
1967 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
1969 * Returns true if everything ok, false otherwise
1971 static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
1973 struct sock *sk = &tsk->sk;
1974 struct net *net = sock_net(sk);
1975 struct tipc_msg *hdr = buf_msg(skb);
1976 u32 pport = msg_origport(hdr);
1977 u32 pnode = msg_orignode(hdr);
1979 if (unlikely(msg_mcast(hdr)))
1980 return false;
1982 switch (sk->sk_state) {
1983 case TIPC_CONNECTING:
1984 /* Accept only ACK or NACK message */
1985 if (unlikely(!msg_connected(hdr))) {
1986 if (pport != tsk_peer_port(tsk) ||
1987 pnode != tsk_peer_node(tsk))
1988 return false;
1990 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1991 sk->sk_err = ECONNREFUSED;
1992 sk->sk_state_change(sk);
1993 return true;
1996 if (unlikely(msg_errcode(hdr))) {
1997 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1998 sk->sk_err = ECONNREFUSED;
1999 sk->sk_state_change(sk);
2000 return true;
2003 if (unlikely(!msg_isdata(hdr))) {
2004 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2005 sk->sk_err = EINVAL;
2006 sk->sk_state_change(sk);
2007 return true;
2010 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
2011 msg_set_importance(&tsk->phdr, msg_importance(hdr));
2013 /* If 'ACK+' message, add to socket receive queue */
2014 if (msg_data_sz(hdr))
2015 return true;
2017 /* If empty 'ACK-' message, wake up sleeping connect() */
2018 sk->sk_data_ready(sk);
2020 /* 'ACK-' message is neither accepted nor rejected: */
2021 msg_set_dest_droppable(hdr, 1);
2022 return false;
2024 case TIPC_OPEN:
2025 case TIPC_DISCONNECTING:
2026 break;
2027 case TIPC_LISTEN:
2028 /* Accept only SYN message */
2029 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
2030 return true;
2031 break;
2032 case TIPC_ESTABLISHED:
2033 /* Accept only connection-based messages sent by peer */
2034 if (unlikely(!tsk_peer_msg(tsk, hdr)))
2035 return false;
2037 if (unlikely(msg_errcode(hdr))) {
2038 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2039 /* Let timer expire on it's own */
2040 tipc_node_remove_conn(net, tsk_peer_node(tsk),
2041 tsk->portid);
2042 sk->sk_state_change(sk);
2044 return true;
2045 default:
2046 pr_err("Unknown sk_state %u\n", sk->sk_state);
2049 return false;
2053 * rcvbuf_limit - get proper overload limit of socket receive queue
2054 * @sk: socket
2055 * @skb: message
2057 * For connection oriented messages, irrespective of importance,
2058 * default queue limit is 2 MB.
2060 * For connectionless messages, queue limits are based on message
2061 * importance as follows:
2063 * TIPC_LOW_IMPORTANCE (2 MB)
2064 * TIPC_MEDIUM_IMPORTANCE (4 MB)
2065 * TIPC_HIGH_IMPORTANCE (8 MB)
2066 * TIPC_CRITICAL_IMPORTANCE (16 MB)
2068 * Returns overload limit according to corresponding message importance
2070 static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
2072 struct tipc_sock *tsk = tipc_sk(sk);
2073 struct tipc_msg *hdr = buf_msg(skb);
2075 if (unlikely(msg_in_group(hdr)))
2076 return sk->sk_rcvbuf;
2078 if (unlikely(!msg_connected(hdr)))
2079 return sk->sk_rcvbuf << msg_importance(hdr);
2081 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
2082 return sk->sk_rcvbuf;
2084 return FLOWCTL_MSG_LIM;
2088 * tipc_sk_filter_rcv - validate incoming message
2089 * @sk: socket
2090 * @skb: pointer to message.
2092 * Enqueues message on receive queue if acceptable; optionally handles
2093 * disconnect indication for a connected socket.
2095 * Called with socket lock already taken
2098 static void tipc_sk_filter_rcv(struct sock *sk, struct sk_buff *skb,
2099 struct sk_buff_head *xmitq)
2101 bool sk_conn = !tipc_sk_type_connectionless(sk);
2102 struct tipc_sock *tsk = tipc_sk(sk);
2103 struct tipc_group *grp = tsk->group;
2104 struct tipc_msg *hdr = buf_msg(skb);
2105 struct net *net = sock_net(sk);
2106 struct sk_buff_head inputq;
2107 int limit, err = TIPC_OK;
2109 TIPC_SKB_CB(skb)->bytes_read = 0;
2110 __skb_queue_head_init(&inputq);
2111 __skb_queue_tail(&inputq, skb);
2113 if (unlikely(!msg_isdata(hdr)))
2114 tipc_sk_proto_rcv(sk, &inputq, xmitq);
2116 if (unlikely(grp))
2117 tipc_group_filter_msg(grp, &inputq, xmitq);
2119 /* Validate and add to receive buffer if there is space */
2120 while ((skb = __skb_dequeue(&inputq))) {
2121 hdr = buf_msg(skb);
2122 limit = rcvbuf_limit(sk, skb);
2123 if ((sk_conn && !tipc_sk_filter_connect(tsk, skb)) ||
2124 (!sk_conn && msg_connected(hdr)) ||
2125 (!grp && msg_in_group(hdr)))
2126 err = TIPC_ERR_NO_PORT;
2127 else if (sk_rmem_alloc_get(sk) + skb->truesize >= limit)
2128 err = TIPC_ERR_OVERLOAD;
2130 if (unlikely(err)) {
2131 tipc_skb_reject(net, err, skb, xmitq);
2132 err = TIPC_OK;
2133 continue;
2135 __skb_queue_tail(&sk->sk_receive_queue, skb);
2136 skb_set_owner_r(skb, sk);
2137 sk->sk_data_ready(sk);
2142 * tipc_sk_backlog_rcv - handle incoming message from backlog queue
2143 * @sk: socket
2144 * @skb: message
2146 * Caller must hold socket lock
2148 static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
2150 unsigned int before = sk_rmem_alloc_get(sk);
2151 struct sk_buff_head xmitq;
2152 unsigned int added;
2154 __skb_queue_head_init(&xmitq);
2156 tipc_sk_filter_rcv(sk, skb, &xmitq);
2157 added = sk_rmem_alloc_get(sk) - before;
2158 atomic_add(added, &tipc_sk(sk)->dupl_rcvcnt);
2160 /* Send pending response/rejected messages, if any */
2161 tipc_node_distr_xmit(sock_net(sk), &xmitq);
2162 return 0;
2166 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
2167 * inputq and try adding them to socket or backlog queue
2168 * @inputq: list of incoming buffers with potentially different destinations
2169 * @sk: socket where the buffers should be enqueued
2170 * @dport: port number for the socket
2172 * Caller must hold socket lock
2174 static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
2175 u32 dport, struct sk_buff_head *xmitq)
2177 unsigned long time_limit = jiffies + 2;
2178 struct sk_buff *skb;
2179 unsigned int lim;
2180 atomic_t *dcnt;
2181 u32 onode;
2183 while (skb_queue_len(inputq)) {
2184 if (unlikely(time_after_eq(jiffies, time_limit)))
2185 return;
2187 skb = tipc_skb_dequeue(inputq, dport);
2188 if (unlikely(!skb))
2189 return;
2191 /* Add message directly to receive queue if possible */
2192 if (!sock_owned_by_user(sk)) {
2193 tipc_sk_filter_rcv(sk, skb, xmitq);
2194 continue;
2197 /* Try backlog, compensating for double-counted bytes */
2198 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
2199 if (!sk->sk_backlog.len)
2200 atomic_set(dcnt, 0);
2201 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
2202 if (likely(!sk_add_backlog(sk, skb, lim)))
2203 continue;
2205 /* Overload => reject message back to sender */
2206 onode = tipc_own_addr(sock_net(sk));
2207 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
2208 __skb_queue_tail(xmitq, skb);
2209 break;
2214 * tipc_sk_rcv - handle a chain of incoming buffers
2215 * @inputq: buffer list containing the buffers
2216 * Consumes all buffers in list until inputq is empty
2217 * Note: may be called in multiple threads referring to the same queue
2219 void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
2221 struct sk_buff_head xmitq;
2222 u32 dnode, dport = 0;
2223 int err;
2224 struct tipc_sock *tsk;
2225 struct sock *sk;
2226 struct sk_buff *skb;
2228 __skb_queue_head_init(&xmitq);
2229 while (skb_queue_len(inputq)) {
2230 dport = tipc_skb_peek_port(inputq, dport);
2231 tsk = tipc_sk_lookup(net, dport);
2233 if (likely(tsk)) {
2234 sk = &tsk->sk;
2235 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
2236 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
2237 spin_unlock_bh(&sk->sk_lock.slock);
2239 /* Send pending response/rejected messages, if any */
2240 tipc_node_distr_xmit(sock_net(sk), &xmitq);
2241 sock_put(sk);
2242 continue;
2244 /* No destination socket => dequeue skb if still there */
2245 skb = tipc_skb_dequeue(inputq, dport);
2246 if (!skb)
2247 return;
2249 /* Try secondary lookup if unresolved named message */
2250 err = TIPC_ERR_NO_PORT;
2251 if (tipc_msg_lookup_dest(net, skb, &err))
2252 goto xmit;
2254 /* Prepare for message rejection */
2255 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
2256 continue;
2257 xmit:
2258 dnode = msg_destnode(buf_msg(skb));
2259 tipc_node_xmit_skb(net, skb, dnode, dport);
2263 static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
2265 DEFINE_WAIT_FUNC(wait, woken_wake_function);
2266 struct sock *sk = sock->sk;
2267 int done;
2269 do {
2270 int err = sock_error(sk);
2271 if (err)
2272 return err;
2273 if (!*timeo_p)
2274 return -ETIMEDOUT;
2275 if (signal_pending(current))
2276 return sock_intr_errno(*timeo_p);
2278 add_wait_queue(sk_sleep(sk), &wait);
2279 done = sk_wait_event(sk, timeo_p,
2280 sk->sk_state != TIPC_CONNECTING, &wait);
2281 remove_wait_queue(sk_sleep(sk), &wait);
2282 } while (!done);
2283 return 0;
2287 * tipc_connect - establish a connection to another TIPC port
2288 * @sock: socket structure
2289 * @dest: socket address for destination port
2290 * @destlen: size of socket address data structure
2291 * @flags: file-related flags associated with socket
2293 * Returns 0 on success, errno otherwise
2295 static int tipc_connect(struct socket *sock, struct sockaddr *dest,
2296 int destlen, int flags)
2298 struct sock *sk = sock->sk;
2299 struct tipc_sock *tsk = tipc_sk(sk);
2300 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
2301 struct msghdr m = {NULL,};
2302 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
2303 int previous;
2304 int res = 0;
2306 if (destlen != sizeof(struct sockaddr_tipc))
2307 return -EINVAL;
2309 lock_sock(sk);
2311 if (tsk->group) {
2312 res = -EINVAL;
2313 goto exit;
2316 if (dst->family == AF_UNSPEC) {
2317 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
2318 if (!tipc_sk_type_connectionless(sk))
2319 res = -EINVAL;
2320 goto exit;
2321 } else if (dst->family != AF_TIPC) {
2322 res = -EINVAL;
2324 if (dst->addrtype != TIPC_ADDR_ID && dst->addrtype != TIPC_ADDR_NAME)
2325 res = -EINVAL;
2326 if (res)
2327 goto exit;
2329 /* DGRAM/RDM connect(), just save the destaddr */
2330 if (tipc_sk_type_connectionless(sk)) {
2331 memcpy(&tsk->peer, dest, destlen);
2332 goto exit;
2335 previous = sk->sk_state;
2337 switch (sk->sk_state) {
2338 case TIPC_OPEN:
2339 /* Send a 'SYN-' to destination */
2340 m.msg_name = dest;
2341 m.msg_namelen = destlen;
2343 /* If connect is in non-blocking case, set MSG_DONTWAIT to
2344 * indicate send_msg() is never blocked.
2346 if (!timeout)
2347 m.msg_flags = MSG_DONTWAIT;
2349 res = __tipc_sendmsg(sock, &m, 0);
2350 if ((res < 0) && (res != -EWOULDBLOCK))
2351 goto exit;
2353 /* Just entered TIPC_CONNECTING state; the only
2354 * difference is that return value in non-blocking
2355 * case is EINPROGRESS, rather than EALREADY.
2357 res = -EINPROGRESS;
2358 /* fall thru' */
2359 case TIPC_CONNECTING:
2360 if (!timeout) {
2361 if (previous == TIPC_CONNECTING)
2362 res = -EALREADY;
2363 goto exit;
2365 timeout = msecs_to_jiffies(timeout);
2366 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
2367 res = tipc_wait_for_connect(sock, &timeout);
2368 break;
2369 case TIPC_ESTABLISHED:
2370 res = -EISCONN;
2371 break;
2372 default:
2373 res = -EINVAL;
2376 exit:
2377 release_sock(sk);
2378 return res;
2382 * tipc_listen - allow socket to listen for incoming connections
2383 * @sock: socket structure
2384 * @len: (unused)
2386 * Returns 0 on success, errno otherwise
2388 static int tipc_listen(struct socket *sock, int len)
2390 struct sock *sk = sock->sk;
2391 int res;
2393 lock_sock(sk);
2394 res = tipc_set_sk_state(sk, TIPC_LISTEN);
2395 release_sock(sk);
2397 return res;
2400 static int tipc_wait_for_accept(struct socket *sock, long timeo)
2402 struct sock *sk = sock->sk;
2403 DEFINE_WAIT(wait);
2404 int err;
2406 /* True wake-one mechanism for incoming connections: only
2407 * one process gets woken up, not the 'whole herd'.
2408 * Since we do not 'race & poll' for established sockets
2409 * anymore, the common case will execute the loop only once.
2411 for (;;) {
2412 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2413 TASK_INTERRUPTIBLE);
2414 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
2415 release_sock(sk);
2416 timeo = schedule_timeout(timeo);
2417 lock_sock(sk);
2419 err = 0;
2420 if (!skb_queue_empty(&sk->sk_receive_queue))
2421 break;
2422 err = -EAGAIN;
2423 if (!timeo)
2424 break;
2425 err = sock_intr_errno(timeo);
2426 if (signal_pending(current))
2427 break;
2429 finish_wait(sk_sleep(sk), &wait);
2430 return err;
2434 * tipc_accept - wait for connection request
2435 * @sock: listening socket
2436 * @newsock: new socket that is to be connected
2437 * @flags: file-related flags associated with socket
2439 * Returns 0 on success, errno otherwise
2441 static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
2442 bool kern)
2444 struct sock *new_sk, *sk = sock->sk;
2445 struct sk_buff *buf;
2446 struct tipc_sock *new_tsock;
2447 struct tipc_msg *msg;
2448 long timeo;
2449 int res;
2451 lock_sock(sk);
2453 if (sk->sk_state != TIPC_LISTEN) {
2454 res = -EINVAL;
2455 goto exit;
2457 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2458 res = tipc_wait_for_accept(sock, timeo);
2459 if (res)
2460 goto exit;
2462 buf = skb_peek(&sk->sk_receive_queue);
2464 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
2465 if (res)
2466 goto exit;
2467 security_sk_clone(sock->sk, new_sock->sk);
2469 new_sk = new_sock->sk;
2470 new_tsock = tipc_sk(new_sk);
2471 msg = buf_msg(buf);
2473 /* we lock on new_sk; but lockdep sees the lock on sk */
2474 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
2477 * Reject any stray messages received by new socket
2478 * before the socket lock was taken (very, very unlikely)
2480 tsk_rej_rx_queue(new_sk);
2482 /* Connect new socket to it's peer */
2483 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
2485 tsk_set_importance(new_tsock, msg_importance(msg));
2486 if (msg_named(msg)) {
2487 new_tsock->conn_type = msg_nametype(msg);
2488 new_tsock->conn_instance = msg_nameinst(msg);
2492 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2493 * Respond to 'SYN+' by queuing it on new socket.
2495 if (!msg_data_sz(msg)) {
2496 struct msghdr m = {NULL,};
2498 tsk_advance_rx_queue(sk);
2499 __tipc_sendstream(new_sock, &m, 0);
2500 } else {
2501 __skb_dequeue(&sk->sk_receive_queue);
2502 __skb_queue_head(&new_sk->sk_receive_queue, buf);
2503 skb_set_owner_r(buf, new_sk);
2505 release_sock(new_sk);
2506 exit:
2507 release_sock(sk);
2508 return res;
2512 * tipc_shutdown - shutdown socket connection
2513 * @sock: socket structure
2514 * @how: direction to close (must be SHUT_RDWR)
2516 * Terminates connection (if necessary), then purges socket's receive queue.
2518 * Returns 0 on success, errno otherwise
2520 static int tipc_shutdown(struct socket *sock, int how)
2522 struct sock *sk = sock->sk;
2523 int res;
2525 if (how != SHUT_RDWR)
2526 return -EINVAL;
2528 lock_sock(sk);
2530 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2531 sk->sk_shutdown = SEND_SHUTDOWN;
2533 if (sk->sk_state == TIPC_DISCONNECTING) {
2534 /* Discard any unreceived messages */
2535 __skb_queue_purge(&sk->sk_receive_queue);
2537 /* Wake up anyone sleeping in poll */
2538 sk->sk_state_change(sk);
2539 res = 0;
2540 } else {
2541 res = -ENOTCONN;
2544 release_sock(sk);
2545 return res;
2548 static void tipc_sk_timeout(struct timer_list *t)
2550 struct sock *sk = from_timer(sk, t, sk_timer);
2551 struct tipc_sock *tsk = tipc_sk(sk);
2552 u32 peer_port = tsk_peer_port(tsk);
2553 u32 peer_node = tsk_peer_node(tsk);
2554 u32 own_node = tsk_own_node(tsk);
2555 u32 own_port = tsk->portid;
2556 struct net *net = sock_net(sk);
2557 struct sk_buff *skb = NULL;
2559 bh_lock_sock(sk);
2560 if (!tipc_sk_connected(sk))
2561 goto exit;
2563 /* Try again later if socket is busy */
2564 if (sock_owned_by_user(sk)) {
2565 sk_reset_timer(sk, &sk->sk_timer, jiffies + HZ / 20);
2566 goto exit;
2569 if (tsk->probe_unacked) {
2570 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2571 tipc_node_remove_conn(net, peer_node, peer_port);
2572 sk->sk_state_change(sk);
2573 goto exit;
2575 /* Send new probe */
2576 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, 0,
2577 peer_node, own_node, peer_port, own_port,
2578 TIPC_OK);
2579 tsk->probe_unacked = true;
2580 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
2581 exit:
2582 bh_unlock_sock(sk);
2583 if (skb)
2584 tipc_node_xmit_skb(net, skb, peer_node, own_port);
2585 sock_put(sk);
2588 static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
2589 struct tipc_name_seq const *seq)
2591 struct sock *sk = &tsk->sk;
2592 struct net *net = sock_net(sk);
2593 struct publication *publ;
2594 u32 key;
2596 if (tipc_sk_connected(sk))
2597 return -EINVAL;
2598 key = tsk->portid + tsk->pub_count + 1;
2599 if (key == tsk->portid)
2600 return -EADDRINUSE;
2602 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
2603 scope, tsk->portid, key);
2604 if (unlikely(!publ))
2605 return -EINVAL;
2607 list_add(&publ->pport_list, &tsk->publications);
2608 tsk->pub_count++;
2609 tsk->published = 1;
2610 return 0;
2613 static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
2614 struct tipc_name_seq const *seq)
2616 struct net *net = sock_net(&tsk->sk);
2617 struct publication *publ;
2618 struct publication *safe;
2619 int rc = -EINVAL;
2621 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
2622 if (seq) {
2623 if (publ->scope != scope)
2624 continue;
2625 if (publ->type != seq->type)
2626 continue;
2627 if (publ->lower != seq->lower)
2628 continue;
2629 if (publ->upper != seq->upper)
2630 break;
2631 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2632 publ->ref, publ->key);
2633 rc = 0;
2634 break;
2636 tipc_nametbl_withdraw(net, publ->type, publ->lower,
2637 publ->ref, publ->key);
2638 rc = 0;
2640 if (list_empty(&tsk->publications))
2641 tsk->published = 0;
2642 return rc;
2645 /* tipc_sk_reinit: set non-zero address in all existing sockets
2646 * when we go from standalone to network mode.
2648 void tipc_sk_reinit(struct net *net)
2650 struct tipc_net *tn = net_generic(net, tipc_net_id);
2651 struct rhashtable_iter iter;
2652 struct tipc_sock *tsk;
2653 struct tipc_msg *msg;
2655 rhashtable_walk_enter(&tn->sk_rht, &iter);
2657 do {
2658 rhashtable_walk_start(&iter);
2660 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
2661 spin_lock_bh(&tsk->sk.sk_lock.slock);
2662 msg = &tsk->phdr;
2663 msg_set_prevnode(msg, tn->own_addr);
2664 msg_set_orignode(msg, tn->own_addr);
2665 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2668 rhashtable_walk_stop(&iter);
2669 } while (tsk == ERR_PTR(-EAGAIN));
2672 static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
2674 struct tipc_net *tn = net_generic(net, tipc_net_id);
2675 struct tipc_sock *tsk;
2677 rcu_read_lock();
2678 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
2679 if (tsk)
2680 sock_hold(&tsk->sk);
2681 rcu_read_unlock();
2683 return tsk;
2686 static int tipc_sk_insert(struct tipc_sock *tsk)
2688 struct sock *sk = &tsk->sk;
2689 struct net *net = sock_net(sk);
2690 struct tipc_net *tn = net_generic(net, tipc_net_id);
2691 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2692 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2694 while (remaining--) {
2695 portid++;
2696 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2697 portid = TIPC_MIN_PORT;
2698 tsk->portid = portid;
2699 sock_hold(&tsk->sk);
2700 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2701 tsk_rht_params))
2702 return 0;
2703 sock_put(&tsk->sk);
2706 return -1;
2709 static void tipc_sk_remove(struct tipc_sock *tsk)
2711 struct sock *sk = &tsk->sk;
2712 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
2714 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
2715 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
2716 __sock_put(sk);
2720 static const struct rhashtable_params tsk_rht_params = {
2721 .nelem_hint = 192,
2722 .head_offset = offsetof(struct tipc_sock, node),
2723 .key_offset = offsetof(struct tipc_sock, portid),
2724 .key_len = sizeof(u32), /* portid */
2725 .max_size = 1048576,
2726 .min_size = 256,
2727 .automatic_shrinking = true,
2730 int tipc_sk_rht_init(struct net *net)
2732 struct tipc_net *tn = net_generic(net, tipc_net_id);
2734 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
2737 void tipc_sk_rht_destroy(struct net *net)
2739 struct tipc_net *tn = net_generic(net, tipc_net_id);
2741 /* Wait for socket readers to complete */
2742 synchronize_net();
2744 rhashtable_destroy(&tn->sk_rht);
2747 static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq)
2749 struct net *net = sock_net(&tsk->sk);
2750 struct tipc_group *grp = tsk->group;
2751 struct tipc_msg *hdr = &tsk->phdr;
2752 struct tipc_name_seq seq;
2753 int rc;
2755 if (mreq->type < TIPC_RESERVED_TYPES)
2756 return -EACCES;
2757 if (mreq->scope > TIPC_NODE_SCOPE)
2758 return -EINVAL;
2759 if (grp)
2760 return -EACCES;
2761 grp = tipc_group_create(net, tsk->portid, mreq, &tsk->group_is_open);
2762 if (!grp)
2763 return -ENOMEM;
2764 tsk->group = grp;
2765 msg_set_lookup_scope(hdr, mreq->scope);
2766 msg_set_nametype(hdr, mreq->type);
2767 msg_set_dest_droppable(hdr, true);
2768 seq.type = mreq->type;
2769 seq.lower = mreq->instance;
2770 seq.upper = seq.lower;
2771 tipc_nametbl_build_group(net, grp, mreq->type, mreq->scope);
2772 rc = tipc_sk_publish(tsk, mreq->scope, &seq);
2773 if (rc) {
2774 tipc_group_delete(net, grp);
2775 tsk->group = NULL;
2776 return rc;
2778 /* Eliminate any risk that a broadcast overtakes sent JOINs */
2779 tsk->mc_method.rcast = true;
2780 tsk->mc_method.mandatory = true;
2781 tipc_group_join(net, grp, &tsk->sk.sk_rcvbuf);
2782 return rc;
2785 static int tipc_sk_leave(struct tipc_sock *tsk)
2787 struct net *net = sock_net(&tsk->sk);
2788 struct tipc_group *grp = tsk->group;
2789 struct tipc_name_seq seq;
2790 int scope;
2792 if (!grp)
2793 return -EINVAL;
2794 tipc_group_self(grp, &seq, &scope);
2795 tipc_group_delete(net, grp);
2796 tsk->group = NULL;
2797 tipc_sk_withdraw(tsk, scope, &seq);
2798 return 0;
2802 * tipc_setsockopt - set socket option
2803 * @sock: socket structure
2804 * @lvl: option level
2805 * @opt: option identifier
2806 * @ov: pointer to new option value
2807 * @ol: length of option value
2809 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
2810 * (to ease compatibility).
2812 * Returns 0 on success, errno otherwise
2814 static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2815 char __user *ov, unsigned int ol)
2817 struct sock *sk = sock->sk;
2818 struct tipc_sock *tsk = tipc_sk(sk);
2819 struct tipc_group_req mreq;
2820 u32 value = 0;
2821 int res = 0;
2823 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2824 return 0;
2825 if (lvl != SOL_TIPC)
2826 return -ENOPROTOOPT;
2828 switch (opt) {
2829 case TIPC_IMPORTANCE:
2830 case TIPC_SRC_DROPPABLE:
2831 case TIPC_DEST_DROPPABLE:
2832 case TIPC_CONN_TIMEOUT:
2833 if (ol < sizeof(value))
2834 return -EINVAL;
2835 if (get_user(value, (u32 __user *)ov))
2836 return -EFAULT;
2837 break;
2838 case TIPC_GROUP_JOIN:
2839 if (ol < sizeof(mreq))
2840 return -EINVAL;
2841 if (copy_from_user(&mreq, ov, sizeof(mreq)))
2842 return -EFAULT;
2843 break;
2844 default:
2845 if (ov || ol)
2846 return -EINVAL;
2849 lock_sock(sk);
2851 switch (opt) {
2852 case TIPC_IMPORTANCE:
2853 res = tsk_set_importance(tsk, value);
2854 break;
2855 case TIPC_SRC_DROPPABLE:
2856 if (sock->type != SOCK_STREAM)
2857 tsk_set_unreliable(tsk, value);
2858 else
2859 res = -ENOPROTOOPT;
2860 break;
2861 case TIPC_DEST_DROPPABLE:
2862 tsk_set_unreturnable(tsk, value);
2863 break;
2864 case TIPC_CONN_TIMEOUT:
2865 tipc_sk(sk)->conn_timeout = value;
2866 break;
2867 case TIPC_MCAST_BROADCAST:
2868 tsk->mc_method.rcast = false;
2869 tsk->mc_method.mandatory = true;
2870 break;
2871 case TIPC_MCAST_REPLICAST:
2872 tsk->mc_method.rcast = true;
2873 tsk->mc_method.mandatory = true;
2874 break;
2875 case TIPC_GROUP_JOIN:
2876 res = tipc_sk_join(tsk, &mreq);
2877 break;
2878 case TIPC_GROUP_LEAVE:
2879 res = tipc_sk_leave(tsk);
2880 break;
2881 default:
2882 res = -EINVAL;
2885 release_sock(sk);
2887 return res;
2891 * tipc_getsockopt - get socket option
2892 * @sock: socket structure
2893 * @lvl: option level
2894 * @opt: option identifier
2895 * @ov: receptacle for option value
2896 * @ol: receptacle for length of option value
2898 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
2899 * (to ease compatibility).
2901 * Returns 0 on success, errno otherwise
2903 static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2904 char __user *ov, int __user *ol)
2906 struct sock *sk = sock->sk;
2907 struct tipc_sock *tsk = tipc_sk(sk);
2908 struct tipc_name_seq seq;
2909 int len, scope;
2910 u32 value;
2911 int res;
2913 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2914 return put_user(0, ol);
2915 if (lvl != SOL_TIPC)
2916 return -ENOPROTOOPT;
2917 res = get_user(len, ol);
2918 if (res)
2919 return res;
2921 lock_sock(sk);
2923 switch (opt) {
2924 case TIPC_IMPORTANCE:
2925 value = tsk_importance(tsk);
2926 break;
2927 case TIPC_SRC_DROPPABLE:
2928 value = tsk_unreliable(tsk);
2929 break;
2930 case TIPC_DEST_DROPPABLE:
2931 value = tsk_unreturnable(tsk);
2932 break;
2933 case TIPC_CONN_TIMEOUT:
2934 value = tsk->conn_timeout;
2935 /* no need to set "res", since already 0 at this point */
2936 break;
2937 case TIPC_NODE_RECVQ_DEPTH:
2938 value = 0; /* was tipc_queue_size, now obsolete */
2939 break;
2940 case TIPC_SOCK_RECVQ_DEPTH:
2941 value = skb_queue_len(&sk->sk_receive_queue);
2942 break;
2943 case TIPC_GROUP_JOIN:
2944 seq.type = 0;
2945 if (tsk->group)
2946 tipc_group_self(tsk->group, &seq, &scope);
2947 value = seq.type;
2948 break;
2949 default:
2950 res = -EINVAL;
2953 release_sock(sk);
2955 if (res)
2956 return res; /* "get" failed */
2958 if (len < sizeof(value))
2959 return -EINVAL;
2961 if (copy_to_user(ov, &value, sizeof(value)))
2962 return -EFAULT;
2964 return put_user(sizeof(value), ol);
2967 static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
2969 struct sock *sk = sock->sk;
2970 struct tipc_sioc_ln_req lnr;
2971 void __user *argp = (void __user *)arg;
2973 switch (cmd) {
2974 case SIOCGETLINKNAME:
2975 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2976 return -EFAULT;
2977 if (!tipc_node_get_linkname(sock_net(sk),
2978 lnr.bearer_id & 0xffff, lnr.peer,
2979 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2980 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2981 return -EFAULT;
2982 return 0;
2984 return -EADDRNOTAVAIL;
2985 default:
2986 return -ENOIOCTLCMD;
2990 static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
2992 struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
2993 struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
2994 u32 onode = tipc_own_addr(sock_net(sock1->sk));
2996 tsk1->peer.family = AF_TIPC;
2997 tsk1->peer.addrtype = TIPC_ADDR_ID;
2998 tsk1->peer.scope = TIPC_NODE_SCOPE;
2999 tsk1->peer.addr.id.ref = tsk2->portid;
3000 tsk1->peer.addr.id.node = onode;
3001 tsk2->peer.family = AF_TIPC;
3002 tsk2->peer.addrtype = TIPC_ADDR_ID;
3003 tsk2->peer.scope = TIPC_NODE_SCOPE;
3004 tsk2->peer.addr.id.ref = tsk1->portid;
3005 tsk2->peer.addr.id.node = onode;
3007 tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
3008 tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
3009 return 0;
3012 /* Protocol switches for the various types of TIPC sockets */
3014 static const struct proto_ops msg_ops = {
3015 .owner = THIS_MODULE,
3016 .family = AF_TIPC,
3017 .release = tipc_release,
3018 .bind = tipc_bind,
3019 .connect = tipc_connect,
3020 .socketpair = tipc_socketpair,
3021 .accept = sock_no_accept,
3022 .getname = tipc_getname,
3023 .poll = tipc_poll,
3024 .ioctl = tipc_ioctl,
3025 .listen = sock_no_listen,
3026 .shutdown = tipc_shutdown,
3027 .setsockopt = tipc_setsockopt,
3028 .getsockopt = tipc_getsockopt,
3029 .sendmsg = tipc_sendmsg,
3030 .recvmsg = tipc_recvmsg,
3031 .mmap = sock_no_mmap,
3032 .sendpage = sock_no_sendpage
3035 static const struct proto_ops packet_ops = {
3036 .owner = THIS_MODULE,
3037 .family = AF_TIPC,
3038 .release = tipc_release,
3039 .bind = tipc_bind,
3040 .connect = tipc_connect,
3041 .socketpair = tipc_socketpair,
3042 .accept = tipc_accept,
3043 .getname = tipc_getname,
3044 .poll = tipc_poll,
3045 .ioctl = tipc_ioctl,
3046 .listen = tipc_listen,
3047 .shutdown = tipc_shutdown,
3048 .setsockopt = tipc_setsockopt,
3049 .getsockopt = tipc_getsockopt,
3050 .sendmsg = tipc_send_packet,
3051 .recvmsg = tipc_recvmsg,
3052 .mmap = sock_no_mmap,
3053 .sendpage = sock_no_sendpage
3056 static const struct proto_ops stream_ops = {
3057 .owner = THIS_MODULE,
3058 .family = AF_TIPC,
3059 .release = tipc_release,
3060 .bind = tipc_bind,
3061 .connect = tipc_connect,
3062 .socketpair = tipc_socketpair,
3063 .accept = tipc_accept,
3064 .getname = tipc_getname,
3065 .poll = tipc_poll,
3066 .ioctl = tipc_ioctl,
3067 .listen = tipc_listen,
3068 .shutdown = tipc_shutdown,
3069 .setsockopt = tipc_setsockopt,
3070 .getsockopt = tipc_getsockopt,
3071 .sendmsg = tipc_sendstream,
3072 .recvmsg = tipc_recvstream,
3073 .mmap = sock_no_mmap,
3074 .sendpage = sock_no_sendpage
3077 static const struct net_proto_family tipc_family_ops = {
3078 .owner = THIS_MODULE,
3079 .family = AF_TIPC,
3080 .create = tipc_sk_create
3083 static struct proto tipc_proto = {
3084 .name = "TIPC",
3085 .owner = THIS_MODULE,
3086 .obj_size = sizeof(struct tipc_sock),
3087 .sysctl_rmem = sysctl_tipc_rmem
3091 * tipc_socket_init - initialize TIPC socket interface
3093 * Returns 0 on success, errno otherwise
3095 int tipc_socket_init(void)
3097 int res;
3099 res = proto_register(&tipc_proto, 1);
3100 if (res) {
3101 pr_err("Failed to register TIPC protocol type\n");
3102 goto out;
3105 res = sock_register(&tipc_family_ops);
3106 if (res) {
3107 pr_err("Failed to register TIPC socket type\n");
3108 proto_unregister(&tipc_proto);
3109 goto out;
3111 out:
3112 return res;
3116 * tipc_socket_stop - stop TIPC socket interface
3118 void tipc_socket_stop(void)
3120 sock_unregister(tipc_family_ops.family);
3121 proto_unregister(&tipc_proto);
3124 /* Caller should hold socket lock for the passed tipc socket. */
3125 static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
3127 u32 peer_node;
3128 u32 peer_port;
3129 struct nlattr *nest;
3131 peer_node = tsk_peer_node(tsk);
3132 peer_port = tsk_peer_port(tsk);
3134 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
3136 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
3137 goto msg_full;
3138 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
3139 goto msg_full;
3141 if (tsk->conn_type != 0) {
3142 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
3143 goto msg_full;
3144 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
3145 goto msg_full;
3146 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
3147 goto msg_full;
3149 nla_nest_end(skb, nest);
3151 return 0;
3153 msg_full:
3154 nla_nest_cancel(skb, nest);
3156 return -EMSGSIZE;
3159 /* Caller should hold socket lock for the passed tipc socket. */
3160 static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
3161 struct tipc_sock *tsk)
3163 int err;
3164 void *hdr;
3165 struct nlattr *attrs;
3166 struct net *net = sock_net(skb->sk);
3167 struct tipc_net *tn = net_generic(net, tipc_net_id);
3168 struct sock *sk = &tsk->sk;
3170 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3171 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
3172 if (!hdr)
3173 goto msg_cancel;
3175 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
3176 if (!attrs)
3177 goto genlmsg_cancel;
3178 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
3179 goto attr_msg_cancel;
3180 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
3181 goto attr_msg_cancel;
3183 if (tipc_sk_connected(sk)) {
3184 err = __tipc_nl_add_sk_con(skb, tsk);
3185 if (err)
3186 goto attr_msg_cancel;
3187 } else if (!list_empty(&tsk->publications)) {
3188 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
3189 goto attr_msg_cancel;
3191 nla_nest_end(skb, attrs);
3192 genlmsg_end(skb, hdr);
3194 return 0;
3196 attr_msg_cancel:
3197 nla_nest_cancel(skb, attrs);
3198 genlmsg_cancel:
3199 genlmsg_cancel(skb, hdr);
3200 msg_cancel:
3201 return -EMSGSIZE;
3204 int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
3206 int err;
3207 struct tipc_sock *tsk;
3208 const struct bucket_table *tbl;
3209 struct rhash_head *pos;
3210 struct net *net = sock_net(skb->sk);
3211 struct tipc_net *tn = net_generic(net, tipc_net_id);
3212 u32 tbl_id = cb->args[0];
3213 u32 prev_portid = cb->args[1];
3215 rcu_read_lock();
3216 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
3217 for (; tbl_id < tbl->size; tbl_id++) {
3218 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
3219 spin_lock_bh(&tsk->sk.sk_lock.slock);
3220 if (prev_portid && prev_portid != tsk->portid) {
3221 spin_unlock_bh(&tsk->sk.sk_lock.slock);
3222 continue;
3225 err = __tipc_nl_add_sk(skb, cb, tsk);
3226 if (err) {
3227 prev_portid = tsk->portid;
3228 spin_unlock_bh(&tsk->sk.sk_lock.slock);
3229 goto out;
3231 prev_portid = 0;
3232 spin_unlock_bh(&tsk->sk.sk_lock.slock);
3235 out:
3236 rcu_read_unlock();
3237 cb->args[0] = tbl_id;
3238 cb->args[1] = prev_portid;
3240 return skb->len;
3243 /* Caller should hold socket lock for the passed tipc socket. */
3244 static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
3245 struct netlink_callback *cb,
3246 struct publication *publ)
3248 void *hdr;
3249 struct nlattr *attrs;
3251 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
3252 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
3253 if (!hdr)
3254 goto msg_cancel;
3256 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
3257 if (!attrs)
3258 goto genlmsg_cancel;
3260 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
3261 goto attr_msg_cancel;
3262 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
3263 goto attr_msg_cancel;
3264 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
3265 goto attr_msg_cancel;
3266 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
3267 goto attr_msg_cancel;
3269 nla_nest_end(skb, attrs);
3270 genlmsg_end(skb, hdr);
3272 return 0;
3274 attr_msg_cancel:
3275 nla_nest_cancel(skb, attrs);
3276 genlmsg_cancel:
3277 genlmsg_cancel(skb, hdr);
3278 msg_cancel:
3279 return -EMSGSIZE;
3282 /* Caller should hold socket lock for the passed tipc socket. */
3283 static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
3284 struct netlink_callback *cb,
3285 struct tipc_sock *tsk, u32 *last_publ)
3287 int err;
3288 struct publication *p;
3290 if (*last_publ) {
3291 list_for_each_entry(p, &tsk->publications, pport_list) {
3292 if (p->key == *last_publ)
3293 break;
3295 if (p->key != *last_publ) {
3296 /* We never set seq or call nl_dump_check_consistent()
3297 * this means that setting prev_seq here will cause the
3298 * consistence check to fail in the netlink callback
3299 * handler. Resulting in the last NLMSG_DONE message
3300 * having the NLM_F_DUMP_INTR flag set.
3302 cb->prev_seq = 1;
3303 *last_publ = 0;
3304 return -EPIPE;
3306 } else {
3307 p = list_first_entry(&tsk->publications, struct publication,
3308 pport_list);
3311 list_for_each_entry_from(p, &tsk->publications, pport_list) {
3312 err = __tipc_nl_add_sk_publ(skb, cb, p);
3313 if (err) {
3314 *last_publ = p->key;
3315 return err;
3318 *last_publ = 0;
3320 return 0;
3323 int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
3325 int err;
3326 u32 tsk_portid = cb->args[0];
3327 u32 last_publ = cb->args[1];
3328 u32 done = cb->args[2];
3329 struct net *net = sock_net(skb->sk);
3330 struct tipc_sock *tsk;
3332 if (!tsk_portid) {
3333 struct nlattr **attrs;
3334 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
3336 err = tipc_nlmsg_parse(cb->nlh, &attrs);
3337 if (err)
3338 return err;
3340 if (!attrs[TIPC_NLA_SOCK])
3341 return -EINVAL;
3343 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
3344 attrs[TIPC_NLA_SOCK],
3345 tipc_nl_sock_policy, NULL);
3346 if (err)
3347 return err;
3349 if (!sock[TIPC_NLA_SOCK_REF])
3350 return -EINVAL;
3352 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
3355 if (done)
3356 return 0;
3358 tsk = tipc_sk_lookup(net, tsk_portid);
3359 if (!tsk)
3360 return -EINVAL;
3362 lock_sock(&tsk->sk);
3363 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
3364 if (!err)
3365 done = 1;
3366 release_sock(&tsk->sk);
3367 sock_put(&tsk->sk);
3369 cb->args[0] = tsk_portid;
3370 cb->args[1] = last_publ;
3371 cb->args[2] = done;
3373 return skb->len;