blob: 3e63c83e641c565f72500ef714694253871d0205 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Maloy60c102e2020-11-25 13:29:13 -05004 * Copyright (c) 2001-2007, 2012-2019, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Jon Maloy998d3902021-03-16 22:06:08 -04006 * Copyright (c) 2020-2021, Red Hat Inc
Per Lidenb97bf3f2006-01-02 19:04:38 +01007 * All rights reserved.
8 *
Per Liden9ea1fd32006-01-11 13:30:43 +01009 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +010010 * modification, are permitted provided that the following conditions are met:
11 *
Per Liden9ea1fd32006-01-11 13:30:43 +010012 * 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 Lidenb97bf3f2006-01-02 19:04:38 +010020 *
Per Liden9ea1fd32006-01-11 13:30:43 +010021 * 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 Lidenb97bf3f2006-01-02 19:04:38 +010035 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
Ying Xue07f6c4b2015-01-07 13:41:58 +080038#include <linux/rhashtable.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010039#include <linux/sched/signal.h>
40
Per Lidenb97bf3f2006-01-02 19:04:38 +010041#include "core.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050042#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020043#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050044#include "link.h"
Jon Paul Maloyc637c102015-02-05 08:36:41 -050045#include "name_distr.h"
Jon Paul Maloy2e84c602014-08-22 18:09:18 -040046#include "socket.h"
Jon Paul Maloya6bf70f2015-05-14 10:46:13 -040047#include "bcast.h"
Richard Alpe49cc66e2016-03-04 17:04:42 +010048#include "netlink.h"
Jon Maloy75da2162017-10-13 11:04:23 +020049#include "group.h"
Tuong Lienb4b97712018-12-19 09:17:56 +070050#include "trace.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040051
Tuong Lien0a3e0602020-05-26 16:38:38 +070052#define NAGLE_START_INIT 4
53#define NAGLE_START_MAX 1024
Tung Nguyen67879272018-09-28 20:23:22 +020054#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Jon Maloy0d5fcebf2017-10-20 11:21:32 +020055#define CONN_PROBING_INTV msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080056#define TIPC_MAX_PORT 0xffffffff
57#define TIPC_MIN_PORT 1
Randy Dunlap60462192020-09-17 21:35:19 -070058#define TIPC_ACK_RATE 4 /* ACK at 1/4 of rcv window size */
Jon Paul Maloy301bae52014-08-22 18:09:20 -040059
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010060enum {
61 TIPC_LISTEN = TCP_LISTEN,
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +010062 TIPC_ESTABLISHED = TCP_ESTABLISHED,
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +010063 TIPC_OPEN = TCP_CLOSE,
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +010064 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +010065 TIPC_CONNECTING = TCP_SYN_SENT,
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010066};
67
Jon Maloy31c82a22017-10-13 11:04:24 +020068struct sockaddr_pair {
69 struct sockaddr_tipc sock;
70 struct sockaddr_tipc member;
71};
72
Jon Paul Maloy301bae52014-08-22 18:09:20 -040073/**
74 * struct tipc_sock - TIPC socket structure
75 * @sk: socket - interacts with 'port' and with user via the socket API
Jon Paul Maloy301bae52014-08-22 18:09:20 -040076 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Jon Maloyc0bceb92019-10-30 14:00:41 +010077 * @maxnagle: maximum size of msg which can be subject to nagle
Ying Xue07f6c4b2015-01-07 13:41:58 +080078 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040079 * @phdr: preformatted message header used when sending messages
Randy Dunlapf172f4b2020-11-29 10:32:49 -080080 * @cong_links: list of congested links
Jon Paul Maloy301bae52014-08-22 18:09:20 -040081 * @publications: list of publications for port
Jon Paul Maloy365ad352017-01-03 10:55:11 -050082 * @blocking_link: address of the congested link we are currently sleeping on
Jon Paul Maloy301bae52014-08-22 18:09:20 -040083 * @pub_count: total # of publications port has made during its lifetime
Jon Paul Maloy301bae52014-08-22 18:09:20 -040084 * @conn_timeout: the time we can wait for an unresponded setup request
Randy Dunlapf172f4b2020-11-29 10:32:49 -080085 * @probe_unacked: probe has not received ack yet
Jon Paul Maloy301bae52014-08-22 18:09:20 -040086 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
Jon Paul Maloy365ad352017-01-03 10:55:11 -050087 * @cong_link_cnt: number of congested links
Jon Maloy75da2162017-10-13 11:04:23 +020088 * @snt_unacked: # messages sent by socket, and not yet acked by peer
Randy Dunlapf172f4b2020-11-29 10:32:49 -080089 * @snd_win: send window size
90 * @peer_caps: peer capabilities mask
Jon Paul Maloy301bae52014-08-22 18:09:20 -040091 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Randy Dunlapf172f4b2020-11-29 10:32:49 -080092 * @rcv_win: receive window size
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +010093 * @peer: 'connected' peer for dgram/rdm
Ying Xue07f6c4b2015-01-07 13:41:58 +080094 * @node: hash table node
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -050095 * @mc_method: cookie for use between socket and broadcast layer
Ying Xue07f6c4b2015-01-07 13:41:58 +080096 * @rcu: rcu struct for tipc_sock
Randy Dunlapf172f4b2020-11-29 10:32:49 -080097 * @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 Maloy14623e02021-06-02 13:44:24 -0400106 * @published: true if port has one or more associated names
107 * @conn_addrtype: address type used when establishing connection
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400108 */
109struct tipc_sock {
110 struct sock sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400111 u32 max_pkt;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100112 u32 maxnagle;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800113 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400114 struct tipc_msg phdr;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500115 struct list_head cong_links;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400116 struct list_head publications;
117 u32 pub_count;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400118 atomic_t dupl_rcvcnt;
Tung Nguyen67879272018-09-28 20:23:22 +0200119 u16 conn_timeout;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100120 bool probe_unacked;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500121 u16 cong_link_cnt;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400122 u16 snt_unacked;
123 u16 snd_win;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400124 u16 peer_caps;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400125 u16 rcv_unacked;
126 u16 rcv_win;
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +0100127 struct sockaddr_tipc peer;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800128 struct rhash_head node;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500129 struct tipc_mc_method mc_method;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800130 struct rcu_head rcu;
Jon Maloy75da2162017-10-13 11:04:23 +0200131 struct tipc_group *group;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100132 u32 oneway;
Tuong Lien0a3e0602020-05-26 16:38:38 +0700133 u32 nagle_start;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100134 u16 snd_backlog;
Tuong Lien0a3e0602020-05-26 16:38:38 +0700135 u16 msg_acc;
136 u16 pkt_cnt;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100137 bool expect_ack;
138 bool nodelay;
Jon Maloy60c25302018-01-17 16:42:46 +0100139 bool group_is_open;
Jon Maloy50a34992021-03-16 22:06:11 -0400140 bool published;
Jon Maloy14623e02021-06-02 13:44:24 -0400141 u8 conn_addrtype;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400142};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143
Jon Maloy64ac5f52017-10-13 11:04:20 +0200144static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400145static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800146static void tipc_write_space(struct sock *sk);
Ying Xuef4195d12015-11-22 15:46:05 +0800147static void tipc_sock_destruct(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800148static int tipc_release(struct socket *sock);
David Howellscdfbabf2017-03-09 08:09:05 +0000149static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
150 bool kern);
Kees Cook31b102b2017-10-30 14:06:45 -0700151static void tipc_sk_timeout(struct timer_list *t);
Jon Maloy50a34992021-03-16 22:06:11 -0400152static int tipc_sk_publish(struct tipc_sock *tsk, struct tipc_uaddr *ua);
Jon Maloy2c98da02021-03-16 22:06:13 -0400153static int tipc_sk_withdraw(struct tipc_sock *tsk, struct tipc_uaddr *ua);
Jon Maloy75da2162017-10-13 11:04:23 +0200154static int tipc_sk_leave(struct tipc_sock *tsk);
Ying Xuee05b31f2015-01-09 15:27:08 +0800155static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800156static int tipc_sk_insert(struct tipc_sock *tsk);
157static void tipc_sk_remove(struct tipc_sock *tsk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500158static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
Ying Xue39a0295f2015-03-02 15:37:47 +0800159static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Tuong Lien0a3e0602020-05-26 16:38:38 +0700160static void tipc_sk_push_backlog(struct tipc_sock *tsk, bool nagle_ack);
Xin Longf8dd60d2021-07-22 12:05:41 -0400161static int tipc_wait_for_connect(struct socket *sock, long *timeo_p);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162
Florian Westphalbca65ea2008-02-07 18:18:01 -0800163static const struct proto_ops packet_ops;
164static const struct proto_ops stream_ops;
165static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166static struct proto tipc_proto;
Herbert Xu6cca72892015-03-20 21:57:05 +1100167static const struct rhashtable_params tsk_rht_params;
168
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500169static u32 tsk_own_node(struct tipc_sock *tsk)
170{
171 return msg_prevnode(&tsk->phdr);
172}
173
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400174static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400175{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400176 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400177}
178
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400179static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400180{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400181 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400182}
183
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400184static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400185{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400186 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400187}
188
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400189static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400190{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400191 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400192}
193
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400194static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400195{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400196 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400197}
198
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400199static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400200{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400201 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400202}
203
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400204static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400205{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400206 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400207}
208
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400209static struct tipc_sock *tipc_sk(const struct sock *sk)
210{
211 return container_of(sk, struct tipc_sock, sk);
212}
213
Christoph Hellwig095ae612020-05-28 07:12:36 +0200214int 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 Maloy10724cc2016-05-02 11:58:47 -0400222static bool tsk_conn_cong(struct tipc_sock *tsk)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400223{
Jon Paul Maloy6998cc62016-11-24 18:47:07 -0500224 return tsk->snt_unacked > tsk->snd_win;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400225}
226
Jon Maloyb7d42632017-10-13 11:04:26 +0200227static u16 tsk_blocks(int len)
228{
229 return ((len / FLOWCTL_BLK_SZ) + 1);
230}
231
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400232/* 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 */
236static 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 */
245static 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 Maloy301bae52014-08-22 18:09:20 -0400250}
251
Jon Maloyc0bceb92019-10-30 14:00:41 +0100252/* tsk_set_nagle - enable/disable nagle property by manipulating maxnagle
253 */
254static 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 Lidenb97bf3f2006-01-02 19:04:38 +0100272/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400273 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Randy Dunlapf172f4b2020-11-29 10:32:49 -0800274 * @sk: network socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700275 *
276 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100277 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400278static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100279{
Tuong Lien01e661e2018-12-19 09:17:58 +0700280 trace_tipc_sk_advance_rx(sk, NULL, TIPC_DUMP_SK_RCVQ, " ");
Allan Stephens5f6d9122011-11-04 13:24:29 -0400281 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282}
283
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400284/* tipc_sk_respond() : send response message back to sender
285 */
286static 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 Lien01e661e2018-12-19 09:17:58 +0700295 trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE, "@sk_respond!");
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400296 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 Lidenb97bf3f2006-01-02 19:04:38 +0100301/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400302 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Randy Dunlapf172f4b2020-11-29 10:32:49 -0800303 * @sk: network socket
304 * @error: response error code
Allan Stephens0c3141e2008-04-15 00:22:02 -0700305 *
306 * Caller must hold socket lock
307 */
Tuong Lien49afb802020-01-08 09:18:15 +0700308static void tsk_rej_rx_queue(struct sock *sk, int error)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700309{
Ying Xuea6ca1092014-11-26 11:41:55 +0800310 struct sk_buff *skb;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700311
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400312 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
Tuong Lien49afb802020-01-08 09:18:15 +0700313 tipc_sk_respond(sk, skb, error);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700314}
315
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100316static bool tipc_sk_connected(struct sock *sk)
317{
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100318 return sk->sk_state == TIPC_ESTABLISHED;
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100319}
320
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +0100321/* tipc_sk_type_connectionless - check if the socket is datagram socket
322 * @sk: socket
323 *
324 * Returns true if connection less, false otherwise
325 */
326static 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 Maloy2e84c602014-08-22 18:09:18 -0400331/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400332 *
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 Maloy2e84c602014-08-22 18:09:18 -0400336static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400337{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100338 struct sock *sk = &tsk->sk;
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100339 u32 self = tipc_own_addr(sock_net(sk));
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400340 u32 peer_port = tsk_peer_port(tsk);
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100341 u32 orig_node, peer_node;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400342
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100343 if (unlikely(!tipc_sk_connected(sk)))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400344 return false;
345
346 if (unlikely(msg_origport(msg) != peer_port))
347 return false;
348
349 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400350 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400351
352 if (likely(orig_node == peer_node))
353 return true;
354
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100355 if (!orig_node && peer_node == self)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400356 return true;
357
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100358 if (!peer_node && orig_node == self)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400359 return true;
360
361 return false;
362}
363
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100364/* 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 */
371static int tipc_set_sk_state(struct sock *sk, int state)
372{
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100373 int oldsk_state = sk->sk_state;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100374 int res = -EINVAL;
375
376 switch (state) {
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100377 case TIPC_OPEN:
378 res = 0;
379 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100380 case TIPC_LISTEN:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100381 case TIPC_CONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100382 if (oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100383 res = 0;
384 break;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100385 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100386 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100387 oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100388 res = 0;
389 break;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100390 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100391 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100392 oldsk_state == TIPC_ESTABLISHED)
393 res = 0;
394 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100395 }
396
397 if (!res)
398 sk->sk_state = state;
399
400 return res;
401}
402
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500403static 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 Maloy844cf762017-05-11 20:28:15 +0200425#define tipc_wait_for_cond(sock_, timeo_, condition_) \
426({ \
Tung Nguyenbfd07f32019-02-25 10:57:20 +0700427 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200428 struct sock *sk_; \
429 int rc_; \
430 \
431 while ((rc_ = !(condition_))) { \
Tung Nguyenbfd07f32019-02-25 10:57:20 +0700432 /* coupled with smp_wmb() in tipc_sk_proto_rcv() */ \
433 smp_rmb(); \
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200434 sk_ = (sock_)->sk; \
435 rc_ = tipc_sk_sock_err((sock_), timeo_); \
436 if (rc_) \
437 break; \
Tung Nguyen223b7322019-02-19 11:20:47 +0700438 add_wait_queue(sk_sleep(sk_), &wait_); \
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200439 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 Maloy8c44e1a2017-01-03 10:55:09 -0500446})
447
Allan Stephens0c3141e2008-04-15 00:22:02 -0700448/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400449 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700450 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451 * @sock: pre-allocated socket structure
452 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800453 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900454 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700455 * This routine creates additional data structures used by the TIPC socket,
456 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 *
Randy Dunlap637b77f2020-11-29 10:32:48 -0800458 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400460static int tipc_sk_create(struct net *net, struct socket *sock,
461 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700463 const struct proto_ops *ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400465 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400466 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700467
468 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469 if (unlikely(protocol != 0))
470 return -EPROTONOSUPPORT;
471
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472 switch (sock->type) {
473 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700474 ops = &stream_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475 break;
476 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700477 ops = &packet_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478 break;
479 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700481 ops = &msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100482 break;
Allan Stephens49978652006-06-25 23:47:18 -0700483 default:
Allan Stephens49978652006-06-25 23:47:18 -0700484 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485 }
486
Allan Stephens0c3141e2008-04-15 00:22:02 -0700487 /* Allocate socket's protocol area */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500488 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700489 if (sk == NULL)
490 return -ENOMEM;
491
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400492 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400493 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100494 tsk->maxnagle = 0;
Tuong Lien0a3e0602020-05-26 16:38:38 +0700495 tsk->nagle_start = NAGLE_START_INIT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400496 INIT_LIST_HEAD(&tsk->publications);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500497 INIT_LIST_HEAD(&tsk->cong_links);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400498 msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499
Allan Stephens0c3141e2008-04-15 00:22:02 -0700500 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700501 sock->ops = ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502 sock_init_data(sock, sk);
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100503 tipc_set_sk_state(sk, TIPC_OPEN);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800504 if (tipc_sk_insert(tsk)) {
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900505 pr_warn("Socket create failed; port number exhausted\n");
Ying Xue07f6c4b2015-01-07 13:41:58 +0800506 return -EINVAL;
507 }
Herbert Xu40f9f432017-02-11 19:26:46 +0800508
509 /* Ensure tsk is visible before we read own_addr. */
510 smp_mb();
511
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100512 tipc_msg_init(tipc_own_addr(net), msg, TIPC_LOW_IMPORTANCE,
513 TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
Herbert Xu40f9f432017-02-11 19:26:46 +0800514
Ying Xue07f6c4b2015-01-07 13:41:58 +0800515 msg_set_origport(msg, tsk->portid);
Kees Cook31b102b2017-10-30 14:06:45 -0700516 timer_setup(&sk->sk_timer, tipc_sk_timeout, 0);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100517 sk->sk_shutdown = 0;
Jon Maloy64ac5f52017-10-13 11:04:20 +0200518 sk->sk_backlog_rcv = tipc_sk_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400519 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800520 sk->sk_data_ready = tipc_data_ready;
521 sk->sk_write_space = tipc_write_space;
Ying Xuef4195d12015-11-22 15:46:05 +0800522 sk->sk_destruct = tipc_sock_destruct;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400523 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Maloy1b22bca2018-02-26 20:14:04 +0100524 tsk->group_is_open = true;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400525 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700526
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400527 /* 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 Bhuvaraganc752023a2016-11-01 14:02:42 +0100531 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400532 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700533 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400534 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700535 }
Jon Maloy2948a1f2019-07-30 20:19:10 +0200536 __skb_queue_head_init(&tsk->mc_method.deferredq);
Tuong Lien01e661e2018-12-19 09:17:58 +0700537 trace_tipc_sk_create(sk, NULL, TIPC_DUMP_NONE, " ");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 return 0;
539}
540
Ying Xue07f6c4b2015-01-07 13:41:58 +0800541static 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 Bhuvaragan6f000892016-11-01 14:02:47 +0100548/* Caller should hold socket lock for the socket. */
549static 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 Nguyen12db3c82019-11-28 10:10:07 +0700554 long timeout = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100555 u32 dnode = tsk_peer_node(tsk);
556 struct sk_buff *skb;
557
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500558 /* 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 Nguyend34910e2019-11-28 10:10:08 +0700562 /* Push out delayed messages if in Nagle mode */
Tuong Lien0a3e0602020-05-26 16:38:38 +0700563 tipc_sk_push_backlog(tsk, false);
Tung Nguyend34910e2019-11-28 10:10:08 +0700564 /* Remove pending SYN */
565 __skb_queue_purge(&sk->sk_write_queue);
Tung Nguyen67879272018-09-28 20:23:22 +0200566
Tuong Lien49afb802020-01-08 09:18:15 +0700567 /* 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 Bhuvaragan6f000892016-11-01 14:02:47 +0100572 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500573
Tuong Lien49afb802020-01-08 09:18:15 +0700574 /* Reject all unreceived messages if connectionless */
575 if (tipc_sk_type_connectionless(sk)) {
576 tsk_rej_rx_queue(sk, error);
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500577 return;
Tuong Lien49afb802020-01-08 09:18:15 +0700578 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500579
Tuong Lien49afb802020-01-08 09:18:15 +0700580 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 Bhuvaragan6f000892016-11-01 14:02:47 +0100592 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 Lien49afb802020-01-08 09:18:15 +0700598 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 Bhuvaragan6f000892016-11-01 14:02:47 +0100606 }
607}
608
Ying Xuec5fa7b32013-06-17 10:54:39 -0400609/**
Ying Xue247f0f32014-02-18 16:06:46 +0800610 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611 * @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 Hideakic4307282007-02-09 23:25:21 +0900618 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100619 * 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 Dunlap637b77f2020-11-29 10:32:48 -0800623 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +0100624 */
Ying Xue247f0f32014-02-18 16:06:46 +0800625static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400628 struct tipc_sock *tsk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100629
Allan Stephens0c3141e2008-04-15 00:22:02 -0700630 /*
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 Stephens0c3141e2008-04-15 00:22:02 -0700634 if (sk == NULL)
635 return 0;
636
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400637 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700638 lock_sock(sk);
639
Tuong Lien01e661e2018-12-19 09:17:58 +0700640 trace_tipc_sk_release(sk, NULL, TIPC_DUMP_ALL, " ");
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100641 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
642 sk->sk_shutdown = SHUTDOWN_MASK;
Jon Maloy75da2162017-10-13 11:04:23 +0200643 tipc_sk_leave(tsk);
Jon Maloy2c98da02021-03-16 22:06:13 -0400644 tipc_sk_withdraw(tsk, NULL);
Hoang Lec55c8ed2019-03-19 18:49:50 +0700645 __skb_queue_purge(&tsk->mc_method.deferredq);
Ying Xue1ea23a22015-05-28 13:19:22 +0800646 sk_stop_timer(sk, &sk->sk_timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800647 tipc_sk_remove(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648
Cong Wang0a3b8b22018-09-03 19:12:41 -0700649 sock_orphan(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700650 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700651 release_sock(sk);
Jon Maloya80ae532017-10-13 11:04:22 +0200652 tipc_dest_list_purge(&tsk->cong_links);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500653 tsk->cong_link_cnt = 0;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800654 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700655 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200657 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100658}
659
660/**
Jon Maloy60c102e2020-11-25 13:29:13 -0500661 * __tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100662 * @sock: socket structure
Jon Maloy60c102e2020-11-25 13:29:13 -0500663 * @skaddr: socket address describing name(s) and desired operation
664 * @alen: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900665 *
gushengxian326af502021-06-09 23:18:53 -0700666 * Name and name sequence binding are indicated using a positive scope value;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667 * 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 Hideakic4307282007-02-09 23:25:21 +0900669 *
Randy Dunlap637b77f2020-11-29 10:32:48 -0800670 * Return: 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700671 *
672 * NOTE: This routine doesn't need to take the socket lock since it doesn't
673 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674 */
Jon Maloy60c102e2020-11-25 13:29:13 -0500675static int __tipc_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676{
Jon Maloy50a34992021-03-16 22:06:11 -0400677 struct tipc_uaddr *ua = (struct tipc_uaddr *)skaddr;
Jon Maloy60c102e2020-11-25 13:29:13 -0500678 struct tipc_sock *tsk = tipc_sk(sock->sk);
Jon Maloy50a34992021-03-16 22:06:11 -0400679 bool unbind = false;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680
Jon Maloy60c102e2020-11-25 13:29:13 -0500681 if (unlikely(!alen))
Jon Maloy2c98da02021-03-16 22:06:13 -0400682 return tipc_sk_withdraw(tsk, NULL);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683
Jon Maloy50a34992021-03-16 22:06:11 -0400684 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 Hideakic4307282007-02-09 23:25:21 +0900695
Jon Maloy60c102e2020-11-25 13:29:13 -0500696 if (tsk->group)
697 return -EACCES;
698
Jon Maloy50a34992021-03-16 22:06:11 -0400699 if (unbind)
Jon Maloy2c98da02021-03-16 22:06:13 -0400700 return tipc_sk_withdraw(tsk, ua);
Jon Maloy50a34992021-03-16 22:06:11 -0400701 return tipc_sk_publish(tsk, ua);
Jon Maloy60c102e2020-11-25 13:29:13 -0500702}
703
704int 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 Xue84602762013-12-27 10:18:28 +0800711 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712}
713
Jon Maloy72671b32020-10-29 21:29:38 -0400714static int tipc_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
715{
Jon Maloy50a34992021-03-16 22:06:11 -0400716 struct tipc_uaddr *ua = (struct tipc_uaddr *)skaddr;
717 u32 atype = ua->addrtype;
Jon Maloy72671b32020-10-29 21:29:38 -0400718
719 if (alen) {
Jon Maloy50a34992021-03-16 22:06:11 -0400720 if (!tipc_uaddr_valid(ua, alen))
Jon Maloy72671b32020-10-29 21:29:38 -0400721 return -EINVAL;
Jon Maloy50a34992021-03-16 22:06:11 -0400722 if (atype == TIPC_SOCKET_ADDR)
Jon Maloy60c102e2020-11-25 13:29:13 -0500723 return -EAFNOSUPPORT;
Jon Maloy50a34992021-03-16 22:06:11 -0400724 if (ua->sr.type < TIPC_RESERVED_TYPES) {
Jon Maloy72671b32020-10-29 21:29:38 -0400725 pr_warn_once("Can't bind to reserved service type %u\n",
Jon Maloy50a34992021-03-16 22:06:11 -0400726 ua->sr.type);
Jon Maloy72671b32020-10-29 21:29:38 -0400727 return -EACCES;
728 }
729 }
730 return tipc_sk_bind(sock, skaddr, alen);
731}
732
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900733/**
Ying Xue247f0f32014-02-18 16:06:46 +0800734 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100735 * @sock: socket structure
736 * @uaddr: area for returned socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700737 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900738 *
Randy Dunlap637b77f2020-11-29 10:32:48 -0800739 * Return: 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700740 *
Allan Stephens2da59912008-07-14 22:43:32 -0700741 * 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 Stephens0e659672010-12-31 18:59:32 +0000743 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100744 */
Ying Xue247f0f32014-02-18 16:06:46 +0800745static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100746 int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100749 struct sock *sk = sock->sk;
750 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100751
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000752 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700753 if (peer) {
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100754 if ((!tipc_sk_connected(sk)) &&
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100755 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
Allan Stephens2da59912008-07-14 22:43:32 -0700756 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400757 addr->addr.id.ref = tsk_peer_port(tsk);
758 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700759 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800760 addr->addr.id.ref = tsk->portid;
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100761 addr->addr.id.node = tipc_own_addr(sock_net(sk));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700762 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100763
Jon Maloyb6f88d92020-11-25 13:29:15 -0500764 addr->addrtype = TIPC_SOCKET_ADDR;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100765 addr->family = AF_TIPC;
766 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100767 addr->addr.name.domain = 0;
768
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100769 return sizeof(*addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100770}
771
772/**
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700773 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100774 * @file: file structure associated with the socket
775 * @sock: socket for which to calculate the poll bits
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700776 * @wait: ???
Per Lidenb97bf3f2006-01-02 19:04:38 +0100777 *
Randy Dunlap637b77f2020-11-29 10:32:48 -0800778 * Return: pollmask value
Allan Stephens9b674e82008-03-26 16:48:21 -0700779 *
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 Stephensf662c072010-08-17 11:00:06 +0000786 * 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 Lidenb97bf3f2006-01-02 19:04:38 +0100789 */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700790static __poll_t tipc_poll(struct file *file, struct socket *sock,
791 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100792{
Allan Stephens9b674e82008-03-26 16:48:21 -0700793 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400794 struct tipc_sock *tsk = tipc_sk(sk);
Al Viroade994f2017-07-03 00:01:49 -0400795 __poll_t revents = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700796
Karsten Graul89ab0662018-10-23 13:40:39 +0200797 sock_poll_wait(file, sock, wait);
Tuong Lien01e661e2018-12-19 09:17:58 +0700798 trace_tipc_sk_poll(sk, NULL, TIPC_DUMP_ALL, " ");
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700799
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100800 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800801 revents |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100802 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800803 revents |= EPOLLHUP;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100804
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100805 switch (sk->sk_state) {
806 case TIPC_ESTABLISHED:
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500807 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800808 revents |= EPOLLOUT;
Miaohe Lin7f8901b2020-08-18 08:07:13 -0400809 fallthrough;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100810 case TIPC_LISTEN:
Parthasarathy Bhuvaraganff946832019-05-09 07:13:42 +0200811 case TIPC_CONNECTING:
Eric Dumazet3ef7cf52019-10-23 22:44:50 -0700812 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800813 revents |= EPOLLIN | EPOLLRDNORM;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100814 break;
815 case TIPC_OPEN:
Jon Maloy60c25302018-01-17 16:42:46 +0100816 if (tsk->group_is_open && !tsk->cong_link_cnt)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800817 revents |= EPOLLOUT;
Jon Maloyae236fb2017-10-13 11:04:25 +0200818 if (!tipc_sk_type_connectionless(sk))
819 break;
Eric Dumazet3ef7cf52019-10-23 22:44:50 -0700820 if (skb_queue_empty_lockless(&sk->sk_receive_queue))
Jon Maloyae236fb2017-10-13 11:04:25 +0200821 break;
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800822 revents |= EPOLLIN | EPOLLRDNORM;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100823 break;
824 case TIPC_DISCONNECTING:
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800825 revents = EPOLLIN | EPOLLRDNORM | EPOLLHUP;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100826 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000827 }
Jon Maloyae236fb2017-10-13 11:04:25 +0200828 return revents;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100829}
830
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400831/**
832 * tipc_sendmcast - send multicast message
833 * @sock: socket structure
Jon Maloy833f8672021-03-16 22:06:17 -0400834 * @ua: destination address struct
Al Viro562640f2014-11-15 01:13:43 -0500835 * @msg: message to send
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500836 * @dlen: length of data to send
837 * @timeout: timeout to wait for wakeup
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400838 *
839 * Called from function tipc_sendmsg(), which has done all sanity checks
Randy Dunlap637b77f2020-11-29 10:32:48 -0800840 * Return: the number of bytes sent on success, or errno
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400841 */
Jon Maloy833f8672021-03-16 22:06:17 -0400842static int tipc_sendmcast(struct socket *sock, struct tipc_uaddr *ua,
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500843 struct msghdr *msg, size_t dlen, long timeout)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400844{
845 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500846 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500847 struct tipc_msg *hdr = &tsk->phdr;
Ying Xuef2f98002015-01-09 15:27:05 +0800848 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500849 int mtu = tipc_bcast_get_mtu(net);
850 struct sk_buff_head pkts;
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500851 struct tipc_nlist dsts;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400852 int rc;
853
Jon Maloy75da2162017-10-13 11:04:23 +0200854 if (tsk->group)
855 return -EACCES;
856
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500857 /* Block or return if any destination link is congested */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500858 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
859 if (unlikely(rc))
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400860 return rc;
861
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500862 /* Lookup destination nodes */
863 tipc_nlist_init(&dsts, tipc_own_addr(net));
Jon Maloy833f8672021-03-16 22:06:17 -0400864 tipc_nametbl_lookup_mcast_nodes(net, ua, &dsts);
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500865 if (!dsts.local && !dsts.remote)
866 return -EHOSTUNREACH;
867
868 /* Build message header */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500869 msg_set_type(hdr, TIPC_MCAST_MSG);
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500870 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500871 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
872 msg_set_destport(hdr, 0);
873 msg_set_destnode(hdr, 0);
Jon Maloy833f8672021-03-16 22:06:17 -0400874 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 Maloy22d85c72015-07-16 16:54:23 -0400877
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500878 /* Build message as chain of buffers */
Jon Maloye654f9f2019-08-15 16:42:50 +0200879 __skb_queue_head_init(&pkts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500880 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500881
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500882 /* Send message if build was successful */
Tuong Lien01e661e2018-12-19 09:17:58 +0700883 if (unlikely(rc == dlen)) {
884 trace_tipc_sk_sendmcast(sk, skb_peek(&pkts),
885 TIPC_DUMP_SK_SNDQ, " ");
Jon Maloy833f8672021-03-16 22:06:17 -0400886 rc = tipc_mcast_xmit(net, &pkts, &tsk->mc_method, &dsts,
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500887 &tsk->cong_link_cnt);
Tuong Lien01e661e2018-12-19 09:17:58 +0700888 }
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500889
890 tipc_nlist_purge(&dsts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500891
892 return rc ? rc : dlen;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400893}
894
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500895/**
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200896 * tipc_send_group_msg - send a message to a member in the group
897 * @net: network namespace
Randy Dunlapf172f4b2020-11-29 10:32:49 -0800898 * @tsk: tipc socket
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200899 * @m: message to send
900 * @mb: group member
901 * @dnode: destination node
902 * @dport: destination port
903 * @dlen: total length of message data
904 */
905static 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 Maloyb87a5ea2017-10-13 11:04:30 +0200909 u16 bc_snd_nxt = tipc_group_bc_snd_nxt(tsk->group);
Jon Maloy2f487712017-10-13 11:04:31 +0200910 struct tipc_mc_method *method = &tsk->mc_method;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200911 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 Maloyb87a5ea2017-10-13 11:04:30 +0200921 msg_set_grp_bc_seqno(hdr, bc_snd_nxt);
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200922
923 /* Build message as chain of buffers */
Jon Maloye654f9f2019-08-15 16:42:50 +0200924 __skb_queue_head_init(&pkts);
Hoang Lef73b1282019-10-29 07:51:21 +0700925 mtu = tipc_node_get_mtu(net, dnode, tsk->portid, false);
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200926 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 Maloy2f487712017-10-13 11:04:31 +0200937 /* Update send window */
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200938 tipc_group_update_member(mb, blks);
939
Jon Maloy2f487712017-10-13 11:04:31 +0200940 /* A broadcast sent within next EXPIRE period must follow same path */
941 method->rcast = true;
942 method->mandatory = true;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200943 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 Dunlap637b77f2020-11-29 10:32:48 -0800954 * Return: the number of bytes sent on success, or errno
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200955 */
956static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m,
957 int dlen, long timeout)
958{
959 struct sock *sk = sock->sk;
Jon Maloy006ed142021-03-16 22:06:18 -0400960 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200961 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
962 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200963 struct net *net = sock_net(sk);
964 struct tipc_member *mb = NULL;
965 u32 node, port;
966 int rc;
967
Jon Maloy006ed142021-03-16 22:06:18 -0400968 node = ua->sk.node;
969 port = ua->sk.ref;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200970 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 Wang143ece62018-12-11 21:43:51 -0800976 tsk->group &&
977 !tipc_group_cong(tsk->group, node, port, blks,
978 &mb));
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200979 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 Maloyee106d72017-10-13 11:04:28 +0200991 * 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 Dunlap637b77f2020-11-29 10:32:48 -0800998 * Return: the number of bytes sent on success, or errno
Jon Maloyee106d72017-10-13 11:04:28 +0200999 */
1000static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m,
1001 int dlen, long timeout)
1002{
Jon Maloy006ed142021-03-16 22:06:18 -04001003 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
Jon Maloyee106d72017-10-13 11:04:28 +02001004 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 Maloy232d07b2018-01-08 21:03:30 +01001008 struct tipc_msg *hdr = &tsk->phdr;
Jon Maloyee106d72017-10-13 11:04:28 +02001009 struct tipc_member *first = NULL;
1010 struct tipc_member *mbr = NULL;
1011 struct net *net = sock_net(sk);
1012 u32 node, port, exclude;
Jon Maloyee106d72017-10-13 11:04:28 +02001013 struct list_head dsts;
1014 int lookups = 0;
1015 int dstcnt, rc;
1016 bool cong;
1017
1018 INIT_LIST_HEAD(&dsts);
Jon Maloy006ed142021-03-16 22:06:18 -04001019 ua->sa.type = msg_nametype(hdr);
1020 ua->scope = msg_lookup_scope(hdr);
Jon Maloyee106d72017-10-13 11:04:28 +02001021
1022 while (++lookups < 4) {
Cong Wang143ece62018-12-11 21:43:51 -08001023 exclude = tipc_group_exclude(tsk->group);
1024
Jon Maloyee106d72017-10-13 11:04:28 +02001025 first = NULL;
1026
1027 /* Look for a non-congested destination member, if any */
1028 while (1) {
Jon Maloy006ed142021-03-16 22:06:18 -04001029 if (!tipc_nametbl_lookup_group(net, ua, &dsts, &dstcnt,
1030 exclude, false))
Jon Maloyee106d72017-10-13 11:04:28 +02001031 return -EHOSTUNREACH;
1032 tipc_dest_pop(&dsts, &node, &port);
Cong Wang143ece62018-12-11 21:43:51 -08001033 cong = tipc_group_cong(tsk->group, node, port, blks,
1034 &mbr);
Jon Maloyee106d72017-10-13 11:04:28 +02001035 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 Wang143ece62018-12-11 21:43:51 -08001053 tsk->group &&
1054 !tipc_group_cong(tsk->group, node, port,
Jon Maloyee106d72017-10-13 11:04:28 +02001055 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 Maloy75da2162017-10-13 11:04:23 +02001073 * tipc_send_group_bcast - send message to all members in communication group
Andrew Lunnd8141202020-07-13 01:15:14 +02001074 * @sock: socket structure
Jon Maloy75da2162017-10-13 11:04:23 +02001075 * @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 Dunlap637b77f2020-11-29 10:32:48 -08001080 * Return: the number of bytes sent on success, or errno
Jon Maloy75da2162017-10-13 11:04:23 +02001081 */
1082static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m,
1083 int dlen, long timeout)
1084{
Jon Maloy006ed142021-03-16 22:06:18 -04001085 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
Jon Maloy75da2162017-10-13 11:04:23 +02001086 struct sock *sk = sock->sk;
1087 struct net *net = sock_net(sk);
1088 struct tipc_sock *tsk = tipc_sk(sk);
Cong Wang3c6306d2018-12-16 23:25:12 -08001089 struct tipc_nlist *dsts;
Jon Maloy75da2162017-10-13 11:04:23 +02001090 struct tipc_mc_method *method = &tsk->mc_method;
Jon Maloy2f487712017-10-13 11:04:31 +02001091 bool ack = method->mandatory && method->rcast;
Jon Maloyb7d42632017-10-13 11:04:26 +02001092 int blks = tsk_blocks(MCAST_H_SIZE + dlen);
Jon Maloy75da2162017-10-13 11:04:23 +02001093 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 Maloyb7d42632017-10-13 11:04:26 +02001098 /* Block or return if any destination link or member is congested */
Cong Wang143ece62018-12-11 21:43:51 -08001099 rc = tipc_wait_for_cond(sock, &timeout,
1100 !tsk->cong_link_cnt && tsk->group &&
1101 !tipc_group_bc_cong(tsk->group, blks));
Jon Maloy75da2162017-10-13 11:04:23 +02001102 if (unlikely(rc))
1103 return rc;
1104
Cong Wang3c6306d2018-12-16 23:25:12 -08001105 dsts = tipc_group_dests(tsk->group);
1106 if (!dsts->local && !dsts->remote)
1107 return -EHOSTUNREACH;
1108
Jon Maloy75da2162017-10-13 11:04:23 +02001109 /* Complete message header */
Jon Maloy006ed142021-03-16 22:06:18 -04001110 if (ua) {
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001111 msg_set_type(hdr, TIPC_GRP_MCAST_MSG);
Jon Maloy006ed142021-03-16 22:06:18 -04001112 msg_set_nameinst(hdr, ua->sa.instance);
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001113 } else {
1114 msg_set_type(hdr, TIPC_GRP_BCAST_MSG);
1115 msg_set_nameinst(hdr, 0);
1116 }
Jon Maloyb7d42632017-10-13 11:04:26 +02001117 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
Jon Maloy75da2162017-10-13 11:04:23 +02001118 msg_set_destport(hdr, 0);
1119 msg_set_destnode(hdr, 0);
Cong Wang143ece62018-12-11 21:43:51 -08001120 msg_set_grp_bc_seqno(hdr, tipc_group_bc_snd_nxt(tsk->group));
Jon Maloy75da2162017-10-13 11:04:23 +02001121
Jon Maloy2f487712017-10-13 11:04:31 +02001122 /* Avoid getting stuck with repeated forced replicasts */
1123 msg_set_grp_bc_ack_req(hdr, ack);
1124
Jon Maloy75da2162017-10-13 11:04:23 +02001125 /* Build message as chain of buffers */
Jon Maloye654f9f2019-08-15 16:42:50 +02001126 __skb_queue_head_init(&pkts);
Jon Maloy75da2162017-10-13 11:04:23 +02001127 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1128 if (unlikely(rc != dlen))
1129 return rc;
1130
1131 /* Send message */
Jon Maloy2f487712017-10-13 11:04:31 +02001132 rc = tipc_mcast_xmit(net, &pkts, method, dsts, &tsk->cong_link_cnt);
Jon Maloy75da2162017-10-13 11:04:23 +02001133 if (unlikely(rc))
1134 return rc;
1135
Jon Maloyb7d42632017-10-13 11:04:26 +02001136 /* Update broadcast sequence number and send windows */
Jon Maloy2f487712017-10-13 11:04:31 +02001137 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 Maloy75da2162017-10-13 11:04:23 +02001143 return dlen;
1144}
1145
1146/**
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001147 * 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 Dunlap637b77f2020-11-29 10:32:48 -08001154 * Return: the number of bytes sent on success, or errno
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001155 */
1156static int tipc_send_group_mcast(struct socket *sock, struct msghdr *m,
1157 int dlen, long timeout)
1158{
Jon Maloy006ed142021-03-16 22:06:18 -04001159 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001160 struct sock *sk = sock->sk;
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001161 struct tipc_sock *tsk = tipc_sk(sk);
1162 struct tipc_group *grp = tsk->group;
Jon Maloy232d07b2018-01-08 21:03:30 +01001163 struct tipc_msg *hdr = &tsk->phdr;
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001164 struct net *net = sock_net(sk);
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001165 struct list_head dsts;
Jon Maloy006ed142021-03-16 22:06:18 -04001166 u32 dstcnt, exclude;
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001167
1168 INIT_LIST_HEAD(&dsts);
Jon Maloy006ed142021-03-16 22:06:18 -04001169 ua->sa.type = msg_nametype(hdr);
1170 ua->scope = msg_lookup_scope(hdr);
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001171 exclude = tipc_group_exclude(grp);
Jon Maloy232d07b2018-01-08 21:03:30 +01001172
Jon Maloy006ed142021-03-16 22:06:18 -04001173 if (!tipc_nametbl_lookup_group(net, ua, &dsts, &dstcnt, exclude, true))
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001174 return -EHOSTUNREACH;
1175
1176 if (dstcnt == 1) {
Jon Maloy006ed142021-03-16 22:06:18 -04001177 tipc_dest_pop(&dsts, &ua->sk.node, &ua->sk.ref);
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001178 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 Maloycb1b7282015-02-05 08:36:44 -05001186 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
Randy Dunlapf172f4b2020-11-29 10:32:49 -08001187 * @net: the associated network namespace
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001188 * @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 Maloy078bec82014-07-16 20:41:00 -04001192 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001193void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
1194 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001195{
Jon Maloy75da2162017-10-13 11:04:23 +02001196 u32 self = tipc_own_addr(net);
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001197 struct sk_buff *skb, *_skb;
Colin Ian Kingb053fcc2018-07-31 17:01:37 +01001198 u32 portid, onode;
Jon Maloy232d07b2018-01-08 21:03:30 +01001199 struct sk_buff_head tmpq;
Jon Maloy75da2162017-10-13 11:04:23 +02001200 struct list_head dports;
Jon Maloy232d07b2018-01-08 21:03:30 +01001201 struct tipc_msg *hdr;
Jon Maloy45ceea22021-03-16 22:06:16 -04001202 struct tipc_uaddr ua;
Jon Maloy232d07b2018-01-08 21:03:30 +01001203 int user, mtyp, hlen;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001204
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001205 __skb_queue_head_init(&tmpq);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001206 INIT_LIST_HEAD(&dports);
Jon Maloy45ceea22021-03-16 22:06:16 -04001207 ua.addrtype = TIPC_SERVICE_RANGE;
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001208
Jon Maloy5ef21322021-06-02 13:44:26 -04001209 /* tipc_skb_peek() increments the head skb's reference counter */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001210 skb = tipc_skb_peek(arrvq, &inputq->lock);
1211 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
Jon Maloy232d07b2018-01-08 21:03:30 +01001212 hdr = buf_msg(skb);
1213 user = msg_user(hdr);
1214 mtyp = msg_type(hdr);
1215 hlen = skb_headroom(skb) + msg_hdr_sz(hdr);
Jon Maloy232d07b2018-01-08 21:03:30 +01001216 onode = msg_orignode(hdr);
Jon Maloy45ceea22021-03-16 22:06:16 -04001217 ua.sr.type = msg_nametype(hdr);
Jon Maloy5ef21322021-06-02 13:44:26 -04001218 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 Maloy232d07b2018-01-08 21:03:30 +01001224
Jon Maloy2f487712017-10-13 11:04:31 +02001225 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 Maloyc545a942017-12-11 19:11:55 +01001231 kfree_skb(skb);
Jon Maloy2f487712017-10-13 11:04:31 +02001232 spin_unlock_bh(&inputq->lock);
1233 continue;
1234 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001235
Jon Maloy232d07b2018-01-08 21:03:30 +01001236 /* Group messages require exact scope match */
1237 if (msg_in_group(hdr)) {
Jon Maloy45ceea22021-03-16 22:06:16 -04001238 ua.sr.lower = 0;
1239 ua.sr.upper = ~0;
1240 ua.scope = msg_lookup_scope(hdr);
Jon Maloy75da2162017-10-13 11:04:23 +02001241 }
Jon Maloy232d07b2018-01-08 21:03:30 +01001242
1243 /* Create destination port list: */
Jon Maloy5ef21322021-06-02 13:44:26 -04001244 tipc_nametbl_lookup_mcast_sockets(net, &ua, &dports);
Jon Maloy232d07b2018-01-08 21:03:30 +01001245
1246 /* Clone message per destination */
Jon Maloya80ae532017-10-13 11:04:22 +02001247 while (tipc_dest_pop(&dports, NULL, &portid)) {
Jon Maloy232d07b2018-01-08 21:03:30 +01001248 _skb = __pskb_copy(skb, hlen, GFP_ATOMIC);
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001249 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 Maloy078bec82014-07-16 20:41:00 -04001255 }
Jon Maloy5ef21322021-06-02 13:44:26 -04001256 /* Append clones to inputq only if skb is still head of arrvq */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001257 spin_lock_bh(&inputq->lock);
1258 if (skb_peek(arrvq) == skb) {
1259 skb_queue_splice_tail_init(&tmpq, inputq);
Jon Maloy5ef21322021-06-02 13:44:26 -04001260 /* Decrement the skb's refcnt */
Hoang Le75016892021-05-14 08:23:03 +07001261 kfree_skb(__skb_dequeue(arrvq));
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001262 }
1263 spin_unlock_bh(&inputq->lock);
1264 __skb_queue_purge(&tmpq);
1265 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001266 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001267 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001268}
1269
Jon Maloyc0bceb92019-10-30 14:00:41 +01001270/* tipc_sk_push_backlog(): send accumulated buffers in socket write queue
1271 * when socket is in Nagle mode
1272 */
Tuong Lien0a3e0602020-05-26 16:38:38 +07001273static void tipc_sk_push_backlog(struct tipc_sock *tsk, bool nagle_ack)
Jon Maloyc0bceb92019-10-30 14:00:41 +01001274{
1275 struct sk_buff_head *txq = &tsk->sk.sk_write_queue;
Tuong Lien0a3e0602020-05-26 16:38:38 +07001276 struct sk_buff *skb = skb_peek_tail(txq);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001277 struct net *net = sock_net(&tsk->sk);
1278 u32 dnode = tsk_peer_node(tsk);
1279 int rc;
1280
Tuong Lien0a3e0602020-05-26 16:38:38 +07001281 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 Nguyend34910e2019-11-28 10:10:08 +07001304 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 Maloyc0bceb92019-10-30 14:00:41 +01001309 return;
1310
Tuong Lien0a3e0602020-05-26 16:38:38 +07001311 if (tsk->msg_acc)
1312 tsk->pkt_cnt += skb_queue_len(txq);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001313 tsk->snt_unacked += tsk->snd_backlog;
1314 tsk->snd_backlog = 0;
Jon Maloyc0bceb92019-10-30 14:00:41 +01001315 rc = tipc_node_xmit(net, txq, dnode, tsk->portid);
1316 if (rc == -ELINKCONG)
1317 tsk->cong_link_cnt = 1;
1318}
1319
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001320/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02001321 * tipc_sk_conn_proto_rcv - receive a connection mng protocol message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001322 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001323 * @skb: pointer to message buffer.
Randy Dunlapf172f4b2020-11-29 10:32:49 -08001324 * @inputq: buffer list containing the buffers
1325 * @xmitq: output message area
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001326 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02001327static void tipc_sk_conn_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
Parthasarathy Bhuvaragane7eb0582018-10-10 17:50:23 +02001328 struct sk_buff_head *inputq,
Jon Maloy64ac5f52017-10-13 11:04:20 +02001329 struct sk_buff_head *xmitq)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001330{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001331 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy64ac5f52017-10-13 11:04:20 +02001332 u32 onode = tsk_own_node(tsk);
1333 struct sock *sk = &tsk->sk;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001334 int mtyp = msg_type(hdr);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001335 bool was_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001336
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001337 /* Ignore if connection cannot be validated: */
Tuong Lien01e661e2018-12-19 09:17:58 +07001338 if (!tsk_peer_msg(tsk, hdr)) {
1339 trace_tipc_sk_drop_msg(sk, skb, TIPC_DUMP_NONE, "@proto_rcv!");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001340 goto exit;
Tuong Lien01e661e2018-12-19 09:17:58 +07001341 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001342
Parthasarathy Bhuvaraganc1be7752017-04-26 10:05:02 +02001343 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 Bhuvaragane7eb0582018-10-10 17:50:23 +02001348
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 Bhuvaraganc1be7752017-04-26 10:05:02 +02001358 }
1359
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001360 tsk->probe_unacked = false;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001361
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001362 if (mtyp == CONN_PROBE) {
1363 msg_set_type(hdr, CONN_PROBE_REPLY);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001364 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
1365 __skb_queue_tail(xmitq, skb);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001366 return;
1367 } else if (mtyp == CONN_ACK) {
Jon Maloyc0bceb92019-10-30 14:00:41 +01001368 was_cong = tsk_conn_cong(tsk);
Tuong Lien0a3e0602020-05-26 16:38:38 +07001369 tipc_sk_push_backlog(tsk, msg_nagle_ack(hdr));
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001370 tsk->snt_unacked -= msg_conn_ack(hdr);
1371 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1372 tsk->snd_win = msg_adv_win(hdr);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001373 if (was_cong && !tsk_conn_cong(tsk))
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001374 sk->sk_write_space(sk);
1375 } else if (mtyp != CONN_PROBE_REPLY) {
1376 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001377 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001378exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001379 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001380}
1381
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001382/**
Ying Xue247f0f32014-02-18 16:06:46 +08001383 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +01001384 * @sock: socket structure
1385 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001386 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001387 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001388 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001389 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
1391 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001392 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08001393 * Return: the number of bytes sent on success, or errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394 */
Ying Xue1b784142015-03-02 15:37:48 +08001395static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001396 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001397{
Ying Xue39a0295f2015-03-02 15:37:47 +08001398 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 Maloy365ad352017-01-03 10:55:11 -05001408static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a0295f2015-03-02 15:37:47 +08001409{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001410 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001411 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001412 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy908148b2021-03-16 22:06:15 -04001413 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001414 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 Maloy75da2162017-10-13 11:04:23 +02001417 struct tipc_group *grp = tsk->group;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001418 struct tipc_msg *hdr = &tsk->phdr;
Jon Maloy908148b2021-03-16 22:06:15 -04001419 struct tipc_socket_addr skaddr;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001420 struct sk_buff_head pkts;
Jon Maloy908148b2021-03-16 22:06:15 -04001421 int atype, mtu, rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001423 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
Allan Stephensc29c3f72010-04-20 17:58:24 -04001424 return -EMSGSIZE;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001425
Jon Maloy908148b2021-03-16 22:06:15 -04001426 if (ua) {
1427 if (!tipc_uaddr_valid(ua, m->msg_namelen))
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001428 return -EINVAL;
Colin Ian King743902c2021-09-03 00:00:11 +01001429 atype = ua->addrtype;
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001430 }
1431
Jon Maloy908148b2021-03-16 22:06:15 -04001432 /* If socket belongs to a communication group follow other paths */
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001433 if (grp) {
Jon Maloy908148b2021-03-16 22:06:15 -04001434 if (!ua)
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001435 return tipc_send_group_bcast(sock, m, dlen, timeout);
Jon Maloy908148b2021-03-16 22:06:15 -04001436 if (atype == TIPC_SERVICE_ADDR)
Jon Maloyee106d72017-10-13 11:04:28 +02001437 return tipc_send_group_anycast(sock, m, dlen, timeout);
Jon Maloy908148b2021-03-16 22:06:15 -04001438 if (atype == TIPC_SOCKET_ADDR)
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001439 return tipc_send_group_unicast(sock, m, dlen, timeout);
Jon Maloy908148b2021-03-16 22:06:15 -04001440 if (atype == TIPC_SERVICE_RANGE)
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001441 return tipc_send_group_mcast(sock, m, dlen, timeout);
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001442 return -EINVAL;
1443 }
Jon Maloy75da2162017-10-13 11:04:23 +02001444
Jon Maloy908148b2021-03-16 22:06:15 -04001445 if (!ua) {
1446 ua = (struct tipc_uaddr *)&tsk->peer;
1447 if (!syn && ua->family != AF_TIPC)
Erik Hugnef2f80362015-03-19 09:02:19 +01001448 return -EDESTADDRREQ;
Jon Maloy02fdc142021-03-29 13:17:31 -04001449 atype = ua->addrtype;
Erik Hugnef2f80362015-03-19 09:02:19 +01001450 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001451
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001452 if (unlikely(syn)) {
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001453 if (sk->sk_state == TIPC_LISTEN)
Ying Xue39a0295f2015-03-02 15:37:47 +08001454 return -EPIPE;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001455 if (sk->sk_state != TIPC_OPEN)
Ying Xue39a0295f2015-03-02 15:37:47 +08001456 return -EISCONN;
1457 if (tsk->published)
1458 return -EOPNOTSUPP;
Jon Maloy14623e02021-06-02 13:44:24 -04001459 if (atype == TIPC_SERVICE_ADDR)
1460 tsk->conn_addrtype = atype;
Jon Maloy25b92212018-09-28 20:23:21 +02001461 msg_set_syn(hdr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001462 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001463
Haimin Zhangd6d86832021-12-31 10:35:23 +08001464 memset(&skaddr, 0, sizeof(skaddr));
1465
Jon Maloy908148b2021-03-16 22:06:15 -04001466 /* Determine destination */
1467 if (atype == TIPC_SERVICE_RANGE) {
Jon Maloy833f8672021-03-16 22:06:17 -04001468 return tipc_sendmcast(sock, ua, m, dlen, timeout);
Jon Maloy908148b2021-03-16 22:06:15 -04001469 } 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 Xue39a0295f2015-03-02 15:37:47 +08001473 return -EHOSTUNREACH;
Jon Maloy908148b2021-03-16 22:06:15 -04001474 } else if (atype == TIPC_SOCKET_ADDR) {
1475 skaddr = ua->sk;
Jon Maloy335b9292018-04-12 01:15:48 +02001476 } else {
1477 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001478 }
1479
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001480 /* Block or return if destination link is congested */
Jon Maloya80ae532017-10-13 11:04:22 +02001481 rc = tipc_wait_for_cond(sock, &timeout,
Jon Maloy908148b2021-03-16 22:06:15 -04001482 !tipc_dest_find(clinks, skaddr.node, 0));
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001483 if (unlikely(rc))
Ying Xue39a0295f2015-03-02 15:37:47 +08001484 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001485
Jon Maloy908148b2021-03-16 22:06:15 -04001486 /* 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 Lienabc9b4e2019-12-10 15:21:04 +07001490 msg_set_type(hdr, TIPC_NAMED_MSG);
1491 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
Jon Maloy908148b2021-03-16 22:06:15 -04001492 msg_set_nametype(hdr, ua->sa.type);
1493 msg_set_nameinst(hdr, ua->sa.instance);
1494 msg_set_lookup_scope(hdr, ua->scope);
Jon Maloyb6f88d92020-11-25 13:29:15 -05001495 } else { /* TIPC_SOCKET_ADDR */
Tuong Lienabc9b4e2019-12-10 15:21:04 +07001496 msg_set_type(hdr, TIPC_DIRECT_MSG);
1497 msg_set_lookup_scope(hdr, 0);
Tuong Lienabc9b4e2019-12-10 15:21:04 +07001498 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
1499 }
1500
Jon Maloy908148b2021-03-16 22:06:15 -04001501 /* Add message body */
Jon Maloye654f9f2019-08-15 16:42:50 +02001502 __skb_queue_head_init(&pkts);
Jon Maloy908148b2021-03-16 22:06:15 -04001503 mtu = tipc_node_get_mtu(net, skaddr.node, tsk->portid, true);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001504 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1505 if (unlikely(rc != dlen))
1506 return rc;
Tung Nguyen2fe97a52019-11-28 10:10:05 +07001507 if (unlikely(syn && !tipc_msg_skb_clone(&pkts, &sk->sk_write_queue))) {
1508 __skb_queue_purge(&pkts);
Tung Nguyen67879272018-09-28 20:23:22 +02001509 return -ENOMEM;
Tung Nguyen2fe97a52019-11-28 10:10:05 +07001510 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001511
Jon Maloy908148b2021-03-16 22:06:15 -04001512 /* Send message */
Tuong Lien01e661e2018-12-19 09:17:58 +07001513 trace_tipc_sk_sendmsg(sk, skb_peek(&pkts), TIPC_DUMP_SK_SNDQ, " ");
Jon Maloy908148b2021-03-16 22:06:15 -04001514 rc = tipc_node_xmit(net, &pkts, skaddr.node, tsk->portid);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001515 if (unlikely(rc == -ELINKCONG)) {
Jon Maloy908148b2021-03-16 22:06:15 -04001516 tipc_dest_push(clinks, skaddr.node, 0);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001517 tsk->cong_link_cnt++;
1518 rc = 0;
1519 }
1520
Xin Longf8dd60d2021-07-22 12:05:41 -04001521 if (unlikely(syn && !rc)) {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001522 tipc_set_sk_state(sk, TIPC_CONNECTING);
Xin Long7387a722021-08-15 03:13:36 -04001523 if (dlen && timeout) {
Xin Longf8dd60d2021-07-22 12:05:41 -04001524 timeout = msecs_to_jiffies(timeout);
1525 tipc_wait_for_connect(sock, &timeout);
1526 }
1527 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001528
1529 return rc ? rc : dlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001530}
1531
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001532/**
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001533 * tipc_sendstream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001534 * @sock: socket structure
1535 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001536 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001537 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001538 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001539 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08001540 * Return: the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001541 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001542 */
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001543static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001544{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001545 struct sock *sk = sock->sk;
Ying Xue39a0295f2015-03-02 15:37:47 +08001546 int ret;
1547
1548 lock_sock(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001549 ret = __tipc_sendstream(sock, m, dsz);
Ying Xue39a0295f2015-03-02 15:37:47 +08001550 release_sock(sk);
1551
1552 return ret;
1553}
1554
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001555static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a0295f2015-03-02 15:37:47 +08001556{
1557 struct sock *sk = sock->sk;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001558 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001559 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001560 struct sk_buff_head *txq = &sk->sk_write_queue;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001561 struct tipc_sock *tsk = tipc_sk(sk);
1562 struct tipc_msg *hdr = &tsk->phdr;
1563 struct net *net = sock_net(sk);
Tuong Lien0a3e0602020-05-26 16:38:38 +07001564 struct sk_buff *skb;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001565 u32 dnode = tsk_peer_node(tsk);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001566 int maxnagle = tsk->maxnagle;
1567 int maxpkt = tsk->max_pkt;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001568 int send, sent = 0;
Jon Maloyc0bceb92019-10-30 14:00:41 +01001569 int blocks, rc = 0;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001570
1571 if (unlikely(dlen > INT_MAX))
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001572 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001573
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001574 /* Handle implicit connection setup */
Xin Longf8dd60d2021-07-22 12:05:41 -04001575 if (unlikely(dest && sk->sk_state == TIPC_OPEN)) {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001576 rc = __tipc_sendmsg(sock, m, dlen);
Parthasarathy Bhuvaragan92ef12b2018-09-25 18:21:58 +02001577 if (dlen && dlen == rc) {
1578 tsk->peer_caps = tipc_node_get_capabilities(net, dnode);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001579 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
Parthasarathy Bhuvaragan92ef12b2018-09-25 18:21:58 +02001580 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001581 return rc;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001582 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001583
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001584 do {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001585 rc = tipc_wait_for_cond(sock, &timeout,
1586 (!tsk->cong_link_cnt &&
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -05001587 !tsk_conn_cong(tsk) &&
1588 tipc_sk_connected(sk)));
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001589 if (unlikely(rc))
1590 break;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001591 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001592 blocks = tsk->snd_backlog;
Tuong Lienc9aa81f2020-06-11 17:07:35 +07001593 if (tsk->oneway++ >= tsk->nagle_start && maxnagle &&
1594 send <= maxnagle) {
Jon Maloyc0bceb92019-10-30 14:00:41 +01001595 rc = tipc_msg_append(hdr, m, send, maxnagle, txq);
1596 if (unlikely(rc < 0))
1597 break;
1598 blocks += rc;
Tuong Lien0a3e0602020-05-26 16:38:38 +07001599 tsk->msg_acc++;
Jon Maloyc0bceb92019-10-30 14:00:41 +01001600 if (blocks <= 64 && tsk->expect_ack) {
1601 tsk->snd_backlog = blocks;
1602 sent += send;
1603 break;
Tuong Lien0a3e0602020-05-26 16:38:38 +07001604 } else if (blocks > 64) {
1605 tsk->pkt_cnt += skb_queue_len(txq);
1606 } else {
1607 skb = skb_peek_tail(txq);
YueHaibing4c21daa2020-05-28 22:34:07 +08001608 if (skb) {
1609 msg_set_ack_required(buf_msg(skb));
1610 tsk->expect_ack = true;
1611 } else {
1612 tsk->expect_ack = false;
1613 }
Tuong Lien0a3e0602020-05-26 16:38:38 +07001614 tsk->msg_acc = 0;
1615 tsk->pkt_cnt = 0;
Jon Maloyc0bceb92019-10-30 14:00:41 +01001616 }
Jon Maloyc0bceb92019-10-30 14:00:41 +01001617 } 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 Lien01e661e2018-12-19 09:17:58 +07001624 TIPC_DUMP_SK_SNDQ, " ");
Jon Maloyc0bceb92019-10-30 14:00:41 +01001625 rc = tipc_node_xmit(net, txq, dnode, tsk->portid);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001626 if (unlikely(rc == -ELINKCONG)) {
1627 tsk->cong_link_cnt = 1;
1628 rc = 0;
1629 }
1630 if (likely(!rc)) {
Jon Maloyc0bceb92019-10-30 14:00:41 +01001631 tsk->snt_unacked += blocks;
1632 tsk->snd_backlog = 0;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001633 sent += send;
1634 }
1635 } while (sent < dlen && !rc);
1636
Parthasarathy Bhuvaragan3364d612017-04-24 15:00:42 +02001637 return sent ? sent : rc;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001638}
1639
1640/**
1641 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001642 * @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 Dunlap637b77f2020-11-29 10:32:48 -08001648 * Return: the number of bytes sent on success, or errno otherwise
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001649 */
Ying Xue1b784142015-03-02 15:37:48 +08001650static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001651{
1652 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1653 return -EMSGSIZE;
1654
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001655 return tipc_sendstream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656}
1657
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001658/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001659 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001660static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001661 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001662{
Ying Xue3721e9c2015-01-13 17:07:48 +08001663 struct sock *sk = &tsk->sk;
1664 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001665 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666
Jon Maloy25b92212018-09-28 20:23:21 +02001667 msg_set_syn(msg, 0);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001668 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 Maloyf9fef182014-03-12 11:31:08 -04001673
Jon Maloy0d5fcebf2017-10-20 11:21:32 +02001674 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001675 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
Ying Xuef2f98002015-01-09 15:27:05 +08001676 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
Hoang Lef73b1282019-10-29 07:51:21 +07001677 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid, true);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001678 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001679 tsk_set_nagle(tsk);
Tung Nguyen67879272018-09-28 20:23:22 +02001680 __skb_queue_purge(&sk->sk_write_queue);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001681 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 Lidenb97bf3f2006-01-02 19:04:38 +01001687}
1688
1689/**
Jon Maloy31c82a22017-10-13 11:04:24 +02001690 * tipc_sk_set_orig_addr - capture sender's address for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001691 * @m: descriptor for message info
Andrew Lunnd8141202020-07-13 01:15:14 +02001692 * @skb: received message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001693 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001694 * Note: Address is not captured if not requested by receiver.
1695 */
Jon Maloy31c82a22017-10-13 11:04:24 +02001696static void tipc_sk_set_orig_addr(struct msghdr *m, struct sk_buff *skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001697{
Jon Maloy31c82a22017-10-13 11:04:24 +02001698 DECLARE_SOCKADDR(struct sockaddr_pair *, srcaddr, m->msg_name);
1699 struct tipc_msg *hdr = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001700
Jon Maloy31c82a22017-10-13 11:04:24 +02001701 if (!srcaddr)
1702 return;
1703
1704 srcaddr->sock.family = AF_TIPC;
Jon Maloyb6f88d92020-11-25 13:29:15 -05001705 srcaddr->sock.addrtype = TIPC_SOCKET_ADDR;
Eric Dumazet09c8b972018-05-09 09:50:22 -07001706 srcaddr->sock.scope = 0;
Jon Maloy31c82a22017-10-13 11:04:24 +02001707 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 Maloy31c82a22017-10-13 11:04:24 +02001710 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 Maloyb6f88d92020-11-25 13:29:15 -05001717 srcaddr->member.addrtype = TIPC_SERVICE_ADDR;
Eric Dumazet09c8b972018-05-09 09:50:22 -07001718 srcaddr->member.scope = 0;
Jon Maloy31c82a22017-10-13 11:04:24 +02001719 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 Lidenb97bf3f2006-01-02 19:04:38 +01001723}
1724
1725/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001726 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001727 * @m: descriptor for message info
Jon Maloy1c1274a2018-11-17 12:17:06 -05001728 * @skb: received message buffer
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001729 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001730 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001731 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001732 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08001733 * Return: 0 if successful, otherwise errno
Per Lidenb97bf3f2006-01-02 19:04:38 +01001734 */
Jon Maloy1c1274a2018-11-17 12:17:06 -05001735static int tipc_sk_anc_data_recv(struct msghdr *m, struct sk_buff *skb,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001736 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001737{
Jon Maloy62633c22021-06-02 13:44:25 -04001738 struct tipc_msg *hdr;
1739 u32 data[3] = {0,};
1740 bool has_addr;
1741 int dlen, rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001742
1743 if (likely(m->msg_controllen == 0))
1744 return 0;
1745
Jon Maloy62633c22021-06-02 13:44:25 -04001746 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 Lidenb97bf3f2006-01-02 19:04:38 +01001762 }
1763
Jon Maloy62633c22021-06-02 13:44:25 -04001764 /* Capture TIPC_SERVICE_ADDR/RANGE destination address, if any */
1765 switch (msg_type(hdr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001766 case TIPC_NAMED_MSG:
Jon Maloy62633c22021-06-02 13:44:25 -04001767 has_addr = true;
1768 data[0] = msg_nametype(hdr);
1769 data[1] = msg_namelower(hdr);
1770 data[2] = data[1];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001771 break;
1772 case TIPC_MCAST_MSG:
Jon Maloy62633c22021-06-02 13:44:25 -04001773 has_addr = true;
1774 data[0] = msg_nametype(hdr);
1775 data[1] = msg_namelower(hdr);
1776 data[2] = msg_nameupper(hdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001777 break;
1778 case TIPC_CONN_MSG:
Jon Maloy62633c22021-06-02 13:44:25 -04001779 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 Lidenb97bf3f2006-01-02 19:04:38 +01001783 break;
1784 default:
Jon Maloy62633c22021-06-02 13:44:25 -04001785 has_addr = false;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001786 }
Jon Maloy62633c22021-06-02 13:44:25 -04001787 if (!has_addr)
1788 return 0;
1789 return put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, data);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001790}
1791
Tuong Lienc7268582020-05-13 19:33:16 +07001792static struct sk_buff *tipc_sk_build_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001793{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001794 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08001795 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001796 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001797 u32 peer_port = tsk_peer_port(tsk);
1798 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001799
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001800 if (!tipc_sk_connected(sk))
Tuong Lienc7268582020-05-13 19:33:16 +07001801 return NULL;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001802 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 Xuea6ca1092014-11-26 11:41:55 +08001805 if (!skb)
Tuong Lienc7268582020-05-13 19:33:16 +07001806 return NULL;
Ying Xuea6ca1092014-11-26 11:41:55 +08001807 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001808 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 Lienc7268582020-05-13 19:33:16 +07001816 return skb;
1817}
1818
1819static 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 Maloy739f5e42014-08-22 18:09:12 -04001829}
1830
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001831static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001832{
1833 struct sock *sk = sock->sk;
Tung Nguyen48766a52019-02-19 11:20:48 +07001834 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001835 long timeo = *timeop;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001836 int err = sock_error(sk);
1837
1838 if (err)
1839 return err;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001840
1841 for (;;) {
Ying Xuefe8e4642014-03-06 14:40:18 +01001842 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01001843 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001844 err = -ENOTCONN;
1845 break;
1846 }
Tung Nguyen48766a52019-02-19 11:20:48 +07001847 add_wait_queue(sk_sleep(sk), &wait);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001848 release_sock(sk);
Tung Nguyen48766a52019-02-19 11:20:48 +07001849 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
1850 sched_annotate_sleep();
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001851 lock_sock(sk);
Tung Nguyen48766a52019-02-19 11:20:48 +07001852 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001853 }
1854 err = 0;
1855 if (!skb_queue_empty(&sk->sk_receive_queue))
1856 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001857 err = -EAGAIN;
1858 if (!timeo)
1859 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001860 err = sock_intr_errno(timeo);
1861 if (signal_pending(current))
1862 break;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001863
1864 err = sock_error(sk);
1865 if (err)
1866 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001867 }
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001868 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001869 return err;
1870}
1871
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001872/**
Ying Xue247f0f32014-02-18 16:06:46 +08001873 * tipc_recvmsg - receive packet-oriented message
Randy Dunlapf172f4b2020-11-29 10:32:49 -08001874 * @sock: network socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001875 * @m: descriptor for message info
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001876 * @buflen: length of user buffer area
Per Lidenb97bf3f2006-01-02 19:04:38 +01001877 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001878 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001879 * 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 Dunlap637b77f2020-11-29 10:32:48 -08001882 * Return: size of returned message data, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001883 */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001884static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
1885 size_t buflen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001887 struct sock *sk = sock->sk;
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001888 bool connected = !tipc_sk_type_connectionless(sk);
Jon Maloyae236fb2017-10-13 11:04:25 +02001889 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001890 int rc, err, hlen, dlen, copy;
Xin Longf4919ff2021-07-16 17:44:07 -04001891 struct tipc_skb_cb *skb_cb;
Jon Maloyb7d42632017-10-13 11:04:26 +02001892 struct sk_buff_head xmitq;
Jon Maloyae236fb2017-10-13 11:04:25 +02001893 struct tipc_msg *hdr;
1894 struct sk_buff *skb;
1895 bool grp_evt;
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001896 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001897
Allan Stephens0c3141e2008-04-15 00:22:02 -07001898 /* Catch invalid receive requests */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001899 if (unlikely(!buflen))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001900 return -EINVAL;
1901
Allan Stephens0c3141e2008-04-15 00:22:02 -07001902 lock_sock(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001903 if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
1904 rc = -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001905 goto exit;
1906 }
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001907 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001908
Jon Maloyb7d42632017-10-13 11:04:26 +02001909 /* Step rcv queue to first msg with data or error; wait if necessary */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001910 do {
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001911 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1912 if (unlikely(rc))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001913 goto exit;
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001914 skb = skb_peek(&sk->sk_receive_queue);
Xin Longf4919ff2021-07-16 17:44:07 -04001915 skb_cb = TIPC_SKB_CB(skb);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001916 hdr = buf_msg(skb);
1917 dlen = msg_data_sz(hdr);
1918 hlen = msg_hdr_sz(hdr);
1919 err = msg_errcode(hdr);
Jon Maloyae236fb2017-10-13 11:04:25 +02001920 grp_evt = msg_is_grp_evt(hdr);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001921 if (likely(dlen || err))
1922 break;
1923 tsk_advance_rx_queue(sk);
1924 } while (1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001925
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001926 /* Collect msg meta data, including error code and rejected data */
Jon Maloy31c82a22017-10-13 11:04:24 +02001927 tipc_sk_set_orig_addr(m, skb);
Jon Maloy1c1274a2018-11-17 12:17:06 -05001928 rc = tipc_sk_anc_data_recv(m, skb, tsk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001929 if (unlikely(rc))
1930 goto exit;
Jon Maloy1c1274a2018-11-17 12:17:06 -05001931 hdr = buf_msg(skb);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001932
1933 /* Capture data if non-error msg, otherwise just set return value */
1934 if (likely(!err)) {
Xin Longf4919ff2021-07-16 17:44:07 -04001935 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 Maloye9f8b102017-05-02 18:16:53 +02001954 } else {
1955 copy = 0;
1956 rc = 0;
Xin Longf4919ff2021-07-16 17:44:07 -04001957 if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control) {
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001958 rc = -ECONNRESET;
Xin Longf4919ff2021-07-16 17:44:07 -04001959 goto exit;
1960 }
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001961 }
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001962
Jon Maloyae236fb2017-10-13 11:04:25 +02001963 /* 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 Maloye9f8b102017-05-02 18:16:53 +02001971 /* Caption of data or error code/rejected data was successful */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001972 if (unlikely(flags & MSG_PEEK))
1973 goto exit;
1974
Jon Maloyb7d42632017-10-13 11:04:26 +02001975 /* Send group flow control advertisement when applicable */
1976 if (tsk->group && msg_in_group(hdr) && !grp_evt) {
Jon Maloye654f9f2019-08-15 16:42:50 +02001977 __skb_queue_head_init(&xmitq);
Jon Maloyb7d42632017-10-13 11:04:26 +02001978 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 Longcc198622021-07-23 13:25:36 -04001984 if (skb_cb->bytes_read)
1985 goto exit;
Jon Maloyae236fb2017-10-13 11:04:25 +02001986
Xin Longcc198622021-07-23 13:25:36 -04001987 tsk_advance_rx_queue(sk);
1988
1989 if (likely(!connected))
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001990 goto exit;
1991
Jon Maloyb7d42632017-10-13 11:04:26 +02001992 /* Send connection flow control advertisement when applicable */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001993 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 Lidenb97bf3f2006-01-02 19:04:38 +01001996exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001997 release_sock(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001998 return rc ? rc : copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001999}
2000
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002001/**
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002002 * tipc_recvstream - receive stream-oriented data
Randy Dunlapf172f4b2020-11-29 10:32:49 -08002003 * @sock: network socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01002004 * @m: descriptor for message info
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002005 * @buflen: total size of user buffer area
Per Lidenb97bf3f2006-01-02 19:04:38 +01002006 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002007 *
2008 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01002009 * will optionally wait for more; never truncates data.
2010 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08002011 * Return: size of returned message data, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01002012 */
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002013static int tipc_recvstream(struct socket *sock, struct msghdr *m,
2014 size_t buflen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002015{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002016 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002017 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002018 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 Lidenb97bf3f2006-01-02 19:04:38 +01002025
2026 /* Catch invalid receive attempts */
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002027 if (unlikely(!buflen))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002028 return -EINVAL;
2029
Allan Stephens0c3141e2008-04-15 00:22:02 -07002030 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002031
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01002032 if (unlikely(sk->sk_state == TIPC_OPEN)) {
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002033 rc = -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002034 goto exit;
2035 }
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002036 required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
2037 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002038
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002039 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 Gortmaker617d3c72012-04-30 15:29:02 -04002050
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002051 /* Discard any empty non-errored (SYN-) message */
2052 if (unlikely(!dlen && !err)) {
2053 tsk_advance_rx_queue(sk);
2054 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002055 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002056
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002057 /* Collect msg meta data, incl. error code and rejected data */
2058 if (!copied) {
Jon Maloy31c82a22017-10-13 11:04:24 +02002059 tipc_sk_set_orig_addr(m, skb);
Jon Maloy1c1274a2018-11-17 12:17:06 -05002060 rc = tipc_sk_anc_data_recv(m, skb, tsk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002061 if (rc)
2062 break;
Jon Maloy1c1274a2018-11-17 12:17:06 -05002063 hdr = buf_msg(skb);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002064 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002065
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002066 /* 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 Maloy10724cc2016-05-02 11:58:47 -04002087
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002088 if (unlikely(peek))
2089 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002090
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002091 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002092
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002093 /* Send connection flow control advertisement when applicable */
2094 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
Tuong Lienc7268582020-05-13 19:33:16 +07002095 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002096 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 Lidenb97bf3f2006-01-02 19:04:38 +01002103exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002104 release_sock(sk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002105 return copied ? copied : rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002106}
2107
2108/**
Ying Xuef288bef2012-08-21 11:16:57 +08002109 * tipc_write_space - wake up thread if port congestion is released
2110 * @sk: socket
2111 */
2112static 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 Xu1ce0bf52015-11-26 13:55:39 +08002118 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002119 wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
2120 EPOLLWRNORM | EPOLLWRBAND);
Ying Xuef288bef2012-08-21 11:16:57 +08002121 rcu_read_unlock();
2122}
2123
2124/**
2125 * tipc_data_ready - wake up threads to indicate messages have been received
2126 * @sk: socket
Ying Xuef288bef2012-08-21 11:16:57 +08002127 */
David S. Miller676d2362014-04-11 16:15:36 -04002128static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08002129{
2130 struct socket_wq *wq;
2131
2132 rcu_read_lock();
2133 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08002134 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002135 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
2136 EPOLLRDNORM | EPOLLRDBAND);
Ying Xuef288bef2012-08-21 11:16:57 +08002137 rcu_read_unlock();
2138}
2139
Ying Xuef4195d12015-11-22 15:46:05 +08002140static void tipc_sock_destruct(struct sock *sk)
2141{
2142 __skb_queue_purge(&sk->sk_receive_queue);
2143}
2144
Jon Maloy64ac5f52017-10-13 11:04:20 +02002145static 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 Maloy75da2162017-10-13 11:04:23 +02002152 struct tipc_group *grp = tsk->group;
Jon Maloyb7d42632017-10-13 11:04:26 +02002153 bool wakeup = false;
Jon Maloy64ac5f52017-10-13 11:04:20 +02002154
2155 switch (msg_user(hdr)) {
2156 case CONN_MANAGER:
Parthasarathy Bhuvaragane7eb0582018-10-10 17:50:23 +02002157 tipc_sk_conn_proto_rcv(tsk, skb, inputq, xmitq);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002158 return;
2159 case SOCK_WAKEUP:
Jon Maloya80ae532017-10-13 11:04:22 +02002160 tipc_dest_del(&tsk->cong_links, msg_orignode(hdr), 0);
Tung Nguyenbfd07f32019-02-25 10:57:20 +07002161 /* coupled with smp_rmb() in tipc_wait_for_cond() */
2162 smp_wmb();
Jon Maloy64ac5f52017-10-13 11:04:20 +02002163 tsk->cong_link_cnt--;
Jon Maloyb7d42632017-10-13 11:04:26 +02002164 wakeup = true;
Tuong Lien0a3e0602020-05-26 16:38:38 +07002165 tipc_sk_push_backlog(tsk, false);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002166 break;
Jon Maloy75da2162017-10-13 11:04:23 +02002167 case GROUP_PROTOCOL:
Jon Maloyb7d42632017-10-13 11:04:26 +02002168 tipc_group_proto_rcv(grp, &wakeup, hdr, inputq, xmitq);
Jon Maloy75da2162017-10-13 11:04:23 +02002169 break;
Jon Maloy64ac5f52017-10-13 11:04:20 +02002170 case TOP_SRV:
Jon Maloyb7d42632017-10-13 11:04:26 +02002171 tipc_group_member_evt(tsk->group, &wakeup, &sk->sk_rcvbuf,
Jon Maloy7ad32bc2018-01-08 21:03:26 +01002172 hdr, inputq, xmitq);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002173 break;
2174 default:
2175 break;
2176 }
2177
Jon Maloyb7d42632017-10-13 11:04:26 +02002178 if (wakeup)
2179 sk->sk_write_space(sk);
2180
Jon Maloy64ac5f52017-10-13 11:04:20 +02002181 kfree_skb(skb);
2182}
2183
Ying Xuef288bef2012-08-21 11:16:57 +08002184/**
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002185 * tipc_sk_filter_connect - check incoming message for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002186 * @tsk: TIPC socket
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002187 * @skb: pointer to message buffer.
Tuong Lienc7268582020-05-13 19:33:16 +07002188 * @xmitq: for Nagle ACK if any
Randy Dunlap637b77f2020-11-29 10:32:48 -08002189 * Return: true if message should be added to receive queue, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05002190 */
Tuong Lienc7268582020-05-13 19:33:16 +07002191static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb,
2192 struct sk_buff_head *xmitq)
Ying Xue7e6c1312012-11-29 18:39:14 -05002193{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002194 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08002195 struct net *net = sock_net(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04002196 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002197 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 Nguyen67879272018-09-28 20:23:22 +02002203 unsigned long delay;
Ying Xue7e6c1312012-11-29 18:39:14 -05002204
Jon Paul Maloycda36962015-07-22 10:11:20 -04002205 if (unlikely(msg_mcast(hdr)))
2206 return false;
Jon Maloyc0bceb92019-10-30 14:00:41 +01002207 tsk->oneway = 0;
Ying Xue7e6c1312012-11-29 18:39:14 -05002208
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002209 switch (sk->sk_state) {
2210 case TIPC_CONNECTING:
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002211 /* 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 Bhuvaraganff946832019-05-09 07:13:42 +02002221 sk->sk_state_change(sk);
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002222 msg_set_dest_droppable(hdr, 1);
2223 return false;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02002224 }
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002225 /* Ignore connectionless message if not from listening socket */
2226 if (oport != pport || onode != pnode)
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002227 return false;
2228
Tung Nguyen67879272018-09-28 20:23:22 +02002229 /* 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 Maloy39fdc9c2018-09-28 20:23:20 +02002241 case TIPC_OPEN:
2242 case TIPC_DISCONNECTING:
2243 return false;
2244 case TIPC_LISTEN:
2245 /* Accept only SYN message */
Jon Maloy25b92212018-09-28 20:23:21 +02002246 if (!msg_is_syn(hdr) &&
2247 tipc_node_get_capabilities(net, onode) & TIPC_SYN_BIT)
2248 return false;
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002249 if (!con_msg && !err)
2250 return true;
2251 return false;
2252 case TIPC_ESTABLISHED:
Jon Maloyc0bceb92019-10-30 14:00:41 +01002253 if (!skb_queue_empty(&sk->sk_write_queue))
Tuong Lien0a3e0602020-05-26 16:38:38 +07002254 tipc_sk_push_backlog(tsk, false);
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002255 /* Accept only connection-based messages sent by peer */
Tuong Lienc7268582020-05-13 19:33:16 +07002256 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 Lien0a3e0602020-05-26 16:38:38 +07002262 if (skb) {
2263 msg_set_nagle_ack(buf_msg(skb));
Tuong Lienc7268582020-05-13 19:33:16 +07002264 __skb_queue_tail(xmitq, skb);
Tuong Lien0a3e0602020-05-26 16:38:38 +07002265 }
Tuong Lienc7268582020-05-13 19:33:16 +07002266 }
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002267 return true;
Tuong Lienc7268582020-05-13 19:33:16 +07002268 }
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002269 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 Bhuvaraganf40acba2016-11-01 14:02:49 +01002276 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05002277 default:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01002278 pr_err("Unknown sk_state %u\n", sk->sk_state);
Ying Xue7e6c1312012-11-29 18:39:14 -05002279 }
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002280 /* 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 Xue7e6c1312012-11-29 18:39:14 -05002285}
2286
2287/**
Ying Xueaba79f32013-01-20 23:30:09 +01002288 * rcvbuf_limit - get proper overload limit of socket receive queue
2289 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002290 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01002291 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002292 * For connection oriented messages, irrespective of importance,
2293 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01002294 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002295 * For connectionless messages, queue limits are based on message
2296 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01002297 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002298 * TIPC_LOW_IMPORTANCE (2 MB)
2299 * TIPC_MEDIUM_IMPORTANCE (4 MB)
2300 * TIPC_HIGH_IMPORTANCE (8 MB)
2301 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01002302 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08002303 * Return: overload limit according to corresponding message importance
Ying Xueaba79f32013-01-20 23:30:09 +01002304 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002305static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01002306{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002307 struct tipc_sock *tsk = tipc_sk(sk);
2308 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01002309
Jon Maloyb7d42632017-10-13 11:04:26 +02002310 if (unlikely(msg_in_group(hdr)))
Eric Dumazet82657922019-10-09 15:21:13 -07002311 return READ_ONCE(sk->sk_rcvbuf);
Jon Maloyb7d42632017-10-13 11:04:26 +02002312
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002313 if (unlikely(!msg_connected(hdr)))
Eric Dumazet82657922019-10-09 15:21:13 -07002314 return READ_ONCE(sk->sk_rcvbuf) << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08002315
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002316 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
Eric Dumazet82657922019-10-09 15:21:13 -07002317 return READ_ONCE(sk->sk_rcvbuf);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002318
2319 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01002320}
2321
2322/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02002323 * tipc_sk_filter_rcv - validate incoming message
Allan Stephens0c3141e2008-04-15 00:22:02 -07002324 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04002325 * @skb: pointer to message.
Randy Dunlapf172f4b2020-11-29 10:32:49 -08002326 * @xmitq: output message area (FIXME)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002327 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07002328 * Enqueues message on receive queue if acceptable; optionally handles
2329 * disconnect indication for a connected socket.
2330 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05002331 * Called with socket lock already taken
Per Lidenb97bf3f2006-01-02 19:04:38 +01002332 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02002333static void tipc_sk_filter_rcv(struct sock *sk, struct sk_buff *skb,
2334 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002335{
Jon Maloy64ac5f52017-10-13 11:04:20 +02002336 bool sk_conn = !tipc_sk_type_connectionless(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002337 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy75da2162017-10-13 11:04:23 +02002338 struct tipc_group *grp = tsk->group;
Jon Paul Maloycda36962015-07-22 10:11:20 -04002339 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002340 struct net *net = sock_net(sk);
2341 struct sk_buff_head inputq;
Hoang Le77d5ad42019-03-21 17:25:17 +07002342 int mtyp = msg_type(hdr);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002343 int limit, err = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002344
Tuong Lien01e661e2018-12-19 09:17:58 +07002345 trace_tipc_sk_filter_rcv(sk, skb, TIPC_DUMP_ALL, " ");
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01002346 TIPC_SKB_CB(skb)->bytes_read = 0;
Jon Maloy64ac5f52017-10-13 11:04:20 +02002347 __skb_queue_head_init(&inputq);
2348 __skb_queue_tail(&inputq, skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002349
Jon Maloy64ac5f52017-10-13 11:04:20 +02002350 if (unlikely(!msg_isdata(hdr)))
2351 tipc_sk_proto_rcv(sk, &inputq, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04002352
Jon Maloy75da2162017-10-13 11:04:23 +02002353 if (unlikely(grp))
2354 tipc_group_filter_msg(grp, &inputq, xmitq);
2355
Hoang Le77d5ad42019-03-21 17:25:17 +07002356 if (unlikely(!grp) && mtyp == TIPC_MCAST_MSG)
Hoang Le08e046c2019-03-21 17:25:18 +07002357 tipc_mcast_filter_msg(net, &tsk->mc_method.deferredq, &inputq);
Hoang Lec55c8ed2019-03-19 18:49:50 +07002358
Jon Maloy64ac5f52017-10-13 11:04:20 +02002359 /* 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 Lienc7268582020-05-13 19:33:16 +07002363 if ((sk_conn && !tipc_sk_filter_connect(tsk, skb, xmitq)) ||
Jon Maloy75da2162017-10-13 11:04:23 +02002364 (!sk_conn && msg_connected(hdr)) ||
2365 (!grp && msg_in_group(hdr)))
Jon Maloy64ac5f52017-10-13 11:04:20 +02002366 err = TIPC_ERR_NO_PORT;
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01002367 else if (sk_rmem_alloc_get(sk) + skb->truesize >= limit) {
Tuong Lien01e661e2018-12-19 09:17:58 +07002368 trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL,
2369 "err_overload2!");
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01002370 atomic_inc(&sk->sk_drops);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002371 err = TIPC_ERR_OVERLOAD;
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01002372 }
Jon Maloy64ac5f52017-10-13 11:04:20 +02002373
2374 if (unlikely(err)) {
Tuong Lien01e661e2018-12-19 09:17:58 +07002375 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 Maloy64ac5f52017-10-13 11:04:20 +02002380 err = TIPC_OK;
2381 continue;
2382 }
2383 __skb_queue_tail(&sk->sk_receive_queue, skb);
2384 skb_set_owner_r(skb, sk);
Tuong Lien01e661e2018-12-19 09:17:58 +07002385 trace_tipc_sk_overlimit2(sk, skb, TIPC_DUMP_ALL,
2386 "rcvq >90% allocated!");
Jon Maloy64ac5f52017-10-13 11:04:20 +02002387 sk->sk_data_ready(sk);
2388 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002389}
2390
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002391/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02002392 * tipc_sk_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07002393 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08002394 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07002395 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05002396 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07002397 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02002398static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07002399{
Jon Maloy64ac5f52017-10-13 11:04:20 +02002400 unsigned int before = sk_rmem_alloc_get(sk);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002401 struct sk_buff_head xmitq;
Jon Maloy64ac5f52017-10-13 11:04:20 +02002402 unsigned int added;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002403
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002404 __skb_queue_head_init(&xmitq);
2405
Jon Maloy64ac5f52017-10-13 11:04:20 +02002406 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 Maloyf70d37b2017-10-13 11:04:21 +02002411 tipc_node_distr_xmit(sock_net(sk), &xmitq);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002412 return 0;
2413}
2414
2415/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002416 * 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 Dunlapf172f4b2020-11-29 10:32:49 -08002421 * @xmitq: output queue
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002422 *
2423 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002424 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04002425static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002426 u32 dport, struct sk_buff_head *xmitq)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002427{
Hoang Lef4bb62e2021-09-13 16:28:52 +07002428 unsigned long time_limit = jiffies + usecs_to_jiffies(20000);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002429 struct sk_buff *skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002430 unsigned int lim;
2431 atomic_t *dcnt;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002432 u32 onode;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002433
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002434 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05002435 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04002436 return;
2437
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002438 skb = tipc_skb_dequeue(inputq, dport);
2439 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04002440 return;
2441
2442 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002443 if (!sock_owned_by_user(sk)) {
Jon Maloy64ac5f52017-10-13 11:04:20 +02002444 tipc_sk_filter_rcv(sk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04002445 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002446 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04002447
2448 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002449 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04002450 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002451 atomic_set(dcnt, 0);
2452 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
Tuong Lien01e661e2018-12-19 09:17:58 +07002453 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 Maloyc637c102015-02-05 08:36:41 -05002456 continue;
Tuong Lien01e661e2018-12-19 09:17:58 +07002457 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04002458
Tuong Lien01e661e2018-12-19 09:17:58 +07002459 trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL, "err_overload!");
Jon Paul Maloycda36962015-07-22 10:11:20 -04002460 /* Overload => reject message back to sender */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002461 onode = tipc_own_addr(sock_net(sk));
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01002462 atomic_inc(&sk->sk_drops);
Tuong Lien01e661e2018-12-19 09:17:58 +07002463 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 Maloyf1d048f2016-06-17 06:35:57 -04002466 __skb_queue_tail(xmitq, skb);
Tuong Lien01e661e2018-12-19 09:17:58 +07002467 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04002468 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002469 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002470}
2471
2472/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002473 * tipc_sk_rcv - handle a chain of incoming buffers
Randy Dunlapf172f4b2020-11-29 10:32:49 -08002474 * @net: the associated network namespace
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002475 * @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 Stephens0c3141e2008-04-15 00:22:02 -07002478 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04002479void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07002480{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002481 struct sk_buff_head xmitq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002482 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04002483 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04002484 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04002485 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04002486 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04002487
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002488 __skb_queue_head_init(&xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002489 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002490 dport = tipc_skb_peek_port(inputq, dport);
2491 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04002492
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002493 if (likely(tsk)) {
2494 sk = &tsk->sk;
2495 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002496 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002497 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002498 }
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002499 /* Send pending response/rejected messages, if any */
Jon Maloyf70d37b2017-10-13 11:04:21 +02002500 tipc_node_distr_xmit(sock_net(sk), &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002501 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002502 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002503 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04002504 /* 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 Maloyc637c102015-02-05 08:36:41 -05002516 continue;
Tuong Lien01e661e2018-12-19 09:17:58 +07002517
2518 trace_tipc_sk_rej_msg(NULL, skb, TIPC_DUMP_NONE, "@sk_rcv!");
Jon Paul Maloye3a77562015-02-05 08:36:39 -05002519xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04002520 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002521 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002522 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07002523}
2524
Ying Xue78eb3a52014-01-17 09:50:03 +08002525static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
2526{
WANG Congd9dc8b02016-11-11 10:20:50 -08002527 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Ying Xue78eb3a52014-01-17 09:50:03 +08002528 struct sock *sk = sock->sk;
Ying Xue78eb3a52014-01-17 09:50:03 +08002529 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 Lien5391a872020-02-10 15:35:44 +07002539 if (sk->sk_state == TIPC_DISCONNECTING)
2540 break;
Ying Xue78eb3a52014-01-17 09:50:03 +08002541
WANG Congd9dc8b02016-11-11 10:20:50 -08002542 add_wait_queue(sk_sleep(sk), &wait);
Tuong Lien9546a0b2020-01-08 09:19:00 +07002543 done = sk_wait_event(sk, timeo_p, tipc_sk_connected(sk),
2544 &wait);
WANG Congd9dc8b02016-11-11 10:20:50 -08002545 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue78eb3a52014-01-17 09:50:03 +08002546 } while (!done);
2547 return 0;
2548}
2549
Erik Hugneea239312019-03-17 18:46:42 +01002550static 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 Lidenb97bf3f2006-01-02 19:04:38 +01002560/**
Ying Xue247f0f32014-02-18 16:06:46 +08002561 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01002562 * @sock: socket structure
2563 * @dest: socket address for destination port
2564 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07002565 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01002566 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08002567 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01002568 */
Ying Xue247f0f32014-02-18 16:06:46 +08002569static int tipc_connect(struct socket *sock, struct sockaddr *dest,
2570 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002571{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002572 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01002573 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07002574 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
2575 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01002576 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002577 int previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01002578 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002579
Jon Maloy23998832017-10-13 11:04:18 +02002580 if (destlen != sizeof(struct sockaddr_tipc))
2581 return -EINVAL;
2582
Allan Stephens0c3141e2008-04-15 00:22:02 -07002583 lock_sock(sk);
2584
Jon Maloy75da2162017-10-13 11:04:23 +02002585 if (tsk->group) {
2586 res = -EINVAL;
2587 goto exit;
2588 }
2589
Jon Maloy23998832017-10-13 11:04:18 +02002590 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 Maloy23998832017-10-13 11:04:18 +02002595 }
Erik Hugneea239312019-03-17 18:46:42 +01002596 if (!tipc_sockaddr_is_sane(dst)) {
Jon Maloy23998832017-10-13 11:04:18 +02002597 res = -EINVAL;
Jon Maloy23998832017-10-13 11:04:18 +02002598 goto exit;
Erik Hugneea239312019-03-17 18:46:42 +01002599 }
Erik Hugnef2f80362015-03-19 09:02:19 +01002600 /* DGRAM/RDM connect(), just save the destaddr */
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +01002601 if (tipc_sk_type_connectionless(sk)) {
Jon Maloy23998832017-10-13 11:04:18 +02002602 memcpy(&tsk->peer, dest, destlen);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002603 goto exit;
Erik Hugneea239312019-03-17 18:46:42 +01002604 } else if (dst->addrtype == TIPC_SERVICE_RANGE) {
2605 res = -EINVAL;
2606 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002607 }
2608
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002609 previous = sk->sk_state;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01002610
2611 switch (sk->sk_state) {
2612 case TIPC_OPEN:
Ying Xue584d24b2012-11-29 18:51:19 -05002613 /* 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 Xue39a0295f2015-03-02 15:37:47 +08002623 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05002624 if ((res < 0) && (res != -EWOULDBLOCK))
2625 goto exit;
2626
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002627 /* Just entered TIPC_CONNECTING state; the only
Ying Xue584d24b2012-11-29 18:51:19 -05002628 * difference is that return value in non-blocking
2629 * case is EINPROGRESS, rather than EALREADY.
2630 */
2631 res = -EINPROGRESS;
Miaohe Lin7f8901b2020-08-18 08:07:13 -04002632 fallthrough;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002633 case TIPC_CONNECTING:
2634 if (!timeout) {
2635 if (previous == TIPC_CONNECTING)
2636 res = -EALREADY;
Ying Xue78eb3a52014-01-17 09:50:03 +08002637 goto exit;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002638 }
Ying Xue78eb3a52014-01-17 09:50:03 +08002639 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 Bhuvaraganf40acba2016-11-01 14:02:49 +01002642 break;
2643 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002644 res = -EISCONN;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002645 break;
2646 default:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002647 res = -EINVAL;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002648 }
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002649
Allan Stephens0c3141e2008-04-15 00:22:02 -07002650exit:
2651 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07002652 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002653}
2654
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002655/**
Ying Xue247f0f32014-02-18 16:06:46 +08002656 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01002657 * @sock: socket structure
2658 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002659 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08002660 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01002661 */
Ying Xue247f0f32014-02-18 16:06:46 +08002662static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002663{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002664 struct sock *sk = sock->sk;
2665 int res;
2666
2667 lock_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002668 res = tipc_set_sk_state(sk, TIPC_LISTEN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002669 release_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002670
Allan Stephens0c3141e2008-04-15 00:22:02 -07002671 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002672}
2673
Ying Xue6398e232014-01-17 09:50:04 +08002674static int tipc_wait_for_accept(struct socket *sock, long timeo)
2675{
2676 struct sock *sk = sock->sk;
Hoang Led237a7f2021-07-23 09:25:34 +07002677 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Ying Xue6398e232014-01-17 09:50:04 +08002678 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 Xuefe8e4642014-03-06 14:40:18 +01002686 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Hoang Led237a7f2021-07-23 09:25:34 +07002687 add_wait_queue(sk_sleep(sk), &wait);
Ying Xue6398e232014-01-17 09:50:04 +08002688 release_sock(sk);
Hoang Led237a7f2021-07-23 09:25:34 +07002689 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
Ying Xue6398e232014-01-17 09:50:04 +08002690 lock_sock(sk);
Hoang Led237a7f2021-07-23 09:25:34 +07002691 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue6398e232014-01-17 09:50:04 +08002692 }
2693 err = 0;
2694 if (!skb_queue_empty(&sk->sk_receive_queue))
2695 break;
Ying Xue6398e232014-01-17 09:50:04 +08002696 err = -EAGAIN;
2697 if (!timeo)
2698 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002699 err = sock_intr_errno(timeo);
2700 if (signal_pending(current))
2701 break;
Ying Xue6398e232014-01-17 09:50:04 +08002702 }
Ying Xue6398e232014-01-17 09:50:04 +08002703 return err;
2704}
2705
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002706/**
Ying Xue247f0f32014-02-18 16:06:46 +08002707 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002708 * @sock: listening socket
Andrew Lunnd8141202020-07-13 01:15:14 +02002709 * @new_sock: new socket that is to be connected
Per Lidenb97bf3f2006-01-02 19:04:38 +01002710 * @flags: file-related flags associated with socket
Randy Dunlapf172f4b2020-11-29 10:32:49 -08002711 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002712 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08002713 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01002714 */
David Howellscdfbabf2017-03-09 08:09:05 +00002715static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
2716 bool kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002717{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002718 struct sock *new_sk, *sk = sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002719 struct tipc_sock *new_tsock;
Xin Longf8dd60d2021-07-22 12:05:41 -04002720 struct msghdr m = {NULL,};
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002721 struct tipc_msg *msg;
Xin Longf8dd60d2021-07-22 12:05:41 -04002722 struct sk_buff *buf;
Ying Xue6398e232014-01-17 09:50:04 +08002723 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002724 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002725
Allan Stephens0c3141e2008-04-15 00:22:02 -07002726 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002727
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002728 if (sk->sk_state != TIPC_LISTEN) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002729 res = -EINVAL;
2730 goto exit;
2731 }
Ying Xue6398e232014-01-17 09:50:04 +08002732 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2733 res = tipc_wait_for_accept(sock, timeo);
2734 if (res)
2735 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002736
2737 buf = skb_peek(&sk->sk_receive_queue);
2738
David Howellscdfbabf2017-03-09 08:09:05 +00002739 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002740 if (res)
2741 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002742 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002743
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002744 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002745 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002746 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002747
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002748 /* we lock on new_sk; but lockdep sees the lock on sk */
2749 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002750
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002751 /*
2752 * Reject any stray messages received by new socket
2753 * before the socket lock was taken (very, very unlikely)
2754 */
Tuong Lien49afb802020-01-08 09:18:15 +07002755 tsk_rej_rx_queue(new_sk, TIPC_ERR_NO_PORT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002756
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002757 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002758 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002759
Christoph Hellwig095ae612020-05-28 07:12:36 +02002760 tsk_set_importance(new_sk, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002761 if (msg_named(msg)) {
Jon Maloy14623e02021-06-02 13:44:24 -04002762 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 Lidenb97bf3f2006-01-02 19:04:38 +01002765 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002766
2767 /*
Xin Longf8dd60d2021-07-22 12:05:41 -04002768 * Respond to 'SYN-' by discarding it & returning 'ACK'.
2769 * Respond to 'SYN+' by queuing it on new socket & returning 'ACK'.
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002770 */
2771 if (!msg_data_sz(msg)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002772 tsk_advance_rx_queue(sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002773 } else {
2774 __skb_dequeue(&sk->sk_receive_queue);
2775 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002776 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002777 }
Xin Longf8dd60d2021-07-22 12:05:41 -04002778 __tipc_sendstream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002779 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002780exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002781 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002782 return res;
2783}
2784
2785/**
Ying Xue247f0f32014-02-18 16:06:46 +08002786 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002787 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002788 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002789 *
2790 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002791 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08002792 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01002793 */
Ying Xue247f0f32014-02-18 16:06:46 +08002794static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002795{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002796 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002797 int res;
2798
Allan Stephense247a8f2008-03-06 15:05:38 -08002799 if (how != SHUT_RDWR)
2800 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002801
Allan Stephens0c3141e2008-04-15 00:22:02 -07002802 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002803
Tuong Lien01e661e2018-12-19 09:17:58 +07002804 trace_tipc_sk_shutdown(sk, NULL, TIPC_DUMP_ALL, " ");
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002805 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
Tetsuo Handaa4b5cc92020-09-05 15:14:47 +09002806 sk->sk_shutdown = SHUTDOWN_MASK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002807
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002808 if (sk->sk_state == TIPC_DISCONNECTING) {
Ying Xue75031152012-10-29 09:38:15 -04002809 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002810 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002811
Per Lidenb97bf3f2006-01-02 19:04:38 +01002812 res = 0;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002813 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002814 res = -ENOTCONN;
2815 }
Tetsuo Handa2a638662020-09-02 22:44:16 +09002816 /* Wake up anyone sleeping in poll. */
2817 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002818
Allan Stephens0c3141e2008-04-15 00:22:02 -07002819 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002820 return res;
2821}
2822
Jon Maloyafe87922018-09-28 20:23:19 +02002823static 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 Nguyen67879272018-09-28 20:23:22 +02002849static 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 Cook31b102b2017-10-30 14:06:45 -07002862static void tipc_sk_timeout(struct timer_list *t)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002863{
Kees Cook31b102b2017-10-30 14:06:45 -07002864 struct sock *sk = from_timer(sk, t, sk_timer);
2865 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloyafe87922018-09-28 20:23:19 +02002866 u32 pnode = tsk_peer_node(tsk);
2867 struct sk_buff_head list;
Tung Nguyen67879272018-09-28 20:23:22 +02002868 int rc = 0;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002869
Jon Maloye654f9f2019-08-15 16:42:50 +02002870 __skb_queue_head_init(&list);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002871 bh_lock_sock(sk);
Jon Maloy0d5fcebf2017-10-20 11:21:32 +02002872
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 Maloyafe87922018-09-28 20:23:19 +02002876 bh_unlock_sock(sk);
Tung Nguyen91a4a3e2019-11-28 10:10:06 +07002877 sock_put(sk);
Jon Maloyafe87922018-09-28 20:23:19 +02002878 return;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002879 }
Jon Paul Maloy57289012014-08-22 18:09:09 -04002880
Jon Maloyafe87922018-09-28 20:23:19 +02002881 if (sk->sk_state == TIPC_ESTABLISHED)
2882 tipc_sk_check_probing_state(sk, &list);
Tung Nguyen67879272018-09-28 20:23:22 +02002883 else if (sk->sk_state == TIPC_CONNECTING)
2884 tipc_sk_retry_connect(sk, &list);
Jon Maloyafe87922018-09-28 20:23:19 +02002885
Jon Paul Maloy57289012014-08-22 18:09:09 -04002886 bh_unlock_sock(sk);
Jon Maloyafe87922018-09-28 20:23:19 +02002887
2888 if (!skb_queue_empty(&list))
Tung Nguyen67879272018-09-28 20:23:22 +02002889 rc = tipc_node_xmit(sock_net(sk), &list, pnode, tsk->portid);
Jon Maloyafe87922018-09-28 20:23:19 +02002890
Tung Nguyen67879272018-09-28 20:23:22 +02002891 /* 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 Xue07f6c4b2015-01-07 13:41:58 +08002896 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002897}
2898
Jon Maloy50a34992021-03-16 22:06:11 -04002899static int tipc_sk_publish(struct tipc_sock *tsk, struct tipc_uaddr *ua)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002900{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002901 struct sock *sk = &tsk->sk;
2902 struct net *net = sock_net(sk);
Jon Maloy50a34992021-03-16 22:06:11 -04002903 struct tipc_socket_addr skaddr;
2904 struct publication *p;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002905 u32 key;
2906
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002907 if (tipc_sk_connected(sk))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002908 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002909 key = tsk->portid + tsk->pub_count + 1;
2910 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002911 return -EADDRINUSE;
Jon Maloy50a34992021-03-16 22:06:11 -04002912 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 Maloy0fc87aa2014-08-22 18:09:17 -04002916 return -EINVAL;
2917
Jon Maloy50a34992021-03-16 22:06:11 -04002918 list_add(&p->binding_sock, &tsk->publications);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002919 tsk->pub_count++;
Jon Maloy50a34992021-03-16 22:06:11 -04002920 tsk->published = true;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002921 return 0;
2922}
2923
Jon Maloy2c98da02021-03-16 22:06:13 -04002924static int tipc_sk_withdraw(struct tipc_sock *tsk, struct tipc_uaddr *ua)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002925{
Ying Xuef2f98002015-01-09 15:27:05 +08002926 struct net *net = sock_net(&tsk->sk);
Jon Maloy2c98da02021-03-16 22:06:13 -04002927 struct publication *safe, *p;
2928 struct tipc_uaddr _ua;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002929 int rc = -EINVAL;
2930
Jon Maloy998d3902021-03-16 22:06:08 -04002931 list_for_each_entry_safe(p, safe, &tsk->publications, binding_sock) {
Jon Maloy2c98da02021-03-16 22:06:13 -04002932 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 Maloy0fc87aa2014-08-22 18:09:17 -04002937 }
Jon Maloy2c98da02021-03-16 22:06:13 -04002938 /* 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 Maloy0fc87aa2014-08-22 18:09:17 -04002953 rc = 0;
2954 }
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002955 return rc;
2956}
2957
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002958/* tipc_sk_reinit: set non-zero address in all existing sockets
2959 * when we go from standalone to network mode.
2960 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002961void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002962{
Ying Xuee05b31f2015-01-09 15:27:08 +08002963 struct tipc_net *tn = net_generic(net, tipc_net_id);
Herbert Xu40f9f432017-02-11 19:26:46 +08002964 struct rhashtable_iter iter;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002965 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002966 struct tipc_msg *msg;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002967
Herbert Xu40f9f432017-02-11 19:26:46 +08002968 rhashtable_walk_enter(&tn->sk_rht, &iter);
2969
2970 do {
Tom Herbert97a6ec42017-12-04 10:31:41 -08002971 rhashtable_walk_start(&iter);
Herbert Xu40f9f432017-02-11 19:26:46 +08002972
2973 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
Cong Wang15ef70e2018-12-10 11:49:55 -08002974 sock_hold(&tsk->sk);
2975 rhashtable_walk_stop(&iter);
2976 lock_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002977 msg = &tsk->phdr;
Jon Maloy23fd3ea2018-03-22 20:42:49 +01002978 msg_set_prevnode(msg, tipc_own_addr(net));
2979 msg_set_orignode(msg, tipc_own_addr(net));
Cong Wang15ef70e2018-12-10 11:49:55 -08002980 release_sock(&tsk->sk);
2981 rhashtable_walk_start(&iter);
2982 sock_put(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002983 }
Tom Herbert97a6ec42017-12-04 10:31:41 -08002984
Herbert Xu40f9f432017-02-11 19:26:46 +08002985 rhashtable_walk_stop(&iter);
2986 } while (tsk == ERR_PTR(-EAGAIN));
Cong Wangbd583fe2018-08-23 16:19:44 -07002987
2988 rhashtable_walk_exit(&iter);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002989}
2990
Ying Xuee05b31f2015-01-09 15:27:08 +08002991static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002992{
Ying Xuee05b31f2015-01-09 15:27:08 +08002993 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002994 struct tipc_sock *tsk;
2995
2996 rcu_read_lock();
Taehee Yooab818362019-11-22 08:15:19 +00002997 tsk = rhashtable_lookup(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002998 if (tsk)
2999 sock_hold(&tsk->sk);
3000 rcu_read_unlock();
3001
3002 return tsk;
3003}
3004
3005static int tipc_sk_insert(struct tipc_sock *tsk)
3006{
Ying Xuee05b31f2015-01-09 15:27:08 +08003007 struct sock *sk = &tsk->sk;
3008 struct net *net = sock_net(sk);
3009 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08003010 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 Xu6cca72892015-03-20 21:57:05 +11003019 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
3020 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08003021 return 0;
3022 sock_put(&tsk->sk);
3023 }
3024
3025 return -1;
3026}
3027
3028static void tipc_sk_remove(struct tipc_sock *tsk)
3029{
3030 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08003031 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08003032
Herbert Xu6cca72892015-03-20 21:57:05 +11003033 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003034 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
Ying Xue07f6c4b2015-01-07 13:41:58 +08003035 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04003036 }
3037}
3038
Herbert Xu6cca72892015-03-20 21:57:05 +11003039static 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 Xu6cca72892015-03-20 21:57:05 +11003044 .max_size = 1048576,
3045 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00003046 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11003047};
3048
Ying Xuee05b31f2015-01-09 15:27:08 +08003049int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003050{
Ying Xuee05b31f2015-01-09 15:27:08 +08003051 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003052
Herbert Xu6cca72892015-03-20 21:57:05 +11003053 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003054}
3055
Ying Xuee05b31f2015-01-09 15:27:08 +08003056void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003057{
Ying Xuee05b31f2015-01-09 15:27:08 +08003058 struct tipc_net *tn = net_generic(net, tipc_net_id);
3059
Ying Xue07f6c4b2015-01-07 13:41:58 +08003060 /* Wait for socket readers to complete */
3061 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003062
Ying Xuee05b31f2015-01-09 15:27:08 +08003063 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003064}
3065
Jon Maloy75da2162017-10-13 11:04:23 +02003066static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq)
3067{
3068 struct net *net = sock_net(&tsk->sk);
Jon Maloy75da2162017-10-13 11:04:23 +02003069 struct tipc_group *grp = tsk->group;
3070 struct tipc_msg *hdr = &tsk->phdr;
Jon Maloy50a34992021-03-16 22:06:11 -04003071 struct tipc_uaddr ua;
Jon Maloy75da2162017-10-13 11:04:23 +02003072 int rc;
3073
3074 if (mreq->type < TIPC_RESERVED_TYPES)
3075 return -EACCES;
Jon Maloy232d07b2018-01-08 21:03:30 +01003076 if (mreq->scope > TIPC_NODE_SCOPE)
3077 return -EINVAL;
Jon Maloy50a34992021-03-16 22:06:11 -04003078 if (mreq->scope != TIPC_NODE_SCOPE)
3079 mreq->scope = TIPC_CLUSTER_SCOPE;
Jon Maloy75da2162017-10-13 11:04:23 +02003080 if (grp)
3081 return -EACCES;
Jon Maloy60c25302018-01-17 16:42:46 +01003082 grp = tipc_group_create(net, tsk->portid, mreq, &tsk->group_is_open);
Jon Maloy75da2162017-10-13 11:04:23 +02003083 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 Maloy50a34992021-03-16 22:06:11 -04003089 tipc_uaddr(&ua, TIPC_SERVICE_RANGE, mreq->scope,
3090 mreq->type, mreq->instance, mreq->instance);
Jon Maloy6e448672021-03-16 22:06:20 -04003091 tipc_nametbl_build_group(net, grp, &ua);
Jon Maloy50a34992021-03-16 22:06:11 -04003092 rc = tipc_sk_publish(tsk, &ua);
Cong Wange233df02017-10-24 15:44:49 -07003093 if (rc) {
Jon Maloy75da2162017-10-13 11:04:23 +02003094 tipc_group_delete(net, grp);
Cong Wange233df02017-10-24 15:44:49 -07003095 tsk->group = NULL;
Jon Maloyfebafc82018-01-10 21:08:50 +01003096 return rc;
Cong Wange233df02017-10-24 15:44:49 -07003097 }
Jon Maloyd12d2e12018-01-08 21:03:28 +01003098 /* Eliminate any risk that a broadcast overtakes sent JOINs */
Jon Maloy399574d2017-10-13 11:04:32 +02003099 tsk->mc_method.rcast = true;
3100 tsk->mc_method.mandatory = true;
Jon Maloyd12d2e12018-01-08 21:03:28 +01003101 tipc_group_join(net, grp, &tsk->sk.sk_rcvbuf);
Jon Maloy75da2162017-10-13 11:04:23 +02003102 return rc;
3103}
3104
3105static 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 Maloy2c98da02021-03-16 22:06:13 -04003109 struct tipc_uaddr ua;
Jon Maloy75da2162017-10-13 11:04:23 +02003110 int scope;
3111
3112 if (!grp)
3113 return -EINVAL;
Jon Maloy2c98da02021-03-16 22:06:13 -04003114 ua.addrtype = TIPC_SERVICE_RANGE;
3115 tipc_group_self(grp, &ua.sr, &scope);
3116 ua.scope = scope;
Jon Maloy75da2162017-10-13 11:04:23 +02003117 tipc_group_delete(net, grp);
3118 tsk->group = NULL;
Jon Maloy2c98da02021-03-16 22:06:13 -04003119 tipc_sk_withdraw(tsk, &ua);
Jon Maloy75da2162017-10-13 11:04:23 +02003120 return 0;
3121}
3122
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003123/**
Ying Xue247f0f32014-02-18 16:06:46 +08003124 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01003125 * @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 Hideakic4307282007-02-09 23:25:21 +09003130 *
3131 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01003132 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003133 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08003134 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01003135 */
Ying Xue247f0f32014-02-18 16:06:46 +08003136static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003137 sockptr_t ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003138{
Allan Stephens0c3141e2008-04-15 00:22:02 -07003139 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04003140 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy75da2162017-10-13 11:04:23 +02003141 struct tipc_group_req mreq;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003142 u32 value = 0;
Dan Carpentera08ef472017-01-24 12:49:35 +03003143 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003144
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003145 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
3146 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003147 if (lvl != SOL_TIPC)
3148 return -ENOPROTOOPT;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003149
3150 switch (opt) {
3151 case TIPC_IMPORTANCE:
3152 case TIPC_SRC_DROPPABLE:
3153 case TIPC_DEST_DROPPABLE:
3154 case TIPC_CONN_TIMEOUT:
Jon Maloyc0bceb92019-10-30 14:00:41 +01003155 case TIPC_NODELAY:
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003156 if (ol < sizeof(value))
3157 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003158 if (copy_from_sockptr(&value, ov, sizeof(u32)))
Jon Maloy75da2162017-10-13 11:04:23 +02003159 return -EFAULT;
3160 break;
3161 case TIPC_GROUP_JOIN:
3162 if (ol < sizeof(mreq))
3163 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003164 if (copy_from_sockptr(&mreq, ov, sizeof(mreq)))
Jon Maloy75da2162017-10-13 11:04:23 +02003165 return -EFAULT;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003166 break;
3167 default:
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003168 if (!sockptr_is_null(ov) || ol)
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003169 return -EINVAL;
3170 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01003171
Allan Stephens0c3141e2008-04-15 00:22:02 -07003172 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003173
Per Lidenb97bf3f2006-01-02 19:04:38 +01003174 switch (opt) {
3175 case TIPC_IMPORTANCE:
Christoph Hellwig095ae612020-05-28 07:12:36 +02003176 res = tsk_set_importance(sk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003177 break;
3178 case TIPC_SRC_DROPPABLE:
3179 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003180 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003181 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01003182 res = -ENOPROTOOPT;
3183 break;
3184 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003185 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003186 break;
3187 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04003188 tipc_sk(sk)->conn_timeout = value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003189 break;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003190 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 Maloy75da2162017-10-13 11:04:23 +02003198 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 Maloyc0bceb92019-10-30 14:00:41 +01003204 case TIPC_NODELAY:
3205 tsk->nodelay = !!value;
3206 tsk_set_nagle(tsk);
3207 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003208 default:
3209 res = -EINVAL;
3210 }
3211
Allan Stephens0c3141e2008-04-15 00:22:02 -07003212 release_sock(sk);
3213
Per Lidenb97bf3f2006-01-02 19:04:38 +01003214 return res;
3215}
3216
3217/**
Ying Xue247f0f32014-02-18 16:06:46 +08003218 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01003219 * @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 Hideakic4307282007-02-09 23:25:21 +09003224 *
3225 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01003226 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003227 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08003228 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01003229 */
Ying Xue247f0f32014-02-18 16:06:46 +08003230static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
3231 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003232{
Allan Stephens0c3141e2008-04-15 00:22:02 -07003233 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04003234 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloyb6f88d92020-11-25 13:29:15 -05003235 struct tipc_service_range seq;
Jon Maloy75da2162017-10-13 11:04:23 +02003236 int len, scope;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003237 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003238 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003239
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003240 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
3241 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003242 if (lvl != SOL_TIPC)
3243 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00003244 res = get_user(len, ol);
3245 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003246 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003247
Allan Stephens0c3141e2008-04-15 00:22:02 -07003248 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003249
3250 switch (opt) {
3251 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003252 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003253 break;
3254 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003255 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003256 break;
3257 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003258 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003259 break;
3260 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003261 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07003262 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01003263 break;
Allan Stephens0e659672010-12-31 18:59:32 +00003264 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05003265 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00003266 break;
Allan Stephens0e659672010-12-31 18:59:32 +00003267 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00003268 value = skb_queue_len(&sk->sk_receive_queue);
3269 break;
Tung Nguyen42e54252019-04-18 21:02:19 +07003270 case TIPC_SOCK_RECVQ_USED:
3271 value = sk_rmem_alloc_get(sk);
3272 break;
Jon Maloy75da2162017-10-13 11:04:23 +02003273 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 Lidenb97bf3f2006-01-02 19:04:38 +01003279 default:
3280 res = -EINVAL;
3281 }
3282
Allan Stephens0c3141e2008-04-15 00:22:02 -07003283 release_sock(sk);
3284
Paul Gortmaker25860c32010-12-31 18:59:31 +00003285 if (res)
3286 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01003287
Paul Gortmaker25860c32010-12-31 18:59:31 +00003288 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 Lidenb97bf3f2006-01-02 19:04:38 +01003295}
3296
Ying Xuef2f98002015-01-09 15:27:05 +08003297static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02003298{
Jon Maloy3e5cf362018-04-25 19:29:36 +02003299 struct net *net = sock_net(sock->sk);
3300 struct tipc_sioc_nodeid_req nr = {0};
Erik Hugne78acb1f2014-04-24 16:26:47 +02003301 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 Maloy3e5cf362018-04-25 19:29:36 +02003308 if (!tipc_node_get_linkname(net,
Ying Xuef2f98002015-01-09 15:27:05 +08003309 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02003310 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 Maloy3e5cf362018-04-25 19:29:36 +02003316 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 Hugne78acb1f2014-04-24 16:26:47 +02003324 default:
3325 return -ENOIOCTLCMD;
3326 }
3327}
3328
Erik Hugne70b03752017-03-29 11:22:16 +02003329static 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 Hugne66bc1e82017-03-29 11:22:17 +02003333 u32 onode = tipc_own_addr(sock_net(sock1->sk));
Erik Hugne70b03752017-03-29 11:22:16 +02003334
Erik Hugne66bc1e82017-03-29 11:22:17 +02003335 tsk1->peer.family = AF_TIPC;
Jon Maloyb6f88d92020-11-25 13:29:15 -05003336 tsk1->peer.addrtype = TIPC_SOCKET_ADDR;
Erik Hugne66bc1e82017-03-29 11:22:17 +02003337 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 Maloyb6f88d92020-11-25 13:29:15 -05003341 tsk2->peer.addrtype = TIPC_SOCKET_ADDR;
Erik Hugne66bc1e82017-03-29 11:22:17 +02003342 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 Hugne70b03752017-03-29 11:22:16 +02003348 return 0;
3349}
3350
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00003351/* Protocol switches for the various types of TIPC sockets */
3352
Florian Westphalbca65ea2008-02-07 18:18:01 -08003353static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00003354 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003355 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08003356 .release = tipc_release,
3357 .bind = tipc_bind,
3358 .connect = tipc_connect,
Erik Hugne66bc1e82017-03-29 11:22:17 +02003359 .socketpair = tipc_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04003360 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08003361 .getname = tipc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003362 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02003363 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04003364 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08003365 .shutdown = tipc_shutdown,
3366 .setsockopt = tipc_setsockopt,
3367 .getsockopt = tipc_getsockopt,
3368 .sendmsg = tipc_sendmsg,
3369 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09003370 .mmap = sock_no_mmap,
3371 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01003372};
3373
Florian Westphalbca65ea2008-02-07 18:18:01 -08003374static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00003375 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003376 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08003377 .release = tipc_release,
3378 .bind = tipc_bind,
3379 .connect = tipc_connect,
Erik Hugne70b03752017-03-29 11:22:16 +02003380 .socketpair = tipc_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08003381 .accept = tipc_accept,
3382 .getname = tipc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003383 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02003384 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08003385 .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 Hideaki82387452007-07-19 10:44:56 +09003391 .mmap = sock_no_mmap,
3392 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01003393};
3394
Florian Westphalbca65ea2008-02-07 18:18:01 -08003395static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00003396 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003397 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08003398 .release = tipc_release,
3399 .bind = tipc_bind,
3400 .connect = tipc_connect,
Erik Hugne70b03752017-03-29 11:22:16 +02003401 .socketpair = tipc_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08003402 .accept = tipc_accept,
3403 .getname = tipc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003404 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02003405 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08003406 .listen = tipc_listen,
3407 .shutdown = tipc_shutdown,
3408 .setsockopt = tipc_setsockopt,
3409 .getsockopt = tipc_getsockopt,
Jon Paul Maloy365ad352017-01-03 10:55:11 -05003410 .sendmsg = tipc_sendstream,
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02003411 .recvmsg = tipc_recvstream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09003412 .mmap = sock_no_mmap,
3413 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01003414};
3415
Florian Westphalbca65ea2008-02-07 18:18:01 -08003416static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00003417 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003418 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04003419 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01003420};
3421
3422static struct proto tipc_proto = {
3423 .name = "TIPC",
3424 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04003425 .obj_size = sizeof(struct tipc_sock),
3426 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01003427};
3428
3429/**
Per Liden4323add2006-01-18 00:38:21 +01003430 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003431 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08003432 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01003433 */
Per Liden4323add2006-01-18 00:38:21 +01003434int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003435{
3436 int res;
3437
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003438 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003439 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04003440 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01003441 goto out;
3442 }
3443
3444 res = sock_register(&tipc_family_ops);
3445 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04003446 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01003447 proto_unregister(&tipc_proto);
3448 goto out;
3449 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01003450 out:
3451 return res;
3452}
3453
3454/**
Per Liden4323add2006-01-18 00:38:21 +01003455 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01003456 */
Per Liden4323add2006-01-18 00:38:21 +01003457void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003458{
Per Lidenb97bf3f2006-01-02 19:04:38 +01003459 sock_unregister(tipc_family_ops.family);
3460 proto_unregister(&tipc_proto);
3461}
Richard Alpe34b78a122014-11-20 10:29:10 +01003462
3463/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01003464static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01003465{
Jon Maloy14623e02021-06-02 13:44:24 -04003466 u32 peer_node, peer_port;
3467 u32 conn_type, conn_instance;
Richard Alpe34b78a122014-11-20 10:29:10 +01003468 struct nlattr *nest;
3469
3470 peer_node = tsk_peer_node(tsk);
3471 peer_port = tsk_peer_port(tsk);
Jon Maloy14623e02021-06-02 13:44:24 -04003472 conn_type = msg_nametype(&tsk->phdr);
3473 conn_instance = msg_nameinst(&tsk->phdr);
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003474 nest = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_CON);
Kangjie Lu517ccc22019-03-16 16:46:05 -05003475 if (!nest)
3476 return -EMSGSIZE;
Richard Alpe34b78a122014-11-20 10:29:10 +01003477
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 Maloy14623e02021-06-02 13:44:24 -04003483 if (tsk->conn_addrtype != 0) {
Richard Alpe34b78a122014-11-20 10:29:10 +01003484 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
3485 goto msg_full;
Jon Maloy14623e02021-06-02 13:44:24 -04003486 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, conn_type))
Richard Alpe34b78a122014-11-20 10:29:10 +01003487 goto msg_full;
Jon Maloy14623e02021-06-02 13:44:24 -04003488 if (nla_put_u32(skb, TIPC_NLA_CON_INST, conn_instance))
Richard Alpe34b78a122014-11-20 10:29:10 +01003489 goto msg_full;
3490 }
3491 nla_nest_end(skb, nest);
3492
3493 return 0;
3494
3495msg_full:
3496 nla_nest_cancel(skb, nest);
3497
3498 return -EMSGSIZE;
3499}
3500
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003501static 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 MohanKrishnadfde3312018-03-21 14:37:43 +01003505 struct sock *sk = &tsk->sk;
3506
3507 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid) ||
Jon Maloy23fd3ea2018-03-22 20:42:49 +01003508 nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr(net)))
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003509 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 Alpe34b78a122014-11-20 10:29:10 +01003521/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01003522static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
3523 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01003524{
Richard Alpe34b78a122014-11-20 10:29:10 +01003525 struct nlattr *attrs;
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003526 void *hdr;
Richard Alpe34b78a122014-11-20 10:29:10 +01003527
3528 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01003529 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01003530 if (!hdr)
3531 goto msg_cancel;
3532
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003533 attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK);
Richard Alpe34b78a122014-11-20 10:29:10 +01003534 if (!attrs)
3535 goto genlmsg_cancel;
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003536
3537 if (__tipc_nl_add_sk_info(skb, tsk))
Richard Alpe34b78a122014-11-20 10:29:10 +01003538 goto attr_msg_cancel;
3539
Richard Alpe34b78a122014-11-20 10:29:10 +01003540 nla_nest_end(skb, attrs);
3541 genlmsg_end(skb, hdr);
3542
3543 return 0;
3544
3545attr_msg_cancel:
3546 nla_nest_cancel(skb, attrs);
3547genlmsg_cancel:
3548 genlmsg_cancel(skb, hdr);
3549msg_cancel:
3550 return -EMSGSIZE;
3551}
3552
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003553int 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 Alpe34b78a122014-11-20 10:29:10 +01003557{
Cong Wang8f5c5fc2018-09-04 14:54:55 -07003558 struct rhashtable_iter *iter = (void *)cb->args[4];
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003559 struct tipc_sock *tsk;
3560 int err;
Richard Alpe34b78a122014-11-20 10:29:10 +01003561
Cong Wang9a07efa2018-08-24 12:28:06 -07003562 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 Alped6e164e2015-01-16 12:30:40 +01003568 continue;
3569 }
Cong Wang9a07efa2018-08-24 12:28:06 -07003570 break;
Ying Xue07f6c4b2015-01-07 13:41:58 +08003571 }
Richard Alpe34b78a122014-11-20 10:29:10 +01003572
Cong Wang9a07efa2018-08-24 12:28:06 -07003573 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);
3587out:
Richard Alpe34b78a122014-11-20 10:29:10 +01003588 return skb->len;
3589}
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003590EXPORT_SYMBOL(tipc_nl_sk_walk);
3591
Cong Wang9a07efa2018-08-24 12:28:06 -07003592int tipc_dump_start(struct netlink_callback *cb)
3593{
Cong Wang8f5c5fc2018-09-04 14:54:55 -07003594 return __tipc_dump_start(cb, sock_net(cb->skb->sk));
3595}
3596EXPORT_SYMBOL(tipc_dump_start);
3597
3598int __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 Wang9a07efa2018-08-24 12:28:06 -07003602 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 Wang8f5c5fc2018-09-04 14:54:55 -07003609 cb->args[4] = (long)iter;
Cong Wang9a07efa2018-08-24 12:28:06 -07003610 }
3611
3612 rhashtable_walk_enter(&tn->sk_rht, iter);
3613 return 0;
3614}
Cong Wang9a07efa2018-08-24 12:28:06 -07003615
3616int tipc_dump_done(struct netlink_callback *cb)
3617{
Cong Wang8f5c5fc2018-09-04 14:54:55 -07003618 struct rhashtable_iter *hti = (void *)cb->args[4];
Cong Wang9a07efa2018-08-24 12:28:06 -07003619
3620 rhashtable_walk_exit(hti);
3621 kfree(hti);
3622 return 0;
3623}
3624EXPORT_SYMBOL(tipc_dump_done);
3625
Cong Wange41f0542018-04-06 18:54:52 -07003626int tipc_sk_fill_sock_diag(struct sk_buff *skb, struct netlink_callback *cb,
3627 struct tipc_sock *tsk, u32 sk_filter_state,
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003628 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 Kubecekae0be8d2019-04-26 11:13:06 +02003638 attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK);
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003639 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 Wange41f0542018-04-06 18:54:52 -07003649 from_kuid_munged(sk_user_ns(NETLINK_CB(cb->skb).sk),
GhantaKrishnamurthy MohanKrishna4b2e6872018-04-04 14:49:47 +02003650 sock_i_uid(sk))) ||
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003651 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 Kubecekae0be8d2019-04-26 11:13:06 +02003656 stat = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_STAT);
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003657 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 MohanKrishna872619d2018-03-21 14:37:45 +01003663 skb_queue_len(&sk->sk_write_queue)) ||
3664 nla_put_u32(skb, TIPC_NLA_SOCK_STAT_DROP,
3665 atomic_read(&sk->sk_drops)))
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003666 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 MohanKrishnaa1be5a22018-06-29 13:26:18 +02003677
3678 if (tsk->group)
3679 if (tipc_group_fill_sock_diag(tsk->group, skb))
3680 goto stat_msg_cancel;
3681
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003682 nla_nest_end(skb, attrs);
3683
3684 return 0;
3685
3686stat_msg_cancel:
3687 nla_nest_cancel(skb, stat);
3688attr_msg_cancel:
3689 nla_nest_cancel(skb, attrs);
3690msg_cancel:
3691 return -EMSGSIZE;
3692}
3693EXPORT_SYMBOL(tipc_sk_fill_sock_diag);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003694
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003695int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
3696{
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003697 return tipc_nl_sk_walk(skb, cb, __tipc_nl_add_sk);
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003698}
3699
Richard Alpe1a1a1432014-11-20 10:29:11 +01003700/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01003701static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
3702 struct netlink_callback *cb,
3703 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01003704{
3705 void *hdr;
3706 struct nlattr *attrs;
3707
3708 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01003709 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003710 if (!hdr)
3711 goto msg_cancel;
3712
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003713 attrs = nla_nest_start_noflag(skb, TIPC_NLA_PUBL);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003714 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 Maloy998d3902021-03-16 22:06:08 -04003719 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->sr.type))
Richard Alpe1a1a1432014-11-20 10:29:11 +01003720 goto attr_msg_cancel;
Jon Maloy998d3902021-03-16 22:06:08 -04003721 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->sr.lower))
Richard Alpe1a1a1432014-11-20 10:29:11 +01003722 goto attr_msg_cancel;
Jon Maloy998d3902021-03-16 22:06:08 -04003723 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->sr.upper))
Richard Alpe1a1a1432014-11-20 10:29:11 +01003724 goto attr_msg_cancel;
3725
3726 nla_nest_end(skb, attrs);
3727 genlmsg_end(skb, hdr);
3728
3729 return 0;
3730
3731attr_msg_cancel:
3732 nla_nest_cancel(skb, attrs);
3733genlmsg_cancel:
3734 genlmsg_cancel(skb, hdr);
3735msg_cancel:
3736 return -EMSGSIZE;
3737}
3738
3739/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01003740static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
3741 struct netlink_callback *cb,
3742 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01003743{
3744 int err;
3745 struct publication *p;
3746
3747 if (*last_publ) {
Jon Maloye50e73e2018-03-15 16:48:55 +01003748 list_for_each_entry(p, &tsk->publications, binding_sock) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01003749 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 Maloye50e73e2018-03-15 16:48:55 +01003765 binding_sock);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003766 }
3767
Jon Maloye50e73e2018-03-15 16:48:55 +01003768 list_for_each_entry_from(p, &tsk->publications, binding_sock) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01003769 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
3780int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
3781{
3782 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08003783 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01003784 u32 last_publ = cb->args[1];
3785 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08003786 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003787 struct tipc_sock *tsk;
3788
Ying Xue07f6c4b2015-01-07 13:41:58 +08003789 if (!tsk_portid) {
Jiri Pirko057af702019-10-05 20:04:39 +02003790 struct nlattr **attrs = genl_dumpit_info(cb)->attrs;
Richard Alpe1a1a1432014-11-20 10:29:11 +01003791 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
3792
Richard Alpe45e093a2016-05-16 11:14:54 +02003793 if (!attrs[TIPC_NLA_SOCK])
3794 return -EINVAL;
3795
Johannes Berg8cb08172019-04-26 14:07:28 +02003796 err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
3797 attrs[TIPC_NLA_SOCK],
3798 tipc_nl_sock_policy, NULL);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003799 if (err)
3800 return err;
3801
3802 if (!sock[TIPC_NLA_SOCK_REF])
3803 return -EINVAL;
3804
Ying Xue07f6c4b2015-01-07 13:41:58 +08003805 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003806 }
3807
3808 if (done)
3809 return 0;
3810
Ying Xuee05b31f2015-01-09 15:27:08 +08003811 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003812 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 Xue07f6c4b2015-01-07 13:41:58 +08003820 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003821
Ying Xue07f6c4b2015-01-07 13:41:58 +08003822 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01003823 cb->args[1] = last_publ;
3824 cb->args[2] = done;
3825
3826 return skb->len;
3827}
Tuong Lienb4b97712018-12-19 09:17:56 +07003828
Tuong Lien01e661e2018-12-19 09:17:58 +07003829/**
3830 * tipc_sk_filtering - check if a socket should be traced
3831 * @sk: the socket to be examined
Randy Dunlapf172f4b2020-11-29 10:32:49 -08003832 *
3833 * @sysctl_tipc_sk_filter is used as the socket tuple for filtering:
Randy Dunlap5fcb7d42020-11-29 10:32:50 -08003834 * (portid, sock type, name type, name lower, name upper)
Tuong Lien01e661e2018-12-19 09:17:58 +07003835 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08003836 * Return: true if the socket meets the socket tuple data
Tuong Lien01e661e2018-12-19 09:17:58 +07003837 * (value 0 = 'any') or when there is no tuple set (all = 0),
3838 * otherwise false
3839 */
3840bool 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 Maloy998d3902021-03-16 22:06:08 -04003871 type = p->sr.type;
3872 lower = p->sr.lower;
3873 upper = p->sr.upper;
Tuong Lien01e661e2018-12-19 09:17:58 +07003874 }
3875 }
3876
3877 if (!tipc_sk_type_connectionless(sk)) {
Jon Maloy14623e02021-06-02 13:44:24 -04003878 type = msg_nametype(&tsk->phdr);
3879 lower = msg_nameinst(&tsk->phdr);
3880 upper = lower;
Tuong Lien01e661e2018-12-19 09:17:58 +07003881 }
3882
3883 if ((_type && _type != type) || (_lower && _lower != lower) ||
3884 (_upper && _upper != upper))
3885 return false;
3886
3887 return true;
3888}
3889
Tuong Lienb4b97712018-12-19 09:17:56 +07003890u32 tipc_sock_get_portid(struct sock *sk)
3891{
3892 return (sk) ? (tipc_sk(sk))->portid : 0;
3893}
3894
3895/**
Tuong Lien01e661e2018-12-19 09:17:58 +07003896 * 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 Dunlap637b77f2020-11-29 10:32:48 -08003901 * Return: true if the socket rx queue allocation is > 90%, otherwise false
Tuong Lien01e661e2018-12-19 09:17:58 +07003902 */
3903
3904bool 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 Dunlap637b77f2020-11-29 10:32:48 -08003919 * Return: true if the socket rx queue allocation is > 90%, otherwise false
Tuong Lien01e661e2018-12-19 09:17:58 +07003920 */
3921
3922bool 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 Lienb4b97712018-12-19 09:17:56 +07003931 * 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 */
3941int 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 Maloy14623e02021-06-02 13:44:24 -04003945 u32 conn_type, conn_instance;
Tuong Lienb4b97712018-12-19 09:17:56 +07003946 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 Maloy14623e02021-06-02 13:44:24 -04003966 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 Lienb4b97712018-12-19 09:17:56 +07003970 }
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 Maloy998d3902021-03-16 22:06:08 -04003975 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 Lienb4b97712018-12-19 09:17:56 +07003978 }
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 Dumazet70c26552019-10-09 15:41:03 -07003992 i += scnprintf(buf + i, sz - i, " | %d\n", READ_ONCE(sk->sk_backlog.len));
Tuong Lienb4b97712018-12-19 09:17:56 +07003993
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}