blob: 58935cd0d068a155fc11d0a59344b211ba33b761 [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 * @conn_type: TIPC type used when connection was established
77 * @conn_instance: TIPC instance used when connection was established
78 * @published: non-zero if port has one or more associated names
79 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Jon Maloyc0bceb92019-10-30 14:00:41 +010080 * @maxnagle: maximum size of msg which can be subject to nagle
Ying Xue07f6c4b2015-01-07 13:41:58 +080081 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040082 * @phdr: preformatted message header used when sending messages
Randy Dunlapf172f4b2020-11-29 10:32:49 -080083 * @cong_links: list of congested links
Jon Paul Maloy301bae52014-08-22 18:09:20 -040084 * @publications: list of publications for port
Jon Paul Maloy365ad352017-01-03 10:55:11 -050085 * @blocking_link: address of the congested link we are currently sleeping on
Jon Paul Maloy301bae52014-08-22 18:09:20 -040086 * @pub_count: total # of publications port has made during its lifetime
Jon Paul Maloy301bae52014-08-22 18:09:20 -040087 * @conn_timeout: the time we can wait for an unresponded setup request
Randy Dunlapf172f4b2020-11-29 10:32:49 -080088 * @probe_unacked: probe has not received ack yet
Jon Paul Maloy301bae52014-08-22 18:09:20 -040089 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
Jon Paul Maloy365ad352017-01-03 10:55:11 -050090 * @cong_link_cnt: number of congested links
Jon Maloy75da2162017-10-13 11:04:23 +020091 * @snt_unacked: # messages sent by socket, and not yet acked by peer
Randy Dunlapf172f4b2020-11-29 10:32:49 -080092 * @snd_win: send window size
93 * @peer_caps: peer capabilities mask
Jon Paul Maloy301bae52014-08-22 18:09:20 -040094 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Randy Dunlapf172f4b2020-11-29 10:32:49 -080095 * @rcv_win: receive window size
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +010096 * @peer: 'connected' peer for dgram/rdm
Ying Xue07f6c4b2015-01-07 13:41:58 +080097 * @node: hash table node
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -050098 * @mc_method: cookie for use between socket and broadcast layer
Ying Xue07f6c4b2015-01-07 13:41:58 +080099 * @rcu: rcu struct for tipc_sock
Randy Dunlapf172f4b2020-11-29 10:32:49 -0800100 * @group: TIPC communications group
101 * @oneway: message count in one direction (FIXME)
102 * @nagle_start: current nagle value
103 * @snd_backlog: send backlog count
104 * @msg_acc: messages accepted; used in managing backlog and nagle
105 * @pkt_cnt: TIPC socket packet count
106 * @expect_ack: whether this TIPC socket is expecting an ack
107 * @nodelay: setsockopt() TIPC_NODELAY setting
108 * @group_is_open: TIPC socket group is fully open (FIXME)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400109 */
110struct tipc_sock {
111 struct sock sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400112 u32 conn_type;
113 u32 conn_instance;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400114 u32 max_pkt;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100115 u32 maxnagle;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800116 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400117 struct tipc_msg phdr;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500118 struct list_head cong_links;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400119 struct list_head publications;
120 u32 pub_count;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400121 atomic_t dupl_rcvcnt;
Tung Nguyen67879272018-09-28 20:23:22 +0200122 u16 conn_timeout;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100123 bool probe_unacked;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500124 u16 cong_link_cnt;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400125 u16 snt_unacked;
126 u16 snd_win;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400127 u16 peer_caps;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400128 u16 rcv_unacked;
129 u16 rcv_win;
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +0100130 struct sockaddr_tipc peer;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800131 struct rhash_head node;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500132 struct tipc_mc_method mc_method;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800133 struct rcu_head rcu;
Jon Maloy75da2162017-10-13 11:04:23 +0200134 struct tipc_group *group;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100135 u32 oneway;
Tuong Lien0a3e0602020-05-26 16:38:38 +0700136 u32 nagle_start;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100137 u16 snd_backlog;
Tuong Lien0a3e0602020-05-26 16:38:38 +0700138 u16 msg_acc;
139 u16 pkt_cnt;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100140 bool expect_ack;
141 bool nodelay;
Jon Maloy60c25302018-01-17 16:42:46 +0100142 bool group_is_open;
Jon Maloy50a34992021-03-16 22:06:11 -0400143 bool published;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400144};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145
Jon Maloy64ac5f52017-10-13 11:04:20 +0200146static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400147static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800148static void tipc_write_space(struct sock *sk);
Ying Xuef4195d12015-11-22 15:46:05 +0800149static void tipc_sock_destruct(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800150static int tipc_release(struct socket *sock);
David Howellscdfbabf2017-03-09 08:09:05 +0000151static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
152 bool kern);
Kees Cook31b102b2017-10-30 14:06:45 -0700153static void tipc_sk_timeout(struct timer_list *t);
Jon Maloy50a34992021-03-16 22:06:11 -0400154static int tipc_sk_publish(struct tipc_sock *tsk, struct tipc_uaddr *ua);
Jon Maloy2c98da02021-03-16 22:06:13 -0400155static int tipc_sk_withdraw(struct tipc_sock *tsk, struct tipc_uaddr *ua);
Jon Maloy75da2162017-10-13 11:04:23 +0200156static int tipc_sk_leave(struct tipc_sock *tsk);
Ying Xuee05b31f2015-01-09 15:27:08 +0800157static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800158static int tipc_sk_insert(struct tipc_sock *tsk);
159static void tipc_sk_remove(struct tipc_sock *tsk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500160static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
Ying Xue39a0295f2015-03-02 15:37:47 +0800161static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Tuong Lien0a3e0602020-05-26 16:38:38 +0700162static void tipc_sk_push_backlog(struct tipc_sock *tsk, bool nagle_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163
Florian Westphalbca65ea2008-02-07 18:18:01 -0800164static const struct proto_ops packet_ops;
165static const struct proto_ops stream_ops;
166static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167static struct proto tipc_proto;
Herbert Xu6cca72892015-03-20 21:57:05 +1100168static const struct rhashtable_params tsk_rht_params;
169
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500170static u32 tsk_own_node(struct tipc_sock *tsk)
171{
172 return msg_prevnode(&tsk->phdr);
173}
174
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400175static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400176{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400177 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400178}
179
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400180static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400181{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400182 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400183}
184
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400185static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400186{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400187 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400188}
189
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400190static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400191{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400192 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400193}
194
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400195static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400196{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400197 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400198}
199
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400200static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400201{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400202 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400203}
204
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400205static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400206{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400207 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400208}
209
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400210static struct tipc_sock *tipc_sk(const struct sock *sk)
211{
212 return container_of(sk, struct tipc_sock, sk);
213}
214
Christoph Hellwig095ae612020-05-28 07:12:36 +0200215int tsk_set_importance(struct sock *sk, int imp)
216{
217 if (imp > TIPC_CRITICAL_IMPORTANCE)
218 return -EINVAL;
219 msg_set_importance(&tipc_sk(sk)->phdr, (u32)imp);
220 return 0;
221}
222
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400223static bool tsk_conn_cong(struct tipc_sock *tsk)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400224{
Jon Paul Maloy6998cc62016-11-24 18:47:07 -0500225 return tsk->snt_unacked > tsk->snd_win;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400226}
227
Jon Maloyb7d42632017-10-13 11:04:26 +0200228static u16 tsk_blocks(int len)
229{
230 return ((len / FLOWCTL_BLK_SZ) + 1);
231}
232
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400233/* tsk_blocks(): translate a buffer size in bytes to number of
234 * advertisable blocks, taking into account the ratio truesize(len)/len
235 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
236 */
237static u16 tsk_adv_blocks(int len)
238{
239 return len / FLOWCTL_BLK_SZ / 4;
240}
241
242/* tsk_inc(): increment counter for sent or received data
243 * - If block based flow control is not supported by peer we
244 * fall back to message based ditto, incrementing the counter
245 */
246static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
247{
248 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
249 return ((msglen / FLOWCTL_BLK_SZ) + 1);
250 return 1;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400251}
252
Jon Maloyc0bceb92019-10-30 14:00:41 +0100253/* tsk_set_nagle - enable/disable nagle property by manipulating maxnagle
254 */
255static void tsk_set_nagle(struct tipc_sock *tsk)
256{
257 struct sock *sk = &tsk->sk;
258
259 tsk->maxnagle = 0;
260 if (sk->sk_type != SOCK_STREAM)
261 return;
262 if (tsk->nodelay)
263 return;
264 if (!(tsk->peer_caps & TIPC_NAGLE))
265 return;
266 /* Limit node local buffer size to avoid receive queue overflow */
267 if (tsk->max_pkt == MAX_MSG_SIZE)
268 tsk->maxnagle = 1500;
269 else
270 tsk->maxnagle = tsk->max_pkt;
271}
272
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400274 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Randy Dunlapf172f4b2020-11-29 10:32:49 -0800275 * @sk: network socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700276 *
277 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400279static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280{
Tuong Lien01e661e2018-12-19 09:17:58 +0700281 trace_tipc_sk_advance_rx(sk, NULL, TIPC_DUMP_SK_RCVQ, " ");
Allan Stephens5f6d9122011-11-04 13:24:29 -0400282 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100283}
284
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400285/* tipc_sk_respond() : send response message back to sender
286 */
287static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
288{
289 u32 selector;
290 u32 dnode;
291 u32 onode = tipc_own_addr(sock_net(sk));
292
293 if (!tipc_msg_reverse(onode, &skb, err))
294 return;
295
Tuong Lien01e661e2018-12-19 09:17:58 +0700296 trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE, "@sk_respond!");
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400297 dnode = msg_destnode(buf_msg(skb));
298 selector = msg_origport(buf_msg(skb));
299 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
300}
301
Per Lidenb97bf3f2006-01-02 19:04:38 +0100302/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400303 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Randy Dunlapf172f4b2020-11-29 10:32:49 -0800304 * @sk: network socket
305 * @error: response error code
Allan Stephens0c3141e2008-04-15 00:22:02 -0700306 *
307 * Caller must hold socket lock
308 */
Tuong Lien49afb802020-01-08 09:18:15 +0700309static void tsk_rej_rx_queue(struct sock *sk, int error)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700310{
Ying Xuea6ca1092014-11-26 11:41:55 +0800311 struct sk_buff *skb;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700312
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400313 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
Tuong Lien49afb802020-01-08 09:18:15 +0700314 tipc_sk_respond(sk, skb, error);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700315}
316
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100317static bool tipc_sk_connected(struct sock *sk)
318{
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100319 return sk->sk_state == TIPC_ESTABLISHED;
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100320}
321
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +0100322/* tipc_sk_type_connectionless - check if the socket is datagram socket
323 * @sk: socket
324 *
325 * Returns true if connection less, false otherwise
326 */
327static bool tipc_sk_type_connectionless(struct sock *sk)
328{
329 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
330}
331
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400332/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400333 *
334 * Handles cases where the node's network address has changed from
335 * the default of <0.0.0> to its configured setting.
336 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400337static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400338{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100339 struct sock *sk = &tsk->sk;
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100340 u32 self = tipc_own_addr(sock_net(sk));
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400341 u32 peer_port = tsk_peer_port(tsk);
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100342 u32 orig_node, peer_node;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400343
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100344 if (unlikely(!tipc_sk_connected(sk)))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400345 return false;
346
347 if (unlikely(msg_origport(msg) != peer_port))
348 return false;
349
350 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400351 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400352
353 if (likely(orig_node == peer_node))
354 return true;
355
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100356 if (!orig_node && peer_node == self)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400357 return true;
358
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100359 if (!peer_node && orig_node == self)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400360 return true;
361
362 return false;
363}
364
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100365/* tipc_set_sk_state - set the sk_state of the socket
366 * @sk: socket
367 *
368 * Caller must hold socket lock
369 *
370 * Returns 0 on success, errno otherwise
371 */
372static int tipc_set_sk_state(struct sock *sk, int state)
373{
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100374 int oldsk_state = sk->sk_state;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100375 int res = -EINVAL;
376
377 switch (state) {
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100378 case TIPC_OPEN:
379 res = 0;
380 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100381 case TIPC_LISTEN:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100382 case TIPC_CONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100383 if (oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100384 res = 0;
385 break;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100386 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100387 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100388 oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100389 res = 0;
390 break;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100391 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100392 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100393 oldsk_state == TIPC_ESTABLISHED)
394 res = 0;
395 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100396 }
397
398 if (!res)
399 sk->sk_state = state;
400
401 return res;
402}
403
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500404static int tipc_sk_sock_err(struct socket *sock, long *timeout)
405{
406 struct sock *sk = sock->sk;
407 int err = sock_error(sk);
408 int typ = sock->type;
409
410 if (err)
411 return err;
412 if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
413 if (sk->sk_state == TIPC_DISCONNECTING)
414 return -EPIPE;
415 else if (!tipc_sk_connected(sk))
416 return -ENOTCONN;
417 }
418 if (!*timeout)
419 return -EAGAIN;
420 if (signal_pending(current))
421 return sock_intr_errno(*timeout);
422
423 return 0;
424}
425
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200426#define tipc_wait_for_cond(sock_, timeo_, condition_) \
427({ \
Tung Nguyenbfd07f32019-02-25 10:57:20 +0700428 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200429 struct sock *sk_; \
430 int rc_; \
431 \
432 while ((rc_ = !(condition_))) { \
Tung Nguyenbfd07f32019-02-25 10:57:20 +0700433 /* coupled with smp_wmb() in tipc_sk_proto_rcv() */ \
434 smp_rmb(); \
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200435 sk_ = (sock_)->sk; \
436 rc_ = tipc_sk_sock_err((sock_), timeo_); \
437 if (rc_) \
438 break; \
Tung Nguyen223b7322019-02-19 11:20:47 +0700439 add_wait_queue(sk_sleep(sk_), &wait_); \
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200440 release_sock(sk_); \
441 *(timeo_) = wait_woken(&wait_, TASK_INTERRUPTIBLE, *(timeo_)); \
442 sched_annotate_sleep(); \
443 lock_sock(sk_); \
444 remove_wait_queue(sk_sleep(sk_), &wait_); \
445 } \
446 rc_; \
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500447})
448
Allan Stephens0c3141e2008-04-15 00:22:02 -0700449/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400450 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700451 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100452 * @sock: pre-allocated socket structure
453 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800454 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900455 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700456 * This routine creates additional data structures used by the TIPC socket,
457 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458 *
Randy Dunlap637b77f2020-11-29 10:32:48 -0800459 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400461static int tipc_sk_create(struct net *net, struct socket *sock,
462 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700464 const struct proto_ops *ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400466 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400467 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700468
469 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470 if (unlikely(protocol != 0))
471 return -EPROTONOSUPPORT;
472
Per Lidenb97bf3f2006-01-02 19:04:38 +0100473 switch (sock->type) {
474 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700475 ops = &stream_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476 break;
477 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700478 ops = &packet_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479 break;
480 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700482 ops = &msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483 break;
Allan Stephens49978652006-06-25 23:47:18 -0700484 default:
Allan Stephens49978652006-06-25 23:47:18 -0700485 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486 }
487
Allan Stephens0c3141e2008-04-15 00:22:02 -0700488 /* Allocate socket's protocol area */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500489 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700490 if (sk == NULL)
491 return -ENOMEM;
492
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400493 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400494 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100495 tsk->maxnagle = 0;
Tuong Lien0a3e0602020-05-26 16:38:38 +0700496 tsk->nagle_start = NAGLE_START_INIT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400497 INIT_LIST_HEAD(&tsk->publications);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500498 INIT_LIST_HEAD(&tsk->cong_links);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400499 msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500
Allan Stephens0c3141e2008-04-15 00:22:02 -0700501 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700502 sock->ops = ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503 sock_init_data(sock, sk);
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100504 tipc_set_sk_state(sk, TIPC_OPEN);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800505 if (tipc_sk_insert(tsk)) {
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900506 pr_warn("Socket create failed; port number exhausted\n");
Ying Xue07f6c4b2015-01-07 13:41:58 +0800507 return -EINVAL;
508 }
Herbert Xu40f9f432017-02-11 19:26:46 +0800509
510 /* Ensure tsk is visible before we read own_addr. */
511 smp_mb();
512
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100513 tipc_msg_init(tipc_own_addr(net), msg, TIPC_LOW_IMPORTANCE,
514 TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
Herbert Xu40f9f432017-02-11 19:26:46 +0800515
Ying Xue07f6c4b2015-01-07 13:41:58 +0800516 msg_set_origport(msg, tsk->portid);
Kees Cook31b102b2017-10-30 14:06:45 -0700517 timer_setup(&sk->sk_timer, tipc_sk_timeout, 0);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100518 sk->sk_shutdown = 0;
Jon Maloy64ac5f52017-10-13 11:04:20 +0200519 sk->sk_backlog_rcv = tipc_sk_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400520 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800521 sk->sk_data_ready = tipc_data_ready;
522 sk->sk_write_space = tipc_write_space;
Ying Xuef4195d12015-11-22 15:46:05 +0800523 sk->sk_destruct = tipc_sock_destruct;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400524 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Maloy1b22bca2018-02-26 20:14:04 +0100525 tsk->group_is_open = true;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400526 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700527
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400528 /* Start out with safe limits until we receive an advertised window */
529 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
530 tsk->rcv_win = tsk->snd_win;
531
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +0100532 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400533 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700534 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400535 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700536 }
Jon Maloy2948a1f2019-07-30 20:19:10 +0200537 __skb_queue_head_init(&tsk->mc_method.deferredq);
Tuong Lien01e661e2018-12-19 09:17:58 +0700538 trace_tipc_sk_create(sk, NULL, TIPC_DUMP_NONE, " ");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539 return 0;
540}
541
Ying Xue07f6c4b2015-01-07 13:41:58 +0800542static void tipc_sk_callback(struct rcu_head *head)
543{
544 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
545
546 sock_put(&tsk->sk);
547}
548
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100549/* Caller should hold socket lock for the socket. */
550static void __tipc_shutdown(struct socket *sock, int error)
551{
552 struct sock *sk = sock->sk;
553 struct tipc_sock *tsk = tipc_sk(sk);
554 struct net *net = sock_net(sk);
Tung Nguyen12db3c82019-11-28 10:10:07 +0700555 long timeout = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100556 u32 dnode = tsk_peer_node(tsk);
557 struct sk_buff *skb;
558
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500559 /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
560 tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
561 !tsk_conn_cong(tsk)));
562
Tung Nguyend34910e2019-11-28 10:10:08 +0700563 /* Push out delayed messages if in Nagle mode */
Tuong Lien0a3e0602020-05-26 16:38:38 +0700564 tipc_sk_push_backlog(tsk, false);
Tung Nguyend34910e2019-11-28 10:10:08 +0700565 /* Remove pending SYN */
566 __skb_queue_purge(&sk->sk_write_queue);
Tung Nguyen67879272018-09-28 20:23:22 +0200567
Tuong Lien49afb802020-01-08 09:18:15 +0700568 /* Remove partially received buffer if any */
569 skb = skb_peek(&sk->sk_receive_queue);
570 if (skb && TIPC_SKB_CB(skb)->bytes_read) {
571 __skb_unlink(skb, &sk->sk_receive_queue);
572 kfree_skb(skb);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100573 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500574
Tuong Lien49afb802020-01-08 09:18:15 +0700575 /* Reject all unreceived messages if connectionless */
576 if (tipc_sk_type_connectionless(sk)) {
577 tsk_rej_rx_queue(sk, error);
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500578 return;
Tuong Lien49afb802020-01-08 09:18:15 +0700579 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500580
Tuong Lien49afb802020-01-08 09:18:15 +0700581 switch (sk->sk_state) {
582 case TIPC_CONNECTING:
583 case TIPC_ESTABLISHED:
584 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
585 tipc_node_remove_conn(net, dnode, tsk->portid);
586 /* Send a FIN+/- to its peer */
587 skb = __skb_dequeue(&sk->sk_receive_queue);
588 if (skb) {
589 __skb_queue_purge(&sk->sk_receive_queue);
590 tipc_sk_respond(sk, skb, error);
591 break;
592 }
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100593 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
594 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
595 tsk_own_node(tsk), tsk_peer_port(tsk),
596 tsk->portid, error);
597 if (skb)
598 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Tuong Lien49afb802020-01-08 09:18:15 +0700599 break;
600 case TIPC_LISTEN:
601 /* Reject all SYN messages */
602 tsk_rej_rx_queue(sk, error);
603 break;
604 default:
605 __skb_queue_purge(&sk->sk_receive_queue);
606 break;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100607 }
608}
609
Ying Xuec5fa7b32013-06-17 10:54:39 -0400610/**
Ying Xue247f0f32014-02-18 16:06:46 +0800611 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 * @sock: socket to destroy
613 *
614 * This routine cleans up any messages that are still queued on the socket.
615 * For DGRAM and RDM socket types, all queued messages are rejected.
616 * For SEQPACKET and STREAM socket types, the first message is rejected
617 * and any others are discarded. (If the first message on a STREAM socket
618 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900619 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620 * NOTE: Rejected messages are not necessarily returned to the sender! They
621 * are returned or discarded according to the "destination droppable" setting
622 * specified for the message by the sender.
623 *
Randy Dunlap637b77f2020-11-29 10:32:48 -0800624 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625 */
Ying Xue247f0f32014-02-18 16:06:46 +0800626static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400629 struct tipc_sock *tsk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100630
Allan Stephens0c3141e2008-04-15 00:22:02 -0700631 /*
632 * Exit if socket isn't fully initialized (occurs when a failed accept()
633 * releases a pre-allocated child socket that was never used)
634 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700635 if (sk == NULL)
636 return 0;
637
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400638 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700639 lock_sock(sk);
640
Tuong Lien01e661e2018-12-19 09:17:58 +0700641 trace_tipc_sk_release(sk, NULL, TIPC_DUMP_ALL, " ");
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100642 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
643 sk->sk_shutdown = SHUTDOWN_MASK;
Jon Maloy75da2162017-10-13 11:04:23 +0200644 tipc_sk_leave(tsk);
Jon Maloy2c98da02021-03-16 22:06:13 -0400645 tipc_sk_withdraw(tsk, NULL);
Hoang Lec55c8ed2019-03-19 18:49:50 +0700646 __skb_queue_purge(&tsk->mc_method.deferredq);
Ying Xue1ea23a22015-05-28 13:19:22 +0800647 sk_stop_timer(sk, &sk->sk_timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800648 tipc_sk_remove(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649
Cong Wang0a3b8b22018-09-03 19:12:41 -0700650 sock_orphan(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700651 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700652 release_sock(sk);
Jon Maloya80ae532017-10-13 11:04:22 +0200653 tipc_dest_list_purge(&tsk->cong_links);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500654 tsk->cong_link_cnt = 0;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800655 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700656 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200658 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659}
660
661/**
Jon Maloy60c102e2020-11-25 13:29:13 -0500662 * __tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663 * @sock: socket structure
Jon Maloy60c102e2020-11-25 13:29:13 -0500664 * @skaddr: socket address describing name(s) and desired operation
665 * @alen: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900666 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100667 * Name and name sequence binding is indicated using a positive scope value;
668 * a negative scope value unbinds the specified name. Specifying no name
669 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900670 *
Randy Dunlap637b77f2020-11-29 10:32:48 -0800671 * Return: 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700672 *
673 * NOTE: This routine doesn't need to take the socket lock since it doesn't
674 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675 */
Jon Maloy60c102e2020-11-25 13:29:13 -0500676static int __tipc_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100677{
Jon Maloy50a34992021-03-16 22:06:11 -0400678 struct tipc_uaddr *ua = (struct tipc_uaddr *)skaddr;
Jon Maloy60c102e2020-11-25 13:29:13 -0500679 struct tipc_sock *tsk = tipc_sk(sock->sk);
Jon Maloy50a34992021-03-16 22:06:11 -0400680 bool unbind = false;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100681
Jon Maloy60c102e2020-11-25 13:29:13 -0500682 if (unlikely(!alen))
Jon Maloy2c98da02021-03-16 22:06:13 -0400683 return tipc_sk_withdraw(tsk, NULL);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100684
Jon Maloy50a34992021-03-16 22:06:11 -0400685 if (ua->addrtype == TIPC_SERVICE_ADDR) {
686 ua->addrtype = TIPC_SERVICE_RANGE;
687 ua->sr.upper = ua->sr.lower;
688 }
689 if (ua->scope < 0) {
690 unbind = true;
691 ua->scope = -ua->scope;
692 }
693 /* Users may still use deprecated TIPC_ZONE_SCOPE */
694 if (ua->scope != TIPC_NODE_SCOPE)
695 ua->scope = TIPC_CLUSTER_SCOPE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900696
Jon Maloy60c102e2020-11-25 13:29:13 -0500697 if (tsk->group)
698 return -EACCES;
699
Jon Maloy50a34992021-03-16 22:06:11 -0400700 if (unbind)
Jon Maloy2c98da02021-03-16 22:06:13 -0400701 return tipc_sk_withdraw(tsk, ua);
Jon Maloy50a34992021-03-16 22:06:11 -0400702 return tipc_sk_publish(tsk, ua);
Jon Maloy60c102e2020-11-25 13:29:13 -0500703}
704
705int tipc_sk_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
706{
707 int res;
708
709 lock_sock(sock->sk);
710 res = __tipc_bind(sock, skaddr, alen);
711 release_sock(sock->sk);
Ying Xue84602762013-12-27 10:18:28 +0800712 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100713}
714
Jon Maloy72671b32020-10-29 21:29:38 -0400715static int tipc_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
716{
Jon Maloy50a34992021-03-16 22:06:11 -0400717 struct tipc_uaddr *ua = (struct tipc_uaddr *)skaddr;
718 u32 atype = ua->addrtype;
Jon Maloy72671b32020-10-29 21:29:38 -0400719
720 if (alen) {
Jon Maloy50a34992021-03-16 22:06:11 -0400721 if (!tipc_uaddr_valid(ua, alen))
Jon Maloy72671b32020-10-29 21:29:38 -0400722 return -EINVAL;
Jon Maloy50a34992021-03-16 22:06:11 -0400723 if (atype == TIPC_SOCKET_ADDR)
Jon Maloy60c102e2020-11-25 13:29:13 -0500724 return -EAFNOSUPPORT;
Jon Maloy50a34992021-03-16 22:06:11 -0400725 if (ua->sr.type < TIPC_RESERVED_TYPES) {
Jon Maloy72671b32020-10-29 21:29:38 -0400726 pr_warn_once("Can't bind to reserved service type %u\n",
Jon Maloy50a34992021-03-16 22:06:11 -0400727 ua->sr.type);
Jon Maloy72671b32020-10-29 21:29:38 -0400728 return -EACCES;
729 }
730 }
731 return tipc_sk_bind(sock, skaddr, alen);
732}
733
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900734/**
Ying Xue247f0f32014-02-18 16:06:46 +0800735 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100736 * @sock: socket structure
737 * @uaddr: area for returned socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700738 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900739 *
Randy Dunlap637b77f2020-11-29 10:32:48 -0800740 * Return: 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700741 *
Allan Stephens2da59912008-07-14 22:43:32 -0700742 * NOTE: This routine doesn't need to take the socket lock since it only
743 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000744 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 */
Ying Xue247f0f32014-02-18 16:06:46 +0800746static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100747 int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100749 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100750 struct sock *sk = sock->sk;
751 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000753 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700754 if (peer) {
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100755 if ((!tipc_sk_connected(sk)) &&
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100756 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
Allan Stephens2da59912008-07-14 22:43:32 -0700757 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400758 addr->addr.id.ref = tsk_peer_port(tsk);
759 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700760 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800761 addr->addr.id.ref = tsk->portid;
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100762 addr->addr.id.node = tipc_own_addr(sock_net(sk));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700763 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764
Jon Maloyb6f88d92020-11-25 13:29:15 -0500765 addr->addrtype = TIPC_SOCKET_ADDR;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766 addr->family = AF_TIPC;
767 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100768 addr->addr.name.domain = 0;
769
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100770 return sizeof(*addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771}
772
773/**
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700774 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775 * @file: file structure associated with the socket
776 * @sock: socket for which to calculate the poll bits
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700777 * @wait: ???
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 *
Randy Dunlap637b77f2020-11-29 10:32:48 -0800779 * Return: pollmask value
Allan Stephens9b674e82008-03-26 16:48:21 -0700780 *
781 * COMMENTARY:
782 * It appears that the usual socket locking mechanisms are not useful here
783 * since the pollmask info is potentially out-of-date the moment this routine
784 * exits. TCP and other protocols seem to rely on higher level poll routines
785 * to handle any preventable race conditions, so TIPC will do the same ...
786 *
Allan Stephensf662c072010-08-17 11:00:06 +0000787 * IMPORTANT: The fact that a read or write operation is indicated does NOT
788 * imply that the operation will succeed, merely that it should be performed
789 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100790 */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700791static __poll_t tipc_poll(struct file *file, struct socket *sock,
792 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100793{
Allan Stephens9b674e82008-03-26 16:48:21 -0700794 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400795 struct tipc_sock *tsk = tipc_sk(sk);
Al Viroade994f2017-07-03 00:01:49 -0400796 __poll_t revents = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700797
Karsten Graul89ab0662018-10-23 13:40:39 +0200798 sock_poll_wait(file, sock, wait);
Tuong Lien01e661e2018-12-19 09:17:58 +0700799 trace_tipc_sk_poll(sk, NULL, TIPC_DUMP_ALL, " ");
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700800
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100801 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800802 revents |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100803 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800804 revents |= EPOLLHUP;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100805
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100806 switch (sk->sk_state) {
807 case TIPC_ESTABLISHED:
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500808 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800809 revents |= EPOLLOUT;
Miaohe Lin7f8901b2020-08-18 08:07:13 -0400810 fallthrough;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100811 case TIPC_LISTEN:
Parthasarathy Bhuvaraganff946832019-05-09 07:13:42 +0200812 case TIPC_CONNECTING:
Eric Dumazet3ef7cf52019-10-23 22:44:50 -0700813 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800814 revents |= EPOLLIN | EPOLLRDNORM;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100815 break;
816 case TIPC_OPEN:
Jon Maloy60c25302018-01-17 16:42:46 +0100817 if (tsk->group_is_open && !tsk->cong_link_cnt)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800818 revents |= EPOLLOUT;
Jon Maloyae236fb2017-10-13 11:04:25 +0200819 if (!tipc_sk_type_connectionless(sk))
820 break;
Eric Dumazet3ef7cf52019-10-23 22:44:50 -0700821 if (skb_queue_empty_lockless(&sk->sk_receive_queue))
Jon Maloyae236fb2017-10-13 11:04:25 +0200822 break;
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800823 revents |= EPOLLIN | EPOLLRDNORM;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100824 break;
825 case TIPC_DISCONNECTING:
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800826 revents = EPOLLIN | EPOLLRDNORM | EPOLLHUP;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100827 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000828 }
Jon Maloyae236fb2017-10-13 11:04:25 +0200829 return revents;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100830}
831
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400832/**
833 * tipc_sendmcast - send multicast message
834 * @sock: socket structure
Jon Maloy833f8672021-03-16 22:06:17 -0400835 * @ua: destination address struct
Al Viro562640f2014-11-15 01:13:43 -0500836 * @msg: message to send
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500837 * @dlen: length of data to send
838 * @timeout: timeout to wait for wakeup
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400839 *
840 * Called from function tipc_sendmsg(), which has done all sanity checks
Randy Dunlap637b77f2020-11-29 10:32:48 -0800841 * Return: the number of bytes sent on success, or errno
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400842 */
Jon Maloy833f8672021-03-16 22:06:17 -0400843static int tipc_sendmcast(struct socket *sock, struct tipc_uaddr *ua,
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500844 struct msghdr *msg, size_t dlen, long timeout)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400845{
846 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500847 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500848 struct tipc_msg *hdr = &tsk->phdr;
Ying Xuef2f98002015-01-09 15:27:05 +0800849 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500850 int mtu = tipc_bcast_get_mtu(net);
851 struct sk_buff_head pkts;
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500852 struct tipc_nlist dsts;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400853 int rc;
854
Jon Maloy75da2162017-10-13 11:04:23 +0200855 if (tsk->group)
856 return -EACCES;
857
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500858 /* Block or return if any destination link is congested */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500859 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
860 if (unlikely(rc))
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400861 return rc;
862
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500863 /* Lookup destination nodes */
864 tipc_nlist_init(&dsts, tipc_own_addr(net));
Jon Maloy833f8672021-03-16 22:06:17 -0400865 tipc_nametbl_lookup_mcast_nodes(net, ua, &dsts);
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500866 if (!dsts.local && !dsts.remote)
867 return -EHOSTUNREACH;
868
869 /* Build message header */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500870 msg_set_type(hdr, TIPC_MCAST_MSG);
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500871 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500872 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
873 msg_set_destport(hdr, 0);
874 msg_set_destnode(hdr, 0);
Jon Maloy833f8672021-03-16 22:06:17 -0400875 msg_set_nametype(hdr, ua->sr.type);
876 msg_set_namelower(hdr, ua->sr.lower);
877 msg_set_nameupper(hdr, ua->sr.upper);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400878
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500879 /* Build message as chain of buffers */
Jon Maloye654f9f2019-08-15 16:42:50 +0200880 __skb_queue_head_init(&pkts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500881 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500882
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500883 /* Send message if build was successful */
Tuong Lien01e661e2018-12-19 09:17:58 +0700884 if (unlikely(rc == dlen)) {
885 trace_tipc_sk_sendmcast(sk, skb_peek(&pkts),
886 TIPC_DUMP_SK_SNDQ, " ");
Jon Maloy833f8672021-03-16 22:06:17 -0400887 rc = tipc_mcast_xmit(net, &pkts, &tsk->mc_method, &dsts,
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500888 &tsk->cong_link_cnt);
Tuong Lien01e661e2018-12-19 09:17:58 +0700889 }
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500890
891 tipc_nlist_purge(&dsts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500892
893 return rc ? rc : dlen;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400894}
895
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500896/**
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200897 * tipc_send_group_msg - send a message to a member in the group
898 * @net: network namespace
Randy Dunlapf172f4b2020-11-29 10:32:49 -0800899 * @tsk: tipc socket
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200900 * @m: message to send
901 * @mb: group member
902 * @dnode: destination node
903 * @dport: destination port
904 * @dlen: total length of message data
905 */
906static int tipc_send_group_msg(struct net *net, struct tipc_sock *tsk,
907 struct msghdr *m, struct tipc_member *mb,
908 u32 dnode, u32 dport, int dlen)
909{
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200910 u16 bc_snd_nxt = tipc_group_bc_snd_nxt(tsk->group);
Jon Maloy2f487712017-10-13 11:04:31 +0200911 struct tipc_mc_method *method = &tsk->mc_method;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200912 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
913 struct tipc_msg *hdr = &tsk->phdr;
914 struct sk_buff_head pkts;
915 int mtu, rc;
916
917 /* Complete message header */
918 msg_set_type(hdr, TIPC_GRP_UCAST_MSG);
919 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
920 msg_set_destport(hdr, dport);
921 msg_set_destnode(hdr, dnode);
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200922 msg_set_grp_bc_seqno(hdr, bc_snd_nxt);
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200923
924 /* Build message as chain of buffers */
Jon Maloye654f9f2019-08-15 16:42:50 +0200925 __skb_queue_head_init(&pkts);
Hoang Lef73b1282019-10-29 07:51:21 +0700926 mtu = tipc_node_get_mtu(net, dnode, tsk->portid, false);
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200927 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
928 if (unlikely(rc != dlen))
929 return rc;
930
931 /* Send message */
932 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
933 if (unlikely(rc == -ELINKCONG)) {
934 tipc_dest_push(&tsk->cong_links, dnode, 0);
935 tsk->cong_link_cnt++;
936 }
937
Jon Maloy2f487712017-10-13 11:04:31 +0200938 /* Update send window */
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200939 tipc_group_update_member(mb, blks);
940
Jon Maloy2f487712017-10-13 11:04:31 +0200941 /* A broadcast sent within next EXPIRE period must follow same path */
942 method->rcast = true;
943 method->mandatory = true;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200944 return dlen;
945}
946
947/**
948 * tipc_send_group_unicast - send message to a member in the group
949 * @sock: socket structure
950 * @m: message to send
951 * @dlen: total length of message data
952 * @timeout: timeout to wait for wakeup
953 *
954 * Called from function tipc_sendmsg(), which has done all sanity checks
Randy Dunlap637b77f2020-11-29 10:32:48 -0800955 * Return: the number of bytes sent on success, or errno
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200956 */
957static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m,
958 int dlen, long timeout)
959{
960 struct sock *sk = sock->sk;
Jon Maloy006ed142021-03-16 22:06:18 -0400961 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200962 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
963 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200964 struct net *net = sock_net(sk);
965 struct tipc_member *mb = NULL;
966 u32 node, port;
967 int rc;
968
Jon Maloy006ed142021-03-16 22:06:18 -0400969 node = ua->sk.node;
970 port = ua->sk.ref;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200971 if (!port && !node)
972 return -EHOSTUNREACH;
973
974 /* Block or return if destination link or member is congested */
975 rc = tipc_wait_for_cond(sock, &timeout,
976 !tipc_dest_find(&tsk->cong_links, node, 0) &&
Cong Wang143ece62018-12-11 21:43:51 -0800977 tsk->group &&
978 !tipc_group_cong(tsk->group, node, port, blks,
979 &mb));
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200980 if (unlikely(rc))
981 return rc;
982
983 if (unlikely(!mb))
984 return -EHOSTUNREACH;
985
986 rc = tipc_send_group_msg(net, tsk, m, mb, node, port, dlen);
987
988 return rc ? rc : dlen;
989}
990
991/**
Jon Maloyee106d72017-10-13 11:04:28 +0200992 * tipc_send_group_anycast - send message to any member with given identity
993 * @sock: socket structure
994 * @m: message to send
995 * @dlen: total length of message data
996 * @timeout: timeout to wait for wakeup
997 *
998 * Called from function tipc_sendmsg(), which has done all sanity checks
Randy Dunlap637b77f2020-11-29 10:32:48 -0800999 * Return: the number of bytes sent on success, or errno
Jon Maloyee106d72017-10-13 11:04:28 +02001000 */
1001static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m,
1002 int dlen, long timeout)
1003{
Jon Maloy006ed142021-03-16 22:06:18 -04001004 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
Jon Maloyee106d72017-10-13 11:04:28 +02001005 struct sock *sk = sock->sk;
1006 struct tipc_sock *tsk = tipc_sk(sk);
1007 struct list_head *cong_links = &tsk->cong_links;
1008 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
Jon Maloy232d07b2018-01-08 21:03:30 +01001009 struct tipc_msg *hdr = &tsk->phdr;
Jon Maloyee106d72017-10-13 11:04:28 +02001010 struct tipc_member *first = NULL;
1011 struct tipc_member *mbr = NULL;
1012 struct net *net = sock_net(sk);
1013 u32 node, port, exclude;
Jon Maloyee106d72017-10-13 11:04:28 +02001014 struct list_head dsts;
1015 int lookups = 0;
1016 int dstcnt, rc;
1017 bool cong;
1018
1019 INIT_LIST_HEAD(&dsts);
Jon Maloy006ed142021-03-16 22:06:18 -04001020 ua->sa.type = msg_nametype(hdr);
1021 ua->scope = msg_lookup_scope(hdr);
Jon Maloyee106d72017-10-13 11:04:28 +02001022
1023 while (++lookups < 4) {
Cong Wang143ece62018-12-11 21:43:51 -08001024 exclude = tipc_group_exclude(tsk->group);
1025
Jon Maloyee106d72017-10-13 11:04:28 +02001026 first = NULL;
1027
1028 /* Look for a non-congested destination member, if any */
1029 while (1) {
Jon Maloy006ed142021-03-16 22:06:18 -04001030 if (!tipc_nametbl_lookup_group(net, ua, &dsts, &dstcnt,
1031 exclude, false))
Jon Maloyee106d72017-10-13 11:04:28 +02001032 return -EHOSTUNREACH;
1033 tipc_dest_pop(&dsts, &node, &port);
Cong Wang143ece62018-12-11 21:43:51 -08001034 cong = tipc_group_cong(tsk->group, node, port, blks,
1035 &mbr);
Jon Maloyee106d72017-10-13 11:04:28 +02001036 if (!cong)
1037 break;
1038 if (mbr == first)
1039 break;
1040 if (!first)
1041 first = mbr;
1042 }
1043
1044 /* Start over if destination was not in member list */
1045 if (unlikely(!mbr))
1046 continue;
1047
1048 if (likely(!cong && !tipc_dest_find(cong_links, node, 0)))
1049 break;
1050
1051 /* Block or return if destination link or member is congested */
1052 rc = tipc_wait_for_cond(sock, &timeout,
1053 !tipc_dest_find(cong_links, node, 0) &&
Cong Wang143ece62018-12-11 21:43:51 -08001054 tsk->group &&
1055 !tipc_group_cong(tsk->group, node, port,
Jon Maloyee106d72017-10-13 11:04:28 +02001056 blks, &mbr));
1057 if (unlikely(rc))
1058 return rc;
1059
1060 /* Send, unless destination disappeared while waiting */
1061 if (likely(mbr))
1062 break;
1063 }
1064
1065 if (unlikely(lookups >= 4))
1066 return -EHOSTUNREACH;
1067
1068 rc = tipc_send_group_msg(net, tsk, m, mbr, node, port, dlen);
1069
1070 return rc ? rc : dlen;
1071}
1072
1073/**
Jon Maloy75da2162017-10-13 11:04:23 +02001074 * tipc_send_group_bcast - send message to all members in communication group
Andrew Lunnd8141202020-07-13 01:15:14 +02001075 * @sock: socket structure
Jon Maloy75da2162017-10-13 11:04:23 +02001076 * @m: message to send
1077 * @dlen: total length of message data
1078 * @timeout: timeout to wait for wakeup
1079 *
1080 * Called from function tipc_sendmsg(), which has done all sanity checks
Randy Dunlap637b77f2020-11-29 10:32:48 -08001081 * Return: the number of bytes sent on success, or errno
Jon Maloy75da2162017-10-13 11:04:23 +02001082 */
1083static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m,
1084 int dlen, long timeout)
1085{
Jon Maloy006ed142021-03-16 22:06:18 -04001086 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
Jon Maloy75da2162017-10-13 11:04:23 +02001087 struct sock *sk = sock->sk;
1088 struct net *net = sock_net(sk);
1089 struct tipc_sock *tsk = tipc_sk(sk);
Cong Wang3c6306d2018-12-16 23:25:12 -08001090 struct tipc_nlist *dsts;
Jon Maloy75da2162017-10-13 11:04:23 +02001091 struct tipc_mc_method *method = &tsk->mc_method;
Jon Maloy2f487712017-10-13 11:04:31 +02001092 bool ack = method->mandatory && method->rcast;
Jon Maloyb7d42632017-10-13 11:04:26 +02001093 int blks = tsk_blocks(MCAST_H_SIZE + dlen);
Jon Maloy75da2162017-10-13 11:04:23 +02001094 struct tipc_msg *hdr = &tsk->phdr;
1095 int mtu = tipc_bcast_get_mtu(net);
1096 struct sk_buff_head pkts;
1097 int rc = -EHOSTUNREACH;
1098
Jon Maloyb7d42632017-10-13 11:04:26 +02001099 /* Block or return if any destination link or member is congested */
Cong Wang143ece62018-12-11 21:43:51 -08001100 rc = tipc_wait_for_cond(sock, &timeout,
1101 !tsk->cong_link_cnt && tsk->group &&
1102 !tipc_group_bc_cong(tsk->group, blks));
Jon Maloy75da2162017-10-13 11:04:23 +02001103 if (unlikely(rc))
1104 return rc;
1105
Cong Wang3c6306d2018-12-16 23:25:12 -08001106 dsts = tipc_group_dests(tsk->group);
1107 if (!dsts->local && !dsts->remote)
1108 return -EHOSTUNREACH;
1109
Jon Maloy75da2162017-10-13 11:04:23 +02001110 /* Complete message header */
Jon Maloy006ed142021-03-16 22:06:18 -04001111 if (ua) {
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001112 msg_set_type(hdr, TIPC_GRP_MCAST_MSG);
Jon Maloy006ed142021-03-16 22:06:18 -04001113 msg_set_nameinst(hdr, ua->sa.instance);
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001114 } else {
1115 msg_set_type(hdr, TIPC_GRP_BCAST_MSG);
1116 msg_set_nameinst(hdr, 0);
1117 }
Jon Maloyb7d42632017-10-13 11:04:26 +02001118 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
Jon Maloy75da2162017-10-13 11:04:23 +02001119 msg_set_destport(hdr, 0);
1120 msg_set_destnode(hdr, 0);
Cong Wang143ece62018-12-11 21:43:51 -08001121 msg_set_grp_bc_seqno(hdr, tipc_group_bc_snd_nxt(tsk->group));
Jon Maloy75da2162017-10-13 11:04:23 +02001122
Jon Maloy2f487712017-10-13 11:04:31 +02001123 /* Avoid getting stuck with repeated forced replicasts */
1124 msg_set_grp_bc_ack_req(hdr, ack);
1125
Jon Maloy75da2162017-10-13 11:04:23 +02001126 /* Build message as chain of buffers */
Jon Maloye654f9f2019-08-15 16:42:50 +02001127 __skb_queue_head_init(&pkts);
Jon Maloy75da2162017-10-13 11:04:23 +02001128 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1129 if (unlikely(rc != dlen))
1130 return rc;
1131
1132 /* Send message */
Jon Maloy2f487712017-10-13 11:04:31 +02001133 rc = tipc_mcast_xmit(net, &pkts, method, dsts, &tsk->cong_link_cnt);
Jon Maloy75da2162017-10-13 11:04:23 +02001134 if (unlikely(rc))
1135 return rc;
1136
Jon Maloyb7d42632017-10-13 11:04:26 +02001137 /* Update broadcast sequence number and send windows */
Jon Maloy2f487712017-10-13 11:04:31 +02001138 tipc_group_update_bc_members(tsk->group, blks, ack);
1139
1140 /* Broadcast link is now free to choose method for next broadcast */
1141 method->mandatory = false;
1142 method->expires = jiffies;
1143
Jon Maloy75da2162017-10-13 11:04:23 +02001144 return dlen;
1145}
1146
1147/**
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001148 * tipc_send_group_mcast - send message to all members with given identity
1149 * @sock: socket structure
1150 * @m: message to send
1151 * @dlen: total length of message data
1152 * @timeout: timeout to wait for wakeup
1153 *
1154 * Called from function tipc_sendmsg(), which has done all sanity checks
Randy Dunlap637b77f2020-11-29 10:32:48 -08001155 * Return: the number of bytes sent on success, or errno
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001156 */
1157static int tipc_send_group_mcast(struct socket *sock, struct msghdr *m,
1158 int dlen, long timeout)
1159{
Jon Maloy006ed142021-03-16 22:06:18 -04001160 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001161 struct sock *sk = sock->sk;
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001162 struct tipc_sock *tsk = tipc_sk(sk);
1163 struct tipc_group *grp = tsk->group;
Jon Maloy232d07b2018-01-08 21:03:30 +01001164 struct tipc_msg *hdr = &tsk->phdr;
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001165 struct net *net = sock_net(sk);
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001166 struct list_head dsts;
Jon Maloy006ed142021-03-16 22:06:18 -04001167 u32 dstcnt, exclude;
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001168
1169 INIT_LIST_HEAD(&dsts);
Jon Maloy006ed142021-03-16 22:06:18 -04001170 ua->sa.type = msg_nametype(hdr);
1171 ua->scope = msg_lookup_scope(hdr);
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001172 exclude = tipc_group_exclude(grp);
Jon Maloy232d07b2018-01-08 21:03:30 +01001173
Jon Maloy006ed142021-03-16 22:06:18 -04001174 if (!tipc_nametbl_lookup_group(net, ua, &dsts, &dstcnt, exclude, true))
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001175 return -EHOSTUNREACH;
1176
1177 if (dstcnt == 1) {
Jon Maloy006ed142021-03-16 22:06:18 -04001178 tipc_dest_pop(&dsts, &ua->sk.node, &ua->sk.ref);
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001179 return tipc_send_group_unicast(sock, m, dlen, timeout);
1180 }
1181
1182 tipc_dest_list_purge(&dsts);
1183 return tipc_send_group_bcast(sock, m, dlen, timeout);
1184}
1185
1186/**
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001187 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
Randy Dunlapf172f4b2020-11-29 10:32:49 -08001188 * @net: the associated network namespace
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001189 * @arrvq: queue with arriving messages, to be cloned after destination lookup
1190 * @inputq: queue with cloned messages, delivered to socket after dest lookup
1191 *
1192 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001193 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001194void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
1195 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001196{
Jon Maloy75da2162017-10-13 11:04:23 +02001197 u32 self = tipc_own_addr(net);
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001198 struct sk_buff *skb, *_skb;
Colin Ian Kingb053fcc2018-07-31 17:01:37 +01001199 u32 portid, onode;
Jon Maloy232d07b2018-01-08 21:03:30 +01001200 struct sk_buff_head tmpq;
Jon Maloy75da2162017-10-13 11:04:23 +02001201 struct list_head dports;
Jon Maloy232d07b2018-01-08 21:03:30 +01001202 struct tipc_msg *hdr;
Jon Maloy45ceea22021-03-16 22:06:16 -04001203 struct tipc_uaddr ua;
Jon Maloy232d07b2018-01-08 21:03:30 +01001204 int user, mtyp, hlen;
1205 bool exact;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001206
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001207 __skb_queue_head_init(&tmpq);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001208 INIT_LIST_HEAD(&dports);
Jon Maloy45ceea22021-03-16 22:06:16 -04001209 ua.addrtype = TIPC_SERVICE_RANGE;
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001210
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001211 skb = tipc_skb_peek(arrvq, &inputq->lock);
1212 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
Jon Maloy232d07b2018-01-08 21:03:30 +01001213 hdr = buf_msg(skb);
1214 user = msg_user(hdr);
1215 mtyp = msg_type(hdr);
1216 hlen = skb_headroom(skb) + msg_hdr_sz(hdr);
Jon Maloy232d07b2018-01-08 21:03:30 +01001217 onode = msg_orignode(hdr);
Jon Maloy45ceea22021-03-16 22:06:16 -04001218 ua.sr.type = msg_nametype(hdr);
Jon Maloy232d07b2018-01-08 21:03:30 +01001219
Jon Maloy2f487712017-10-13 11:04:31 +02001220 if (mtyp == TIPC_GRP_UCAST_MSG || user == GROUP_PROTOCOL) {
1221 spin_lock_bh(&inputq->lock);
1222 if (skb_peek(arrvq) == skb) {
1223 __skb_dequeue(arrvq);
1224 __skb_queue_tail(inputq, skb);
1225 }
Jon Maloyc545a942017-12-11 19:11:55 +01001226 kfree_skb(skb);
Jon Maloy2f487712017-10-13 11:04:31 +02001227 spin_unlock_bh(&inputq->lock);
1228 continue;
1229 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001230
Jon Maloy232d07b2018-01-08 21:03:30 +01001231 /* Group messages require exact scope match */
1232 if (msg_in_group(hdr)) {
Jon Maloy45ceea22021-03-16 22:06:16 -04001233 ua.sr.lower = 0;
1234 ua.sr.upper = ~0;
1235 ua.scope = msg_lookup_scope(hdr);
Jon Maloy232d07b2018-01-08 21:03:30 +01001236 exact = true;
1237 } else {
1238 /* TIPC_NODE_SCOPE means "any scope" in this context */
1239 if (onode == self)
Jon Maloy45ceea22021-03-16 22:06:16 -04001240 ua.scope = TIPC_NODE_SCOPE;
Jon Maloy232d07b2018-01-08 21:03:30 +01001241 else
Jon Maloy45ceea22021-03-16 22:06:16 -04001242 ua.scope = TIPC_CLUSTER_SCOPE;
Jon Maloy232d07b2018-01-08 21:03:30 +01001243 exact = false;
Jon Maloy45ceea22021-03-16 22:06:16 -04001244 ua.sr.lower = msg_namelower(hdr);
1245 ua.sr.upper = msg_nameupper(hdr);
Jon Maloy75da2162017-10-13 11:04:23 +02001246 }
Jon Maloy232d07b2018-01-08 21:03:30 +01001247
1248 /* Create destination port list: */
Jon Maloy45ceea22021-03-16 22:06:16 -04001249 tipc_nametbl_lookup_mcast_sockets(net, &ua, exact, &dports);
Jon Maloy232d07b2018-01-08 21:03:30 +01001250
1251 /* Clone message per destination */
Jon Maloya80ae532017-10-13 11:04:22 +02001252 while (tipc_dest_pop(&dports, NULL, &portid)) {
Jon Maloy232d07b2018-01-08 21:03:30 +01001253 _skb = __pskb_copy(skb, hlen, GFP_ATOMIC);
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001254 if (_skb) {
1255 msg_set_destport(buf_msg(_skb), portid);
1256 __skb_queue_tail(&tmpq, _skb);
1257 continue;
1258 }
1259 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001260 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001261 /* Append to inputq if not already done by other thread */
1262 spin_lock_bh(&inputq->lock);
1263 if (skb_peek(arrvq) == skb) {
1264 skb_queue_splice_tail_init(&tmpq, inputq);
Lv Yunlong6bf24dc2021-03-28 00:30:29 -07001265 __skb_dequeue(arrvq);
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001266 }
1267 spin_unlock_bh(&inputq->lock);
1268 __skb_queue_purge(&tmpq);
1269 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001270 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001271 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001272}
1273
Jon Maloyc0bceb92019-10-30 14:00:41 +01001274/* tipc_sk_push_backlog(): send accumulated buffers in socket write queue
1275 * when socket is in Nagle mode
1276 */
Tuong Lien0a3e0602020-05-26 16:38:38 +07001277static void tipc_sk_push_backlog(struct tipc_sock *tsk, bool nagle_ack)
Jon Maloyc0bceb92019-10-30 14:00:41 +01001278{
1279 struct sk_buff_head *txq = &tsk->sk.sk_write_queue;
Tuong Lien0a3e0602020-05-26 16:38:38 +07001280 struct sk_buff *skb = skb_peek_tail(txq);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001281 struct net *net = sock_net(&tsk->sk);
1282 u32 dnode = tsk_peer_node(tsk);
1283 int rc;
1284
Tuong Lien0a3e0602020-05-26 16:38:38 +07001285 if (nagle_ack) {
1286 tsk->pkt_cnt += skb_queue_len(txq);
1287 if (!tsk->pkt_cnt || tsk->msg_acc / tsk->pkt_cnt < 2) {
1288 tsk->oneway = 0;
1289 if (tsk->nagle_start < NAGLE_START_MAX)
1290 tsk->nagle_start *= 2;
1291 tsk->expect_ack = false;
1292 pr_debug("tsk %10u: bad nagle %u -> %u, next start %u!\n",
1293 tsk->portid, tsk->msg_acc, tsk->pkt_cnt,
1294 tsk->nagle_start);
1295 } else {
1296 tsk->nagle_start = NAGLE_START_INIT;
1297 if (skb) {
1298 msg_set_ack_required(buf_msg(skb));
1299 tsk->expect_ack = true;
1300 } else {
1301 tsk->expect_ack = false;
1302 }
1303 }
1304 tsk->msg_acc = 0;
1305 tsk->pkt_cnt = 0;
1306 }
1307
Tung Nguyend34910e2019-11-28 10:10:08 +07001308 if (!skb || tsk->cong_link_cnt)
1309 return;
1310
1311 /* Do not send SYN again after congestion */
1312 if (msg_is_syn(buf_msg(skb)))
Jon Maloyc0bceb92019-10-30 14:00:41 +01001313 return;
1314
Tuong Lien0a3e0602020-05-26 16:38:38 +07001315 if (tsk->msg_acc)
1316 tsk->pkt_cnt += skb_queue_len(txq);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001317 tsk->snt_unacked += tsk->snd_backlog;
1318 tsk->snd_backlog = 0;
Jon Maloyc0bceb92019-10-30 14:00:41 +01001319 rc = tipc_node_xmit(net, txq, dnode, tsk->portid);
1320 if (rc == -ELINKCONG)
1321 tsk->cong_link_cnt = 1;
1322}
1323
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001324/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02001325 * tipc_sk_conn_proto_rcv - receive a connection mng protocol message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001326 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001327 * @skb: pointer to message buffer.
Randy Dunlapf172f4b2020-11-29 10:32:49 -08001328 * @inputq: buffer list containing the buffers
1329 * @xmitq: output message area
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001330 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02001331static void tipc_sk_conn_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
Parthasarathy Bhuvaragane7eb0582018-10-10 17:50:23 +02001332 struct sk_buff_head *inputq,
Jon Maloy64ac5f52017-10-13 11:04:20 +02001333 struct sk_buff_head *xmitq)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001334{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001335 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy64ac5f52017-10-13 11:04:20 +02001336 u32 onode = tsk_own_node(tsk);
1337 struct sock *sk = &tsk->sk;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001338 int mtyp = msg_type(hdr);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001339 bool was_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001340
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001341 /* Ignore if connection cannot be validated: */
Tuong Lien01e661e2018-12-19 09:17:58 +07001342 if (!tsk_peer_msg(tsk, hdr)) {
1343 trace_tipc_sk_drop_msg(sk, skb, TIPC_DUMP_NONE, "@proto_rcv!");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001344 goto exit;
Tuong Lien01e661e2018-12-19 09:17:58 +07001345 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001346
Parthasarathy Bhuvaraganc1be7752017-04-26 10:05:02 +02001347 if (unlikely(msg_errcode(hdr))) {
1348 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1349 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
1350 tsk_peer_port(tsk));
1351 sk->sk_state_change(sk);
Parthasarathy Bhuvaragane7eb0582018-10-10 17:50:23 +02001352
1353 /* State change is ignored if socket already awake,
1354 * - convert msg to abort msg and add to inqueue
1355 */
1356 msg_set_user(hdr, TIPC_CRITICAL_IMPORTANCE);
1357 msg_set_type(hdr, TIPC_CONN_MSG);
1358 msg_set_size(hdr, BASIC_H_SIZE);
1359 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
1360 __skb_queue_tail(inputq, skb);
1361 return;
Parthasarathy Bhuvaraganc1be7752017-04-26 10:05:02 +02001362 }
1363
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001364 tsk->probe_unacked = false;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001365
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001366 if (mtyp == CONN_PROBE) {
1367 msg_set_type(hdr, CONN_PROBE_REPLY);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001368 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
1369 __skb_queue_tail(xmitq, skb);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001370 return;
1371 } else if (mtyp == CONN_ACK) {
Jon Maloyc0bceb92019-10-30 14:00:41 +01001372 was_cong = tsk_conn_cong(tsk);
Tuong Lien0a3e0602020-05-26 16:38:38 +07001373 tipc_sk_push_backlog(tsk, msg_nagle_ack(hdr));
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001374 tsk->snt_unacked -= msg_conn_ack(hdr);
1375 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1376 tsk->snd_win = msg_adv_win(hdr);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001377 if (was_cong && !tsk_conn_cong(tsk))
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001378 sk->sk_write_space(sk);
1379 } else if (mtyp != CONN_PROBE_REPLY) {
1380 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001381 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001382exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001383 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001384}
1385
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001386/**
Ying Xue247f0f32014-02-18 16:06:46 +08001387 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +01001388 * @sock: socket structure
1389 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001390 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001391 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001393 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
1395 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001396 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08001397 * Return: the number of bytes sent on success, or errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398 */
Ying Xue1b784142015-03-02 15:37:48 +08001399static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001400 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001401{
Ying Xue39a0295f2015-03-02 15:37:47 +08001402 struct sock *sk = sock->sk;
1403 int ret;
1404
1405 lock_sock(sk);
1406 ret = __tipc_sendmsg(sock, m, dsz);
1407 release_sock(sk);
1408
1409 return ret;
1410}
1411
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001412static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a0295f2015-03-02 15:37:47 +08001413{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001414 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001415 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001416 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy908148b2021-03-16 22:06:15 -04001417 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001418 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1419 struct list_head *clinks = &tsk->cong_links;
1420 bool syn = !tipc_sk_type_connectionless(sk);
Jon Maloy75da2162017-10-13 11:04:23 +02001421 struct tipc_group *grp = tsk->group;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001422 struct tipc_msg *hdr = &tsk->phdr;
Jon Maloy908148b2021-03-16 22:06:15 -04001423 struct tipc_socket_addr skaddr;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001424 struct sk_buff_head pkts;
Jon Maloy908148b2021-03-16 22:06:15 -04001425 int atype, mtu, rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001426
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001427 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
Allan Stephensc29c3f72010-04-20 17:58:24 -04001428 return -EMSGSIZE;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001429
Jon Maloy908148b2021-03-16 22:06:15 -04001430 if (ua) {
1431 if (!tipc_uaddr_valid(ua, m->msg_namelen))
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001432 return -EINVAL;
Jon Maloy908148b2021-03-16 22:06:15 -04001433 atype = ua->addrtype;
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001434 }
1435
Jon Maloy908148b2021-03-16 22:06:15 -04001436 /* If socket belongs to a communication group follow other paths */
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001437 if (grp) {
Jon Maloy908148b2021-03-16 22:06:15 -04001438 if (!ua)
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001439 return tipc_send_group_bcast(sock, m, dlen, timeout);
Jon Maloy908148b2021-03-16 22:06:15 -04001440 if (atype == TIPC_SERVICE_ADDR)
Jon Maloyee106d72017-10-13 11:04:28 +02001441 return tipc_send_group_anycast(sock, m, dlen, timeout);
Jon Maloy908148b2021-03-16 22:06:15 -04001442 if (atype == TIPC_SOCKET_ADDR)
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001443 return tipc_send_group_unicast(sock, m, dlen, timeout);
Jon Maloy908148b2021-03-16 22:06:15 -04001444 if (atype == TIPC_SERVICE_RANGE)
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001445 return tipc_send_group_mcast(sock, m, dlen, timeout);
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001446 return -EINVAL;
1447 }
Jon Maloy75da2162017-10-13 11:04:23 +02001448
Jon Maloy908148b2021-03-16 22:06:15 -04001449 if (!ua) {
1450 ua = (struct tipc_uaddr *)&tsk->peer;
1451 if (!syn && ua->family != AF_TIPC)
Erik Hugnef2f80362015-03-19 09:02:19 +01001452 return -EDESTADDRREQ;
Jon Maloy02fdc142021-03-29 13:17:31 -04001453 atype = ua->addrtype;
Erik Hugnef2f80362015-03-19 09:02:19 +01001454 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001455
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001456 if (unlikely(syn)) {
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001457 if (sk->sk_state == TIPC_LISTEN)
Ying Xue39a0295f2015-03-02 15:37:47 +08001458 return -EPIPE;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001459 if (sk->sk_state != TIPC_OPEN)
Ying Xue39a0295f2015-03-02 15:37:47 +08001460 return -EISCONN;
1461 if (tsk->published)
1462 return -EOPNOTSUPP;
Jon Maloy908148b2021-03-16 22:06:15 -04001463 if (atype == TIPC_SERVICE_ADDR) {
1464 tsk->conn_type = ua->sa.type;
1465 tsk->conn_instance = ua->sa.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001466 }
Jon Maloy25b92212018-09-28 20:23:21 +02001467 msg_set_syn(hdr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001468 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001469
Jon Maloy908148b2021-03-16 22:06:15 -04001470 /* Determine destination */
1471 if (atype == TIPC_SERVICE_RANGE) {
Jon Maloy833f8672021-03-16 22:06:17 -04001472 return tipc_sendmcast(sock, ua, m, dlen, timeout);
Jon Maloy908148b2021-03-16 22:06:15 -04001473 } else if (atype == TIPC_SERVICE_ADDR) {
1474 skaddr.node = ua->lookup_node;
1475 ua->scope = tipc_node2scope(skaddr.node);
1476 if (!tipc_nametbl_lookup_anycast(net, ua, &skaddr))
Ying Xue39a0295f2015-03-02 15:37:47 +08001477 return -EHOSTUNREACH;
Jon Maloy908148b2021-03-16 22:06:15 -04001478 } else if (atype == TIPC_SOCKET_ADDR) {
1479 skaddr = ua->sk;
Jon Maloy335b9292018-04-12 01:15:48 +02001480 } else {
1481 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001482 }
1483
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001484 /* Block or return if destination link is congested */
Jon Maloya80ae532017-10-13 11:04:22 +02001485 rc = tipc_wait_for_cond(sock, &timeout,
Jon Maloy908148b2021-03-16 22:06:15 -04001486 !tipc_dest_find(clinks, skaddr.node, 0));
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001487 if (unlikely(rc))
Ying Xue39a0295f2015-03-02 15:37:47 +08001488 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001489
Jon Maloy908148b2021-03-16 22:06:15 -04001490 /* Finally build message header */
1491 msg_set_destnode(hdr, skaddr.node);
1492 msg_set_destport(hdr, skaddr.ref);
1493 if (atype == TIPC_SERVICE_ADDR) {
Tuong Lienabc9b4e2019-12-10 15:21:04 +07001494 msg_set_type(hdr, TIPC_NAMED_MSG);
1495 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
Jon Maloy908148b2021-03-16 22:06:15 -04001496 msg_set_nametype(hdr, ua->sa.type);
1497 msg_set_nameinst(hdr, ua->sa.instance);
1498 msg_set_lookup_scope(hdr, ua->scope);
Jon Maloyb6f88d92020-11-25 13:29:15 -05001499 } else { /* TIPC_SOCKET_ADDR */
Tuong Lienabc9b4e2019-12-10 15:21:04 +07001500 msg_set_type(hdr, TIPC_DIRECT_MSG);
1501 msg_set_lookup_scope(hdr, 0);
Tuong Lienabc9b4e2019-12-10 15:21:04 +07001502 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
1503 }
1504
Jon Maloy908148b2021-03-16 22:06:15 -04001505 /* Add message body */
Jon Maloye654f9f2019-08-15 16:42:50 +02001506 __skb_queue_head_init(&pkts);
Jon Maloy908148b2021-03-16 22:06:15 -04001507 mtu = tipc_node_get_mtu(net, skaddr.node, tsk->portid, true);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001508 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1509 if (unlikely(rc != dlen))
1510 return rc;
Tung Nguyen2fe97a52019-11-28 10:10:05 +07001511 if (unlikely(syn && !tipc_msg_skb_clone(&pkts, &sk->sk_write_queue))) {
1512 __skb_queue_purge(&pkts);
Tung Nguyen67879272018-09-28 20:23:22 +02001513 return -ENOMEM;
Tung Nguyen2fe97a52019-11-28 10:10:05 +07001514 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001515
Jon Maloy908148b2021-03-16 22:06:15 -04001516 /* Send message */
Tuong Lien01e661e2018-12-19 09:17:58 +07001517 trace_tipc_sk_sendmsg(sk, skb_peek(&pkts), TIPC_DUMP_SK_SNDQ, " ");
Jon Maloy908148b2021-03-16 22:06:15 -04001518 rc = tipc_node_xmit(net, &pkts, skaddr.node, tsk->portid);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001519 if (unlikely(rc == -ELINKCONG)) {
Jon Maloy908148b2021-03-16 22:06:15 -04001520 tipc_dest_push(clinks, skaddr.node, 0);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001521 tsk->cong_link_cnt++;
1522 rc = 0;
1523 }
1524
1525 if (unlikely(syn && !rc))
1526 tipc_set_sk_state(sk, TIPC_CONNECTING);
1527
1528 return rc ? rc : dlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001529}
1530
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001531/**
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001532 * tipc_sendstream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001533 * @sock: socket structure
1534 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001535 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001536 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001537 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001538 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08001539 * Return: the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001540 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001541 */
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001542static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001543{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001544 struct sock *sk = sock->sk;
Ying Xue39a0295f2015-03-02 15:37:47 +08001545 int ret;
1546
1547 lock_sock(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001548 ret = __tipc_sendstream(sock, m, dsz);
Ying Xue39a0295f2015-03-02 15:37:47 +08001549 release_sock(sk);
1550
1551 return ret;
1552}
1553
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001554static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a0295f2015-03-02 15:37:47 +08001555{
1556 struct sock *sk = sock->sk;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001557 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001558 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001559 struct sk_buff_head *txq = &sk->sk_write_queue;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001560 struct tipc_sock *tsk = tipc_sk(sk);
1561 struct tipc_msg *hdr = &tsk->phdr;
1562 struct net *net = sock_net(sk);
Tuong Lien0a3e0602020-05-26 16:38:38 +07001563 struct sk_buff *skb;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001564 u32 dnode = tsk_peer_node(tsk);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001565 int maxnagle = tsk->maxnagle;
1566 int maxpkt = tsk->max_pkt;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001567 int send, sent = 0;
Jon Maloyc0bceb92019-10-30 14:00:41 +01001568 int blocks, rc = 0;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001569
1570 if (unlikely(dlen > INT_MAX))
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001571 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001572
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001573 /* Handle implicit connection setup */
1574 if (unlikely(dest)) {
1575 rc = __tipc_sendmsg(sock, m, dlen);
Parthasarathy Bhuvaragan92ef12b2018-09-25 18:21:58 +02001576 if (dlen && dlen == rc) {
1577 tsk->peer_caps = tipc_node_get_capabilities(net, dnode);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001578 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
Parthasarathy Bhuvaragan92ef12b2018-09-25 18:21:58 +02001579 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001580 return rc;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001581 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001582
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001583 do {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001584 rc = tipc_wait_for_cond(sock, &timeout,
1585 (!tsk->cong_link_cnt &&
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -05001586 !tsk_conn_cong(tsk) &&
1587 tipc_sk_connected(sk)));
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001588 if (unlikely(rc))
1589 break;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001590 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001591 blocks = tsk->snd_backlog;
Tuong Lienc9aa81f2020-06-11 17:07:35 +07001592 if (tsk->oneway++ >= tsk->nagle_start && maxnagle &&
1593 send <= maxnagle) {
Jon Maloyc0bceb92019-10-30 14:00:41 +01001594 rc = tipc_msg_append(hdr, m, send, maxnagle, txq);
1595 if (unlikely(rc < 0))
1596 break;
1597 blocks += rc;
Tuong Lien0a3e0602020-05-26 16:38:38 +07001598 tsk->msg_acc++;
Jon Maloyc0bceb92019-10-30 14:00:41 +01001599 if (blocks <= 64 && tsk->expect_ack) {
1600 tsk->snd_backlog = blocks;
1601 sent += send;
1602 break;
Tuong Lien0a3e0602020-05-26 16:38:38 +07001603 } else if (blocks > 64) {
1604 tsk->pkt_cnt += skb_queue_len(txq);
1605 } else {
1606 skb = skb_peek_tail(txq);
YueHaibing4c21daa2020-05-28 22:34:07 +08001607 if (skb) {
1608 msg_set_ack_required(buf_msg(skb));
1609 tsk->expect_ack = true;
1610 } else {
1611 tsk->expect_ack = false;
1612 }
Tuong Lien0a3e0602020-05-26 16:38:38 +07001613 tsk->msg_acc = 0;
1614 tsk->pkt_cnt = 0;
Jon Maloyc0bceb92019-10-30 14:00:41 +01001615 }
Jon Maloyc0bceb92019-10-30 14:00:41 +01001616 } else {
1617 rc = tipc_msg_build(hdr, m, sent, send, maxpkt, txq);
1618 if (unlikely(rc != send))
1619 break;
1620 blocks += tsk_inc(tsk, send + MIN_H_SIZE);
1621 }
1622 trace_tipc_sk_sendstream(sk, skb_peek(txq),
Tuong Lien01e661e2018-12-19 09:17:58 +07001623 TIPC_DUMP_SK_SNDQ, " ");
Jon Maloyc0bceb92019-10-30 14:00:41 +01001624 rc = tipc_node_xmit(net, txq, dnode, tsk->portid);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001625 if (unlikely(rc == -ELINKCONG)) {
1626 tsk->cong_link_cnt = 1;
1627 rc = 0;
1628 }
1629 if (likely(!rc)) {
Jon Maloyc0bceb92019-10-30 14:00:41 +01001630 tsk->snt_unacked += blocks;
1631 tsk->snd_backlog = 0;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001632 sent += send;
1633 }
1634 } while (sent < dlen && !rc);
1635
Parthasarathy Bhuvaragan3364d612017-04-24 15:00:42 +02001636 return sent ? sent : rc;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001637}
1638
1639/**
1640 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001641 * @sock: socket structure
1642 * @m: message to send
1643 * @dsz: length of data to be transmitted
1644 *
1645 * Used for SOCK_SEQPACKET messages.
1646 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08001647 * Return: the number of bytes sent on success, or errno otherwise
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001648 */
Ying Xue1b784142015-03-02 15:37:48 +08001649static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001650{
1651 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1652 return -EMSGSIZE;
1653
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001654 return tipc_sendstream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001655}
1656
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001657/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001658 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001659static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001660 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661{
Ying Xue3721e9c2015-01-13 17:07:48 +08001662 struct sock *sk = &tsk->sk;
1663 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001664 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001665
Jon Maloy25b92212018-09-28 20:23:21 +02001666 msg_set_syn(msg, 0);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001667 msg_set_destnode(msg, peer_node);
1668 msg_set_destport(msg, peer_port);
1669 msg_set_type(msg, TIPC_CONN_MSG);
1670 msg_set_lookup_scope(msg, 0);
1671 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001672
Jon Maloy0d5fcebf2017-10-20 11:21:32 +02001673 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001674 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
Ying Xuef2f98002015-01-09 15:27:05 +08001675 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
Hoang Lef73b1282019-10-29 07:51:21 +07001676 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid, true);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001677 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001678 tsk_set_nagle(tsk);
Tung Nguyen67879272018-09-28 20:23:22 +02001679 __skb_queue_purge(&sk->sk_write_queue);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001680 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1681 return;
1682
1683 /* Fall back to message based flow control */
1684 tsk->rcv_win = FLOWCTL_MSG_WIN;
1685 tsk->snd_win = FLOWCTL_MSG_WIN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001686}
1687
1688/**
Jon Maloy31c82a22017-10-13 11:04:24 +02001689 * tipc_sk_set_orig_addr - capture sender's address for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001690 * @m: descriptor for message info
Andrew Lunnd8141202020-07-13 01:15:14 +02001691 * @skb: received message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001692 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001693 * Note: Address is not captured if not requested by receiver.
1694 */
Jon Maloy31c82a22017-10-13 11:04:24 +02001695static void tipc_sk_set_orig_addr(struct msghdr *m, struct sk_buff *skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696{
Jon Maloy31c82a22017-10-13 11:04:24 +02001697 DECLARE_SOCKADDR(struct sockaddr_pair *, srcaddr, m->msg_name);
1698 struct tipc_msg *hdr = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699
Jon Maloy31c82a22017-10-13 11:04:24 +02001700 if (!srcaddr)
1701 return;
1702
1703 srcaddr->sock.family = AF_TIPC;
Jon Maloyb6f88d92020-11-25 13:29:15 -05001704 srcaddr->sock.addrtype = TIPC_SOCKET_ADDR;
Eric Dumazet09c8b972018-05-09 09:50:22 -07001705 srcaddr->sock.scope = 0;
Jon Maloy31c82a22017-10-13 11:04:24 +02001706 srcaddr->sock.addr.id.ref = msg_origport(hdr);
1707 srcaddr->sock.addr.id.node = msg_orignode(hdr);
1708 srcaddr->sock.addr.name.domain = 0;
Jon Maloy31c82a22017-10-13 11:04:24 +02001709 m->msg_namelen = sizeof(struct sockaddr_tipc);
1710
1711 if (!msg_in_group(hdr))
1712 return;
1713
1714 /* Group message users may also want to know sending member's id */
1715 srcaddr->member.family = AF_TIPC;
Jon Maloyb6f88d92020-11-25 13:29:15 -05001716 srcaddr->member.addrtype = TIPC_SERVICE_ADDR;
Eric Dumazet09c8b972018-05-09 09:50:22 -07001717 srcaddr->member.scope = 0;
Jon Maloy31c82a22017-10-13 11:04:24 +02001718 srcaddr->member.addr.name.name.type = msg_nametype(hdr);
1719 srcaddr->member.addr.name.name.instance = TIPC_SKB_CB(skb)->orig_member;
1720 srcaddr->member.addr.name.domain = 0;
1721 m->msg_namelen = sizeof(*srcaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001722}
1723
1724/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001725 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726 * @m: descriptor for message info
Jon Maloy1c1274a2018-11-17 12:17:06 -05001727 * @skb: received message buffer
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001728 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001729 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001730 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001731 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08001732 * Return: 0 if successful, otherwise errno
Per Lidenb97bf3f2006-01-02 19:04:38 +01001733 */
Jon Maloy1c1274a2018-11-17 12:17:06 -05001734static int tipc_sk_anc_data_recv(struct msghdr *m, struct sk_buff *skb,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001735 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001736{
Jon Maloy1c1274a2018-11-17 12:17:06 -05001737 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001738 u32 anc_data[3];
1739 u32 err;
1740 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001741 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001742 int res;
1743
1744 if (likely(m->msg_controllen == 0))
1745 return 0;
Jon Maloy1c1274a2018-11-17 12:17:06 -05001746 msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001747
1748 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001749 err = msg ? msg_errcode(msg) : 0;
1750 if (unlikely(err)) {
1751 anc_data[0] = err;
1752 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001753 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1754 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001755 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001756 if (anc_data[1]) {
Jon Maloy1c1274a2018-11-17 12:17:06 -05001757 if (skb_linearize(skb))
1758 return -ENOMEM;
1759 msg = buf_msg(skb);
Allan Stephens2db99832010-12-31 18:59:33 +00001760 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1761 msg_data(msg));
1762 if (res)
1763 return res;
1764 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001765 }
1766
1767 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001768 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1769 switch (dest_type) {
1770 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001771 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001772 anc_data[0] = msg_nametype(msg);
1773 anc_data[1] = msg_namelower(msg);
1774 anc_data[2] = msg_namelower(msg);
1775 break;
1776 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001777 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001778 anc_data[0] = msg_nametype(msg);
1779 anc_data[1] = msg_namelower(msg);
1780 anc_data[2] = msg_nameupper(msg);
1781 break;
1782 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001783 has_name = (tsk->conn_type != 0);
1784 anc_data[0] = tsk->conn_type;
1785 anc_data[1] = tsk->conn_instance;
1786 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001787 break;
1788 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001789 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001790 }
Allan Stephens2db99832010-12-31 18:59:33 +00001791 if (has_name) {
1792 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1793 if (res)
1794 return res;
1795 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001796
1797 return 0;
1798}
1799
Tuong Lienc7268582020-05-13 19:33:16 +07001800static struct sk_buff *tipc_sk_build_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001801{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001802 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08001803 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001804 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001805 u32 peer_port = tsk_peer_port(tsk);
1806 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001807
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001808 if (!tipc_sk_connected(sk))
Tuong Lienc7268582020-05-13 19:33:16 +07001809 return NULL;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001810 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1811 dnode, tsk_own_node(tsk), peer_port,
1812 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001813 if (!skb)
Tuong Lienc7268582020-05-13 19:33:16 +07001814 return NULL;
Ying Xuea6ca1092014-11-26 11:41:55 +08001815 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001816 msg_set_conn_ack(msg, tsk->rcv_unacked);
1817 tsk->rcv_unacked = 0;
1818
1819 /* Adjust to and advertize the correct window limit */
1820 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1821 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1822 msg_set_adv_win(msg, tsk->rcv_win);
1823 }
Tuong Lienc7268582020-05-13 19:33:16 +07001824 return skb;
1825}
1826
1827static void tipc_sk_send_ack(struct tipc_sock *tsk)
1828{
1829 struct sk_buff *skb;
1830
1831 skb = tipc_sk_build_ack(tsk);
1832 if (!skb)
1833 return;
1834
1835 tipc_node_xmit_skb(sock_net(&tsk->sk), skb, tsk_peer_node(tsk),
1836 msg_link_selector(buf_msg(skb)));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001837}
1838
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001839static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001840{
1841 struct sock *sk = sock->sk;
Tung Nguyen48766a52019-02-19 11:20:48 +07001842 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001843 long timeo = *timeop;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001844 int err = sock_error(sk);
1845
1846 if (err)
1847 return err;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001848
1849 for (;;) {
Ying Xuefe8e4642014-03-06 14:40:18 +01001850 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01001851 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001852 err = -ENOTCONN;
1853 break;
1854 }
Tung Nguyen48766a52019-02-19 11:20:48 +07001855 add_wait_queue(sk_sleep(sk), &wait);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001856 release_sock(sk);
Tung Nguyen48766a52019-02-19 11:20:48 +07001857 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
1858 sched_annotate_sleep();
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001859 lock_sock(sk);
Tung Nguyen48766a52019-02-19 11:20:48 +07001860 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001861 }
1862 err = 0;
1863 if (!skb_queue_empty(&sk->sk_receive_queue))
1864 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001865 err = -EAGAIN;
1866 if (!timeo)
1867 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001868 err = sock_intr_errno(timeo);
1869 if (signal_pending(current))
1870 break;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001871
1872 err = sock_error(sk);
1873 if (err)
1874 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001875 }
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001876 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001877 return err;
1878}
1879
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001880/**
Ying Xue247f0f32014-02-18 16:06:46 +08001881 * tipc_recvmsg - receive packet-oriented message
Randy Dunlapf172f4b2020-11-29 10:32:49 -08001882 * @sock: network socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001883 * @m: descriptor for message info
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001884 * @buflen: length of user buffer area
Per Lidenb97bf3f2006-01-02 19:04:38 +01001885 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001886 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001887 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1888 * If the complete message doesn't fit in user area, truncate it.
1889 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08001890 * Return: size of returned message data, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001891 */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001892static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
1893 size_t buflen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001894{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001895 struct sock *sk = sock->sk;
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001896 bool connected = !tipc_sk_type_connectionless(sk);
Jon Maloyae236fb2017-10-13 11:04:25 +02001897 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001898 int rc, err, hlen, dlen, copy;
Jon Maloyb7d42632017-10-13 11:04:26 +02001899 struct sk_buff_head xmitq;
Jon Maloyae236fb2017-10-13 11:04:25 +02001900 struct tipc_msg *hdr;
1901 struct sk_buff *skb;
1902 bool grp_evt;
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001903 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001904
Allan Stephens0c3141e2008-04-15 00:22:02 -07001905 /* Catch invalid receive requests */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001906 if (unlikely(!buflen))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001907 return -EINVAL;
1908
Allan Stephens0c3141e2008-04-15 00:22:02 -07001909 lock_sock(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001910 if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
1911 rc = -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001912 goto exit;
1913 }
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001914 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001915
Jon Maloyb7d42632017-10-13 11:04:26 +02001916 /* Step rcv queue to first msg with data or error; wait if necessary */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001917 do {
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001918 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1919 if (unlikely(rc))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001920 goto exit;
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001921 skb = skb_peek(&sk->sk_receive_queue);
1922 hdr = buf_msg(skb);
1923 dlen = msg_data_sz(hdr);
1924 hlen = msg_hdr_sz(hdr);
1925 err = msg_errcode(hdr);
Jon Maloyae236fb2017-10-13 11:04:25 +02001926 grp_evt = msg_is_grp_evt(hdr);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001927 if (likely(dlen || err))
1928 break;
1929 tsk_advance_rx_queue(sk);
1930 } while (1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001931
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001932 /* Collect msg meta data, including error code and rejected data */
Jon Maloy31c82a22017-10-13 11:04:24 +02001933 tipc_sk_set_orig_addr(m, skb);
Jon Maloy1c1274a2018-11-17 12:17:06 -05001934 rc = tipc_sk_anc_data_recv(m, skb, tsk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001935 if (unlikely(rc))
1936 goto exit;
Jon Maloy1c1274a2018-11-17 12:17:06 -05001937 hdr = buf_msg(skb);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001938
1939 /* Capture data if non-error msg, otherwise just set return value */
1940 if (likely(!err)) {
1941 copy = min_t(int, dlen, buflen);
1942 if (unlikely(copy != dlen))
1943 m->msg_flags |= MSG_TRUNC;
1944 rc = skb_copy_datagram_msg(skb, hlen, m, copy);
1945 } else {
1946 copy = 0;
1947 rc = 0;
1948 if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control)
1949 rc = -ECONNRESET;
1950 }
1951 if (unlikely(rc))
1952 goto exit;
1953
Jon Maloyae236fb2017-10-13 11:04:25 +02001954 /* Mark message as group event if applicable */
1955 if (unlikely(grp_evt)) {
1956 if (msg_grp_evt(hdr) == TIPC_WITHDRAWN)
1957 m->msg_flags |= MSG_EOR;
1958 m->msg_flags |= MSG_OOB;
1959 copy = 0;
1960 }
1961
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001962 /* Caption of data or error code/rejected data was successful */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001963 if (unlikely(flags & MSG_PEEK))
1964 goto exit;
1965
Jon Maloyb7d42632017-10-13 11:04:26 +02001966 /* Send group flow control advertisement when applicable */
1967 if (tsk->group && msg_in_group(hdr) && !grp_evt) {
Jon Maloye654f9f2019-08-15 16:42:50 +02001968 __skb_queue_head_init(&xmitq);
Jon Maloyb7d42632017-10-13 11:04:26 +02001969 tipc_group_update_rcv_win(tsk->group, tsk_blocks(hlen + dlen),
1970 msg_orignode(hdr), msg_origport(hdr),
1971 &xmitq);
1972 tipc_node_distr_xmit(sock_net(sk), &xmitq);
1973 }
1974
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001975 tsk_advance_rx_queue(sk);
Jon Maloyae236fb2017-10-13 11:04:25 +02001976
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001977 if (likely(!connected))
1978 goto exit;
1979
Jon Maloyb7d42632017-10-13 11:04:26 +02001980 /* Send connection flow control advertisement when applicable */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001981 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1982 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
1983 tipc_sk_send_ack(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001984exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001985 release_sock(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001986 return rc ? rc : copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001987}
1988
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001989/**
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001990 * tipc_recvstream - receive stream-oriented data
Randy Dunlapf172f4b2020-11-29 10:32:49 -08001991 * @sock: network socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001992 * @m: descriptor for message info
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001993 * @buflen: total size of user buffer area
Per Lidenb97bf3f2006-01-02 19:04:38 +01001994 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001995 *
1996 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001997 * will optionally wait for more; never truncates data.
1998 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08001999 * Return: size of returned message data, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01002000 */
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002001static int tipc_recvstream(struct socket *sock, struct msghdr *m,
2002 size_t buflen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002003{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002004 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002005 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002006 struct sk_buff *skb;
2007 struct tipc_msg *hdr;
2008 struct tipc_skb_cb *skb_cb;
2009 bool peek = flags & MSG_PEEK;
2010 int offset, required, copy, copied = 0;
2011 int hlen, dlen, err, rc;
2012 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002013
2014 /* Catch invalid receive attempts */
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002015 if (unlikely(!buflen))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002016 return -EINVAL;
2017
Allan Stephens0c3141e2008-04-15 00:22:02 -07002018 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002019
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01002020 if (unlikely(sk->sk_state == TIPC_OPEN)) {
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002021 rc = -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002022 goto exit;
2023 }
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002024 required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
2025 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002026
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002027 do {
2028 /* Look at first msg in receive queue; wait if necessary */
2029 rc = tipc_wait_for_rcvmsg(sock, &timeout);
2030 if (unlikely(rc))
2031 break;
2032 skb = skb_peek(&sk->sk_receive_queue);
2033 skb_cb = TIPC_SKB_CB(skb);
2034 hdr = buf_msg(skb);
2035 dlen = msg_data_sz(hdr);
2036 hlen = msg_hdr_sz(hdr);
2037 err = msg_errcode(hdr);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002038
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002039 /* Discard any empty non-errored (SYN-) message */
2040 if (unlikely(!dlen && !err)) {
2041 tsk_advance_rx_queue(sk);
2042 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002043 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002044
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002045 /* Collect msg meta data, incl. error code and rejected data */
2046 if (!copied) {
Jon Maloy31c82a22017-10-13 11:04:24 +02002047 tipc_sk_set_orig_addr(m, skb);
Jon Maloy1c1274a2018-11-17 12:17:06 -05002048 rc = tipc_sk_anc_data_recv(m, skb, tsk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002049 if (rc)
2050 break;
Jon Maloy1c1274a2018-11-17 12:17:06 -05002051 hdr = buf_msg(skb);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002052 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002053
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002054 /* Copy data if msg ok, otherwise return error/partial data */
2055 if (likely(!err)) {
2056 offset = skb_cb->bytes_read;
2057 copy = min_t(int, dlen - offset, buflen - copied);
2058 rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
2059 if (unlikely(rc))
2060 break;
2061 copied += copy;
2062 offset += copy;
2063 if (unlikely(offset < dlen)) {
2064 if (!peek)
2065 skb_cb->bytes_read = offset;
2066 break;
2067 }
2068 } else {
2069 rc = 0;
2070 if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control)
2071 rc = -ECONNRESET;
2072 if (copied || rc)
2073 break;
2074 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002075
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002076 if (unlikely(peek))
2077 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002078
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002079 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002080
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002081 /* Send connection flow control advertisement when applicable */
2082 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
Tuong Lienc7268582020-05-13 19:33:16 +07002083 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002084 tipc_sk_send_ack(tsk);
2085
2086 /* Exit if all requested data or FIN/error received */
2087 if (copied == buflen || err)
2088 break;
2089
2090 } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002091exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002092 release_sock(sk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002093 return copied ? copied : rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002094}
2095
2096/**
Ying Xuef288bef2012-08-21 11:16:57 +08002097 * tipc_write_space - wake up thread if port congestion is released
2098 * @sk: socket
2099 */
2100static void tipc_write_space(struct sock *sk)
2101{
2102 struct socket_wq *wq;
2103
2104 rcu_read_lock();
2105 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08002106 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002107 wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
2108 EPOLLWRNORM | EPOLLWRBAND);
Ying Xuef288bef2012-08-21 11:16:57 +08002109 rcu_read_unlock();
2110}
2111
2112/**
2113 * tipc_data_ready - wake up threads to indicate messages have been received
2114 * @sk: socket
Ying Xuef288bef2012-08-21 11:16:57 +08002115 */
David S. Miller676d2362014-04-11 16:15:36 -04002116static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08002117{
2118 struct socket_wq *wq;
2119
2120 rcu_read_lock();
2121 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08002122 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002123 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
2124 EPOLLRDNORM | EPOLLRDBAND);
Ying Xuef288bef2012-08-21 11:16:57 +08002125 rcu_read_unlock();
2126}
2127
Ying Xuef4195d12015-11-22 15:46:05 +08002128static void tipc_sock_destruct(struct sock *sk)
2129{
2130 __skb_queue_purge(&sk->sk_receive_queue);
2131}
2132
Jon Maloy64ac5f52017-10-13 11:04:20 +02002133static void tipc_sk_proto_rcv(struct sock *sk,
2134 struct sk_buff_head *inputq,
2135 struct sk_buff_head *xmitq)
2136{
2137 struct sk_buff *skb = __skb_dequeue(inputq);
2138 struct tipc_sock *tsk = tipc_sk(sk);
2139 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy75da2162017-10-13 11:04:23 +02002140 struct tipc_group *grp = tsk->group;
Jon Maloyb7d42632017-10-13 11:04:26 +02002141 bool wakeup = false;
Jon Maloy64ac5f52017-10-13 11:04:20 +02002142
2143 switch (msg_user(hdr)) {
2144 case CONN_MANAGER:
Parthasarathy Bhuvaragane7eb0582018-10-10 17:50:23 +02002145 tipc_sk_conn_proto_rcv(tsk, skb, inputq, xmitq);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002146 return;
2147 case SOCK_WAKEUP:
Jon Maloya80ae532017-10-13 11:04:22 +02002148 tipc_dest_del(&tsk->cong_links, msg_orignode(hdr), 0);
Tung Nguyenbfd07f32019-02-25 10:57:20 +07002149 /* coupled with smp_rmb() in tipc_wait_for_cond() */
2150 smp_wmb();
Jon Maloy64ac5f52017-10-13 11:04:20 +02002151 tsk->cong_link_cnt--;
Jon Maloyb7d42632017-10-13 11:04:26 +02002152 wakeup = true;
Tuong Lien0a3e0602020-05-26 16:38:38 +07002153 tipc_sk_push_backlog(tsk, false);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002154 break;
Jon Maloy75da2162017-10-13 11:04:23 +02002155 case GROUP_PROTOCOL:
Jon Maloyb7d42632017-10-13 11:04:26 +02002156 tipc_group_proto_rcv(grp, &wakeup, hdr, inputq, xmitq);
Jon Maloy75da2162017-10-13 11:04:23 +02002157 break;
Jon Maloy64ac5f52017-10-13 11:04:20 +02002158 case TOP_SRV:
Jon Maloyb7d42632017-10-13 11:04:26 +02002159 tipc_group_member_evt(tsk->group, &wakeup, &sk->sk_rcvbuf,
Jon Maloy7ad32bc2018-01-08 21:03:26 +01002160 hdr, inputq, xmitq);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002161 break;
2162 default:
2163 break;
2164 }
2165
Jon Maloyb7d42632017-10-13 11:04:26 +02002166 if (wakeup)
2167 sk->sk_write_space(sk);
2168
Jon Maloy64ac5f52017-10-13 11:04:20 +02002169 kfree_skb(skb);
2170}
2171
Ying Xuef288bef2012-08-21 11:16:57 +08002172/**
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002173 * tipc_sk_filter_connect - check incoming message for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002174 * @tsk: TIPC socket
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002175 * @skb: pointer to message buffer.
Tuong Lienc7268582020-05-13 19:33:16 +07002176 * @xmitq: for Nagle ACK if any
Randy Dunlap637b77f2020-11-29 10:32:48 -08002177 * Return: true if message should be added to receive queue, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05002178 */
Tuong Lienc7268582020-05-13 19:33:16 +07002179static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb,
2180 struct sk_buff_head *xmitq)
Ying Xue7e6c1312012-11-29 18:39:14 -05002181{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002182 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08002183 struct net *net = sock_net(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04002184 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002185 bool con_msg = msg_connected(hdr);
2186 u32 pport = tsk_peer_port(tsk);
2187 u32 pnode = tsk_peer_node(tsk);
2188 u32 oport = msg_origport(hdr);
2189 u32 onode = msg_orignode(hdr);
2190 int err = msg_errcode(hdr);
Tung Nguyen67879272018-09-28 20:23:22 +02002191 unsigned long delay;
Ying Xue7e6c1312012-11-29 18:39:14 -05002192
Jon Paul Maloycda36962015-07-22 10:11:20 -04002193 if (unlikely(msg_mcast(hdr)))
2194 return false;
Jon Maloyc0bceb92019-10-30 14:00:41 +01002195 tsk->oneway = 0;
Ying Xue7e6c1312012-11-29 18:39:14 -05002196
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002197 switch (sk->sk_state) {
2198 case TIPC_CONNECTING:
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002199 /* Setup ACK */
2200 if (likely(con_msg)) {
2201 if (err)
2202 break;
2203 tipc_sk_finish_conn(tsk, oport, onode);
2204 msg_set_importance(&tsk->phdr, msg_importance(hdr));
2205 /* ACK+ message with data is added to receive queue */
2206 if (msg_data_sz(hdr))
2207 return true;
2208 /* Empty ACK-, - wake up sleeping connect() and drop */
Parthasarathy Bhuvaraganff946832019-05-09 07:13:42 +02002209 sk->sk_state_change(sk);
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002210 msg_set_dest_droppable(hdr, 1);
2211 return false;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02002212 }
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002213 /* Ignore connectionless message if not from listening socket */
2214 if (oport != pport || onode != pnode)
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002215 return false;
2216
Tung Nguyen67879272018-09-28 20:23:22 +02002217 /* Rejected SYN */
2218 if (err != TIPC_ERR_OVERLOAD)
2219 break;
2220
2221 /* Prepare for new setup attempt if we have a SYN clone */
2222 if (skb_queue_empty(&sk->sk_write_queue))
2223 break;
2224 get_random_bytes(&delay, 2);
2225 delay %= (tsk->conn_timeout / 4);
2226 delay = msecs_to_jiffies(delay + 100);
2227 sk_reset_timer(sk, &sk->sk_timer, jiffies + delay);
2228 return false;
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002229 case TIPC_OPEN:
2230 case TIPC_DISCONNECTING:
2231 return false;
2232 case TIPC_LISTEN:
2233 /* Accept only SYN message */
Jon Maloy25b92212018-09-28 20:23:21 +02002234 if (!msg_is_syn(hdr) &&
2235 tipc_node_get_capabilities(net, onode) & TIPC_SYN_BIT)
2236 return false;
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002237 if (!con_msg && !err)
2238 return true;
2239 return false;
2240 case TIPC_ESTABLISHED:
Jon Maloyc0bceb92019-10-30 14:00:41 +01002241 if (!skb_queue_empty(&sk->sk_write_queue))
Tuong Lien0a3e0602020-05-26 16:38:38 +07002242 tipc_sk_push_backlog(tsk, false);
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002243 /* Accept only connection-based messages sent by peer */
Tuong Lienc7268582020-05-13 19:33:16 +07002244 if (likely(con_msg && !err && pport == oport &&
2245 pnode == onode)) {
2246 if (msg_ack_required(hdr)) {
2247 struct sk_buff *skb;
2248
2249 skb = tipc_sk_build_ack(tsk);
Tuong Lien0a3e0602020-05-26 16:38:38 +07002250 if (skb) {
2251 msg_set_nagle_ack(buf_msg(skb));
Tuong Lienc7268582020-05-13 19:33:16 +07002252 __skb_queue_tail(xmitq, skb);
Tuong Lien0a3e0602020-05-26 16:38:38 +07002253 }
Tuong Lienc7268582020-05-13 19:33:16 +07002254 }
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002255 return true;
Tuong Lienc7268582020-05-13 19:33:16 +07002256 }
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002257 if (!tsk_peer_msg(tsk, hdr))
2258 return false;
2259 if (!err)
2260 return true;
2261 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2262 tipc_node_remove_conn(net, pnode, tsk->portid);
2263 sk->sk_state_change(sk);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002264 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05002265 default:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01002266 pr_err("Unknown sk_state %u\n", sk->sk_state);
Ying Xue7e6c1312012-11-29 18:39:14 -05002267 }
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002268 /* Abort connection setup attempt */
2269 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2270 sk->sk_err = ECONNREFUSED;
2271 sk->sk_state_change(sk);
2272 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05002273}
2274
2275/**
Ying Xueaba79f32013-01-20 23:30:09 +01002276 * rcvbuf_limit - get proper overload limit of socket receive queue
2277 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002278 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01002279 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002280 * For connection oriented messages, irrespective of importance,
2281 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01002282 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002283 * For connectionless messages, queue limits are based on message
2284 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01002285 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002286 * TIPC_LOW_IMPORTANCE (2 MB)
2287 * TIPC_MEDIUM_IMPORTANCE (4 MB)
2288 * TIPC_HIGH_IMPORTANCE (8 MB)
2289 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01002290 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08002291 * Return: overload limit according to corresponding message importance
Ying Xueaba79f32013-01-20 23:30:09 +01002292 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002293static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01002294{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002295 struct tipc_sock *tsk = tipc_sk(sk);
2296 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01002297
Jon Maloyb7d42632017-10-13 11:04:26 +02002298 if (unlikely(msg_in_group(hdr)))
Eric Dumazet82657922019-10-09 15:21:13 -07002299 return READ_ONCE(sk->sk_rcvbuf);
Jon Maloyb7d42632017-10-13 11:04:26 +02002300
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002301 if (unlikely(!msg_connected(hdr)))
Eric Dumazet82657922019-10-09 15:21:13 -07002302 return READ_ONCE(sk->sk_rcvbuf) << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08002303
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002304 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
Eric Dumazet82657922019-10-09 15:21:13 -07002305 return READ_ONCE(sk->sk_rcvbuf);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002306
2307 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01002308}
2309
2310/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02002311 * tipc_sk_filter_rcv - validate incoming message
Allan Stephens0c3141e2008-04-15 00:22:02 -07002312 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04002313 * @skb: pointer to message.
Randy Dunlapf172f4b2020-11-29 10:32:49 -08002314 * @xmitq: output message area (FIXME)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002315 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07002316 * Enqueues message on receive queue if acceptable; optionally handles
2317 * disconnect indication for a connected socket.
2318 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05002319 * Called with socket lock already taken
Per Lidenb97bf3f2006-01-02 19:04:38 +01002320 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02002321static void tipc_sk_filter_rcv(struct sock *sk, struct sk_buff *skb,
2322 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002323{
Jon Maloy64ac5f52017-10-13 11:04:20 +02002324 bool sk_conn = !tipc_sk_type_connectionless(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002325 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy75da2162017-10-13 11:04:23 +02002326 struct tipc_group *grp = tsk->group;
Jon Paul Maloycda36962015-07-22 10:11:20 -04002327 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002328 struct net *net = sock_net(sk);
2329 struct sk_buff_head inputq;
Hoang Le77d5ad42019-03-21 17:25:17 +07002330 int mtyp = msg_type(hdr);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002331 int limit, err = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002332
Tuong Lien01e661e2018-12-19 09:17:58 +07002333 trace_tipc_sk_filter_rcv(sk, skb, TIPC_DUMP_ALL, " ");
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01002334 TIPC_SKB_CB(skb)->bytes_read = 0;
Jon Maloy64ac5f52017-10-13 11:04:20 +02002335 __skb_queue_head_init(&inputq);
2336 __skb_queue_tail(&inputq, skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002337
Jon Maloy64ac5f52017-10-13 11:04:20 +02002338 if (unlikely(!msg_isdata(hdr)))
2339 tipc_sk_proto_rcv(sk, &inputq, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04002340
Jon Maloy75da2162017-10-13 11:04:23 +02002341 if (unlikely(grp))
2342 tipc_group_filter_msg(grp, &inputq, xmitq);
2343
Hoang Le77d5ad42019-03-21 17:25:17 +07002344 if (unlikely(!grp) && mtyp == TIPC_MCAST_MSG)
Hoang Le08e046c2019-03-21 17:25:18 +07002345 tipc_mcast_filter_msg(net, &tsk->mc_method.deferredq, &inputq);
Hoang Lec55c8ed2019-03-19 18:49:50 +07002346
Jon Maloy64ac5f52017-10-13 11:04:20 +02002347 /* Validate and add to receive buffer if there is space */
2348 while ((skb = __skb_dequeue(&inputq))) {
2349 hdr = buf_msg(skb);
2350 limit = rcvbuf_limit(sk, skb);
Tuong Lienc7268582020-05-13 19:33:16 +07002351 if ((sk_conn && !tipc_sk_filter_connect(tsk, skb, xmitq)) ||
Jon Maloy75da2162017-10-13 11:04:23 +02002352 (!sk_conn && msg_connected(hdr)) ||
2353 (!grp && msg_in_group(hdr)))
Jon Maloy64ac5f52017-10-13 11:04:20 +02002354 err = TIPC_ERR_NO_PORT;
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01002355 else if (sk_rmem_alloc_get(sk) + skb->truesize >= limit) {
Tuong Lien01e661e2018-12-19 09:17:58 +07002356 trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL,
2357 "err_overload2!");
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01002358 atomic_inc(&sk->sk_drops);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002359 err = TIPC_ERR_OVERLOAD;
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01002360 }
Jon Maloy64ac5f52017-10-13 11:04:20 +02002361
2362 if (unlikely(err)) {
Tuong Lien01e661e2018-12-19 09:17:58 +07002363 if (tipc_msg_reverse(tipc_own_addr(net), &skb, err)) {
2364 trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE,
2365 "@filter_rcv!");
2366 __skb_queue_tail(xmitq, skb);
2367 }
Jon Maloy64ac5f52017-10-13 11:04:20 +02002368 err = TIPC_OK;
2369 continue;
2370 }
2371 __skb_queue_tail(&sk->sk_receive_queue, skb);
2372 skb_set_owner_r(skb, sk);
Tuong Lien01e661e2018-12-19 09:17:58 +07002373 trace_tipc_sk_overlimit2(sk, skb, TIPC_DUMP_ALL,
2374 "rcvq >90% allocated!");
Jon Maloy64ac5f52017-10-13 11:04:20 +02002375 sk->sk_data_ready(sk);
2376 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002377}
2378
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002379/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02002380 * tipc_sk_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07002381 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08002382 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07002383 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05002384 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07002385 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02002386static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07002387{
Jon Maloy64ac5f52017-10-13 11:04:20 +02002388 unsigned int before = sk_rmem_alloc_get(sk);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002389 struct sk_buff_head xmitq;
Jon Maloy64ac5f52017-10-13 11:04:20 +02002390 unsigned int added;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002391
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002392 __skb_queue_head_init(&xmitq);
2393
Jon Maloy64ac5f52017-10-13 11:04:20 +02002394 tipc_sk_filter_rcv(sk, skb, &xmitq);
2395 added = sk_rmem_alloc_get(sk) - before;
2396 atomic_add(added, &tipc_sk(sk)->dupl_rcvcnt);
2397
2398 /* Send pending response/rejected messages, if any */
Jon Maloyf70d37b2017-10-13 11:04:21 +02002399 tipc_node_distr_xmit(sock_net(sk), &xmitq);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002400 return 0;
2401}
2402
2403/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002404 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
2405 * inputq and try adding them to socket or backlog queue
2406 * @inputq: list of incoming buffers with potentially different destinations
2407 * @sk: socket where the buffers should be enqueued
2408 * @dport: port number for the socket
Randy Dunlapf172f4b2020-11-29 10:32:49 -08002409 * @xmitq: output queue
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002410 *
2411 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002412 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04002413static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002414 u32 dport, struct sk_buff_head *xmitq)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002415{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002416 unsigned long time_limit = jiffies + 2;
2417 struct sk_buff *skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002418 unsigned int lim;
2419 atomic_t *dcnt;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002420 u32 onode;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002421
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002422 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05002423 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04002424 return;
2425
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002426 skb = tipc_skb_dequeue(inputq, dport);
2427 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04002428 return;
2429
2430 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002431 if (!sock_owned_by_user(sk)) {
Jon Maloy64ac5f52017-10-13 11:04:20 +02002432 tipc_sk_filter_rcv(sk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04002433 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002434 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04002435
2436 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002437 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04002438 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002439 atomic_set(dcnt, 0);
2440 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
Tuong Lien01e661e2018-12-19 09:17:58 +07002441 if (likely(!sk_add_backlog(sk, skb, lim))) {
2442 trace_tipc_sk_overlimit1(sk, skb, TIPC_DUMP_ALL,
2443 "bklg & rcvq >90% allocated!");
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002444 continue;
Tuong Lien01e661e2018-12-19 09:17:58 +07002445 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04002446
Tuong Lien01e661e2018-12-19 09:17:58 +07002447 trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL, "err_overload!");
Jon Paul Maloycda36962015-07-22 10:11:20 -04002448 /* Overload => reject message back to sender */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002449 onode = tipc_own_addr(sock_net(sk));
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01002450 atomic_inc(&sk->sk_drops);
Tuong Lien01e661e2018-12-19 09:17:58 +07002451 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD)) {
2452 trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_ALL,
2453 "@sk_enqueue!");
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002454 __skb_queue_tail(xmitq, skb);
Tuong Lien01e661e2018-12-19 09:17:58 +07002455 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04002456 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002457 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002458}
2459
2460/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002461 * tipc_sk_rcv - handle a chain of incoming buffers
Randy Dunlapf172f4b2020-11-29 10:32:49 -08002462 * @net: the associated network namespace
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002463 * @inputq: buffer list containing the buffers
2464 * Consumes all buffers in list until inputq is empty
2465 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07002466 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04002467void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07002468{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002469 struct sk_buff_head xmitq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002470 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04002471 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04002472 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04002473 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04002474 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04002475
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002476 __skb_queue_head_init(&xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002477 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002478 dport = tipc_skb_peek_port(inputq, dport);
2479 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04002480
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002481 if (likely(tsk)) {
2482 sk = &tsk->sk;
2483 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002484 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002485 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002486 }
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002487 /* Send pending response/rejected messages, if any */
Jon Maloyf70d37b2017-10-13 11:04:21 +02002488 tipc_node_distr_xmit(sock_net(sk), &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002489 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002490 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002491 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04002492 /* No destination socket => dequeue skb if still there */
2493 skb = tipc_skb_dequeue(inputq, dport);
2494 if (!skb)
2495 return;
2496
2497 /* Try secondary lookup if unresolved named message */
2498 err = TIPC_ERR_NO_PORT;
2499 if (tipc_msg_lookup_dest(net, skb, &err))
2500 goto xmit;
2501
2502 /* Prepare for message rejection */
2503 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002504 continue;
Tuong Lien01e661e2018-12-19 09:17:58 +07002505
2506 trace_tipc_sk_rej_msg(NULL, skb, TIPC_DUMP_NONE, "@sk_rcv!");
Jon Paul Maloye3a77562015-02-05 08:36:39 -05002507xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04002508 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002509 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002510 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07002511}
2512
Ying Xue78eb3a52014-01-17 09:50:03 +08002513static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
2514{
WANG Congd9dc8b02016-11-11 10:20:50 -08002515 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Ying Xue78eb3a52014-01-17 09:50:03 +08002516 struct sock *sk = sock->sk;
Ying Xue78eb3a52014-01-17 09:50:03 +08002517 int done;
2518
2519 do {
2520 int err = sock_error(sk);
2521 if (err)
2522 return err;
2523 if (!*timeo_p)
2524 return -ETIMEDOUT;
2525 if (signal_pending(current))
2526 return sock_intr_errno(*timeo_p);
Tuong Lien5391a872020-02-10 15:35:44 +07002527 if (sk->sk_state == TIPC_DISCONNECTING)
2528 break;
Ying Xue78eb3a52014-01-17 09:50:03 +08002529
WANG Congd9dc8b02016-11-11 10:20:50 -08002530 add_wait_queue(sk_sleep(sk), &wait);
Tuong Lien9546a0b2020-01-08 09:19:00 +07002531 done = sk_wait_event(sk, timeo_p, tipc_sk_connected(sk),
2532 &wait);
WANG Congd9dc8b02016-11-11 10:20:50 -08002533 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue78eb3a52014-01-17 09:50:03 +08002534 } while (!done);
2535 return 0;
2536}
2537
Erik Hugneea239312019-03-17 18:46:42 +01002538static bool tipc_sockaddr_is_sane(struct sockaddr_tipc *addr)
2539{
2540 if (addr->family != AF_TIPC)
2541 return false;
2542 if (addr->addrtype == TIPC_SERVICE_RANGE)
2543 return (addr->addr.nameseq.lower <= addr->addr.nameseq.upper);
2544 return (addr->addrtype == TIPC_SERVICE_ADDR ||
2545 addr->addrtype == TIPC_SOCKET_ADDR);
2546}
2547
Per Lidenb97bf3f2006-01-02 19:04:38 +01002548/**
Ying Xue247f0f32014-02-18 16:06:46 +08002549 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01002550 * @sock: socket structure
2551 * @dest: socket address for destination port
2552 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07002553 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01002554 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08002555 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01002556 */
Ying Xue247f0f32014-02-18 16:06:46 +08002557static int tipc_connect(struct socket *sock, struct sockaddr *dest,
2558 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002559{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002560 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01002561 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07002562 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
2563 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01002564 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002565 int previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01002566 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002567
Jon Maloy23998832017-10-13 11:04:18 +02002568 if (destlen != sizeof(struct sockaddr_tipc))
2569 return -EINVAL;
2570
Allan Stephens0c3141e2008-04-15 00:22:02 -07002571 lock_sock(sk);
2572
Jon Maloy75da2162017-10-13 11:04:23 +02002573 if (tsk->group) {
2574 res = -EINVAL;
2575 goto exit;
2576 }
2577
Jon Maloy23998832017-10-13 11:04:18 +02002578 if (dst->family == AF_UNSPEC) {
2579 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
2580 if (!tipc_sk_type_connectionless(sk))
2581 res = -EINVAL;
2582 goto exit;
Jon Maloy23998832017-10-13 11:04:18 +02002583 }
Erik Hugneea239312019-03-17 18:46:42 +01002584 if (!tipc_sockaddr_is_sane(dst)) {
Jon Maloy23998832017-10-13 11:04:18 +02002585 res = -EINVAL;
Jon Maloy23998832017-10-13 11:04:18 +02002586 goto exit;
Erik Hugneea239312019-03-17 18:46:42 +01002587 }
Erik Hugnef2f80362015-03-19 09:02:19 +01002588 /* DGRAM/RDM connect(), just save the destaddr */
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +01002589 if (tipc_sk_type_connectionless(sk)) {
Jon Maloy23998832017-10-13 11:04:18 +02002590 memcpy(&tsk->peer, dest, destlen);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002591 goto exit;
Erik Hugneea239312019-03-17 18:46:42 +01002592 } else if (dst->addrtype == TIPC_SERVICE_RANGE) {
2593 res = -EINVAL;
2594 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002595 }
2596
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002597 previous = sk->sk_state;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01002598
2599 switch (sk->sk_state) {
2600 case TIPC_OPEN:
Ying Xue584d24b2012-11-29 18:51:19 -05002601 /* Send a 'SYN-' to destination */
2602 m.msg_name = dest;
2603 m.msg_namelen = destlen;
2604
2605 /* If connect is in non-blocking case, set MSG_DONTWAIT to
2606 * indicate send_msg() is never blocked.
2607 */
2608 if (!timeout)
2609 m.msg_flags = MSG_DONTWAIT;
2610
Ying Xue39a0295f2015-03-02 15:37:47 +08002611 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05002612 if ((res < 0) && (res != -EWOULDBLOCK))
2613 goto exit;
2614
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002615 /* Just entered TIPC_CONNECTING state; the only
Ying Xue584d24b2012-11-29 18:51:19 -05002616 * difference is that return value in non-blocking
2617 * case is EINPROGRESS, rather than EALREADY.
2618 */
2619 res = -EINPROGRESS;
Miaohe Lin7f8901b2020-08-18 08:07:13 -04002620 fallthrough;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002621 case TIPC_CONNECTING:
2622 if (!timeout) {
2623 if (previous == TIPC_CONNECTING)
2624 res = -EALREADY;
Ying Xue78eb3a52014-01-17 09:50:03 +08002625 goto exit;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002626 }
Ying Xue78eb3a52014-01-17 09:50:03 +08002627 timeout = msecs_to_jiffies(timeout);
2628 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
2629 res = tipc_wait_for_connect(sock, &timeout);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002630 break;
2631 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002632 res = -EISCONN;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002633 break;
2634 default:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002635 res = -EINVAL;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002636 }
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002637
Allan Stephens0c3141e2008-04-15 00:22:02 -07002638exit:
2639 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07002640 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002641}
2642
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002643/**
Ying Xue247f0f32014-02-18 16:06:46 +08002644 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01002645 * @sock: socket structure
2646 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002647 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08002648 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01002649 */
Ying Xue247f0f32014-02-18 16:06:46 +08002650static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002651{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002652 struct sock *sk = sock->sk;
2653 int res;
2654
2655 lock_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002656 res = tipc_set_sk_state(sk, TIPC_LISTEN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002657 release_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002658
Allan Stephens0c3141e2008-04-15 00:22:02 -07002659 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002660}
2661
Ying Xue6398e232014-01-17 09:50:04 +08002662static int tipc_wait_for_accept(struct socket *sock, long timeo)
2663{
2664 struct sock *sk = sock->sk;
2665 DEFINE_WAIT(wait);
2666 int err;
2667
2668 /* True wake-one mechanism for incoming connections: only
2669 * one process gets woken up, not the 'whole herd'.
2670 * Since we do not 'race & poll' for established sockets
2671 * anymore, the common case will execute the loop only once.
2672 */
2673 for (;;) {
2674 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2675 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01002676 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08002677 release_sock(sk);
2678 timeo = schedule_timeout(timeo);
2679 lock_sock(sk);
2680 }
2681 err = 0;
2682 if (!skb_queue_empty(&sk->sk_receive_queue))
2683 break;
Ying Xue6398e232014-01-17 09:50:04 +08002684 err = -EAGAIN;
2685 if (!timeo)
2686 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002687 err = sock_intr_errno(timeo);
2688 if (signal_pending(current))
2689 break;
Ying Xue6398e232014-01-17 09:50:04 +08002690 }
2691 finish_wait(sk_sleep(sk), &wait);
2692 return err;
2693}
2694
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002695/**
Ying Xue247f0f32014-02-18 16:06:46 +08002696 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002697 * @sock: listening socket
Andrew Lunnd8141202020-07-13 01:15:14 +02002698 * @new_sock: new socket that is to be connected
Per Lidenb97bf3f2006-01-02 19:04:38 +01002699 * @flags: file-related flags associated with socket
Randy Dunlapf172f4b2020-11-29 10:32:49 -08002700 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002701 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08002702 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01002703 */
David Howellscdfbabf2017-03-09 08:09:05 +00002704static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
2705 bool kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002706{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002707 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002708 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002709 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002710 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002711 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002712 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002713
Allan Stephens0c3141e2008-04-15 00:22:02 -07002714 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002715
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002716 if (sk->sk_state != TIPC_LISTEN) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002717 res = -EINVAL;
2718 goto exit;
2719 }
Ying Xue6398e232014-01-17 09:50:04 +08002720 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2721 res = tipc_wait_for_accept(sock, timeo);
2722 if (res)
2723 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002724
2725 buf = skb_peek(&sk->sk_receive_queue);
2726
David Howellscdfbabf2017-03-09 08:09:05 +00002727 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002728 if (res)
2729 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002730 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002731
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002732 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002733 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002734 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002735
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002736 /* we lock on new_sk; but lockdep sees the lock on sk */
2737 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002738
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002739 /*
2740 * Reject any stray messages received by new socket
2741 * before the socket lock was taken (very, very unlikely)
2742 */
Tuong Lien49afb802020-01-08 09:18:15 +07002743 tsk_rej_rx_queue(new_sk, TIPC_ERR_NO_PORT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002744
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002745 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002746 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002747
Christoph Hellwig095ae612020-05-28 07:12:36 +02002748 tsk_set_importance(new_sk, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002749 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002750 new_tsock->conn_type = msg_nametype(msg);
2751 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002752 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002753
2754 /*
2755 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2756 * Respond to 'SYN+' by queuing it on new socket.
2757 */
2758 if (!msg_data_sz(msg)) {
2759 struct msghdr m = {NULL,};
2760
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002761 tsk_advance_rx_queue(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002762 __tipc_sendstream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002763 } else {
2764 __skb_dequeue(&sk->sk_receive_queue);
2765 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002766 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002767 }
2768 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002769exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002770 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002771 return res;
2772}
2773
2774/**
Ying Xue247f0f32014-02-18 16:06:46 +08002775 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002776 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002777 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002778 *
2779 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002780 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08002781 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01002782 */
Ying Xue247f0f32014-02-18 16:06:46 +08002783static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002784{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002785 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002786 int res;
2787
Allan Stephense247a8f2008-03-06 15:05:38 -08002788 if (how != SHUT_RDWR)
2789 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002790
Allan Stephens0c3141e2008-04-15 00:22:02 -07002791 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002792
Tuong Lien01e661e2018-12-19 09:17:58 +07002793 trace_tipc_sk_shutdown(sk, NULL, TIPC_DUMP_ALL, " ");
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002794 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
Tetsuo Handaa4b5cc92020-09-05 15:14:47 +09002795 sk->sk_shutdown = SHUTDOWN_MASK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002796
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002797 if (sk->sk_state == TIPC_DISCONNECTING) {
Ying Xue75031152012-10-29 09:38:15 -04002798 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002799 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002800
Per Lidenb97bf3f2006-01-02 19:04:38 +01002801 res = 0;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002802 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002803 res = -ENOTCONN;
2804 }
Tetsuo Handa2a638662020-09-02 22:44:16 +09002805 /* Wake up anyone sleeping in poll. */
2806 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002807
Allan Stephens0c3141e2008-04-15 00:22:02 -07002808 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002809 return res;
2810}
2811
Jon Maloyafe87922018-09-28 20:23:19 +02002812static void tipc_sk_check_probing_state(struct sock *sk,
2813 struct sk_buff_head *list)
2814{
2815 struct tipc_sock *tsk = tipc_sk(sk);
2816 u32 pnode = tsk_peer_node(tsk);
2817 u32 pport = tsk_peer_port(tsk);
2818 u32 self = tsk_own_node(tsk);
2819 u32 oport = tsk->portid;
2820 struct sk_buff *skb;
2821
2822 if (tsk->probe_unacked) {
2823 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2824 sk->sk_err = ECONNABORTED;
2825 tipc_node_remove_conn(sock_net(sk), pnode, pport);
2826 sk->sk_state_change(sk);
2827 return;
2828 }
2829 /* Prepare new probe */
2830 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, 0,
2831 pnode, self, pport, oport, TIPC_OK);
2832 if (skb)
2833 __skb_queue_tail(list, skb);
2834 tsk->probe_unacked = true;
2835 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
2836}
2837
Tung Nguyen67879272018-09-28 20:23:22 +02002838static void tipc_sk_retry_connect(struct sock *sk, struct sk_buff_head *list)
2839{
2840 struct tipc_sock *tsk = tipc_sk(sk);
2841
2842 /* Try again later if dest link is congested */
2843 if (tsk->cong_link_cnt) {
2844 sk_reset_timer(sk, &sk->sk_timer, msecs_to_jiffies(100));
2845 return;
2846 }
2847 /* Prepare SYN for retransmit */
2848 tipc_msg_skb_clone(&sk->sk_write_queue, list);
2849}
2850
Kees Cook31b102b2017-10-30 14:06:45 -07002851static void tipc_sk_timeout(struct timer_list *t)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002852{
Kees Cook31b102b2017-10-30 14:06:45 -07002853 struct sock *sk = from_timer(sk, t, sk_timer);
2854 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloyafe87922018-09-28 20:23:19 +02002855 u32 pnode = tsk_peer_node(tsk);
2856 struct sk_buff_head list;
Tung Nguyen67879272018-09-28 20:23:22 +02002857 int rc = 0;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002858
Jon Maloye654f9f2019-08-15 16:42:50 +02002859 __skb_queue_head_init(&list);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002860 bh_lock_sock(sk);
Jon Maloy0d5fcebf2017-10-20 11:21:32 +02002861
2862 /* Try again later if socket is busy */
2863 if (sock_owned_by_user(sk)) {
2864 sk_reset_timer(sk, &sk->sk_timer, jiffies + HZ / 20);
Jon Maloyafe87922018-09-28 20:23:19 +02002865 bh_unlock_sock(sk);
Tung Nguyen91a4a3e2019-11-28 10:10:06 +07002866 sock_put(sk);
Jon Maloyafe87922018-09-28 20:23:19 +02002867 return;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002868 }
Jon Paul Maloy57289012014-08-22 18:09:09 -04002869
Jon Maloyafe87922018-09-28 20:23:19 +02002870 if (sk->sk_state == TIPC_ESTABLISHED)
2871 tipc_sk_check_probing_state(sk, &list);
Tung Nguyen67879272018-09-28 20:23:22 +02002872 else if (sk->sk_state == TIPC_CONNECTING)
2873 tipc_sk_retry_connect(sk, &list);
Jon Maloyafe87922018-09-28 20:23:19 +02002874
Jon Paul Maloy57289012014-08-22 18:09:09 -04002875 bh_unlock_sock(sk);
Jon Maloyafe87922018-09-28 20:23:19 +02002876
2877 if (!skb_queue_empty(&list))
Tung Nguyen67879272018-09-28 20:23:22 +02002878 rc = tipc_node_xmit(sock_net(sk), &list, pnode, tsk->portid);
Jon Maloyafe87922018-09-28 20:23:19 +02002879
Tung Nguyen67879272018-09-28 20:23:22 +02002880 /* SYN messages may cause link congestion */
2881 if (rc == -ELINKCONG) {
2882 tipc_dest_push(&tsk->cong_links, pnode, 0);
2883 tsk->cong_link_cnt = 1;
2884 }
Ying Xue07f6c4b2015-01-07 13:41:58 +08002885 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002886}
2887
Jon Maloy50a34992021-03-16 22:06:11 -04002888static int tipc_sk_publish(struct tipc_sock *tsk, struct tipc_uaddr *ua)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002889{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002890 struct sock *sk = &tsk->sk;
2891 struct net *net = sock_net(sk);
Jon Maloy50a34992021-03-16 22:06:11 -04002892 struct tipc_socket_addr skaddr;
2893 struct publication *p;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002894 u32 key;
2895
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002896 if (tipc_sk_connected(sk))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002897 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002898 key = tsk->portid + tsk->pub_count + 1;
2899 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002900 return -EADDRINUSE;
Jon Maloy50a34992021-03-16 22:06:11 -04002901 skaddr.ref = tsk->portid;
2902 skaddr.node = tipc_own_addr(net);
2903 p = tipc_nametbl_publish(net, ua, &skaddr, key);
2904 if (unlikely(!p))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002905 return -EINVAL;
2906
Jon Maloy50a34992021-03-16 22:06:11 -04002907 list_add(&p->binding_sock, &tsk->publications);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002908 tsk->pub_count++;
Jon Maloy50a34992021-03-16 22:06:11 -04002909 tsk->published = true;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002910 return 0;
2911}
2912
Jon Maloy2c98da02021-03-16 22:06:13 -04002913static int tipc_sk_withdraw(struct tipc_sock *tsk, struct tipc_uaddr *ua)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002914{
Ying Xuef2f98002015-01-09 15:27:05 +08002915 struct net *net = sock_net(&tsk->sk);
Jon Maloy2c98da02021-03-16 22:06:13 -04002916 struct publication *safe, *p;
2917 struct tipc_uaddr _ua;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002918 int rc = -EINVAL;
2919
Jon Maloy998d3902021-03-16 22:06:08 -04002920 list_for_each_entry_safe(p, safe, &tsk->publications, binding_sock) {
Jon Maloy2c98da02021-03-16 22:06:13 -04002921 if (!ua) {
2922 tipc_uaddr(&_ua, TIPC_SERVICE_RANGE, p->scope,
2923 p->sr.type, p->sr.lower, p->sr.upper);
2924 tipc_nametbl_withdraw(net, &_ua, &p->sk, p->key);
2925 continue;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002926 }
Jon Maloy2c98da02021-03-16 22:06:13 -04002927 /* Unbind specific publication */
2928 if (p->scope != ua->scope)
2929 continue;
2930 if (p->sr.type != ua->sr.type)
2931 continue;
2932 if (p->sr.lower != ua->sr.lower)
2933 continue;
2934 if (p->sr.upper != ua->sr.upper)
2935 break;
2936 tipc_nametbl_withdraw(net, ua, &p->sk, p->key);
2937 rc = 0;
2938 break;
2939 }
2940 if (list_empty(&tsk->publications)) {
2941 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002942 rc = 0;
2943 }
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002944 return rc;
2945}
2946
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002947/* tipc_sk_reinit: set non-zero address in all existing sockets
2948 * when we go from standalone to network mode.
2949 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002950void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002951{
Ying Xuee05b31f2015-01-09 15:27:08 +08002952 struct tipc_net *tn = net_generic(net, tipc_net_id);
Herbert Xu40f9f432017-02-11 19:26:46 +08002953 struct rhashtable_iter iter;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002954 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002955 struct tipc_msg *msg;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002956
Herbert Xu40f9f432017-02-11 19:26:46 +08002957 rhashtable_walk_enter(&tn->sk_rht, &iter);
2958
2959 do {
Tom Herbert97a6ec42017-12-04 10:31:41 -08002960 rhashtable_walk_start(&iter);
Herbert Xu40f9f432017-02-11 19:26:46 +08002961
2962 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
Cong Wang15ef70e2018-12-10 11:49:55 -08002963 sock_hold(&tsk->sk);
2964 rhashtable_walk_stop(&iter);
2965 lock_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002966 msg = &tsk->phdr;
Jon Maloy23fd3ea2018-03-22 20:42:49 +01002967 msg_set_prevnode(msg, tipc_own_addr(net));
2968 msg_set_orignode(msg, tipc_own_addr(net));
Cong Wang15ef70e2018-12-10 11:49:55 -08002969 release_sock(&tsk->sk);
2970 rhashtable_walk_start(&iter);
2971 sock_put(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002972 }
Tom Herbert97a6ec42017-12-04 10:31:41 -08002973
Herbert Xu40f9f432017-02-11 19:26:46 +08002974 rhashtable_walk_stop(&iter);
2975 } while (tsk == ERR_PTR(-EAGAIN));
Cong Wangbd583fe2018-08-23 16:19:44 -07002976
2977 rhashtable_walk_exit(&iter);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002978}
2979
Ying Xuee05b31f2015-01-09 15:27:08 +08002980static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002981{
Ying Xuee05b31f2015-01-09 15:27:08 +08002982 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002983 struct tipc_sock *tsk;
2984
2985 rcu_read_lock();
Taehee Yooab818362019-11-22 08:15:19 +00002986 tsk = rhashtable_lookup(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002987 if (tsk)
2988 sock_hold(&tsk->sk);
2989 rcu_read_unlock();
2990
2991 return tsk;
2992}
2993
2994static int tipc_sk_insert(struct tipc_sock *tsk)
2995{
Ying Xuee05b31f2015-01-09 15:27:08 +08002996 struct sock *sk = &tsk->sk;
2997 struct net *net = sock_net(sk);
2998 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002999 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
3000 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
3001
3002 while (remaining--) {
3003 portid++;
3004 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
3005 portid = TIPC_MIN_PORT;
3006 tsk->portid = portid;
3007 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11003008 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
3009 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08003010 return 0;
3011 sock_put(&tsk->sk);
3012 }
3013
3014 return -1;
3015}
3016
3017static void tipc_sk_remove(struct tipc_sock *tsk)
3018{
3019 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08003020 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08003021
Herbert Xu6cca72892015-03-20 21:57:05 +11003022 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003023 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
Ying Xue07f6c4b2015-01-07 13:41:58 +08003024 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04003025 }
3026}
3027
Herbert Xu6cca72892015-03-20 21:57:05 +11003028static const struct rhashtable_params tsk_rht_params = {
3029 .nelem_hint = 192,
3030 .head_offset = offsetof(struct tipc_sock, node),
3031 .key_offset = offsetof(struct tipc_sock, portid),
3032 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11003033 .max_size = 1048576,
3034 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00003035 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11003036};
3037
Ying Xuee05b31f2015-01-09 15:27:08 +08003038int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003039{
Ying Xuee05b31f2015-01-09 15:27:08 +08003040 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003041
Herbert Xu6cca72892015-03-20 21:57:05 +11003042 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003043}
3044
Ying Xuee05b31f2015-01-09 15:27:08 +08003045void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003046{
Ying Xuee05b31f2015-01-09 15:27:08 +08003047 struct tipc_net *tn = net_generic(net, tipc_net_id);
3048
Ying Xue07f6c4b2015-01-07 13:41:58 +08003049 /* Wait for socket readers to complete */
3050 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003051
Ying Xuee05b31f2015-01-09 15:27:08 +08003052 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003053}
3054
Jon Maloy75da2162017-10-13 11:04:23 +02003055static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq)
3056{
3057 struct net *net = sock_net(&tsk->sk);
Jon Maloy75da2162017-10-13 11:04:23 +02003058 struct tipc_group *grp = tsk->group;
3059 struct tipc_msg *hdr = &tsk->phdr;
Jon Maloy50a34992021-03-16 22:06:11 -04003060 struct tipc_uaddr ua;
Jon Maloy75da2162017-10-13 11:04:23 +02003061 int rc;
3062
3063 if (mreq->type < TIPC_RESERVED_TYPES)
3064 return -EACCES;
Jon Maloy232d07b2018-01-08 21:03:30 +01003065 if (mreq->scope > TIPC_NODE_SCOPE)
3066 return -EINVAL;
Jon Maloy50a34992021-03-16 22:06:11 -04003067 if (mreq->scope != TIPC_NODE_SCOPE)
3068 mreq->scope = TIPC_CLUSTER_SCOPE;
Jon Maloy75da2162017-10-13 11:04:23 +02003069 if (grp)
3070 return -EACCES;
Jon Maloy60c25302018-01-17 16:42:46 +01003071 grp = tipc_group_create(net, tsk->portid, mreq, &tsk->group_is_open);
Jon Maloy75da2162017-10-13 11:04:23 +02003072 if (!grp)
3073 return -ENOMEM;
3074 tsk->group = grp;
3075 msg_set_lookup_scope(hdr, mreq->scope);
3076 msg_set_nametype(hdr, mreq->type);
3077 msg_set_dest_droppable(hdr, true);
Jon Maloy50a34992021-03-16 22:06:11 -04003078 tipc_uaddr(&ua, TIPC_SERVICE_RANGE, mreq->scope,
3079 mreq->type, mreq->instance, mreq->instance);
Jon Maloy6e448672021-03-16 22:06:20 -04003080 tipc_nametbl_build_group(net, grp, &ua);
Jon Maloy50a34992021-03-16 22:06:11 -04003081 rc = tipc_sk_publish(tsk, &ua);
Cong Wange233df02017-10-24 15:44:49 -07003082 if (rc) {
Jon Maloy75da2162017-10-13 11:04:23 +02003083 tipc_group_delete(net, grp);
Cong Wange233df02017-10-24 15:44:49 -07003084 tsk->group = NULL;
Jon Maloyfebafc82018-01-10 21:08:50 +01003085 return rc;
Cong Wange233df02017-10-24 15:44:49 -07003086 }
Jon Maloyd12d2e12018-01-08 21:03:28 +01003087 /* Eliminate any risk that a broadcast overtakes sent JOINs */
Jon Maloy399574d2017-10-13 11:04:32 +02003088 tsk->mc_method.rcast = true;
3089 tsk->mc_method.mandatory = true;
Jon Maloyd12d2e12018-01-08 21:03:28 +01003090 tipc_group_join(net, grp, &tsk->sk.sk_rcvbuf);
Jon Maloy75da2162017-10-13 11:04:23 +02003091 return rc;
3092}
3093
3094static int tipc_sk_leave(struct tipc_sock *tsk)
3095{
3096 struct net *net = sock_net(&tsk->sk);
3097 struct tipc_group *grp = tsk->group;
Jon Maloy2c98da02021-03-16 22:06:13 -04003098 struct tipc_uaddr ua;
Jon Maloy75da2162017-10-13 11:04:23 +02003099 int scope;
3100
3101 if (!grp)
3102 return -EINVAL;
Jon Maloy2c98da02021-03-16 22:06:13 -04003103 ua.addrtype = TIPC_SERVICE_RANGE;
3104 tipc_group_self(grp, &ua.sr, &scope);
3105 ua.scope = scope;
Jon Maloy75da2162017-10-13 11:04:23 +02003106 tipc_group_delete(net, grp);
3107 tsk->group = NULL;
Jon Maloy2c98da02021-03-16 22:06:13 -04003108 tipc_sk_withdraw(tsk, &ua);
Jon Maloy75da2162017-10-13 11:04:23 +02003109 return 0;
3110}
3111
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003112/**
Ying Xue247f0f32014-02-18 16:06:46 +08003113 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01003114 * @sock: socket structure
3115 * @lvl: option level
3116 * @opt: option identifier
3117 * @ov: pointer to new option value
3118 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003119 *
3120 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01003121 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003122 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08003123 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01003124 */
Ying Xue247f0f32014-02-18 16:06:46 +08003125static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003126 sockptr_t ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003127{
Allan Stephens0c3141e2008-04-15 00:22:02 -07003128 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04003129 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy75da2162017-10-13 11:04:23 +02003130 struct tipc_group_req mreq;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003131 u32 value = 0;
Dan Carpentera08ef472017-01-24 12:49:35 +03003132 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003133
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003134 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
3135 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003136 if (lvl != SOL_TIPC)
3137 return -ENOPROTOOPT;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003138
3139 switch (opt) {
3140 case TIPC_IMPORTANCE:
3141 case TIPC_SRC_DROPPABLE:
3142 case TIPC_DEST_DROPPABLE:
3143 case TIPC_CONN_TIMEOUT:
Jon Maloyc0bceb92019-10-30 14:00:41 +01003144 case TIPC_NODELAY:
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003145 if (ol < sizeof(value))
3146 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003147 if (copy_from_sockptr(&value, ov, sizeof(u32)))
Jon Maloy75da2162017-10-13 11:04:23 +02003148 return -EFAULT;
3149 break;
3150 case TIPC_GROUP_JOIN:
3151 if (ol < sizeof(mreq))
3152 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003153 if (copy_from_sockptr(&mreq, ov, sizeof(mreq)))
Jon Maloy75da2162017-10-13 11:04:23 +02003154 return -EFAULT;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003155 break;
3156 default:
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003157 if (!sockptr_is_null(ov) || ol)
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003158 return -EINVAL;
3159 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01003160
Allan Stephens0c3141e2008-04-15 00:22:02 -07003161 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003162
Per Lidenb97bf3f2006-01-02 19:04:38 +01003163 switch (opt) {
3164 case TIPC_IMPORTANCE:
Christoph Hellwig095ae612020-05-28 07:12:36 +02003165 res = tsk_set_importance(sk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003166 break;
3167 case TIPC_SRC_DROPPABLE:
3168 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003169 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003170 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01003171 res = -ENOPROTOOPT;
3172 break;
3173 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003174 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003175 break;
3176 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04003177 tipc_sk(sk)->conn_timeout = value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003178 break;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003179 case TIPC_MCAST_BROADCAST:
3180 tsk->mc_method.rcast = false;
3181 tsk->mc_method.mandatory = true;
3182 break;
3183 case TIPC_MCAST_REPLICAST:
3184 tsk->mc_method.rcast = true;
3185 tsk->mc_method.mandatory = true;
3186 break;
Jon Maloy75da2162017-10-13 11:04:23 +02003187 case TIPC_GROUP_JOIN:
3188 res = tipc_sk_join(tsk, &mreq);
3189 break;
3190 case TIPC_GROUP_LEAVE:
3191 res = tipc_sk_leave(tsk);
3192 break;
Jon Maloyc0bceb92019-10-30 14:00:41 +01003193 case TIPC_NODELAY:
3194 tsk->nodelay = !!value;
3195 tsk_set_nagle(tsk);
3196 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003197 default:
3198 res = -EINVAL;
3199 }
3200
Allan Stephens0c3141e2008-04-15 00:22:02 -07003201 release_sock(sk);
3202
Per Lidenb97bf3f2006-01-02 19:04:38 +01003203 return res;
3204}
3205
3206/**
Ying Xue247f0f32014-02-18 16:06:46 +08003207 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01003208 * @sock: socket structure
3209 * @lvl: option level
3210 * @opt: option identifier
3211 * @ov: receptacle for option value
3212 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003213 *
3214 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01003215 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003216 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08003217 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01003218 */
Ying Xue247f0f32014-02-18 16:06:46 +08003219static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
3220 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003221{
Allan Stephens0c3141e2008-04-15 00:22:02 -07003222 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04003223 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloyb6f88d92020-11-25 13:29:15 -05003224 struct tipc_service_range seq;
Jon Maloy75da2162017-10-13 11:04:23 +02003225 int len, scope;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003226 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003227 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003228
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003229 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
3230 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003231 if (lvl != SOL_TIPC)
3232 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00003233 res = get_user(len, ol);
3234 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003235 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003236
Allan Stephens0c3141e2008-04-15 00:22:02 -07003237 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003238
3239 switch (opt) {
3240 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003241 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003242 break;
3243 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003244 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003245 break;
3246 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003247 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003248 break;
3249 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003250 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07003251 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01003252 break;
Allan Stephens0e659672010-12-31 18:59:32 +00003253 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05003254 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00003255 break;
Allan Stephens0e659672010-12-31 18:59:32 +00003256 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00003257 value = skb_queue_len(&sk->sk_receive_queue);
3258 break;
Tung Nguyen42e54252019-04-18 21:02:19 +07003259 case TIPC_SOCK_RECVQ_USED:
3260 value = sk_rmem_alloc_get(sk);
3261 break;
Jon Maloy75da2162017-10-13 11:04:23 +02003262 case TIPC_GROUP_JOIN:
3263 seq.type = 0;
3264 if (tsk->group)
3265 tipc_group_self(tsk->group, &seq, &scope);
3266 value = seq.type;
3267 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003268 default:
3269 res = -EINVAL;
3270 }
3271
Allan Stephens0c3141e2008-04-15 00:22:02 -07003272 release_sock(sk);
3273
Paul Gortmaker25860c32010-12-31 18:59:31 +00003274 if (res)
3275 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01003276
Paul Gortmaker25860c32010-12-31 18:59:31 +00003277 if (len < sizeof(value))
3278 return -EINVAL;
3279
3280 if (copy_to_user(ov, &value, sizeof(value)))
3281 return -EFAULT;
3282
3283 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003284}
3285
Ying Xuef2f98002015-01-09 15:27:05 +08003286static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02003287{
Jon Maloy3e5cf362018-04-25 19:29:36 +02003288 struct net *net = sock_net(sock->sk);
3289 struct tipc_sioc_nodeid_req nr = {0};
Erik Hugne78acb1f2014-04-24 16:26:47 +02003290 struct tipc_sioc_ln_req lnr;
3291 void __user *argp = (void __user *)arg;
3292
3293 switch (cmd) {
3294 case SIOCGETLINKNAME:
3295 if (copy_from_user(&lnr, argp, sizeof(lnr)))
3296 return -EFAULT;
Jon Maloy3e5cf362018-04-25 19:29:36 +02003297 if (!tipc_node_get_linkname(net,
Ying Xuef2f98002015-01-09 15:27:05 +08003298 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02003299 lnr.linkname, TIPC_MAX_LINK_NAME)) {
3300 if (copy_to_user(argp, &lnr, sizeof(lnr)))
3301 return -EFAULT;
3302 return 0;
3303 }
3304 return -EADDRNOTAVAIL;
Jon Maloy3e5cf362018-04-25 19:29:36 +02003305 case SIOCGETNODEID:
3306 if (copy_from_user(&nr, argp, sizeof(nr)))
3307 return -EFAULT;
3308 if (!tipc_node_get_id(net, nr.peer, nr.node_id))
3309 return -EADDRNOTAVAIL;
3310 if (copy_to_user(argp, &nr, sizeof(nr)))
3311 return -EFAULT;
3312 return 0;
Erik Hugne78acb1f2014-04-24 16:26:47 +02003313 default:
3314 return -ENOIOCTLCMD;
3315 }
3316}
3317
Erik Hugne70b03752017-03-29 11:22:16 +02003318static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
3319{
3320 struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
3321 struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
Erik Hugne66bc1e82017-03-29 11:22:17 +02003322 u32 onode = tipc_own_addr(sock_net(sock1->sk));
Erik Hugne70b03752017-03-29 11:22:16 +02003323
Erik Hugne66bc1e82017-03-29 11:22:17 +02003324 tsk1->peer.family = AF_TIPC;
Jon Maloyb6f88d92020-11-25 13:29:15 -05003325 tsk1->peer.addrtype = TIPC_SOCKET_ADDR;
Erik Hugne66bc1e82017-03-29 11:22:17 +02003326 tsk1->peer.scope = TIPC_NODE_SCOPE;
3327 tsk1->peer.addr.id.ref = tsk2->portid;
3328 tsk1->peer.addr.id.node = onode;
3329 tsk2->peer.family = AF_TIPC;
Jon Maloyb6f88d92020-11-25 13:29:15 -05003330 tsk2->peer.addrtype = TIPC_SOCKET_ADDR;
Erik Hugne66bc1e82017-03-29 11:22:17 +02003331 tsk2->peer.scope = TIPC_NODE_SCOPE;
3332 tsk2->peer.addr.id.ref = tsk1->portid;
3333 tsk2->peer.addr.id.node = onode;
3334
3335 tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
3336 tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
Erik Hugne70b03752017-03-29 11:22:16 +02003337 return 0;
3338}
3339
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00003340/* Protocol switches for the various types of TIPC sockets */
3341
Florian Westphalbca65ea2008-02-07 18:18:01 -08003342static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00003343 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003344 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08003345 .release = tipc_release,
3346 .bind = tipc_bind,
3347 .connect = tipc_connect,
Erik Hugne66bc1e82017-03-29 11:22:17 +02003348 .socketpair = tipc_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04003349 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08003350 .getname = tipc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003351 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02003352 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04003353 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08003354 .shutdown = tipc_shutdown,
3355 .setsockopt = tipc_setsockopt,
3356 .getsockopt = tipc_getsockopt,
3357 .sendmsg = tipc_sendmsg,
3358 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09003359 .mmap = sock_no_mmap,
3360 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01003361};
3362
Florian Westphalbca65ea2008-02-07 18:18:01 -08003363static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00003364 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003365 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08003366 .release = tipc_release,
3367 .bind = tipc_bind,
3368 .connect = tipc_connect,
Erik Hugne70b03752017-03-29 11:22:16 +02003369 .socketpair = tipc_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08003370 .accept = tipc_accept,
3371 .getname = tipc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003372 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02003373 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08003374 .listen = tipc_listen,
3375 .shutdown = tipc_shutdown,
3376 .setsockopt = tipc_setsockopt,
3377 .getsockopt = tipc_getsockopt,
3378 .sendmsg = tipc_send_packet,
3379 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09003380 .mmap = sock_no_mmap,
3381 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01003382};
3383
Florian Westphalbca65ea2008-02-07 18:18:01 -08003384static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00003385 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003386 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08003387 .release = tipc_release,
3388 .bind = tipc_bind,
3389 .connect = tipc_connect,
Erik Hugne70b03752017-03-29 11:22:16 +02003390 .socketpair = tipc_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08003391 .accept = tipc_accept,
3392 .getname = tipc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003393 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02003394 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08003395 .listen = tipc_listen,
3396 .shutdown = tipc_shutdown,
3397 .setsockopt = tipc_setsockopt,
3398 .getsockopt = tipc_getsockopt,
Jon Paul Maloy365ad352017-01-03 10:55:11 -05003399 .sendmsg = tipc_sendstream,
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02003400 .recvmsg = tipc_recvstream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09003401 .mmap = sock_no_mmap,
3402 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01003403};
3404
Florian Westphalbca65ea2008-02-07 18:18:01 -08003405static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00003406 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003407 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04003408 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01003409};
3410
3411static struct proto tipc_proto = {
3412 .name = "TIPC",
3413 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04003414 .obj_size = sizeof(struct tipc_sock),
3415 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01003416};
3417
3418/**
Per Liden4323add2006-01-18 00:38:21 +01003419 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003420 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08003421 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01003422 */
Per Liden4323add2006-01-18 00:38:21 +01003423int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003424{
3425 int res;
3426
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003427 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003428 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04003429 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01003430 goto out;
3431 }
3432
3433 res = sock_register(&tipc_family_ops);
3434 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04003435 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01003436 proto_unregister(&tipc_proto);
3437 goto out;
3438 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01003439 out:
3440 return res;
3441}
3442
3443/**
Per Liden4323add2006-01-18 00:38:21 +01003444 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01003445 */
Per Liden4323add2006-01-18 00:38:21 +01003446void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003447{
Per Lidenb97bf3f2006-01-02 19:04:38 +01003448 sock_unregister(tipc_family_ops.family);
3449 proto_unregister(&tipc_proto);
3450}
Richard Alpe34b78a122014-11-20 10:29:10 +01003451
3452/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01003453static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01003454{
3455 u32 peer_node;
3456 u32 peer_port;
3457 struct nlattr *nest;
3458
3459 peer_node = tsk_peer_node(tsk);
3460 peer_port = tsk_peer_port(tsk);
3461
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003462 nest = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_CON);
Kangjie Lu517ccc22019-03-16 16:46:05 -05003463 if (!nest)
3464 return -EMSGSIZE;
Richard Alpe34b78a122014-11-20 10:29:10 +01003465
3466 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
3467 goto msg_full;
3468 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
3469 goto msg_full;
3470
3471 if (tsk->conn_type != 0) {
3472 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
3473 goto msg_full;
3474 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
3475 goto msg_full;
3476 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
3477 goto msg_full;
3478 }
3479 nla_nest_end(skb, nest);
3480
3481 return 0;
3482
3483msg_full:
3484 nla_nest_cancel(skb, nest);
3485
3486 return -EMSGSIZE;
3487}
3488
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003489static int __tipc_nl_add_sk_info(struct sk_buff *skb, struct tipc_sock
3490 *tsk)
3491{
3492 struct net *net = sock_net(skb->sk);
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003493 struct sock *sk = &tsk->sk;
3494
3495 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid) ||
Jon Maloy23fd3ea2018-03-22 20:42:49 +01003496 nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr(net)))
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003497 return -EMSGSIZE;
3498
3499 if (tipc_sk_connected(sk)) {
3500 if (__tipc_nl_add_sk_con(skb, tsk))
3501 return -EMSGSIZE;
3502 } else if (!list_empty(&tsk->publications)) {
3503 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
3504 return -EMSGSIZE;
3505 }
3506 return 0;
3507}
3508
Richard Alpe34b78a122014-11-20 10:29:10 +01003509/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01003510static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
3511 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01003512{
Richard Alpe34b78a122014-11-20 10:29:10 +01003513 struct nlattr *attrs;
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003514 void *hdr;
Richard Alpe34b78a122014-11-20 10:29:10 +01003515
3516 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01003517 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01003518 if (!hdr)
3519 goto msg_cancel;
3520
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003521 attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK);
Richard Alpe34b78a122014-11-20 10:29:10 +01003522 if (!attrs)
3523 goto genlmsg_cancel;
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003524
3525 if (__tipc_nl_add_sk_info(skb, tsk))
Richard Alpe34b78a122014-11-20 10:29:10 +01003526 goto attr_msg_cancel;
3527
Richard Alpe34b78a122014-11-20 10:29:10 +01003528 nla_nest_end(skb, attrs);
3529 genlmsg_end(skb, hdr);
3530
3531 return 0;
3532
3533attr_msg_cancel:
3534 nla_nest_cancel(skb, attrs);
3535genlmsg_cancel:
3536 genlmsg_cancel(skb, hdr);
3537msg_cancel:
3538 return -EMSGSIZE;
3539}
3540
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003541int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,
3542 int (*skb_handler)(struct sk_buff *skb,
3543 struct netlink_callback *cb,
3544 struct tipc_sock *tsk))
Richard Alpe34b78a122014-11-20 10:29:10 +01003545{
Cong Wang8f5c5fc2018-09-04 14:54:55 -07003546 struct rhashtable_iter *iter = (void *)cb->args[4];
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003547 struct tipc_sock *tsk;
3548 int err;
Richard Alpe34b78a122014-11-20 10:29:10 +01003549
Cong Wang9a07efa2018-08-24 12:28:06 -07003550 rhashtable_walk_start(iter);
3551 while ((tsk = rhashtable_walk_next(iter)) != NULL) {
3552 if (IS_ERR(tsk)) {
3553 err = PTR_ERR(tsk);
3554 if (err == -EAGAIN) {
3555 err = 0;
Richard Alped6e164e2015-01-16 12:30:40 +01003556 continue;
3557 }
Cong Wang9a07efa2018-08-24 12:28:06 -07003558 break;
Ying Xue07f6c4b2015-01-07 13:41:58 +08003559 }
Richard Alpe34b78a122014-11-20 10:29:10 +01003560
Cong Wang9a07efa2018-08-24 12:28:06 -07003561 sock_hold(&tsk->sk);
3562 rhashtable_walk_stop(iter);
3563 lock_sock(&tsk->sk);
3564 err = skb_handler(skb, cb, tsk);
3565 if (err) {
3566 release_sock(&tsk->sk);
3567 sock_put(&tsk->sk);
3568 goto out;
3569 }
3570 release_sock(&tsk->sk);
3571 rhashtable_walk_start(iter);
3572 sock_put(&tsk->sk);
3573 }
3574 rhashtable_walk_stop(iter);
3575out:
Richard Alpe34b78a122014-11-20 10:29:10 +01003576 return skb->len;
3577}
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003578EXPORT_SYMBOL(tipc_nl_sk_walk);
3579
Cong Wang9a07efa2018-08-24 12:28:06 -07003580int tipc_dump_start(struct netlink_callback *cb)
3581{
Cong Wang8f5c5fc2018-09-04 14:54:55 -07003582 return __tipc_dump_start(cb, sock_net(cb->skb->sk));
3583}
3584EXPORT_SYMBOL(tipc_dump_start);
3585
3586int __tipc_dump_start(struct netlink_callback *cb, struct net *net)
3587{
3588 /* tipc_nl_name_table_dump() uses cb->args[0...3]. */
3589 struct rhashtable_iter *iter = (void *)cb->args[4];
Cong Wang9a07efa2018-08-24 12:28:06 -07003590 struct tipc_net *tn = tipc_net(net);
3591
3592 if (!iter) {
3593 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3594 if (!iter)
3595 return -ENOMEM;
3596
Cong Wang8f5c5fc2018-09-04 14:54:55 -07003597 cb->args[4] = (long)iter;
Cong Wang9a07efa2018-08-24 12:28:06 -07003598 }
3599
3600 rhashtable_walk_enter(&tn->sk_rht, iter);
3601 return 0;
3602}
Cong Wang9a07efa2018-08-24 12:28:06 -07003603
3604int tipc_dump_done(struct netlink_callback *cb)
3605{
Cong Wang8f5c5fc2018-09-04 14:54:55 -07003606 struct rhashtable_iter *hti = (void *)cb->args[4];
Cong Wang9a07efa2018-08-24 12:28:06 -07003607
3608 rhashtable_walk_exit(hti);
3609 kfree(hti);
3610 return 0;
3611}
3612EXPORT_SYMBOL(tipc_dump_done);
3613
Cong Wange41f0542018-04-06 18:54:52 -07003614int tipc_sk_fill_sock_diag(struct sk_buff *skb, struct netlink_callback *cb,
3615 struct tipc_sock *tsk, u32 sk_filter_state,
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003616 u64 (*tipc_diag_gen_cookie)(struct sock *sk))
3617{
3618 struct sock *sk = &tsk->sk;
3619 struct nlattr *attrs;
3620 struct nlattr *stat;
3621
3622 /*filter response w.r.t sk_state*/
3623 if (!(sk_filter_state & (1 << sk->sk_state)))
3624 return 0;
3625
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003626 attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK);
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003627 if (!attrs)
3628 goto msg_cancel;
3629
3630 if (__tipc_nl_add_sk_info(skb, tsk))
3631 goto attr_msg_cancel;
3632
3633 if (nla_put_u32(skb, TIPC_NLA_SOCK_TYPE, (u32)sk->sk_type) ||
3634 nla_put_u32(skb, TIPC_NLA_SOCK_TIPC_STATE, (u32)sk->sk_state) ||
3635 nla_put_u32(skb, TIPC_NLA_SOCK_INO, sock_i_ino(sk)) ||
3636 nla_put_u32(skb, TIPC_NLA_SOCK_UID,
Cong Wange41f0542018-04-06 18:54:52 -07003637 from_kuid_munged(sk_user_ns(NETLINK_CB(cb->skb).sk),
GhantaKrishnamurthy MohanKrishna4b2e6872018-04-04 14:49:47 +02003638 sock_i_uid(sk))) ||
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003639 nla_put_u64_64bit(skb, TIPC_NLA_SOCK_COOKIE,
3640 tipc_diag_gen_cookie(sk),
3641 TIPC_NLA_SOCK_PAD))
3642 goto attr_msg_cancel;
3643
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003644 stat = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_STAT);
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003645 if (!stat)
3646 goto attr_msg_cancel;
3647
3648 if (nla_put_u32(skb, TIPC_NLA_SOCK_STAT_RCVQ,
3649 skb_queue_len(&sk->sk_receive_queue)) ||
3650 nla_put_u32(skb, TIPC_NLA_SOCK_STAT_SENDQ,
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01003651 skb_queue_len(&sk->sk_write_queue)) ||
3652 nla_put_u32(skb, TIPC_NLA_SOCK_STAT_DROP,
3653 atomic_read(&sk->sk_drops)))
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003654 goto stat_msg_cancel;
3655
3656 if (tsk->cong_link_cnt &&
3657 nla_put_flag(skb, TIPC_NLA_SOCK_STAT_LINK_CONG))
3658 goto stat_msg_cancel;
3659
3660 if (tsk_conn_cong(tsk) &&
3661 nla_put_flag(skb, TIPC_NLA_SOCK_STAT_CONN_CONG))
3662 goto stat_msg_cancel;
3663
3664 nla_nest_end(skb, stat);
GhantaKrishnamurthy MohanKrishnaa1be5a22018-06-29 13:26:18 +02003665
3666 if (tsk->group)
3667 if (tipc_group_fill_sock_diag(tsk->group, skb))
3668 goto stat_msg_cancel;
3669
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003670 nla_nest_end(skb, attrs);
3671
3672 return 0;
3673
3674stat_msg_cancel:
3675 nla_nest_cancel(skb, stat);
3676attr_msg_cancel:
3677 nla_nest_cancel(skb, attrs);
3678msg_cancel:
3679 return -EMSGSIZE;
3680}
3681EXPORT_SYMBOL(tipc_sk_fill_sock_diag);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003682
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003683int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
3684{
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003685 return tipc_nl_sk_walk(skb, cb, __tipc_nl_add_sk);
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003686}
3687
Richard Alpe1a1a1432014-11-20 10:29:11 +01003688/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01003689static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
3690 struct netlink_callback *cb,
3691 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01003692{
3693 void *hdr;
3694 struct nlattr *attrs;
3695
3696 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01003697 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003698 if (!hdr)
3699 goto msg_cancel;
3700
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003701 attrs = nla_nest_start_noflag(skb, TIPC_NLA_PUBL);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003702 if (!attrs)
3703 goto genlmsg_cancel;
3704
3705 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
3706 goto attr_msg_cancel;
Jon Maloy998d3902021-03-16 22:06:08 -04003707 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->sr.type))
Richard Alpe1a1a1432014-11-20 10:29:11 +01003708 goto attr_msg_cancel;
Jon Maloy998d3902021-03-16 22:06:08 -04003709 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->sr.lower))
Richard Alpe1a1a1432014-11-20 10:29:11 +01003710 goto attr_msg_cancel;
Jon Maloy998d3902021-03-16 22:06:08 -04003711 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->sr.upper))
Richard Alpe1a1a1432014-11-20 10:29:11 +01003712 goto attr_msg_cancel;
3713
3714 nla_nest_end(skb, attrs);
3715 genlmsg_end(skb, hdr);
3716
3717 return 0;
3718
3719attr_msg_cancel:
3720 nla_nest_cancel(skb, attrs);
3721genlmsg_cancel:
3722 genlmsg_cancel(skb, hdr);
3723msg_cancel:
3724 return -EMSGSIZE;
3725}
3726
3727/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01003728static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
3729 struct netlink_callback *cb,
3730 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01003731{
3732 int err;
3733 struct publication *p;
3734
3735 if (*last_publ) {
Jon Maloye50e73e2018-03-15 16:48:55 +01003736 list_for_each_entry(p, &tsk->publications, binding_sock) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01003737 if (p->key == *last_publ)
3738 break;
3739 }
3740 if (p->key != *last_publ) {
3741 /* We never set seq or call nl_dump_check_consistent()
3742 * this means that setting prev_seq here will cause the
3743 * consistence check to fail in the netlink callback
3744 * handler. Resulting in the last NLMSG_DONE message
3745 * having the NLM_F_DUMP_INTR flag set.
3746 */
3747 cb->prev_seq = 1;
3748 *last_publ = 0;
3749 return -EPIPE;
3750 }
3751 } else {
3752 p = list_first_entry(&tsk->publications, struct publication,
Jon Maloye50e73e2018-03-15 16:48:55 +01003753 binding_sock);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003754 }
3755
Jon Maloye50e73e2018-03-15 16:48:55 +01003756 list_for_each_entry_from(p, &tsk->publications, binding_sock) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01003757 err = __tipc_nl_add_sk_publ(skb, cb, p);
3758 if (err) {
3759 *last_publ = p->key;
3760 return err;
3761 }
3762 }
3763 *last_publ = 0;
3764
3765 return 0;
3766}
3767
3768int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
3769{
3770 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08003771 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01003772 u32 last_publ = cb->args[1];
3773 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08003774 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003775 struct tipc_sock *tsk;
3776
Ying Xue07f6c4b2015-01-07 13:41:58 +08003777 if (!tsk_portid) {
Jiri Pirko057af702019-10-05 20:04:39 +02003778 struct nlattr **attrs = genl_dumpit_info(cb)->attrs;
Richard Alpe1a1a1432014-11-20 10:29:11 +01003779 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
3780
Richard Alpe45e093a2016-05-16 11:14:54 +02003781 if (!attrs[TIPC_NLA_SOCK])
3782 return -EINVAL;
3783
Johannes Berg8cb08172019-04-26 14:07:28 +02003784 err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
3785 attrs[TIPC_NLA_SOCK],
3786 tipc_nl_sock_policy, NULL);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003787 if (err)
3788 return err;
3789
3790 if (!sock[TIPC_NLA_SOCK_REF])
3791 return -EINVAL;
3792
Ying Xue07f6c4b2015-01-07 13:41:58 +08003793 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003794 }
3795
3796 if (done)
3797 return 0;
3798
Ying Xuee05b31f2015-01-09 15:27:08 +08003799 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003800 if (!tsk)
3801 return -EINVAL;
3802
3803 lock_sock(&tsk->sk);
3804 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
3805 if (!err)
3806 done = 1;
3807 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08003808 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003809
Ying Xue07f6c4b2015-01-07 13:41:58 +08003810 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01003811 cb->args[1] = last_publ;
3812 cb->args[2] = done;
3813
3814 return skb->len;
3815}
Tuong Lienb4b97712018-12-19 09:17:56 +07003816
Tuong Lien01e661e2018-12-19 09:17:58 +07003817/**
3818 * tipc_sk_filtering - check if a socket should be traced
3819 * @sk: the socket to be examined
Randy Dunlapf172f4b2020-11-29 10:32:49 -08003820 *
3821 * @sysctl_tipc_sk_filter is used as the socket tuple for filtering:
Randy Dunlap5fcb7d42020-11-29 10:32:50 -08003822 * (portid, sock type, name type, name lower, name upper)
Tuong Lien01e661e2018-12-19 09:17:58 +07003823 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08003824 * Return: true if the socket meets the socket tuple data
Tuong Lien01e661e2018-12-19 09:17:58 +07003825 * (value 0 = 'any') or when there is no tuple set (all = 0),
3826 * otherwise false
3827 */
3828bool tipc_sk_filtering(struct sock *sk)
3829{
3830 struct tipc_sock *tsk;
3831 struct publication *p;
3832 u32 _port, _sktype, _type, _lower, _upper;
3833 u32 type = 0, lower = 0, upper = 0;
3834
3835 if (!sk)
3836 return true;
3837
3838 tsk = tipc_sk(sk);
3839
3840 _port = sysctl_tipc_sk_filter[0];
3841 _sktype = sysctl_tipc_sk_filter[1];
3842 _type = sysctl_tipc_sk_filter[2];
3843 _lower = sysctl_tipc_sk_filter[3];
3844 _upper = sysctl_tipc_sk_filter[4];
3845
3846 if (!_port && !_sktype && !_type && !_lower && !_upper)
3847 return true;
3848
3849 if (_port)
3850 return (_port == tsk->portid);
3851
3852 if (_sktype && _sktype != sk->sk_type)
3853 return false;
3854
3855 if (tsk->published) {
3856 p = list_first_entry_or_null(&tsk->publications,
3857 struct publication, binding_sock);
3858 if (p) {
Jon Maloy998d3902021-03-16 22:06:08 -04003859 type = p->sr.type;
3860 lower = p->sr.lower;
3861 upper = p->sr.upper;
Tuong Lien01e661e2018-12-19 09:17:58 +07003862 }
3863 }
3864
3865 if (!tipc_sk_type_connectionless(sk)) {
3866 type = tsk->conn_type;
3867 lower = tsk->conn_instance;
3868 upper = tsk->conn_instance;
3869 }
3870
3871 if ((_type && _type != type) || (_lower && _lower != lower) ||
3872 (_upper && _upper != upper))
3873 return false;
3874
3875 return true;
3876}
3877
Tuong Lienb4b97712018-12-19 09:17:56 +07003878u32 tipc_sock_get_portid(struct sock *sk)
3879{
3880 return (sk) ? (tipc_sk(sk))->portid : 0;
3881}
3882
3883/**
Tuong Lien01e661e2018-12-19 09:17:58 +07003884 * tipc_sk_overlimit1 - check if socket rx queue is about to be overloaded,
3885 * both the rcv and backlog queues are considered
3886 * @sk: tipc sk to be checked
3887 * @skb: tipc msg to be checked
3888 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08003889 * Return: true if the socket rx queue allocation is > 90%, otherwise false
Tuong Lien01e661e2018-12-19 09:17:58 +07003890 */
3891
3892bool tipc_sk_overlimit1(struct sock *sk, struct sk_buff *skb)
3893{
3894 atomic_t *dcnt = &tipc_sk(sk)->dupl_rcvcnt;
3895 unsigned int lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
3896 unsigned int qsize = sk->sk_backlog.len + sk_rmem_alloc_get(sk);
3897
3898 return (qsize > lim * 90 / 100);
3899}
3900
3901/**
3902 * tipc_sk_overlimit2 - check if socket rx queue is about to be overloaded,
3903 * only the rcv queue is considered
3904 * @sk: tipc sk to be checked
3905 * @skb: tipc msg to be checked
3906 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08003907 * Return: true if the socket rx queue allocation is > 90%, otherwise false
Tuong Lien01e661e2018-12-19 09:17:58 +07003908 */
3909
3910bool tipc_sk_overlimit2(struct sock *sk, struct sk_buff *skb)
3911{
3912 unsigned int lim = rcvbuf_limit(sk, skb);
3913 unsigned int qsize = sk_rmem_alloc_get(sk);
3914
3915 return (qsize > lim * 90 / 100);
3916}
3917
3918/**
Tuong Lienb4b97712018-12-19 09:17:56 +07003919 * tipc_sk_dump - dump TIPC socket
3920 * @sk: tipc sk to be dumped
3921 * @dqueues: bitmask to decide if any socket queue to be dumped?
3922 * - TIPC_DUMP_NONE: don't dump socket queues
3923 * - TIPC_DUMP_SK_SNDQ: dump socket send queue
3924 * - TIPC_DUMP_SK_RCVQ: dump socket rcv queue
3925 * - TIPC_DUMP_SK_BKLGQ: dump socket backlog queue
3926 * - TIPC_DUMP_ALL: dump all the socket queues above
3927 * @buf: returned buffer of dump data in format
3928 */
3929int tipc_sk_dump(struct sock *sk, u16 dqueues, char *buf)
3930{
3931 int i = 0;
3932 size_t sz = (dqueues) ? SK_LMAX : SK_LMIN;
3933 struct tipc_sock *tsk;
3934 struct publication *p;
3935 bool tsk_connected;
3936
3937 if (!sk) {
3938 i += scnprintf(buf, sz, "sk data: (null)\n");
3939 return i;
3940 }
3941
3942 tsk = tipc_sk(sk);
3943 tsk_connected = !tipc_sk_type_connectionless(sk);
3944
3945 i += scnprintf(buf, sz, "sk data: %u", sk->sk_type);
3946 i += scnprintf(buf + i, sz - i, " %d", sk->sk_state);
3947 i += scnprintf(buf + i, sz - i, " %x", tsk_own_node(tsk));
3948 i += scnprintf(buf + i, sz - i, " %u", tsk->portid);
3949 i += scnprintf(buf + i, sz - i, " | %u", tsk_connected);
3950 if (tsk_connected) {
3951 i += scnprintf(buf + i, sz - i, " %x", tsk_peer_node(tsk));
3952 i += scnprintf(buf + i, sz - i, " %u", tsk_peer_port(tsk));
3953 i += scnprintf(buf + i, sz - i, " %u", tsk->conn_type);
3954 i += scnprintf(buf + i, sz - i, " %u", tsk->conn_instance);
3955 }
3956 i += scnprintf(buf + i, sz - i, " | %u", tsk->published);
3957 if (tsk->published) {
3958 p = list_first_entry_or_null(&tsk->publications,
3959 struct publication, binding_sock);
Jon Maloy998d3902021-03-16 22:06:08 -04003960 i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.type : 0);
3961 i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.lower : 0);
3962 i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.upper : 0);
Tuong Lienb4b97712018-12-19 09:17:56 +07003963 }
3964 i += scnprintf(buf + i, sz - i, " | %u", tsk->snd_win);
3965 i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_win);
3966 i += scnprintf(buf + i, sz - i, " %u", tsk->max_pkt);
3967 i += scnprintf(buf + i, sz - i, " %x", tsk->peer_caps);
3968 i += scnprintf(buf + i, sz - i, " %u", tsk->cong_link_cnt);
3969 i += scnprintf(buf + i, sz - i, " %u", tsk->snt_unacked);
3970 i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_unacked);
3971 i += scnprintf(buf + i, sz - i, " %u", atomic_read(&tsk->dupl_rcvcnt));
3972 i += scnprintf(buf + i, sz - i, " %u", sk->sk_shutdown);
3973 i += scnprintf(buf + i, sz - i, " | %d", sk_wmem_alloc_get(sk));
3974 i += scnprintf(buf + i, sz - i, " %d", sk->sk_sndbuf);
3975 i += scnprintf(buf + i, sz - i, " | %d", sk_rmem_alloc_get(sk));
3976 i += scnprintf(buf + i, sz - i, " %d", sk->sk_rcvbuf);
Eric Dumazet70c26552019-10-09 15:41:03 -07003977 i += scnprintf(buf + i, sz - i, " | %d\n", READ_ONCE(sk->sk_backlog.len));
Tuong Lienb4b97712018-12-19 09:17:56 +07003978
3979 if (dqueues & TIPC_DUMP_SK_SNDQ) {
3980 i += scnprintf(buf + i, sz - i, "sk_write_queue: ");
3981 i += tipc_list_dump(&sk->sk_write_queue, false, buf + i);
3982 }
3983
3984 if (dqueues & TIPC_DUMP_SK_RCVQ) {
3985 i += scnprintf(buf + i, sz - i, "sk_receive_queue: ");
3986 i += tipc_list_dump(&sk->sk_receive_queue, false, buf + i);
3987 }
3988
3989 if (dqueues & TIPC_DUMP_SK_BKLGQ) {
3990 i += scnprintf(buf + i, sz - i, "sk_backlog:\n head ");
3991 i += tipc_skb_dump(sk->sk_backlog.head, false, buf + i);
3992 if (sk->sk_backlog.tail != sk->sk_backlog.head) {
3993 i += scnprintf(buf + i, sz - i, " tail ");
3994 i += tipc_skb_dump(sk->sk_backlog.tail, false,
3995 buf + i);
3996 }
3997 }
3998
3999 return i;
4000}