Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1 | /* |
Jon Paul Maloy | 02c00c2 | 2014-06-09 11:08:18 -0500 | [diff] [blame] | 2 | * net/tipc/socket.c: TIPC socket API |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3 | * |
Jon Maloy | 60c102e | 2020-11-25 13:29:13 -0500 | [diff] [blame] | 4 | * Copyright (c) 2001-2007, 2012-2019, Ericsson AB |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 5 | * Copyright (c) 2004-2008, 2010-2013, Wind River Systems |
Jon Maloy | 998d390 | 2021-03-16 22:06:08 -0400 | [diff] [blame] | 6 | * Copyright (c) 2020-2021, Red Hat Inc |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 7 | * All rights reserved. |
| 8 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 9 | * Redistribution and use in source and binary forms, with or without |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 10 | * modification, are permitted provided that the following conditions are met: |
| 11 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. Neither the names of the copyright holders nor the names of its |
| 18 | * contributors may be used to endorse or promote products derived from |
| 19 | * this software without specific prior written permission. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 20 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 21 | * Alternatively, this software may be distributed under the terms of the |
| 22 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 23 | * Software Foundation. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 29 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 35 | * POSSIBILITY OF SUCH DAMAGE. |
| 36 | */ |
| 37 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 38 | #include <linux/rhashtable.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 39 | #include <linux/sched/signal.h> |
| 40 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 41 | #include "core.h" |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 42 | #include "name_table.h" |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 43 | #include "node.h" |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 44 | #include "link.h" |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 45 | #include "name_distr.h" |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 46 | #include "socket.h" |
Jon Paul Maloy | a6bf70f | 2015-05-14 10:46:13 -0400 | [diff] [blame] | 47 | #include "bcast.h" |
Richard Alpe | 49cc66e | 2016-03-04 17:04:42 +0100 | [diff] [blame] | 48 | #include "netlink.h" |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 49 | #include "group.h" |
Tuong Lien | b4b9771 | 2018-12-19 09:17:56 +0700 | [diff] [blame] | 50 | #include "trace.h" |
Erik Hugne | 2cf8aa1 | 2012-06-29 00:16:37 -0400 | [diff] [blame] | 51 | |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 52 | #define NAGLE_START_INIT 4 |
| 53 | #define NAGLE_START_MAX 1024 |
Tung Nguyen | 6787927 | 2018-09-28 20:23:22 +0200 | [diff] [blame] | 54 | #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */ |
Jon Maloy | 0d5fcebf | 2017-10-20 11:21:32 +0200 | [diff] [blame] | 55 | #define CONN_PROBING_INTV msecs_to_jiffies(3600000) /* [ms] => 1 h */ |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 56 | #define TIPC_MAX_PORT 0xffffffff |
| 57 | #define TIPC_MIN_PORT 1 |
Randy Dunlap | 6046219 | 2020-09-17 21:35:19 -0700 | [diff] [blame] | 58 | #define TIPC_ACK_RATE 4 /* ACK at 1/4 of rcv window size */ |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 59 | |
Parthasarathy Bhuvaragan | 0c288c8 | 2016-11-01 14:02:43 +0100 | [diff] [blame] | 60 | enum { |
| 61 | TIPC_LISTEN = TCP_LISTEN, |
Parthasarathy Bhuvaragan | 8ea642e | 2016-11-01 14:02:44 +0100 | [diff] [blame] | 62 | TIPC_ESTABLISHED = TCP_ESTABLISHED, |
Parthasarathy Bhuvaragan | 438adca | 2016-11-01 14:02:45 +0100 | [diff] [blame] | 63 | TIPC_OPEN = TCP_CLOSE, |
Parthasarathy Bhuvaragan | 9fd4b07 | 2016-11-01 14:02:46 +0100 | [diff] [blame] | 64 | TIPC_DISCONNECTING = TCP_CLOSE_WAIT, |
Parthasarathy Bhuvaragan | 99a2088 | 2016-11-01 14:02:48 +0100 | [diff] [blame] | 65 | TIPC_CONNECTING = TCP_SYN_SENT, |
Parthasarathy Bhuvaragan | 0c288c8 | 2016-11-01 14:02:43 +0100 | [diff] [blame] | 66 | }; |
| 67 | |
Jon Maloy | 31c82a2 | 2017-10-13 11:04:24 +0200 | [diff] [blame] | 68 | struct sockaddr_pair { |
| 69 | struct sockaddr_tipc sock; |
| 70 | struct sockaddr_tipc member; |
| 71 | }; |
| 72 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 73 | /** |
| 74 | * struct tipc_sock - TIPC socket structure |
| 75 | * @sk: socket - interacts with 'port' and with user via the socket API |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 76 | * @max_pkt: maximum packet size "hint" used when building messages sent by port |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 77 | * @maxnagle: maximum size of msg which can be subject to nagle |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 78 | * @portid: unique port identity in TIPC socket hash table |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 79 | * @phdr: preformatted message header used when sending messages |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 80 | * @cong_links: list of congested links |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 81 | * @publications: list of publications for port |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 82 | * @blocking_link: address of the congested link we are currently sleeping on |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 83 | * @pub_count: total # of publications port has made during its lifetime |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 84 | * @conn_timeout: the time we can wait for an unresponded setup request |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 85 | * @probe_unacked: probe has not received ack yet |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 86 | * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 87 | * @cong_link_cnt: number of congested links |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 88 | * @snt_unacked: # messages sent by socket, and not yet acked by peer |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 89 | * @snd_win: send window size |
| 90 | * @peer_caps: peer capabilities mask |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 91 | * @rcv_unacked: # messages read by user, but not yet acked back to peer |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 92 | * @rcv_win: receive window size |
Parthasarathy Bhuvaragan | aeda16b | 2016-11-01 14:02:38 +0100 | [diff] [blame] | 93 | * @peer: 'connected' peer for dgram/rdm |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 94 | * @node: hash table node |
Jon Paul Maloy | 01fd12b | 2017-01-18 13:50:53 -0500 | [diff] [blame] | 95 | * @mc_method: cookie for use between socket and broadcast layer |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 96 | * @rcu: rcu struct for tipc_sock |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 97 | * @group: TIPC communications group |
| 98 | * @oneway: message count in one direction (FIXME) |
| 99 | * @nagle_start: current nagle value |
| 100 | * @snd_backlog: send backlog count |
| 101 | * @msg_acc: messages accepted; used in managing backlog and nagle |
| 102 | * @pkt_cnt: TIPC socket packet count |
| 103 | * @expect_ack: whether this TIPC socket is expecting an ack |
| 104 | * @nodelay: setsockopt() TIPC_NODELAY setting |
| 105 | * @group_is_open: TIPC socket group is fully open (FIXME) |
Jon Maloy | 14623e0 | 2021-06-02 13:44:24 -0400 | [diff] [blame] | 106 | * @published: true if port has one or more associated names |
| 107 | * @conn_addrtype: address type used when establishing connection |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 108 | */ |
| 109 | struct tipc_sock { |
| 110 | struct sock sk; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 111 | u32 max_pkt; |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 112 | u32 maxnagle; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 113 | u32 portid; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 114 | struct tipc_msg phdr; |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 115 | struct list_head cong_links; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 116 | struct list_head publications; |
| 117 | u32 pub_count; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 118 | atomic_t dupl_rcvcnt; |
Tung Nguyen | 6787927 | 2018-09-28 20:23:22 +0200 | [diff] [blame] | 119 | u16 conn_timeout; |
Parthasarathy Bhuvaragan | 8ea642e | 2016-11-01 14:02:44 +0100 | [diff] [blame] | 120 | bool probe_unacked; |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 121 | u16 cong_link_cnt; |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 122 | u16 snt_unacked; |
| 123 | u16 snd_win; |
Jon Paul Maloy | 60020e1 | 2016-05-02 11:58:46 -0400 | [diff] [blame] | 124 | u16 peer_caps; |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 125 | u16 rcv_unacked; |
| 126 | u16 rcv_win; |
Parthasarathy Bhuvaragan | aeda16b | 2016-11-01 14:02:38 +0100 | [diff] [blame] | 127 | struct sockaddr_tipc peer; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 128 | struct rhash_head node; |
Jon Paul Maloy | 01fd12b | 2017-01-18 13:50:53 -0500 | [diff] [blame] | 129 | struct tipc_mc_method mc_method; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 130 | struct rcu_head rcu; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 131 | struct tipc_group *group; |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 132 | u32 oneway; |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 133 | u32 nagle_start; |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 134 | u16 snd_backlog; |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 135 | u16 msg_acc; |
| 136 | u16 pkt_cnt; |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 137 | bool expect_ack; |
| 138 | bool nodelay; |
Jon Maloy | 60c2530 | 2018-01-17 16:42:46 +0100 | [diff] [blame] | 139 | bool group_is_open; |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 140 | bool published; |
Jon Maloy | 14623e0 | 2021-06-02 13:44:24 -0400 | [diff] [blame] | 141 | u8 conn_addrtype; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 142 | }; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 143 | |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 144 | static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb); |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 145 | static void tipc_data_ready(struct sock *sk); |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 146 | static void tipc_write_space(struct sock *sk); |
Ying Xue | f4195d1 | 2015-11-22 15:46:05 +0800 | [diff] [blame] | 147 | static void tipc_sock_destruct(struct sock *sk); |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 148 | static int tipc_release(struct socket *sock); |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 149 | static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags, |
| 150 | bool kern); |
Kees Cook | 31b102b | 2017-10-30 14:06:45 -0700 | [diff] [blame] | 151 | static void tipc_sk_timeout(struct timer_list *t); |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 152 | static int tipc_sk_publish(struct tipc_sock *tsk, struct tipc_uaddr *ua); |
Jon Maloy | 2c98da0 | 2021-03-16 22:06:13 -0400 | [diff] [blame] | 153 | static int tipc_sk_withdraw(struct tipc_sock *tsk, struct tipc_uaddr *ua); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 154 | static int tipc_sk_leave(struct tipc_sock *tsk); |
Ying Xue | e05b31f | 2015-01-09 15:27:08 +0800 | [diff] [blame] | 155 | static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 156 | static int tipc_sk_insert(struct tipc_sock *tsk); |
| 157 | static void tipc_sk_remove(struct tipc_sock *tsk); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 158 | static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz); |
Ying Xue | 39a0295f | 2015-03-02 15:37:47 +0800 | [diff] [blame] | 159 | static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz); |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 160 | static void tipc_sk_push_backlog(struct tipc_sock *tsk, bool nagle_ack); |
Xin Long | f8dd60d | 2021-07-22 12:05:41 -0400 | [diff] [blame] | 161 | static int tipc_wait_for_connect(struct socket *sock, long *timeo_p); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 162 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 163 | static const struct proto_ops packet_ops; |
| 164 | static const struct proto_ops stream_ops; |
| 165 | static const struct proto_ops msg_ops; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 166 | static struct proto tipc_proto; |
Herbert Xu | 6cca7289 | 2015-03-20 21:57:05 +1100 | [diff] [blame] | 167 | static const struct rhashtable_params tsk_rht_params; |
| 168 | |
Jon Paul Maloy | c589863 | 2015-02-05 08:36:36 -0500 | [diff] [blame] | 169 | static u32 tsk_own_node(struct tipc_sock *tsk) |
| 170 | { |
| 171 | return msg_prevnode(&tsk->phdr); |
| 172 | } |
| 173 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 174 | static u32 tsk_peer_node(struct tipc_sock *tsk) |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 175 | { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 176 | return msg_destnode(&tsk->phdr); |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 179 | static u32 tsk_peer_port(struct tipc_sock *tsk) |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 180 | { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 181 | return msg_destport(&tsk->phdr); |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 182 | } |
| 183 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 184 | static bool tsk_unreliable(struct tipc_sock *tsk) |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 185 | { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 186 | return msg_src_droppable(&tsk->phdr) != 0; |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 187 | } |
| 188 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 189 | static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable) |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 190 | { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 191 | msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0); |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 192 | } |
| 193 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 194 | static bool tsk_unreturnable(struct tipc_sock *tsk) |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 195 | { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 196 | return msg_dest_droppable(&tsk->phdr) != 0; |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 197 | } |
| 198 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 199 | static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable) |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 200 | { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 201 | msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0); |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 202 | } |
| 203 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 204 | static int tsk_importance(struct tipc_sock *tsk) |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 205 | { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 206 | return msg_importance(&tsk->phdr); |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 207 | } |
| 208 | |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 209 | static struct tipc_sock *tipc_sk(const struct sock *sk) |
| 210 | { |
| 211 | return container_of(sk, struct tipc_sock, sk); |
| 212 | } |
| 213 | |
Christoph Hellwig | 095ae61 | 2020-05-28 07:12:36 +0200 | [diff] [blame] | 214 | int tsk_set_importance(struct sock *sk, int imp) |
| 215 | { |
| 216 | if (imp > TIPC_CRITICAL_IMPORTANCE) |
| 217 | return -EINVAL; |
| 218 | msg_set_importance(&tipc_sk(sk)->phdr, (u32)imp); |
| 219 | return 0; |
| 220 | } |
| 221 | |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 222 | static bool tsk_conn_cong(struct tipc_sock *tsk) |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 223 | { |
Jon Paul Maloy | 6998cc6 | 2016-11-24 18:47:07 -0500 | [diff] [blame] | 224 | return tsk->snt_unacked > tsk->snd_win; |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 225 | } |
| 226 | |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 227 | static u16 tsk_blocks(int len) |
| 228 | { |
| 229 | return ((len / FLOWCTL_BLK_SZ) + 1); |
| 230 | } |
| 231 | |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 232 | /* tsk_blocks(): translate a buffer size in bytes to number of |
| 233 | * advertisable blocks, taking into account the ratio truesize(len)/len |
| 234 | * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ |
| 235 | */ |
| 236 | static u16 tsk_adv_blocks(int len) |
| 237 | { |
| 238 | return len / FLOWCTL_BLK_SZ / 4; |
| 239 | } |
| 240 | |
| 241 | /* tsk_inc(): increment counter for sent or received data |
| 242 | * - If block based flow control is not supported by peer we |
| 243 | * fall back to message based ditto, incrementing the counter |
| 244 | */ |
| 245 | static u16 tsk_inc(struct tipc_sock *tsk, int msglen) |
| 246 | { |
| 247 | if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL)) |
| 248 | return ((msglen / FLOWCTL_BLK_SZ) + 1); |
| 249 | return 1; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 252 | /* tsk_set_nagle - enable/disable nagle property by manipulating maxnagle |
| 253 | */ |
| 254 | static void tsk_set_nagle(struct tipc_sock *tsk) |
| 255 | { |
| 256 | struct sock *sk = &tsk->sk; |
| 257 | |
| 258 | tsk->maxnagle = 0; |
| 259 | if (sk->sk_type != SOCK_STREAM) |
| 260 | return; |
| 261 | if (tsk->nodelay) |
| 262 | return; |
| 263 | if (!(tsk->peer_caps & TIPC_NAGLE)) |
| 264 | return; |
| 265 | /* Limit node local buffer size to avoid receive queue overflow */ |
| 266 | if (tsk->max_pkt == MAX_MSG_SIZE) |
| 267 | tsk->maxnagle = 1500; |
| 268 | else |
| 269 | tsk->maxnagle = tsk->max_pkt; |
| 270 | } |
| 271 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 272 | /** |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 273 | * tsk_advance_rx_queue - discard first buffer in socket receive queue |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 274 | * @sk: network socket |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 275 | * |
| 276 | * Caller must hold socket lock |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 277 | */ |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 278 | static void tsk_advance_rx_queue(struct sock *sk) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 279 | { |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 280 | trace_tipc_sk_advance_rx(sk, NULL, TIPC_DUMP_SK_RCVQ, " "); |
Allan Stephens | 5f6d912 | 2011-11-04 13:24:29 -0400 | [diff] [blame] | 281 | kfree_skb(__skb_dequeue(&sk->sk_receive_queue)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Jon Paul Maloy | bcd3ffd | 2015-07-22 10:11:19 -0400 | [diff] [blame] | 284 | /* tipc_sk_respond() : send response message back to sender |
| 285 | */ |
| 286 | static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err) |
| 287 | { |
| 288 | u32 selector; |
| 289 | u32 dnode; |
| 290 | u32 onode = tipc_own_addr(sock_net(sk)); |
| 291 | |
| 292 | if (!tipc_msg_reverse(onode, &skb, err)) |
| 293 | return; |
| 294 | |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 295 | trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE, "@sk_respond!"); |
Jon Paul Maloy | bcd3ffd | 2015-07-22 10:11:19 -0400 | [diff] [blame] | 296 | dnode = msg_destnode(buf_msg(skb)); |
| 297 | selector = msg_origport(buf_msg(skb)); |
| 298 | tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector); |
| 299 | } |
| 300 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 301 | /** |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 302 | * tsk_rej_rx_queue - reject all buffers in socket receive queue |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 303 | * @sk: network socket |
| 304 | * @error: response error code |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 305 | * |
| 306 | * Caller must hold socket lock |
| 307 | */ |
Tuong Lien | 49afb80 | 2020-01-08 09:18:15 +0700 | [diff] [blame] | 308 | static void tsk_rej_rx_queue(struct sock *sk, int error) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 309 | { |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 310 | struct sk_buff *skb; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 311 | |
Jon Paul Maloy | bcd3ffd | 2015-07-22 10:11:19 -0400 | [diff] [blame] | 312 | while ((skb = __skb_dequeue(&sk->sk_receive_queue))) |
Tuong Lien | 49afb80 | 2020-01-08 09:18:15 +0700 | [diff] [blame] | 313 | tipc_sk_respond(sk, skb, error); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Parthasarathy Bhuvaragan | d6fb7e9 | 2016-11-01 14:02:40 +0100 | [diff] [blame] | 316 | static bool tipc_sk_connected(struct sock *sk) |
| 317 | { |
Parthasarathy Bhuvaragan | f40acba | 2016-11-01 14:02:49 +0100 | [diff] [blame] | 318 | return sk->sk_state == TIPC_ESTABLISHED; |
Parthasarathy Bhuvaragan | d6fb7e9 | 2016-11-01 14:02:40 +0100 | [diff] [blame] | 319 | } |
| 320 | |
Parthasarathy Bhuvaragan | c752023a | 2016-11-01 14:02:42 +0100 | [diff] [blame] | 321 | /* tipc_sk_type_connectionless - check if the socket is datagram socket |
| 322 | * @sk: socket |
| 323 | * |
| 324 | * Returns true if connection less, false otherwise |
| 325 | */ |
| 326 | static bool tipc_sk_type_connectionless(struct sock *sk) |
| 327 | { |
| 328 | return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM; |
| 329 | } |
| 330 | |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 331 | /* tsk_peer_msg - verify if message was sent by connected port's peer |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 332 | * |
| 333 | * Handles cases where the node's network address has changed from |
| 334 | * the default of <0.0.0> to its configured setting. |
| 335 | */ |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 336 | static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg) |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 337 | { |
Parthasarathy Bhuvaragan | d6fb7e9 | 2016-11-01 14:02:40 +0100 | [diff] [blame] | 338 | struct sock *sk = &tsk->sk; |
Jon Maloy | 23fd3ea | 2018-03-22 20:42:49 +0100 | [diff] [blame] | 339 | u32 self = tipc_own_addr(sock_net(sk)); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 340 | u32 peer_port = tsk_peer_port(tsk); |
Jon Maloy | 23fd3ea | 2018-03-22 20:42:49 +0100 | [diff] [blame] | 341 | u32 orig_node, peer_node; |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 342 | |
Parthasarathy Bhuvaragan | d6fb7e9 | 2016-11-01 14:02:40 +0100 | [diff] [blame] | 343 | if (unlikely(!tipc_sk_connected(sk))) |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 344 | return false; |
| 345 | |
| 346 | if (unlikely(msg_origport(msg) != peer_port)) |
| 347 | return false; |
| 348 | |
| 349 | orig_node = msg_orignode(msg); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 350 | peer_node = tsk_peer_node(tsk); |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 351 | |
| 352 | if (likely(orig_node == peer_node)) |
| 353 | return true; |
| 354 | |
Jon Maloy | 23fd3ea | 2018-03-22 20:42:49 +0100 | [diff] [blame] | 355 | if (!orig_node && peer_node == self) |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 356 | return true; |
| 357 | |
Jon Maloy | 23fd3ea | 2018-03-22 20:42:49 +0100 | [diff] [blame] | 358 | if (!peer_node && orig_node == self) |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 359 | return true; |
| 360 | |
| 361 | return false; |
| 362 | } |
| 363 | |
Parthasarathy Bhuvaragan | 0c288c8 | 2016-11-01 14:02:43 +0100 | [diff] [blame] | 364 | /* tipc_set_sk_state - set the sk_state of the socket |
| 365 | * @sk: socket |
| 366 | * |
| 367 | * Caller must hold socket lock |
| 368 | * |
| 369 | * Returns 0 on success, errno otherwise |
| 370 | */ |
| 371 | static int tipc_set_sk_state(struct sock *sk, int state) |
| 372 | { |
Parthasarathy Bhuvaragan | 438adca | 2016-11-01 14:02:45 +0100 | [diff] [blame] | 373 | int oldsk_state = sk->sk_state; |
Parthasarathy Bhuvaragan | 0c288c8 | 2016-11-01 14:02:43 +0100 | [diff] [blame] | 374 | int res = -EINVAL; |
| 375 | |
| 376 | switch (state) { |
Parthasarathy Bhuvaragan | 438adca | 2016-11-01 14:02:45 +0100 | [diff] [blame] | 377 | case TIPC_OPEN: |
| 378 | res = 0; |
| 379 | break; |
Parthasarathy Bhuvaragan | 0c288c8 | 2016-11-01 14:02:43 +0100 | [diff] [blame] | 380 | case TIPC_LISTEN: |
Parthasarathy Bhuvaragan | 99a2088 | 2016-11-01 14:02:48 +0100 | [diff] [blame] | 381 | case TIPC_CONNECTING: |
Parthasarathy Bhuvaragan | 438adca | 2016-11-01 14:02:45 +0100 | [diff] [blame] | 382 | if (oldsk_state == TIPC_OPEN) |
Parthasarathy Bhuvaragan | 0c288c8 | 2016-11-01 14:02:43 +0100 | [diff] [blame] | 383 | res = 0; |
| 384 | break; |
Parthasarathy Bhuvaragan | 8ea642e | 2016-11-01 14:02:44 +0100 | [diff] [blame] | 385 | case TIPC_ESTABLISHED: |
Parthasarathy Bhuvaragan | 99a2088 | 2016-11-01 14:02:48 +0100 | [diff] [blame] | 386 | if (oldsk_state == TIPC_CONNECTING || |
Parthasarathy Bhuvaragan | 438adca | 2016-11-01 14:02:45 +0100 | [diff] [blame] | 387 | oldsk_state == TIPC_OPEN) |
Parthasarathy Bhuvaragan | 8ea642e | 2016-11-01 14:02:44 +0100 | [diff] [blame] | 388 | res = 0; |
| 389 | break; |
Parthasarathy Bhuvaragan | 9fd4b07 | 2016-11-01 14:02:46 +0100 | [diff] [blame] | 390 | case TIPC_DISCONNECTING: |
Parthasarathy Bhuvaragan | 99a2088 | 2016-11-01 14:02:48 +0100 | [diff] [blame] | 391 | if (oldsk_state == TIPC_CONNECTING || |
Parthasarathy Bhuvaragan | 9fd4b07 | 2016-11-01 14:02:46 +0100 | [diff] [blame] | 392 | oldsk_state == TIPC_ESTABLISHED) |
| 393 | res = 0; |
| 394 | break; |
Parthasarathy Bhuvaragan | 0c288c8 | 2016-11-01 14:02:43 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | if (!res) |
| 398 | sk->sk_state = state; |
| 399 | |
| 400 | return res; |
| 401 | } |
| 402 | |
Jon Paul Maloy | 8c44e1a | 2017-01-03 10:55:09 -0500 | [diff] [blame] | 403 | static int tipc_sk_sock_err(struct socket *sock, long *timeout) |
| 404 | { |
| 405 | struct sock *sk = sock->sk; |
| 406 | int err = sock_error(sk); |
| 407 | int typ = sock->type; |
| 408 | |
| 409 | if (err) |
| 410 | return err; |
| 411 | if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) { |
| 412 | if (sk->sk_state == TIPC_DISCONNECTING) |
| 413 | return -EPIPE; |
| 414 | else if (!tipc_sk_connected(sk)) |
| 415 | return -ENOTCONN; |
| 416 | } |
| 417 | if (!*timeout) |
| 418 | return -EAGAIN; |
| 419 | if (signal_pending(current)) |
| 420 | return sock_intr_errno(*timeout); |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
Jon Paul Maloy | 844cf76 | 2017-05-11 20:28:15 +0200 | [diff] [blame] | 425 | #define tipc_wait_for_cond(sock_, timeo_, condition_) \ |
| 426 | ({ \ |
Tung Nguyen | bfd07f3 | 2019-02-25 10:57:20 +0700 | [diff] [blame] | 427 | DEFINE_WAIT_FUNC(wait_, woken_wake_function); \ |
Jon Paul Maloy | 844cf76 | 2017-05-11 20:28:15 +0200 | [diff] [blame] | 428 | struct sock *sk_; \ |
| 429 | int rc_; \ |
| 430 | \ |
| 431 | while ((rc_ = !(condition_))) { \ |
Tung Nguyen | bfd07f3 | 2019-02-25 10:57:20 +0700 | [diff] [blame] | 432 | /* coupled with smp_wmb() in tipc_sk_proto_rcv() */ \ |
| 433 | smp_rmb(); \ |
Jon Paul Maloy | 844cf76 | 2017-05-11 20:28:15 +0200 | [diff] [blame] | 434 | sk_ = (sock_)->sk; \ |
| 435 | rc_ = tipc_sk_sock_err((sock_), timeo_); \ |
| 436 | if (rc_) \ |
| 437 | break; \ |
Tung Nguyen | 223b732 | 2019-02-19 11:20:47 +0700 | [diff] [blame] | 438 | add_wait_queue(sk_sleep(sk_), &wait_); \ |
Jon Paul Maloy | 844cf76 | 2017-05-11 20:28:15 +0200 | [diff] [blame] | 439 | release_sock(sk_); \ |
| 440 | *(timeo_) = wait_woken(&wait_, TASK_INTERRUPTIBLE, *(timeo_)); \ |
| 441 | sched_annotate_sleep(); \ |
| 442 | lock_sock(sk_); \ |
| 443 | remove_wait_queue(sk_sleep(sk_), &wait_); \ |
| 444 | } \ |
| 445 | rc_; \ |
Jon Paul Maloy | 8c44e1a | 2017-01-03 10:55:09 -0500 | [diff] [blame] | 446 | }) |
| 447 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 448 | /** |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 449 | * tipc_sk_create - create a TIPC socket |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 450 | * @net: network namespace (must be default network) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 451 | * @sock: pre-allocated socket structure |
| 452 | * @protocol: protocol indicator (must be 0) |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 453 | * @kern: caused by kernel or by userspace? |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 454 | * |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 455 | * This routine creates additional data structures used by the TIPC socket, |
| 456 | * initializes them, and links them together. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 457 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 458 | * Return: 0 on success, errno otherwise |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 459 | */ |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 460 | static int tipc_sk_create(struct net *net, struct socket *sock, |
| 461 | int protocol, int kern) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 462 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 463 | const struct proto_ops *ops; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 464 | struct sock *sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 465 | struct tipc_sock *tsk; |
Jon Paul Maloy | 5b8fa7c | 2014-08-22 18:09:13 -0400 | [diff] [blame] | 466 | struct tipc_msg *msg; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 467 | |
| 468 | /* Validate arguments */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 469 | if (unlikely(protocol != 0)) |
| 470 | return -EPROTONOSUPPORT; |
| 471 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 472 | switch (sock->type) { |
| 473 | case SOCK_STREAM: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 474 | ops = &stream_ops; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 475 | break; |
| 476 | case SOCK_SEQPACKET: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 477 | ops = &packet_ops; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 478 | break; |
| 479 | case SOCK_DGRAM: |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 480 | case SOCK_RDM: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 481 | ops = &msg_ops; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 482 | break; |
Allan Stephens | 4997865 | 2006-06-25 23:47:18 -0700 | [diff] [blame] | 483 | default: |
Allan Stephens | 4997865 | 2006-06-25 23:47:18 -0700 | [diff] [blame] | 484 | return -EPROTOTYPE; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 485 | } |
| 486 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 487 | /* Allocate socket's protocol area */ |
Eric W. Biederman | 11aa9c2 | 2015-05-08 21:09:13 -0500 | [diff] [blame] | 488 | sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 489 | if (sk == NULL) |
| 490 | return -ENOMEM; |
| 491 | |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 492 | tsk = tipc_sk(sk); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 493 | tsk->max_pkt = MAX_PKT_DEFAULT; |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 494 | tsk->maxnagle = 0; |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 495 | tsk->nagle_start = NAGLE_START_INIT; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 496 | INIT_LIST_HEAD(&tsk->publications); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 497 | INIT_LIST_HEAD(&tsk->cong_links); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 498 | msg = &tsk->phdr; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 499 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 500 | /* Finish initializing socket data structures */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 501 | sock->ops = ops; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 502 | sock_init_data(sock, sk); |
Parthasarathy Bhuvaragan | 438adca | 2016-11-01 14:02:45 +0100 | [diff] [blame] | 503 | tipc_set_sk_state(sk, TIPC_OPEN); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 504 | if (tipc_sk_insert(tsk)) { |
Masanari Iida | c19ca6c | 2016-02-08 20:53:12 +0900 | [diff] [blame] | 505 | pr_warn("Socket create failed; port number exhausted\n"); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 506 | return -EINVAL; |
| 507 | } |
Herbert Xu | 40f9f43 | 2017-02-11 19:26:46 +0800 | [diff] [blame] | 508 | |
| 509 | /* Ensure tsk is visible before we read own_addr. */ |
| 510 | smp_mb(); |
| 511 | |
Jon Maloy | 23fd3ea | 2018-03-22 20:42:49 +0100 | [diff] [blame] | 512 | tipc_msg_init(tipc_own_addr(net), msg, TIPC_LOW_IMPORTANCE, |
| 513 | TIPC_NAMED_MSG, NAMED_H_SIZE, 0); |
Herbert Xu | 40f9f43 | 2017-02-11 19:26:46 +0800 | [diff] [blame] | 514 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 515 | msg_set_origport(msg, tsk->portid); |
Kees Cook | 31b102b | 2017-10-30 14:06:45 -0700 | [diff] [blame] | 516 | timer_setup(&sk->sk_timer, tipc_sk_timeout, 0); |
Parthasarathy Bhuvaragan | 6f00089 | 2016-11-01 14:02:47 +0100 | [diff] [blame] | 517 | sk->sk_shutdown = 0; |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 518 | sk->sk_backlog_rcv = tipc_sk_backlog_rcv; |
Ying Xue | cc79dd1 | 2013-06-17 10:54:37 -0400 | [diff] [blame] | 519 | sk->sk_rcvbuf = sysctl_tipc_rmem[1]; |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 520 | sk->sk_data_ready = tipc_data_ready; |
| 521 | sk->sk_write_space = tipc_write_space; |
Ying Xue | f4195d1 | 2015-11-22 15:46:05 +0800 | [diff] [blame] | 522 | sk->sk_destruct = tipc_sock_destruct; |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 523 | tsk->conn_timeout = CONN_TIMEOUT_DEFAULT; |
Jon Maloy | 1b22bca | 2018-02-26 20:14:04 +0100 | [diff] [blame] | 524 | tsk->group_is_open = true; |
Jon Paul Maloy | 4f4482d | 2014-05-14 05:39:09 -0400 | [diff] [blame] | 525 | atomic_set(&tsk->dupl_rcvcnt, 0); |
Allan Stephens | 7ef43eb | 2008-05-12 15:42:28 -0700 | [diff] [blame] | 526 | |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 527 | /* Start out with safe limits until we receive an advertised window */ |
| 528 | tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN); |
| 529 | tsk->rcv_win = tsk->snd_win; |
| 530 | |
Parthasarathy Bhuvaragan | c752023a | 2016-11-01 14:02:42 +0100 | [diff] [blame] | 531 | if (tipc_sk_type_connectionless(sk)) { |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 532 | tsk_set_unreturnable(tsk, true); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 533 | if (sock->type == SOCK_DGRAM) |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 534 | tsk_set_unreliable(tsk, true); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 535 | } |
Jon Maloy | 2948a1f | 2019-07-30 20:19:10 +0200 | [diff] [blame] | 536 | __skb_queue_head_init(&tsk->mc_method.deferredq); |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 537 | trace_tipc_sk_create(sk, NULL, TIPC_DUMP_NONE, " "); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 538 | return 0; |
| 539 | } |
| 540 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 541 | static void tipc_sk_callback(struct rcu_head *head) |
| 542 | { |
| 543 | struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu); |
| 544 | |
| 545 | sock_put(&tsk->sk); |
| 546 | } |
| 547 | |
Parthasarathy Bhuvaragan | 6f00089 | 2016-11-01 14:02:47 +0100 | [diff] [blame] | 548 | /* Caller should hold socket lock for the socket. */ |
| 549 | static void __tipc_shutdown(struct socket *sock, int error) |
| 550 | { |
| 551 | struct sock *sk = sock->sk; |
| 552 | struct tipc_sock *tsk = tipc_sk(sk); |
| 553 | struct net *net = sock_net(sk); |
Tung Nguyen | 12db3c8 | 2019-11-28 10:10:07 +0700 | [diff] [blame] | 554 | long timeout = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT); |
Parthasarathy Bhuvaragan | 6f00089 | 2016-11-01 14:02:47 +0100 | [diff] [blame] | 555 | u32 dnode = tsk_peer_node(tsk); |
| 556 | struct sk_buff *skb; |
| 557 | |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 558 | /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */ |
| 559 | tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt && |
| 560 | !tsk_conn_cong(tsk))); |
| 561 | |
Tung Nguyen | d34910e | 2019-11-28 10:10:08 +0700 | [diff] [blame] | 562 | /* Push out delayed messages if in Nagle mode */ |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 563 | tipc_sk_push_backlog(tsk, false); |
Tung Nguyen | d34910e | 2019-11-28 10:10:08 +0700 | [diff] [blame] | 564 | /* Remove pending SYN */ |
| 565 | __skb_queue_purge(&sk->sk_write_queue); |
Tung Nguyen | 6787927 | 2018-09-28 20:23:22 +0200 | [diff] [blame] | 566 | |
Tuong Lien | 49afb80 | 2020-01-08 09:18:15 +0700 | [diff] [blame] | 567 | /* Remove partially received buffer if any */ |
| 568 | skb = skb_peek(&sk->sk_receive_queue); |
| 569 | if (skb && TIPC_SKB_CB(skb)->bytes_read) { |
| 570 | __skb_unlink(skb, &sk->sk_receive_queue); |
| 571 | kfree_skb(skb); |
Parthasarathy Bhuvaragan | 6f00089 | 2016-11-01 14:02:47 +0100 | [diff] [blame] | 572 | } |
Jon Paul Maloy | 693c564 | 2016-12-22 07:22:29 -0500 | [diff] [blame] | 573 | |
Tuong Lien | 49afb80 | 2020-01-08 09:18:15 +0700 | [diff] [blame] | 574 | /* Reject all unreceived messages if connectionless */ |
| 575 | if (tipc_sk_type_connectionless(sk)) { |
| 576 | tsk_rej_rx_queue(sk, error); |
Jon Paul Maloy | 693c564 | 2016-12-22 07:22:29 -0500 | [diff] [blame] | 577 | return; |
Tuong Lien | 49afb80 | 2020-01-08 09:18:15 +0700 | [diff] [blame] | 578 | } |
Jon Paul Maloy | 693c564 | 2016-12-22 07:22:29 -0500 | [diff] [blame] | 579 | |
Tuong Lien | 49afb80 | 2020-01-08 09:18:15 +0700 | [diff] [blame] | 580 | switch (sk->sk_state) { |
| 581 | case TIPC_CONNECTING: |
| 582 | case TIPC_ESTABLISHED: |
| 583 | tipc_set_sk_state(sk, TIPC_DISCONNECTING); |
| 584 | tipc_node_remove_conn(net, dnode, tsk->portid); |
| 585 | /* Send a FIN+/- to its peer */ |
| 586 | skb = __skb_dequeue(&sk->sk_receive_queue); |
| 587 | if (skb) { |
| 588 | __skb_queue_purge(&sk->sk_receive_queue); |
| 589 | tipc_sk_respond(sk, skb, error); |
| 590 | break; |
| 591 | } |
Parthasarathy Bhuvaragan | 6f00089 | 2016-11-01 14:02:47 +0100 | [diff] [blame] | 592 | skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE, |
| 593 | TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode, |
| 594 | tsk_own_node(tsk), tsk_peer_port(tsk), |
| 595 | tsk->portid, error); |
| 596 | if (skb) |
| 597 | tipc_node_xmit_skb(net, skb, dnode, tsk->portid); |
Tuong Lien | 49afb80 | 2020-01-08 09:18:15 +0700 | [diff] [blame] | 598 | break; |
| 599 | case TIPC_LISTEN: |
| 600 | /* Reject all SYN messages */ |
| 601 | tsk_rej_rx_queue(sk, error); |
| 602 | break; |
| 603 | default: |
| 604 | __skb_queue_purge(&sk->sk_receive_queue); |
| 605 | break; |
Parthasarathy Bhuvaragan | 6f00089 | 2016-11-01 14:02:47 +0100 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 609 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 610 | * tipc_release - destroy a TIPC socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 611 | * @sock: socket to destroy |
| 612 | * |
| 613 | * This routine cleans up any messages that are still queued on the socket. |
| 614 | * For DGRAM and RDM socket types, all queued messages are rejected. |
| 615 | * For SEQPACKET and STREAM socket types, the first message is rejected |
| 616 | * and any others are discarded. (If the first message on a STREAM socket |
| 617 | * is partially-read, it is discarded and the next one is rejected instead.) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 618 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 619 | * NOTE: Rejected messages are not necessarily returned to the sender! They |
| 620 | * are returned or discarded according to the "destination droppable" setting |
| 621 | * specified for the message by the sender. |
| 622 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 623 | * Return: 0 on success, errno otherwise |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 624 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 625 | static int tipc_release(struct socket *sock) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 626 | { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 627 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 628 | struct tipc_sock *tsk; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 629 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 630 | /* |
| 631 | * Exit if socket isn't fully initialized (occurs when a failed accept() |
| 632 | * releases a pre-allocated child socket that was never used) |
| 633 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 634 | if (sk == NULL) |
| 635 | return 0; |
| 636 | |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 637 | tsk = tipc_sk(sk); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 638 | lock_sock(sk); |
| 639 | |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 640 | trace_tipc_sk_release(sk, NULL, TIPC_DUMP_ALL, " "); |
Parthasarathy Bhuvaragan | 6f00089 | 2016-11-01 14:02:47 +0100 | [diff] [blame] | 641 | __tipc_shutdown(sock, TIPC_ERR_NO_PORT); |
| 642 | sk->sk_shutdown = SHUTDOWN_MASK; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 643 | tipc_sk_leave(tsk); |
Jon Maloy | 2c98da0 | 2021-03-16 22:06:13 -0400 | [diff] [blame] | 644 | tipc_sk_withdraw(tsk, NULL); |
Hoang Le | c55c8ed | 2019-03-19 18:49:50 +0700 | [diff] [blame] | 645 | __skb_queue_purge(&tsk->mc_method.deferredq); |
Ying Xue | 1ea23a2 | 2015-05-28 13:19:22 +0800 | [diff] [blame] | 646 | sk_stop_timer(sk, &sk->sk_timer); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 647 | tipc_sk_remove(tsk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 648 | |
Cong Wang | 0a3b8b2 | 2018-09-03 19:12:41 -0700 | [diff] [blame] | 649 | sock_orphan(sk); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 650 | /* Reject any messages that accumulated in backlog queue */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 651 | release_sock(sk); |
Jon Maloy | a80ae53 | 2017-10-13 11:04:22 +0200 | [diff] [blame] | 652 | tipc_dest_list_purge(&tsk->cong_links); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 653 | tsk->cong_link_cnt = 0; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 654 | call_rcu(&tsk->rcu, tipc_sk_callback); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 655 | sock->sk = NULL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 656 | |
Geert Uytterhoeven | 065d7e3 | 2014-04-06 15:56:14 +0200 | [diff] [blame] | 657 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | /** |
Jon Maloy | 60c102e | 2020-11-25 13:29:13 -0500 | [diff] [blame] | 661 | * __tipc_bind - associate or disassocate TIPC name(s) with a socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 662 | * @sock: socket structure |
Jon Maloy | 60c102e | 2020-11-25 13:29:13 -0500 | [diff] [blame] | 663 | * @skaddr: socket address describing name(s) and desired operation |
| 664 | * @alen: size of socket address data structure |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 665 | * |
gushengxian | 326af50 | 2021-06-09 23:18:53 -0700 | [diff] [blame] | 666 | * Name and name sequence binding are indicated using a positive scope value; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 667 | * a negative scope value unbinds the specified name. Specifying no name |
| 668 | * (i.e. a socket address length of 0) unbinds all names from the socket. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 669 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 670 | * Return: 0 on success, errno otherwise |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 671 | * |
| 672 | * NOTE: This routine doesn't need to take the socket lock since it doesn't |
| 673 | * access any non-constant socket information. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 674 | */ |
Jon Maloy | 60c102e | 2020-11-25 13:29:13 -0500 | [diff] [blame] | 675 | static int __tipc_bind(struct socket *sock, struct sockaddr *skaddr, int alen) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 676 | { |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 677 | struct tipc_uaddr *ua = (struct tipc_uaddr *)skaddr; |
Jon Maloy | 60c102e | 2020-11-25 13:29:13 -0500 | [diff] [blame] | 678 | struct tipc_sock *tsk = tipc_sk(sock->sk); |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 679 | bool unbind = false; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 680 | |
Jon Maloy | 60c102e | 2020-11-25 13:29:13 -0500 | [diff] [blame] | 681 | if (unlikely(!alen)) |
Jon Maloy | 2c98da0 | 2021-03-16 22:06:13 -0400 | [diff] [blame] | 682 | return tipc_sk_withdraw(tsk, NULL); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 683 | |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 684 | if (ua->addrtype == TIPC_SERVICE_ADDR) { |
| 685 | ua->addrtype = TIPC_SERVICE_RANGE; |
| 686 | ua->sr.upper = ua->sr.lower; |
| 687 | } |
| 688 | if (ua->scope < 0) { |
| 689 | unbind = true; |
| 690 | ua->scope = -ua->scope; |
| 691 | } |
| 692 | /* Users may still use deprecated TIPC_ZONE_SCOPE */ |
| 693 | if (ua->scope != TIPC_NODE_SCOPE) |
| 694 | ua->scope = TIPC_CLUSTER_SCOPE; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 695 | |
Jon Maloy | 60c102e | 2020-11-25 13:29:13 -0500 | [diff] [blame] | 696 | if (tsk->group) |
| 697 | return -EACCES; |
| 698 | |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 699 | if (unbind) |
Jon Maloy | 2c98da0 | 2021-03-16 22:06:13 -0400 | [diff] [blame] | 700 | return tipc_sk_withdraw(tsk, ua); |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 701 | return tipc_sk_publish(tsk, ua); |
Jon Maloy | 60c102e | 2020-11-25 13:29:13 -0500 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | int tipc_sk_bind(struct socket *sock, struct sockaddr *skaddr, int alen) |
| 705 | { |
| 706 | int res; |
| 707 | |
| 708 | lock_sock(sock->sk); |
| 709 | res = __tipc_bind(sock, skaddr, alen); |
| 710 | release_sock(sock->sk); |
Ying Xue | 8460276 | 2013-12-27 10:18:28 +0800 | [diff] [blame] | 711 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 712 | } |
| 713 | |
Jon Maloy | 72671b3 | 2020-10-29 21:29:38 -0400 | [diff] [blame] | 714 | static int tipc_bind(struct socket *sock, struct sockaddr *skaddr, int alen) |
| 715 | { |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 716 | struct tipc_uaddr *ua = (struct tipc_uaddr *)skaddr; |
| 717 | u32 atype = ua->addrtype; |
Jon Maloy | 72671b3 | 2020-10-29 21:29:38 -0400 | [diff] [blame] | 718 | |
| 719 | if (alen) { |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 720 | if (!tipc_uaddr_valid(ua, alen)) |
Jon Maloy | 72671b3 | 2020-10-29 21:29:38 -0400 | [diff] [blame] | 721 | return -EINVAL; |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 722 | if (atype == TIPC_SOCKET_ADDR) |
Jon Maloy | 60c102e | 2020-11-25 13:29:13 -0500 | [diff] [blame] | 723 | return -EAFNOSUPPORT; |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 724 | if (ua->sr.type < TIPC_RESERVED_TYPES) { |
Jon Maloy | 72671b3 | 2020-10-29 21:29:38 -0400 | [diff] [blame] | 725 | pr_warn_once("Can't bind to reserved service type %u\n", |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 726 | ua->sr.type); |
Jon Maloy | 72671b3 | 2020-10-29 21:29:38 -0400 | [diff] [blame] | 727 | return -EACCES; |
| 728 | } |
| 729 | } |
| 730 | return tipc_sk_bind(sock, skaddr, alen); |
| 731 | } |
| 732 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 733 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 734 | * tipc_getname - get port ID of socket or peer socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 735 | * @sock: socket structure |
| 736 | * @uaddr: area for returned socket address |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 737 | * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 738 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 739 | * Return: 0 on success, errno otherwise |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 740 | * |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 741 | * NOTE: This routine doesn't need to take the socket lock since it only |
| 742 | * accesses socket information that is unchanging (or which changes in |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 743 | * a completely predictable manner). |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 744 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 745 | static int tipc_getname(struct socket *sock, struct sockaddr *uaddr, |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 746 | int peer) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 747 | { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 748 | struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; |
Parthasarathy Bhuvaragan | 9fd4b07 | 2016-11-01 14:02:46 +0100 | [diff] [blame] | 749 | struct sock *sk = sock->sk; |
| 750 | struct tipc_sock *tsk = tipc_sk(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 751 | |
Kulikov Vasiliy | 88f8a5e | 2010-10-31 07:10:32 +0000 | [diff] [blame] | 752 | memset(addr, 0, sizeof(*addr)); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 753 | if (peer) { |
Parthasarathy Bhuvaragan | f40acba | 2016-11-01 14:02:49 +0100 | [diff] [blame] | 754 | if ((!tipc_sk_connected(sk)) && |
Parthasarathy Bhuvaragan | 9fd4b07 | 2016-11-01 14:02:46 +0100 | [diff] [blame] | 755 | ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING))) |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 756 | return -ENOTCONN; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 757 | addr->addr.id.ref = tsk_peer_port(tsk); |
| 758 | addr->addr.id.node = tsk_peer_node(tsk); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 759 | } else { |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 760 | addr->addr.id.ref = tsk->portid; |
Jon Maloy | 23fd3ea | 2018-03-22 20:42:49 +0100 | [diff] [blame] | 761 | addr->addr.id.node = tipc_own_addr(sock_net(sk)); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 762 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 763 | |
Jon Maloy | b6f88d9 | 2020-11-25 13:29:15 -0500 | [diff] [blame] | 764 | addr->addrtype = TIPC_SOCKET_ADDR; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 765 | addr->family = AF_TIPC; |
| 766 | addr->scope = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 767 | addr->addr.name.domain = 0; |
| 768 | |
Denys Vlasenko | 9b2c45d | 2018-02-12 20:00:20 +0100 | [diff] [blame] | 769 | return sizeof(*addr); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | /** |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 773 | * tipc_poll - read and possibly block on pollmask |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 774 | * @file: file structure associated with the socket |
| 775 | * @sock: socket for which to calculate the poll bits |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 776 | * @wait: ??? |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 777 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 778 | * Return: pollmask value |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 779 | * |
| 780 | * COMMENTARY: |
| 781 | * It appears that the usual socket locking mechanisms are not useful here |
| 782 | * since the pollmask info is potentially out-of-date the moment this routine |
| 783 | * exits. TCP and other protocols seem to rely on higher level poll routines |
| 784 | * to handle any preventable race conditions, so TIPC will do the same ... |
| 785 | * |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 786 | * IMPORTANT: The fact that a read or write operation is indicated does NOT |
| 787 | * imply that the operation will succeed, merely that it should be performed |
| 788 | * and will not block. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 789 | */ |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 790 | static __poll_t tipc_poll(struct file *file, struct socket *sock, |
| 791 | poll_table *wait) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 792 | { |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 793 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 794 | struct tipc_sock *tsk = tipc_sk(sk); |
Al Viro | ade994f | 2017-07-03 00:01:49 -0400 | [diff] [blame] | 795 | __poll_t revents = 0; |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 796 | |
Karsten Graul | 89ab066 | 2018-10-23 13:40:39 +0200 | [diff] [blame] | 797 | sock_poll_wait(file, sock, wait); |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 798 | trace_tipc_sk_poll(sk, NULL, TIPC_DUMP_ALL, " "); |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 799 | |
Parthasarathy Bhuvaragan | 6f00089 | 2016-11-01 14:02:47 +0100 | [diff] [blame] | 800 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 801 | revents |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM; |
Parthasarathy Bhuvaragan | 6f00089 | 2016-11-01 14:02:47 +0100 | [diff] [blame] | 802 | if (sk->sk_shutdown == SHUTDOWN_MASK) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 803 | revents |= EPOLLHUP; |
Parthasarathy Bhuvaragan | 6f00089 | 2016-11-01 14:02:47 +0100 | [diff] [blame] | 804 | |
Parthasarathy Bhuvaragan | f40acba | 2016-11-01 14:02:49 +0100 | [diff] [blame] | 805 | switch (sk->sk_state) { |
| 806 | case TIPC_ESTABLISHED: |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 807 | if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 808 | revents |= EPOLLOUT; |
Miaohe Lin | 7f8901b | 2020-08-18 08:07:13 -0400 | [diff] [blame] | 809 | fallthrough; |
Parthasarathy Bhuvaragan | f40acba | 2016-11-01 14:02:49 +0100 | [diff] [blame] | 810 | case TIPC_LISTEN: |
Parthasarathy Bhuvaragan | ff94683 | 2019-05-09 07:13:42 +0200 | [diff] [blame] | 811 | case TIPC_CONNECTING: |
Eric Dumazet | 3ef7cf5 | 2019-10-23 22:44:50 -0700 | [diff] [blame] | 812 | if (!skb_queue_empty_lockless(&sk->sk_receive_queue)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 813 | revents |= EPOLLIN | EPOLLRDNORM; |
Parthasarathy Bhuvaragan | f40acba | 2016-11-01 14:02:49 +0100 | [diff] [blame] | 814 | break; |
| 815 | case TIPC_OPEN: |
Jon Maloy | 60c2530 | 2018-01-17 16:42:46 +0100 | [diff] [blame] | 816 | if (tsk->group_is_open && !tsk->cong_link_cnt) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 817 | revents |= EPOLLOUT; |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 818 | if (!tipc_sk_type_connectionless(sk)) |
| 819 | break; |
Eric Dumazet | 3ef7cf5 | 2019-10-23 22:44:50 -0700 | [diff] [blame] | 820 | if (skb_queue_empty_lockless(&sk->sk_receive_queue)) |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 821 | break; |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 822 | revents |= EPOLLIN | EPOLLRDNORM; |
Parthasarathy Bhuvaragan | f40acba | 2016-11-01 14:02:49 +0100 | [diff] [blame] | 823 | break; |
| 824 | case TIPC_DISCONNECTING: |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 825 | revents = EPOLLIN | EPOLLRDNORM | EPOLLHUP; |
Parthasarathy Bhuvaragan | f40acba | 2016-11-01 14:02:49 +0100 | [diff] [blame] | 826 | break; |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 827 | } |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 828 | return revents; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 829 | } |
| 830 | |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 831 | /** |
| 832 | * tipc_sendmcast - send multicast message |
| 833 | * @sock: socket structure |
Jon Maloy | 833f867 | 2021-03-16 22:06:17 -0400 | [diff] [blame] | 834 | * @ua: destination address struct |
Al Viro | 562640f | 2014-11-15 01:13:43 -0500 | [diff] [blame] | 835 | * @msg: message to send |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 836 | * @dlen: length of data to send |
| 837 | * @timeout: timeout to wait for wakeup |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 838 | * |
| 839 | * Called from function tipc_sendmsg(), which has done all sanity checks |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 840 | * Return: the number of bytes sent on success, or errno |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 841 | */ |
Jon Maloy | 833f867 | 2021-03-16 22:06:17 -0400 | [diff] [blame] | 842 | static int tipc_sendmcast(struct socket *sock, struct tipc_uaddr *ua, |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 843 | struct msghdr *msg, size_t dlen, long timeout) |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 844 | { |
| 845 | struct sock *sk = sock->sk; |
Jon Paul Maloy | c589863 | 2015-02-05 08:36:36 -0500 | [diff] [blame] | 846 | struct tipc_sock *tsk = tipc_sk(sk); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 847 | struct tipc_msg *hdr = &tsk->phdr; |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 848 | struct net *net = sock_net(sk); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 849 | int mtu = tipc_bcast_get_mtu(net); |
| 850 | struct sk_buff_head pkts; |
Jon Paul Maloy | a853e4c | 2017-01-18 13:50:52 -0500 | [diff] [blame] | 851 | struct tipc_nlist dsts; |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 852 | int rc; |
| 853 | |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 854 | if (tsk->group) |
| 855 | return -EACCES; |
| 856 | |
Jon Paul Maloy | a853e4c | 2017-01-18 13:50:52 -0500 | [diff] [blame] | 857 | /* Block or return if any destination link is congested */ |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 858 | rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt); |
| 859 | if (unlikely(rc)) |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 860 | return rc; |
| 861 | |
Jon Paul Maloy | a853e4c | 2017-01-18 13:50:52 -0500 | [diff] [blame] | 862 | /* Lookup destination nodes */ |
| 863 | tipc_nlist_init(&dsts, tipc_own_addr(net)); |
Jon Maloy | 833f867 | 2021-03-16 22:06:17 -0400 | [diff] [blame] | 864 | tipc_nametbl_lookup_mcast_nodes(net, ua, &dsts); |
Jon Paul Maloy | a853e4c | 2017-01-18 13:50:52 -0500 | [diff] [blame] | 865 | if (!dsts.local && !dsts.remote) |
| 866 | return -EHOSTUNREACH; |
| 867 | |
| 868 | /* Build message header */ |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 869 | msg_set_type(hdr, TIPC_MCAST_MSG); |
Jon Paul Maloy | a853e4c | 2017-01-18 13:50:52 -0500 | [diff] [blame] | 870 | msg_set_hdr_sz(hdr, MCAST_H_SIZE); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 871 | msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE); |
| 872 | msg_set_destport(hdr, 0); |
| 873 | msg_set_destnode(hdr, 0); |
Jon Maloy | 833f867 | 2021-03-16 22:06:17 -0400 | [diff] [blame] | 874 | msg_set_nametype(hdr, ua->sr.type); |
| 875 | msg_set_namelower(hdr, ua->sr.lower); |
| 876 | msg_set_nameupper(hdr, ua->sr.upper); |
Jon Paul Maloy | 22d85c7 | 2015-07-16 16:54:23 -0400 | [diff] [blame] | 877 | |
Jon Paul Maloy | a853e4c | 2017-01-18 13:50:52 -0500 | [diff] [blame] | 878 | /* Build message as chain of buffers */ |
Jon Maloy | e654f9f | 2019-08-15 16:42:50 +0200 | [diff] [blame] | 879 | __skb_queue_head_init(&pkts); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 880 | rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 881 | |
Jon Paul Maloy | a853e4c | 2017-01-18 13:50:52 -0500 | [diff] [blame] | 882 | /* Send message if build was successful */ |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 883 | if (unlikely(rc == dlen)) { |
| 884 | trace_tipc_sk_sendmcast(sk, skb_peek(&pkts), |
| 885 | TIPC_DUMP_SK_SNDQ, " "); |
Jon Maloy | 833f867 | 2021-03-16 22:06:17 -0400 | [diff] [blame] | 886 | rc = tipc_mcast_xmit(net, &pkts, &tsk->mc_method, &dsts, |
Jon Paul Maloy | a853e4c | 2017-01-18 13:50:52 -0500 | [diff] [blame] | 887 | &tsk->cong_link_cnt); |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 888 | } |
Jon Paul Maloy | a853e4c | 2017-01-18 13:50:52 -0500 | [diff] [blame] | 889 | |
| 890 | tipc_nlist_purge(&dsts); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 891 | |
| 892 | return rc ? rc : dlen; |
Jon Paul Maloy | 0abd8ff | 2014-07-16 20:41:01 -0400 | [diff] [blame] | 893 | } |
| 894 | |
Jon Paul Maloy | cb1b728 | 2015-02-05 08:36:44 -0500 | [diff] [blame] | 895 | /** |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 896 | * tipc_send_group_msg - send a message to a member in the group |
| 897 | * @net: network namespace |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 898 | * @tsk: tipc socket |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 899 | * @m: message to send |
| 900 | * @mb: group member |
| 901 | * @dnode: destination node |
| 902 | * @dport: destination port |
| 903 | * @dlen: total length of message data |
| 904 | */ |
| 905 | static int tipc_send_group_msg(struct net *net, struct tipc_sock *tsk, |
| 906 | struct msghdr *m, struct tipc_member *mb, |
| 907 | u32 dnode, u32 dport, int dlen) |
| 908 | { |
Jon Maloy | b87a5ea | 2017-10-13 11:04:30 +0200 | [diff] [blame] | 909 | u16 bc_snd_nxt = tipc_group_bc_snd_nxt(tsk->group); |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 910 | struct tipc_mc_method *method = &tsk->mc_method; |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 911 | int blks = tsk_blocks(GROUP_H_SIZE + dlen); |
| 912 | struct tipc_msg *hdr = &tsk->phdr; |
| 913 | struct sk_buff_head pkts; |
| 914 | int mtu, rc; |
| 915 | |
| 916 | /* Complete message header */ |
| 917 | msg_set_type(hdr, TIPC_GRP_UCAST_MSG); |
| 918 | msg_set_hdr_sz(hdr, GROUP_H_SIZE); |
| 919 | msg_set_destport(hdr, dport); |
| 920 | msg_set_destnode(hdr, dnode); |
Jon Maloy | b87a5ea | 2017-10-13 11:04:30 +0200 | [diff] [blame] | 921 | msg_set_grp_bc_seqno(hdr, bc_snd_nxt); |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 922 | |
| 923 | /* Build message as chain of buffers */ |
Jon Maloy | e654f9f | 2019-08-15 16:42:50 +0200 | [diff] [blame] | 924 | __skb_queue_head_init(&pkts); |
Hoang Le | f73b128 | 2019-10-29 07:51:21 +0700 | [diff] [blame] | 925 | mtu = tipc_node_get_mtu(net, dnode, tsk->portid, false); |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 926 | rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts); |
| 927 | if (unlikely(rc != dlen)) |
| 928 | return rc; |
| 929 | |
| 930 | /* Send message */ |
| 931 | rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid); |
| 932 | if (unlikely(rc == -ELINKCONG)) { |
| 933 | tipc_dest_push(&tsk->cong_links, dnode, 0); |
| 934 | tsk->cong_link_cnt++; |
| 935 | } |
| 936 | |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 937 | /* Update send window */ |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 938 | tipc_group_update_member(mb, blks); |
| 939 | |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 940 | /* A broadcast sent within next EXPIRE period must follow same path */ |
| 941 | method->rcast = true; |
| 942 | method->mandatory = true; |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 943 | return dlen; |
| 944 | } |
| 945 | |
| 946 | /** |
| 947 | * tipc_send_group_unicast - send message to a member in the group |
| 948 | * @sock: socket structure |
| 949 | * @m: message to send |
| 950 | * @dlen: total length of message data |
| 951 | * @timeout: timeout to wait for wakeup |
| 952 | * |
| 953 | * Called from function tipc_sendmsg(), which has done all sanity checks |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 954 | * Return: the number of bytes sent on success, or errno |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 955 | */ |
| 956 | static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m, |
| 957 | int dlen, long timeout) |
| 958 | { |
| 959 | struct sock *sk = sock->sk; |
Jon Maloy | 006ed14 | 2021-03-16 22:06:18 -0400 | [diff] [blame] | 960 | struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name; |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 961 | int blks = tsk_blocks(GROUP_H_SIZE + dlen); |
| 962 | struct tipc_sock *tsk = tipc_sk(sk); |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 963 | struct net *net = sock_net(sk); |
| 964 | struct tipc_member *mb = NULL; |
| 965 | u32 node, port; |
| 966 | int rc; |
| 967 | |
Jon Maloy | 006ed14 | 2021-03-16 22:06:18 -0400 | [diff] [blame] | 968 | node = ua->sk.node; |
| 969 | port = ua->sk.ref; |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 970 | if (!port && !node) |
| 971 | return -EHOSTUNREACH; |
| 972 | |
| 973 | /* Block or return if destination link or member is congested */ |
| 974 | rc = tipc_wait_for_cond(sock, &timeout, |
| 975 | !tipc_dest_find(&tsk->cong_links, node, 0) && |
Cong Wang | 143ece6 | 2018-12-11 21:43:51 -0800 | [diff] [blame] | 976 | tsk->group && |
| 977 | !tipc_group_cong(tsk->group, node, port, blks, |
| 978 | &mb)); |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 979 | if (unlikely(rc)) |
| 980 | return rc; |
| 981 | |
| 982 | if (unlikely(!mb)) |
| 983 | return -EHOSTUNREACH; |
| 984 | |
| 985 | rc = tipc_send_group_msg(net, tsk, m, mb, node, port, dlen); |
| 986 | |
| 987 | return rc ? rc : dlen; |
| 988 | } |
| 989 | |
| 990 | /** |
Jon Maloy | ee106d7 | 2017-10-13 11:04:28 +0200 | [diff] [blame] | 991 | * tipc_send_group_anycast - send message to any member with given identity |
| 992 | * @sock: socket structure |
| 993 | * @m: message to send |
| 994 | * @dlen: total length of message data |
| 995 | * @timeout: timeout to wait for wakeup |
| 996 | * |
| 997 | * Called from function tipc_sendmsg(), which has done all sanity checks |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 998 | * Return: the number of bytes sent on success, or errno |
Jon Maloy | ee106d7 | 2017-10-13 11:04:28 +0200 | [diff] [blame] | 999 | */ |
| 1000 | static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m, |
| 1001 | int dlen, long timeout) |
| 1002 | { |
Jon Maloy | 006ed14 | 2021-03-16 22:06:18 -0400 | [diff] [blame] | 1003 | struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name; |
Jon Maloy | ee106d7 | 2017-10-13 11:04:28 +0200 | [diff] [blame] | 1004 | struct sock *sk = sock->sk; |
| 1005 | struct tipc_sock *tsk = tipc_sk(sk); |
| 1006 | struct list_head *cong_links = &tsk->cong_links; |
| 1007 | int blks = tsk_blocks(GROUP_H_SIZE + dlen); |
Jon Maloy | 232d07b | 2018-01-08 21:03:30 +0100 | [diff] [blame] | 1008 | struct tipc_msg *hdr = &tsk->phdr; |
Jon Maloy | ee106d7 | 2017-10-13 11:04:28 +0200 | [diff] [blame] | 1009 | struct tipc_member *first = NULL; |
| 1010 | struct tipc_member *mbr = NULL; |
| 1011 | struct net *net = sock_net(sk); |
| 1012 | u32 node, port, exclude; |
Jon Maloy | ee106d7 | 2017-10-13 11:04:28 +0200 | [diff] [blame] | 1013 | struct list_head dsts; |
| 1014 | int lookups = 0; |
| 1015 | int dstcnt, rc; |
| 1016 | bool cong; |
| 1017 | |
| 1018 | INIT_LIST_HEAD(&dsts); |
Jon Maloy | 006ed14 | 2021-03-16 22:06:18 -0400 | [diff] [blame] | 1019 | ua->sa.type = msg_nametype(hdr); |
| 1020 | ua->scope = msg_lookup_scope(hdr); |
Jon Maloy | ee106d7 | 2017-10-13 11:04:28 +0200 | [diff] [blame] | 1021 | |
| 1022 | while (++lookups < 4) { |
Cong Wang | 143ece6 | 2018-12-11 21:43:51 -0800 | [diff] [blame] | 1023 | exclude = tipc_group_exclude(tsk->group); |
| 1024 | |
Jon Maloy | ee106d7 | 2017-10-13 11:04:28 +0200 | [diff] [blame] | 1025 | first = NULL; |
| 1026 | |
| 1027 | /* Look for a non-congested destination member, if any */ |
| 1028 | while (1) { |
Jon Maloy | 006ed14 | 2021-03-16 22:06:18 -0400 | [diff] [blame] | 1029 | if (!tipc_nametbl_lookup_group(net, ua, &dsts, &dstcnt, |
| 1030 | exclude, false)) |
Jon Maloy | ee106d7 | 2017-10-13 11:04:28 +0200 | [diff] [blame] | 1031 | return -EHOSTUNREACH; |
| 1032 | tipc_dest_pop(&dsts, &node, &port); |
Cong Wang | 143ece6 | 2018-12-11 21:43:51 -0800 | [diff] [blame] | 1033 | cong = tipc_group_cong(tsk->group, node, port, blks, |
| 1034 | &mbr); |
Jon Maloy | ee106d7 | 2017-10-13 11:04:28 +0200 | [diff] [blame] | 1035 | if (!cong) |
| 1036 | break; |
| 1037 | if (mbr == first) |
| 1038 | break; |
| 1039 | if (!first) |
| 1040 | first = mbr; |
| 1041 | } |
| 1042 | |
| 1043 | /* Start over if destination was not in member list */ |
| 1044 | if (unlikely(!mbr)) |
| 1045 | continue; |
| 1046 | |
| 1047 | if (likely(!cong && !tipc_dest_find(cong_links, node, 0))) |
| 1048 | break; |
| 1049 | |
| 1050 | /* Block or return if destination link or member is congested */ |
| 1051 | rc = tipc_wait_for_cond(sock, &timeout, |
| 1052 | !tipc_dest_find(cong_links, node, 0) && |
Cong Wang | 143ece6 | 2018-12-11 21:43:51 -0800 | [diff] [blame] | 1053 | tsk->group && |
| 1054 | !tipc_group_cong(tsk->group, node, port, |
Jon Maloy | ee106d7 | 2017-10-13 11:04:28 +0200 | [diff] [blame] | 1055 | blks, &mbr)); |
| 1056 | if (unlikely(rc)) |
| 1057 | return rc; |
| 1058 | |
| 1059 | /* Send, unless destination disappeared while waiting */ |
| 1060 | if (likely(mbr)) |
| 1061 | break; |
| 1062 | } |
| 1063 | |
| 1064 | if (unlikely(lookups >= 4)) |
| 1065 | return -EHOSTUNREACH; |
| 1066 | |
| 1067 | rc = tipc_send_group_msg(net, tsk, m, mbr, node, port, dlen); |
| 1068 | |
| 1069 | return rc ? rc : dlen; |
| 1070 | } |
| 1071 | |
| 1072 | /** |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1073 | * tipc_send_group_bcast - send message to all members in communication group |
Andrew Lunn | d814120 | 2020-07-13 01:15:14 +0200 | [diff] [blame] | 1074 | * @sock: socket structure |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1075 | * @m: message to send |
| 1076 | * @dlen: total length of message data |
| 1077 | * @timeout: timeout to wait for wakeup |
| 1078 | * |
| 1079 | * Called from function tipc_sendmsg(), which has done all sanity checks |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 1080 | * Return: the number of bytes sent on success, or errno |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1081 | */ |
| 1082 | static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m, |
| 1083 | int dlen, long timeout) |
| 1084 | { |
Jon Maloy | 006ed14 | 2021-03-16 22:06:18 -0400 | [diff] [blame] | 1085 | struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1086 | struct sock *sk = sock->sk; |
| 1087 | struct net *net = sock_net(sk); |
| 1088 | struct tipc_sock *tsk = tipc_sk(sk); |
Cong Wang | 3c6306d | 2018-12-16 23:25:12 -0800 | [diff] [blame] | 1089 | struct tipc_nlist *dsts; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1090 | struct tipc_mc_method *method = &tsk->mc_method; |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 1091 | bool ack = method->mandatory && method->rcast; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 1092 | int blks = tsk_blocks(MCAST_H_SIZE + dlen); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1093 | struct tipc_msg *hdr = &tsk->phdr; |
| 1094 | int mtu = tipc_bcast_get_mtu(net); |
| 1095 | struct sk_buff_head pkts; |
| 1096 | int rc = -EHOSTUNREACH; |
| 1097 | |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 1098 | /* Block or return if any destination link or member is congested */ |
Cong Wang | 143ece6 | 2018-12-11 21:43:51 -0800 | [diff] [blame] | 1099 | rc = tipc_wait_for_cond(sock, &timeout, |
| 1100 | !tsk->cong_link_cnt && tsk->group && |
| 1101 | !tipc_group_bc_cong(tsk->group, blks)); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1102 | if (unlikely(rc)) |
| 1103 | return rc; |
| 1104 | |
Cong Wang | 3c6306d | 2018-12-16 23:25:12 -0800 | [diff] [blame] | 1105 | dsts = tipc_group_dests(tsk->group); |
| 1106 | if (!dsts->local && !dsts->remote) |
| 1107 | return -EHOSTUNREACH; |
| 1108 | |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1109 | /* Complete message header */ |
Jon Maloy | 006ed14 | 2021-03-16 22:06:18 -0400 | [diff] [blame] | 1110 | if (ua) { |
Jon Maloy | 5b8dddb | 2017-10-13 11:04:29 +0200 | [diff] [blame] | 1111 | msg_set_type(hdr, TIPC_GRP_MCAST_MSG); |
Jon Maloy | 006ed14 | 2021-03-16 22:06:18 -0400 | [diff] [blame] | 1112 | msg_set_nameinst(hdr, ua->sa.instance); |
Jon Maloy | 5b8dddb | 2017-10-13 11:04:29 +0200 | [diff] [blame] | 1113 | } else { |
| 1114 | msg_set_type(hdr, TIPC_GRP_BCAST_MSG); |
| 1115 | msg_set_nameinst(hdr, 0); |
| 1116 | } |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 1117 | msg_set_hdr_sz(hdr, GROUP_H_SIZE); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1118 | msg_set_destport(hdr, 0); |
| 1119 | msg_set_destnode(hdr, 0); |
Cong Wang | 143ece6 | 2018-12-11 21:43:51 -0800 | [diff] [blame] | 1120 | msg_set_grp_bc_seqno(hdr, tipc_group_bc_snd_nxt(tsk->group)); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1121 | |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 1122 | /* Avoid getting stuck with repeated forced replicasts */ |
| 1123 | msg_set_grp_bc_ack_req(hdr, ack); |
| 1124 | |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1125 | /* Build message as chain of buffers */ |
Jon Maloy | e654f9f | 2019-08-15 16:42:50 +0200 | [diff] [blame] | 1126 | __skb_queue_head_init(&pkts); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1127 | rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts); |
| 1128 | if (unlikely(rc != dlen)) |
| 1129 | return rc; |
| 1130 | |
| 1131 | /* Send message */ |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 1132 | rc = tipc_mcast_xmit(net, &pkts, method, dsts, &tsk->cong_link_cnt); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1133 | if (unlikely(rc)) |
| 1134 | return rc; |
| 1135 | |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 1136 | /* Update broadcast sequence number and send windows */ |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 1137 | tipc_group_update_bc_members(tsk->group, blks, ack); |
| 1138 | |
| 1139 | /* Broadcast link is now free to choose method for next broadcast */ |
| 1140 | method->mandatory = false; |
| 1141 | method->expires = jiffies; |
| 1142 | |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1143 | return dlen; |
| 1144 | } |
| 1145 | |
| 1146 | /** |
Jon Maloy | 5b8dddb | 2017-10-13 11:04:29 +0200 | [diff] [blame] | 1147 | * tipc_send_group_mcast - send message to all members with given identity |
| 1148 | * @sock: socket structure |
| 1149 | * @m: message to send |
| 1150 | * @dlen: total length of message data |
| 1151 | * @timeout: timeout to wait for wakeup |
| 1152 | * |
| 1153 | * Called from function tipc_sendmsg(), which has done all sanity checks |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 1154 | * Return: the number of bytes sent on success, or errno |
Jon Maloy | 5b8dddb | 2017-10-13 11:04:29 +0200 | [diff] [blame] | 1155 | */ |
| 1156 | static int tipc_send_group_mcast(struct socket *sock, struct msghdr *m, |
| 1157 | int dlen, long timeout) |
| 1158 | { |
Jon Maloy | 006ed14 | 2021-03-16 22:06:18 -0400 | [diff] [blame] | 1159 | struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name; |
Jon Maloy | 5b8dddb | 2017-10-13 11:04:29 +0200 | [diff] [blame] | 1160 | struct sock *sk = sock->sk; |
Jon Maloy | 5b8dddb | 2017-10-13 11:04:29 +0200 | [diff] [blame] | 1161 | struct tipc_sock *tsk = tipc_sk(sk); |
| 1162 | struct tipc_group *grp = tsk->group; |
Jon Maloy | 232d07b | 2018-01-08 21:03:30 +0100 | [diff] [blame] | 1163 | struct tipc_msg *hdr = &tsk->phdr; |
Jon Maloy | 5b8dddb | 2017-10-13 11:04:29 +0200 | [diff] [blame] | 1164 | struct net *net = sock_net(sk); |
Jon Maloy | 5b8dddb | 2017-10-13 11:04:29 +0200 | [diff] [blame] | 1165 | struct list_head dsts; |
Jon Maloy | 006ed14 | 2021-03-16 22:06:18 -0400 | [diff] [blame] | 1166 | u32 dstcnt, exclude; |
Jon Maloy | 5b8dddb | 2017-10-13 11:04:29 +0200 | [diff] [blame] | 1167 | |
| 1168 | INIT_LIST_HEAD(&dsts); |
Jon Maloy | 006ed14 | 2021-03-16 22:06:18 -0400 | [diff] [blame] | 1169 | ua->sa.type = msg_nametype(hdr); |
| 1170 | ua->scope = msg_lookup_scope(hdr); |
Jon Maloy | 5b8dddb | 2017-10-13 11:04:29 +0200 | [diff] [blame] | 1171 | exclude = tipc_group_exclude(grp); |
Jon Maloy | 232d07b | 2018-01-08 21:03:30 +0100 | [diff] [blame] | 1172 | |
Jon Maloy | 006ed14 | 2021-03-16 22:06:18 -0400 | [diff] [blame] | 1173 | if (!tipc_nametbl_lookup_group(net, ua, &dsts, &dstcnt, exclude, true)) |
Jon Maloy | 5b8dddb | 2017-10-13 11:04:29 +0200 | [diff] [blame] | 1174 | return -EHOSTUNREACH; |
| 1175 | |
| 1176 | if (dstcnt == 1) { |
Jon Maloy | 006ed14 | 2021-03-16 22:06:18 -0400 | [diff] [blame] | 1177 | tipc_dest_pop(&dsts, &ua->sk.node, &ua->sk.ref); |
Jon Maloy | 5b8dddb | 2017-10-13 11:04:29 +0200 | [diff] [blame] | 1178 | return tipc_send_group_unicast(sock, m, dlen, timeout); |
| 1179 | } |
| 1180 | |
| 1181 | tipc_dest_list_purge(&dsts); |
| 1182 | return tipc_send_group_bcast(sock, m, dlen, timeout); |
| 1183 | } |
| 1184 | |
| 1185 | /** |
Jon Paul Maloy | cb1b728 | 2015-02-05 08:36:44 -0500 | [diff] [blame] | 1186 | * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 1187 | * @net: the associated network namespace |
Jon Paul Maloy | cb1b728 | 2015-02-05 08:36:44 -0500 | [diff] [blame] | 1188 | * @arrvq: queue with arriving messages, to be cloned after destination lookup |
| 1189 | * @inputq: queue with cloned messages, delivered to socket after dest lookup |
| 1190 | * |
| 1191 | * Multi-threaded: parallel calls with reference to same queues may occur |
Jon Paul Maloy | 078bec8 | 2014-07-16 20:41:00 -0400 | [diff] [blame] | 1192 | */ |
Jon Paul Maloy | cb1b728 | 2015-02-05 08:36:44 -0500 | [diff] [blame] | 1193 | void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq, |
| 1194 | struct sk_buff_head *inputq) |
Jon Paul Maloy | 078bec8 | 2014-07-16 20:41:00 -0400 | [diff] [blame] | 1195 | { |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1196 | u32 self = tipc_own_addr(net); |
Jon Paul Maloy | cb1b728 | 2015-02-05 08:36:44 -0500 | [diff] [blame] | 1197 | struct sk_buff *skb, *_skb; |
Colin Ian King | b053fcc | 2018-07-31 17:01:37 +0100 | [diff] [blame] | 1198 | u32 portid, onode; |
Jon Maloy | 232d07b | 2018-01-08 21:03:30 +0100 | [diff] [blame] | 1199 | struct sk_buff_head tmpq; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1200 | struct list_head dports; |
Jon Maloy | 232d07b | 2018-01-08 21:03:30 +0100 | [diff] [blame] | 1201 | struct tipc_msg *hdr; |
Jon Maloy | 45ceea2 | 2021-03-16 22:06:16 -0400 | [diff] [blame] | 1202 | struct tipc_uaddr ua; |
Jon Maloy | 232d07b | 2018-01-08 21:03:30 +0100 | [diff] [blame] | 1203 | int user, mtyp, hlen; |
Jon Paul Maloy | 3c724ac | 2015-02-05 08:36:43 -0500 | [diff] [blame] | 1204 | |
Jon Paul Maloy | cb1b728 | 2015-02-05 08:36:44 -0500 | [diff] [blame] | 1205 | __skb_queue_head_init(&tmpq); |
Jon Paul Maloy | 4d8642d | 2017-01-03 10:55:10 -0500 | [diff] [blame] | 1206 | INIT_LIST_HEAD(&dports); |
Jon Maloy | 45ceea2 | 2021-03-16 22:06:16 -0400 | [diff] [blame] | 1207 | ua.addrtype = TIPC_SERVICE_RANGE; |
Jon Paul Maloy | 078bec8 | 2014-07-16 20:41:00 -0400 | [diff] [blame] | 1208 | |
Jon Maloy | 5ef2132 | 2021-06-02 13:44:26 -0400 | [diff] [blame] | 1209 | /* tipc_skb_peek() increments the head skb's reference counter */ |
Jon Paul Maloy | cb1b728 | 2015-02-05 08:36:44 -0500 | [diff] [blame] | 1210 | skb = tipc_skb_peek(arrvq, &inputq->lock); |
| 1211 | for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) { |
Jon Maloy | 232d07b | 2018-01-08 21:03:30 +0100 | [diff] [blame] | 1212 | hdr = buf_msg(skb); |
| 1213 | user = msg_user(hdr); |
| 1214 | mtyp = msg_type(hdr); |
| 1215 | hlen = skb_headroom(skb) + msg_hdr_sz(hdr); |
Jon Maloy | 232d07b | 2018-01-08 21:03:30 +0100 | [diff] [blame] | 1216 | onode = msg_orignode(hdr); |
Jon Maloy | 45ceea2 | 2021-03-16 22:06:16 -0400 | [diff] [blame] | 1217 | ua.sr.type = msg_nametype(hdr); |
Jon Maloy | 5ef2132 | 2021-06-02 13:44:26 -0400 | [diff] [blame] | 1218 | ua.sr.lower = msg_namelower(hdr); |
| 1219 | ua.sr.upper = msg_nameupper(hdr); |
| 1220 | if (onode == self) |
| 1221 | ua.scope = TIPC_ANY_SCOPE; |
| 1222 | else |
| 1223 | ua.scope = TIPC_CLUSTER_SCOPE; |
Jon Maloy | 232d07b | 2018-01-08 21:03:30 +0100 | [diff] [blame] | 1224 | |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 1225 | if (mtyp == TIPC_GRP_UCAST_MSG || user == GROUP_PROTOCOL) { |
| 1226 | spin_lock_bh(&inputq->lock); |
| 1227 | if (skb_peek(arrvq) == skb) { |
| 1228 | __skb_dequeue(arrvq); |
| 1229 | __skb_queue_tail(inputq, skb); |
| 1230 | } |
Jon Maloy | c545a94 | 2017-12-11 19:11:55 +0100 | [diff] [blame] | 1231 | kfree_skb(skb); |
Jon Maloy | 2f48771 | 2017-10-13 11:04:31 +0200 | [diff] [blame] | 1232 | spin_unlock_bh(&inputq->lock); |
| 1233 | continue; |
| 1234 | } |
Jon Paul Maloy | cb1b728 | 2015-02-05 08:36:44 -0500 | [diff] [blame] | 1235 | |
Jon Maloy | 232d07b | 2018-01-08 21:03:30 +0100 | [diff] [blame] | 1236 | /* Group messages require exact scope match */ |
| 1237 | if (msg_in_group(hdr)) { |
Jon Maloy | 45ceea2 | 2021-03-16 22:06:16 -0400 | [diff] [blame] | 1238 | ua.sr.lower = 0; |
| 1239 | ua.sr.upper = ~0; |
| 1240 | ua.scope = msg_lookup_scope(hdr); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1241 | } |
Jon Maloy | 232d07b | 2018-01-08 21:03:30 +0100 | [diff] [blame] | 1242 | |
| 1243 | /* Create destination port list: */ |
Jon Maloy | 5ef2132 | 2021-06-02 13:44:26 -0400 | [diff] [blame] | 1244 | tipc_nametbl_lookup_mcast_sockets(net, &ua, &dports); |
Jon Maloy | 232d07b | 2018-01-08 21:03:30 +0100 | [diff] [blame] | 1245 | |
| 1246 | /* Clone message per destination */ |
Jon Maloy | a80ae53 | 2017-10-13 11:04:22 +0200 | [diff] [blame] | 1247 | while (tipc_dest_pop(&dports, NULL, &portid)) { |
Jon Maloy | 232d07b | 2018-01-08 21:03:30 +0100 | [diff] [blame] | 1248 | _skb = __pskb_copy(skb, hlen, GFP_ATOMIC); |
Jon Paul Maloy | cb1b728 | 2015-02-05 08:36:44 -0500 | [diff] [blame] | 1249 | if (_skb) { |
| 1250 | msg_set_destport(buf_msg(_skb), portid); |
| 1251 | __skb_queue_tail(&tmpq, _skb); |
| 1252 | continue; |
| 1253 | } |
| 1254 | pr_warn("Failed to clone mcast rcv buffer\n"); |
Jon Paul Maloy | 078bec8 | 2014-07-16 20:41:00 -0400 | [diff] [blame] | 1255 | } |
Jon Maloy | 5ef2132 | 2021-06-02 13:44:26 -0400 | [diff] [blame] | 1256 | /* Append clones to inputq only if skb is still head of arrvq */ |
Jon Paul Maloy | cb1b728 | 2015-02-05 08:36:44 -0500 | [diff] [blame] | 1257 | spin_lock_bh(&inputq->lock); |
| 1258 | if (skb_peek(arrvq) == skb) { |
| 1259 | skb_queue_splice_tail_init(&tmpq, inputq); |
Jon Maloy | 5ef2132 | 2021-06-02 13:44:26 -0400 | [diff] [blame] | 1260 | /* Decrement the skb's refcnt */ |
Hoang Le | 7501689 | 2021-05-14 08:23:03 +0700 | [diff] [blame] | 1261 | kfree_skb(__skb_dequeue(arrvq)); |
Jon Paul Maloy | cb1b728 | 2015-02-05 08:36:44 -0500 | [diff] [blame] | 1262 | } |
| 1263 | spin_unlock_bh(&inputq->lock); |
| 1264 | __skb_queue_purge(&tmpq); |
| 1265 | kfree_skb(skb); |
Jon Paul Maloy | 078bec8 | 2014-07-16 20:41:00 -0400 | [diff] [blame] | 1266 | } |
Jon Paul Maloy | cb1b728 | 2015-02-05 08:36:44 -0500 | [diff] [blame] | 1267 | tipc_sk_rcv(net, inputq); |
Jon Paul Maloy | 078bec8 | 2014-07-16 20:41:00 -0400 | [diff] [blame] | 1268 | } |
| 1269 | |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1270 | /* tipc_sk_push_backlog(): send accumulated buffers in socket write queue |
| 1271 | * when socket is in Nagle mode |
| 1272 | */ |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 1273 | static void tipc_sk_push_backlog(struct tipc_sock *tsk, bool nagle_ack) |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1274 | { |
| 1275 | struct sk_buff_head *txq = &tsk->sk.sk_write_queue; |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 1276 | struct sk_buff *skb = skb_peek_tail(txq); |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1277 | struct net *net = sock_net(&tsk->sk); |
| 1278 | u32 dnode = tsk_peer_node(tsk); |
| 1279 | int rc; |
| 1280 | |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 1281 | if (nagle_ack) { |
| 1282 | tsk->pkt_cnt += skb_queue_len(txq); |
| 1283 | if (!tsk->pkt_cnt || tsk->msg_acc / tsk->pkt_cnt < 2) { |
| 1284 | tsk->oneway = 0; |
| 1285 | if (tsk->nagle_start < NAGLE_START_MAX) |
| 1286 | tsk->nagle_start *= 2; |
| 1287 | tsk->expect_ack = false; |
| 1288 | pr_debug("tsk %10u: bad nagle %u -> %u, next start %u!\n", |
| 1289 | tsk->portid, tsk->msg_acc, tsk->pkt_cnt, |
| 1290 | tsk->nagle_start); |
| 1291 | } else { |
| 1292 | tsk->nagle_start = NAGLE_START_INIT; |
| 1293 | if (skb) { |
| 1294 | msg_set_ack_required(buf_msg(skb)); |
| 1295 | tsk->expect_ack = true; |
| 1296 | } else { |
| 1297 | tsk->expect_ack = false; |
| 1298 | } |
| 1299 | } |
| 1300 | tsk->msg_acc = 0; |
| 1301 | tsk->pkt_cnt = 0; |
| 1302 | } |
| 1303 | |
Tung Nguyen | d34910e | 2019-11-28 10:10:08 +0700 | [diff] [blame] | 1304 | if (!skb || tsk->cong_link_cnt) |
| 1305 | return; |
| 1306 | |
| 1307 | /* Do not send SYN again after congestion */ |
| 1308 | if (msg_is_syn(buf_msg(skb))) |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1309 | return; |
| 1310 | |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 1311 | if (tsk->msg_acc) |
| 1312 | tsk->pkt_cnt += skb_queue_len(txq); |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1313 | tsk->snt_unacked += tsk->snd_backlog; |
| 1314 | tsk->snd_backlog = 0; |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1315 | rc = tipc_node_xmit(net, txq, dnode, tsk->portid); |
| 1316 | if (rc == -ELINKCONG) |
| 1317 | tsk->cong_link_cnt = 1; |
| 1318 | } |
| 1319 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1320 | /** |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 1321 | * tipc_sk_conn_proto_rcv - receive a connection mng protocol message |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1322 | * @tsk: receiving socket |
Jon Paul Maloy | bcd3ffd | 2015-07-22 10:11:19 -0400 | [diff] [blame] | 1323 | * @skb: pointer to message buffer. |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 1324 | * @inputq: buffer list containing the buffers |
| 1325 | * @xmitq: output message area |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1326 | */ |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 1327 | static void tipc_sk_conn_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb, |
Parthasarathy Bhuvaragan | e7eb058 | 2018-10-10 17:50:23 +0200 | [diff] [blame] | 1328 | struct sk_buff_head *inputq, |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 1329 | struct sk_buff_head *xmitq) |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1330 | { |
Jon Paul Maloy | bcd3ffd | 2015-07-22 10:11:19 -0400 | [diff] [blame] | 1331 | struct tipc_msg *hdr = buf_msg(skb); |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 1332 | u32 onode = tsk_own_node(tsk); |
| 1333 | struct sock *sk = &tsk->sk; |
Jon Paul Maloy | bcd3ffd | 2015-07-22 10:11:19 -0400 | [diff] [blame] | 1334 | int mtyp = msg_type(hdr); |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1335 | bool was_cong; |
Jon Paul Maloy | bcd3ffd | 2015-07-22 10:11:19 -0400 | [diff] [blame] | 1336 | |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1337 | /* Ignore if connection cannot be validated: */ |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 1338 | if (!tsk_peer_msg(tsk, hdr)) { |
| 1339 | trace_tipc_sk_drop_msg(sk, skb, TIPC_DUMP_NONE, "@proto_rcv!"); |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1340 | goto exit; |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 1341 | } |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1342 | |
Parthasarathy Bhuvaragan | c1be775 | 2017-04-26 10:05:02 +0200 | [diff] [blame] | 1343 | if (unlikely(msg_errcode(hdr))) { |
| 1344 | tipc_set_sk_state(sk, TIPC_DISCONNECTING); |
| 1345 | tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk), |
| 1346 | tsk_peer_port(tsk)); |
| 1347 | sk->sk_state_change(sk); |
Parthasarathy Bhuvaragan | e7eb058 | 2018-10-10 17:50:23 +0200 | [diff] [blame] | 1348 | |
| 1349 | /* State change is ignored if socket already awake, |
| 1350 | * - convert msg to abort msg and add to inqueue |
| 1351 | */ |
| 1352 | msg_set_user(hdr, TIPC_CRITICAL_IMPORTANCE); |
| 1353 | msg_set_type(hdr, TIPC_CONN_MSG); |
| 1354 | msg_set_size(hdr, BASIC_H_SIZE); |
| 1355 | msg_set_hdr_sz(hdr, BASIC_H_SIZE); |
| 1356 | __skb_queue_tail(inputq, skb); |
| 1357 | return; |
Parthasarathy Bhuvaragan | c1be775 | 2017-04-26 10:05:02 +0200 | [diff] [blame] | 1358 | } |
| 1359 | |
Parthasarathy Bhuvaragan | 8ea642e | 2016-11-01 14:02:44 +0100 | [diff] [blame] | 1360 | tsk->probe_unacked = false; |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1361 | |
Jon Paul Maloy | bcd3ffd | 2015-07-22 10:11:19 -0400 | [diff] [blame] | 1362 | if (mtyp == CONN_PROBE) { |
| 1363 | msg_set_type(hdr, CONN_PROBE_REPLY); |
Jon Paul Maloy | f1d048f | 2016-06-17 06:35:57 -0400 | [diff] [blame] | 1364 | if (tipc_msg_reverse(onode, &skb, TIPC_OK)) |
| 1365 | __skb_queue_tail(xmitq, skb); |
Jon Paul Maloy | bcd3ffd | 2015-07-22 10:11:19 -0400 | [diff] [blame] | 1366 | return; |
| 1367 | } else if (mtyp == CONN_ACK) { |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1368 | was_cong = tsk_conn_cong(tsk); |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 1369 | tipc_sk_push_backlog(tsk, msg_nagle_ack(hdr)); |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 1370 | tsk->snt_unacked -= msg_conn_ack(hdr); |
| 1371 | if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) |
| 1372 | tsk->snd_win = msg_adv_win(hdr); |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1373 | if (was_cong && !tsk_conn_cong(tsk)) |
Jon Paul Maloy | bcd3ffd | 2015-07-22 10:11:19 -0400 | [diff] [blame] | 1374 | sk->sk_write_space(sk); |
| 1375 | } else if (mtyp != CONN_PROBE_REPLY) { |
| 1376 | pr_warn("Received unknown CONN_PROTO msg\n"); |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1377 | } |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1378 | exit: |
Jon Paul Maloy | bcd3ffd | 2015-07-22 10:11:19 -0400 | [diff] [blame] | 1379 | kfree_skb(skb); |
Jon Paul Maloy | ac0074e | 2014-06-25 20:41:41 -0500 | [diff] [blame] | 1380 | } |
| 1381 | |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 1382 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1383 | * tipc_sendmsg - send message in connectionless manner |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1384 | * @sock: socket structure |
| 1385 | * @m: message to send |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 1386 | * @dsz: amount of user data to be sent |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1387 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1388 | * Message must have an destination specified explicitly. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1389 | * Used for SOCK_RDM and SOCK_DGRAM messages, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1390 | * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections. |
| 1391 | * (Note: 'SYN+' is prohibited on SOCK_STREAM.) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1392 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 1393 | * Return: the number of bytes sent on success, or errno otherwise |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1394 | */ |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1395 | static int tipc_sendmsg(struct socket *sock, |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 1396 | struct msghdr *m, size_t dsz) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1397 | { |
Ying Xue | 39a0295f | 2015-03-02 15:37:47 +0800 | [diff] [blame] | 1398 | struct sock *sk = sock->sk; |
| 1399 | int ret; |
| 1400 | |
| 1401 | lock_sock(sk); |
| 1402 | ret = __tipc_sendmsg(sock, m, dsz); |
| 1403 | release_sock(sk); |
| 1404 | |
| 1405 | return ret; |
| 1406 | } |
| 1407 | |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1408 | static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen) |
Ying Xue | 39a0295f | 2015-03-02 15:37:47 +0800 | [diff] [blame] | 1409 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1410 | struct sock *sk = sock->sk; |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 1411 | struct net *net = sock_net(sk); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1412 | struct tipc_sock *tsk = tipc_sk(sk); |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1413 | struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name; |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1414 | long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); |
| 1415 | struct list_head *clinks = &tsk->cong_links; |
| 1416 | bool syn = !tipc_sk_type_connectionless(sk); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1417 | struct tipc_group *grp = tsk->group; |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1418 | struct tipc_msg *hdr = &tsk->phdr; |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1419 | struct tipc_socket_addr skaddr; |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1420 | struct sk_buff_head pkts; |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1421 | int atype, mtu, rc; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1422 | |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1423 | if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE)) |
Allan Stephens | c29c3f7 | 2010-04-20 17:58:24 -0400 | [diff] [blame] | 1424 | return -EMSGSIZE; |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1425 | |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1426 | if (ua) { |
| 1427 | if (!tipc_uaddr_valid(ua, m->msg_namelen)) |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 1428 | return -EINVAL; |
Colin Ian King | 743902c | 2021-09-03 00:00:11 +0100 | [diff] [blame] | 1429 | atype = ua->addrtype; |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 1430 | } |
| 1431 | |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1432 | /* If socket belongs to a communication group follow other paths */ |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 1433 | if (grp) { |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1434 | if (!ua) |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 1435 | return tipc_send_group_bcast(sock, m, dlen, timeout); |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1436 | if (atype == TIPC_SERVICE_ADDR) |
Jon Maloy | ee106d7 | 2017-10-13 11:04:28 +0200 | [diff] [blame] | 1437 | return tipc_send_group_anycast(sock, m, dlen, timeout); |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1438 | if (atype == TIPC_SOCKET_ADDR) |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 1439 | return tipc_send_group_unicast(sock, m, dlen, timeout); |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1440 | if (atype == TIPC_SERVICE_RANGE) |
Jon Maloy | 5b8dddb | 2017-10-13 11:04:29 +0200 | [diff] [blame] | 1441 | return tipc_send_group_mcast(sock, m, dlen, timeout); |
Jon Maloy | 27bd9ec | 2017-10-13 11:04:27 +0200 | [diff] [blame] | 1442 | return -EINVAL; |
| 1443 | } |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 1444 | |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1445 | if (!ua) { |
| 1446 | ua = (struct tipc_uaddr *)&tsk->peer; |
| 1447 | if (!syn && ua->family != AF_TIPC) |
Erik Hugne | f2f8036 | 2015-03-19 09:02:19 +0100 | [diff] [blame] | 1448 | return -EDESTADDRREQ; |
Jon Maloy | 02fdc14 | 2021-03-29 13:17:31 -0400 | [diff] [blame] | 1449 | atype = ua->addrtype; |
Erik Hugne | f2f8036 | 2015-03-19 09:02:19 +0100 | [diff] [blame] | 1450 | } |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1451 | |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1452 | if (unlikely(syn)) { |
Parthasarathy Bhuvaragan | 0c288c8 | 2016-11-01 14:02:43 +0100 | [diff] [blame] | 1453 | if (sk->sk_state == TIPC_LISTEN) |
Ying Xue | 39a0295f | 2015-03-02 15:37:47 +0800 | [diff] [blame] | 1454 | return -EPIPE; |
Parthasarathy Bhuvaragan | 438adca | 2016-11-01 14:02:45 +0100 | [diff] [blame] | 1455 | if (sk->sk_state != TIPC_OPEN) |
Ying Xue | 39a0295f | 2015-03-02 15:37:47 +0800 | [diff] [blame] | 1456 | return -EISCONN; |
| 1457 | if (tsk->published) |
| 1458 | return -EOPNOTSUPP; |
Jon Maloy | 14623e0 | 2021-06-02 13:44:24 -0400 | [diff] [blame] | 1459 | if (atype == TIPC_SERVICE_ADDR) |
| 1460 | tsk->conn_addrtype = atype; |
Jon Maloy | 25b9221 | 2018-09-28 20:23:21 +0200 | [diff] [blame] | 1461 | msg_set_syn(hdr, 1); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1462 | } |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1463 | |
Haimin Zhang | d6d8683 | 2021-12-31 10:35:23 +0800 | [diff] [blame] | 1464 | memset(&skaddr, 0, sizeof(skaddr)); |
| 1465 | |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1466 | /* Determine destination */ |
| 1467 | if (atype == TIPC_SERVICE_RANGE) { |
Jon Maloy | 833f867 | 2021-03-16 22:06:17 -0400 | [diff] [blame] | 1468 | return tipc_sendmcast(sock, ua, m, dlen, timeout); |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1469 | } else if (atype == TIPC_SERVICE_ADDR) { |
| 1470 | skaddr.node = ua->lookup_node; |
| 1471 | ua->scope = tipc_node2scope(skaddr.node); |
| 1472 | if (!tipc_nametbl_lookup_anycast(net, ua, &skaddr)) |
Ying Xue | 39a0295f | 2015-03-02 15:37:47 +0800 | [diff] [blame] | 1473 | return -EHOSTUNREACH; |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1474 | } else if (atype == TIPC_SOCKET_ADDR) { |
| 1475 | skaddr = ua->sk; |
Jon Maloy | 335b929 | 2018-04-12 01:15:48 +0200 | [diff] [blame] | 1476 | } else { |
| 1477 | return -EINVAL; |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 1478 | } |
| 1479 | |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1480 | /* Block or return if destination link is congested */ |
Jon Maloy | a80ae53 | 2017-10-13 11:04:22 +0200 | [diff] [blame] | 1481 | rc = tipc_wait_for_cond(sock, &timeout, |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1482 | !tipc_dest_find(clinks, skaddr.node, 0)); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1483 | if (unlikely(rc)) |
Ying Xue | 39a0295f | 2015-03-02 15:37:47 +0800 | [diff] [blame] | 1484 | return rc; |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 1485 | |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1486 | /* Finally build message header */ |
| 1487 | msg_set_destnode(hdr, skaddr.node); |
| 1488 | msg_set_destport(hdr, skaddr.ref); |
| 1489 | if (atype == TIPC_SERVICE_ADDR) { |
Tuong Lien | abc9b4e | 2019-12-10 15:21:04 +0700 | [diff] [blame] | 1490 | msg_set_type(hdr, TIPC_NAMED_MSG); |
| 1491 | msg_set_hdr_sz(hdr, NAMED_H_SIZE); |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1492 | msg_set_nametype(hdr, ua->sa.type); |
| 1493 | msg_set_nameinst(hdr, ua->sa.instance); |
| 1494 | msg_set_lookup_scope(hdr, ua->scope); |
Jon Maloy | b6f88d9 | 2020-11-25 13:29:15 -0500 | [diff] [blame] | 1495 | } else { /* TIPC_SOCKET_ADDR */ |
Tuong Lien | abc9b4e | 2019-12-10 15:21:04 +0700 | [diff] [blame] | 1496 | msg_set_type(hdr, TIPC_DIRECT_MSG); |
| 1497 | msg_set_lookup_scope(hdr, 0); |
Tuong Lien | abc9b4e | 2019-12-10 15:21:04 +0700 | [diff] [blame] | 1498 | msg_set_hdr_sz(hdr, BASIC_H_SIZE); |
| 1499 | } |
| 1500 | |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1501 | /* Add message body */ |
Jon Maloy | e654f9f | 2019-08-15 16:42:50 +0200 | [diff] [blame] | 1502 | __skb_queue_head_init(&pkts); |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1503 | mtu = tipc_node_get_mtu(net, skaddr.node, tsk->portid, true); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1504 | rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts); |
| 1505 | if (unlikely(rc != dlen)) |
| 1506 | return rc; |
Tung Nguyen | 2fe97a5 | 2019-11-28 10:10:05 +0700 | [diff] [blame] | 1507 | if (unlikely(syn && !tipc_msg_skb_clone(&pkts, &sk->sk_write_queue))) { |
| 1508 | __skb_queue_purge(&pkts); |
Tung Nguyen | 6787927 | 2018-09-28 20:23:22 +0200 | [diff] [blame] | 1509 | return -ENOMEM; |
Tung Nguyen | 2fe97a5 | 2019-11-28 10:10:05 +0700 | [diff] [blame] | 1510 | } |
Jon Paul Maloy | e2dafe8 | 2014-06-25 20:41:37 -0500 | [diff] [blame] | 1511 | |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1512 | /* Send message */ |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 1513 | trace_tipc_sk_sendmsg(sk, skb_peek(&pkts), TIPC_DUMP_SK_SNDQ, " "); |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1514 | rc = tipc_node_xmit(net, &pkts, skaddr.node, tsk->portid); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1515 | if (unlikely(rc == -ELINKCONG)) { |
Jon Maloy | 908148b | 2021-03-16 22:06:15 -0400 | [diff] [blame] | 1516 | tipc_dest_push(clinks, skaddr.node, 0); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1517 | tsk->cong_link_cnt++; |
| 1518 | rc = 0; |
| 1519 | } |
| 1520 | |
Xin Long | f8dd60d | 2021-07-22 12:05:41 -0400 | [diff] [blame] | 1521 | if (unlikely(syn && !rc)) { |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1522 | tipc_set_sk_state(sk, TIPC_CONNECTING); |
Xin Long | 7387a72 | 2021-08-15 03:13:36 -0400 | [diff] [blame] | 1523 | if (dlen && timeout) { |
Xin Long | f8dd60d | 2021-07-22 12:05:41 -0400 | [diff] [blame] | 1524 | timeout = msecs_to_jiffies(timeout); |
| 1525 | tipc_wait_for_connect(sock, &timeout); |
| 1526 | } |
| 1527 | } |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1528 | |
| 1529 | return rc ? rc : dlen; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1530 | } |
| 1531 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1532 | /** |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1533 | * tipc_sendstream - send stream-oriented data |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1534 | * @sock: socket structure |
| 1535 | * @m: data to send |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1536 | * @dsz: total length of data to be transmitted |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1537 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1538 | * Used for SOCK_STREAM data. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1539 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 1540 | * Return: the number of bytes sent on success (or partial success), |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 1541 | * or errno if no data sent |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1542 | */ |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1543 | static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1544 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1545 | struct sock *sk = sock->sk; |
Ying Xue | 39a0295f | 2015-03-02 15:37:47 +0800 | [diff] [blame] | 1546 | int ret; |
| 1547 | |
| 1548 | lock_sock(sk); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1549 | ret = __tipc_sendstream(sock, m, dsz); |
Ying Xue | 39a0295f | 2015-03-02 15:37:47 +0800 | [diff] [blame] | 1550 | release_sock(sk); |
| 1551 | |
| 1552 | return ret; |
| 1553 | } |
| 1554 | |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1555 | static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen) |
Ying Xue | 39a0295f | 2015-03-02 15:37:47 +0800 | [diff] [blame] | 1556 | { |
| 1557 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1558 | DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1559 | long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1560 | struct sk_buff_head *txq = &sk->sk_write_queue; |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1561 | struct tipc_sock *tsk = tipc_sk(sk); |
| 1562 | struct tipc_msg *hdr = &tsk->phdr; |
| 1563 | struct net *net = sock_net(sk); |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 1564 | struct sk_buff *skb; |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1565 | u32 dnode = tsk_peer_node(tsk); |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1566 | int maxnagle = tsk->maxnagle; |
| 1567 | int maxpkt = tsk->max_pkt; |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1568 | int send, sent = 0; |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1569 | int blocks, rc = 0; |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1570 | |
| 1571 | if (unlikely(dlen > INT_MAX)) |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1572 | return -EMSGSIZE; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1573 | |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1574 | /* Handle implicit connection setup */ |
Xin Long | f8dd60d | 2021-07-22 12:05:41 -0400 | [diff] [blame] | 1575 | if (unlikely(dest && sk->sk_state == TIPC_OPEN)) { |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1576 | rc = __tipc_sendmsg(sock, m, dlen); |
Parthasarathy Bhuvaragan | 92ef12b | 2018-09-25 18:21:58 +0200 | [diff] [blame] | 1577 | if (dlen && dlen == rc) { |
| 1578 | tsk->peer_caps = tipc_node_get_capabilities(net, dnode); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1579 | tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr)); |
Parthasarathy Bhuvaragan | 92ef12b | 2018-09-25 18:21:58 +0200 | [diff] [blame] | 1580 | } |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1581 | return rc; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1582 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1583 | |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1584 | do { |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1585 | rc = tipc_wait_for_cond(sock, &timeout, |
| 1586 | (!tsk->cong_link_cnt && |
Jon Paul Maloy | 8c44e1a | 2017-01-03 10:55:09 -0500 | [diff] [blame] | 1587 | !tsk_conn_cong(tsk) && |
| 1588 | tipc_sk_connected(sk))); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1589 | if (unlikely(rc)) |
| 1590 | break; |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1591 | send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE); |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1592 | blocks = tsk->snd_backlog; |
Tuong Lien | c9aa81f | 2020-06-11 17:07:35 +0700 | [diff] [blame] | 1593 | if (tsk->oneway++ >= tsk->nagle_start && maxnagle && |
| 1594 | send <= maxnagle) { |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1595 | rc = tipc_msg_append(hdr, m, send, maxnagle, txq); |
| 1596 | if (unlikely(rc < 0)) |
| 1597 | break; |
| 1598 | blocks += rc; |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 1599 | tsk->msg_acc++; |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1600 | if (blocks <= 64 && tsk->expect_ack) { |
| 1601 | tsk->snd_backlog = blocks; |
| 1602 | sent += send; |
| 1603 | break; |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 1604 | } else if (blocks > 64) { |
| 1605 | tsk->pkt_cnt += skb_queue_len(txq); |
| 1606 | } else { |
| 1607 | skb = skb_peek_tail(txq); |
YueHaibing | 4c21daa | 2020-05-28 22:34:07 +0800 | [diff] [blame] | 1608 | if (skb) { |
| 1609 | msg_set_ack_required(buf_msg(skb)); |
| 1610 | tsk->expect_ack = true; |
| 1611 | } else { |
| 1612 | tsk->expect_ack = false; |
| 1613 | } |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 1614 | tsk->msg_acc = 0; |
| 1615 | tsk->pkt_cnt = 0; |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1616 | } |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1617 | } else { |
| 1618 | rc = tipc_msg_build(hdr, m, sent, send, maxpkt, txq); |
| 1619 | if (unlikely(rc != send)) |
| 1620 | break; |
| 1621 | blocks += tsk_inc(tsk, send + MIN_H_SIZE); |
| 1622 | } |
| 1623 | trace_tipc_sk_sendstream(sk, skb_peek(txq), |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 1624 | TIPC_DUMP_SK_SNDQ, " "); |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1625 | rc = tipc_node_xmit(net, txq, dnode, tsk->portid); |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1626 | if (unlikely(rc == -ELINKCONG)) { |
| 1627 | tsk->cong_link_cnt = 1; |
| 1628 | rc = 0; |
| 1629 | } |
| 1630 | if (likely(!rc)) { |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1631 | tsk->snt_unacked += blocks; |
| 1632 | tsk->snd_backlog = 0; |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1633 | sent += send; |
| 1634 | } |
| 1635 | } while (sent < dlen && !rc); |
| 1636 | |
Parthasarathy Bhuvaragan | 3364d61 | 2017-04-24 15:00:42 +0200 | [diff] [blame] | 1637 | return sent ? sent : rc; |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | /** |
| 1641 | * tipc_send_packet - send a connection-oriented message |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1642 | * @sock: socket structure |
| 1643 | * @m: message to send |
| 1644 | * @dsz: length of data to be transmitted |
| 1645 | * |
| 1646 | * Used for SOCK_SEQPACKET messages. |
| 1647 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 1648 | * Return: the number of bytes sent on success, or errno otherwise |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1649 | */ |
Ying Xue | 1b78414 | 2015-03-02 15:37:48 +0800 | [diff] [blame] | 1650 | static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz) |
Jon Paul Maloy | 4ccfe5e | 2014-06-25 20:41:38 -0500 | [diff] [blame] | 1651 | { |
| 1652 | if (dsz > TIPC_MAX_USER_MSG_SIZE) |
| 1653 | return -EMSGSIZE; |
| 1654 | |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 1655 | return tipc_sendstream(sock, m, dsz); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1656 | } |
| 1657 | |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1658 | /* tipc_sk_finish_conn - complete the setup of a connection |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1659 | */ |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1660 | static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port, |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1661 | u32 peer_node) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1662 | { |
Ying Xue | 3721e9c | 2015-01-13 17:07:48 +0800 | [diff] [blame] | 1663 | struct sock *sk = &tsk->sk; |
| 1664 | struct net *net = sock_net(sk); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1665 | struct tipc_msg *msg = &tsk->phdr; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1666 | |
Jon Maloy | 25b9221 | 2018-09-28 20:23:21 +0200 | [diff] [blame] | 1667 | msg_set_syn(msg, 0); |
Jon Paul Maloy | dadebc0 | 2014-08-22 18:09:11 -0400 | [diff] [blame] | 1668 | msg_set_destnode(msg, peer_node); |
| 1669 | msg_set_destport(msg, peer_port); |
| 1670 | msg_set_type(msg, TIPC_CONN_MSG); |
| 1671 | msg_set_lookup_scope(msg, 0); |
| 1672 | msg_set_hdr_sz(msg, SHORT_H_SIZE); |
Jon Paul Maloy | f9fef18 | 2014-03-12 11:31:08 -0400 | [diff] [blame] | 1673 | |
Jon Maloy | 0d5fcebf | 2017-10-20 11:21:32 +0200 | [diff] [blame] | 1674 | sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV); |
Parthasarathy Bhuvaragan | 8ea642e | 2016-11-01 14:02:44 +0100 | [diff] [blame] | 1675 | tipc_set_sk_state(sk, TIPC_ESTABLISHED); |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 1676 | tipc_node_add_conn(net, peer_node, tsk->portid, peer_port); |
Hoang Le | f73b128 | 2019-10-29 07:51:21 +0700 | [diff] [blame] | 1677 | tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid, true); |
Jon Paul Maloy | 60020e1 | 2016-05-02 11:58:46 -0400 | [diff] [blame] | 1678 | tsk->peer_caps = tipc_node_get_capabilities(net, peer_node); |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 1679 | tsk_set_nagle(tsk); |
Tung Nguyen | 6787927 | 2018-09-28 20:23:22 +0200 | [diff] [blame] | 1680 | __skb_queue_purge(&sk->sk_write_queue); |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 1681 | if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) |
| 1682 | return; |
| 1683 | |
| 1684 | /* Fall back to message based flow control */ |
| 1685 | tsk->rcv_win = FLOWCTL_MSG_WIN; |
| 1686 | tsk->snd_win = FLOWCTL_MSG_WIN; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | /** |
Jon Maloy | 31c82a2 | 2017-10-13 11:04:24 +0200 | [diff] [blame] | 1690 | * tipc_sk_set_orig_addr - capture sender's address for received message |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1691 | * @m: descriptor for message info |
Andrew Lunn | d814120 | 2020-07-13 01:15:14 +0200 | [diff] [blame] | 1692 | * @skb: received message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1693 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1694 | * Note: Address is not captured if not requested by receiver. |
| 1695 | */ |
Jon Maloy | 31c82a2 | 2017-10-13 11:04:24 +0200 | [diff] [blame] | 1696 | static void tipc_sk_set_orig_addr(struct msghdr *m, struct sk_buff *skb) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1697 | { |
Jon Maloy | 31c82a2 | 2017-10-13 11:04:24 +0200 | [diff] [blame] | 1698 | DECLARE_SOCKADDR(struct sockaddr_pair *, srcaddr, m->msg_name); |
| 1699 | struct tipc_msg *hdr = buf_msg(skb); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1700 | |
Jon Maloy | 31c82a2 | 2017-10-13 11:04:24 +0200 | [diff] [blame] | 1701 | if (!srcaddr) |
| 1702 | return; |
| 1703 | |
| 1704 | srcaddr->sock.family = AF_TIPC; |
Jon Maloy | b6f88d9 | 2020-11-25 13:29:15 -0500 | [diff] [blame] | 1705 | srcaddr->sock.addrtype = TIPC_SOCKET_ADDR; |
Eric Dumazet | 09c8b97 | 2018-05-09 09:50:22 -0700 | [diff] [blame] | 1706 | srcaddr->sock.scope = 0; |
Jon Maloy | 31c82a2 | 2017-10-13 11:04:24 +0200 | [diff] [blame] | 1707 | srcaddr->sock.addr.id.ref = msg_origport(hdr); |
| 1708 | srcaddr->sock.addr.id.node = msg_orignode(hdr); |
| 1709 | srcaddr->sock.addr.name.domain = 0; |
Jon Maloy | 31c82a2 | 2017-10-13 11:04:24 +0200 | [diff] [blame] | 1710 | m->msg_namelen = sizeof(struct sockaddr_tipc); |
| 1711 | |
| 1712 | if (!msg_in_group(hdr)) |
| 1713 | return; |
| 1714 | |
| 1715 | /* Group message users may also want to know sending member's id */ |
| 1716 | srcaddr->member.family = AF_TIPC; |
Jon Maloy | b6f88d9 | 2020-11-25 13:29:15 -0500 | [diff] [blame] | 1717 | srcaddr->member.addrtype = TIPC_SERVICE_ADDR; |
Eric Dumazet | 09c8b97 | 2018-05-09 09:50:22 -0700 | [diff] [blame] | 1718 | srcaddr->member.scope = 0; |
Jon Maloy | 31c82a2 | 2017-10-13 11:04:24 +0200 | [diff] [blame] | 1719 | srcaddr->member.addr.name.name.type = msg_nametype(hdr); |
| 1720 | srcaddr->member.addr.name.name.instance = TIPC_SKB_CB(skb)->orig_member; |
| 1721 | srcaddr->member.addr.name.domain = 0; |
| 1722 | m->msg_namelen = sizeof(*srcaddr); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | /** |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1726 | * tipc_sk_anc_data_recv - optionally capture ancillary data for received message |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1727 | * @m: descriptor for message info |
Jon Maloy | 1c1274a | 2018-11-17 12:17:06 -0500 | [diff] [blame] | 1728 | * @skb: received message buffer |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1729 | * @tsk: TIPC port associated with message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1730 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1731 | * Note: Ancillary data is not captured if not requested by receiver. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1732 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 1733 | * Return: 0 if successful, otherwise errno |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1734 | */ |
Jon Maloy | 1c1274a | 2018-11-17 12:17:06 -0500 | [diff] [blame] | 1735 | static int tipc_sk_anc_data_recv(struct msghdr *m, struct sk_buff *skb, |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1736 | struct tipc_sock *tsk) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1737 | { |
Jon Maloy | 62633c2 | 2021-06-02 13:44:25 -0400 | [diff] [blame] | 1738 | struct tipc_msg *hdr; |
| 1739 | u32 data[3] = {0,}; |
| 1740 | bool has_addr; |
| 1741 | int dlen, rc; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1742 | |
| 1743 | if (likely(m->msg_controllen == 0)) |
| 1744 | return 0; |
| 1745 | |
Jon Maloy | 62633c2 | 2021-06-02 13:44:25 -0400 | [diff] [blame] | 1746 | hdr = buf_msg(skb); |
| 1747 | dlen = msg_data_sz(hdr); |
| 1748 | |
| 1749 | /* Capture errored message object, if any */ |
| 1750 | if (msg_errcode(hdr)) { |
| 1751 | if (skb_linearize(skb)) |
| 1752 | return -ENOMEM; |
| 1753 | hdr = buf_msg(skb); |
| 1754 | data[0] = msg_errcode(hdr); |
| 1755 | data[1] = dlen; |
| 1756 | rc = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, data); |
| 1757 | if (rc || !dlen) |
| 1758 | return rc; |
| 1759 | rc = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, dlen, msg_data(hdr)); |
| 1760 | if (rc) |
| 1761 | return rc; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1762 | } |
| 1763 | |
Jon Maloy | 62633c2 | 2021-06-02 13:44:25 -0400 | [diff] [blame] | 1764 | /* Capture TIPC_SERVICE_ADDR/RANGE destination address, if any */ |
| 1765 | switch (msg_type(hdr)) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1766 | case TIPC_NAMED_MSG: |
Jon Maloy | 62633c2 | 2021-06-02 13:44:25 -0400 | [diff] [blame] | 1767 | has_addr = true; |
| 1768 | data[0] = msg_nametype(hdr); |
| 1769 | data[1] = msg_namelower(hdr); |
| 1770 | data[2] = data[1]; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1771 | break; |
| 1772 | case TIPC_MCAST_MSG: |
Jon Maloy | 62633c2 | 2021-06-02 13:44:25 -0400 | [diff] [blame] | 1773 | has_addr = true; |
| 1774 | data[0] = msg_nametype(hdr); |
| 1775 | data[1] = msg_namelower(hdr); |
| 1776 | data[2] = msg_nameupper(hdr); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1777 | break; |
| 1778 | case TIPC_CONN_MSG: |
Jon Maloy | 62633c2 | 2021-06-02 13:44:25 -0400 | [diff] [blame] | 1779 | has_addr = !!tsk->conn_addrtype; |
| 1780 | data[0] = msg_nametype(&tsk->phdr); |
| 1781 | data[1] = msg_nameinst(&tsk->phdr); |
| 1782 | data[2] = data[1]; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1783 | break; |
| 1784 | default: |
Jon Maloy | 62633c2 | 2021-06-02 13:44:25 -0400 | [diff] [blame] | 1785 | has_addr = false; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1786 | } |
Jon Maloy | 62633c2 | 2021-06-02 13:44:25 -0400 | [diff] [blame] | 1787 | if (!has_addr) |
| 1788 | return 0; |
| 1789 | return put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, data); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1790 | } |
| 1791 | |
Tuong Lien | c726858 | 2020-05-13 19:33:16 +0700 | [diff] [blame] | 1792 | static struct sk_buff *tipc_sk_build_ack(struct tipc_sock *tsk) |
Jon Paul Maloy | 739f5e4 | 2014-08-22 18:09:12 -0400 | [diff] [blame] | 1793 | { |
Parthasarathy Bhuvaragan | d6fb7e9 | 2016-11-01 14:02:40 +0100 | [diff] [blame] | 1794 | struct sock *sk = &tsk->sk; |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1795 | struct sk_buff *skb = NULL; |
Jon Paul Maloy | 739f5e4 | 2014-08-22 18:09:12 -0400 | [diff] [blame] | 1796 | struct tipc_msg *msg; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 1797 | u32 peer_port = tsk_peer_port(tsk); |
| 1798 | u32 dnode = tsk_peer_node(tsk); |
Jon Paul Maloy | 739f5e4 | 2014-08-22 18:09:12 -0400 | [diff] [blame] | 1799 | |
Parthasarathy Bhuvaragan | d6fb7e9 | 2016-11-01 14:02:40 +0100 | [diff] [blame] | 1800 | if (!tipc_sk_connected(sk)) |
Tuong Lien | c726858 | 2020-05-13 19:33:16 +0700 | [diff] [blame] | 1801 | return NULL; |
Jon Paul Maloy | c589863 | 2015-02-05 08:36:36 -0500 | [diff] [blame] | 1802 | skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0, |
| 1803 | dnode, tsk_own_node(tsk), peer_port, |
| 1804 | tsk->portid, TIPC_OK); |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1805 | if (!skb) |
Tuong Lien | c726858 | 2020-05-13 19:33:16 +0700 | [diff] [blame] | 1806 | return NULL; |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 1807 | msg = buf_msg(skb); |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 1808 | msg_set_conn_ack(msg, tsk->rcv_unacked); |
| 1809 | tsk->rcv_unacked = 0; |
| 1810 | |
| 1811 | /* Adjust to and advertize the correct window limit */ |
| 1812 | if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) { |
| 1813 | tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf); |
| 1814 | msg_set_adv_win(msg, tsk->rcv_win); |
| 1815 | } |
Tuong Lien | c726858 | 2020-05-13 19:33:16 +0700 | [diff] [blame] | 1816 | return skb; |
| 1817 | } |
| 1818 | |
| 1819 | static void tipc_sk_send_ack(struct tipc_sock *tsk) |
| 1820 | { |
| 1821 | struct sk_buff *skb; |
| 1822 | |
| 1823 | skb = tipc_sk_build_ack(tsk); |
| 1824 | if (!skb) |
| 1825 | return; |
| 1826 | |
| 1827 | tipc_node_xmit_skb(sock_net(&tsk->sk), skb, tsk_peer_node(tsk), |
| 1828 | msg_link_selector(buf_msg(skb))); |
Jon Paul Maloy | 739f5e4 | 2014-08-22 18:09:12 -0400 | [diff] [blame] | 1829 | } |
| 1830 | |
Arnaldo Carvalho de Melo | 85d3fc9 | 2014-05-23 15:55:12 -0400 | [diff] [blame] | 1831 | static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop) |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1832 | { |
| 1833 | struct sock *sk = sock->sk; |
Tung Nguyen | 48766a5 | 2019-02-19 11:20:48 +0700 | [diff] [blame] | 1834 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
Arnaldo Carvalho de Melo | 85d3fc9 | 2014-05-23 15:55:12 -0400 | [diff] [blame] | 1835 | long timeo = *timeop; |
Parthasarathy Bhuvaragan | 4e0df49 | 2017-04-26 10:05:01 +0200 | [diff] [blame] | 1836 | int err = sock_error(sk); |
| 1837 | |
| 1838 | if (err) |
| 1839 | return err; |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1840 | |
| 1841 | for (;;) { |
Ying Xue | fe8e464 | 2014-03-06 14:40:18 +0100 | [diff] [blame] | 1842 | if (timeo && skb_queue_empty(&sk->sk_receive_queue)) { |
Parthasarathy Bhuvaragan | 6f00089 | 2016-11-01 14:02:47 +0100 | [diff] [blame] | 1843 | if (sk->sk_shutdown & RCV_SHUTDOWN) { |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1844 | err = -ENOTCONN; |
| 1845 | break; |
| 1846 | } |
Tung Nguyen | 48766a5 | 2019-02-19 11:20:48 +0700 | [diff] [blame] | 1847 | add_wait_queue(sk_sleep(sk), &wait); |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1848 | release_sock(sk); |
Tung Nguyen | 48766a5 | 2019-02-19 11:20:48 +0700 | [diff] [blame] | 1849 | timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo); |
| 1850 | sched_annotate_sleep(); |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1851 | lock_sock(sk); |
Tung Nguyen | 48766a5 | 2019-02-19 11:20:48 +0700 | [diff] [blame] | 1852 | remove_wait_queue(sk_sleep(sk), &wait); |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1853 | } |
| 1854 | err = 0; |
| 1855 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 1856 | break; |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1857 | err = -EAGAIN; |
| 1858 | if (!timeo) |
| 1859 | break; |
Erik Hugne | 143fe22 | 2015-03-09 10:43:42 +0100 | [diff] [blame] | 1860 | err = sock_intr_errno(timeo); |
| 1861 | if (signal_pending(current)) |
| 1862 | break; |
Parthasarathy Bhuvaragan | 4e0df49 | 2017-04-26 10:05:01 +0200 | [diff] [blame] | 1863 | |
| 1864 | err = sock_error(sk); |
| 1865 | if (err) |
| 1866 | break; |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1867 | } |
Arnaldo Carvalho de Melo | 85d3fc9 | 2014-05-23 15:55:12 -0400 | [diff] [blame] | 1868 | *timeop = timeo; |
Ying Xue | 9bbb4ec | 2014-01-17 09:50:07 +0800 | [diff] [blame] | 1869 | return err; |
| 1870 | } |
| 1871 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1872 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 1873 | * tipc_recvmsg - receive packet-oriented message |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 1874 | * @sock: network socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1875 | * @m: descriptor for message info |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1876 | * @buflen: length of user buffer area |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1877 | * @flags: receive flags |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1878 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1879 | * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages. |
| 1880 | * If the complete message doesn't fit in user area, truncate it. |
| 1881 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 1882 | * Return: size of returned message data, errno otherwise |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1883 | */ |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1884 | static int tipc_recvmsg(struct socket *sock, struct msghdr *m, |
| 1885 | size_t buflen, int flags) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1886 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1887 | struct sock *sk = sock->sk; |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1888 | bool connected = !tipc_sk_type_connectionless(sk); |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 1889 | struct tipc_sock *tsk = tipc_sk(sk); |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1890 | int rc, err, hlen, dlen, copy; |
Xin Long | f4919ff | 2021-07-16 17:44:07 -0400 | [diff] [blame] | 1891 | struct tipc_skb_cb *skb_cb; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 1892 | struct sk_buff_head xmitq; |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 1893 | struct tipc_msg *hdr; |
| 1894 | struct sk_buff *skb; |
| 1895 | bool grp_evt; |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1896 | long timeout; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1897 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1898 | /* Catch invalid receive requests */ |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1899 | if (unlikely(!buflen)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1900 | return -EINVAL; |
| 1901 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1902 | lock_sock(sk); |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1903 | if (unlikely(connected && sk->sk_state == TIPC_OPEN)) { |
| 1904 | rc = -ENOTCONN; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1905 | goto exit; |
| 1906 | } |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1907 | timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1908 | |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 1909 | /* Step rcv queue to first msg with data or error; wait if necessary */ |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1910 | do { |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1911 | rc = tipc_wait_for_rcvmsg(sock, &timeout); |
| 1912 | if (unlikely(rc)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1913 | goto exit; |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1914 | skb = skb_peek(&sk->sk_receive_queue); |
Xin Long | f4919ff | 2021-07-16 17:44:07 -0400 | [diff] [blame] | 1915 | skb_cb = TIPC_SKB_CB(skb); |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1916 | hdr = buf_msg(skb); |
| 1917 | dlen = msg_data_sz(hdr); |
| 1918 | hlen = msg_hdr_sz(hdr); |
| 1919 | err = msg_errcode(hdr); |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 1920 | grp_evt = msg_is_grp_evt(hdr); |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1921 | if (likely(dlen || err)) |
| 1922 | break; |
| 1923 | tsk_advance_rx_queue(sk); |
| 1924 | } while (1); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1925 | |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1926 | /* Collect msg meta data, including error code and rejected data */ |
Jon Maloy | 31c82a2 | 2017-10-13 11:04:24 +0200 | [diff] [blame] | 1927 | tipc_sk_set_orig_addr(m, skb); |
Jon Maloy | 1c1274a | 2018-11-17 12:17:06 -0500 | [diff] [blame] | 1928 | rc = tipc_sk_anc_data_recv(m, skb, tsk); |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1929 | if (unlikely(rc)) |
| 1930 | goto exit; |
Jon Maloy | 1c1274a | 2018-11-17 12:17:06 -0500 | [diff] [blame] | 1931 | hdr = buf_msg(skb); |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1932 | |
| 1933 | /* Capture data if non-error msg, otherwise just set return value */ |
| 1934 | if (likely(!err)) { |
Xin Long | f4919ff | 2021-07-16 17:44:07 -0400 | [diff] [blame] | 1935 | int offset = skb_cb->bytes_read; |
| 1936 | |
| 1937 | copy = min_t(int, dlen - offset, buflen); |
| 1938 | rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy); |
| 1939 | if (unlikely(rc)) |
| 1940 | goto exit; |
| 1941 | if (unlikely(offset + copy < dlen)) { |
| 1942 | if (flags & MSG_EOR) { |
| 1943 | if (!(flags & MSG_PEEK)) |
| 1944 | skb_cb->bytes_read = offset + copy; |
| 1945 | } else { |
| 1946 | m->msg_flags |= MSG_TRUNC; |
| 1947 | skb_cb->bytes_read = 0; |
| 1948 | } |
| 1949 | } else { |
| 1950 | if (flags & MSG_EOR) |
| 1951 | m->msg_flags |= MSG_EOR; |
| 1952 | skb_cb->bytes_read = 0; |
| 1953 | } |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1954 | } else { |
| 1955 | copy = 0; |
| 1956 | rc = 0; |
Xin Long | f4919ff | 2021-07-16 17:44:07 -0400 | [diff] [blame] | 1957 | if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control) { |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1958 | rc = -ECONNRESET; |
Xin Long | f4919ff | 2021-07-16 17:44:07 -0400 | [diff] [blame] | 1959 | goto exit; |
| 1960 | } |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1961 | } |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1962 | |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 1963 | /* Mark message as group event if applicable */ |
| 1964 | if (unlikely(grp_evt)) { |
| 1965 | if (msg_grp_evt(hdr) == TIPC_WITHDRAWN) |
| 1966 | m->msg_flags |= MSG_EOR; |
| 1967 | m->msg_flags |= MSG_OOB; |
| 1968 | copy = 0; |
| 1969 | } |
| 1970 | |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1971 | /* Caption of data or error code/rejected data was successful */ |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 1972 | if (unlikely(flags & MSG_PEEK)) |
| 1973 | goto exit; |
| 1974 | |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 1975 | /* Send group flow control advertisement when applicable */ |
| 1976 | if (tsk->group && msg_in_group(hdr) && !grp_evt) { |
Jon Maloy | e654f9f | 2019-08-15 16:42:50 +0200 | [diff] [blame] | 1977 | __skb_queue_head_init(&xmitq); |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 1978 | tipc_group_update_rcv_win(tsk->group, tsk_blocks(hlen + dlen), |
| 1979 | msg_orignode(hdr), msg_origport(hdr), |
| 1980 | &xmitq); |
| 1981 | tipc_node_distr_xmit(sock_net(sk), &xmitq); |
| 1982 | } |
| 1983 | |
Xin Long | cc19862 | 2021-07-23 13:25:36 -0400 | [diff] [blame] | 1984 | if (skb_cb->bytes_read) |
| 1985 | goto exit; |
Jon Maloy | ae236fb | 2017-10-13 11:04:25 +0200 | [diff] [blame] | 1986 | |
Xin Long | cc19862 | 2021-07-23 13:25:36 -0400 | [diff] [blame] | 1987 | tsk_advance_rx_queue(sk); |
| 1988 | |
| 1989 | if (likely(!connected)) |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1990 | goto exit; |
| 1991 | |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 1992 | /* Send connection flow control advertisement when applicable */ |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1993 | tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen); |
| 1994 | if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE) |
| 1995 | tipc_sk_send_ack(tsk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1996 | exit: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1997 | release_sock(sk); |
Jon Paul Maloy | e9f8b10 | 2017-05-02 18:16:53 +0200 | [diff] [blame] | 1998 | return rc ? rc : copy; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1999 | } |
| 2000 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2001 | /** |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2002 | * tipc_recvstream - receive stream-oriented data |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 2003 | * @sock: network socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2004 | * @m: descriptor for message info |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2005 | * @buflen: total size of user buffer area |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2006 | * @flags: receive flags |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2007 | * |
| 2008 | * Used for SOCK_STREAM messages only. If not enough data is available |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2009 | * will optionally wait for more; never truncates data. |
| 2010 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 2011 | * Return: size of returned message data, errno otherwise |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2012 | */ |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2013 | static int tipc_recvstream(struct socket *sock, struct msghdr *m, |
| 2014 | size_t buflen, int flags) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2015 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2016 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 2017 | struct tipc_sock *tsk = tipc_sk(sk); |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2018 | struct sk_buff *skb; |
| 2019 | struct tipc_msg *hdr; |
| 2020 | struct tipc_skb_cb *skb_cb; |
| 2021 | bool peek = flags & MSG_PEEK; |
| 2022 | int offset, required, copy, copied = 0; |
| 2023 | int hlen, dlen, err, rc; |
| 2024 | long timeout; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2025 | |
| 2026 | /* Catch invalid receive attempts */ |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2027 | if (unlikely(!buflen)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2028 | return -EINVAL; |
| 2029 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2030 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2031 | |
Parthasarathy Bhuvaragan | 438adca | 2016-11-01 14:02:45 +0100 | [diff] [blame] | 2032 | if (unlikely(sk->sk_state == TIPC_OPEN)) { |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2033 | rc = -ENOTCONN; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2034 | goto exit; |
| 2035 | } |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2036 | required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen); |
| 2037 | timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2038 | |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2039 | do { |
| 2040 | /* Look at first msg in receive queue; wait if necessary */ |
| 2041 | rc = tipc_wait_for_rcvmsg(sock, &timeout); |
| 2042 | if (unlikely(rc)) |
| 2043 | break; |
| 2044 | skb = skb_peek(&sk->sk_receive_queue); |
| 2045 | skb_cb = TIPC_SKB_CB(skb); |
| 2046 | hdr = buf_msg(skb); |
| 2047 | dlen = msg_data_sz(hdr); |
| 2048 | hlen = msg_hdr_sz(hdr); |
| 2049 | err = msg_errcode(hdr); |
Paul Gortmaker | 617d3c7 | 2012-04-30 15:29:02 -0400 | [diff] [blame] | 2050 | |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2051 | /* Discard any empty non-errored (SYN-) message */ |
| 2052 | if (unlikely(!dlen && !err)) { |
| 2053 | tsk_advance_rx_queue(sk); |
| 2054 | continue; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2055 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2056 | |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2057 | /* Collect msg meta data, incl. error code and rejected data */ |
| 2058 | if (!copied) { |
Jon Maloy | 31c82a2 | 2017-10-13 11:04:24 +0200 | [diff] [blame] | 2059 | tipc_sk_set_orig_addr(m, skb); |
Jon Maloy | 1c1274a | 2018-11-17 12:17:06 -0500 | [diff] [blame] | 2060 | rc = tipc_sk_anc_data_recv(m, skb, tsk); |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2061 | if (rc) |
| 2062 | break; |
Jon Maloy | 1c1274a | 2018-11-17 12:17:06 -0500 | [diff] [blame] | 2063 | hdr = buf_msg(skb); |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2064 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2065 | |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2066 | /* Copy data if msg ok, otherwise return error/partial data */ |
| 2067 | if (likely(!err)) { |
| 2068 | offset = skb_cb->bytes_read; |
| 2069 | copy = min_t(int, dlen - offset, buflen - copied); |
| 2070 | rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy); |
| 2071 | if (unlikely(rc)) |
| 2072 | break; |
| 2073 | copied += copy; |
| 2074 | offset += copy; |
| 2075 | if (unlikely(offset < dlen)) { |
| 2076 | if (!peek) |
| 2077 | skb_cb->bytes_read = offset; |
| 2078 | break; |
| 2079 | } |
| 2080 | } else { |
| 2081 | rc = 0; |
| 2082 | if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control) |
| 2083 | rc = -ECONNRESET; |
| 2084 | if (copied || rc) |
| 2085 | break; |
| 2086 | } |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 2087 | |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2088 | if (unlikely(peek)) |
| 2089 | break; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2090 | |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2091 | tsk_advance_rx_queue(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2092 | |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2093 | /* Send connection flow control advertisement when applicable */ |
| 2094 | tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen); |
Tuong Lien | c726858 | 2020-05-13 19:33:16 +0700 | [diff] [blame] | 2095 | if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE) |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2096 | tipc_sk_send_ack(tsk); |
| 2097 | |
| 2098 | /* Exit if all requested data or FIN/error received */ |
| 2099 | if (copied == buflen || err) |
| 2100 | break; |
| 2101 | |
| 2102 | } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2103 | exit: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2104 | release_sock(sk); |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 2105 | return copied ? copied : rc; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | /** |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 2109 | * tipc_write_space - wake up thread if port congestion is released |
| 2110 | * @sk: socket |
| 2111 | */ |
| 2112 | static void tipc_write_space(struct sock *sk) |
| 2113 | { |
| 2114 | struct socket_wq *wq; |
| 2115 | |
| 2116 | rcu_read_lock(); |
| 2117 | wq = rcu_dereference(sk->sk_wq); |
Herbert Xu | 1ce0bf5 | 2015-11-26 13:55:39 +0800 | [diff] [blame] | 2118 | if (skwq_has_sleeper(wq)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 2119 | wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT | |
| 2120 | EPOLLWRNORM | EPOLLWRBAND); |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 2121 | rcu_read_unlock(); |
| 2122 | } |
| 2123 | |
| 2124 | /** |
| 2125 | * tipc_data_ready - wake up threads to indicate messages have been received |
| 2126 | * @sk: socket |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 2127 | */ |
David S. Miller | 676d236 | 2014-04-11 16:15:36 -0400 | [diff] [blame] | 2128 | static void tipc_data_ready(struct sock *sk) |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 2129 | { |
| 2130 | struct socket_wq *wq; |
| 2131 | |
| 2132 | rcu_read_lock(); |
| 2133 | wq = rcu_dereference(sk->sk_wq); |
Herbert Xu | 1ce0bf5 | 2015-11-26 13:55:39 +0800 | [diff] [blame] | 2134 | if (skwq_has_sleeper(wq)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 2135 | wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN | |
| 2136 | EPOLLRDNORM | EPOLLRDBAND); |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 2137 | rcu_read_unlock(); |
| 2138 | } |
| 2139 | |
Ying Xue | f4195d1 | 2015-11-22 15:46:05 +0800 | [diff] [blame] | 2140 | static void tipc_sock_destruct(struct sock *sk) |
| 2141 | { |
| 2142 | __skb_queue_purge(&sk->sk_receive_queue); |
| 2143 | } |
| 2144 | |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2145 | static void tipc_sk_proto_rcv(struct sock *sk, |
| 2146 | struct sk_buff_head *inputq, |
| 2147 | struct sk_buff_head *xmitq) |
| 2148 | { |
| 2149 | struct sk_buff *skb = __skb_dequeue(inputq); |
| 2150 | struct tipc_sock *tsk = tipc_sk(sk); |
| 2151 | struct tipc_msg *hdr = buf_msg(skb); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 2152 | struct tipc_group *grp = tsk->group; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 2153 | bool wakeup = false; |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2154 | |
| 2155 | switch (msg_user(hdr)) { |
| 2156 | case CONN_MANAGER: |
Parthasarathy Bhuvaragan | e7eb058 | 2018-10-10 17:50:23 +0200 | [diff] [blame] | 2157 | tipc_sk_conn_proto_rcv(tsk, skb, inputq, xmitq); |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2158 | return; |
| 2159 | case SOCK_WAKEUP: |
Jon Maloy | a80ae53 | 2017-10-13 11:04:22 +0200 | [diff] [blame] | 2160 | tipc_dest_del(&tsk->cong_links, msg_orignode(hdr), 0); |
Tung Nguyen | bfd07f3 | 2019-02-25 10:57:20 +0700 | [diff] [blame] | 2161 | /* coupled with smp_rmb() in tipc_wait_for_cond() */ |
| 2162 | smp_wmb(); |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2163 | tsk->cong_link_cnt--; |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 2164 | wakeup = true; |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 2165 | tipc_sk_push_backlog(tsk, false); |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2166 | break; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 2167 | case GROUP_PROTOCOL: |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 2168 | tipc_group_proto_rcv(grp, &wakeup, hdr, inputq, xmitq); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 2169 | break; |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2170 | case TOP_SRV: |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 2171 | tipc_group_member_evt(tsk->group, &wakeup, &sk->sk_rcvbuf, |
Jon Maloy | 7ad32bc | 2018-01-08 21:03:26 +0100 | [diff] [blame] | 2172 | hdr, inputq, xmitq); |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2173 | break; |
| 2174 | default: |
| 2175 | break; |
| 2176 | } |
| 2177 | |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 2178 | if (wakeup) |
| 2179 | sk->sk_write_space(sk); |
| 2180 | |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2181 | kfree_skb(skb); |
| 2182 | } |
| 2183 | |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 2184 | /** |
Jon Maloy | 39fdc9c | 2018-09-28 20:23:20 +0200 | [diff] [blame] | 2185 | * tipc_sk_filter_connect - check incoming message for a connection-based socket |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 2186 | * @tsk: TIPC socket |
Jon Maloy | 39fdc9c | 2018-09-28 20:23:20 +0200 | [diff] [blame] | 2187 | * @skb: pointer to message buffer. |
Tuong Lien | c726858 | 2020-05-13 19:33:16 +0700 | [diff] [blame] | 2188 | * @xmitq: for Nagle ACK if any |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 2189 | * Return: true if message should be added to receive queue, false otherwise |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 2190 | */ |
Tuong Lien | c726858 | 2020-05-13 19:33:16 +0700 | [diff] [blame] | 2191 | static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb, |
| 2192 | struct sk_buff_head *xmitq) |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 2193 | { |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 2194 | struct sock *sk = &tsk->sk; |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 2195 | struct net *net = sock_net(sk); |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2196 | struct tipc_msg *hdr = buf_msg(skb); |
Jon Maloy | 39fdc9c | 2018-09-28 20:23:20 +0200 | [diff] [blame] | 2197 | bool con_msg = msg_connected(hdr); |
| 2198 | u32 pport = tsk_peer_port(tsk); |
| 2199 | u32 pnode = tsk_peer_node(tsk); |
| 2200 | u32 oport = msg_origport(hdr); |
| 2201 | u32 onode = msg_orignode(hdr); |
| 2202 | int err = msg_errcode(hdr); |
Tung Nguyen | 6787927 | 2018-09-28 20:23:22 +0200 | [diff] [blame] | 2203 | unsigned long delay; |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 2204 | |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2205 | if (unlikely(msg_mcast(hdr))) |
| 2206 | return false; |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 2207 | tsk->oneway = 0; |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 2208 | |
Parthasarathy Bhuvaragan | 99a2088 | 2016-11-01 14:02:48 +0100 | [diff] [blame] | 2209 | switch (sk->sk_state) { |
| 2210 | case TIPC_CONNECTING: |
Jon Maloy | 39fdc9c | 2018-09-28 20:23:20 +0200 | [diff] [blame] | 2211 | /* Setup ACK */ |
| 2212 | if (likely(con_msg)) { |
| 2213 | if (err) |
| 2214 | break; |
| 2215 | tipc_sk_finish_conn(tsk, oport, onode); |
| 2216 | msg_set_importance(&tsk->phdr, msg_importance(hdr)); |
| 2217 | /* ACK+ message with data is added to receive queue */ |
| 2218 | if (msg_data_sz(hdr)) |
| 2219 | return true; |
| 2220 | /* Empty ACK-, - wake up sleeping connect() and drop */ |
Parthasarathy Bhuvaragan | ff94683 | 2019-05-09 07:13:42 +0200 | [diff] [blame] | 2221 | sk->sk_state_change(sk); |
Jon Maloy | 39fdc9c | 2018-09-28 20:23:20 +0200 | [diff] [blame] | 2222 | msg_set_dest_droppable(hdr, 1); |
| 2223 | return false; |
Parthasarathy Bhuvaragan | 4e0df49 | 2017-04-26 10:05:01 +0200 | [diff] [blame] | 2224 | } |
Jon Maloy | 39fdc9c | 2018-09-28 20:23:20 +0200 | [diff] [blame] | 2225 | /* Ignore connectionless message if not from listening socket */ |
| 2226 | if (oport != pport || onode != pnode) |
Parthasarathy Bhuvaragan | f40acba | 2016-11-01 14:02:49 +0100 | [diff] [blame] | 2227 | return false; |
| 2228 | |
Tung Nguyen | 6787927 | 2018-09-28 20:23:22 +0200 | [diff] [blame] | 2229 | /* Rejected SYN */ |
| 2230 | if (err != TIPC_ERR_OVERLOAD) |
| 2231 | break; |
| 2232 | |
| 2233 | /* Prepare for new setup attempt if we have a SYN clone */ |
| 2234 | if (skb_queue_empty(&sk->sk_write_queue)) |
| 2235 | break; |
| 2236 | get_random_bytes(&delay, 2); |
| 2237 | delay %= (tsk->conn_timeout / 4); |
| 2238 | delay = msecs_to_jiffies(delay + 100); |
| 2239 | sk_reset_timer(sk, &sk->sk_timer, jiffies + delay); |
| 2240 | return false; |
Jon Maloy | 39fdc9c | 2018-09-28 20:23:20 +0200 | [diff] [blame] | 2241 | case TIPC_OPEN: |
| 2242 | case TIPC_DISCONNECTING: |
| 2243 | return false; |
| 2244 | case TIPC_LISTEN: |
| 2245 | /* Accept only SYN message */ |
Jon Maloy | 25b9221 | 2018-09-28 20:23:21 +0200 | [diff] [blame] | 2246 | if (!msg_is_syn(hdr) && |
| 2247 | tipc_node_get_capabilities(net, onode) & TIPC_SYN_BIT) |
| 2248 | return false; |
Jon Maloy | 39fdc9c | 2018-09-28 20:23:20 +0200 | [diff] [blame] | 2249 | if (!con_msg && !err) |
| 2250 | return true; |
| 2251 | return false; |
| 2252 | case TIPC_ESTABLISHED: |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 2253 | if (!skb_queue_empty(&sk->sk_write_queue)) |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 2254 | tipc_sk_push_backlog(tsk, false); |
Jon Maloy | 39fdc9c | 2018-09-28 20:23:20 +0200 | [diff] [blame] | 2255 | /* Accept only connection-based messages sent by peer */ |
Tuong Lien | c726858 | 2020-05-13 19:33:16 +0700 | [diff] [blame] | 2256 | if (likely(con_msg && !err && pport == oport && |
| 2257 | pnode == onode)) { |
| 2258 | if (msg_ack_required(hdr)) { |
| 2259 | struct sk_buff *skb; |
| 2260 | |
| 2261 | skb = tipc_sk_build_ack(tsk); |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 2262 | if (skb) { |
| 2263 | msg_set_nagle_ack(buf_msg(skb)); |
Tuong Lien | c726858 | 2020-05-13 19:33:16 +0700 | [diff] [blame] | 2264 | __skb_queue_tail(xmitq, skb); |
Tuong Lien | 0a3e060 | 2020-05-26 16:38:38 +0700 | [diff] [blame] | 2265 | } |
Tuong Lien | c726858 | 2020-05-13 19:33:16 +0700 | [diff] [blame] | 2266 | } |
Jon Maloy | 39fdc9c | 2018-09-28 20:23:20 +0200 | [diff] [blame] | 2267 | return true; |
Tuong Lien | c726858 | 2020-05-13 19:33:16 +0700 | [diff] [blame] | 2268 | } |
Jon Maloy | 39fdc9c | 2018-09-28 20:23:20 +0200 | [diff] [blame] | 2269 | if (!tsk_peer_msg(tsk, hdr)) |
| 2270 | return false; |
| 2271 | if (!err) |
| 2272 | return true; |
| 2273 | tipc_set_sk_state(sk, TIPC_DISCONNECTING); |
| 2274 | tipc_node_remove_conn(net, pnode, tsk->portid); |
| 2275 | sk->sk_state_change(sk); |
Parthasarathy Bhuvaragan | f40acba | 2016-11-01 14:02:49 +0100 | [diff] [blame] | 2276 | return true; |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 2277 | default: |
Parthasarathy Bhuvaragan | 438adca | 2016-11-01 14:02:45 +0100 | [diff] [blame] | 2278 | pr_err("Unknown sk_state %u\n", sk->sk_state); |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 2279 | } |
Jon Maloy | 39fdc9c | 2018-09-28 20:23:20 +0200 | [diff] [blame] | 2280 | /* Abort connection setup attempt */ |
| 2281 | tipc_set_sk_state(sk, TIPC_DISCONNECTING); |
| 2282 | sk->sk_err = ECONNREFUSED; |
| 2283 | sk->sk_state_change(sk); |
| 2284 | return true; |
Ying Xue | 7e6c131 | 2012-11-29 18:39:14 -0500 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | /** |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 2288 | * rcvbuf_limit - get proper overload limit of socket receive queue |
| 2289 | * @sk: socket |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 2290 | * @skb: message |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 2291 | * |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 2292 | * For connection oriented messages, irrespective of importance, |
| 2293 | * default queue limit is 2 MB. |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 2294 | * |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 2295 | * For connectionless messages, queue limits are based on message |
| 2296 | * importance as follows: |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 2297 | * |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 2298 | * TIPC_LOW_IMPORTANCE (2 MB) |
| 2299 | * TIPC_MEDIUM_IMPORTANCE (4 MB) |
| 2300 | * TIPC_HIGH_IMPORTANCE (8 MB) |
| 2301 | * TIPC_CRITICAL_IMPORTANCE (16 MB) |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 2302 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 2303 | * Return: overload limit according to corresponding message importance |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 2304 | */ |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 2305 | static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb) |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 2306 | { |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 2307 | struct tipc_sock *tsk = tipc_sk(sk); |
| 2308 | struct tipc_msg *hdr = buf_msg(skb); |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 2309 | |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 2310 | if (unlikely(msg_in_group(hdr))) |
Eric Dumazet | 8265792 | 2019-10-09 15:21:13 -0700 | [diff] [blame] | 2311 | return READ_ONCE(sk->sk_rcvbuf); |
Jon Maloy | b7d4263 | 2017-10-13 11:04:26 +0200 | [diff] [blame] | 2312 | |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 2313 | if (unlikely(!msg_connected(hdr))) |
Eric Dumazet | 8265792 | 2019-10-09 15:21:13 -0700 | [diff] [blame] | 2314 | return READ_ONCE(sk->sk_rcvbuf) << msg_importance(hdr); |
wangweidong | 0cee6bb | 2013-12-12 09:36:39 +0800 | [diff] [blame] | 2315 | |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 2316 | if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL)) |
Eric Dumazet | 8265792 | 2019-10-09 15:21:13 -0700 | [diff] [blame] | 2317 | return READ_ONCE(sk->sk_rcvbuf); |
Jon Paul Maloy | 10724cc | 2016-05-02 11:58:47 -0400 | [diff] [blame] | 2318 | |
| 2319 | return FLOWCTL_MSG_LIM; |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | /** |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2323 | * tipc_sk_filter_rcv - validate incoming message |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2324 | * @sk: socket |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2325 | * @skb: pointer to message. |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 2326 | * @xmitq: output message area (FIXME) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2327 | * |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2328 | * Enqueues message on receive queue if acceptable; optionally handles |
| 2329 | * disconnect indication for a connected socket. |
| 2330 | * |
Jon Paul Maloy | 1186adf | 2015-02-05 08:36:37 -0500 | [diff] [blame] | 2331 | * Called with socket lock already taken |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2332 | */ |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2333 | static void tipc_sk_filter_rcv(struct sock *sk, struct sk_buff *skb, |
| 2334 | struct sk_buff_head *xmitq) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2335 | { |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2336 | bool sk_conn = !tipc_sk_type_connectionless(sk); |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 2337 | struct tipc_sock *tsk = tipc_sk(sk); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 2338 | struct tipc_group *grp = tsk->group; |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2339 | struct tipc_msg *hdr = buf_msg(skb); |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2340 | struct net *net = sock_net(sk); |
| 2341 | struct sk_buff_head inputq; |
Hoang Le | 77d5ad4 | 2019-03-21 17:25:17 +0700 | [diff] [blame] | 2342 | int mtyp = msg_type(hdr); |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2343 | int limit, err = TIPC_OK; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2344 | |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 2345 | trace_tipc_sk_filter_rcv(sk, skb, TIPC_DUMP_ALL, " "); |
Parthasarathy Bhuvaragan | ba8aebe | 2016-11-01 14:02:37 +0100 | [diff] [blame] | 2346 | TIPC_SKB_CB(skb)->bytes_read = 0; |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2347 | __skb_queue_head_init(&inputq); |
| 2348 | __skb_queue_tail(&inputq, skb); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2349 | |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2350 | if (unlikely(!msg_isdata(hdr))) |
| 2351 | tipc_sk_proto_rcv(sk, &inputq, xmitq); |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2352 | |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 2353 | if (unlikely(grp)) |
| 2354 | tipc_group_filter_msg(grp, &inputq, xmitq); |
| 2355 | |
Hoang Le | 77d5ad4 | 2019-03-21 17:25:17 +0700 | [diff] [blame] | 2356 | if (unlikely(!grp) && mtyp == TIPC_MCAST_MSG) |
Hoang Le | 08e046c | 2019-03-21 17:25:18 +0700 | [diff] [blame] | 2357 | tipc_mcast_filter_msg(net, &tsk->mc_method.deferredq, &inputq); |
Hoang Le | c55c8ed | 2019-03-19 18:49:50 +0700 | [diff] [blame] | 2358 | |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2359 | /* Validate and add to receive buffer if there is space */ |
| 2360 | while ((skb = __skb_dequeue(&inputq))) { |
| 2361 | hdr = buf_msg(skb); |
| 2362 | limit = rcvbuf_limit(sk, skb); |
Tuong Lien | c726858 | 2020-05-13 19:33:16 +0700 | [diff] [blame] | 2363 | if ((sk_conn && !tipc_sk_filter_connect(tsk, skb, xmitq)) || |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 2364 | (!sk_conn && msg_connected(hdr)) || |
| 2365 | (!grp && msg_in_group(hdr))) |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2366 | err = TIPC_ERR_NO_PORT; |
GhantaKrishnamurthy MohanKrishna | 872619d | 2018-03-21 14:37:45 +0100 | [diff] [blame] | 2367 | else if (sk_rmem_alloc_get(sk) + skb->truesize >= limit) { |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 2368 | trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL, |
| 2369 | "err_overload2!"); |
GhantaKrishnamurthy MohanKrishna | 872619d | 2018-03-21 14:37:45 +0100 | [diff] [blame] | 2370 | atomic_inc(&sk->sk_drops); |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2371 | err = TIPC_ERR_OVERLOAD; |
GhantaKrishnamurthy MohanKrishna | 872619d | 2018-03-21 14:37:45 +0100 | [diff] [blame] | 2372 | } |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2373 | |
| 2374 | if (unlikely(err)) { |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 2375 | if (tipc_msg_reverse(tipc_own_addr(net), &skb, err)) { |
| 2376 | trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE, |
| 2377 | "@filter_rcv!"); |
| 2378 | __skb_queue_tail(xmitq, skb); |
| 2379 | } |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2380 | err = TIPC_OK; |
| 2381 | continue; |
| 2382 | } |
| 2383 | __skb_queue_tail(&sk->sk_receive_queue, skb); |
| 2384 | skb_set_owner_r(skb, sk); |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 2385 | trace_tipc_sk_overlimit2(sk, skb, TIPC_DUMP_ALL, |
| 2386 | "rcvq >90% allocated!"); |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2387 | sk->sk_data_ready(sk); |
| 2388 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2389 | } |
| 2390 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2391 | /** |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2392 | * tipc_sk_backlog_rcv - handle incoming message from backlog queue |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2393 | * @sk: socket |
Ying Xue | a6ca109 | 2014-11-26 11:41:55 +0800 | [diff] [blame] | 2394 | * @skb: message |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2395 | * |
Jon Paul Maloy | e3a7756 | 2015-02-05 08:36:39 -0500 | [diff] [blame] | 2396 | * Caller must hold socket lock |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2397 | */ |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2398 | static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2399 | { |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2400 | unsigned int before = sk_rmem_alloc_get(sk); |
Jon Paul Maloy | f1d048f | 2016-06-17 06:35:57 -0400 | [diff] [blame] | 2401 | struct sk_buff_head xmitq; |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2402 | unsigned int added; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2403 | |
Jon Paul Maloy | f1d048f | 2016-06-17 06:35:57 -0400 | [diff] [blame] | 2404 | __skb_queue_head_init(&xmitq); |
| 2405 | |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2406 | tipc_sk_filter_rcv(sk, skb, &xmitq); |
| 2407 | added = sk_rmem_alloc_get(sk) - before; |
| 2408 | atomic_add(added, &tipc_sk(sk)->dupl_rcvcnt); |
| 2409 | |
| 2410 | /* Send pending response/rejected messages, if any */ |
Jon Maloy | f70d37b | 2017-10-13 11:04:21 +0200 | [diff] [blame] | 2411 | tipc_node_distr_xmit(sock_net(sk), &xmitq); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2412 | return 0; |
| 2413 | } |
| 2414 | |
| 2415 | /** |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2416 | * tipc_sk_enqueue - extract all buffers with destination 'dport' from |
| 2417 | * inputq and try adding them to socket or backlog queue |
| 2418 | * @inputq: list of incoming buffers with potentially different destinations |
| 2419 | * @sk: socket where the buffers should be enqueued |
| 2420 | * @dport: port number for the socket |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 2421 | * @xmitq: output queue |
Jon Paul Maloy | d570d86 | 2015-02-05 08:36:38 -0500 | [diff] [blame] | 2422 | * |
| 2423 | * Caller must hold socket lock |
Jon Paul Maloy | d570d86 | 2015-02-05 08:36:38 -0500 | [diff] [blame] | 2424 | */ |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2425 | static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk, |
Jon Paul Maloy | f1d048f | 2016-06-17 06:35:57 -0400 | [diff] [blame] | 2426 | u32 dport, struct sk_buff_head *xmitq) |
Jon Paul Maloy | d570d86 | 2015-02-05 08:36:38 -0500 | [diff] [blame] | 2427 | { |
Hoang Le | f4bb62e | 2021-09-13 16:28:52 +0700 | [diff] [blame] | 2428 | unsigned long time_limit = jiffies + usecs_to_jiffies(20000); |
Jon Paul Maloy | f1d048f | 2016-06-17 06:35:57 -0400 | [diff] [blame] | 2429 | struct sk_buff *skb; |
Jon Paul Maloy | d570d86 | 2015-02-05 08:36:38 -0500 | [diff] [blame] | 2430 | unsigned int lim; |
| 2431 | atomic_t *dcnt; |
Jon Paul Maloy | f1d048f | 2016-06-17 06:35:57 -0400 | [diff] [blame] | 2432 | u32 onode; |
Jon Paul Maloy | d570d86 | 2015-02-05 08:36:38 -0500 | [diff] [blame] | 2433 | |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2434 | while (skb_queue_len(inputq)) { |
Jon Paul Maloy | 51a00da | 2015-02-08 11:10:50 -0500 | [diff] [blame] | 2435 | if (unlikely(time_after_eq(jiffies, time_limit))) |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2436 | return; |
| 2437 | |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2438 | skb = tipc_skb_dequeue(inputq, dport); |
| 2439 | if (unlikely(!skb)) |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2440 | return; |
| 2441 | |
| 2442 | /* Add message directly to receive queue if possible */ |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2443 | if (!sock_owned_by_user(sk)) { |
Jon Maloy | 64ac5f5 | 2017-10-13 11:04:20 +0200 | [diff] [blame] | 2444 | tipc_sk_filter_rcv(sk, skb, xmitq); |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2445 | continue; |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2446 | } |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2447 | |
| 2448 | /* Try backlog, compensating for double-counted bytes */ |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2449 | dcnt = &tipc_sk(sk)->dupl_rcvcnt; |
Jon Paul Maloy | 7c8bcfb | 2016-05-02 11:58:45 -0400 | [diff] [blame] | 2450 | if (!sk->sk_backlog.len) |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2451 | atomic_set(dcnt, 0); |
| 2452 | lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt); |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 2453 | if (likely(!sk_add_backlog(sk, skb, lim))) { |
| 2454 | trace_tipc_sk_overlimit1(sk, skb, TIPC_DUMP_ALL, |
| 2455 | "bklg & rcvq >90% allocated!"); |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2456 | continue; |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 2457 | } |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2458 | |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 2459 | trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL, "err_overload!"); |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2460 | /* Overload => reject message back to sender */ |
Jon Paul Maloy | f1d048f | 2016-06-17 06:35:57 -0400 | [diff] [blame] | 2461 | onode = tipc_own_addr(sock_net(sk)); |
GhantaKrishnamurthy MohanKrishna | 872619d | 2018-03-21 14:37:45 +0100 | [diff] [blame] | 2462 | atomic_inc(&sk->sk_drops); |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 2463 | if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD)) { |
| 2464 | trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_ALL, |
| 2465 | "@sk_enqueue!"); |
Jon Paul Maloy | f1d048f | 2016-06-17 06:35:57 -0400 | [diff] [blame] | 2466 | __skb_queue_tail(xmitq, skb); |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 2467 | } |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2468 | break; |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2469 | } |
Jon Paul Maloy | d570d86 | 2015-02-05 08:36:38 -0500 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | /** |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2473 | * tipc_sk_rcv - handle a chain of incoming buffers |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 2474 | * @net: the associated network namespace |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2475 | * @inputq: buffer list containing the buffers |
| 2476 | * Consumes all buffers in list until inputq is empty |
| 2477 | * Note: may be called in multiple threads referring to the same queue |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2478 | */ |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2479 | void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2480 | { |
Jon Paul Maloy | f1d048f | 2016-06-17 06:35:57 -0400 | [diff] [blame] | 2481 | struct sk_buff_head xmitq; |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2482 | u32 dnode, dport = 0; |
Erik Hugne | 9871b27 | 2015-04-23 09:37:39 -0400 | [diff] [blame] | 2483 | int err; |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 2484 | struct tipc_sock *tsk; |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 2485 | struct sock *sk; |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2486 | struct sk_buff *skb; |
Jon Paul Maloy | 9816f06 | 2014-05-14 05:39:15 -0400 | [diff] [blame] | 2487 | |
Jon Paul Maloy | f1d048f | 2016-06-17 06:35:57 -0400 | [diff] [blame] | 2488 | __skb_queue_head_init(&xmitq); |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2489 | while (skb_queue_len(inputq)) { |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2490 | dport = tipc_skb_peek_port(inputq, dport); |
| 2491 | tsk = tipc_sk_lookup(net, dport); |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2492 | |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2493 | if (likely(tsk)) { |
| 2494 | sk = &tsk->sk; |
| 2495 | if (likely(spin_trylock_bh(&sk->sk_lock.slock))) { |
Jon Paul Maloy | f1d048f | 2016-06-17 06:35:57 -0400 | [diff] [blame] | 2496 | tipc_sk_enqueue(inputq, sk, dport, &xmitq); |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2497 | spin_unlock_bh(&sk->sk_lock.slock); |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2498 | } |
Jon Paul Maloy | f1d048f | 2016-06-17 06:35:57 -0400 | [diff] [blame] | 2499 | /* Send pending response/rejected messages, if any */ |
Jon Maloy | f70d37b | 2017-10-13 11:04:21 +0200 | [diff] [blame] | 2500 | tipc_node_distr_xmit(sock_net(sk), &xmitq); |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2501 | sock_put(sk); |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2502 | continue; |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2503 | } |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2504 | /* No destination socket => dequeue skb if still there */ |
| 2505 | skb = tipc_skb_dequeue(inputq, dport); |
| 2506 | if (!skb) |
| 2507 | return; |
| 2508 | |
| 2509 | /* Try secondary lookup if unresolved named message */ |
| 2510 | err = TIPC_ERR_NO_PORT; |
| 2511 | if (tipc_msg_lookup_dest(net, skb, &err)) |
| 2512 | goto xmit; |
| 2513 | |
| 2514 | /* Prepare for message rejection */ |
| 2515 | if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err)) |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2516 | continue; |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 2517 | |
| 2518 | trace_tipc_sk_rej_msg(NULL, skb, TIPC_DUMP_NONE, "@sk_rcv!"); |
Jon Paul Maloy | e3a7756 | 2015-02-05 08:36:39 -0500 | [diff] [blame] | 2519 | xmit: |
Jon Paul Maloy | cda3696 | 2015-07-22 10:11:20 -0400 | [diff] [blame] | 2520 | dnode = msg_destnode(buf_msg(skb)); |
Jon Paul Maloy | af9b028 | 2015-07-16 16:54:24 -0400 | [diff] [blame] | 2521 | tipc_node_xmit_skb(net, skb, dnode, dport); |
Jon Paul Maloy | c637c10 | 2015-02-05 08:36:41 -0500 | [diff] [blame] | 2522 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2523 | } |
| 2524 | |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 2525 | static int tipc_wait_for_connect(struct socket *sock, long *timeo_p) |
| 2526 | { |
WANG Cong | d9dc8b0 | 2016-11-11 10:20:50 -0800 | [diff] [blame] | 2527 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 2528 | struct sock *sk = sock->sk; |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 2529 | int done; |
| 2530 | |
| 2531 | do { |
| 2532 | int err = sock_error(sk); |
| 2533 | if (err) |
| 2534 | return err; |
| 2535 | if (!*timeo_p) |
| 2536 | return -ETIMEDOUT; |
| 2537 | if (signal_pending(current)) |
| 2538 | return sock_intr_errno(*timeo_p); |
Tuong Lien | 5391a87 | 2020-02-10 15:35:44 +0700 | [diff] [blame] | 2539 | if (sk->sk_state == TIPC_DISCONNECTING) |
| 2540 | break; |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 2541 | |
WANG Cong | d9dc8b0 | 2016-11-11 10:20:50 -0800 | [diff] [blame] | 2542 | add_wait_queue(sk_sleep(sk), &wait); |
Tuong Lien | 9546a0b | 2020-01-08 09:19:00 +0700 | [diff] [blame] | 2543 | done = sk_wait_event(sk, timeo_p, tipc_sk_connected(sk), |
| 2544 | &wait); |
WANG Cong | d9dc8b0 | 2016-11-11 10:20:50 -0800 | [diff] [blame] | 2545 | remove_wait_queue(sk_sleep(sk), &wait); |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 2546 | } while (!done); |
| 2547 | return 0; |
| 2548 | } |
| 2549 | |
Erik Hugne | ea23931 | 2019-03-17 18:46:42 +0100 | [diff] [blame] | 2550 | static bool tipc_sockaddr_is_sane(struct sockaddr_tipc *addr) |
| 2551 | { |
| 2552 | if (addr->family != AF_TIPC) |
| 2553 | return false; |
| 2554 | if (addr->addrtype == TIPC_SERVICE_RANGE) |
| 2555 | return (addr->addr.nameseq.lower <= addr->addr.nameseq.upper); |
| 2556 | return (addr->addrtype == TIPC_SERVICE_ADDR || |
| 2557 | addr->addrtype == TIPC_SOCKET_ADDR); |
| 2558 | } |
| 2559 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2560 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2561 | * tipc_connect - establish a connection to another TIPC port |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2562 | * @sock: socket structure |
| 2563 | * @dest: socket address for destination port |
| 2564 | * @destlen: size of socket address data structure |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2565 | * @flags: file-related flags associated with socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2566 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 2567 | * Return: 0 on success, errno otherwise |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2568 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2569 | static int tipc_connect(struct socket *sock, struct sockaddr *dest, |
| 2570 | int destlen, int flags) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2571 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2572 | struct sock *sk = sock->sk; |
Erik Hugne | f2f8036 | 2015-03-19 09:02:19 +0100 | [diff] [blame] | 2573 | struct tipc_sock *tsk = tipc_sk(sk); |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 2574 | struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest; |
| 2575 | struct msghdr m = {NULL,}; |
Erik Hugne | f2f8036 | 2015-03-19 09:02:19 +0100 | [diff] [blame] | 2576 | long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout; |
Parthasarathy Bhuvaragan | 99a2088 | 2016-11-01 14:02:48 +0100 | [diff] [blame] | 2577 | int previous; |
Erik Hugne | f2f8036 | 2015-03-19 09:02:19 +0100 | [diff] [blame] | 2578 | int res = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2579 | |
Jon Maloy | 2399883 | 2017-10-13 11:04:18 +0200 | [diff] [blame] | 2580 | if (destlen != sizeof(struct sockaddr_tipc)) |
| 2581 | return -EINVAL; |
| 2582 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2583 | lock_sock(sk); |
| 2584 | |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 2585 | if (tsk->group) { |
| 2586 | res = -EINVAL; |
| 2587 | goto exit; |
| 2588 | } |
| 2589 | |
Jon Maloy | 2399883 | 2017-10-13 11:04:18 +0200 | [diff] [blame] | 2590 | if (dst->family == AF_UNSPEC) { |
| 2591 | memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc)); |
| 2592 | if (!tipc_sk_type_connectionless(sk)) |
| 2593 | res = -EINVAL; |
| 2594 | goto exit; |
Jon Maloy | 2399883 | 2017-10-13 11:04:18 +0200 | [diff] [blame] | 2595 | } |
Erik Hugne | ea23931 | 2019-03-17 18:46:42 +0100 | [diff] [blame] | 2596 | if (!tipc_sockaddr_is_sane(dst)) { |
Jon Maloy | 2399883 | 2017-10-13 11:04:18 +0200 | [diff] [blame] | 2597 | res = -EINVAL; |
Jon Maloy | 2399883 | 2017-10-13 11:04:18 +0200 | [diff] [blame] | 2598 | goto exit; |
Erik Hugne | ea23931 | 2019-03-17 18:46:42 +0100 | [diff] [blame] | 2599 | } |
Erik Hugne | f2f8036 | 2015-03-19 09:02:19 +0100 | [diff] [blame] | 2600 | /* DGRAM/RDM connect(), just save the destaddr */ |
Parthasarathy Bhuvaragan | c752023a | 2016-11-01 14:02:42 +0100 | [diff] [blame] | 2601 | if (tipc_sk_type_connectionless(sk)) { |
Jon Maloy | 2399883 | 2017-10-13 11:04:18 +0200 | [diff] [blame] | 2602 | memcpy(&tsk->peer, dest, destlen); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2603 | goto exit; |
Erik Hugne | ea23931 | 2019-03-17 18:46:42 +0100 | [diff] [blame] | 2604 | } else if (dst->addrtype == TIPC_SERVICE_RANGE) { |
| 2605 | res = -EINVAL; |
| 2606 | goto exit; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2607 | } |
| 2608 | |
Parthasarathy Bhuvaragan | 99a2088 | 2016-11-01 14:02:48 +0100 | [diff] [blame] | 2609 | previous = sk->sk_state; |
Parthasarathy Bhuvaragan | 438adca | 2016-11-01 14:02:45 +0100 | [diff] [blame] | 2610 | |
| 2611 | switch (sk->sk_state) { |
| 2612 | case TIPC_OPEN: |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 2613 | /* Send a 'SYN-' to destination */ |
| 2614 | m.msg_name = dest; |
| 2615 | m.msg_namelen = destlen; |
| 2616 | |
| 2617 | /* If connect is in non-blocking case, set MSG_DONTWAIT to |
| 2618 | * indicate send_msg() is never blocked. |
| 2619 | */ |
| 2620 | if (!timeout) |
| 2621 | m.msg_flags = MSG_DONTWAIT; |
| 2622 | |
Ying Xue | 39a0295f | 2015-03-02 15:37:47 +0800 | [diff] [blame] | 2623 | res = __tipc_sendmsg(sock, &m, 0); |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 2624 | if ((res < 0) && (res != -EWOULDBLOCK)) |
| 2625 | goto exit; |
| 2626 | |
Parthasarathy Bhuvaragan | 99a2088 | 2016-11-01 14:02:48 +0100 | [diff] [blame] | 2627 | /* Just entered TIPC_CONNECTING state; the only |
Ying Xue | 584d24b | 2012-11-29 18:51:19 -0500 | [diff] [blame] | 2628 | * difference is that return value in non-blocking |
| 2629 | * case is EINPROGRESS, rather than EALREADY. |
| 2630 | */ |
| 2631 | res = -EINPROGRESS; |
Miaohe Lin | 7f8901b | 2020-08-18 08:07:13 -0400 | [diff] [blame] | 2632 | fallthrough; |
Parthasarathy Bhuvaragan | 99a2088 | 2016-11-01 14:02:48 +0100 | [diff] [blame] | 2633 | case TIPC_CONNECTING: |
| 2634 | if (!timeout) { |
| 2635 | if (previous == TIPC_CONNECTING) |
| 2636 | res = -EALREADY; |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 2637 | goto exit; |
Parthasarathy Bhuvaragan | 99a2088 | 2016-11-01 14:02:48 +0100 | [diff] [blame] | 2638 | } |
Ying Xue | 78eb3a5 | 2014-01-17 09:50:03 +0800 | [diff] [blame] | 2639 | timeout = msecs_to_jiffies(timeout); |
| 2640 | /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */ |
| 2641 | res = tipc_wait_for_connect(sock, &timeout); |
Parthasarathy Bhuvaragan | f40acba | 2016-11-01 14:02:49 +0100 | [diff] [blame] | 2642 | break; |
| 2643 | case TIPC_ESTABLISHED: |
Parthasarathy Bhuvaragan | 99a2088 | 2016-11-01 14:02:48 +0100 | [diff] [blame] | 2644 | res = -EISCONN; |
Parthasarathy Bhuvaragan | f40acba | 2016-11-01 14:02:49 +0100 | [diff] [blame] | 2645 | break; |
| 2646 | default: |
Parthasarathy Bhuvaragan | 99a2088 | 2016-11-01 14:02:48 +0100 | [diff] [blame] | 2647 | res = -EINVAL; |
Parthasarathy Bhuvaragan | f40acba | 2016-11-01 14:02:49 +0100 | [diff] [blame] | 2648 | } |
Parthasarathy Bhuvaragan | 99a2088 | 2016-11-01 14:02:48 +0100 | [diff] [blame] | 2649 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2650 | exit: |
| 2651 | release_sock(sk); |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 2652 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2653 | } |
| 2654 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2655 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2656 | * tipc_listen - allow socket to listen for incoming connections |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2657 | * @sock: socket structure |
| 2658 | * @len: (unused) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2659 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 2660 | * Return: 0 on success, errno otherwise |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2661 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2662 | static int tipc_listen(struct socket *sock, int len) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2663 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2664 | struct sock *sk = sock->sk; |
| 2665 | int res; |
| 2666 | |
| 2667 | lock_sock(sk); |
Parthasarathy Bhuvaragan | 0c288c8 | 2016-11-01 14:02:43 +0100 | [diff] [blame] | 2668 | res = tipc_set_sk_state(sk, TIPC_LISTEN); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2669 | release_sock(sk); |
Parthasarathy Bhuvaragan | 0c288c8 | 2016-11-01 14:02:43 +0100 | [diff] [blame] | 2670 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2671 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2672 | } |
| 2673 | |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 2674 | static int tipc_wait_for_accept(struct socket *sock, long timeo) |
| 2675 | { |
| 2676 | struct sock *sk = sock->sk; |
Hoang Le | d237a7f | 2021-07-23 09:25:34 +0700 | [diff] [blame] | 2677 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 2678 | int err; |
| 2679 | |
| 2680 | /* True wake-one mechanism for incoming connections: only |
| 2681 | * one process gets woken up, not the 'whole herd'. |
| 2682 | * Since we do not 'race & poll' for established sockets |
| 2683 | * anymore, the common case will execute the loop only once. |
| 2684 | */ |
| 2685 | for (;;) { |
Ying Xue | fe8e464 | 2014-03-06 14:40:18 +0100 | [diff] [blame] | 2686 | if (timeo && skb_queue_empty(&sk->sk_receive_queue)) { |
Hoang Le | d237a7f | 2021-07-23 09:25:34 +0700 | [diff] [blame] | 2687 | add_wait_queue(sk_sleep(sk), &wait); |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 2688 | release_sock(sk); |
Hoang Le | d237a7f | 2021-07-23 09:25:34 +0700 | [diff] [blame] | 2689 | timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo); |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 2690 | lock_sock(sk); |
Hoang Le | d237a7f | 2021-07-23 09:25:34 +0700 | [diff] [blame] | 2691 | remove_wait_queue(sk_sleep(sk), &wait); |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 2692 | } |
| 2693 | err = 0; |
| 2694 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 2695 | break; |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 2696 | err = -EAGAIN; |
| 2697 | if (!timeo) |
| 2698 | break; |
Erik Hugne | 143fe22 | 2015-03-09 10:43:42 +0100 | [diff] [blame] | 2699 | err = sock_intr_errno(timeo); |
| 2700 | if (signal_pending(current)) |
| 2701 | break; |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 2702 | } |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 2703 | return err; |
| 2704 | } |
| 2705 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2706 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2707 | * tipc_accept - wait for connection request |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2708 | * @sock: listening socket |
Andrew Lunn | d814120 | 2020-07-13 01:15:14 +0200 | [diff] [blame] | 2709 | * @new_sock: new socket that is to be connected |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2710 | * @flags: file-related flags associated with socket |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 2711 | * @kern: caused by kernel or by userspace? |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2712 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 2713 | * Return: 0 on success, errno otherwise |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2714 | */ |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 2715 | static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags, |
| 2716 | bool kern) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2717 | { |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2718 | struct sock *new_sk, *sk = sock->sk; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2719 | struct tipc_sock *new_tsock; |
Xin Long | f8dd60d | 2021-07-22 12:05:41 -0400 | [diff] [blame] | 2720 | struct msghdr m = {NULL,}; |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2721 | struct tipc_msg *msg; |
Xin Long | f8dd60d | 2021-07-22 12:05:41 -0400 | [diff] [blame] | 2722 | struct sk_buff *buf; |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 2723 | long timeo; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2724 | int res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2725 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2726 | lock_sock(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2727 | |
Parthasarathy Bhuvaragan | 0c288c8 | 2016-11-01 14:02:43 +0100 | [diff] [blame] | 2728 | if (sk->sk_state != TIPC_LISTEN) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2729 | res = -EINVAL; |
| 2730 | goto exit; |
| 2731 | } |
Ying Xue | 6398e23 | 2014-01-17 09:50:04 +0800 | [diff] [blame] | 2732 | timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK); |
| 2733 | res = tipc_wait_for_accept(sock, timeo); |
| 2734 | if (res) |
| 2735 | goto exit; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2736 | |
| 2737 | buf = skb_peek(&sk->sk_receive_queue); |
| 2738 | |
David Howells | cdfbabf | 2017-03-09 08:09:05 +0000 | [diff] [blame] | 2739 | res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2740 | if (res) |
| 2741 | goto exit; |
Stephen Smalley | fdd75ea | 2015-07-07 09:43:45 -0400 | [diff] [blame] | 2742 | security_sk_clone(sock->sk, new_sock->sk); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2743 | |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2744 | new_sk = new_sock->sk; |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2745 | new_tsock = tipc_sk(new_sk); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2746 | msg = buf_msg(buf); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2747 | |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2748 | /* we lock on new_sk; but lockdep sees the lock on sk */ |
| 2749 | lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2750 | |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2751 | /* |
| 2752 | * Reject any stray messages received by new socket |
| 2753 | * before the socket lock was taken (very, very unlikely) |
| 2754 | */ |
Tuong Lien | 49afb80 | 2020-01-08 09:18:15 +0700 | [diff] [blame] | 2755 | tsk_rej_rx_queue(new_sk, TIPC_ERR_NO_PORT); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2756 | |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2757 | /* Connect new socket to it's peer */ |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2758 | tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2759 | |
Christoph Hellwig | 095ae61 | 2020-05-28 07:12:36 +0200 | [diff] [blame] | 2760 | tsk_set_importance(new_sk, msg_importance(msg)); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2761 | if (msg_named(msg)) { |
Jon Maloy | 14623e0 | 2021-06-02 13:44:24 -0400 | [diff] [blame] | 2762 | new_tsock->conn_addrtype = TIPC_SERVICE_ADDR; |
| 2763 | msg_set_nametype(&new_tsock->phdr, msg_nametype(msg)); |
| 2764 | msg_set_nameinst(&new_tsock->phdr, msg_nameinst(msg)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2765 | } |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2766 | |
| 2767 | /* |
Xin Long | f8dd60d | 2021-07-22 12:05:41 -0400 | [diff] [blame] | 2768 | * Respond to 'SYN-' by discarding it & returning 'ACK'. |
| 2769 | * Respond to 'SYN+' by queuing it on new socket & returning 'ACK'. |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2770 | */ |
| 2771 | if (!msg_data_sz(msg)) { |
Jon Paul Maloy | 2e84c60 | 2014-08-22 18:09:18 -0400 | [diff] [blame] | 2772 | tsk_advance_rx_queue(sk); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2773 | } else { |
| 2774 | __skb_dequeue(&sk->sk_receive_queue); |
| 2775 | __skb_queue_head(&new_sk->sk_receive_queue, buf); |
Ying Xue | aba79f3 | 2013-01-20 23:30:09 +0100 | [diff] [blame] | 2776 | skb_set_owner_r(buf, new_sk); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2777 | } |
Xin Long | f8dd60d | 2021-07-22 12:05:41 -0400 | [diff] [blame] | 2778 | __tipc_sendstream(new_sock, &m, 0); |
Paul Gortmaker | 0fef8f2 | 2012-12-04 11:01:55 -0500 | [diff] [blame] | 2779 | release_sock(new_sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2780 | exit: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2781 | release_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2782 | return res; |
| 2783 | } |
| 2784 | |
| 2785 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2786 | * tipc_shutdown - shutdown socket connection |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2787 | * @sock: socket structure |
Allan Stephens | e247a8f | 2008-03-06 15:05:38 -0800 | [diff] [blame] | 2788 | * @how: direction to close (must be SHUT_RDWR) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2789 | * |
| 2790 | * Terminates connection (if necessary), then purges socket's receive queue. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 2791 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 2792 | * Return: 0 on success, errno otherwise |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2793 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 2794 | static int tipc_shutdown(struct socket *sock, int how) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2795 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2796 | struct sock *sk = sock->sk; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2797 | int res; |
| 2798 | |
Allan Stephens | e247a8f | 2008-03-06 15:05:38 -0800 | [diff] [blame] | 2799 | if (how != SHUT_RDWR) |
| 2800 | return -EINVAL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2801 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2802 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2803 | |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 2804 | trace_tipc_sk_shutdown(sk, NULL, TIPC_DUMP_ALL, " "); |
Parthasarathy Bhuvaragan | 6f00089 | 2016-11-01 14:02:47 +0100 | [diff] [blame] | 2805 | __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN); |
Tetsuo Handa | a4b5cc9 | 2020-09-05 15:14:47 +0900 | [diff] [blame] | 2806 | sk->sk_shutdown = SHUTDOWN_MASK; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2807 | |
Parthasarathy Bhuvaragan | 6f00089 | 2016-11-01 14:02:47 +0100 | [diff] [blame] | 2808 | if (sk->sk_state == TIPC_DISCONNECTING) { |
Ying Xue | 7503115 | 2012-10-29 09:38:15 -0400 | [diff] [blame] | 2809 | /* Discard any unreceived messages */ |
Ying Xue | 57467e5 | 2013-01-20 23:30:08 +0100 | [diff] [blame] | 2810 | __skb_queue_purge(&sk->sk_receive_queue); |
Ying Xue | 7503115 | 2012-10-29 09:38:15 -0400 | [diff] [blame] | 2811 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2812 | res = 0; |
Parthasarathy Bhuvaragan | 6f00089 | 2016-11-01 14:02:47 +0100 | [diff] [blame] | 2813 | } else { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2814 | res = -ENOTCONN; |
| 2815 | } |
Tetsuo Handa | 2a63866 | 2020-09-02 22:44:16 +0900 | [diff] [blame] | 2816 | /* Wake up anyone sleeping in poll. */ |
| 2817 | sk->sk_state_change(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2818 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 2819 | release_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 2820 | return res; |
| 2821 | } |
| 2822 | |
Jon Maloy | afe8792 | 2018-09-28 20:23:19 +0200 | [diff] [blame] | 2823 | static void tipc_sk_check_probing_state(struct sock *sk, |
| 2824 | struct sk_buff_head *list) |
| 2825 | { |
| 2826 | struct tipc_sock *tsk = tipc_sk(sk); |
| 2827 | u32 pnode = tsk_peer_node(tsk); |
| 2828 | u32 pport = tsk_peer_port(tsk); |
| 2829 | u32 self = tsk_own_node(tsk); |
| 2830 | u32 oport = tsk->portid; |
| 2831 | struct sk_buff *skb; |
| 2832 | |
| 2833 | if (tsk->probe_unacked) { |
| 2834 | tipc_set_sk_state(sk, TIPC_DISCONNECTING); |
| 2835 | sk->sk_err = ECONNABORTED; |
| 2836 | tipc_node_remove_conn(sock_net(sk), pnode, pport); |
| 2837 | sk->sk_state_change(sk); |
| 2838 | return; |
| 2839 | } |
| 2840 | /* Prepare new probe */ |
| 2841 | skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, 0, |
| 2842 | pnode, self, pport, oport, TIPC_OK); |
| 2843 | if (skb) |
| 2844 | __skb_queue_tail(list, skb); |
| 2845 | tsk->probe_unacked = true; |
| 2846 | sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV); |
| 2847 | } |
| 2848 | |
Tung Nguyen | 6787927 | 2018-09-28 20:23:22 +0200 | [diff] [blame] | 2849 | static void tipc_sk_retry_connect(struct sock *sk, struct sk_buff_head *list) |
| 2850 | { |
| 2851 | struct tipc_sock *tsk = tipc_sk(sk); |
| 2852 | |
| 2853 | /* Try again later if dest link is congested */ |
| 2854 | if (tsk->cong_link_cnt) { |
| 2855 | sk_reset_timer(sk, &sk->sk_timer, msecs_to_jiffies(100)); |
| 2856 | return; |
| 2857 | } |
| 2858 | /* Prepare SYN for retransmit */ |
| 2859 | tipc_msg_skb_clone(&sk->sk_write_queue, list); |
| 2860 | } |
| 2861 | |
Kees Cook | 31b102b | 2017-10-30 14:06:45 -0700 | [diff] [blame] | 2862 | static void tipc_sk_timeout(struct timer_list *t) |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2863 | { |
Kees Cook | 31b102b | 2017-10-30 14:06:45 -0700 | [diff] [blame] | 2864 | struct sock *sk = from_timer(sk, t, sk_timer); |
| 2865 | struct tipc_sock *tsk = tipc_sk(sk); |
Jon Maloy | afe8792 | 2018-09-28 20:23:19 +0200 | [diff] [blame] | 2866 | u32 pnode = tsk_peer_node(tsk); |
| 2867 | struct sk_buff_head list; |
Tung Nguyen | 6787927 | 2018-09-28 20:23:22 +0200 | [diff] [blame] | 2868 | int rc = 0; |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2869 | |
Jon Maloy | e654f9f | 2019-08-15 16:42:50 +0200 | [diff] [blame] | 2870 | __skb_queue_head_init(&list); |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2871 | bh_lock_sock(sk); |
Jon Maloy | 0d5fcebf | 2017-10-20 11:21:32 +0200 | [diff] [blame] | 2872 | |
| 2873 | /* Try again later if socket is busy */ |
| 2874 | if (sock_owned_by_user(sk)) { |
| 2875 | sk_reset_timer(sk, &sk->sk_timer, jiffies + HZ / 20); |
Jon Maloy | afe8792 | 2018-09-28 20:23:19 +0200 | [diff] [blame] | 2876 | bh_unlock_sock(sk); |
Tung Nguyen | 91a4a3e | 2019-11-28 10:10:06 +0700 | [diff] [blame] | 2877 | sock_put(sk); |
Jon Maloy | afe8792 | 2018-09-28 20:23:19 +0200 | [diff] [blame] | 2878 | return; |
Jon Paul Maloy | 6c9808c | 2014-08-22 18:09:16 -0400 | [diff] [blame] | 2879 | } |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2880 | |
Jon Maloy | afe8792 | 2018-09-28 20:23:19 +0200 | [diff] [blame] | 2881 | if (sk->sk_state == TIPC_ESTABLISHED) |
| 2882 | tipc_sk_check_probing_state(sk, &list); |
Tung Nguyen | 6787927 | 2018-09-28 20:23:22 +0200 | [diff] [blame] | 2883 | else if (sk->sk_state == TIPC_CONNECTING) |
| 2884 | tipc_sk_retry_connect(sk, &list); |
Jon Maloy | afe8792 | 2018-09-28 20:23:19 +0200 | [diff] [blame] | 2885 | |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2886 | bh_unlock_sock(sk); |
Jon Maloy | afe8792 | 2018-09-28 20:23:19 +0200 | [diff] [blame] | 2887 | |
| 2888 | if (!skb_queue_empty(&list)) |
Tung Nguyen | 6787927 | 2018-09-28 20:23:22 +0200 | [diff] [blame] | 2889 | rc = tipc_node_xmit(sock_net(sk), &list, pnode, tsk->portid); |
Jon Maloy | afe8792 | 2018-09-28 20:23:19 +0200 | [diff] [blame] | 2890 | |
Tung Nguyen | 6787927 | 2018-09-28 20:23:22 +0200 | [diff] [blame] | 2891 | /* SYN messages may cause link congestion */ |
| 2892 | if (rc == -ELINKCONG) { |
| 2893 | tipc_dest_push(&tsk->cong_links, pnode, 0); |
| 2894 | tsk->cong_link_cnt = 1; |
| 2895 | } |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2896 | sock_put(sk); |
Jon Paul Maloy | 5728901 | 2014-08-22 18:09:09 -0400 | [diff] [blame] | 2897 | } |
| 2898 | |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 2899 | static int tipc_sk_publish(struct tipc_sock *tsk, struct tipc_uaddr *ua) |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2900 | { |
Parthasarathy Bhuvaragan | d6fb7e9 | 2016-11-01 14:02:40 +0100 | [diff] [blame] | 2901 | struct sock *sk = &tsk->sk; |
| 2902 | struct net *net = sock_net(sk); |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 2903 | struct tipc_socket_addr skaddr; |
| 2904 | struct publication *p; |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2905 | u32 key; |
| 2906 | |
Parthasarathy Bhuvaragan | d6fb7e9 | 2016-11-01 14:02:40 +0100 | [diff] [blame] | 2907 | if (tipc_sk_connected(sk)) |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2908 | return -EINVAL; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2909 | key = tsk->portid + tsk->pub_count + 1; |
| 2910 | if (key == tsk->portid) |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2911 | return -EADDRINUSE; |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 2912 | skaddr.ref = tsk->portid; |
| 2913 | skaddr.node = tipc_own_addr(net); |
| 2914 | p = tipc_nametbl_publish(net, ua, &skaddr, key); |
| 2915 | if (unlikely(!p)) |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2916 | return -EINVAL; |
| 2917 | |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 2918 | list_add(&p->binding_sock, &tsk->publications); |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 2919 | tsk->pub_count++; |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 2920 | tsk->published = true; |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2921 | return 0; |
| 2922 | } |
| 2923 | |
Jon Maloy | 2c98da0 | 2021-03-16 22:06:13 -0400 | [diff] [blame] | 2924 | static int tipc_sk_withdraw(struct tipc_sock *tsk, struct tipc_uaddr *ua) |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2925 | { |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 2926 | struct net *net = sock_net(&tsk->sk); |
Jon Maloy | 2c98da0 | 2021-03-16 22:06:13 -0400 | [diff] [blame] | 2927 | struct publication *safe, *p; |
| 2928 | struct tipc_uaddr _ua; |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2929 | int rc = -EINVAL; |
| 2930 | |
Jon Maloy | 998d390 | 2021-03-16 22:06:08 -0400 | [diff] [blame] | 2931 | list_for_each_entry_safe(p, safe, &tsk->publications, binding_sock) { |
Jon Maloy | 2c98da0 | 2021-03-16 22:06:13 -0400 | [diff] [blame] | 2932 | if (!ua) { |
| 2933 | tipc_uaddr(&_ua, TIPC_SERVICE_RANGE, p->scope, |
| 2934 | p->sr.type, p->sr.lower, p->sr.upper); |
| 2935 | tipc_nametbl_withdraw(net, &_ua, &p->sk, p->key); |
| 2936 | continue; |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2937 | } |
Jon Maloy | 2c98da0 | 2021-03-16 22:06:13 -0400 | [diff] [blame] | 2938 | /* Unbind specific publication */ |
| 2939 | if (p->scope != ua->scope) |
| 2940 | continue; |
| 2941 | if (p->sr.type != ua->sr.type) |
| 2942 | continue; |
| 2943 | if (p->sr.lower != ua->sr.lower) |
| 2944 | continue; |
| 2945 | if (p->sr.upper != ua->sr.upper) |
| 2946 | break; |
| 2947 | tipc_nametbl_withdraw(net, ua, &p->sk, p->key); |
| 2948 | rc = 0; |
| 2949 | break; |
| 2950 | } |
| 2951 | if (list_empty(&tsk->publications)) { |
| 2952 | tsk->published = 0; |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2953 | rc = 0; |
| 2954 | } |
Jon Paul Maloy | 0fc87aa | 2014-08-22 18:09:17 -0400 | [diff] [blame] | 2955 | return rc; |
| 2956 | } |
| 2957 | |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2958 | /* tipc_sk_reinit: set non-zero address in all existing sockets |
| 2959 | * when we go from standalone to network mode. |
| 2960 | */ |
Ying Xue | e05b31f | 2015-01-09 15:27:08 +0800 | [diff] [blame] | 2961 | void tipc_sk_reinit(struct net *net) |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2962 | { |
Ying Xue | e05b31f | 2015-01-09 15:27:08 +0800 | [diff] [blame] | 2963 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
Herbert Xu | 40f9f43 | 2017-02-11 19:26:46 +0800 | [diff] [blame] | 2964 | struct rhashtable_iter iter; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2965 | struct tipc_sock *tsk; |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2966 | struct tipc_msg *msg; |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 2967 | |
Herbert Xu | 40f9f43 | 2017-02-11 19:26:46 +0800 | [diff] [blame] | 2968 | rhashtable_walk_enter(&tn->sk_rht, &iter); |
| 2969 | |
| 2970 | do { |
Tom Herbert | 97a6ec4 | 2017-12-04 10:31:41 -0800 | [diff] [blame] | 2971 | rhashtable_walk_start(&iter); |
Herbert Xu | 40f9f43 | 2017-02-11 19:26:46 +0800 | [diff] [blame] | 2972 | |
| 2973 | while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) { |
Cong Wang | 15ef70e | 2018-12-10 11:49:55 -0800 | [diff] [blame] | 2974 | sock_hold(&tsk->sk); |
| 2975 | rhashtable_walk_stop(&iter); |
| 2976 | lock_sock(&tsk->sk); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2977 | msg = &tsk->phdr; |
Jon Maloy | 23fd3ea | 2018-03-22 20:42:49 +0100 | [diff] [blame] | 2978 | msg_set_prevnode(msg, tipc_own_addr(net)); |
| 2979 | msg_set_orignode(msg, tipc_own_addr(net)); |
Cong Wang | 15ef70e | 2018-12-10 11:49:55 -0800 | [diff] [blame] | 2980 | release_sock(&tsk->sk); |
| 2981 | rhashtable_walk_start(&iter); |
| 2982 | sock_put(&tsk->sk); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2983 | } |
Tom Herbert | 97a6ec4 | 2017-12-04 10:31:41 -0800 | [diff] [blame] | 2984 | |
Herbert Xu | 40f9f43 | 2017-02-11 19:26:46 +0800 | [diff] [blame] | 2985 | rhashtable_walk_stop(&iter); |
| 2986 | } while (tsk == ERR_PTR(-EAGAIN)); |
Cong Wang | bd583fe | 2018-08-23 16:19:44 -0700 | [diff] [blame] | 2987 | |
| 2988 | rhashtable_walk_exit(&iter); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2989 | } |
| 2990 | |
Ying Xue | e05b31f | 2015-01-09 15:27:08 +0800 | [diff] [blame] | 2991 | static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid) |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2992 | { |
Ying Xue | e05b31f | 2015-01-09 15:27:08 +0800 | [diff] [blame] | 2993 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2994 | struct tipc_sock *tsk; |
| 2995 | |
| 2996 | rcu_read_lock(); |
Taehee Yoo | ab81836 | 2019-11-22 08:15:19 +0000 | [diff] [blame] | 2997 | tsk = rhashtable_lookup(&tn->sk_rht, &portid, tsk_rht_params); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 2998 | if (tsk) |
| 2999 | sock_hold(&tsk->sk); |
| 3000 | rcu_read_unlock(); |
| 3001 | |
| 3002 | return tsk; |
| 3003 | } |
| 3004 | |
| 3005 | static int tipc_sk_insert(struct tipc_sock *tsk) |
| 3006 | { |
Ying Xue | e05b31f | 2015-01-09 15:27:08 +0800 | [diff] [blame] | 3007 | struct sock *sk = &tsk->sk; |
| 3008 | struct net *net = sock_net(sk); |
| 3009 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 3010 | u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1; |
| 3011 | u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT; |
| 3012 | |
| 3013 | while (remaining--) { |
| 3014 | portid++; |
| 3015 | if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT)) |
| 3016 | portid = TIPC_MIN_PORT; |
| 3017 | tsk->portid = portid; |
| 3018 | sock_hold(&tsk->sk); |
Herbert Xu | 6cca7289 | 2015-03-20 21:57:05 +1100 | [diff] [blame] | 3019 | if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node, |
| 3020 | tsk_rht_params)) |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 3021 | return 0; |
| 3022 | sock_put(&tsk->sk); |
| 3023 | } |
| 3024 | |
| 3025 | return -1; |
| 3026 | } |
| 3027 | |
| 3028 | static void tipc_sk_remove(struct tipc_sock *tsk) |
| 3029 | { |
| 3030 | struct sock *sk = &tsk->sk; |
Ying Xue | e05b31f | 2015-01-09 15:27:08 +0800 | [diff] [blame] | 3031 | struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 3032 | |
Herbert Xu | 6cca7289 | 2015-03-20 21:57:05 +1100 | [diff] [blame] | 3033 | if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) { |
Reshetova, Elena | 41c6d65 | 2017-06-30 13:08:01 +0300 | [diff] [blame] | 3034 | WARN_ON(refcount_read(&sk->sk_refcnt) == 1); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 3035 | __sock_put(sk); |
Jon Paul Maloy | 5a9ee0be | 2014-08-22 18:09:14 -0400 | [diff] [blame] | 3036 | } |
| 3037 | } |
| 3038 | |
Herbert Xu | 6cca7289 | 2015-03-20 21:57:05 +1100 | [diff] [blame] | 3039 | static const struct rhashtable_params tsk_rht_params = { |
| 3040 | .nelem_hint = 192, |
| 3041 | .head_offset = offsetof(struct tipc_sock, node), |
| 3042 | .key_offset = offsetof(struct tipc_sock, portid), |
| 3043 | .key_len = sizeof(u32), /* portid */ |
Herbert Xu | 6cca7289 | 2015-03-20 21:57:05 +1100 | [diff] [blame] | 3044 | .max_size = 1048576, |
| 3045 | .min_size = 256, |
Thomas Graf | b5e2c15 | 2015-03-24 20:42:19 +0000 | [diff] [blame] | 3046 | .automatic_shrinking = true, |
Herbert Xu | 6cca7289 | 2015-03-20 21:57:05 +1100 | [diff] [blame] | 3047 | }; |
| 3048 | |
Ying Xue | e05b31f | 2015-01-09 15:27:08 +0800 | [diff] [blame] | 3049 | int tipc_sk_rht_init(struct net *net) |
Jon Paul Maloy | 808d90f | 2014-08-22 18:09:19 -0400 | [diff] [blame] | 3050 | { |
Ying Xue | e05b31f | 2015-01-09 15:27:08 +0800 | [diff] [blame] | 3051 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
Jon Paul Maloy | 808d90f | 2014-08-22 18:09:19 -0400 | [diff] [blame] | 3052 | |
Herbert Xu | 6cca7289 | 2015-03-20 21:57:05 +1100 | [diff] [blame] | 3053 | return rhashtable_init(&tn->sk_rht, &tsk_rht_params); |
Jon Paul Maloy | 808d90f | 2014-08-22 18:09:19 -0400 | [diff] [blame] | 3054 | } |
| 3055 | |
Ying Xue | e05b31f | 2015-01-09 15:27:08 +0800 | [diff] [blame] | 3056 | void tipc_sk_rht_destroy(struct net *net) |
Jon Paul Maloy | 808d90f | 2014-08-22 18:09:19 -0400 | [diff] [blame] | 3057 | { |
Ying Xue | e05b31f | 2015-01-09 15:27:08 +0800 | [diff] [blame] | 3058 | struct tipc_net *tn = net_generic(net, tipc_net_id); |
| 3059 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 3060 | /* Wait for socket readers to complete */ |
| 3061 | synchronize_net(); |
Jon Paul Maloy | 808d90f | 2014-08-22 18:09:19 -0400 | [diff] [blame] | 3062 | |
Ying Xue | e05b31f | 2015-01-09 15:27:08 +0800 | [diff] [blame] | 3063 | rhashtable_destroy(&tn->sk_rht); |
Jon Paul Maloy | 808d90f | 2014-08-22 18:09:19 -0400 | [diff] [blame] | 3064 | } |
| 3065 | |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 3066 | static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq) |
| 3067 | { |
| 3068 | struct net *net = sock_net(&tsk->sk); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 3069 | struct tipc_group *grp = tsk->group; |
| 3070 | struct tipc_msg *hdr = &tsk->phdr; |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 3071 | struct tipc_uaddr ua; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 3072 | int rc; |
| 3073 | |
| 3074 | if (mreq->type < TIPC_RESERVED_TYPES) |
| 3075 | return -EACCES; |
Jon Maloy | 232d07b | 2018-01-08 21:03:30 +0100 | [diff] [blame] | 3076 | if (mreq->scope > TIPC_NODE_SCOPE) |
| 3077 | return -EINVAL; |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 3078 | if (mreq->scope != TIPC_NODE_SCOPE) |
| 3079 | mreq->scope = TIPC_CLUSTER_SCOPE; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 3080 | if (grp) |
| 3081 | return -EACCES; |
Jon Maloy | 60c2530 | 2018-01-17 16:42:46 +0100 | [diff] [blame] | 3082 | grp = tipc_group_create(net, tsk->portid, mreq, &tsk->group_is_open); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 3083 | if (!grp) |
| 3084 | return -ENOMEM; |
| 3085 | tsk->group = grp; |
| 3086 | msg_set_lookup_scope(hdr, mreq->scope); |
| 3087 | msg_set_nametype(hdr, mreq->type); |
| 3088 | msg_set_dest_droppable(hdr, true); |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 3089 | tipc_uaddr(&ua, TIPC_SERVICE_RANGE, mreq->scope, |
| 3090 | mreq->type, mreq->instance, mreq->instance); |
Jon Maloy | 6e44867 | 2021-03-16 22:06:20 -0400 | [diff] [blame] | 3091 | tipc_nametbl_build_group(net, grp, &ua); |
Jon Maloy | 50a3499 | 2021-03-16 22:06:11 -0400 | [diff] [blame] | 3092 | rc = tipc_sk_publish(tsk, &ua); |
Cong Wang | e233df0 | 2017-10-24 15:44:49 -0700 | [diff] [blame] | 3093 | if (rc) { |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 3094 | tipc_group_delete(net, grp); |
Cong Wang | e233df0 | 2017-10-24 15:44:49 -0700 | [diff] [blame] | 3095 | tsk->group = NULL; |
Jon Maloy | febafc8 | 2018-01-10 21:08:50 +0100 | [diff] [blame] | 3096 | return rc; |
Cong Wang | e233df0 | 2017-10-24 15:44:49 -0700 | [diff] [blame] | 3097 | } |
Jon Maloy | d12d2e1 | 2018-01-08 21:03:28 +0100 | [diff] [blame] | 3098 | /* Eliminate any risk that a broadcast overtakes sent JOINs */ |
Jon Maloy | 399574d | 2017-10-13 11:04:32 +0200 | [diff] [blame] | 3099 | tsk->mc_method.rcast = true; |
| 3100 | tsk->mc_method.mandatory = true; |
Jon Maloy | d12d2e1 | 2018-01-08 21:03:28 +0100 | [diff] [blame] | 3101 | tipc_group_join(net, grp, &tsk->sk.sk_rcvbuf); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 3102 | return rc; |
| 3103 | } |
| 3104 | |
| 3105 | static int tipc_sk_leave(struct tipc_sock *tsk) |
| 3106 | { |
| 3107 | struct net *net = sock_net(&tsk->sk); |
| 3108 | struct tipc_group *grp = tsk->group; |
Jon Maloy | 2c98da0 | 2021-03-16 22:06:13 -0400 | [diff] [blame] | 3109 | struct tipc_uaddr ua; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 3110 | int scope; |
| 3111 | |
| 3112 | if (!grp) |
| 3113 | return -EINVAL; |
Jon Maloy | 2c98da0 | 2021-03-16 22:06:13 -0400 | [diff] [blame] | 3114 | ua.addrtype = TIPC_SERVICE_RANGE; |
| 3115 | tipc_group_self(grp, &ua.sr, &scope); |
| 3116 | ua.scope = scope; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 3117 | tipc_group_delete(net, grp); |
| 3118 | tsk->group = NULL; |
Jon Maloy | 2c98da0 | 2021-03-16 22:06:13 -0400 | [diff] [blame] | 3119 | tipc_sk_withdraw(tsk, &ua); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 3120 | return 0; |
| 3121 | } |
| 3122 | |
Jon Paul Maloy | 808d90f | 2014-08-22 18:09:19 -0400 | [diff] [blame] | 3123 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 3124 | * tipc_setsockopt - set socket option |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3125 | * @sock: socket structure |
| 3126 | * @lvl: option level |
| 3127 | * @opt: option identifier |
| 3128 | * @ov: pointer to new option value |
| 3129 | * @ol: length of option value |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3130 | * |
| 3131 | * For stream sockets only, accepts and ignores all IPPROTO_TCP options |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3132 | * (to ease compatibility). |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3133 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 3134 | * Return: 0 on success, errno otherwise |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3135 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 3136 | static int tipc_setsockopt(struct socket *sock, int lvl, int opt, |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3137 | sockptr_t ov, unsigned int ol) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3138 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 3139 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 3140 | struct tipc_sock *tsk = tipc_sk(sk); |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 3141 | struct tipc_group_req mreq; |
Jon Paul Maloy | 01fd12b | 2017-01-18 13:50:53 -0500 | [diff] [blame] | 3142 | u32 value = 0; |
Dan Carpenter | a08ef47 | 2017-01-24 12:49:35 +0300 | [diff] [blame] | 3143 | int res = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3144 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3145 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
| 3146 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3147 | if (lvl != SOL_TIPC) |
| 3148 | return -ENOPROTOOPT; |
Jon Paul Maloy | 01fd12b | 2017-01-18 13:50:53 -0500 | [diff] [blame] | 3149 | |
| 3150 | switch (opt) { |
| 3151 | case TIPC_IMPORTANCE: |
| 3152 | case TIPC_SRC_DROPPABLE: |
| 3153 | case TIPC_DEST_DROPPABLE: |
| 3154 | case TIPC_CONN_TIMEOUT: |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 3155 | case TIPC_NODELAY: |
Jon Paul Maloy | 01fd12b | 2017-01-18 13:50:53 -0500 | [diff] [blame] | 3156 | if (ol < sizeof(value)) |
| 3157 | return -EINVAL; |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3158 | if (copy_from_sockptr(&value, ov, sizeof(u32))) |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 3159 | return -EFAULT; |
| 3160 | break; |
| 3161 | case TIPC_GROUP_JOIN: |
| 3162 | if (ol < sizeof(mreq)) |
| 3163 | return -EINVAL; |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3164 | if (copy_from_sockptr(&mreq, ov, sizeof(mreq))) |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 3165 | return -EFAULT; |
Jon Paul Maloy | 01fd12b | 2017-01-18 13:50:53 -0500 | [diff] [blame] | 3166 | break; |
| 3167 | default: |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 3168 | if (!sockptr_is_null(ov) || ol) |
Jon Paul Maloy | 01fd12b | 2017-01-18 13:50:53 -0500 | [diff] [blame] | 3169 | return -EINVAL; |
| 3170 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3171 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 3172 | lock_sock(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3173 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3174 | switch (opt) { |
| 3175 | case TIPC_IMPORTANCE: |
Christoph Hellwig | 095ae61 | 2020-05-28 07:12:36 +0200 | [diff] [blame] | 3176 | res = tsk_set_importance(sk, value); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3177 | break; |
| 3178 | case TIPC_SRC_DROPPABLE: |
| 3179 | if (sock->type != SOCK_STREAM) |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 3180 | tsk_set_unreliable(tsk, value); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3181 | else |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3182 | res = -ENOPROTOOPT; |
| 3183 | break; |
| 3184 | case TIPC_DEST_DROPPABLE: |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 3185 | tsk_set_unreturnable(tsk, value); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3186 | break; |
| 3187 | case TIPC_CONN_TIMEOUT: |
Allan Stephens | a0f40f0 | 2011-05-26 13:44:34 -0400 | [diff] [blame] | 3188 | tipc_sk(sk)->conn_timeout = value; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3189 | break; |
Jon Paul Maloy | 01fd12b | 2017-01-18 13:50:53 -0500 | [diff] [blame] | 3190 | case TIPC_MCAST_BROADCAST: |
| 3191 | tsk->mc_method.rcast = false; |
| 3192 | tsk->mc_method.mandatory = true; |
| 3193 | break; |
| 3194 | case TIPC_MCAST_REPLICAST: |
| 3195 | tsk->mc_method.rcast = true; |
| 3196 | tsk->mc_method.mandatory = true; |
| 3197 | break; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 3198 | case TIPC_GROUP_JOIN: |
| 3199 | res = tipc_sk_join(tsk, &mreq); |
| 3200 | break; |
| 3201 | case TIPC_GROUP_LEAVE: |
| 3202 | res = tipc_sk_leave(tsk); |
| 3203 | break; |
Jon Maloy | c0bceb9 | 2019-10-30 14:00:41 +0100 | [diff] [blame] | 3204 | case TIPC_NODELAY: |
| 3205 | tsk->nodelay = !!value; |
| 3206 | tsk_set_nagle(tsk); |
| 3207 | break; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3208 | default: |
| 3209 | res = -EINVAL; |
| 3210 | } |
| 3211 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 3212 | release_sock(sk); |
| 3213 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3214 | return res; |
| 3215 | } |
| 3216 | |
| 3217 | /** |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 3218 | * tipc_getsockopt - get socket option |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3219 | * @sock: socket structure |
| 3220 | * @lvl: option level |
| 3221 | * @opt: option identifier |
| 3222 | * @ov: receptacle for option value |
| 3223 | * @ol: receptacle for length of option value |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3224 | * |
| 3225 | * For stream sockets only, returns 0 length result for all IPPROTO_TCP options |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3226 | * (to ease compatibility). |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3227 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 3228 | * Return: 0 on success, errno otherwise |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3229 | */ |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 3230 | static int tipc_getsockopt(struct socket *sock, int lvl, int opt, |
| 3231 | char __user *ov, int __user *ol) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3232 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 3233 | struct sock *sk = sock->sk; |
Jon Paul Maloy | 58ed944 | 2014-03-12 11:31:12 -0400 | [diff] [blame] | 3234 | struct tipc_sock *tsk = tipc_sk(sk); |
Jon Maloy | b6f88d9 | 2020-11-25 13:29:15 -0500 | [diff] [blame] | 3235 | struct tipc_service_range seq; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 3236 | int len, scope; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3237 | u32 value; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3238 | int res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3239 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3240 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
| 3241 | return put_user(0, ol); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3242 | if (lvl != SOL_TIPC) |
| 3243 | return -ENOPROTOOPT; |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 3244 | res = get_user(len, ol); |
| 3245 | if (res) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3246 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3247 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 3248 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3249 | |
| 3250 | switch (opt) { |
| 3251 | case TIPC_IMPORTANCE: |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 3252 | value = tsk_importance(tsk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3253 | break; |
| 3254 | case TIPC_SRC_DROPPABLE: |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 3255 | value = tsk_unreliable(tsk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3256 | break; |
| 3257 | case TIPC_DEST_DROPPABLE: |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 3258 | value = tsk_unreturnable(tsk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3259 | break; |
| 3260 | case TIPC_CONN_TIMEOUT: |
Jon Paul Maloy | 301bae5 | 2014-08-22 18:09:20 -0400 | [diff] [blame] | 3261 | value = tsk->conn_timeout; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 3262 | /* no need to set "res", since already 0 at this point */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3263 | break; |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 3264 | case TIPC_NODE_RECVQ_DEPTH: |
Ying Xue | 9da3d47 | 2012-11-27 06:15:27 -0500 | [diff] [blame] | 3265 | value = 0; /* was tipc_queue_size, now obsolete */ |
oscar.medina@motorola.com | 6650613 | 2009-06-30 03:25:39 +0000 | [diff] [blame] | 3266 | break; |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 3267 | case TIPC_SOCK_RECVQ_DEPTH: |
oscar.medina@motorola.com | 6650613 | 2009-06-30 03:25:39 +0000 | [diff] [blame] | 3268 | value = skb_queue_len(&sk->sk_receive_queue); |
| 3269 | break; |
Tung Nguyen | 42e5425 | 2019-04-18 21:02:19 +0700 | [diff] [blame] | 3270 | case TIPC_SOCK_RECVQ_USED: |
| 3271 | value = sk_rmem_alloc_get(sk); |
| 3272 | break; |
Jon Maloy | 75da216 | 2017-10-13 11:04:23 +0200 | [diff] [blame] | 3273 | case TIPC_GROUP_JOIN: |
| 3274 | seq.type = 0; |
| 3275 | if (tsk->group) |
| 3276 | tipc_group_self(tsk->group, &seq, &scope); |
| 3277 | value = seq.type; |
| 3278 | break; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3279 | default: |
| 3280 | res = -EINVAL; |
| 3281 | } |
| 3282 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 3283 | release_sock(sk); |
| 3284 | |
Paul Gortmaker | 25860c3 | 2010-12-31 18:59:31 +0000 | [diff] [blame] | 3285 | if (res) |
| 3286 | return res; /* "get" failed */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3287 | |
Paul Gortmaker | 25860c3 | 2010-12-31 18:59:31 +0000 | [diff] [blame] | 3288 | if (len < sizeof(value)) |
| 3289 | return -EINVAL; |
| 3290 | |
| 3291 | if (copy_to_user(ov, &value, sizeof(value))) |
| 3292 | return -EFAULT; |
| 3293 | |
| 3294 | return put_user(sizeof(value), ol); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3295 | } |
| 3296 | |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 3297 | static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 3298 | { |
Jon Maloy | 3e5cf36 | 2018-04-25 19:29:36 +0200 | [diff] [blame] | 3299 | struct net *net = sock_net(sock->sk); |
| 3300 | struct tipc_sioc_nodeid_req nr = {0}; |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 3301 | struct tipc_sioc_ln_req lnr; |
| 3302 | void __user *argp = (void __user *)arg; |
| 3303 | |
| 3304 | switch (cmd) { |
| 3305 | case SIOCGETLINKNAME: |
| 3306 | if (copy_from_user(&lnr, argp, sizeof(lnr))) |
| 3307 | return -EFAULT; |
Jon Maloy | 3e5cf36 | 2018-04-25 19:29:36 +0200 | [diff] [blame] | 3308 | if (!tipc_node_get_linkname(net, |
Ying Xue | f2f9800 | 2015-01-09 15:27:05 +0800 | [diff] [blame] | 3309 | lnr.bearer_id & 0xffff, lnr.peer, |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 3310 | lnr.linkname, TIPC_MAX_LINK_NAME)) { |
| 3311 | if (copy_to_user(argp, &lnr, sizeof(lnr))) |
| 3312 | return -EFAULT; |
| 3313 | return 0; |
| 3314 | } |
| 3315 | return -EADDRNOTAVAIL; |
Jon Maloy | 3e5cf36 | 2018-04-25 19:29:36 +0200 | [diff] [blame] | 3316 | case SIOCGETNODEID: |
| 3317 | if (copy_from_user(&nr, argp, sizeof(nr))) |
| 3318 | return -EFAULT; |
| 3319 | if (!tipc_node_get_id(net, nr.peer, nr.node_id)) |
| 3320 | return -EADDRNOTAVAIL; |
| 3321 | if (copy_to_user(argp, &nr, sizeof(nr))) |
| 3322 | return -EFAULT; |
| 3323 | return 0; |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 3324 | default: |
| 3325 | return -ENOIOCTLCMD; |
| 3326 | } |
| 3327 | } |
| 3328 | |
Erik Hugne | 70b0375 | 2017-03-29 11:22:16 +0200 | [diff] [blame] | 3329 | static int tipc_socketpair(struct socket *sock1, struct socket *sock2) |
| 3330 | { |
| 3331 | struct tipc_sock *tsk2 = tipc_sk(sock2->sk); |
| 3332 | struct tipc_sock *tsk1 = tipc_sk(sock1->sk); |
Erik Hugne | 66bc1e8 | 2017-03-29 11:22:17 +0200 | [diff] [blame] | 3333 | u32 onode = tipc_own_addr(sock_net(sock1->sk)); |
Erik Hugne | 70b0375 | 2017-03-29 11:22:16 +0200 | [diff] [blame] | 3334 | |
Erik Hugne | 66bc1e8 | 2017-03-29 11:22:17 +0200 | [diff] [blame] | 3335 | tsk1->peer.family = AF_TIPC; |
Jon Maloy | b6f88d9 | 2020-11-25 13:29:15 -0500 | [diff] [blame] | 3336 | tsk1->peer.addrtype = TIPC_SOCKET_ADDR; |
Erik Hugne | 66bc1e8 | 2017-03-29 11:22:17 +0200 | [diff] [blame] | 3337 | tsk1->peer.scope = TIPC_NODE_SCOPE; |
| 3338 | tsk1->peer.addr.id.ref = tsk2->portid; |
| 3339 | tsk1->peer.addr.id.node = onode; |
| 3340 | tsk2->peer.family = AF_TIPC; |
Jon Maloy | b6f88d9 | 2020-11-25 13:29:15 -0500 | [diff] [blame] | 3341 | tsk2->peer.addrtype = TIPC_SOCKET_ADDR; |
Erik Hugne | 66bc1e8 | 2017-03-29 11:22:17 +0200 | [diff] [blame] | 3342 | tsk2->peer.scope = TIPC_NODE_SCOPE; |
| 3343 | tsk2->peer.addr.id.ref = tsk1->portid; |
| 3344 | tsk2->peer.addr.id.node = onode; |
| 3345 | |
| 3346 | tipc_sk_finish_conn(tsk1, tsk2->portid, onode); |
| 3347 | tipc_sk_finish_conn(tsk2, tsk1->portid, onode); |
Erik Hugne | 70b0375 | 2017-03-29 11:22:16 +0200 | [diff] [blame] | 3348 | return 0; |
| 3349 | } |
| 3350 | |
Ben Hutchings | ae86b9e | 2012-07-10 10:55:35 +0000 | [diff] [blame] | 3351 | /* Protocol switches for the various types of TIPC sockets */ |
| 3352 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 3353 | static const struct proto_ops msg_ops = { |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 3354 | .owner = THIS_MODULE, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3355 | .family = AF_TIPC, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 3356 | .release = tipc_release, |
| 3357 | .bind = tipc_bind, |
| 3358 | .connect = tipc_connect, |
Erik Hugne | 66bc1e8 | 2017-03-29 11:22:17 +0200 | [diff] [blame] | 3359 | .socketpair = tipc_socketpair, |
Ying Xue | 245f3d3 | 2011-07-06 06:01:13 -0400 | [diff] [blame] | 3360 | .accept = sock_no_accept, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 3361 | .getname = tipc_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 3362 | .poll = tipc_poll, |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 3363 | .ioctl = tipc_ioctl, |
Ying Xue | 245f3d3 | 2011-07-06 06:01:13 -0400 | [diff] [blame] | 3364 | .listen = sock_no_listen, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 3365 | .shutdown = tipc_shutdown, |
| 3366 | .setsockopt = tipc_setsockopt, |
| 3367 | .getsockopt = tipc_getsockopt, |
| 3368 | .sendmsg = tipc_sendmsg, |
| 3369 | .recvmsg = tipc_recvmsg, |
YOSHIFUJI Hideaki | 8238745 | 2007-07-19 10:44:56 +0900 | [diff] [blame] | 3370 | .mmap = sock_no_mmap, |
| 3371 | .sendpage = sock_no_sendpage |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3372 | }; |
| 3373 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 3374 | static const struct proto_ops packet_ops = { |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 3375 | .owner = THIS_MODULE, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3376 | .family = AF_TIPC, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 3377 | .release = tipc_release, |
| 3378 | .bind = tipc_bind, |
| 3379 | .connect = tipc_connect, |
Erik Hugne | 70b0375 | 2017-03-29 11:22:16 +0200 | [diff] [blame] | 3380 | .socketpair = tipc_socketpair, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 3381 | .accept = tipc_accept, |
| 3382 | .getname = tipc_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 3383 | .poll = tipc_poll, |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 3384 | .ioctl = tipc_ioctl, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 3385 | .listen = tipc_listen, |
| 3386 | .shutdown = tipc_shutdown, |
| 3387 | .setsockopt = tipc_setsockopt, |
| 3388 | .getsockopt = tipc_getsockopt, |
| 3389 | .sendmsg = tipc_send_packet, |
| 3390 | .recvmsg = tipc_recvmsg, |
YOSHIFUJI Hideaki | 8238745 | 2007-07-19 10:44:56 +0900 | [diff] [blame] | 3391 | .mmap = sock_no_mmap, |
| 3392 | .sendpage = sock_no_sendpage |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3393 | }; |
| 3394 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 3395 | static const struct proto_ops stream_ops = { |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 3396 | .owner = THIS_MODULE, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3397 | .family = AF_TIPC, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 3398 | .release = tipc_release, |
| 3399 | .bind = tipc_bind, |
| 3400 | .connect = tipc_connect, |
Erik Hugne | 70b0375 | 2017-03-29 11:22:16 +0200 | [diff] [blame] | 3401 | .socketpair = tipc_socketpair, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 3402 | .accept = tipc_accept, |
| 3403 | .getname = tipc_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 3404 | .poll = tipc_poll, |
Erik Hugne | 78acb1f | 2014-04-24 16:26:47 +0200 | [diff] [blame] | 3405 | .ioctl = tipc_ioctl, |
Ying Xue | 247f0f3 | 2014-02-18 16:06:46 +0800 | [diff] [blame] | 3406 | .listen = tipc_listen, |
| 3407 | .shutdown = tipc_shutdown, |
| 3408 | .setsockopt = tipc_setsockopt, |
| 3409 | .getsockopt = tipc_getsockopt, |
Jon Paul Maloy | 365ad35 | 2017-01-03 10:55:11 -0500 | [diff] [blame] | 3410 | .sendmsg = tipc_sendstream, |
Jon Paul Maloy | ec8a09f | 2017-05-02 18:16:54 +0200 | [diff] [blame] | 3411 | .recvmsg = tipc_recvstream, |
YOSHIFUJI Hideaki | 8238745 | 2007-07-19 10:44:56 +0900 | [diff] [blame] | 3412 | .mmap = sock_no_mmap, |
| 3413 | .sendpage = sock_no_sendpage |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3414 | }; |
| 3415 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 3416 | static const struct net_proto_family tipc_family_ops = { |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 3417 | .owner = THIS_MODULE, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3418 | .family = AF_TIPC, |
Ying Xue | c5fa7b3 | 2013-06-17 10:54:39 -0400 | [diff] [blame] | 3419 | .create = tipc_sk_create |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3420 | }; |
| 3421 | |
| 3422 | static struct proto tipc_proto = { |
| 3423 | .name = "TIPC", |
| 3424 | .owner = THIS_MODULE, |
Ying Xue | cc79dd1 | 2013-06-17 10:54:37 -0400 | [diff] [blame] | 3425 | .obj_size = sizeof(struct tipc_sock), |
| 3426 | .sysctl_rmem = sysctl_tipc_rmem |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3427 | }; |
| 3428 | |
| 3429 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 3430 | * tipc_socket_init - initialize TIPC socket interface |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3431 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 3432 | * Return: 0 on success, errno otherwise |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3433 | */ |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 3434 | int tipc_socket_init(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3435 | { |
| 3436 | int res; |
| 3437 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3438 | res = proto_register(&tipc_proto, 1); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3439 | if (res) { |
Erik Hugne | 2cf8aa1 | 2012-06-29 00:16:37 -0400 | [diff] [blame] | 3440 | pr_err("Failed to register TIPC protocol type\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3441 | goto out; |
| 3442 | } |
| 3443 | |
| 3444 | res = sock_register(&tipc_family_ops); |
| 3445 | if (res) { |
Erik Hugne | 2cf8aa1 | 2012-06-29 00:16:37 -0400 | [diff] [blame] | 3446 | pr_err("Failed to register TIPC socket type\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3447 | proto_unregister(&tipc_proto); |
| 3448 | goto out; |
| 3449 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3450 | out: |
| 3451 | return res; |
| 3452 | } |
| 3453 | |
| 3454 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 3455 | * tipc_socket_stop - stop TIPC socket interface |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3456 | */ |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 3457 | void tipc_socket_stop(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3458 | { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 3459 | sock_unregister(tipc_family_ops.family); |
| 3460 | proto_unregister(&tipc_proto); |
| 3461 | } |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3462 | |
| 3463 | /* Caller should hold socket lock for the passed tipc socket. */ |
Richard Alpe | d818280 | 2014-11-24 11:10:29 +0100 | [diff] [blame] | 3464 | static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk) |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3465 | { |
Jon Maloy | 14623e0 | 2021-06-02 13:44:24 -0400 | [diff] [blame] | 3466 | u32 peer_node, peer_port; |
| 3467 | u32 conn_type, conn_instance; |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3468 | struct nlattr *nest; |
| 3469 | |
| 3470 | peer_node = tsk_peer_node(tsk); |
| 3471 | peer_port = tsk_peer_port(tsk); |
Jon Maloy | 14623e0 | 2021-06-02 13:44:24 -0400 | [diff] [blame] | 3472 | conn_type = msg_nametype(&tsk->phdr); |
| 3473 | conn_instance = msg_nameinst(&tsk->phdr); |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3474 | nest = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_CON); |
Kangjie Lu | 517ccc2 | 2019-03-16 16:46:05 -0500 | [diff] [blame] | 3475 | if (!nest) |
| 3476 | return -EMSGSIZE; |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3477 | |
| 3478 | if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node)) |
| 3479 | goto msg_full; |
| 3480 | if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port)) |
| 3481 | goto msg_full; |
| 3482 | |
Jon Maloy | 14623e0 | 2021-06-02 13:44:24 -0400 | [diff] [blame] | 3483 | if (tsk->conn_addrtype != 0) { |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3484 | if (nla_put_flag(skb, TIPC_NLA_CON_FLAG)) |
| 3485 | goto msg_full; |
Jon Maloy | 14623e0 | 2021-06-02 13:44:24 -0400 | [diff] [blame] | 3486 | if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, conn_type)) |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3487 | goto msg_full; |
Jon Maloy | 14623e0 | 2021-06-02 13:44:24 -0400 | [diff] [blame] | 3488 | if (nla_put_u32(skb, TIPC_NLA_CON_INST, conn_instance)) |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3489 | goto msg_full; |
| 3490 | } |
| 3491 | nla_nest_end(skb, nest); |
| 3492 | |
| 3493 | return 0; |
| 3494 | |
| 3495 | msg_full: |
| 3496 | nla_nest_cancel(skb, nest); |
| 3497 | |
| 3498 | return -EMSGSIZE; |
| 3499 | } |
| 3500 | |
GhantaKrishnamurthy MohanKrishna | dfde331 | 2018-03-21 14:37:43 +0100 | [diff] [blame] | 3501 | static int __tipc_nl_add_sk_info(struct sk_buff *skb, struct tipc_sock |
| 3502 | *tsk) |
| 3503 | { |
| 3504 | struct net *net = sock_net(skb->sk); |
GhantaKrishnamurthy MohanKrishna | dfde331 | 2018-03-21 14:37:43 +0100 | [diff] [blame] | 3505 | struct sock *sk = &tsk->sk; |
| 3506 | |
| 3507 | if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid) || |
Jon Maloy | 23fd3ea | 2018-03-22 20:42:49 +0100 | [diff] [blame] | 3508 | nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr(net))) |
GhantaKrishnamurthy MohanKrishna | dfde331 | 2018-03-21 14:37:43 +0100 | [diff] [blame] | 3509 | return -EMSGSIZE; |
| 3510 | |
| 3511 | if (tipc_sk_connected(sk)) { |
| 3512 | if (__tipc_nl_add_sk_con(skb, tsk)) |
| 3513 | return -EMSGSIZE; |
| 3514 | } else if (!list_empty(&tsk->publications)) { |
| 3515 | if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL)) |
| 3516 | return -EMSGSIZE; |
| 3517 | } |
| 3518 | return 0; |
| 3519 | } |
| 3520 | |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3521 | /* Caller should hold socket lock for the passed tipc socket. */ |
Richard Alpe | d818280 | 2014-11-24 11:10:29 +0100 | [diff] [blame] | 3522 | static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb, |
| 3523 | struct tipc_sock *tsk) |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3524 | { |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3525 | struct nlattr *attrs; |
GhantaKrishnamurthy MohanKrishna | dfde331 | 2018-03-21 14:37:43 +0100 | [diff] [blame] | 3526 | void *hdr; |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3527 | |
| 3528 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
Richard Alpe | bfb3e5d | 2015-02-09 09:50:03 +0100 | [diff] [blame] | 3529 | &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET); |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3530 | if (!hdr) |
| 3531 | goto msg_cancel; |
| 3532 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3533 | attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK); |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3534 | if (!attrs) |
| 3535 | goto genlmsg_cancel; |
GhantaKrishnamurthy MohanKrishna | dfde331 | 2018-03-21 14:37:43 +0100 | [diff] [blame] | 3536 | |
| 3537 | if (__tipc_nl_add_sk_info(skb, tsk)) |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3538 | goto attr_msg_cancel; |
| 3539 | |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3540 | nla_nest_end(skb, attrs); |
| 3541 | genlmsg_end(skb, hdr); |
| 3542 | |
| 3543 | return 0; |
| 3544 | |
| 3545 | attr_msg_cancel: |
| 3546 | nla_nest_cancel(skb, attrs); |
| 3547 | genlmsg_cancel: |
| 3548 | genlmsg_cancel(skb, hdr); |
| 3549 | msg_cancel: |
| 3550 | return -EMSGSIZE; |
| 3551 | } |
| 3552 | |
GhantaKrishnamurthy MohanKrishna | c30b70d | 2018-03-21 14:37:44 +0100 | [diff] [blame] | 3553 | int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb, |
| 3554 | int (*skb_handler)(struct sk_buff *skb, |
| 3555 | struct netlink_callback *cb, |
| 3556 | struct tipc_sock *tsk)) |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3557 | { |
Cong Wang | 8f5c5fc | 2018-09-04 14:54:55 -0700 | [diff] [blame] | 3558 | struct rhashtable_iter *iter = (void *)cb->args[4]; |
GhantaKrishnamurthy MohanKrishna | dfde331 | 2018-03-21 14:37:43 +0100 | [diff] [blame] | 3559 | struct tipc_sock *tsk; |
| 3560 | int err; |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3561 | |
Cong Wang | 9a07efa | 2018-08-24 12:28:06 -0700 | [diff] [blame] | 3562 | rhashtable_walk_start(iter); |
| 3563 | while ((tsk = rhashtable_walk_next(iter)) != NULL) { |
| 3564 | if (IS_ERR(tsk)) { |
| 3565 | err = PTR_ERR(tsk); |
| 3566 | if (err == -EAGAIN) { |
| 3567 | err = 0; |
Richard Alpe | d6e164e | 2015-01-16 12:30:40 +0100 | [diff] [blame] | 3568 | continue; |
| 3569 | } |
Cong Wang | 9a07efa | 2018-08-24 12:28:06 -0700 | [diff] [blame] | 3570 | break; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 3571 | } |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3572 | |
Cong Wang | 9a07efa | 2018-08-24 12:28:06 -0700 | [diff] [blame] | 3573 | sock_hold(&tsk->sk); |
| 3574 | rhashtable_walk_stop(iter); |
| 3575 | lock_sock(&tsk->sk); |
| 3576 | err = skb_handler(skb, cb, tsk); |
| 3577 | if (err) { |
| 3578 | release_sock(&tsk->sk); |
| 3579 | sock_put(&tsk->sk); |
| 3580 | goto out; |
| 3581 | } |
| 3582 | release_sock(&tsk->sk); |
| 3583 | rhashtable_walk_start(iter); |
| 3584 | sock_put(&tsk->sk); |
| 3585 | } |
| 3586 | rhashtable_walk_stop(iter); |
| 3587 | out: |
Richard Alpe | 34b78a12 | 2014-11-20 10:29:10 +0100 | [diff] [blame] | 3588 | return skb->len; |
| 3589 | } |
GhantaKrishnamurthy MohanKrishna | c30b70d | 2018-03-21 14:37:44 +0100 | [diff] [blame] | 3590 | EXPORT_SYMBOL(tipc_nl_sk_walk); |
| 3591 | |
Cong Wang | 9a07efa | 2018-08-24 12:28:06 -0700 | [diff] [blame] | 3592 | int tipc_dump_start(struct netlink_callback *cb) |
| 3593 | { |
Cong Wang | 8f5c5fc | 2018-09-04 14:54:55 -0700 | [diff] [blame] | 3594 | return __tipc_dump_start(cb, sock_net(cb->skb->sk)); |
| 3595 | } |
| 3596 | EXPORT_SYMBOL(tipc_dump_start); |
| 3597 | |
| 3598 | int __tipc_dump_start(struct netlink_callback *cb, struct net *net) |
| 3599 | { |
| 3600 | /* tipc_nl_name_table_dump() uses cb->args[0...3]. */ |
| 3601 | struct rhashtable_iter *iter = (void *)cb->args[4]; |
Cong Wang | 9a07efa | 2018-08-24 12:28:06 -0700 | [diff] [blame] | 3602 | struct tipc_net *tn = tipc_net(net); |
| 3603 | |
| 3604 | if (!iter) { |
| 3605 | iter = kmalloc(sizeof(*iter), GFP_KERNEL); |
| 3606 | if (!iter) |
| 3607 | return -ENOMEM; |
| 3608 | |
Cong Wang | 8f5c5fc | 2018-09-04 14:54:55 -0700 | [diff] [blame] | 3609 | cb->args[4] = (long)iter; |
Cong Wang | 9a07efa | 2018-08-24 12:28:06 -0700 | [diff] [blame] | 3610 | } |
| 3611 | |
| 3612 | rhashtable_walk_enter(&tn->sk_rht, iter); |
| 3613 | return 0; |
| 3614 | } |
Cong Wang | 9a07efa | 2018-08-24 12:28:06 -0700 | [diff] [blame] | 3615 | |
| 3616 | int tipc_dump_done(struct netlink_callback *cb) |
| 3617 | { |
Cong Wang | 8f5c5fc | 2018-09-04 14:54:55 -0700 | [diff] [blame] | 3618 | struct rhashtable_iter *hti = (void *)cb->args[4]; |
Cong Wang | 9a07efa | 2018-08-24 12:28:06 -0700 | [diff] [blame] | 3619 | |
| 3620 | rhashtable_walk_exit(hti); |
| 3621 | kfree(hti); |
| 3622 | return 0; |
| 3623 | } |
| 3624 | EXPORT_SYMBOL(tipc_dump_done); |
| 3625 | |
Cong Wang | e41f054 | 2018-04-06 18:54:52 -0700 | [diff] [blame] | 3626 | int tipc_sk_fill_sock_diag(struct sk_buff *skb, struct netlink_callback *cb, |
| 3627 | struct tipc_sock *tsk, u32 sk_filter_state, |
GhantaKrishnamurthy MohanKrishna | c30b70d | 2018-03-21 14:37:44 +0100 | [diff] [blame] | 3628 | u64 (*tipc_diag_gen_cookie)(struct sock *sk)) |
| 3629 | { |
| 3630 | struct sock *sk = &tsk->sk; |
| 3631 | struct nlattr *attrs; |
| 3632 | struct nlattr *stat; |
| 3633 | |
| 3634 | /*filter response w.r.t sk_state*/ |
| 3635 | if (!(sk_filter_state & (1 << sk->sk_state))) |
| 3636 | return 0; |
| 3637 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3638 | attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK); |
GhantaKrishnamurthy MohanKrishna | c30b70d | 2018-03-21 14:37:44 +0100 | [diff] [blame] | 3639 | if (!attrs) |
| 3640 | goto msg_cancel; |
| 3641 | |
| 3642 | if (__tipc_nl_add_sk_info(skb, tsk)) |
| 3643 | goto attr_msg_cancel; |
| 3644 | |
| 3645 | if (nla_put_u32(skb, TIPC_NLA_SOCK_TYPE, (u32)sk->sk_type) || |
| 3646 | nla_put_u32(skb, TIPC_NLA_SOCK_TIPC_STATE, (u32)sk->sk_state) || |
| 3647 | nla_put_u32(skb, TIPC_NLA_SOCK_INO, sock_i_ino(sk)) || |
| 3648 | nla_put_u32(skb, TIPC_NLA_SOCK_UID, |
Cong Wang | e41f054 | 2018-04-06 18:54:52 -0700 | [diff] [blame] | 3649 | from_kuid_munged(sk_user_ns(NETLINK_CB(cb->skb).sk), |
GhantaKrishnamurthy MohanKrishna | 4b2e687 | 2018-04-04 14:49:47 +0200 | [diff] [blame] | 3650 | sock_i_uid(sk))) || |
GhantaKrishnamurthy MohanKrishna | c30b70d | 2018-03-21 14:37:44 +0100 | [diff] [blame] | 3651 | nla_put_u64_64bit(skb, TIPC_NLA_SOCK_COOKIE, |
| 3652 | tipc_diag_gen_cookie(sk), |
| 3653 | TIPC_NLA_SOCK_PAD)) |
| 3654 | goto attr_msg_cancel; |
| 3655 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3656 | stat = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_STAT); |
GhantaKrishnamurthy MohanKrishna | c30b70d | 2018-03-21 14:37:44 +0100 | [diff] [blame] | 3657 | if (!stat) |
| 3658 | goto attr_msg_cancel; |
| 3659 | |
| 3660 | if (nla_put_u32(skb, TIPC_NLA_SOCK_STAT_RCVQ, |
| 3661 | skb_queue_len(&sk->sk_receive_queue)) || |
| 3662 | nla_put_u32(skb, TIPC_NLA_SOCK_STAT_SENDQ, |
GhantaKrishnamurthy MohanKrishna | 872619d | 2018-03-21 14:37:45 +0100 | [diff] [blame] | 3663 | skb_queue_len(&sk->sk_write_queue)) || |
| 3664 | nla_put_u32(skb, TIPC_NLA_SOCK_STAT_DROP, |
| 3665 | atomic_read(&sk->sk_drops))) |
GhantaKrishnamurthy MohanKrishna | c30b70d | 2018-03-21 14:37:44 +0100 | [diff] [blame] | 3666 | goto stat_msg_cancel; |
| 3667 | |
| 3668 | if (tsk->cong_link_cnt && |
| 3669 | nla_put_flag(skb, TIPC_NLA_SOCK_STAT_LINK_CONG)) |
| 3670 | goto stat_msg_cancel; |
| 3671 | |
| 3672 | if (tsk_conn_cong(tsk) && |
| 3673 | nla_put_flag(skb, TIPC_NLA_SOCK_STAT_CONN_CONG)) |
| 3674 | goto stat_msg_cancel; |
| 3675 | |
| 3676 | nla_nest_end(skb, stat); |
GhantaKrishnamurthy MohanKrishna | a1be5a2 | 2018-06-29 13:26:18 +0200 | [diff] [blame] | 3677 | |
| 3678 | if (tsk->group) |
| 3679 | if (tipc_group_fill_sock_diag(tsk->group, skb)) |
| 3680 | goto stat_msg_cancel; |
| 3681 | |
GhantaKrishnamurthy MohanKrishna | c30b70d | 2018-03-21 14:37:44 +0100 | [diff] [blame] | 3682 | nla_nest_end(skb, attrs); |
| 3683 | |
| 3684 | return 0; |
| 3685 | |
| 3686 | stat_msg_cancel: |
| 3687 | nla_nest_cancel(skb, stat); |
| 3688 | attr_msg_cancel: |
| 3689 | nla_nest_cancel(skb, attrs); |
| 3690 | msg_cancel: |
| 3691 | return -EMSGSIZE; |
| 3692 | } |
| 3693 | EXPORT_SYMBOL(tipc_sk_fill_sock_diag); |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3694 | |
GhantaKrishnamurthy MohanKrishna | dfde331 | 2018-03-21 14:37:43 +0100 | [diff] [blame] | 3695 | int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 3696 | { |
GhantaKrishnamurthy MohanKrishna | c30b70d | 2018-03-21 14:37:44 +0100 | [diff] [blame] | 3697 | return tipc_nl_sk_walk(skb, cb, __tipc_nl_add_sk); |
GhantaKrishnamurthy MohanKrishna | dfde331 | 2018-03-21 14:37:43 +0100 | [diff] [blame] | 3698 | } |
| 3699 | |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3700 | /* Caller should hold socket lock for the passed tipc socket. */ |
Richard Alpe | d818280 | 2014-11-24 11:10:29 +0100 | [diff] [blame] | 3701 | static int __tipc_nl_add_sk_publ(struct sk_buff *skb, |
| 3702 | struct netlink_callback *cb, |
| 3703 | struct publication *publ) |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3704 | { |
| 3705 | void *hdr; |
| 3706 | struct nlattr *attrs; |
| 3707 | |
| 3708 | hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, |
Richard Alpe | bfb3e5d | 2015-02-09 09:50:03 +0100 | [diff] [blame] | 3709 | &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET); |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3710 | if (!hdr) |
| 3711 | goto msg_cancel; |
| 3712 | |
Michal Kubecek | ae0be8d | 2019-04-26 11:13:06 +0200 | [diff] [blame] | 3713 | attrs = nla_nest_start_noflag(skb, TIPC_NLA_PUBL); |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3714 | if (!attrs) |
| 3715 | goto genlmsg_cancel; |
| 3716 | |
| 3717 | if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key)) |
| 3718 | goto attr_msg_cancel; |
Jon Maloy | 998d390 | 2021-03-16 22:06:08 -0400 | [diff] [blame] | 3719 | if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->sr.type)) |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3720 | goto attr_msg_cancel; |
Jon Maloy | 998d390 | 2021-03-16 22:06:08 -0400 | [diff] [blame] | 3721 | if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->sr.lower)) |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3722 | goto attr_msg_cancel; |
Jon Maloy | 998d390 | 2021-03-16 22:06:08 -0400 | [diff] [blame] | 3723 | if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->sr.upper)) |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3724 | goto attr_msg_cancel; |
| 3725 | |
| 3726 | nla_nest_end(skb, attrs); |
| 3727 | genlmsg_end(skb, hdr); |
| 3728 | |
| 3729 | return 0; |
| 3730 | |
| 3731 | attr_msg_cancel: |
| 3732 | nla_nest_cancel(skb, attrs); |
| 3733 | genlmsg_cancel: |
| 3734 | genlmsg_cancel(skb, hdr); |
| 3735 | msg_cancel: |
| 3736 | return -EMSGSIZE; |
| 3737 | } |
| 3738 | |
| 3739 | /* Caller should hold socket lock for the passed tipc socket. */ |
Richard Alpe | d818280 | 2014-11-24 11:10:29 +0100 | [diff] [blame] | 3740 | static int __tipc_nl_list_sk_publ(struct sk_buff *skb, |
| 3741 | struct netlink_callback *cb, |
| 3742 | struct tipc_sock *tsk, u32 *last_publ) |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3743 | { |
| 3744 | int err; |
| 3745 | struct publication *p; |
| 3746 | |
| 3747 | if (*last_publ) { |
Jon Maloy | e50e73e | 2018-03-15 16:48:55 +0100 | [diff] [blame] | 3748 | list_for_each_entry(p, &tsk->publications, binding_sock) { |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3749 | if (p->key == *last_publ) |
| 3750 | break; |
| 3751 | } |
| 3752 | if (p->key != *last_publ) { |
| 3753 | /* We never set seq or call nl_dump_check_consistent() |
| 3754 | * this means that setting prev_seq here will cause the |
| 3755 | * consistence check to fail in the netlink callback |
| 3756 | * handler. Resulting in the last NLMSG_DONE message |
| 3757 | * having the NLM_F_DUMP_INTR flag set. |
| 3758 | */ |
| 3759 | cb->prev_seq = 1; |
| 3760 | *last_publ = 0; |
| 3761 | return -EPIPE; |
| 3762 | } |
| 3763 | } else { |
| 3764 | p = list_first_entry(&tsk->publications, struct publication, |
Jon Maloy | e50e73e | 2018-03-15 16:48:55 +0100 | [diff] [blame] | 3765 | binding_sock); |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3766 | } |
| 3767 | |
Jon Maloy | e50e73e | 2018-03-15 16:48:55 +0100 | [diff] [blame] | 3768 | list_for_each_entry_from(p, &tsk->publications, binding_sock) { |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3769 | err = __tipc_nl_add_sk_publ(skb, cb, p); |
| 3770 | if (err) { |
| 3771 | *last_publ = p->key; |
| 3772 | return err; |
| 3773 | } |
| 3774 | } |
| 3775 | *last_publ = 0; |
| 3776 | |
| 3777 | return 0; |
| 3778 | } |
| 3779 | |
| 3780 | int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 3781 | { |
| 3782 | int err; |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 3783 | u32 tsk_portid = cb->args[0]; |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3784 | u32 last_publ = cb->args[1]; |
| 3785 | u32 done = cb->args[2]; |
Ying Xue | e05b31f | 2015-01-09 15:27:08 +0800 | [diff] [blame] | 3786 | struct net *net = sock_net(skb->sk); |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3787 | struct tipc_sock *tsk; |
| 3788 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 3789 | if (!tsk_portid) { |
Jiri Pirko | 057af70 | 2019-10-05 20:04:39 +0200 | [diff] [blame] | 3790 | struct nlattr **attrs = genl_dumpit_info(cb)->attrs; |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3791 | struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1]; |
| 3792 | |
Richard Alpe | 45e093a | 2016-05-16 11:14:54 +0200 | [diff] [blame] | 3793 | if (!attrs[TIPC_NLA_SOCK]) |
| 3794 | return -EINVAL; |
| 3795 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 3796 | err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX, |
| 3797 | attrs[TIPC_NLA_SOCK], |
| 3798 | tipc_nl_sock_policy, NULL); |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3799 | if (err) |
| 3800 | return err; |
| 3801 | |
| 3802 | if (!sock[TIPC_NLA_SOCK_REF]) |
| 3803 | return -EINVAL; |
| 3804 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 3805 | tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]); |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | if (done) |
| 3809 | return 0; |
| 3810 | |
Ying Xue | e05b31f | 2015-01-09 15:27:08 +0800 | [diff] [blame] | 3811 | tsk = tipc_sk_lookup(net, tsk_portid); |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3812 | if (!tsk) |
| 3813 | return -EINVAL; |
| 3814 | |
| 3815 | lock_sock(&tsk->sk); |
| 3816 | err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ); |
| 3817 | if (!err) |
| 3818 | done = 1; |
| 3819 | release_sock(&tsk->sk); |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 3820 | sock_put(&tsk->sk); |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3821 | |
Ying Xue | 07f6c4b | 2015-01-07 13:41:58 +0800 | [diff] [blame] | 3822 | cb->args[0] = tsk_portid; |
Richard Alpe | 1a1a143 | 2014-11-20 10:29:11 +0100 | [diff] [blame] | 3823 | cb->args[1] = last_publ; |
| 3824 | cb->args[2] = done; |
| 3825 | |
| 3826 | return skb->len; |
| 3827 | } |
Tuong Lien | b4b9771 | 2018-12-19 09:17:56 +0700 | [diff] [blame] | 3828 | |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 3829 | /** |
| 3830 | * tipc_sk_filtering - check if a socket should be traced |
| 3831 | * @sk: the socket to be examined |
Randy Dunlap | f172f4b | 2020-11-29 10:32:49 -0800 | [diff] [blame] | 3832 | * |
| 3833 | * @sysctl_tipc_sk_filter is used as the socket tuple for filtering: |
Randy Dunlap | 5fcb7d4 | 2020-11-29 10:32:50 -0800 | [diff] [blame] | 3834 | * (portid, sock type, name type, name lower, name upper) |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 3835 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 3836 | * Return: true if the socket meets the socket tuple data |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 3837 | * (value 0 = 'any') or when there is no tuple set (all = 0), |
| 3838 | * otherwise false |
| 3839 | */ |
| 3840 | bool tipc_sk_filtering(struct sock *sk) |
| 3841 | { |
| 3842 | struct tipc_sock *tsk; |
| 3843 | struct publication *p; |
| 3844 | u32 _port, _sktype, _type, _lower, _upper; |
| 3845 | u32 type = 0, lower = 0, upper = 0; |
| 3846 | |
| 3847 | if (!sk) |
| 3848 | return true; |
| 3849 | |
| 3850 | tsk = tipc_sk(sk); |
| 3851 | |
| 3852 | _port = sysctl_tipc_sk_filter[0]; |
| 3853 | _sktype = sysctl_tipc_sk_filter[1]; |
| 3854 | _type = sysctl_tipc_sk_filter[2]; |
| 3855 | _lower = sysctl_tipc_sk_filter[3]; |
| 3856 | _upper = sysctl_tipc_sk_filter[4]; |
| 3857 | |
| 3858 | if (!_port && !_sktype && !_type && !_lower && !_upper) |
| 3859 | return true; |
| 3860 | |
| 3861 | if (_port) |
| 3862 | return (_port == tsk->portid); |
| 3863 | |
| 3864 | if (_sktype && _sktype != sk->sk_type) |
| 3865 | return false; |
| 3866 | |
| 3867 | if (tsk->published) { |
| 3868 | p = list_first_entry_or_null(&tsk->publications, |
| 3869 | struct publication, binding_sock); |
| 3870 | if (p) { |
Jon Maloy | 998d390 | 2021-03-16 22:06:08 -0400 | [diff] [blame] | 3871 | type = p->sr.type; |
| 3872 | lower = p->sr.lower; |
| 3873 | upper = p->sr.upper; |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 3874 | } |
| 3875 | } |
| 3876 | |
| 3877 | if (!tipc_sk_type_connectionless(sk)) { |
Jon Maloy | 14623e0 | 2021-06-02 13:44:24 -0400 | [diff] [blame] | 3878 | type = msg_nametype(&tsk->phdr); |
| 3879 | lower = msg_nameinst(&tsk->phdr); |
| 3880 | upper = lower; |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 3881 | } |
| 3882 | |
| 3883 | if ((_type && _type != type) || (_lower && _lower != lower) || |
| 3884 | (_upper && _upper != upper)) |
| 3885 | return false; |
| 3886 | |
| 3887 | return true; |
| 3888 | } |
| 3889 | |
Tuong Lien | b4b9771 | 2018-12-19 09:17:56 +0700 | [diff] [blame] | 3890 | u32 tipc_sock_get_portid(struct sock *sk) |
| 3891 | { |
| 3892 | return (sk) ? (tipc_sk(sk))->portid : 0; |
| 3893 | } |
| 3894 | |
| 3895 | /** |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 3896 | * tipc_sk_overlimit1 - check if socket rx queue is about to be overloaded, |
| 3897 | * both the rcv and backlog queues are considered |
| 3898 | * @sk: tipc sk to be checked |
| 3899 | * @skb: tipc msg to be checked |
| 3900 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 3901 | * Return: true if the socket rx queue allocation is > 90%, otherwise false |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 3902 | */ |
| 3903 | |
| 3904 | bool tipc_sk_overlimit1(struct sock *sk, struct sk_buff *skb) |
| 3905 | { |
| 3906 | atomic_t *dcnt = &tipc_sk(sk)->dupl_rcvcnt; |
| 3907 | unsigned int lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt); |
| 3908 | unsigned int qsize = sk->sk_backlog.len + sk_rmem_alloc_get(sk); |
| 3909 | |
| 3910 | return (qsize > lim * 90 / 100); |
| 3911 | } |
| 3912 | |
| 3913 | /** |
| 3914 | * tipc_sk_overlimit2 - check if socket rx queue is about to be overloaded, |
| 3915 | * only the rcv queue is considered |
| 3916 | * @sk: tipc sk to be checked |
| 3917 | * @skb: tipc msg to be checked |
| 3918 | * |
Randy Dunlap | 637b77f | 2020-11-29 10:32:48 -0800 | [diff] [blame] | 3919 | * Return: true if the socket rx queue allocation is > 90%, otherwise false |
Tuong Lien | 01e661e | 2018-12-19 09:17:58 +0700 | [diff] [blame] | 3920 | */ |
| 3921 | |
| 3922 | bool tipc_sk_overlimit2(struct sock *sk, struct sk_buff *skb) |
| 3923 | { |
| 3924 | unsigned int lim = rcvbuf_limit(sk, skb); |
| 3925 | unsigned int qsize = sk_rmem_alloc_get(sk); |
| 3926 | |
| 3927 | return (qsize > lim * 90 / 100); |
| 3928 | } |
| 3929 | |
| 3930 | /** |
Tuong Lien | b4b9771 | 2018-12-19 09:17:56 +0700 | [diff] [blame] | 3931 | * tipc_sk_dump - dump TIPC socket |
| 3932 | * @sk: tipc sk to be dumped |
| 3933 | * @dqueues: bitmask to decide if any socket queue to be dumped? |
| 3934 | * - TIPC_DUMP_NONE: don't dump socket queues |
| 3935 | * - TIPC_DUMP_SK_SNDQ: dump socket send queue |
| 3936 | * - TIPC_DUMP_SK_RCVQ: dump socket rcv queue |
| 3937 | * - TIPC_DUMP_SK_BKLGQ: dump socket backlog queue |
| 3938 | * - TIPC_DUMP_ALL: dump all the socket queues above |
| 3939 | * @buf: returned buffer of dump data in format |
| 3940 | */ |
| 3941 | int tipc_sk_dump(struct sock *sk, u16 dqueues, char *buf) |
| 3942 | { |
| 3943 | int i = 0; |
| 3944 | size_t sz = (dqueues) ? SK_LMAX : SK_LMIN; |
Jon Maloy | 14623e0 | 2021-06-02 13:44:24 -0400 | [diff] [blame] | 3945 | u32 conn_type, conn_instance; |
Tuong Lien | b4b9771 | 2018-12-19 09:17:56 +0700 | [diff] [blame] | 3946 | struct tipc_sock *tsk; |
| 3947 | struct publication *p; |
| 3948 | bool tsk_connected; |
| 3949 | |
| 3950 | if (!sk) { |
| 3951 | i += scnprintf(buf, sz, "sk data: (null)\n"); |
| 3952 | return i; |
| 3953 | } |
| 3954 | |
| 3955 | tsk = tipc_sk(sk); |
| 3956 | tsk_connected = !tipc_sk_type_connectionless(sk); |
| 3957 | |
| 3958 | i += scnprintf(buf, sz, "sk data: %u", sk->sk_type); |
| 3959 | i += scnprintf(buf + i, sz - i, " %d", sk->sk_state); |
| 3960 | i += scnprintf(buf + i, sz - i, " %x", tsk_own_node(tsk)); |
| 3961 | i += scnprintf(buf + i, sz - i, " %u", tsk->portid); |
| 3962 | i += scnprintf(buf + i, sz - i, " | %u", tsk_connected); |
| 3963 | if (tsk_connected) { |
| 3964 | i += scnprintf(buf + i, sz - i, " %x", tsk_peer_node(tsk)); |
| 3965 | i += scnprintf(buf + i, sz - i, " %u", tsk_peer_port(tsk)); |
Jon Maloy | 14623e0 | 2021-06-02 13:44:24 -0400 | [diff] [blame] | 3966 | conn_type = msg_nametype(&tsk->phdr); |
| 3967 | conn_instance = msg_nameinst(&tsk->phdr); |
| 3968 | i += scnprintf(buf + i, sz - i, " %u", conn_type); |
| 3969 | i += scnprintf(buf + i, sz - i, " %u", conn_instance); |
Tuong Lien | b4b9771 | 2018-12-19 09:17:56 +0700 | [diff] [blame] | 3970 | } |
| 3971 | i += scnprintf(buf + i, sz - i, " | %u", tsk->published); |
| 3972 | if (tsk->published) { |
| 3973 | p = list_first_entry_or_null(&tsk->publications, |
| 3974 | struct publication, binding_sock); |
Jon Maloy | 998d390 | 2021-03-16 22:06:08 -0400 | [diff] [blame] | 3975 | i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.type : 0); |
| 3976 | i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.lower : 0); |
| 3977 | i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.upper : 0); |
Tuong Lien | b4b9771 | 2018-12-19 09:17:56 +0700 | [diff] [blame] | 3978 | } |
| 3979 | i += scnprintf(buf + i, sz - i, " | %u", tsk->snd_win); |
| 3980 | i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_win); |
| 3981 | i += scnprintf(buf + i, sz - i, " %u", tsk->max_pkt); |
| 3982 | i += scnprintf(buf + i, sz - i, " %x", tsk->peer_caps); |
| 3983 | i += scnprintf(buf + i, sz - i, " %u", tsk->cong_link_cnt); |
| 3984 | i += scnprintf(buf + i, sz - i, " %u", tsk->snt_unacked); |
| 3985 | i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_unacked); |
| 3986 | i += scnprintf(buf + i, sz - i, " %u", atomic_read(&tsk->dupl_rcvcnt)); |
| 3987 | i += scnprintf(buf + i, sz - i, " %u", sk->sk_shutdown); |
| 3988 | i += scnprintf(buf + i, sz - i, " | %d", sk_wmem_alloc_get(sk)); |
| 3989 | i += scnprintf(buf + i, sz - i, " %d", sk->sk_sndbuf); |
| 3990 | i += scnprintf(buf + i, sz - i, " | %d", sk_rmem_alloc_get(sk)); |
| 3991 | i += scnprintf(buf + i, sz - i, " %d", sk->sk_rcvbuf); |
Eric Dumazet | 70c2655 | 2019-10-09 15:41:03 -0700 | [diff] [blame] | 3992 | i += scnprintf(buf + i, sz - i, " | %d\n", READ_ONCE(sk->sk_backlog.len)); |
Tuong Lien | b4b9771 | 2018-12-19 09:17:56 +0700 | [diff] [blame] | 3993 | |
| 3994 | if (dqueues & TIPC_DUMP_SK_SNDQ) { |
| 3995 | i += scnprintf(buf + i, sz - i, "sk_write_queue: "); |
| 3996 | i += tipc_list_dump(&sk->sk_write_queue, false, buf + i); |
| 3997 | } |
| 3998 | |
| 3999 | if (dqueues & TIPC_DUMP_SK_RCVQ) { |
| 4000 | i += scnprintf(buf + i, sz - i, "sk_receive_queue: "); |
| 4001 | i += tipc_list_dump(&sk->sk_receive_queue, false, buf + i); |
| 4002 | } |
| 4003 | |
| 4004 | if (dqueues & TIPC_DUMP_SK_BKLGQ) { |
| 4005 | i += scnprintf(buf + i, sz - i, "sk_backlog:\n head "); |
| 4006 | i += tipc_skb_dump(sk->sk_backlog.head, false, buf + i); |
| 4007 | if (sk->sk_backlog.tail != sk->sk_backlog.head) { |
| 4008 | i += scnprintf(buf + i, sz - i, " tail "); |
| 4009 | i += tipc_skb_dump(sk->sk_backlog.tail, false, |
| 4010 | buf + i); |
| 4011 | } |
| 4012 | } |
| 4013 | |
| 4014 | return i; |
| 4015 | } |