blob: 575a0238deb29be5cc10bc22e902b19f51bd0069 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Jon Paul Maloy02c00c22014-06-09 11:08:18 -05002 * net/tipc/socket.c: TIPC socket API
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Jon Maloy60c102e2020-11-25 13:29:13 -05004 * Copyright (c) 2001-2007, 2012-2019, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Jon Maloy998d3902021-03-16 22:06:08 -04006 * Copyright (c) 2020-2021, Red Hat Inc
Per Lidenb97bf3f2006-01-02 19:04:38 +01007 * All rights reserved.
8 *
Per Liden9ea1fd32006-01-11 13:30:43 +01009 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +010010 * modification, are permitted provided that the following conditions are met:
11 *
Per Liden9ea1fd32006-01-11 13:30:43 +010012 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010020 *
Per Liden9ea1fd32006-01-11 13:30:43 +010021 * Alternatively, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL") version 2 as published by the Free
23 * Software Foundation.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010035 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
Ying Xue07f6c4b2015-01-07 13:41:58 +080038#include <linux/rhashtable.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010039#include <linux/sched/signal.h>
40
Per Lidenb97bf3f2006-01-02 19:04:38 +010041#include "core.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050042#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020043#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050044#include "link.h"
Jon Paul Maloyc637c102015-02-05 08:36:41 -050045#include "name_distr.h"
Jon Paul Maloy2e84c602014-08-22 18:09:18 -040046#include "socket.h"
Jon Paul Maloya6bf70f2015-05-14 10:46:13 -040047#include "bcast.h"
Richard Alpe49cc66e2016-03-04 17:04:42 +010048#include "netlink.h"
Jon Maloy75da2162017-10-13 11:04:23 +020049#include "group.h"
Tuong Lienb4b97712018-12-19 09:17:56 +070050#include "trace.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040051
Tuong Lien0a3e0602020-05-26 16:38:38 +070052#define NAGLE_START_INIT 4
53#define NAGLE_START_MAX 1024
Tung Nguyen67879272018-09-28 20:23:22 +020054#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Jon Maloy0d5fcebf2017-10-20 11:21:32 +020055#define CONN_PROBING_INTV msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080056#define TIPC_MAX_PORT 0xffffffff
57#define TIPC_MIN_PORT 1
Randy Dunlap60462192020-09-17 21:35:19 -070058#define TIPC_ACK_RATE 4 /* ACK at 1/4 of rcv window size */
Jon Paul Maloy301bae52014-08-22 18:09:20 -040059
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010060enum {
61 TIPC_LISTEN = TCP_LISTEN,
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +010062 TIPC_ESTABLISHED = TCP_ESTABLISHED,
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +010063 TIPC_OPEN = TCP_CLOSE,
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +010064 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +010065 TIPC_CONNECTING = TCP_SYN_SENT,
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010066};
67
Jon Maloy31c82a22017-10-13 11:04:24 +020068struct sockaddr_pair {
69 struct sockaddr_tipc sock;
70 struct sockaddr_tipc member;
71};
72
Jon Paul Maloy301bae52014-08-22 18:09:20 -040073/**
74 * struct tipc_sock - TIPC socket structure
75 * @sk: socket - interacts with 'port' and with user via the socket API
Jon Paul Maloy301bae52014-08-22 18:09:20 -040076 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Jon Maloyc0bceb92019-10-30 14:00:41 +010077 * @maxnagle: maximum size of msg which can be subject to nagle
Ying Xue07f6c4b2015-01-07 13:41:58 +080078 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040079 * @phdr: preformatted message header used when sending messages
Randy Dunlapf172f4b2020-11-29 10:32:49 -080080 * @cong_links: list of congested links
Jon Paul Maloy301bae52014-08-22 18:09:20 -040081 * @publications: list of publications for port
Jon Paul Maloy365ad352017-01-03 10:55:11 -050082 * @blocking_link: address of the congested link we are currently sleeping on
Jon Paul Maloy301bae52014-08-22 18:09:20 -040083 * @pub_count: total # of publications port has made during its lifetime
Jon Paul Maloy301bae52014-08-22 18:09:20 -040084 * @conn_timeout: the time we can wait for an unresponded setup request
Randy Dunlapf172f4b2020-11-29 10:32:49 -080085 * @probe_unacked: probe has not received ack yet
Jon Paul Maloy301bae52014-08-22 18:09:20 -040086 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
Jon Paul Maloy365ad352017-01-03 10:55:11 -050087 * @cong_link_cnt: number of congested links
Jon Maloy75da2162017-10-13 11:04:23 +020088 * @snt_unacked: # messages sent by socket, and not yet acked by peer
Randy Dunlapf172f4b2020-11-29 10:32:49 -080089 * @snd_win: send window size
90 * @peer_caps: peer capabilities mask
Jon Paul Maloy301bae52014-08-22 18:09:20 -040091 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Randy Dunlapf172f4b2020-11-29 10:32:49 -080092 * @rcv_win: receive window size
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +010093 * @peer: 'connected' peer for dgram/rdm
Ying Xue07f6c4b2015-01-07 13:41:58 +080094 * @node: hash table node
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -050095 * @mc_method: cookie for use between socket and broadcast layer
Ying Xue07f6c4b2015-01-07 13:41:58 +080096 * @rcu: rcu struct for tipc_sock
Randy Dunlapf172f4b2020-11-29 10:32:49 -080097 * @group: TIPC communications group
98 * @oneway: message count in one direction (FIXME)
99 * @nagle_start: current nagle value
100 * @snd_backlog: send backlog count
101 * @msg_acc: messages accepted; used in managing backlog and nagle
102 * @pkt_cnt: TIPC socket packet count
103 * @expect_ack: whether this TIPC socket is expecting an ack
104 * @nodelay: setsockopt() TIPC_NODELAY setting
105 * @group_is_open: TIPC socket group is fully open (FIXME)
Jon Maloy14623e02021-06-02 13:44:24 -0400106 * @published: true if port has one or more associated names
107 * @conn_addrtype: address type used when establishing connection
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400108 */
109struct tipc_sock {
110 struct sock sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400111 u32 max_pkt;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100112 u32 maxnagle;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800113 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400114 struct tipc_msg phdr;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500115 struct list_head cong_links;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400116 struct list_head publications;
117 u32 pub_count;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400118 atomic_t dupl_rcvcnt;
Tung Nguyen67879272018-09-28 20:23:22 +0200119 u16 conn_timeout;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100120 bool probe_unacked;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500121 u16 cong_link_cnt;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400122 u16 snt_unacked;
123 u16 snd_win;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400124 u16 peer_caps;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400125 u16 rcv_unacked;
126 u16 rcv_win;
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +0100127 struct sockaddr_tipc peer;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800128 struct rhash_head node;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500129 struct tipc_mc_method mc_method;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800130 struct rcu_head rcu;
Jon Maloy75da2162017-10-13 11:04:23 +0200131 struct tipc_group *group;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100132 u32 oneway;
Tuong Lien0a3e0602020-05-26 16:38:38 +0700133 u32 nagle_start;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100134 u16 snd_backlog;
Tuong Lien0a3e0602020-05-26 16:38:38 +0700135 u16 msg_acc;
136 u16 pkt_cnt;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100137 bool expect_ack;
138 bool nodelay;
Jon Maloy60c25302018-01-17 16:42:46 +0100139 bool group_is_open;
Jon Maloy50a34992021-03-16 22:06:11 -0400140 bool published;
Jon Maloy14623e02021-06-02 13:44:24 -0400141 u8 conn_addrtype;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400142};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143
Jon Maloy64ac5f52017-10-13 11:04:20 +0200144static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400145static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800146static void tipc_write_space(struct sock *sk);
Ying Xuef4195d12015-11-22 15:46:05 +0800147static void tipc_sock_destruct(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800148static int tipc_release(struct socket *sock);
David Howellscdfbabf2017-03-09 08:09:05 +0000149static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
150 bool kern);
Kees Cook31b102b2017-10-30 14:06:45 -0700151static void tipc_sk_timeout(struct timer_list *t);
Jon Maloy50a34992021-03-16 22:06:11 -0400152static int tipc_sk_publish(struct tipc_sock *tsk, struct tipc_uaddr *ua);
Jon Maloy2c98da02021-03-16 22:06:13 -0400153static int tipc_sk_withdraw(struct tipc_sock *tsk, struct tipc_uaddr *ua);
Jon Maloy75da2162017-10-13 11:04:23 +0200154static int tipc_sk_leave(struct tipc_sock *tsk);
Ying Xuee05b31f2015-01-09 15:27:08 +0800155static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800156static int tipc_sk_insert(struct tipc_sock *tsk);
157static void tipc_sk_remove(struct tipc_sock *tsk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500158static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
Ying Xue39a0295f2015-03-02 15:37:47 +0800159static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Tuong Lien0a3e0602020-05-26 16:38:38 +0700160static void tipc_sk_push_backlog(struct tipc_sock *tsk, bool nagle_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100161
Florian Westphalbca65ea2008-02-07 18:18:01 -0800162static const struct proto_ops packet_ops;
163static const struct proto_ops stream_ops;
164static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165static struct proto tipc_proto;
Herbert Xu6cca72892015-03-20 21:57:05 +1100166static const struct rhashtable_params tsk_rht_params;
167
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500168static u32 tsk_own_node(struct tipc_sock *tsk)
169{
170 return msg_prevnode(&tsk->phdr);
171}
172
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400173static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400174{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400175 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400176}
177
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400178static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400179{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400180 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400181}
182
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400183static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400184{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400185 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400186}
187
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400188static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400189{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400190 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400191}
192
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400193static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400194{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400195 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400196}
197
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400198static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400199{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400200 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400201}
202
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400203static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400204{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400205 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400206}
207
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400208static struct tipc_sock *tipc_sk(const struct sock *sk)
209{
210 return container_of(sk, struct tipc_sock, sk);
211}
212
Christoph Hellwig095ae612020-05-28 07:12:36 +0200213int tsk_set_importance(struct sock *sk, int imp)
214{
215 if (imp > TIPC_CRITICAL_IMPORTANCE)
216 return -EINVAL;
217 msg_set_importance(&tipc_sk(sk)->phdr, (u32)imp);
218 return 0;
219}
220
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400221static bool tsk_conn_cong(struct tipc_sock *tsk)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400222{
Jon Paul Maloy6998cc62016-11-24 18:47:07 -0500223 return tsk->snt_unacked > tsk->snd_win;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400224}
225
Jon Maloyb7d42632017-10-13 11:04:26 +0200226static u16 tsk_blocks(int len)
227{
228 return ((len / FLOWCTL_BLK_SZ) + 1);
229}
230
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400231/* tsk_blocks(): translate a buffer size in bytes to number of
232 * advertisable blocks, taking into account the ratio truesize(len)/len
233 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
234 */
235static u16 tsk_adv_blocks(int len)
236{
237 return len / FLOWCTL_BLK_SZ / 4;
238}
239
240/* tsk_inc(): increment counter for sent or received data
241 * - If block based flow control is not supported by peer we
242 * fall back to message based ditto, incrementing the counter
243 */
244static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
245{
246 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
247 return ((msglen / FLOWCTL_BLK_SZ) + 1);
248 return 1;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400249}
250
Jon Maloyc0bceb92019-10-30 14:00:41 +0100251/* tsk_set_nagle - enable/disable nagle property by manipulating maxnagle
252 */
253static void tsk_set_nagle(struct tipc_sock *tsk)
254{
255 struct sock *sk = &tsk->sk;
256
257 tsk->maxnagle = 0;
258 if (sk->sk_type != SOCK_STREAM)
259 return;
260 if (tsk->nodelay)
261 return;
262 if (!(tsk->peer_caps & TIPC_NAGLE))
263 return;
264 /* Limit node local buffer size to avoid receive queue overflow */
265 if (tsk->max_pkt == MAX_MSG_SIZE)
266 tsk->maxnagle = 1500;
267 else
268 tsk->maxnagle = tsk->max_pkt;
269}
270
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400272 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Randy Dunlapf172f4b2020-11-29 10:32:49 -0800273 * @sk: network socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700274 *
275 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400277static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278{
Tuong Lien01e661e2018-12-19 09:17:58 +0700279 trace_tipc_sk_advance_rx(sk, NULL, TIPC_DUMP_SK_RCVQ, " ");
Allan Stephens5f6d9122011-11-04 13:24:29 -0400280 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100281}
282
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400283/* tipc_sk_respond() : send response message back to sender
284 */
285static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
286{
287 u32 selector;
288 u32 dnode;
289 u32 onode = tipc_own_addr(sock_net(sk));
290
291 if (!tipc_msg_reverse(onode, &skb, err))
292 return;
293
Tuong Lien01e661e2018-12-19 09:17:58 +0700294 trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE, "@sk_respond!");
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400295 dnode = msg_destnode(buf_msg(skb));
296 selector = msg_origport(buf_msg(skb));
297 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
298}
299
Per Lidenb97bf3f2006-01-02 19:04:38 +0100300/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400301 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Randy Dunlapf172f4b2020-11-29 10:32:49 -0800302 * @sk: network socket
303 * @error: response error code
Allan Stephens0c3141e2008-04-15 00:22:02 -0700304 *
305 * Caller must hold socket lock
306 */
Tuong Lien49afb802020-01-08 09:18:15 +0700307static void tsk_rej_rx_queue(struct sock *sk, int error)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700308{
Ying Xuea6ca1092014-11-26 11:41:55 +0800309 struct sk_buff *skb;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700310
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400311 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
Tuong Lien49afb802020-01-08 09:18:15 +0700312 tipc_sk_respond(sk, skb, error);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700313}
314
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100315static bool tipc_sk_connected(struct sock *sk)
316{
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100317 return sk->sk_state == TIPC_ESTABLISHED;
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100318}
319
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +0100320/* tipc_sk_type_connectionless - check if the socket is datagram socket
321 * @sk: socket
322 *
323 * Returns true if connection less, false otherwise
324 */
325static bool tipc_sk_type_connectionless(struct sock *sk)
326{
327 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
328}
329
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400330/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400331 *
332 * Handles cases where the node's network address has changed from
333 * the default of <0.0.0> to its configured setting.
334 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400335static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400336{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100337 struct sock *sk = &tsk->sk;
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100338 u32 self = tipc_own_addr(sock_net(sk));
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400339 u32 peer_port = tsk_peer_port(tsk);
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100340 u32 orig_node, peer_node;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400341
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100342 if (unlikely(!tipc_sk_connected(sk)))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400343 return false;
344
345 if (unlikely(msg_origport(msg) != peer_port))
346 return false;
347
348 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400349 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400350
351 if (likely(orig_node == peer_node))
352 return true;
353
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100354 if (!orig_node && peer_node == self)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400355 return true;
356
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100357 if (!peer_node && orig_node == self)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400358 return true;
359
360 return false;
361}
362
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100363/* tipc_set_sk_state - set the sk_state of the socket
364 * @sk: socket
365 *
366 * Caller must hold socket lock
367 *
368 * Returns 0 on success, errno otherwise
369 */
370static int tipc_set_sk_state(struct sock *sk, int state)
371{
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100372 int oldsk_state = sk->sk_state;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100373 int res = -EINVAL;
374
375 switch (state) {
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100376 case TIPC_OPEN:
377 res = 0;
378 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100379 case TIPC_LISTEN:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100380 case TIPC_CONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100381 if (oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100382 res = 0;
383 break;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100384 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100385 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100386 oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100387 res = 0;
388 break;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100389 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100390 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100391 oldsk_state == TIPC_ESTABLISHED)
392 res = 0;
393 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100394 }
395
396 if (!res)
397 sk->sk_state = state;
398
399 return res;
400}
401
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500402static int tipc_sk_sock_err(struct socket *sock, long *timeout)
403{
404 struct sock *sk = sock->sk;
405 int err = sock_error(sk);
406 int typ = sock->type;
407
408 if (err)
409 return err;
410 if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
411 if (sk->sk_state == TIPC_DISCONNECTING)
412 return -EPIPE;
413 else if (!tipc_sk_connected(sk))
414 return -ENOTCONN;
415 }
416 if (!*timeout)
417 return -EAGAIN;
418 if (signal_pending(current))
419 return sock_intr_errno(*timeout);
420
421 return 0;
422}
423
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200424#define tipc_wait_for_cond(sock_, timeo_, condition_) \
425({ \
Tung Nguyenbfd07f32019-02-25 10:57:20 +0700426 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200427 struct sock *sk_; \
428 int rc_; \
429 \
430 while ((rc_ = !(condition_))) { \
Tung Nguyenbfd07f32019-02-25 10:57:20 +0700431 /* coupled with smp_wmb() in tipc_sk_proto_rcv() */ \
432 smp_rmb(); \
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200433 sk_ = (sock_)->sk; \
434 rc_ = tipc_sk_sock_err((sock_), timeo_); \
435 if (rc_) \
436 break; \
Tung Nguyen223b7322019-02-19 11:20:47 +0700437 add_wait_queue(sk_sleep(sk_), &wait_); \
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200438 release_sock(sk_); \
439 *(timeo_) = wait_woken(&wait_, TASK_INTERRUPTIBLE, *(timeo_)); \
440 sched_annotate_sleep(); \
441 lock_sock(sk_); \
442 remove_wait_queue(sk_sleep(sk_), &wait_); \
443 } \
444 rc_; \
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500445})
446
Allan Stephens0c3141e2008-04-15 00:22:02 -0700447/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400448 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700449 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 * @sock: pre-allocated socket structure
451 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800452 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900453 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700454 * This routine creates additional data structures used by the TIPC socket,
455 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456 *
Randy Dunlap637b77f2020-11-29 10:32:48 -0800457 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +0100458 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400459static int tipc_sk_create(struct net *net, struct socket *sock,
460 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700462 const struct proto_ops *ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100463 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400464 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400465 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700466
467 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468 if (unlikely(protocol != 0))
469 return -EPROTONOSUPPORT;
470
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471 switch (sock->type) {
472 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700473 ops = &stream_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474 break;
475 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700476 ops = &packet_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477 break;
478 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700480 ops = &msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481 break;
Allan Stephens49978652006-06-25 23:47:18 -0700482 default:
Allan Stephens49978652006-06-25 23:47:18 -0700483 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484 }
485
Allan Stephens0c3141e2008-04-15 00:22:02 -0700486 /* Allocate socket's protocol area */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500487 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700488 if (sk == NULL)
489 return -ENOMEM;
490
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400491 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400492 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100493 tsk->maxnagle = 0;
Tuong Lien0a3e0602020-05-26 16:38:38 +0700494 tsk->nagle_start = NAGLE_START_INIT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400495 INIT_LIST_HEAD(&tsk->publications);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500496 INIT_LIST_HEAD(&tsk->cong_links);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400497 msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498
Allan Stephens0c3141e2008-04-15 00:22:02 -0700499 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700500 sock->ops = ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 sock_init_data(sock, sk);
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100502 tipc_set_sk_state(sk, TIPC_OPEN);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800503 if (tipc_sk_insert(tsk)) {
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900504 pr_warn("Socket create failed; port number exhausted\n");
Ying Xue07f6c4b2015-01-07 13:41:58 +0800505 return -EINVAL;
506 }
Herbert Xu40f9f432017-02-11 19:26:46 +0800507
508 /* Ensure tsk is visible before we read own_addr. */
509 smp_mb();
510
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100511 tipc_msg_init(tipc_own_addr(net), msg, TIPC_LOW_IMPORTANCE,
512 TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
Herbert Xu40f9f432017-02-11 19:26:46 +0800513
Ying Xue07f6c4b2015-01-07 13:41:58 +0800514 msg_set_origport(msg, tsk->portid);
Kees Cook31b102b2017-10-30 14:06:45 -0700515 timer_setup(&sk->sk_timer, tipc_sk_timeout, 0);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100516 sk->sk_shutdown = 0;
Jon Maloy64ac5f52017-10-13 11:04:20 +0200517 sk->sk_backlog_rcv = tipc_sk_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400518 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800519 sk->sk_data_ready = tipc_data_ready;
520 sk->sk_write_space = tipc_write_space;
Ying Xuef4195d12015-11-22 15:46:05 +0800521 sk->sk_destruct = tipc_sock_destruct;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400522 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Maloy1b22bca2018-02-26 20:14:04 +0100523 tsk->group_is_open = true;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400524 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700525
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400526 /* Start out with safe limits until we receive an advertised window */
527 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
528 tsk->rcv_win = tsk->snd_win;
529
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +0100530 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400531 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700532 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400533 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700534 }
Jon Maloy2948a1f2019-07-30 20:19:10 +0200535 __skb_queue_head_init(&tsk->mc_method.deferredq);
Tuong Lien01e661e2018-12-19 09:17:58 +0700536 trace_tipc_sk_create(sk, NULL, TIPC_DUMP_NONE, " ");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 return 0;
538}
539
Ying Xue07f6c4b2015-01-07 13:41:58 +0800540static void tipc_sk_callback(struct rcu_head *head)
541{
542 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
543
544 sock_put(&tsk->sk);
545}
546
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100547/* Caller should hold socket lock for the socket. */
548static void __tipc_shutdown(struct socket *sock, int error)
549{
550 struct sock *sk = sock->sk;
551 struct tipc_sock *tsk = tipc_sk(sk);
552 struct net *net = sock_net(sk);
Tung Nguyen12db3c82019-11-28 10:10:07 +0700553 long timeout = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100554 u32 dnode = tsk_peer_node(tsk);
555 struct sk_buff *skb;
556
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500557 /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
558 tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
559 !tsk_conn_cong(tsk)));
560
Tung Nguyend34910e2019-11-28 10:10:08 +0700561 /* Push out delayed messages if in Nagle mode */
Tuong Lien0a3e0602020-05-26 16:38:38 +0700562 tipc_sk_push_backlog(tsk, false);
Tung Nguyend34910e2019-11-28 10:10:08 +0700563 /* Remove pending SYN */
564 __skb_queue_purge(&sk->sk_write_queue);
Tung Nguyen67879272018-09-28 20:23:22 +0200565
Tuong Lien49afb802020-01-08 09:18:15 +0700566 /* Remove partially received buffer if any */
567 skb = skb_peek(&sk->sk_receive_queue);
568 if (skb && TIPC_SKB_CB(skb)->bytes_read) {
569 __skb_unlink(skb, &sk->sk_receive_queue);
570 kfree_skb(skb);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100571 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500572
Tuong Lien49afb802020-01-08 09:18:15 +0700573 /* Reject all unreceived messages if connectionless */
574 if (tipc_sk_type_connectionless(sk)) {
575 tsk_rej_rx_queue(sk, error);
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500576 return;
Tuong Lien49afb802020-01-08 09:18:15 +0700577 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500578
Tuong Lien49afb802020-01-08 09:18:15 +0700579 switch (sk->sk_state) {
580 case TIPC_CONNECTING:
581 case TIPC_ESTABLISHED:
582 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
583 tipc_node_remove_conn(net, dnode, tsk->portid);
584 /* Send a FIN+/- to its peer */
585 skb = __skb_dequeue(&sk->sk_receive_queue);
586 if (skb) {
587 __skb_queue_purge(&sk->sk_receive_queue);
588 tipc_sk_respond(sk, skb, error);
589 break;
590 }
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100591 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
592 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
593 tsk_own_node(tsk), tsk_peer_port(tsk),
594 tsk->portid, error);
595 if (skb)
596 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Tuong Lien49afb802020-01-08 09:18:15 +0700597 break;
598 case TIPC_LISTEN:
599 /* Reject all SYN messages */
600 tsk_rej_rx_queue(sk, error);
601 break;
602 default:
603 __skb_queue_purge(&sk->sk_receive_queue);
604 break;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100605 }
606}
607
Ying Xuec5fa7b32013-06-17 10:54:39 -0400608/**
Ying Xue247f0f32014-02-18 16:06:46 +0800609 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100610 * @sock: socket to destroy
611 *
612 * This routine cleans up any messages that are still queued on the socket.
613 * For DGRAM and RDM socket types, all queued messages are rejected.
614 * For SEQPACKET and STREAM socket types, the first message is rejected
615 * and any others are discarded. (If the first message on a STREAM socket
616 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900617 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618 * NOTE: Rejected messages are not necessarily returned to the sender! They
619 * are returned or discarded according to the "destination droppable" setting
620 * specified for the message by the sender.
621 *
Randy Dunlap637b77f2020-11-29 10:32:48 -0800622 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623 */
Ying Xue247f0f32014-02-18 16:06:46 +0800624static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100625{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100626 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400627 struct tipc_sock *tsk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100628
Allan Stephens0c3141e2008-04-15 00:22:02 -0700629 /*
630 * Exit if socket isn't fully initialized (occurs when a failed accept()
631 * releases a pre-allocated child socket that was never used)
632 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700633 if (sk == NULL)
634 return 0;
635
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400636 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700637 lock_sock(sk);
638
Tuong Lien01e661e2018-12-19 09:17:58 +0700639 trace_tipc_sk_release(sk, NULL, TIPC_DUMP_ALL, " ");
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100640 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
641 sk->sk_shutdown = SHUTDOWN_MASK;
Jon Maloy75da2162017-10-13 11:04:23 +0200642 tipc_sk_leave(tsk);
Jon Maloy2c98da02021-03-16 22:06:13 -0400643 tipc_sk_withdraw(tsk, NULL);
Hoang Lec55c8ed2019-03-19 18:49:50 +0700644 __skb_queue_purge(&tsk->mc_method.deferredq);
Ying Xue1ea23a22015-05-28 13:19:22 +0800645 sk_stop_timer(sk, &sk->sk_timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800646 tipc_sk_remove(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100647
Cong Wang0a3b8b22018-09-03 19:12:41 -0700648 sock_orphan(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700649 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700650 release_sock(sk);
Jon Maloya80ae532017-10-13 11:04:22 +0200651 tipc_dest_list_purge(&tsk->cong_links);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500652 tsk->cong_link_cnt = 0;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800653 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700654 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100655
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200656 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657}
658
659/**
Jon Maloy60c102e2020-11-25 13:29:13 -0500660 * __tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661 * @sock: socket structure
Jon Maloy60c102e2020-11-25 13:29:13 -0500662 * @skaddr: socket address describing name(s) and desired operation
663 * @alen: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900664 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100665 * Name and name sequence binding is indicated using a positive scope value;
666 * a negative scope value unbinds the specified name. Specifying no name
667 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900668 *
Randy Dunlap637b77f2020-11-29 10:32:48 -0800669 * Return: 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700670 *
671 * NOTE: This routine doesn't need to take the socket lock since it doesn't
672 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673 */
Jon Maloy60c102e2020-11-25 13:29:13 -0500674static int __tipc_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675{
Jon Maloy50a34992021-03-16 22:06:11 -0400676 struct tipc_uaddr *ua = (struct tipc_uaddr *)skaddr;
Jon Maloy60c102e2020-11-25 13:29:13 -0500677 struct tipc_sock *tsk = tipc_sk(sock->sk);
Jon Maloy50a34992021-03-16 22:06:11 -0400678 bool unbind = false;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100679
Jon Maloy60c102e2020-11-25 13:29:13 -0500680 if (unlikely(!alen))
Jon Maloy2c98da02021-03-16 22:06:13 -0400681 return tipc_sk_withdraw(tsk, NULL);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100682
Jon Maloy50a34992021-03-16 22:06:11 -0400683 if (ua->addrtype == TIPC_SERVICE_ADDR) {
684 ua->addrtype = TIPC_SERVICE_RANGE;
685 ua->sr.upper = ua->sr.lower;
686 }
687 if (ua->scope < 0) {
688 unbind = true;
689 ua->scope = -ua->scope;
690 }
691 /* Users may still use deprecated TIPC_ZONE_SCOPE */
692 if (ua->scope != TIPC_NODE_SCOPE)
693 ua->scope = TIPC_CLUSTER_SCOPE;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900694
Jon Maloy60c102e2020-11-25 13:29:13 -0500695 if (tsk->group)
696 return -EACCES;
697
Jon Maloy50a34992021-03-16 22:06:11 -0400698 if (unbind)
Jon Maloy2c98da02021-03-16 22:06:13 -0400699 return tipc_sk_withdraw(tsk, ua);
Jon Maloy50a34992021-03-16 22:06:11 -0400700 return tipc_sk_publish(tsk, ua);
Jon Maloy60c102e2020-11-25 13:29:13 -0500701}
702
703int tipc_sk_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
704{
705 int res;
706
707 lock_sock(sock->sk);
708 res = __tipc_bind(sock, skaddr, alen);
709 release_sock(sock->sk);
Ying Xue84602762013-12-27 10:18:28 +0800710 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100711}
712
Jon Maloy72671b32020-10-29 21:29:38 -0400713static int tipc_bind(struct socket *sock, struct sockaddr *skaddr, int alen)
714{
Jon Maloy50a34992021-03-16 22:06:11 -0400715 struct tipc_uaddr *ua = (struct tipc_uaddr *)skaddr;
716 u32 atype = ua->addrtype;
Jon Maloy72671b32020-10-29 21:29:38 -0400717
718 if (alen) {
Jon Maloy50a34992021-03-16 22:06:11 -0400719 if (!tipc_uaddr_valid(ua, alen))
Jon Maloy72671b32020-10-29 21:29:38 -0400720 return -EINVAL;
Jon Maloy50a34992021-03-16 22:06:11 -0400721 if (atype == TIPC_SOCKET_ADDR)
Jon Maloy60c102e2020-11-25 13:29:13 -0500722 return -EAFNOSUPPORT;
Jon Maloy50a34992021-03-16 22:06:11 -0400723 if (ua->sr.type < TIPC_RESERVED_TYPES) {
Jon Maloy72671b32020-10-29 21:29:38 -0400724 pr_warn_once("Can't bind to reserved service type %u\n",
Jon Maloy50a34992021-03-16 22:06:11 -0400725 ua->sr.type);
Jon Maloy72671b32020-10-29 21:29:38 -0400726 return -EACCES;
727 }
728 }
729 return tipc_sk_bind(sock, skaddr, alen);
730}
731
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900732/**
Ying Xue247f0f32014-02-18 16:06:46 +0800733 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100734 * @sock: socket structure
735 * @uaddr: area for returned socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700736 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900737 *
Randy Dunlap637b77f2020-11-29 10:32:48 -0800738 * Return: 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700739 *
Allan Stephens2da59912008-07-14 22:43:32 -0700740 * NOTE: This routine doesn't need to take the socket lock since it only
741 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000742 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100743 */
Ying Xue247f0f32014-02-18 16:06:46 +0800744static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100745 int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100746{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100747 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100748 struct sock *sk = sock->sk;
749 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000751 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700752 if (peer) {
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100753 if ((!tipc_sk_connected(sk)) &&
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100754 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
Allan Stephens2da59912008-07-14 22:43:32 -0700755 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400756 addr->addr.id.ref = tsk_peer_port(tsk);
757 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700758 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800759 addr->addr.id.ref = tsk->portid;
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100760 addr->addr.id.node = tipc_own_addr(sock_net(sk));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700761 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100762
Jon Maloyb6f88d92020-11-25 13:29:15 -0500763 addr->addrtype = TIPC_SOCKET_ADDR;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100764 addr->family = AF_TIPC;
765 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100766 addr->addr.name.domain = 0;
767
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100768 return sizeof(*addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100769}
770
771/**
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700772 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 * @file: file structure associated with the socket
774 * @sock: socket for which to calculate the poll bits
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700775 * @wait: ???
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776 *
Randy Dunlap637b77f2020-11-29 10:32:48 -0800777 * Return: pollmask value
Allan Stephens9b674e82008-03-26 16:48:21 -0700778 *
779 * COMMENTARY:
780 * It appears that the usual socket locking mechanisms are not useful here
781 * since the pollmask info is potentially out-of-date the moment this routine
782 * exits. TCP and other protocols seem to rely on higher level poll routines
783 * to handle any preventable race conditions, so TIPC will do the same ...
784 *
Allan Stephensf662c072010-08-17 11:00:06 +0000785 * IMPORTANT: The fact that a read or write operation is indicated does NOT
786 * imply that the operation will succeed, merely that it should be performed
787 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788 */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700789static __poll_t tipc_poll(struct file *file, struct socket *sock,
790 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100791{
Allan Stephens9b674e82008-03-26 16:48:21 -0700792 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400793 struct tipc_sock *tsk = tipc_sk(sk);
Al Viroade994f2017-07-03 00:01:49 -0400794 __poll_t revents = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700795
Karsten Graul89ab0662018-10-23 13:40:39 +0200796 sock_poll_wait(file, sock, wait);
Tuong Lien01e661e2018-12-19 09:17:58 +0700797 trace_tipc_sk_poll(sk, NULL, TIPC_DUMP_ALL, " ");
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700798
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100799 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800800 revents |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100801 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800802 revents |= EPOLLHUP;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100803
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100804 switch (sk->sk_state) {
805 case TIPC_ESTABLISHED:
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500806 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800807 revents |= EPOLLOUT;
Miaohe Lin7f8901b2020-08-18 08:07:13 -0400808 fallthrough;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100809 case TIPC_LISTEN:
Parthasarathy Bhuvaraganff946832019-05-09 07:13:42 +0200810 case TIPC_CONNECTING:
Eric Dumazet3ef7cf52019-10-23 22:44:50 -0700811 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800812 revents |= EPOLLIN | EPOLLRDNORM;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100813 break;
814 case TIPC_OPEN:
Jon Maloy60c25302018-01-17 16:42:46 +0100815 if (tsk->group_is_open && !tsk->cong_link_cnt)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800816 revents |= EPOLLOUT;
Jon Maloyae236fb2017-10-13 11:04:25 +0200817 if (!tipc_sk_type_connectionless(sk))
818 break;
Eric Dumazet3ef7cf52019-10-23 22:44:50 -0700819 if (skb_queue_empty_lockless(&sk->sk_receive_queue))
Jon Maloyae236fb2017-10-13 11:04:25 +0200820 break;
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800821 revents |= EPOLLIN | EPOLLRDNORM;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100822 break;
823 case TIPC_DISCONNECTING:
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800824 revents = EPOLLIN | EPOLLRDNORM | EPOLLHUP;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100825 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000826 }
Jon Maloyae236fb2017-10-13 11:04:25 +0200827 return revents;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100828}
829
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400830/**
831 * tipc_sendmcast - send multicast message
832 * @sock: socket structure
Jon Maloy833f8672021-03-16 22:06:17 -0400833 * @ua: destination address struct
Al Viro562640f2014-11-15 01:13:43 -0500834 * @msg: message to send
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500835 * @dlen: length of data to send
836 * @timeout: timeout to wait for wakeup
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400837 *
838 * Called from function tipc_sendmsg(), which has done all sanity checks
Randy Dunlap637b77f2020-11-29 10:32:48 -0800839 * Return: the number of bytes sent on success, or errno
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400840 */
Jon Maloy833f8672021-03-16 22:06:17 -0400841static int tipc_sendmcast(struct socket *sock, struct tipc_uaddr *ua,
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500842 struct msghdr *msg, size_t dlen, long timeout)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400843{
844 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500845 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500846 struct tipc_msg *hdr = &tsk->phdr;
Ying Xuef2f98002015-01-09 15:27:05 +0800847 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500848 int mtu = tipc_bcast_get_mtu(net);
849 struct sk_buff_head pkts;
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500850 struct tipc_nlist dsts;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400851 int rc;
852
Jon Maloy75da2162017-10-13 11:04:23 +0200853 if (tsk->group)
854 return -EACCES;
855
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500856 /* Block or return if any destination link is congested */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500857 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
858 if (unlikely(rc))
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400859 return rc;
860
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500861 /* Lookup destination nodes */
862 tipc_nlist_init(&dsts, tipc_own_addr(net));
Jon Maloy833f8672021-03-16 22:06:17 -0400863 tipc_nametbl_lookup_mcast_nodes(net, ua, &dsts);
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500864 if (!dsts.local && !dsts.remote)
865 return -EHOSTUNREACH;
866
867 /* Build message header */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500868 msg_set_type(hdr, TIPC_MCAST_MSG);
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500869 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500870 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
871 msg_set_destport(hdr, 0);
872 msg_set_destnode(hdr, 0);
Jon Maloy833f8672021-03-16 22:06:17 -0400873 msg_set_nametype(hdr, ua->sr.type);
874 msg_set_namelower(hdr, ua->sr.lower);
875 msg_set_nameupper(hdr, ua->sr.upper);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400876
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500877 /* Build message as chain of buffers */
Jon Maloye654f9f2019-08-15 16:42:50 +0200878 __skb_queue_head_init(&pkts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500879 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500880
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500881 /* Send message if build was successful */
Tuong Lien01e661e2018-12-19 09:17:58 +0700882 if (unlikely(rc == dlen)) {
883 trace_tipc_sk_sendmcast(sk, skb_peek(&pkts),
884 TIPC_DUMP_SK_SNDQ, " ");
Jon Maloy833f8672021-03-16 22:06:17 -0400885 rc = tipc_mcast_xmit(net, &pkts, &tsk->mc_method, &dsts,
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500886 &tsk->cong_link_cnt);
Tuong Lien01e661e2018-12-19 09:17:58 +0700887 }
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500888
889 tipc_nlist_purge(&dsts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500890
891 return rc ? rc : dlen;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400892}
893
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500894/**
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200895 * tipc_send_group_msg - send a message to a member in the group
896 * @net: network namespace
Randy Dunlapf172f4b2020-11-29 10:32:49 -0800897 * @tsk: tipc socket
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200898 * @m: message to send
899 * @mb: group member
900 * @dnode: destination node
901 * @dport: destination port
902 * @dlen: total length of message data
903 */
904static int tipc_send_group_msg(struct net *net, struct tipc_sock *tsk,
905 struct msghdr *m, struct tipc_member *mb,
906 u32 dnode, u32 dport, int dlen)
907{
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200908 u16 bc_snd_nxt = tipc_group_bc_snd_nxt(tsk->group);
Jon Maloy2f487712017-10-13 11:04:31 +0200909 struct tipc_mc_method *method = &tsk->mc_method;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200910 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
911 struct tipc_msg *hdr = &tsk->phdr;
912 struct sk_buff_head pkts;
913 int mtu, rc;
914
915 /* Complete message header */
916 msg_set_type(hdr, TIPC_GRP_UCAST_MSG);
917 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
918 msg_set_destport(hdr, dport);
919 msg_set_destnode(hdr, dnode);
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200920 msg_set_grp_bc_seqno(hdr, bc_snd_nxt);
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200921
922 /* Build message as chain of buffers */
Jon Maloye654f9f2019-08-15 16:42:50 +0200923 __skb_queue_head_init(&pkts);
Hoang Lef73b1282019-10-29 07:51:21 +0700924 mtu = tipc_node_get_mtu(net, dnode, tsk->portid, false);
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200925 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
926 if (unlikely(rc != dlen))
927 return rc;
928
929 /* Send message */
930 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
931 if (unlikely(rc == -ELINKCONG)) {
932 tipc_dest_push(&tsk->cong_links, dnode, 0);
933 tsk->cong_link_cnt++;
934 }
935
Jon Maloy2f487712017-10-13 11:04:31 +0200936 /* Update send window */
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200937 tipc_group_update_member(mb, blks);
938
Jon Maloy2f487712017-10-13 11:04:31 +0200939 /* A broadcast sent within next EXPIRE period must follow same path */
940 method->rcast = true;
941 method->mandatory = true;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200942 return dlen;
943}
944
945/**
946 * tipc_send_group_unicast - send message to a member in the group
947 * @sock: socket structure
948 * @m: message to send
949 * @dlen: total length of message data
950 * @timeout: timeout to wait for wakeup
951 *
952 * Called from function tipc_sendmsg(), which has done all sanity checks
Randy Dunlap637b77f2020-11-29 10:32:48 -0800953 * Return: the number of bytes sent on success, or errno
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200954 */
955static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m,
956 int dlen, long timeout)
957{
958 struct sock *sk = sock->sk;
Jon Maloy006ed142021-03-16 22:06:18 -0400959 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200960 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
961 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200962 struct net *net = sock_net(sk);
963 struct tipc_member *mb = NULL;
964 u32 node, port;
965 int rc;
966
Jon Maloy006ed142021-03-16 22:06:18 -0400967 node = ua->sk.node;
968 port = ua->sk.ref;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200969 if (!port && !node)
970 return -EHOSTUNREACH;
971
972 /* Block or return if destination link or member is congested */
973 rc = tipc_wait_for_cond(sock, &timeout,
974 !tipc_dest_find(&tsk->cong_links, node, 0) &&
Cong Wang143ece62018-12-11 21:43:51 -0800975 tsk->group &&
976 !tipc_group_cong(tsk->group, node, port, blks,
977 &mb));
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200978 if (unlikely(rc))
979 return rc;
980
981 if (unlikely(!mb))
982 return -EHOSTUNREACH;
983
984 rc = tipc_send_group_msg(net, tsk, m, mb, node, port, dlen);
985
986 return rc ? rc : dlen;
987}
988
989/**
Jon Maloyee106d72017-10-13 11:04:28 +0200990 * tipc_send_group_anycast - send message to any member with given identity
991 * @sock: socket structure
992 * @m: message to send
993 * @dlen: total length of message data
994 * @timeout: timeout to wait for wakeup
995 *
996 * Called from function tipc_sendmsg(), which has done all sanity checks
Randy Dunlap637b77f2020-11-29 10:32:48 -0800997 * Return: the number of bytes sent on success, or errno
Jon Maloyee106d72017-10-13 11:04:28 +0200998 */
999static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m,
1000 int dlen, long timeout)
1001{
Jon Maloy006ed142021-03-16 22:06:18 -04001002 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
Jon Maloyee106d72017-10-13 11:04:28 +02001003 struct sock *sk = sock->sk;
1004 struct tipc_sock *tsk = tipc_sk(sk);
1005 struct list_head *cong_links = &tsk->cong_links;
1006 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
Jon Maloy232d07b2018-01-08 21:03:30 +01001007 struct tipc_msg *hdr = &tsk->phdr;
Jon Maloyee106d72017-10-13 11:04:28 +02001008 struct tipc_member *first = NULL;
1009 struct tipc_member *mbr = NULL;
1010 struct net *net = sock_net(sk);
1011 u32 node, port, exclude;
Jon Maloyee106d72017-10-13 11:04:28 +02001012 struct list_head dsts;
1013 int lookups = 0;
1014 int dstcnt, rc;
1015 bool cong;
1016
1017 INIT_LIST_HEAD(&dsts);
Jon Maloy006ed142021-03-16 22:06:18 -04001018 ua->sa.type = msg_nametype(hdr);
1019 ua->scope = msg_lookup_scope(hdr);
Jon Maloyee106d72017-10-13 11:04:28 +02001020
1021 while (++lookups < 4) {
Cong Wang143ece62018-12-11 21:43:51 -08001022 exclude = tipc_group_exclude(tsk->group);
1023
Jon Maloyee106d72017-10-13 11:04:28 +02001024 first = NULL;
1025
1026 /* Look for a non-congested destination member, if any */
1027 while (1) {
Jon Maloy006ed142021-03-16 22:06:18 -04001028 if (!tipc_nametbl_lookup_group(net, ua, &dsts, &dstcnt,
1029 exclude, false))
Jon Maloyee106d72017-10-13 11:04:28 +02001030 return -EHOSTUNREACH;
1031 tipc_dest_pop(&dsts, &node, &port);
Cong Wang143ece62018-12-11 21:43:51 -08001032 cong = tipc_group_cong(tsk->group, node, port, blks,
1033 &mbr);
Jon Maloyee106d72017-10-13 11:04:28 +02001034 if (!cong)
1035 break;
1036 if (mbr == first)
1037 break;
1038 if (!first)
1039 first = mbr;
1040 }
1041
1042 /* Start over if destination was not in member list */
1043 if (unlikely(!mbr))
1044 continue;
1045
1046 if (likely(!cong && !tipc_dest_find(cong_links, node, 0)))
1047 break;
1048
1049 /* Block or return if destination link or member is congested */
1050 rc = tipc_wait_for_cond(sock, &timeout,
1051 !tipc_dest_find(cong_links, node, 0) &&
Cong Wang143ece62018-12-11 21:43:51 -08001052 tsk->group &&
1053 !tipc_group_cong(tsk->group, node, port,
Jon Maloyee106d72017-10-13 11:04:28 +02001054 blks, &mbr));
1055 if (unlikely(rc))
1056 return rc;
1057
1058 /* Send, unless destination disappeared while waiting */
1059 if (likely(mbr))
1060 break;
1061 }
1062
1063 if (unlikely(lookups >= 4))
1064 return -EHOSTUNREACH;
1065
1066 rc = tipc_send_group_msg(net, tsk, m, mbr, node, port, dlen);
1067
1068 return rc ? rc : dlen;
1069}
1070
1071/**
Jon Maloy75da2162017-10-13 11:04:23 +02001072 * tipc_send_group_bcast - send message to all members in communication group
Andrew Lunnd8141202020-07-13 01:15:14 +02001073 * @sock: socket structure
Jon Maloy75da2162017-10-13 11:04:23 +02001074 * @m: message to send
1075 * @dlen: total length of message data
1076 * @timeout: timeout to wait for wakeup
1077 *
1078 * Called from function tipc_sendmsg(), which has done all sanity checks
Randy Dunlap637b77f2020-11-29 10:32:48 -08001079 * Return: the number of bytes sent on success, or errno
Jon Maloy75da2162017-10-13 11:04:23 +02001080 */
1081static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m,
1082 int dlen, long timeout)
1083{
Jon Maloy006ed142021-03-16 22:06:18 -04001084 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
Jon Maloy75da2162017-10-13 11:04:23 +02001085 struct sock *sk = sock->sk;
1086 struct net *net = sock_net(sk);
1087 struct tipc_sock *tsk = tipc_sk(sk);
Cong Wang3c6306d2018-12-16 23:25:12 -08001088 struct tipc_nlist *dsts;
Jon Maloy75da2162017-10-13 11:04:23 +02001089 struct tipc_mc_method *method = &tsk->mc_method;
Jon Maloy2f487712017-10-13 11:04:31 +02001090 bool ack = method->mandatory && method->rcast;
Jon Maloyb7d42632017-10-13 11:04:26 +02001091 int blks = tsk_blocks(MCAST_H_SIZE + dlen);
Jon Maloy75da2162017-10-13 11:04:23 +02001092 struct tipc_msg *hdr = &tsk->phdr;
1093 int mtu = tipc_bcast_get_mtu(net);
1094 struct sk_buff_head pkts;
1095 int rc = -EHOSTUNREACH;
1096
Jon Maloyb7d42632017-10-13 11:04:26 +02001097 /* Block or return if any destination link or member is congested */
Cong Wang143ece62018-12-11 21:43:51 -08001098 rc = tipc_wait_for_cond(sock, &timeout,
1099 !tsk->cong_link_cnt && tsk->group &&
1100 !tipc_group_bc_cong(tsk->group, blks));
Jon Maloy75da2162017-10-13 11:04:23 +02001101 if (unlikely(rc))
1102 return rc;
1103
Cong Wang3c6306d2018-12-16 23:25:12 -08001104 dsts = tipc_group_dests(tsk->group);
1105 if (!dsts->local && !dsts->remote)
1106 return -EHOSTUNREACH;
1107
Jon Maloy75da2162017-10-13 11:04:23 +02001108 /* Complete message header */
Jon Maloy006ed142021-03-16 22:06:18 -04001109 if (ua) {
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001110 msg_set_type(hdr, TIPC_GRP_MCAST_MSG);
Jon Maloy006ed142021-03-16 22:06:18 -04001111 msg_set_nameinst(hdr, ua->sa.instance);
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001112 } else {
1113 msg_set_type(hdr, TIPC_GRP_BCAST_MSG);
1114 msg_set_nameinst(hdr, 0);
1115 }
Jon Maloyb7d42632017-10-13 11:04:26 +02001116 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
Jon Maloy75da2162017-10-13 11:04:23 +02001117 msg_set_destport(hdr, 0);
1118 msg_set_destnode(hdr, 0);
Cong Wang143ece62018-12-11 21:43:51 -08001119 msg_set_grp_bc_seqno(hdr, tipc_group_bc_snd_nxt(tsk->group));
Jon Maloy75da2162017-10-13 11:04:23 +02001120
Jon Maloy2f487712017-10-13 11:04:31 +02001121 /* Avoid getting stuck with repeated forced replicasts */
1122 msg_set_grp_bc_ack_req(hdr, ack);
1123
Jon Maloy75da2162017-10-13 11:04:23 +02001124 /* Build message as chain of buffers */
Jon Maloye654f9f2019-08-15 16:42:50 +02001125 __skb_queue_head_init(&pkts);
Jon Maloy75da2162017-10-13 11:04:23 +02001126 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1127 if (unlikely(rc != dlen))
1128 return rc;
1129
1130 /* Send message */
Jon Maloy2f487712017-10-13 11:04:31 +02001131 rc = tipc_mcast_xmit(net, &pkts, method, dsts, &tsk->cong_link_cnt);
Jon Maloy75da2162017-10-13 11:04:23 +02001132 if (unlikely(rc))
1133 return rc;
1134
Jon Maloyb7d42632017-10-13 11:04:26 +02001135 /* Update broadcast sequence number and send windows */
Jon Maloy2f487712017-10-13 11:04:31 +02001136 tipc_group_update_bc_members(tsk->group, blks, ack);
1137
1138 /* Broadcast link is now free to choose method for next broadcast */
1139 method->mandatory = false;
1140 method->expires = jiffies;
1141
Jon Maloy75da2162017-10-13 11:04:23 +02001142 return dlen;
1143}
1144
1145/**
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001146 * tipc_send_group_mcast - send message to all members with given identity
1147 * @sock: socket structure
1148 * @m: message to send
1149 * @dlen: total length of message data
1150 * @timeout: timeout to wait for wakeup
1151 *
1152 * Called from function tipc_sendmsg(), which has done all sanity checks
Randy Dunlap637b77f2020-11-29 10:32:48 -08001153 * Return: the number of bytes sent on success, or errno
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001154 */
1155static int tipc_send_group_mcast(struct socket *sock, struct msghdr *m,
1156 int dlen, long timeout)
1157{
Jon Maloy006ed142021-03-16 22:06:18 -04001158 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001159 struct sock *sk = sock->sk;
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001160 struct tipc_sock *tsk = tipc_sk(sk);
1161 struct tipc_group *grp = tsk->group;
Jon Maloy232d07b2018-01-08 21:03:30 +01001162 struct tipc_msg *hdr = &tsk->phdr;
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001163 struct net *net = sock_net(sk);
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001164 struct list_head dsts;
Jon Maloy006ed142021-03-16 22:06:18 -04001165 u32 dstcnt, exclude;
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001166
1167 INIT_LIST_HEAD(&dsts);
Jon Maloy006ed142021-03-16 22:06:18 -04001168 ua->sa.type = msg_nametype(hdr);
1169 ua->scope = msg_lookup_scope(hdr);
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001170 exclude = tipc_group_exclude(grp);
Jon Maloy232d07b2018-01-08 21:03:30 +01001171
Jon Maloy006ed142021-03-16 22:06:18 -04001172 if (!tipc_nametbl_lookup_group(net, ua, &dsts, &dstcnt, exclude, true))
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001173 return -EHOSTUNREACH;
1174
1175 if (dstcnt == 1) {
Jon Maloy006ed142021-03-16 22:06:18 -04001176 tipc_dest_pop(&dsts, &ua->sk.node, &ua->sk.ref);
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001177 return tipc_send_group_unicast(sock, m, dlen, timeout);
1178 }
1179
1180 tipc_dest_list_purge(&dsts);
1181 return tipc_send_group_bcast(sock, m, dlen, timeout);
1182}
1183
1184/**
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001185 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
Randy Dunlapf172f4b2020-11-29 10:32:49 -08001186 * @net: the associated network namespace
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001187 * @arrvq: queue with arriving messages, to be cloned after destination lookup
1188 * @inputq: queue with cloned messages, delivered to socket after dest lookup
1189 *
1190 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001191 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001192void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
1193 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001194{
Jon Maloy75da2162017-10-13 11:04:23 +02001195 u32 self = tipc_own_addr(net);
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001196 struct sk_buff *skb, *_skb;
Colin Ian Kingb053fcc2018-07-31 17:01:37 +01001197 u32 portid, onode;
Jon Maloy232d07b2018-01-08 21:03:30 +01001198 struct sk_buff_head tmpq;
Jon Maloy75da2162017-10-13 11:04:23 +02001199 struct list_head dports;
Jon Maloy232d07b2018-01-08 21:03:30 +01001200 struct tipc_msg *hdr;
Jon Maloy45ceea22021-03-16 22:06:16 -04001201 struct tipc_uaddr ua;
Jon Maloy232d07b2018-01-08 21:03:30 +01001202 int user, mtyp, hlen;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001203
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001204 __skb_queue_head_init(&tmpq);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001205 INIT_LIST_HEAD(&dports);
Jon Maloy45ceea22021-03-16 22:06:16 -04001206 ua.addrtype = TIPC_SERVICE_RANGE;
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001207
Jon Maloy5ef21322021-06-02 13:44:26 -04001208 /* tipc_skb_peek() increments the head skb's reference counter */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001209 skb = tipc_skb_peek(arrvq, &inputq->lock);
1210 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
Jon Maloy232d07b2018-01-08 21:03:30 +01001211 hdr = buf_msg(skb);
1212 user = msg_user(hdr);
1213 mtyp = msg_type(hdr);
1214 hlen = skb_headroom(skb) + msg_hdr_sz(hdr);
Jon Maloy232d07b2018-01-08 21:03:30 +01001215 onode = msg_orignode(hdr);
Jon Maloy45ceea22021-03-16 22:06:16 -04001216 ua.sr.type = msg_nametype(hdr);
Jon Maloy5ef21322021-06-02 13:44:26 -04001217 ua.sr.lower = msg_namelower(hdr);
1218 ua.sr.upper = msg_nameupper(hdr);
1219 if (onode == self)
1220 ua.scope = TIPC_ANY_SCOPE;
1221 else
1222 ua.scope = TIPC_CLUSTER_SCOPE;
Jon Maloy232d07b2018-01-08 21:03:30 +01001223
Jon Maloy2f487712017-10-13 11:04:31 +02001224 if (mtyp == TIPC_GRP_UCAST_MSG || user == GROUP_PROTOCOL) {
1225 spin_lock_bh(&inputq->lock);
1226 if (skb_peek(arrvq) == skb) {
1227 __skb_dequeue(arrvq);
1228 __skb_queue_tail(inputq, skb);
1229 }
Jon Maloyc545a942017-12-11 19:11:55 +01001230 kfree_skb(skb);
Jon Maloy2f487712017-10-13 11:04:31 +02001231 spin_unlock_bh(&inputq->lock);
1232 continue;
1233 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001234
Jon Maloy232d07b2018-01-08 21:03:30 +01001235 /* Group messages require exact scope match */
1236 if (msg_in_group(hdr)) {
Jon Maloy45ceea22021-03-16 22:06:16 -04001237 ua.sr.lower = 0;
1238 ua.sr.upper = ~0;
1239 ua.scope = msg_lookup_scope(hdr);
Jon Maloy75da2162017-10-13 11:04:23 +02001240 }
Jon Maloy232d07b2018-01-08 21:03:30 +01001241
1242 /* Create destination port list: */
Jon Maloy5ef21322021-06-02 13:44:26 -04001243 tipc_nametbl_lookup_mcast_sockets(net, &ua, &dports);
Jon Maloy232d07b2018-01-08 21:03:30 +01001244
1245 /* Clone message per destination */
Jon Maloya80ae532017-10-13 11:04:22 +02001246 while (tipc_dest_pop(&dports, NULL, &portid)) {
Jon Maloy232d07b2018-01-08 21:03:30 +01001247 _skb = __pskb_copy(skb, hlen, GFP_ATOMIC);
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001248 if (_skb) {
1249 msg_set_destport(buf_msg(_skb), portid);
1250 __skb_queue_tail(&tmpq, _skb);
1251 continue;
1252 }
1253 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001254 }
Jon Maloy5ef21322021-06-02 13:44:26 -04001255 /* Append clones to inputq only if skb is still head of arrvq */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001256 spin_lock_bh(&inputq->lock);
1257 if (skb_peek(arrvq) == skb) {
1258 skb_queue_splice_tail_init(&tmpq, inputq);
Jon Maloy5ef21322021-06-02 13:44:26 -04001259 /* Decrement the skb's refcnt */
Hoang Le75016892021-05-14 08:23:03 +07001260 kfree_skb(__skb_dequeue(arrvq));
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001261 }
1262 spin_unlock_bh(&inputq->lock);
1263 __skb_queue_purge(&tmpq);
1264 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001265 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001266 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001267}
1268
Jon Maloyc0bceb92019-10-30 14:00:41 +01001269/* tipc_sk_push_backlog(): send accumulated buffers in socket write queue
1270 * when socket is in Nagle mode
1271 */
Tuong Lien0a3e0602020-05-26 16:38:38 +07001272static void tipc_sk_push_backlog(struct tipc_sock *tsk, bool nagle_ack)
Jon Maloyc0bceb92019-10-30 14:00:41 +01001273{
1274 struct sk_buff_head *txq = &tsk->sk.sk_write_queue;
Tuong Lien0a3e0602020-05-26 16:38:38 +07001275 struct sk_buff *skb = skb_peek_tail(txq);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001276 struct net *net = sock_net(&tsk->sk);
1277 u32 dnode = tsk_peer_node(tsk);
1278 int rc;
1279
Tuong Lien0a3e0602020-05-26 16:38:38 +07001280 if (nagle_ack) {
1281 tsk->pkt_cnt += skb_queue_len(txq);
1282 if (!tsk->pkt_cnt || tsk->msg_acc / tsk->pkt_cnt < 2) {
1283 tsk->oneway = 0;
1284 if (tsk->nagle_start < NAGLE_START_MAX)
1285 tsk->nagle_start *= 2;
1286 tsk->expect_ack = false;
1287 pr_debug("tsk %10u: bad nagle %u -> %u, next start %u!\n",
1288 tsk->portid, tsk->msg_acc, tsk->pkt_cnt,
1289 tsk->nagle_start);
1290 } else {
1291 tsk->nagle_start = NAGLE_START_INIT;
1292 if (skb) {
1293 msg_set_ack_required(buf_msg(skb));
1294 tsk->expect_ack = true;
1295 } else {
1296 tsk->expect_ack = false;
1297 }
1298 }
1299 tsk->msg_acc = 0;
1300 tsk->pkt_cnt = 0;
1301 }
1302
Tung Nguyend34910e2019-11-28 10:10:08 +07001303 if (!skb || tsk->cong_link_cnt)
1304 return;
1305
1306 /* Do not send SYN again after congestion */
1307 if (msg_is_syn(buf_msg(skb)))
Jon Maloyc0bceb92019-10-30 14:00:41 +01001308 return;
1309
Tuong Lien0a3e0602020-05-26 16:38:38 +07001310 if (tsk->msg_acc)
1311 tsk->pkt_cnt += skb_queue_len(txq);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001312 tsk->snt_unacked += tsk->snd_backlog;
1313 tsk->snd_backlog = 0;
Jon Maloyc0bceb92019-10-30 14:00:41 +01001314 rc = tipc_node_xmit(net, txq, dnode, tsk->portid);
1315 if (rc == -ELINKCONG)
1316 tsk->cong_link_cnt = 1;
1317}
1318
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001319/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02001320 * tipc_sk_conn_proto_rcv - receive a connection mng protocol message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001321 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001322 * @skb: pointer to message buffer.
Randy Dunlapf172f4b2020-11-29 10:32:49 -08001323 * @inputq: buffer list containing the buffers
1324 * @xmitq: output message area
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001325 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02001326static void tipc_sk_conn_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
Parthasarathy Bhuvaragane7eb0582018-10-10 17:50:23 +02001327 struct sk_buff_head *inputq,
Jon Maloy64ac5f52017-10-13 11:04:20 +02001328 struct sk_buff_head *xmitq)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001329{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001330 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy64ac5f52017-10-13 11:04:20 +02001331 u32 onode = tsk_own_node(tsk);
1332 struct sock *sk = &tsk->sk;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001333 int mtyp = msg_type(hdr);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001334 bool was_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001335
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001336 /* Ignore if connection cannot be validated: */
Tuong Lien01e661e2018-12-19 09:17:58 +07001337 if (!tsk_peer_msg(tsk, hdr)) {
1338 trace_tipc_sk_drop_msg(sk, skb, TIPC_DUMP_NONE, "@proto_rcv!");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001339 goto exit;
Tuong Lien01e661e2018-12-19 09:17:58 +07001340 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001341
Parthasarathy Bhuvaraganc1be7752017-04-26 10:05:02 +02001342 if (unlikely(msg_errcode(hdr))) {
1343 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1344 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
1345 tsk_peer_port(tsk));
1346 sk->sk_state_change(sk);
Parthasarathy Bhuvaragane7eb0582018-10-10 17:50:23 +02001347
1348 /* State change is ignored if socket already awake,
1349 * - convert msg to abort msg and add to inqueue
1350 */
1351 msg_set_user(hdr, TIPC_CRITICAL_IMPORTANCE);
1352 msg_set_type(hdr, TIPC_CONN_MSG);
1353 msg_set_size(hdr, BASIC_H_SIZE);
1354 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
1355 __skb_queue_tail(inputq, skb);
1356 return;
Parthasarathy Bhuvaraganc1be7752017-04-26 10:05:02 +02001357 }
1358
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001359 tsk->probe_unacked = false;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001360
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001361 if (mtyp == CONN_PROBE) {
1362 msg_set_type(hdr, CONN_PROBE_REPLY);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001363 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
1364 __skb_queue_tail(xmitq, skb);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001365 return;
1366 } else if (mtyp == CONN_ACK) {
Jon Maloyc0bceb92019-10-30 14:00:41 +01001367 was_cong = tsk_conn_cong(tsk);
Tuong Lien0a3e0602020-05-26 16:38:38 +07001368 tipc_sk_push_backlog(tsk, msg_nagle_ack(hdr));
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001369 tsk->snt_unacked -= msg_conn_ack(hdr);
1370 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1371 tsk->snd_win = msg_adv_win(hdr);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001372 if (was_cong && !tsk_conn_cong(tsk))
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001373 sk->sk_write_space(sk);
1374 } else if (mtyp != CONN_PROBE_REPLY) {
1375 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001376 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001377exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001378 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001379}
1380
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001381/**
Ying Xue247f0f32014-02-18 16:06:46 +08001382 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383 * @sock: socket structure
1384 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001385 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001386 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001388 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001389 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
1390 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001391 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08001392 * Return: the number of bytes sent on success, or errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001393 */
Ying Xue1b784142015-03-02 15:37:48 +08001394static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001395 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396{
Ying Xue39a0295f2015-03-02 15:37:47 +08001397 struct sock *sk = sock->sk;
1398 int ret;
1399
1400 lock_sock(sk);
1401 ret = __tipc_sendmsg(sock, m, dsz);
1402 release_sock(sk);
1403
1404 return ret;
1405}
1406
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001407static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a0295f2015-03-02 15:37:47 +08001408{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001409 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001410 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001411 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy908148b2021-03-16 22:06:15 -04001412 struct tipc_uaddr *ua = (struct tipc_uaddr *)m->msg_name;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001413 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1414 struct list_head *clinks = &tsk->cong_links;
1415 bool syn = !tipc_sk_type_connectionless(sk);
Jon Maloy75da2162017-10-13 11:04:23 +02001416 struct tipc_group *grp = tsk->group;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001417 struct tipc_msg *hdr = &tsk->phdr;
Jon Maloy908148b2021-03-16 22:06:15 -04001418 struct tipc_socket_addr skaddr;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001419 struct sk_buff_head pkts;
Jon Maloy908148b2021-03-16 22:06:15 -04001420 int atype, mtu, rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001422 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
Allan Stephensc29c3f72010-04-20 17:58:24 -04001423 return -EMSGSIZE;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001424
Jon Maloy908148b2021-03-16 22:06:15 -04001425 if (ua) {
1426 if (!tipc_uaddr_valid(ua, m->msg_namelen))
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001427 return -EINVAL;
Jon Maloy908148b2021-03-16 22:06:15 -04001428 atype = ua->addrtype;
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001429 }
1430
Jon Maloy908148b2021-03-16 22:06:15 -04001431 /* If socket belongs to a communication group follow other paths */
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001432 if (grp) {
Jon Maloy908148b2021-03-16 22:06:15 -04001433 if (!ua)
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001434 return tipc_send_group_bcast(sock, m, dlen, timeout);
Jon Maloy908148b2021-03-16 22:06:15 -04001435 if (atype == TIPC_SERVICE_ADDR)
Jon Maloyee106d72017-10-13 11:04:28 +02001436 return tipc_send_group_anycast(sock, m, dlen, timeout);
Jon Maloy908148b2021-03-16 22:06:15 -04001437 if (atype == TIPC_SOCKET_ADDR)
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001438 return tipc_send_group_unicast(sock, m, dlen, timeout);
Jon Maloy908148b2021-03-16 22:06:15 -04001439 if (atype == TIPC_SERVICE_RANGE)
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001440 return tipc_send_group_mcast(sock, m, dlen, timeout);
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001441 return -EINVAL;
1442 }
Jon Maloy75da2162017-10-13 11:04:23 +02001443
Jon Maloy908148b2021-03-16 22:06:15 -04001444 if (!ua) {
1445 ua = (struct tipc_uaddr *)&tsk->peer;
1446 if (!syn && ua->family != AF_TIPC)
Erik Hugnef2f80362015-03-19 09:02:19 +01001447 return -EDESTADDRREQ;
Jon Maloy02fdc142021-03-29 13:17:31 -04001448 atype = ua->addrtype;
Erik Hugnef2f80362015-03-19 09:02:19 +01001449 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001450
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001451 if (unlikely(syn)) {
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001452 if (sk->sk_state == TIPC_LISTEN)
Ying Xue39a0295f2015-03-02 15:37:47 +08001453 return -EPIPE;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001454 if (sk->sk_state != TIPC_OPEN)
Ying Xue39a0295f2015-03-02 15:37:47 +08001455 return -EISCONN;
1456 if (tsk->published)
1457 return -EOPNOTSUPP;
Jon Maloy14623e02021-06-02 13:44:24 -04001458 if (atype == TIPC_SERVICE_ADDR)
1459 tsk->conn_addrtype = atype;
Jon Maloy25b92212018-09-28 20:23:21 +02001460 msg_set_syn(hdr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001461 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001462
Jon Maloy908148b2021-03-16 22:06:15 -04001463 /* Determine destination */
1464 if (atype == TIPC_SERVICE_RANGE) {
Jon Maloy833f8672021-03-16 22:06:17 -04001465 return tipc_sendmcast(sock, ua, m, dlen, timeout);
Jon Maloy908148b2021-03-16 22:06:15 -04001466 } else if (atype == TIPC_SERVICE_ADDR) {
1467 skaddr.node = ua->lookup_node;
1468 ua->scope = tipc_node2scope(skaddr.node);
1469 if (!tipc_nametbl_lookup_anycast(net, ua, &skaddr))
Ying Xue39a0295f2015-03-02 15:37:47 +08001470 return -EHOSTUNREACH;
Jon Maloy908148b2021-03-16 22:06:15 -04001471 } else if (atype == TIPC_SOCKET_ADDR) {
1472 skaddr = ua->sk;
Jon Maloy335b9292018-04-12 01:15:48 +02001473 } else {
1474 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001475 }
1476
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001477 /* Block or return if destination link is congested */
Jon Maloya80ae532017-10-13 11:04:22 +02001478 rc = tipc_wait_for_cond(sock, &timeout,
Jon Maloy908148b2021-03-16 22:06:15 -04001479 !tipc_dest_find(clinks, skaddr.node, 0));
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001480 if (unlikely(rc))
Ying Xue39a0295f2015-03-02 15:37:47 +08001481 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001482
Jon Maloy908148b2021-03-16 22:06:15 -04001483 /* Finally build message header */
1484 msg_set_destnode(hdr, skaddr.node);
1485 msg_set_destport(hdr, skaddr.ref);
1486 if (atype == TIPC_SERVICE_ADDR) {
Tuong Lienabc9b4e2019-12-10 15:21:04 +07001487 msg_set_type(hdr, TIPC_NAMED_MSG);
1488 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
Jon Maloy908148b2021-03-16 22:06:15 -04001489 msg_set_nametype(hdr, ua->sa.type);
1490 msg_set_nameinst(hdr, ua->sa.instance);
1491 msg_set_lookup_scope(hdr, ua->scope);
Jon Maloyb6f88d92020-11-25 13:29:15 -05001492 } else { /* TIPC_SOCKET_ADDR */
Tuong Lienabc9b4e2019-12-10 15:21:04 +07001493 msg_set_type(hdr, TIPC_DIRECT_MSG);
1494 msg_set_lookup_scope(hdr, 0);
Tuong Lienabc9b4e2019-12-10 15:21:04 +07001495 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
1496 }
1497
Jon Maloy908148b2021-03-16 22:06:15 -04001498 /* Add message body */
Jon Maloye654f9f2019-08-15 16:42:50 +02001499 __skb_queue_head_init(&pkts);
Jon Maloy908148b2021-03-16 22:06:15 -04001500 mtu = tipc_node_get_mtu(net, skaddr.node, tsk->portid, true);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001501 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1502 if (unlikely(rc != dlen))
1503 return rc;
Tung Nguyen2fe97a52019-11-28 10:10:05 +07001504 if (unlikely(syn && !tipc_msg_skb_clone(&pkts, &sk->sk_write_queue))) {
1505 __skb_queue_purge(&pkts);
Tung Nguyen67879272018-09-28 20:23:22 +02001506 return -ENOMEM;
Tung Nguyen2fe97a52019-11-28 10:10:05 +07001507 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001508
Jon Maloy908148b2021-03-16 22:06:15 -04001509 /* Send message */
Tuong Lien01e661e2018-12-19 09:17:58 +07001510 trace_tipc_sk_sendmsg(sk, skb_peek(&pkts), TIPC_DUMP_SK_SNDQ, " ");
Jon Maloy908148b2021-03-16 22:06:15 -04001511 rc = tipc_node_xmit(net, &pkts, skaddr.node, tsk->portid);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001512 if (unlikely(rc == -ELINKCONG)) {
Jon Maloy908148b2021-03-16 22:06:15 -04001513 tipc_dest_push(clinks, skaddr.node, 0);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001514 tsk->cong_link_cnt++;
1515 rc = 0;
1516 }
1517
1518 if (unlikely(syn && !rc))
1519 tipc_set_sk_state(sk, TIPC_CONNECTING);
1520
1521 return rc ? rc : dlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001522}
1523
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001524/**
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001525 * tipc_sendstream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001526 * @sock: socket structure
1527 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001528 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001529 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001530 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001531 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08001532 * Return: the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001533 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001534 */
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001535static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001536{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001537 struct sock *sk = sock->sk;
Ying Xue39a0295f2015-03-02 15:37:47 +08001538 int ret;
1539
1540 lock_sock(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001541 ret = __tipc_sendstream(sock, m, dsz);
Ying Xue39a0295f2015-03-02 15:37:47 +08001542 release_sock(sk);
1543
1544 return ret;
1545}
1546
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001547static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a0295f2015-03-02 15:37:47 +08001548{
1549 struct sock *sk = sock->sk;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001550 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001551 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001552 struct sk_buff_head *txq = &sk->sk_write_queue;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001553 struct tipc_sock *tsk = tipc_sk(sk);
1554 struct tipc_msg *hdr = &tsk->phdr;
1555 struct net *net = sock_net(sk);
Tuong Lien0a3e0602020-05-26 16:38:38 +07001556 struct sk_buff *skb;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001557 u32 dnode = tsk_peer_node(tsk);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001558 int maxnagle = tsk->maxnagle;
1559 int maxpkt = tsk->max_pkt;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001560 int send, sent = 0;
Jon Maloyc0bceb92019-10-30 14:00:41 +01001561 int blocks, rc = 0;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001562
1563 if (unlikely(dlen > INT_MAX))
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001564 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001565
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001566 /* Handle implicit connection setup */
1567 if (unlikely(dest)) {
1568 rc = __tipc_sendmsg(sock, m, dlen);
Parthasarathy Bhuvaragan92ef12b2018-09-25 18:21:58 +02001569 if (dlen && dlen == rc) {
1570 tsk->peer_caps = tipc_node_get_capabilities(net, dnode);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001571 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
Parthasarathy Bhuvaragan92ef12b2018-09-25 18:21:58 +02001572 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001573 return rc;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001574 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001575
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001576 do {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001577 rc = tipc_wait_for_cond(sock, &timeout,
1578 (!tsk->cong_link_cnt &&
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -05001579 !tsk_conn_cong(tsk) &&
1580 tipc_sk_connected(sk)));
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001581 if (unlikely(rc))
1582 break;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001583 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001584 blocks = tsk->snd_backlog;
Tuong Lienc9aa81f2020-06-11 17:07:35 +07001585 if (tsk->oneway++ >= tsk->nagle_start && maxnagle &&
1586 send <= maxnagle) {
Jon Maloyc0bceb92019-10-30 14:00:41 +01001587 rc = tipc_msg_append(hdr, m, send, maxnagle, txq);
1588 if (unlikely(rc < 0))
1589 break;
1590 blocks += rc;
Tuong Lien0a3e0602020-05-26 16:38:38 +07001591 tsk->msg_acc++;
Jon Maloyc0bceb92019-10-30 14:00:41 +01001592 if (blocks <= 64 && tsk->expect_ack) {
1593 tsk->snd_backlog = blocks;
1594 sent += send;
1595 break;
Tuong Lien0a3e0602020-05-26 16:38:38 +07001596 } else if (blocks > 64) {
1597 tsk->pkt_cnt += skb_queue_len(txq);
1598 } else {
1599 skb = skb_peek_tail(txq);
YueHaibing4c21daa2020-05-28 22:34:07 +08001600 if (skb) {
1601 msg_set_ack_required(buf_msg(skb));
1602 tsk->expect_ack = true;
1603 } else {
1604 tsk->expect_ack = false;
1605 }
Tuong Lien0a3e0602020-05-26 16:38:38 +07001606 tsk->msg_acc = 0;
1607 tsk->pkt_cnt = 0;
Jon Maloyc0bceb92019-10-30 14:00:41 +01001608 }
Jon Maloyc0bceb92019-10-30 14:00:41 +01001609 } else {
1610 rc = tipc_msg_build(hdr, m, sent, send, maxpkt, txq);
1611 if (unlikely(rc != send))
1612 break;
1613 blocks += tsk_inc(tsk, send + MIN_H_SIZE);
1614 }
1615 trace_tipc_sk_sendstream(sk, skb_peek(txq),
Tuong Lien01e661e2018-12-19 09:17:58 +07001616 TIPC_DUMP_SK_SNDQ, " ");
Jon Maloyc0bceb92019-10-30 14:00:41 +01001617 rc = tipc_node_xmit(net, txq, dnode, tsk->portid);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001618 if (unlikely(rc == -ELINKCONG)) {
1619 tsk->cong_link_cnt = 1;
1620 rc = 0;
1621 }
1622 if (likely(!rc)) {
Jon Maloyc0bceb92019-10-30 14:00:41 +01001623 tsk->snt_unacked += blocks;
1624 tsk->snd_backlog = 0;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001625 sent += send;
1626 }
1627 } while (sent < dlen && !rc);
1628
Parthasarathy Bhuvaragan3364d612017-04-24 15:00:42 +02001629 return sent ? sent : rc;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001630}
1631
1632/**
1633 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001634 * @sock: socket structure
1635 * @m: message to send
1636 * @dsz: length of data to be transmitted
1637 *
1638 * Used for SOCK_SEQPACKET messages.
1639 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08001640 * Return: the number of bytes sent on success, or errno otherwise
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001641 */
Ying Xue1b784142015-03-02 15:37:48 +08001642static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001643{
1644 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1645 return -EMSGSIZE;
1646
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001647 return tipc_sendstream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001648}
1649
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001650/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001651 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001652static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001653 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001654{
Ying Xue3721e9c2015-01-13 17:07:48 +08001655 struct sock *sk = &tsk->sk;
1656 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001657 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001658
Jon Maloy25b92212018-09-28 20:23:21 +02001659 msg_set_syn(msg, 0);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001660 msg_set_destnode(msg, peer_node);
1661 msg_set_destport(msg, peer_port);
1662 msg_set_type(msg, TIPC_CONN_MSG);
1663 msg_set_lookup_scope(msg, 0);
1664 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001665
Jon Maloy0d5fcebf2017-10-20 11:21:32 +02001666 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001667 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
Ying Xuef2f98002015-01-09 15:27:05 +08001668 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
Hoang Lef73b1282019-10-29 07:51:21 +07001669 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid, true);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001670 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001671 tsk_set_nagle(tsk);
Tung Nguyen67879272018-09-28 20:23:22 +02001672 __skb_queue_purge(&sk->sk_write_queue);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001673 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1674 return;
1675
1676 /* Fall back to message based flow control */
1677 tsk->rcv_win = FLOWCTL_MSG_WIN;
1678 tsk->snd_win = FLOWCTL_MSG_WIN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679}
1680
1681/**
Jon Maloy31c82a22017-10-13 11:04:24 +02001682 * tipc_sk_set_orig_addr - capture sender's address for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001683 * @m: descriptor for message info
Andrew Lunnd8141202020-07-13 01:15:14 +02001684 * @skb: received message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001685 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001686 * Note: Address is not captured if not requested by receiver.
1687 */
Jon Maloy31c82a22017-10-13 11:04:24 +02001688static void tipc_sk_set_orig_addr(struct msghdr *m, struct sk_buff *skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001689{
Jon Maloy31c82a22017-10-13 11:04:24 +02001690 DECLARE_SOCKADDR(struct sockaddr_pair *, srcaddr, m->msg_name);
1691 struct tipc_msg *hdr = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001692
Jon Maloy31c82a22017-10-13 11:04:24 +02001693 if (!srcaddr)
1694 return;
1695
1696 srcaddr->sock.family = AF_TIPC;
Jon Maloyb6f88d92020-11-25 13:29:15 -05001697 srcaddr->sock.addrtype = TIPC_SOCKET_ADDR;
Eric Dumazet09c8b972018-05-09 09:50:22 -07001698 srcaddr->sock.scope = 0;
Jon Maloy31c82a22017-10-13 11:04:24 +02001699 srcaddr->sock.addr.id.ref = msg_origport(hdr);
1700 srcaddr->sock.addr.id.node = msg_orignode(hdr);
1701 srcaddr->sock.addr.name.domain = 0;
Jon Maloy31c82a22017-10-13 11:04:24 +02001702 m->msg_namelen = sizeof(struct sockaddr_tipc);
1703
1704 if (!msg_in_group(hdr))
1705 return;
1706
1707 /* Group message users may also want to know sending member's id */
1708 srcaddr->member.family = AF_TIPC;
Jon Maloyb6f88d92020-11-25 13:29:15 -05001709 srcaddr->member.addrtype = TIPC_SERVICE_ADDR;
Eric Dumazet09c8b972018-05-09 09:50:22 -07001710 srcaddr->member.scope = 0;
Jon Maloy31c82a22017-10-13 11:04:24 +02001711 srcaddr->member.addr.name.name.type = msg_nametype(hdr);
1712 srcaddr->member.addr.name.name.instance = TIPC_SKB_CB(skb)->orig_member;
1713 srcaddr->member.addr.name.domain = 0;
1714 m->msg_namelen = sizeof(*srcaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001715}
1716
1717/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001718 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001719 * @m: descriptor for message info
Jon Maloy1c1274a2018-11-17 12:17:06 -05001720 * @skb: received message buffer
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001721 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001722 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001723 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001724 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08001725 * Return: 0 if successful, otherwise errno
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726 */
Jon Maloy1c1274a2018-11-17 12:17:06 -05001727static int tipc_sk_anc_data_recv(struct msghdr *m, struct sk_buff *skb,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001728 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001729{
Jon Maloy62633c22021-06-02 13:44:25 -04001730 struct tipc_msg *hdr;
1731 u32 data[3] = {0,};
1732 bool has_addr;
1733 int dlen, rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001734
1735 if (likely(m->msg_controllen == 0))
1736 return 0;
1737
Jon Maloy62633c22021-06-02 13:44:25 -04001738 hdr = buf_msg(skb);
1739 dlen = msg_data_sz(hdr);
1740
1741 /* Capture errored message object, if any */
1742 if (msg_errcode(hdr)) {
1743 if (skb_linearize(skb))
1744 return -ENOMEM;
1745 hdr = buf_msg(skb);
1746 data[0] = msg_errcode(hdr);
1747 data[1] = dlen;
1748 rc = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, data);
1749 if (rc || !dlen)
1750 return rc;
1751 rc = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, dlen, msg_data(hdr));
1752 if (rc)
1753 return rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001754 }
1755
Jon Maloy62633c22021-06-02 13:44:25 -04001756 /* Capture TIPC_SERVICE_ADDR/RANGE destination address, if any */
1757 switch (msg_type(hdr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001758 case TIPC_NAMED_MSG:
Jon Maloy62633c22021-06-02 13:44:25 -04001759 has_addr = true;
1760 data[0] = msg_nametype(hdr);
1761 data[1] = msg_namelower(hdr);
1762 data[2] = data[1];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001763 break;
1764 case TIPC_MCAST_MSG:
Jon Maloy62633c22021-06-02 13:44:25 -04001765 has_addr = true;
1766 data[0] = msg_nametype(hdr);
1767 data[1] = msg_namelower(hdr);
1768 data[2] = msg_nameupper(hdr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001769 break;
1770 case TIPC_CONN_MSG:
Jon Maloy62633c22021-06-02 13:44:25 -04001771 has_addr = !!tsk->conn_addrtype;
1772 data[0] = msg_nametype(&tsk->phdr);
1773 data[1] = msg_nameinst(&tsk->phdr);
1774 data[2] = data[1];
Per Lidenb97bf3f2006-01-02 19:04:38 +01001775 break;
1776 default:
Jon Maloy62633c22021-06-02 13:44:25 -04001777 has_addr = false;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001778 }
Jon Maloy62633c22021-06-02 13:44:25 -04001779 if (!has_addr)
1780 return 0;
1781 return put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, data);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001782}
1783
Tuong Lienc7268582020-05-13 19:33:16 +07001784static struct sk_buff *tipc_sk_build_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001785{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001786 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08001787 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001788 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001789 u32 peer_port = tsk_peer_port(tsk);
1790 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001791
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001792 if (!tipc_sk_connected(sk))
Tuong Lienc7268582020-05-13 19:33:16 +07001793 return NULL;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001794 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1795 dnode, tsk_own_node(tsk), peer_port,
1796 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001797 if (!skb)
Tuong Lienc7268582020-05-13 19:33:16 +07001798 return NULL;
Ying Xuea6ca1092014-11-26 11:41:55 +08001799 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001800 msg_set_conn_ack(msg, tsk->rcv_unacked);
1801 tsk->rcv_unacked = 0;
1802
1803 /* Adjust to and advertize the correct window limit */
1804 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1805 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1806 msg_set_adv_win(msg, tsk->rcv_win);
1807 }
Tuong Lienc7268582020-05-13 19:33:16 +07001808 return skb;
1809}
1810
1811static void tipc_sk_send_ack(struct tipc_sock *tsk)
1812{
1813 struct sk_buff *skb;
1814
1815 skb = tipc_sk_build_ack(tsk);
1816 if (!skb)
1817 return;
1818
1819 tipc_node_xmit_skb(sock_net(&tsk->sk), skb, tsk_peer_node(tsk),
1820 msg_link_selector(buf_msg(skb)));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001821}
1822
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001823static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001824{
1825 struct sock *sk = sock->sk;
Tung Nguyen48766a52019-02-19 11:20:48 +07001826 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001827 long timeo = *timeop;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001828 int err = sock_error(sk);
1829
1830 if (err)
1831 return err;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001832
1833 for (;;) {
Ying Xuefe8e4642014-03-06 14:40:18 +01001834 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01001835 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001836 err = -ENOTCONN;
1837 break;
1838 }
Tung Nguyen48766a52019-02-19 11:20:48 +07001839 add_wait_queue(sk_sleep(sk), &wait);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001840 release_sock(sk);
Tung Nguyen48766a52019-02-19 11:20:48 +07001841 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
1842 sched_annotate_sleep();
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001843 lock_sock(sk);
Tung Nguyen48766a52019-02-19 11:20:48 +07001844 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001845 }
1846 err = 0;
1847 if (!skb_queue_empty(&sk->sk_receive_queue))
1848 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001849 err = -EAGAIN;
1850 if (!timeo)
1851 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001852 err = sock_intr_errno(timeo);
1853 if (signal_pending(current))
1854 break;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001855
1856 err = sock_error(sk);
1857 if (err)
1858 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001859 }
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001860 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001861 return err;
1862}
1863
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001864/**
Ying Xue247f0f32014-02-18 16:06:46 +08001865 * tipc_recvmsg - receive packet-oriented message
Randy Dunlapf172f4b2020-11-29 10:32:49 -08001866 * @sock: network socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001867 * @m: descriptor for message info
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001868 * @buflen: length of user buffer area
Per Lidenb97bf3f2006-01-02 19:04:38 +01001869 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001870 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001871 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1872 * If the complete message doesn't fit in user area, truncate it.
1873 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08001874 * Return: size of returned message data, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001875 */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001876static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
1877 size_t buflen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001878{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001879 struct sock *sk = sock->sk;
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001880 bool connected = !tipc_sk_type_connectionless(sk);
Jon Maloyae236fb2017-10-13 11:04:25 +02001881 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001882 int rc, err, hlen, dlen, copy;
Jon Maloyb7d42632017-10-13 11:04:26 +02001883 struct sk_buff_head xmitq;
Jon Maloyae236fb2017-10-13 11:04:25 +02001884 struct tipc_msg *hdr;
1885 struct sk_buff *skb;
1886 bool grp_evt;
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001887 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001888
Allan Stephens0c3141e2008-04-15 00:22:02 -07001889 /* Catch invalid receive requests */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001890 if (unlikely(!buflen))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001891 return -EINVAL;
1892
Allan Stephens0c3141e2008-04-15 00:22:02 -07001893 lock_sock(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001894 if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
1895 rc = -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001896 goto exit;
1897 }
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001898 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001899
Jon Maloyb7d42632017-10-13 11:04:26 +02001900 /* Step rcv queue to first msg with data or error; wait if necessary */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001901 do {
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001902 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1903 if (unlikely(rc))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001904 goto exit;
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001905 skb = skb_peek(&sk->sk_receive_queue);
1906 hdr = buf_msg(skb);
1907 dlen = msg_data_sz(hdr);
1908 hlen = msg_hdr_sz(hdr);
1909 err = msg_errcode(hdr);
Jon Maloyae236fb2017-10-13 11:04:25 +02001910 grp_evt = msg_is_grp_evt(hdr);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001911 if (likely(dlen || err))
1912 break;
1913 tsk_advance_rx_queue(sk);
1914 } while (1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001915
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001916 /* Collect msg meta data, including error code and rejected data */
Jon Maloy31c82a22017-10-13 11:04:24 +02001917 tipc_sk_set_orig_addr(m, skb);
Jon Maloy1c1274a2018-11-17 12:17:06 -05001918 rc = tipc_sk_anc_data_recv(m, skb, tsk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001919 if (unlikely(rc))
1920 goto exit;
Jon Maloy1c1274a2018-11-17 12:17:06 -05001921 hdr = buf_msg(skb);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001922
1923 /* Capture data if non-error msg, otherwise just set return value */
1924 if (likely(!err)) {
1925 copy = min_t(int, dlen, buflen);
1926 if (unlikely(copy != dlen))
1927 m->msg_flags |= MSG_TRUNC;
1928 rc = skb_copy_datagram_msg(skb, hlen, m, copy);
1929 } else {
1930 copy = 0;
1931 rc = 0;
1932 if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control)
1933 rc = -ECONNRESET;
1934 }
1935 if (unlikely(rc))
1936 goto exit;
1937
Jon Maloyae236fb2017-10-13 11:04:25 +02001938 /* Mark message as group event if applicable */
1939 if (unlikely(grp_evt)) {
1940 if (msg_grp_evt(hdr) == TIPC_WITHDRAWN)
1941 m->msg_flags |= MSG_EOR;
1942 m->msg_flags |= MSG_OOB;
1943 copy = 0;
1944 }
1945
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001946 /* Caption of data or error code/rejected data was successful */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001947 if (unlikely(flags & MSG_PEEK))
1948 goto exit;
1949
Jon Maloyb7d42632017-10-13 11:04:26 +02001950 /* Send group flow control advertisement when applicable */
1951 if (tsk->group && msg_in_group(hdr) && !grp_evt) {
Jon Maloye654f9f2019-08-15 16:42:50 +02001952 __skb_queue_head_init(&xmitq);
Jon Maloyb7d42632017-10-13 11:04:26 +02001953 tipc_group_update_rcv_win(tsk->group, tsk_blocks(hlen + dlen),
1954 msg_orignode(hdr), msg_origport(hdr),
1955 &xmitq);
1956 tipc_node_distr_xmit(sock_net(sk), &xmitq);
1957 }
1958
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001959 tsk_advance_rx_queue(sk);
Jon Maloyae236fb2017-10-13 11:04:25 +02001960
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001961 if (likely(!connected))
1962 goto exit;
1963
Jon Maloyb7d42632017-10-13 11:04:26 +02001964 /* Send connection flow control advertisement when applicable */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001965 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1966 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
1967 tipc_sk_send_ack(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001968exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001969 release_sock(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001970 return rc ? rc : copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001971}
1972
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001973/**
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001974 * tipc_recvstream - receive stream-oriented data
Randy Dunlapf172f4b2020-11-29 10:32:49 -08001975 * @sock: network socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001976 * @m: descriptor for message info
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001977 * @buflen: total size of user buffer area
Per Lidenb97bf3f2006-01-02 19:04:38 +01001978 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001979 *
1980 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001981 * will optionally wait for more; never truncates data.
1982 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08001983 * Return: size of returned message data, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01001984 */
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001985static int tipc_recvstream(struct socket *sock, struct msghdr *m,
1986 size_t buflen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001987{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001988 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001989 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001990 struct sk_buff *skb;
1991 struct tipc_msg *hdr;
1992 struct tipc_skb_cb *skb_cb;
1993 bool peek = flags & MSG_PEEK;
1994 int offset, required, copy, copied = 0;
1995 int hlen, dlen, err, rc;
1996 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001997
1998 /* Catch invalid receive attempts */
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001999 if (unlikely(!buflen))
Per Lidenb97bf3f2006-01-02 19:04:38 +01002000 return -EINVAL;
2001
Allan Stephens0c3141e2008-04-15 00:22:02 -07002002 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002003
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01002004 if (unlikely(sk->sk_state == TIPC_OPEN)) {
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002005 rc = -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002006 goto exit;
2007 }
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002008 required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
2009 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002010
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002011 do {
2012 /* Look at first msg in receive queue; wait if necessary */
2013 rc = tipc_wait_for_rcvmsg(sock, &timeout);
2014 if (unlikely(rc))
2015 break;
2016 skb = skb_peek(&sk->sk_receive_queue);
2017 skb_cb = TIPC_SKB_CB(skb);
2018 hdr = buf_msg(skb);
2019 dlen = msg_data_sz(hdr);
2020 hlen = msg_hdr_sz(hdr);
2021 err = msg_errcode(hdr);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002022
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002023 /* Discard any empty non-errored (SYN-) message */
2024 if (unlikely(!dlen && !err)) {
2025 tsk_advance_rx_queue(sk);
2026 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002027 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002028
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002029 /* Collect msg meta data, incl. error code and rejected data */
2030 if (!copied) {
Jon Maloy31c82a22017-10-13 11:04:24 +02002031 tipc_sk_set_orig_addr(m, skb);
Jon Maloy1c1274a2018-11-17 12:17:06 -05002032 rc = tipc_sk_anc_data_recv(m, skb, tsk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002033 if (rc)
2034 break;
Jon Maloy1c1274a2018-11-17 12:17:06 -05002035 hdr = buf_msg(skb);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002036 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002037
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002038 /* Copy data if msg ok, otherwise return error/partial data */
2039 if (likely(!err)) {
2040 offset = skb_cb->bytes_read;
2041 copy = min_t(int, dlen - offset, buflen - copied);
2042 rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
2043 if (unlikely(rc))
2044 break;
2045 copied += copy;
2046 offset += copy;
2047 if (unlikely(offset < dlen)) {
2048 if (!peek)
2049 skb_cb->bytes_read = offset;
2050 break;
2051 }
2052 } else {
2053 rc = 0;
2054 if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control)
2055 rc = -ECONNRESET;
2056 if (copied || rc)
2057 break;
2058 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002059
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002060 if (unlikely(peek))
2061 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002062
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002063 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002064
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002065 /* Send connection flow control advertisement when applicable */
2066 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
Tuong Lienc7268582020-05-13 19:33:16 +07002067 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002068 tipc_sk_send_ack(tsk);
2069
2070 /* Exit if all requested data or FIN/error received */
2071 if (copied == buflen || err)
2072 break;
2073
2074 } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002075exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002076 release_sock(sk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002077 return copied ? copied : rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002078}
2079
2080/**
Ying Xuef288bef2012-08-21 11:16:57 +08002081 * tipc_write_space - wake up thread if port congestion is released
2082 * @sk: socket
2083 */
2084static void tipc_write_space(struct sock *sk)
2085{
2086 struct socket_wq *wq;
2087
2088 rcu_read_lock();
2089 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08002090 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002091 wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
2092 EPOLLWRNORM | EPOLLWRBAND);
Ying Xuef288bef2012-08-21 11:16:57 +08002093 rcu_read_unlock();
2094}
2095
2096/**
2097 * tipc_data_ready - wake up threads to indicate messages have been received
2098 * @sk: socket
Ying Xuef288bef2012-08-21 11:16:57 +08002099 */
David S. Miller676d2362014-04-11 16:15:36 -04002100static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08002101{
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, EPOLLIN |
2108 EPOLLRDNORM | EPOLLRDBAND);
Ying Xuef288bef2012-08-21 11:16:57 +08002109 rcu_read_unlock();
2110}
2111
Ying Xuef4195d12015-11-22 15:46:05 +08002112static void tipc_sock_destruct(struct sock *sk)
2113{
2114 __skb_queue_purge(&sk->sk_receive_queue);
2115}
2116
Jon Maloy64ac5f52017-10-13 11:04:20 +02002117static void tipc_sk_proto_rcv(struct sock *sk,
2118 struct sk_buff_head *inputq,
2119 struct sk_buff_head *xmitq)
2120{
2121 struct sk_buff *skb = __skb_dequeue(inputq);
2122 struct tipc_sock *tsk = tipc_sk(sk);
2123 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy75da2162017-10-13 11:04:23 +02002124 struct tipc_group *grp = tsk->group;
Jon Maloyb7d42632017-10-13 11:04:26 +02002125 bool wakeup = false;
Jon Maloy64ac5f52017-10-13 11:04:20 +02002126
2127 switch (msg_user(hdr)) {
2128 case CONN_MANAGER:
Parthasarathy Bhuvaragane7eb0582018-10-10 17:50:23 +02002129 tipc_sk_conn_proto_rcv(tsk, skb, inputq, xmitq);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002130 return;
2131 case SOCK_WAKEUP:
Jon Maloya80ae532017-10-13 11:04:22 +02002132 tipc_dest_del(&tsk->cong_links, msg_orignode(hdr), 0);
Tung Nguyenbfd07f32019-02-25 10:57:20 +07002133 /* coupled with smp_rmb() in tipc_wait_for_cond() */
2134 smp_wmb();
Jon Maloy64ac5f52017-10-13 11:04:20 +02002135 tsk->cong_link_cnt--;
Jon Maloyb7d42632017-10-13 11:04:26 +02002136 wakeup = true;
Tuong Lien0a3e0602020-05-26 16:38:38 +07002137 tipc_sk_push_backlog(tsk, false);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002138 break;
Jon Maloy75da2162017-10-13 11:04:23 +02002139 case GROUP_PROTOCOL:
Jon Maloyb7d42632017-10-13 11:04:26 +02002140 tipc_group_proto_rcv(grp, &wakeup, hdr, inputq, xmitq);
Jon Maloy75da2162017-10-13 11:04:23 +02002141 break;
Jon Maloy64ac5f52017-10-13 11:04:20 +02002142 case TOP_SRV:
Jon Maloyb7d42632017-10-13 11:04:26 +02002143 tipc_group_member_evt(tsk->group, &wakeup, &sk->sk_rcvbuf,
Jon Maloy7ad32bc2018-01-08 21:03:26 +01002144 hdr, inputq, xmitq);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002145 break;
2146 default:
2147 break;
2148 }
2149
Jon Maloyb7d42632017-10-13 11:04:26 +02002150 if (wakeup)
2151 sk->sk_write_space(sk);
2152
Jon Maloy64ac5f52017-10-13 11:04:20 +02002153 kfree_skb(skb);
2154}
2155
Ying Xuef288bef2012-08-21 11:16:57 +08002156/**
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002157 * tipc_sk_filter_connect - check incoming message for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002158 * @tsk: TIPC socket
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002159 * @skb: pointer to message buffer.
Tuong Lienc7268582020-05-13 19:33:16 +07002160 * @xmitq: for Nagle ACK if any
Randy Dunlap637b77f2020-11-29 10:32:48 -08002161 * Return: true if message should be added to receive queue, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05002162 */
Tuong Lienc7268582020-05-13 19:33:16 +07002163static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb,
2164 struct sk_buff_head *xmitq)
Ying Xue7e6c1312012-11-29 18:39:14 -05002165{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002166 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08002167 struct net *net = sock_net(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04002168 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002169 bool con_msg = msg_connected(hdr);
2170 u32 pport = tsk_peer_port(tsk);
2171 u32 pnode = tsk_peer_node(tsk);
2172 u32 oport = msg_origport(hdr);
2173 u32 onode = msg_orignode(hdr);
2174 int err = msg_errcode(hdr);
Tung Nguyen67879272018-09-28 20:23:22 +02002175 unsigned long delay;
Ying Xue7e6c1312012-11-29 18:39:14 -05002176
Jon Paul Maloycda36962015-07-22 10:11:20 -04002177 if (unlikely(msg_mcast(hdr)))
2178 return false;
Jon Maloyc0bceb92019-10-30 14:00:41 +01002179 tsk->oneway = 0;
Ying Xue7e6c1312012-11-29 18:39:14 -05002180
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002181 switch (sk->sk_state) {
2182 case TIPC_CONNECTING:
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002183 /* Setup ACK */
2184 if (likely(con_msg)) {
2185 if (err)
2186 break;
2187 tipc_sk_finish_conn(tsk, oport, onode);
2188 msg_set_importance(&tsk->phdr, msg_importance(hdr));
2189 /* ACK+ message with data is added to receive queue */
2190 if (msg_data_sz(hdr))
2191 return true;
2192 /* Empty ACK-, - wake up sleeping connect() and drop */
Parthasarathy Bhuvaraganff946832019-05-09 07:13:42 +02002193 sk->sk_state_change(sk);
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002194 msg_set_dest_droppable(hdr, 1);
2195 return false;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02002196 }
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002197 /* Ignore connectionless message if not from listening socket */
2198 if (oport != pport || onode != pnode)
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002199 return false;
2200
Tung Nguyen67879272018-09-28 20:23:22 +02002201 /* Rejected SYN */
2202 if (err != TIPC_ERR_OVERLOAD)
2203 break;
2204
2205 /* Prepare for new setup attempt if we have a SYN clone */
2206 if (skb_queue_empty(&sk->sk_write_queue))
2207 break;
2208 get_random_bytes(&delay, 2);
2209 delay %= (tsk->conn_timeout / 4);
2210 delay = msecs_to_jiffies(delay + 100);
2211 sk_reset_timer(sk, &sk->sk_timer, jiffies + delay);
2212 return false;
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002213 case TIPC_OPEN:
2214 case TIPC_DISCONNECTING:
2215 return false;
2216 case TIPC_LISTEN:
2217 /* Accept only SYN message */
Jon Maloy25b92212018-09-28 20:23:21 +02002218 if (!msg_is_syn(hdr) &&
2219 tipc_node_get_capabilities(net, onode) & TIPC_SYN_BIT)
2220 return false;
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002221 if (!con_msg && !err)
2222 return true;
2223 return false;
2224 case TIPC_ESTABLISHED:
Jon Maloyc0bceb92019-10-30 14:00:41 +01002225 if (!skb_queue_empty(&sk->sk_write_queue))
Tuong Lien0a3e0602020-05-26 16:38:38 +07002226 tipc_sk_push_backlog(tsk, false);
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002227 /* Accept only connection-based messages sent by peer */
Tuong Lienc7268582020-05-13 19:33:16 +07002228 if (likely(con_msg && !err && pport == oport &&
2229 pnode == onode)) {
2230 if (msg_ack_required(hdr)) {
2231 struct sk_buff *skb;
2232
2233 skb = tipc_sk_build_ack(tsk);
Tuong Lien0a3e0602020-05-26 16:38:38 +07002234 if (skb) {
2235 msg_set_nagle_ack(buf_msg(skb));
Tuong Lienc7268582020-05-13 19:33:16 +07002236 __skb_queue_tail(xmitq, skb);
Tuong Lien0a3e0602020-05-26 16:38:38 +07002237 }
Tuong Lienc7268582020-05-13 19:33:16 +07002238 }
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002239 return true;
Tuong Lienc7268582020-05-13 19:33:16 +07002240 }
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002241 if (!tsk_peer_msg(tsk, hdr))
2242 return false;
2243 if (!err)
2244 return true;
2245 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2246 tipc_node_remove_conn(net, pnode, tsk->portid);
2247 sk->sk_state_change(sk);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002248 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05002249 default:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01002250 pr_err("Unknown sk_state %u\n", sk->sk_state);
Ying Xue7e6c1312012-11-29 18:39:14 -05002251 }
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002252 /* Abort connection setup attempt */
2253 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2254 sk->sk_err = ECONNREFUSED;
2255 sk->sk_state_change(sk);
2256 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05002257}
2258
2259/**
Ying Xueaba79f32013-01-20 23:30:09 +01002260 * rcvbuf_limit - get proper overload limit of socket receive queue
2261 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002262 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01002263 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002264 * For connection oriented messages, irrespective of importance,
2265 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01002266 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002267 * For connectionless messages, queue limits are based on message
2268 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01002269 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002270 * TIPC_LOW_IMPORTANCE (2 MB)
2271 * TIPC_MEDIUM_IMPORTANCE (4 MB)
2272 * TIPC_HIGH_IMPORTANCE (8 MB)
2273 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01002274 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08002275 * Return: overload limit according to corresponding message importance
Ying Xueaba79f32013-01-20 23:30:09 +01002276 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002277static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01002278{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002279 struct tipc_sock *tsk = tipc_sk(sk);
2280 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01002281
Jon Maloyb7d42632017-10-13 11:04:26 +02002282 if (unlikely(msg_in_group(hdr)))
Eric Dumazet82657922019-10-09 15:21:13 -07002283 return READ_ONCE(sk->sk_rcvbuf);
Jon Maloyb7d42632017-10-13 11:04:26 +02002284
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002285 if (unlikely(!msg_connected(hdr)))
Eric Dumazet82657922019-10-09 15:21:13 -07002286 return READ_ONCE(sk->sk_rcvbuf) << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08002287
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002288 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
Eric Dumazet82657922019-10-09 15:21:13 -07002289 return READ_ONCE(sk->sk_rcvbuf);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002290
2291 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01002292}
2293
2294/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02002295 * tipc_sk_filter_rcv - validate incoming message
Allan Stephens0c3141e2008-04-15 00:22:02 -07002296 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04002297 * @skb: pointer to message.
Randy Dunlapf172f4b2020-11-29 10:32:49 -08002298 * @xmitq: output message area (FIXME)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002299 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07002300 * Enqueues message on receive queue if acceptable; optionally handles
2301 * disconnect indication for a connected socket.
2302 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05002303 * Called with socket lock already taken
Per Lidenb97bf3f2006-01-02 19:04:38 +01002304 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02002305static void tipc_sk_filter_rcv(struct sock *sk, struct sk_buff *skb,
2306 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002307{
Jon Maloy64ac5f52017-10-13 11:04:20 +02002308 bool sk_conn = !tipc_sk_type_connectionless(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002309 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy75da2162017-10-13 11:04:23 +02002310 struct tipc_group *grp = tsk->group;
Jon Paul Maloycda36962015-07-22 10:11:20 -04002311 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002312 struct net *net = sock_net(sk);
2313 struct sk_buff_head inputq;
Hoang Le77d5ad42019-03-21 17:25:17 +07002314 int mtyp = msg_type(hdr);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002315 int limit, err = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002316
Tuong Lien01e661e2018-12-19 09:17:58 +07002317 trace_tipc_sk_filter_rcv(sk, skb, TIPC_DUMP_ALL, " ");
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01002318 TIPC_SKB_CB(skb)->bytes_read = 0;
Jon Maloy64ac5f52017-10-13 11:04:20 +02002319 __skb_queue_head_init(&inputq);
2320 __skb_queue_tail(&inputq, skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002321
Jon Maloy64ac5f52017-10-13 11:04:20 +02002322 if (unlikely(!msg_isdata(hdr)))
2323 tipc_sk_proto_rcv(sk, &inputq, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04002324
Jon Maloy75da2162017-10-13 11:04:23 +02002325 if (unlikely(grp))
2326 tipc_group_filter_msg(grp, &inputq, xmitq);
2327
Hoang Le77d5ad42019-03-21 17:25:17 +07002328 if (unlikely(!grp) && mtyp == TIPC_MCAST_MSG)
Hoang Le08e046c2019-03-21 17:25:18 +07002329 tipc_mcast_filter_msg(net, &tsk->mc_method.deferredq, &inputq);
Hoang Lec55c8ed2019-03-19 18:49:50 +07002330
Jon Maloy64ac5f52017-10-13 11:04:20 +02002331 /* Validate and add to receive buffer if there is space */
2332 while ((skb = __skb_dequeue(&inputq))) {
2333 hdr = buf_msg(skb);
2334 limit = rcvbuf_limit(sk, skb);
Tuong Lienc7268582020-05-13 19:33:16 +07002335 if ((sk_conn && !tipc_sk_filter_connect(tsk, skb, xmitq)) ||
Jon Maloy75da2162017-10-13 11:04:23 +02002336 (!sk_conn && msg_connected(hdr)) ||
2337 (!grp && msg_in_group(hdr)))
Jon Maloy64ac5f52017-10-13 11:04:20 +02002338 err = TIPC_ERR_NO_PORT;
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01002339 else if (sk_rmem_alloc_get(sk) + skb->truesize >= limit) {
Tuong Lien01e661e2018-12-19 09:17:58 +07002340 trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL,
2341 "err_overload2!");
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01002342 atomic_inc(&sk->sk_drops);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002343 err = TIPC_ERR_OVERLOAD;
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01002344 }
Jon Maloy64ac5f52017-10-13 11:04:20 +02002345
2346 if (unlikely(err)) {
Tuong Lien01e661e2018-12-19 09:17:58 +07002347 if (tipc_msg_reverse(tipc_own_addr(net), &skb, err)) {
2348 trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE,
2349 "@filter_rcv!");
2350 __skb_queue_tail(xmitq, skb);
2351 }
Jon Maloy64ac5f52017-10-13 11:04:20 +02002352 err = TIPC_OK;
2353 continue;
2354 }
2355 __skb_queue_tail(&sk->sk_receive_queue, skb);
2356 skb_set_owner_r(skb, sk);
Tuong Lien01e661e2018-12-19 09:17:58 +07002357 trace_tipc_sk_overlimit2(sk, skb, TIPC_DUMP_ALL,
2358 "rcvq >90% allocated!");
Jon Maloy64ac5f52017-10-13 11:04:20 +02002359 sk->sk_data_ready(sk);
2360 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002361}
2362
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002363/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02002364 * tipc_sk_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07002365 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08002366 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07002367 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05002368 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07002369 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02002370static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07002371{
Jon Maloy64ac5f52017-10-13 11:04:20 +02002372 unsigned int before = sk_rmem_alloc_get(sk);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002373 struct sk_buff_head xmitq;
Jon Maloy64ac5f52017-10-13 11:04:20 +02002374 unsigned int added;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002375
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002376 __skb_queue_head_init(&xmitq);
2377
Jon Maloy64ac5f52017-10-13 11:04:20 +02002378 tipc_sk_filter_rcv(sk, skb, &xmitq);
2379 added = sk_rmem_alloc_get(sk) - before;
2380 atomic_add(added, &tipc_sk(sk)->dupl_rcvcnt);
2381
2382 /* Send pending response/rejected messages, if any */
Jon Maloyf70d37b2017-10-13 11:04:21 +02002383 tipc_node_distr_xmit(sock_net(sk), &xmitq);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002384 return 0;
2385}
2386
2387/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002388 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
2389 * inputq and try adding them to socket or backlog queue
2390 * @inputq: list of incoming buffers with potentially different destinations
2391 * @sk: socket where the buffers should be enqueued
2392 * @dport: port number for the socket
Randy Dunlapf172f4b2020-11-29 10:32:49 -08002393 * @xmitq: output queue
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002394 *
2395 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002396 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04002397static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002398 u32 dport, struct sk_buff_head *xmitq)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002399{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002400 unsigned long time_limit = jiffies + 2;
2401 struct sk_buff *skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002402 unsigned int lim;
2403 atomic_t *dcnt;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002404 u32 onode;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002405
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002406 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05002407 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04002408 return;
2409
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002410 skb = tipc_skb_dequeue(inputq, dport);
2411 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04002412 return;
2413
2414 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002415 if (!sock_owned_by_user(sk)) {
Jon Maloy64ac5f52017-10-13 11:04:20 +02002416 tipc_sk_filter_rcv(sk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04002417 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002418 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04002419
2420 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002421 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04002422 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002423 atomic_set(dcnt, 0);
2424 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
Tuong Lien01e661e2018-12-19 09:17:58 +07002425 if (likely(!sk_add_backlog(sk, skb, lim))) {
2426 trace_tipc_sk_overlimit1(sk, skb, TIPC_DUMP_ALL,
2427 "bklg & rcvq >90% allocated!");
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002428 continue;
Tuong Lien01e661e2018-12-19 09:17:58 +07002429 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04002430
Tuong Lien01e661e2018-12-19 09:17:58 +07002431 trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL, "err_overload!");
Jon Paul Maloycda36962015-07-22 10:11:20 -04002432 /* Overload => reject message back to sender */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002433 onode = tipc_own_addr(sock_net(sk));
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01002434 atomic_inc(&sk->sk_drops);
Tuong Lien01e661e2018-12-19 09:17:58 +07002435 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD)) {
2436 trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_ALL,
2437 "@sk_enqueue!");
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002438 __skb_queue_tail(xmitq, skb);
Tuong Lien01e661e2018-12-19 09:17:58 +07002439 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04002440 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002441 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002442}
2443
2444/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002445 * tipc_sk_rcv - handle a chain of incoming buffers
Randy Dunlapf172f4b2020-11-29 10:32:49 -08002446 * @net: the associated network namespace
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002447 * @inputq: buffer list containing the buffers
2448 * Consumes all buffers in list until inputq is empty
2449 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07002450 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04002451void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07002452{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002453 struct sk_buff_head xmitq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002454 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04002455 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04002456 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04002457 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04002458 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04002459
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002460 __skb_queue_head_init(&xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002461 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002462 dport = tipc_skb_peek_port(inputq, dport);
2463 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04002464
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002465 if (likely(tsk)) {
2466 sk = &tsk->sk;
2467 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002468 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002469 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002470 }
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002471 /* Send pending response/rejected messages, if any */
Jon Maloyf70d37b2017-10-13 11:04:21 +02002472 tipc_node_distr_xmit(sock_net(sk), &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002473 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002474 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002475 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04002476 /* No destination socket => dequeue skb if still there */
2477 skb = tipc_skb_dequeue(inputq, dport);
2478 if (!skb)
2479 return;
2480
2481 /* Try secondary lookup if unresolved named message */
2482 err = TIPC_ERR_NO_PORT;
2483 if (tipc_msg_lookup_dest(net, skb, &err))
2484 goto xmit;
2485
2486 /* Prepare for message rejection */
2487 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002488 continue;
Tuong Lien01e661e2018-12-19 09:17:58 +07002489
2490 trace_tipc_sk_rej_msg(NULL, skb, TIPC_DUMP_NONE, "@sk_rcv!");
Jon Paul Maloye3a77562015-02-05 08:36:39 -05002491xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04002492 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002493 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002494 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07002495}
2496
Ying Xue78eb3a52014-01-17 09:50:03 +08002497static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
2498{
WANG Congd9dc8b02016-11-11 10:20:50 -08002499 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Ying Xue78eb3a52014-01-17 09:50:03 +08002500 struct sock *sk = sock->sk;
Ying Xue78eb3a52014-01-17 09:50:03 +08002501 int done;
2502
2503 do {
2504 int err = sock_error(sk);
2505 if (err)
2506 return err;
2507 if (!*timeo_p)
2508 return -ETIMEDOUT;
2509 if (signal_pending(current))
2510 return sock_intr_errno(*timeo_p);
Tuong Lien5391a872020-02-10 15:35:44 +07002511 if (sk->sk_state == TIPC_DISCONNECTING)
2512 break;
Ying Xue78eb3a52014-01-17 09:50:03 +08002513
WANG Congd9dc8b02016-11-11 10:20:50 -08002514 add_wait_queue(sk_sleep(sk), &wait);
Tuong Lien9546a0b2020-01-08 09:19:00 +07002515 done = sk_wait_event(sk, timeo_p, tipc_sk_connected(sk),
2516 &wait);
WANG Congd9dc8b02016-11-11 10:20:50 -08002517 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue78eb3a52014-01-17 09:50:03 +08002518 } while (!done);
2519 return 0;
2520}
2521
Erik Hugneea239312019-03-17 18:46:42 +01002522static bool tipc_sockaddr_is_sane(struct sockaddr_tipc *addr)
2523{
2524 if (addr->family != AF_TIPC)
2525 return false;
2526 if (addr->addrtype == TIPC_SERVICE_RANGE)
2527 return (addr->addr.nameseq.lower <= addr->addr.nameseq.upper);
2528 return (addr->addrtype == TIPC_SERVICE_ADDR ||
2529 addr->addrtype == TIPC_SOCKET_ADDR);
2530}
2531
Per Lidenb97bf3f2006-01-02 19:04:38 +01002532/**
Ying Xue247f0f32014-02-18 16:06:46 +08002533 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01002534 * @sock: socket structure
2535 * @dest: socket address for destination port
2536 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07002537 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01002538 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08002539 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01002540 */
Ying Xue247f0f32014-02-18 16:06:46 +08002541static int tipc_connect(struct socket *sock, struct sockaddr *dest,
2542 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002543{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002544 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01002545 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07002546 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
2547 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01002548 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002549 int previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01002550 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002551
Jon Maloy23998832017-10-13 11:04:18 +02002552 if (destlen != sizeof(struct sockaddr_tipc))
2553 return -EINVAL;
2554
Allan Stephens0c3141e2008-04-15 00:22:02 -07002555 lock_sock(sk);
2556
Jon Maloy75da2162017-10-13 11:04:23 +02002557 if (tsk->group) {
2558 res = -EINVAL;
2559 goto exit;
2560 }
2561
Jon Maloy23998832017-10-13 11:04:18 +02002562 if (dst->family == AF_UNSPEC) {
2563 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
2564 if (!tipc_sk_type_connectionless(sk))
2565 res = -EINVAL;
2566 goto exit;
Jon Maloy23998832017-10-13 11:04:18 +02002567 }
Erik Hugneea239312019-03-17 18:46:42 +01002568 if (!tipc_sockaddr_is_sane(dst)) {
Jon Maloy23998832017-10-13 11:04:18 +02002569 res = -EINVAL;
Jon Maloy23998832017-10-13 11:04:18 +02002570 goto exit;
Erik Hugneea239312019-03-17 18:46:42 +01002571 }
Erik Hugnef2f80362015-03-19 09:02:19 +01002572 /* DGRAM/RDM connect(), just save the destaddr */
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +01002573 if (tipc_sk_type_connectionless(sk)) {
Jon Maloy23998832017-10-13 11:04:18 +02002574 memcpy(&tsk->peer, dest, destlen);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002575 goto exit;
Erik Hugneea239312019-03-17 18:46:42 +01002576 } else if (dst->addrtype == TIPC_SERVICE_RANGE) {
2577 res = -EINVAL;
2578 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002579 }
2580
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002581 previous = sk->sk_state;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01002582
2583 switch (sk->sk_state) {
2584 case TIPC_OPEN:
Ying Xue584d24b2012-11-29 18:51:19 -05002585 /* Send a 'SYN-' to destination */
2586 m.msg_name = dest;
2587 m.msg_namelen = destlen;
2588
2589 /* If connect is in non-blocking case, set MSG_DONTWAIT to
2590 * indicate send_msg() is never blocked.
2591 */
2592 if (!timeout)
2593 m.msg_flags = MSG_DONTWAIT;
2594
Ying Xue39a0295f2015-03-02 15:37:47 +08002595 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05002596 if ((res < 0) && (res != -EWOULDBLOCK))
2597 goto exit;
2598
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002599 /* Just entered TIPC_CONNECTING state; the only
Ying Xue584d24b2012-11-29 18:51:19 -05002600 * difference is that return value in non-blocking
2601 * case is EINPROGRESS, rather than EALREADY.
2602 */
2603 res = -EINPROGRESS;
Miaohe Lin7f8901b2020-08-18 08:07:13 -04002604 fallthrough;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002605 case TIPC_CONNECTING:
2606 if (!timeout) {
2607 if (previous == TIPC_CONNECTING)
2608 res = -EALREADY;
Ying Xue78eb3a52014-01-17 09:50:03 +08002609 goto exit;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002610 }
Ying Xue78eb3a52014-01-17 09:50:03 +08002611 timeout = msecs_to_jiffies(timeout);
2612 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
2613 res = tipc_wait_for_connect(sock, &timeout);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002614 break;
2615 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002616 res = -EISCONN;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002617 break;
2618 default:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002619 res = -EINVAL;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002620 }
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002621
Allan Stephens0c3141e2008-04-15 00:22:02 -07002622exit:
2623 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07002624 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002625}
2626
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002627/**
Ying Xue247f0f32014-02-18 16:06:46 +08002628 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01002629 * @sock: socket structure
2630 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002631 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08002632 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01002633 */
Ying Xue247f0f32014-02-18 16:06:46 +08002634static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002635{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002636 struct sock *sk = sock->sk;
2637 int res;
2638
2639 lock_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002640 res = tipc_set_sk_state(sk, TIPC_LISTEN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002641 release_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002642
Allan Stephens0c3141e2008-04-15 00:22:02 -07002643 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002644}
2645
Ying Xue6398e232014-01-17 09:50:04 +08002646static int tipc_wait_for_accept(struct socket *sock, long timeo)
2647{
2648 struct sock *sk = sock->sk;
2649 DEFINE_WAIT(wait);
2650 int err;
2651
2652 /* True wake-one mechanism for incoming connections: only
2653 * one process gets woken up, not the 'whole herd'.
2654 * Since we do not 'race & poll' for established sockets
2655 * anymore, the common case will execute the loop only once.
2656 */
2657 for (;;) {
2658 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2659 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01002660 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08002661 release_sock(sk);
2662 timeo = schedule_timeout(timeo);
2663 lock_sock(sk);
2664 }
2665 err = 0;
2666 if (!skb_queue_empty(&sk->sk_receive_queue))
2667 break;
Ying Xue6398e232014-01-17 09:50:04 +08002668 err = -EAGAIN;
2669 if (!timeo)
2670 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002671 err = sock_intr_errno(timeo);
2672 if (signal_pending(current))
2673 break;
Ying Xue6398e232014-01-17 09:50:04 +08002674 }
2675 finish_wait(sk_sleep(sk), &wait);
2676 return err;
2677}
2678
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002679/**
Ying Xue247f0f32014-02-18 16:06:46 +08002680 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002681 * @sock: listening socket
Andrew Lunnd8141202020-07-13 01:15:14 +02002682 * @new_sock: new socket that is to be connected
Per Lidenb97bf3f2006-01-02 19:04:38 +01002683 * @flags: file-related flags associated with socket
Randy Dunlapf172f4b2020-11-29 10:32:49 -08002684 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002685 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08002686 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01002687 */
David Howellscdfbabf2017-03-09 08:09:05 +00002688static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
2689 bool kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002690{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002691 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002692 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002693 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002694 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002695 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002696 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002697
Allan Stephens0c3141e2008-04-15 00:22:02 -07002698 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002699
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002700 if (sk->sk_state != TIPC_LISTEN) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002701 res = -EINVAL;
2702 goto exit;
2703 }
Ying Xue6398e232014-01-17 09:50:04 +08002704 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2705 res = tipc_wait_for_accept(sock, timeo);
2706 if (res)
2707 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002708
2709 buf = skb_peek(&sk->sk_receive_queue);
2710
David Howellscdfbabf2017-03-09 08:09:05 +00002711 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002712 if (res)
2713 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002714 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002715
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002716 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002717 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002718 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002719
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002720 /* we lock on new_sk; but lockdep sees the lock on sk */
2721 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002722
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002723 /*
2724 * Reject any stray messages received by new socket
2725 * before the socket lock was taken (very, very unlikely)
2726 */
Tuong Lien49afb802020-01-08 09:18:15 +07002727 tsk_rej_rx_queue(new_sk, TIPC_ERR_NO_PORT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002728
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002729 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002730 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002731
Christoph Hellwig095ae612020-05-28 07:12:36 +02002732 tsk_set_importance(new_sk, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002733 if (msg_named(msg)) {
Jon Maloy14623e02021-06-02 13:44:24 -04002734 new_tsock->conn_addrtype = TIPC_SERVICE_ADDR;
2735 msg_set_nametype(&new_tsock->phdr, msg_nametype(msg));
2736 msg_set_nameinst(&new_tsock->phdr, msg_nameinst(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002737 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002738
2739 /*
2740 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2741 * Respond to 'SYN+' by queuing it on new socket.
2742 */
2743 if (!msg_data_sz(msg)) {
2744 struct msghdr m = {NULL,};
2745
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002746 tsk_advance_rx_queue(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002747 __tipc_sendstream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002748 } else {
2749 __skb_dequeue(&sk->sk_receive_queue);
2750 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002751 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002752 }
2753 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002754exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002755 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002756 return res;
2757}
2758
2759/**
Ying Xue247f0f32014-02-18 16:06:46 +08002760 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002761 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002762 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002763 *
2764 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002765 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08002766 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01002767 */
Ying Xue247f0f32014-02-18 16:06:46 +08002768static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002769{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002770 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002771 int res;
2772
Allan Stephense247a8f2008-03-06 15:05:38 -08002773 if (how != SHUT_RDWR)
2774 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002775
Allan Stephens0c3141e2008-04-15 00:22:02 -07002776 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002777
Tuong Lien01e661e2018-12-19 09:17:58 +07002778 trace_tipc_sk_shutdown(sk, NULL, TIPC_DUMP_ALL, " ");
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002779 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
Tetsuo Handaa4b5cc92020-09-05 15:14:47 +09002780 sk->sk_shutdown = SHUTDOWN_MASK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002781
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002782 if (sk->sk_state == TIPC_DISCONNECTING) {
Ying Xue75031152012-10-29 09:38:15 -04002783 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002784 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002785
Per Lidenb97bf3f2006-01-02 19:04:38 +01002786 res = 0;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002787 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002788 res = -ENOTCONN;
2789 }
Tetsuo Handa2a638662020-09-02 22:44:16 +09002790 /* Wake up anyone sleeping in poll. */
2791 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002792
Allan Stephens0c3141e2008-04-15 00:22:02 -07002793 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002794 return res;
2795}
2796
Jon Maloyafe87922018-09-28 20:23:19 +02002797static void tipc_sk_check_probing_state(struct sock *sk,
2798 struct sk_buff_head *list)
2799{
2800 struct tipc_sock *tsk = tipc_sk(sk);
2801 u32 pnode = tsk_peer_node(tsk);
2802 u32 pport = tsk_peer_port(tsk);
2803 u32 self = tsk_own_node(tsk);
2804 u32 oport = tsk->portid;
2805 struct sk_buff *skb;
2806
2807 if (tsk->probe_unacked) {
2808 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2809 sk->sk_err = ECONNABORTED;
2810 tipc_node_remove_conn(sock_net(sk), pnode, pport);
2811 sk->sk_state_change(sk);
2812 return;
2813 }
2814 /* Prepare new probe */
2815 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, 0,
2816 pnode, self, pport, oport, TIPC_OK);
2817 if (skb)
2818 __skb_queue_tail(list, skb);
2819 tsk->probe_unacked = true;
2820 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
2821}
2822
Tung Nguyen67879272018-09-28 20:23:22 +02002823static void tipc_sk_retry_connect(struct sock *sk, struct sk_buff_head *list)
2824{
2825 struct tipc_sock *tsk = tipc_sk(sk);
2826
2827 /* Try again later if dest link is congested */
2828 if (tsk->cong_link_cnt) {
2829 sk_reset_timer(sk, &sk->sk_timer, msecs_to_jiffies(100));
2830 return;
2831 }
2832 /* Prepare SYN for retransmit */
2833 tipc_msg_skb_clone(&sk->sk_write_queue, list);
2834}
2835
Kees Cook31b102b2017-10-30 14:06:45 -07002836static void tipc_sk_timeout(struct timer_list *t)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002837{
Kees Cook31b102b2017-10-30 14:06:45 -07002838 struct sock *sk = from_timer(sk, t, sk_timer);
2839 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloyafe87922018-09-28 20:23:19 +02002840 u32 pnode = tsk_peer_node(tsk);
2841 struct sk_buff_head list;
Tung Nguyen67879272018-09-28 20:23:22 +02002842 int rc = 0;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002843
Jon Maloye654f9f2019-08-15 16:42:50 +02002844 __skb_queue_head_init(&list);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002845 bh_lock_sock(sk);
Jon Maloy0d5fcebf2017-10-20 11:21:32 +02002846
2847 /* Try again later if socket is busy */
2848 if (sock_owned_by_user(sk)) {
2849 sk_reset_timer(sk, &sk->sk_timer, jiffies + HZ / 20);
Jon Maloyafe87922018-09-28 20:23:19 +02002850 bh_unlock_sock(sk);
Tung Nguyen91a4a3e2019-11-28 10:10:06 +07002851 sock_put(sk);
Jon Maloyafe87922018-09-28 20:23:19 +02002852 return;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002853 }
Jon Paul Maloy57289012014-08-22 18:09:09 -04002854
Jon Maloyafe87922018-09-28 20:23:19 +02002855 if (sk->sk_state == TIPC_ESTABLISHED)
2856 tipc_sk_check_probing_state(sk, &list);
Tung Nguyen67879272018-09-28 20:23:22 +02002857 else if (sk->sk_state == TIPC_CONNECTING)
2858 tipc_sk_retry_connect(sk, &list);
Jon Maloyafe87922018-09-28 20:23:19 +02002859
Jon Paul Maloy57289012014-08-22 18:09:09 -04002860 bh_unlock_sock(sk);
Jon Maloyafe87922018-09-28 20:23:19 +02002861
2862 if (!skb_queue_empty(&list))
Tung Nguyen67879272018-09-28 20:23:22 +02002863 rc = tipc_node_xmit(sock_net(sk), &list, pnode, tsk->portid);
Jon Maloyafe87922018-09-28 20:23:19 +02002864
Tung Nguyen67879272018-09-28 20:23:22 +02002865 /* SYN messages may cause link congestion */
2866 if (rc == -ELINKCONG) {
2867 tipc_dest_push(&tsk->cong_links, pnode, 0);
2868 tsk->cong_link_cnt = 1;
2869 }
Ying Xue07f6c4b2015-01-07 13:41:58 +08002870 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002871}
2872
Jon Maloy50a34992021-03-16 22:06:11 -04002873static int tipc_sk_publish(struct tipc_sock *tsk, struct tipc_uaddr *ua)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002874{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002875 struct sock *sk = &tsk->sk;
2876 struct net *net = sock_net(sk);
Jon Maloy50a34992021-03-16 22:06:11 -04002877 struct tipc_socket_addr skaddr;
2878 struct publication *p;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002879 u32 key;
2880
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002881 if (tipc_sk_connected(sk))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002882 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002883 key = tsk->portid + tsk->pub_count + 1;
2884 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002885 return -EADDRINUSE;
Jon Maloy50a34992021-03-16 22:06:11 -04002886 skaddr.ref = tsk->portid;
2887 skaddr.node = tipc_own_addr(net);
2888 p = tipc_nametbl_publish(net, ua, &skaddr, key);
2889 if (unlikely(!p))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002890 return -EINVAL;
2891
Jon Maloy50a34992021-03-16 22:06:11 -04002892 list_add(&p->binding_sock, &tsk->publications);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002893 tsk->pub_count++;
Jon Maloy50a34992021-03-16 22:06:11 -04002894 tsk->published = true;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002895 return 0;
2896}
2897
Jon Maloy2c98da02021-03-16 22:06:13 -04002898static int tipc_sk_withdraw(struct tipc_sock *tsk, struct tipc_uaddr *ua)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002899{
Ying Xuef2f98002015-01-09 15:27:05 +08002900 struct net *net = sock_net(&tsk->sk);
Jon Maloy2c98da02021-03-16 22:06:13 -04002901 struct publication *safe, *p;
2902 struct tipc_uaddr _ua;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002903 int rc = -EINVAL;
2904
Jon Maloy998d3902021-03-16 22:06:08 -04002905 list_for_each_entry_safe(p, safe, &tsk->publications, binding_sock) {
Jon Maloy2c98da02021-03-16 22:06:13 -04002906 if (!ua) {
2907 tipc_uaddr(&_ua, TIPC_SERVICE_RANGE, p->scope,
2908 p->sr.type, p->sr.lower, p->sr.upper);
2909 tipc_nametbl_withdraw(net, &_ua, &p->sk, p->key);
2910 continue;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002911 }
Jon Maloy2c98da02021-03-16 22:06:13 -04002912 /* Unbind specific publication */
2913 if (p->scope != ua->scope)
2914 continue;
2915 if (p->sr.type != ua->sr.type)
2916 continue;
2917 if (p->sr.lower != ua->sr.lower)
2918 continue;
2919 if (p->sr.upper != ua->sr.upper)
2920 break;
2921 tipc_nametbl_withdraw(net, ua, &p->sk, p->key);
2922 rc = 0;
2923 break;
2924 }
2925 if (list_empty(&tsk->publications)) {
2926 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002927 rc = 0;
2928 }
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002929 return rc;
2930}
2931
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002932/* tipc_sk_reinit: set non-zero address in all existing sockets
2933 * when we go from standalone to network mode.
2934 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002935void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002936{
Ying Xuee05b31f2015-01-09 15:27:08 +08002937 struct tipc_net *tn = net_generic(net, tipc_net_id);
Herbert Xu40f9f432017-02-11 19:26:46 +08002938 struct rhashtable_iter iter;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002939 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002940 struct tipc_msg *msg;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002941
Herbert Xu40f9f432017-02-11 19:26:46 +08002942 rhashtable_walk_enter(&tn->sk_rht, &iter);
2943
2944 do {
Tom Herbert97a6ec42017-12-04 10:31:41 -08002945 rhashtable_walk_start(&iter);
Herbert Xu40f9f432017-02-11 19:26:46 +08002946
2947 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
Cong Wang15ef70e2018-12-10 11:49:55 -08002948 sock_hold(&tsk->sk);
2949 rhashtable_walk_stop(&iter);
2950 lock_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002951 msg = &tsk->phdr;
Jon Maloy23fd3ea2018-03-22 20:42:49 +01002952 msg_set_prevnode(msg, tipc_own_addr(net));
2953 msg_set_orignode(msg, tipc_own_addr(net));
Cong Wang15ef70e2018-12-10 11:49:55 -08002954 release_sock(&tsk->sk);
2955 rhashtable_walk_start(&iter);
2956 sock_put(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002957 }
Tom Herbert97a6ec42017-12-04 10:31:41 -08002958
Herbert Xu40f9f432017-02-11 19:26:46 +08002959 rhashtable_walk_stop(&iter);
2960 } while (tsk == ERR_PTR(-EAGAIN));
Cong Wangbd583fe2018-08-23 16:19:44 -07002961
2962 rhashtable_walk_exit(&iter);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002963}
2964
Ying Xuee05b31f2015-01-09 15:27:08 +08002965static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002966{
Ying Xuee05b31f2015-01-09 15:27:08 +08002967 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002968 struct tipc_sock *tsk;
2969
2970 rcu_read_lock();
Taehee Yooab818362019-11-22 08:15:19 +00002971 tsk = rhashtable_lookup(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002972 if (tsk)
2973 sock_hold(&tsk->sk);
2974 rcu_read_unlock();
2975
2976 return tsk;
2977}
2978
2979static int tipc_sk_insert(struct tipc_sock *tsk)
2980{
Ying Xuee05b31f2015-01-09 15:27:08 +08002981 struct sock *sk = &tsk->sk;
2982 struct net *net = sock_net(sk);
2983 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002984 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2985 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2986
2987 while (remaining--) {
2988 portid++;
2989 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2990 portid = TIPC_MIN_PORT;
2991 tsk->portid = portid;
2992 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002993 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2994 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002995 return 0;
2996 sock_put(&tsk->sk);
2997 }
2998
2999 return -1;
3000}
3001
3002static void tipc_sk_remove(struct tipc_sock *tsk)
3003{
3004 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08003005 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08003006
Herbert Xu6cca72892015-03-20 21:57:05 +11003007 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003008 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
Ying Xue07f6c4b2015-01-07 13:41:58 +08003009 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04003010 }
3011}
3012
Herbert Xu6cca72892015-03-20 21:57:05 +11003013static const struct rhashtable_params tsk_rht_params = {
3014 .nelem_hint = 192,
3015 .head_offset = offsetof(struct tipc_sock, node),
3016 .key_offset = offsetof(struct tipc_sock, portid),
3017 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11003018 .max_size = 1048576,
3019 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00003020 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11003021};
3022
Ying Xuee05b31f2015-01-09 15:27:08 +08003023int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003024{
Ying Xuee05b31f2015-01-09 15:27:08 +08003025 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003026
Herbert Xu6cca72892015-03-20 21:57:05 +11003027 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003028}
3029
Ying Xuee05b31f2015-01-09 15:27:08 +08003030void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003031{
Ying Xuee05b31f2015-01-09 15:27:08 +08003032 struct tipc_net *tn = net_generic(net, tipc_net_id);
3033
Ying Xue07f6c4b2015-01-07 13:41:58 +08003034 /* Wait for socket readers to complete */
3035 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003036
Ying Xuee05b31f2015-01-09 15:27:08 +08003037 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003038}
3039
Jon Maloy75da2162017-10-13 11:04:23 +02003040static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq)
3041{
3042 struct net *net = sock_net(&tsk->sk);
Jon Maloy75da2162017-10-13 11:04:23 +02003043 struct tipc_group *grp = tsk->group;
3044 struct tipc_msg *hdr = &tsk->phdr;
Jon Maloy50a34992021-03-16 22:06:11 -04003045 struct tipc_uaddr ua;
Jon Maloy75da2162017-10-13 11:04:23 +02003046 int rc;
3047
3048 if (mreq->type < TIPC_RESERVED_TYPES)
3049 return -EACCES;
Jon Maloy232d07b2018-01-08 21:03:30 +01003050 if (mreq->scope > TIPC_NODE_SCOPE)
3051 return -EINVAL;
Jon Maloy50a34992021-03-16 22:06:11 -04003052 if (mreq->scope != TIPC_NODE_SCOPE)
3053 mreq->scope = TIPC_CLUSTER_SCOPE;
Jon Maloy75da2162017-10-13 11:04:23 +02003054 if (grp)
3055 return -EACCES;
Jon Maloy60c25302018-01-17 16:42:46 +01003056 grp = tipc_group_create(net, tsk->portid, mreq, &tsk->group_is_open);
Jon Maloy75da2162017-10-13 11:04:23 +02003057 if (!grp)
3058 return -ENOMEM;
3059 tsk->group = grp;
3060 msg_set_lookup_scope(hdr, mreq->scope);
3061 msg_set_nametype(hdr, mreq->type);
3062 msg_set_dest_droppable(hdr, true);
Jon Maloy50a34992021-03-16 22:06:11 -04003063 tipc_uaddr(&ua, TIPC_SERVICE_RANGE, mreq->scope,
3064 mreq->type, mreq->instance, mreq->instance);
Jon Maloy6e448672021-03-16 22:06:20 -04003065 tipc_nametbl_build_group(net, grp, &ua);
Jon Maloy50a34992021-03-16 22:06:11 -04003066 rc = tipc_sk_publish(tsk, &ua);
Cong Wange233df02017-10-24 15:44:49 -07003067 if (rc) {
Jon Maloy75da2162017-10-13 11:04:23 +02003068 tipc_group_delete(net, grp);
Cong Wange233df02017-10-24 15:44:49 -07003069 tsk->group = NULL;
Jon Maloyfebafc82018-01-10 21:08:50 +01003070 return rc;
Cong Wange233df02017-10-24 15:44:49 -07003071 }
Jon Maloyd12d2e12018-01-08 21:03:28 +01003072 /* Eliminate any risk that a broadcast overtakes sent JOINs */
Jon Maloy399574d2017-10-13 11:04:32 +02003073 tsk->mc_method.rcast = true;
3074 tsk->mc_method.mandatory = true;
Jon Maloyd12d2e12018-01-08 21:03:28 +01003075 tipc_group_join(net, grp, &tsk->sk.sk_rcvbuf);
Jon Maloy75da2162017-10-13 11:04:23 +02003076 return rc;
3077}
3078
3079static int tipc_sk_leave(struct tipc_sock *tsk)
3080{
3081 struct net *net = sock_net(&tsk->sk);
3082 struct tipc_group *grp = tsk->group;
Jon Maloy2c98da02021-03-16 22:06:13 -04003083 struct tipc_uaddr ua;
Jon Maloy75da2162017-10-13 11:04:23 +02003084 int scope;
3085
3086 if (!grp)
3087 return -EINVAL;
Jon Maloy2c98da02021-03-16 22:06:13 -04003088 ua.addrtype = TIPC_SERVICE_RANGE;
3089 tipc_group_self(grp, &ua.sr, &scope);
3090 ua.scope = scope;
Jon Maloy75da2162017-10-13 11:04:23 +02003091 tipc_group_delete(net, grp);
3092 tsk->group = NULL;
Jon Maloy2c98da02021-03-16 22:06:13 -04003093 tipc_sk_withdraw(tsk, &ua);
Jon Maloy75da2162017-10-13 11:04:23 +02003094 return 0;
3095}
3096
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003097/**
Ying Xue247f0f32014-02-18 16:06:46 +08003098 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01003099 * @sock: socket structure
3100 * @lvl: option level
3101 * @opt: option identifier
3102 * @ov: pointer to new option value
3103 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003104 *
3105 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01003106 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003107 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08003108 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01003109 */
Ying Xue247f0f32014-02-18 16:06:46 +08003110static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003111 sockptr_t ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003112{
Allan Stephens0c3141e2008-04-15 00:22:02 -07003113 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04003114 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy75da2162017-10-13 11:04:23 +02003115 struct tipc_group_req mreq;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003116 u32 value = 0;
Dan Carpentera08ef472017-01-24 12:49:35 +03003117 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003118
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003119 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
3120 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003121 if (lvl != SOL_TIPC)
3122 return -ENOPROTOOPT;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003123
3124 switch (opt) {
3125 case TIPC_IMPORTANCE:
3126 case TIPC_SRC_DROPPABLE:
3127 case TIPC_DEST_DROPPABLE:
3128 case TIPC_CONN_TIMEOUT:
Jon Maloyc0bceb92019-10-30 14:00:41 +01003129 case TIPC_NODELAY:
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003130 if (ol < sizeof(value))
3131 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003132 if (copy_from_sockptr(&value, ov, sizeof(u32)))
Jon Maloy75da2162017-10-13 11:04:23 +02003133 return -EFAULT;
3134 break;
3135 case TIPC_GROUP_JOIN:
3136 if (ol < sizeof(mreq))
3137 return -EINVAL;
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003138 if (copy_from_sockptr(&mreq, ov, sizeof(mreq)))
Jon Maloy75da2162017-10-13 11:04:23 +02003139 return -EFAULT;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003140 break;
3141 default:
Christoph Hellwiga7b75c52020-07-23 08:09:07 +02003142 if (!sockptr_is_null(ov) || ol)
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003143 return -EINVAL;
3144 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01003145
Allan Stephens0c3141e2008-04-15 00:22:02 -07003146 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003147
Per Lidenb97bf3f2006-01-02 19:04:38 +01003148 switch (opt) {
3149 case TIPC_IMPORTANCE:
Christoph Hellwig095ae612020-05-28 07:12:36 +02003150 res = tsk_set_importance(sk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003151 break;
3152 case TIPC_SRC_DROPPABLE:
3153 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003154 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003155 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01003156 res = -ENOPROTOOPT;
3157 break;
3158 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003159 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003160 break;
3161 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04003162 tipc_sk(sk)->conn_timeout = value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003163 break;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003164 case TIPC_MCAST_BROADCAST:
3165 tsk->mc_method.rcast = false;
3166 tsk->mc_method.mandatory = true;
3167 break;
3168 case TIPC_MCAST_REPLICAST:
3169 tsk->mc_method.rcast = true;
3170 tsk->mc_method.mandatory = true;
3171 break;
Jon Maloy75da2162017-10-13 11:04:23 +02003172 case TIPC_GROUP_JOIN:
3173 res = tipc_sk_join(tsk, &mreq);
3174 break;
3175 case TIPC_GROUP_LEAVE:
3176 res = tipc_sk_leave(tsk);
3177 break;
Jon Maloyc0bceb92019-10-30 14:00:41 +01003178 case TIPC_NODELAY:
3179 tsk->nodelay = !!value;
3180 tsk_set_nagle(tsk);
3181 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003182 default:
3183 res = -EINVAL;
3184 }
3185
Allan Stephens0c3141e2008-04-15 00:22:02 -07003186 release_sock(sk);
3187
Per Lidenb97bf3f2006-01-02 19:04:38 +01003188 return res;
3189}
3190
3191/**
Ying Xue247f0f32014-02-18 16:06:46 +08003192 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01003193 * @sock: socket structure
3194 * @lvl: option level
3195 * @opt: option identifier
3196 * @ov: receptacle for option value
3197 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003198 *
3199 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01003200 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003201 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08003202 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01003203 */
Ying Xue247f0f32014-02-18 16:06:46 +08003204static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
3205 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003206{
Allan Stephens0c3141e2008-04-15 00:22:02 -07003207 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04003208 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloyb6f88d92020-11-25 13:29:15 -05003209 struct tipc_service_range seq;
Jon Maloy75da2162017-10-13 11:04:23 +02003210 int len, scope;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003211 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003212 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003213
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003214 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
3215 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003216 if (lvl != SOL_TIPC)
3217 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00003218 res = get_user(len, ol);
3219 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003220 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003221
Allan Stephens0c3141e2008-04-15 00:22:02 -07003222 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003223
3224 switch (opt) {
3225 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003226 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003227 break;
3228 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003229 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003230 break;
3231 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003232 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003233 break;
3234 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003235 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07003236 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01003237 break;
Allan Stephens0e659672010-12-31 18:59:32 +00003238 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05003239 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00003240 break;
Allan Stephens0e659672010-12-31 18:59:32 +00003241 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00003242 value = skb_queue_len(&sk->sk_receive_queue);
3243 break;
Tung Nguyen42e54252019-04-18 21:02:19 +07003244 case TIPC_SOCK_RECVQ_USED:
3245 value = sk_rmem_alloc_get(sk);
3246 break;
Jon Maloy75da2162017-10-13 11:04:23 +02003247 case TIPC_GROUP_JOIN:
3248 seq.type = 0;
3249 if (tsk->group)
3250 tipc_group_self(tsk->group, &seq, &scope);
3251 value = seq.type;
3252 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003253 default:
3254 res = -EINVAL;
3255 }
3256
Allan Stephens0c3141e2008-04-15 00:22:02 -07003257 release_sock(sk);
3258
Paul Gortmaker25860c32010-12-31 18:59:31 +00003259 if (res)
3260 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01003261
Paul Gortmaker25860c32010-12-31 18:59:31 +00003262 if (len < sizeof(value))
3263 return -EINVAL;
3264
3265 if (copy_to_user(ov, &value, sizeof(value)))
3266 return -EFAULT;
3267
3268 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003269}
3270
Ying Xuef2f98002015-01-09 15:27:05 +08003271static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02003272{
Jon Maloy3e5cf362018-04-25 19:29:36 +02003273 struct net *net = sock_net(sock->sk);
3274 struct tipc_sioc_nodeid_req nr = {0};
Erik Hugne78acb1f2014-04-24 16:26:47 +02003275 struct tipc_sioc_ln_req lnr;
3276 void __user *argp = (void __user *)arg;
3277
3278 switch (cmd) {
3279 case SIOCGETLINKNAME:
3280 if (copy_from_user(&lnr, argp, sizeof(lnr)))
3281 return -EFAULT;
Jon Maloy3e5cf362018-04-25 19:29:36 +02003282 if (!tipc_node_get_linkname(net,
Ying Xuef2f98002015-01-09 15:27:05 +08003283 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02003284 lnr.linkname, TIPC_MAX_LINK_NAME)) {
3285 if (copy_to_user(argp, &lnr, sizeof(lnr)))
3286 return -EFAULT;
3287 return 0;
3288 }
3289 return -EADDRNOTAVAIL;
Jon Maloy3e5cf362018-04-25 19:29:36 +02003290 case SIOCGETNODEID:
3291 if (copy_from_user(&nr, argp, sizeof(nr)))
3292 return -EFAULT;
3293 if (!tipc_node_get_id(net, nr.peer, nr.node_id))
3294 return -EADDRNOTAVAIL;
3295 if (copy_to_user(argp, &nr, sizeof(nr)))
3296 return -EFAULT;
3297 return 0;
Erik Hugne78acb1f2014-04-24 16:26:47 +02003298 default:
3299 return -ENOIOCTLCMD;
3300 }
3301}
3302
Erik Hugne70b03752017-03-29 11:22:16 +02003303static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
3304{
3305 struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
3306 struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
Erik Hugne66bc1e82017-03-29 11:22:17 +02003307 u32 onode = tipc_own_addr(sock_net(sock1->sk));
Erik Hugne70b03752017-03-29 11:22:16 +02003308
Erik Hugne66bc1e82017-03-29 11:22:17 +02003309 tsk1->peer.family = AF_TIPC;
Jon Maloyb6f88d92020-11-25 13:29:15 -05003310 tsk1->peer.addrtype = TIPC_SOCKET_ADDR;
Erik Hugne66bc1e82017-03-29 11:22:17 +02003311 tsk1->peer.scope = TIPC_NODE_SCOPE;
3312 tsk1->peer.addr.id.ref = tsk2->portid;
3313 tsk1->peer.addr.id.node = onode;
3314 tsk2->peer.family = AF_TIPC;
Jon Maloyb6f88d92020-11-25 13:29:15 -05003315 tsk2->peer.addrtype = TIPC_SOCKET_ADDR;
Erik Hugne66bc1e82017-03-29 11:22:17 +02003316 tsk2->peer.scope = TIPC_NODE_SCOPE;
3317 tsk2->peer.addr.id.ref = tsk1->portid;
3318 tsk2->peer.addr.id.node = onode;
3319
3320 tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
3321 tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
Erik Hugne70b03752017-03-29 11:22:16 +02003322 return 0;
3323}
3324
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00003325/* Protocol switches for the various types of TIPC sockets */
3326
Florian Westphalbca65ea2008-02-07 18:18:01 -08003327static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00003328 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003329 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08003330 .release = tipc_release,
3331 .bind = tipc_bind,
3332 .connect = tipc_connect,
Erik Hugne66bc1e82017-03-29 11:22:17 +02003333 .socketpair = tipc_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04003334 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08003335 .getname = tipc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003336 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02003337 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04003338 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08003339 .shutdown = tipc_shutdown,
3340 .setsockopt = tipc_setsockopt,
3341 .getsockopt = tipc_getsockopt,
3342 .sendmsg = tipc_sendmsg,
3343 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09003344 .mmap = sock_no_mmap,
3345 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01003346};
3347
Florian Westphalbca65ea2008-02-07 18:18:01 -08003348static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00003349 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003350 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08003351 .release = tipc_release,
3352 .bind = tipc_bind,
3353 .connect = tipc_connect,
Erik Hugne70b03752017-03-29 11:22:16 +02003354 .socketpair = tipc_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08003355 .accept = tipc_accept,
3356 .getname = tipc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003357 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02003358 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08003359 .listen = tipc_listen,
3360 .shutdown = tipc_shutdown,
3361 .setsockopt = tipc_setsockopt,
3362 .getsockopt = tipc_getsockopt,
3363 .sendmsg = tipc_send_packet,
3364 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09003365 .mmap = sock_no_mmap,
3366 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01003367};
3368
Florian Westphalbca65ea2008-02-07 18:18:01 -08003369static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00003370 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003371 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08003372 .release = tipc_release,
3373 .bind = tipc_bind,
3374 .connect = tipc_connect,
Erik Hugne70b03752017-03-29 11:22:16 +02003375 .socketpair = tipc_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08003376 .accept = tipc_accept,
3377 .getname = tipc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003378 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02003379 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08003380 .listen = tipc_listen,
3381 .shutdown = tipc_shutdown,
3382 .setsockopt = tipc_setsockopt,
3383 .getsockopt = tipc_getsockopt,
Jon Paul Maloy365ad352017-01-03 10:55:11 -05003384 .sendmsg = tipc_sendstream,
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02003385 .recvmsg = tipc_recvstream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09003386 .mmap = sock_no_mmap,
3387 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01003388};
3389
Florian Westphalbca65ea2008-02-07 18:18:01 -08003390static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00003391 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003392 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04003393 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01003394};
3395
3396static struct proto tipc_proto = {
3397 .name = "TIPC",
3398 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04003399 .obj_size = sizeof(struct tipc_sock),
3400 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01003401};
3402
3403/**
Per Liden4323add2006-01-18 00:38:21 +01003404 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003405 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08003406 * Return: 0 on success, errno otherwise
Per Lidenb97bf3f2006-01-02 19:04:38 +01003407 */
Per Liden4323add2006-01-18 00:38:21 +01003408int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003409{
3410 int res;
3411
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003412 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003413 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04003414 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01003415 goto out;
3416 }
3417
3418 res = sock_register(&tipc_family_ops);
3419 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04003420 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01003421 proto_unregister(&tipc_proto);
3422 goto out;
3423 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01003424 out:
3425 return res;
3426}
3427
3428/**
Per Liden4323add2006-01-18 00:38:21 +01003429 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01003430 */
Per Liden4323add2006-01-18 00:38:21 +01003431void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003432{
Per Lidenb97bf3f2006-01-02 19:04:38 +01003433 sock_unregister(tipc_family_ops.family);
3434 proto_unregister(&tipc_proto);
3435}
Richard Alpe34b78a122014-11-20 10:29:10 +01003436
3437/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01003438static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01003439{
Jon Maloy14623e02021-06-02 13:44:24 -04003440 u32 peer_node, peer_port;
3441 u32 conn_type, conn_instance;
Richard Alpe34b78a122014-11-20 10:29:10 +01003442 struct nlattr *nest;
3443
3444 peer_node = tsk_peer_node(tsk);
3445 peer_port = tsk_peer_port(tsk);
Jon Maloy14623e02021-06-02 13:44:24 -04003446 conn_type = msg_nametype(&tsk->phdr);
3447 conn_instance = msg_nameinst(&tsk->phdr);
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003448 nest = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_CON);
Kangjie Lu517ccc22019-03-16 16:46:05 -05003449 if (!nest)
3450 return -EMSGSIZE;
Richard Alpe34b78a122014-11-20 10:29:10 +01003451
3452 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
3453 goto msg_full;
3454 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
3455 goto msg_full;
3456
Jon Maloy14623e02021-06-02 13:44:24 -04003457 if (tsk->conn_addrtype != 0) {
Richard Alpe34b78a122014-11-20 10:29:10 +01003458 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
3459 goto msg_full;
Jon Maloy14623e02021-06-02 13:44:24 -04003460 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, conn_type))
Richard Alpe34b78a122014-11-20 10:29:10 +01003461 goto msg_full;
Jon Maloy14623e02021-06-02 13:44:24 -04003462 if (nla_put_u32(skb, TIPC_NLA_CON_INST, conn_instance))
Richard Alpe34b78a122014-11-20 10:29:10 +01003463 goto msg_full;
3464 }
3465 nla_nest_end(skb, nest);
3466
3467 return 0;
3468
3469msg_full:
3470 nla_nest_cancel(skb, nest);
3471
3472 return -EMSGSIZE;
3473}
3474
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003475static int __tipc_nl_add_sk_info(struct sk_buff *skb, struct tipc_sock
3476 *tsk)
3477{
3478 struct net *net = sock_net(skb->sk);
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003479 struct sock *sk = &tsk->sk;
3480
3481 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid) ||
Jon Maloy23fd3ea2018-03-22 20:42:49 +01003482 nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr(net)))
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003483 return -EMSGSIZE;
3484
3485 if (tipc_sk_connected(sk)) {
3486 if (__tipc_nl_add_sk_con(skb, tsk))
3487 return -EMSGSIZE;
3488 } else if (!list_empty(&tsk->publications)) {
3489 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
3490 return -EMSGSIZE;
3491 }
3492 return 0;
3493}
3494
Richard Alpe34b78a122014-11-20 10:29:10 +01003495/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01003496static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
3497 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01003498{
Richard Alpe34b78a122014-11-20 10:29:10 +01003499 struct nlattr *attrs;
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003500 void *hdr;
Richard Alpe34b78a122014-11-20 10:29:10 +01003501
3502 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01003503 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01003504 if (!hdr)
3505 goto msg_cancel;
3506
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003507 attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK);
Richard Alpe34b78a122014-11-20 10:29:10 +01003508 if (!attrs)
3509 goto genlmsg_cancel;
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003510
3511 if (__tipc_nl_add_sk_info(skb, tsk))
Richard Alpe34b78a122014-11-20 10:29:10 +01003512 goto attr_msg_cancel;
3513
Richard Alpe34b78a122014-11-20 10:29:10 +01003514 nla_nest_end(skb, attrs);
3515 genlmsg_end(skb, hdr);
3516
3517 return 0;
3518
3519attr_msg_cancel:
3520 nla_nest_cancel(skb, attrs);
3521genlmsg_cancel:
3522 genlmsg_cancel(skb, hdr);
3523msg_cancel:
3524 return -EMSGSIZE;
3525}
3526
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003527int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,
3528 int (*skb_handler)(struct sk_buff *skb,
3529 struct netlink_callback *cb,
3530 struct tipc_sock *tsk))
Richard Alpe34b78a122014-11-20 10:29:10 +01003531{
Cong Wang8f5c5fc2018-09-04 14:54:55 -07003532 struct rhashtable_iter *iter = (void *)cb->args[4];
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003533 struct tipc_sock *tsk;
3534 int err;
Richard Alpe34b78a122014-11-20 10:29:10 +01003535
Cong Wang9a07efa2018-08-24 12:28:06 -07003536 rhashtable_walk_start(iter);
3537 while ((tsk = rhashtable_walk_next(iter)) != NULL) {
3538 if (IS_ERR(tsk)) {
3539 err = PTR_ERR(tsk);
3540 if (err == -EAGAIN) {
3541 err = 0;
Richard Alped6e164e2015-01-16 12:30:40 +01003542 continue;
3543 }
Cong Wang9a07efa2018-08-24 12:28:06 -07003544 break;
Ying Xue07f6c4b2015-01-07 13:41:58 +08003545 }
Richard Alpe34b78a122014-11-20 10:29:10 +01003546
Cong Wang9a07efa2018-08-24 12:28:06 -07003547 sock_hold(&tsk->sk);
3548 rhashtable_walk_stop(iter);
3549 lock_sock(&tsk->sk);
3550 err = skb_handler(skb, cb, tsk);
3551 if (err) {
3552 release_sock(&tsk->sk);
3553 sock_put(&tsk->sk);
3554 goto out;
3555 }
3556 release_sock(&tsk->sk);
3557 rhashtable_walk_start(iter);
3558 sock_put(&tsk->sk);
3559 }
3560 rhashtable_walk_stop(iter);
3561out:
Richard Alpe34b78a122014-11-20 10:29:10 +01003562 return skb->len;
3563}
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003564EXPORT_SYMBOL(tipc_nl_sk_walk);
3565
Cong Wang9a07efa2018-08-24 12:28:06 -07003566int tipc_dump_start(struct netlink_callback *cb)
3567{
Cong Wang8f5c5fc2018-09-04 14:54:55 -07003568 return __tipc_dump_start(cb, sock_net(cb->skb->sk));
3569}
3570EXPORT_SYMBOL(tipc_dump_start);
3571
3572int __tipc_dump_start(struct netlink_callback *cb, struct net *net)
3573{
3574 /* tipc_nl_name_table_dump() uses cb->args[0...3]. */
3575 struct rhashtable_iter *iter = (void *)cb->args[4];
Cong Wang9a07efa2018-08-24 12:28:06 -07003576 struct tipc_net *tn = tipc_net(net);
3577
3578 if (!iter) {
3579 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3580 if (!iter)
3581 return -ENOMEM;
3582
Cong Wang8f5c5fc2018-09-04 14:54:55 -07003583 cb->args[4] = (long)iter;
Cong Wang9a07efa2018-08-24 12:28:06 -07003584 }
3585
3586 rhashtable_walk_enter(&tn->sk_rht, iter);
3587 return 0;
3588}
Cong Wang9a07efa2018-08-24 12:28:06 -07003589
3590int tipc_dump_done(struct netlink_callback *cb)
3591{
Cong Wang8f5c5fc2018-09-04 14:54:55 -07003592 struct rhashtable_iter *hti = (void *)cb->args[4];
Cong Wang9a07efa2018-08-24 12:28:06 -07003593
3594 rhashtable_walk_exit(hti);
3595 kfree(hti);
3596 return 0;
3597}
3598EXPORT_SYMBOL(tipc_dump_done);
3599
Cong Wange41f0542018-04-06 18:54:52 -07003600int tipc_sk_fill_sock_diag(struct sk_buff *skb, struct netlink_callback *cb,
3601 struct tipc_sock *tsk, u32 sk_filter_state,
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003602 u64 (*tipc_diag_gen_cookie)(struct sock *sk))
3603{
3604 struct sock *sk = &tsk->sk;
3605 struct nlattr *attrs;
3606 struct nlattr *stat;
3607
3608 /*filter response w.r.t sk_state*/
3609 if (!(sk_filter_state & (1 << sk->sk_state)))
3610 return 0;
3611
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003612 attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK);
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003613 if (!attrs)
3614 goto msg_cancel;
3615
3616 if (__tipc_nl_add_sk_info(skb, tsk))
3617 goto attr_msg_cancel;
3618
3619 if (nla_put_u32(skb, TIPC_NLA_SOCK_TYPE, (u32)sk->sk_type) ||
3620 nla_put_u32(skb, TIPC_NLA_SOCK_TIPC_STATE, (u32)sk->sk_state) ||
3621 nla_put_u32(skb, TIPC_NLA_SOCK_INO, sock_i_ino(sk)) ||
3622 nla_put_u32(skb, TIPC_NLA_SOCK_UID,
Cong Wange41f0542018-04-06 18:54:52 -07003623 from_kuid_munged(sk_user_ns(NETLINK_CB(cb->skb).sk),
GhantaKrishnamurthy MohanKrishna4b2e6872018-04-04 14:49:47 +02003624 sock_i_uid(sk))) ||
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003625 nla_put_u64_64bit(skb, TIPC_NLA_SOCK_COOKIE,
3626 tipc_diag_gen_cookie(sk),
3627 TIPC_NLA_SOCK_PAD))
3628 goto attr_msg_cancel;
3629
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003630 stat = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_STAT);
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003631 if (!stat)
3632 goto attr_msg_cancel;
3633
3634 if (nla_put_u32(skb, TIPC_NLA_SOCK_STAT_RCVQ,
3635 skb_queue_len(&sk->sk_receive_queue)) ||
3636 nla_put_u32(skb, TIPC_NLA_SOCK_STAT_SENDQ,
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01003637 skb_queue_len(&sk->sk_write_queue)) ||
3638 nla_put_u32(skb, TIPC_NLA_SOCK_STAT_DROP,
3639 atomic_read(&sk->sk_drops)))
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003640 goto stat_msg_cancel;
3641
3642 if (tsk->cong_link_cnt &&
3643 nla_put_flag(skb, TIPC_NLA_SOCK_STAT_LINK_CONG))
3644 goto stat_msg_cancel;
3645
3646 if (tsk_conn_cong(tsk) &&
3647 nla_put_flag(skb, TIPC_NLA_SOCK_STAT_CONN_CONG))
3648 goto stat_msg_cancel;
3649
3650 nla_nest_end(skb, stat);
GhantaKrishnamurthy MohanKrishnaa1be5a22018-06-29 13:26:18 +02003651
3652 if (tsk->group)
3653 if (tipc_group_fill_sock_diag(tsk->group, skb))
3654 goto stat_msg_cancel;
3655
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003656 nla_nest_end(skb, attrs);
3657
3658 return 0;
3659
3660stat_msg_cancel:
3661 nla_nest_cancel(skb, stat);
3662attr_msg_cancel:
3663 nla_nest_cancel(skb, attrs);
3664msg_cancel:
3665 return -EMSGSIZE;
3666}
3667EXPORT_SYMBOL(tipc_sk_fill_sock_diag);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003668
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003669int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
3670{
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003671 return tipc_nl_sk_walk(skb, cb, __tipc_nl_add_sk);
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003672}
3673
Richard Alpe1a1a1432014-11-20 10:29:11 +01003674/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01003675static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
3676 struct netlink_callback *cb,
3677 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01003678{
3679 void *hdr;
3680 struct nlattr *attrs;
3681
3682 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01003683 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003684 if (!hdr)
3685 goto msg_cancel;
3686
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003687 attrs = nla_nest_start_noflag(skb, TIPC_NLA_PUBL);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003688 if (!attrs)
3689 goto genlmsg_cancel;
3690
3691 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
3692 goto attr_msg_cancel;
Jon Maloy998d3902021-03-16 22:06:08 -04003693 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->sr.type))
Richard Alpe1a1a1432014-11-20 10:29:11 +01003694 goto attr_msg_cancel;
Jon Maloy998d3902021-03-16 22:06:08 -04003695 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->sr.lower))
Richard Alpe1a1a1432014-11-20 10:29:11 +01003696 goto attr_msg_cancel;
Jon Maloy998d3902021-03-16 22:06:08 -04003697 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->sr.upper))
Richard Alpe1a1a1432014-11-20 10:29:11 +01003698 goto attr_msg_cancel;
3699
3700 nla_nest_end(skb, attrs);
3701 genlmsg_end(skb, hdr);
3702
3703 return 0;
3704
3705attr_msg_cancel:
3706 nla_nest_cancel(skb, attrs);
3707genlmsg_cancel:
3708 genlmsg_cancel(skb, hdr);
3709msg_cancel:
3710 return -EMSGSIZE;
3711}
3712
3713/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01003714static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
3715 struct netlink_callback *cb,
3716 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01003717{
3718 int err;
3719 struct publication *p;
3720
3721 if (*last_publ) {
Jon Maloye50e73e2018-03-15 16:48:55 +01003722 list_for_each_entry(p, &tsk->publications, binding_sock) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01003723 if (p->key == *last_publ)
3724 break;
3725 }
3726 if (p->key != *last_publ) {
3727 /* We never set seq or call nl_dump_check_consistent()
3728 * this means that setting prev_seq here will cause the
3729 * consistence check to fail in the netlink callback
3730 * handler. Resulting in the last NLMSG_DONE message
3731 * having the NLM_F_DUMP_INTR flag set.
3732 */
3733 cb->prev_seq = 1;
3734 *last_publ = 0;
3735 return -EPIPE;
3736 }
3737 } else {
3738 p = list_first_entry(&tsk->publications, struct publication,
Jon Maloye50e73e2018-03-15 16:48:55 +01003739 binding_sock);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003740 }
3741
Jon Maloye50e73e2018-03-15 16:48:55 +01003742 list_for_each_entry_from(p, &tsk->publications, binding_sock) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01003743 err = __tipc_nl_add_sk_publ(skb, cb, p);
3744 if (err) {
3745 *last_publ = p->key;
3746 return err;
3747 }
3748 }
3749 *last_publ = 0;
3750
3751 return 0;
3752}
3753
3754int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
3755{
3756 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08003757 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01003758 u32 last_publ = cb->args[1];
3759 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08003760 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003761 struct tipc_sock *tsk;
3762
Ying Xue07f6c4b2015-01-07 13:41:58 +08003763 if (!tsk_portid) {
Jiri Pirko057af702019-10-05 20:04:39 +02003764 struct nlattr **attrs = genl_dumpit_info(cb)->attrs;
Richard Alpe1a1a1432014-11-20 10:29:11 +01003765 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
3766
Richard Alpe45e093a2016-05-16 11:14:54 +02003767 if (!attrs[TIPC_NLA_SOCK])
3768 return -EINVAL;
3769
Johannes Berg8cb08172019-04-26 14:07:28 +02003770 err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
3771 attrs[TIPC_NLA_SOCK],
3772 tipc_nl_sock_policy, NULL);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003773 if (err)
3774 return err;
3775
3776 if (!sock[TIPC_NLA_SOCK_REF])
3777 return -EINVAL;
3778
Ying Xue07f6c4b2015-01-07 13:41:58 +08003779 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003780 }
3781
3782 if (done)
3783 return 0;
3784
Ying Xuee05b31f2015-01-09 15:27:08 +08003785 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003786 if (!tsk)
3787 return -EINVAL;
3788
3789 lock_sock(&tsk->sk);
3790 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
3791 if (!err)
3792 done = 1;
3793 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08003794 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003795
Ying Xue07f6c4b2015-01-07 13:41:58 +08003796 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01003797 cb->args[1] = last_publ;
3798 cb->args[2] = done;
3799
3800 return skb->len;
3801}
Tuong Lienb4b97712018-12-19 09:17:56 +07003802
Tuong Lien01e661e2018-12-19 09:17:58 +07003803/**
3804 * tipc_sk_filtering - check if a socket should be traced
3805 * @sk: the socket to be examined
Randy Dunlapf172f4b2020-11-29 10:32:49 -08003806 *
3807 * @sysctl_tipc_sk_filter is used as the socket tuple for filtering:
Randy Dunlap5fcb7d42020-11-29 10:32:50 -08003808 * (portid, sock type, name type, name lower, name upper)
Tuong Lien01e661e2018-12-19 09:17:58 +07003809 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08003810 * Return: true if the socket meets the socket tuple data
Tuong Lien01e661e2018-12-19 09:17:58 +07003811 * (value 0 = 'any') or when there is no tuple set (all = 0),
3812 * otherwise false
3813 */
3814bool tipc_sk_filtering(struct sock *sk)
3815{
3816 struct tipc_sock *tsk;
3817 struct publication *p;
3818 u32 _port, _sktype, _type, _lower, _upper;
3819 u32 type = 0, lower = 0, upper = 0;
3820
3821 if (!sk)
3822 return true;
3823
3824 tsk = tipc_sk(sk);
3825
3826 _port = sysctl_tipc_sk_filter[0];
3827 _sktype = sysctl_tipc_sk_filter[1];
3828 _type = sysctl_tipc_sk_filter[2];
3829 _lower = sysctl_tipc_sk_filter[3];
3830 _upper = sysctl_tipc_sk_filter[4];
3831
3832 if (!_port && !_sktype && !_type && !_lower && !_upper)
3833 return true;
3834
3835 if (_port)
3836 return (_port == tsk->portid);
3837
3838 if (_sktype && _sktype != sk->sk_type)
3839 return false;
3840
3841 if (tsk->published) {
3842 p = list_first_entry_or_null(&tsk->publications,
3843 struct publication, binding_sock);
3844 if (p) {
Jon Maloy998d3902021-03-16 22:06:08 -04003845 type = p->sr.type;
3846 lower = p->sr.lower;
3847 upper = p->sr.upper;
Tuong Lien01e661e2018-12-19 09:17:58 +07003848 }
3849 }
3850
3851 if (!tipc_sk_type_connectionless(sk)) {
Jon Maloy14623e02021-06-02 13:44:24 -04003852 type = msg_nametype(&tsk->phdr);
3853 lower = msg_nameinst(&tsk->phdr);
3854 upper = lower;
Tuong Lien01e661e2018-12-19 09:17:58 +07003855 }
3856
3857 if ((_type && _type != type) || (_lower && _lower != lower) ||
3858 (_upper && _upper != upper))
3859 return false;
3860
3861 return true;
3862}
3863
Tuong Lienb4b97712018-12-19 09:17:56 +07003864u32 tipc_sock_get_portid(struct sock *sk)
3865{
3866 return (sk) ? (tipc_sk(sk))->portid : 0;
3867}
3868
3869/**
Tuong Lien01e661e2018-12-19 09:17:58 +07003870 * tipc_sk_overlimit1 - check if socket rx queue is about to be overloaded,
3871 * both the rcv and backlog queues are considered
3872 * @sk: tipc sk to be checked
3873 * @skb: tipc msg to be checked
3874 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08003875 * Return: true if the socket rx queue allocation is > 90%, otherwise false
Tuong Lien01e661e2018-12-19 09:17:58 +07003876 */
3877
3878bool tipc_sk_overlimit1(struct sock *sk, struct sk_buff *skb)
3879{
3880 atomic_t *dcnt = &tipc_sk(sk)->dupl_rcvcnt;
3881 unsigned int lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
3882 unsigned int qsize = sk->sk_backlog.len + sk_rmem_alloc_get(sk);
3883
3884 return (qsize > lim * 90 / 100);
3885}
3886
3887/**
3888 * tipc_sk_overlimit2 - check if socket rx queue is about to be overloaded,
3889 * only the rcv queue is considered
3890 * @sk: tipc sk to be checked
3891 * @skb: tipc msg to be checked
3892 *
Randy Dunlap637b77f2020-11-29 10:32:48 -08003893 * Return: true if the socket rx queue allocation is > 90%, otherwise false
Tuong Lien01e661e2018-12-19 09:17:58 +07003894 */
3895
3896bool tipc_sk_overlimit2(struct sock *sk, struct sk_buff *skb)
3897{
3898 unsigned int lim = rcvbuf_limit(sk, skb);
3899 unsigned int qsize = sk_rmem_alloc_get(sk);
3900
3901 return (qsize > lim * 90 / 100);
3902}
3903
3904/**
Tuong Lienb4b97712018-12-19 09:17:56 +07003905 * tipc_sk_dump - dump TIPC socket
3906 * @sk: tipc sk to be dumped
3907 * @dqueues: bitmask to decide if any socket queue to be dumped?
3908 * - TIPC_DUMP_NONE: don't dump socket queues
3909 * - TIPC_DUMP_SK_SNDQ: dump socket send queue
3910 * - TIPC_DUMP_SK_RCVQ: dump socket rcv queue
3911 * - TIPC_DUMP_SK_BKLGQ: dump socket backlog queue
3912 * - TIPC_DUMP_ALL: dump all the socket queues above
3913 * @buf: returned buffer of dump data in format
3914 */
3915int tipc_sk_dump(struct sock *sk, u16 dqueues, char *buf)
3916{
3917 int i = 0;
3918 size_t sz = (dqueues) ? SK_LMAX : SK_LMIN;
Jon Maloy14623e02021-06-02 13:44:24 -04003919 u32 conn_type, conn_instance;
Tuong Lienb4b97712018-12-19 09:17:56 +07003920 struct tipc_sock *tsk;
3921 struct publication *p;
3922 bool tsk_connected;
3923
3924 if (!sk) {
3925 i += scnprintf(buf, sz, "sk data: (null)\n");
3926 return i;
3927 }
3928
3929 tsk = tipc_sk(sk);
3930 tsk_connected = !tipc_sk_type_connectionless(sk);
3931
3932 i += scnprintf(buf, sz, "sk data: %u", sk->sk_type);
3933 i += scnprintf(buf + i, sz - i, " %d", sk->sk_state);
3934 i += scnprintf(buf + i, sz - i, " %x", tsk_own_node(tsk));
3935 i += scnprintf(buf + i, sz - i, " %u", tsk->portid);
3936 i += scnprintf(buf + i, sz - i, " | %u", tsk_connected);
3937 if (tsk_connected) {
3938 i += scnprintf(buf + i, sz - i, " %x", tsk_peer_node(tsk));
3939 i += scnprintf(buf + i, sz - i, " %u", tsk_peer_port(tsk));
Jon Maloy14623e02021-06-02 13:44:24 -04003940 conn_type = msg_nametype(&tsk->phdr);
3941 conn_instance = msg_nameinst(&tsk->phdr);
3942 i += scnprintf(buf + i, sz - i, " %u", conn_type);
3943 i += scnprintf(buf + i, sz - i, " %u", conn_instance);
Tuong Lienb4b97712018-12-19 09:17:56 +07003944 }
3945 i += scnprintf(buf + i, sz - i, " | %u", tsk->published);
3946 if (tsk->published) {
3947 p = list_first_entry_or_null(&tsk->publications,
3948 struct publication, binding_sock);
Jon Maloy998d3902021-03-16 22:06:08 -04003949 i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.type : 0);
3950 i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.lower : 0);
3951 i += scnprintf(buf + i, sz - i, " %u", (p) ? p->sr.upper : 0);
Tuong Lienb4b97712018-12-19 09:17:56 +07003952 }
3953 i += scnprintf(buf + i, sz - i, " | %u", tsk->snd_win);
3954 i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_win);
3955 i += scnprintf(buf + i, sz - i, " %u", tsk->max_pkt);
3956 i += scnprintf(buf + i, sz - i, " %x", tsk->peer_caps);
3957 i += scnprintf(buf + i, sz - i, " %u", tsk->cong_link_cnt);
3958 i += scnprintf(buf + i, sz - i, " %u", tsk->snt_unacked);
3959 i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_unacked);
3960 i += scnprintf(buf + i, sz - i, " %u", atomic_read(&tsk->dupl_rcvcnt));
3961 i += scnprintf(buf + i, sz - i, " %u", sk->sk_shutdown);
3962 i += scnprintf(buf + i, sz - i, " | %d", sk_wmem_alloc_get(sk));
3963 i += scnprintf(buf + i, sz - i, " %d", sk->sk_sndbuf);
3964 i += scnprintf(buf + i, sz - i, " | %d", sk_rmem_alloc_get(sk));
3965 i += scnprintf(buf + i, sz - i, " %d", sk->sk_rcvbuf);
Eric Dumazet70c26552019-10-09 15:41:03 -07003966 i += scnprintf(buf + i, sz - i, " | %d\n", READ_ONCE(sk->sk_backlog.len));
Tuong Lienb4b97712018-12-19 09:17:56 +07003967
3968 if (dqueues & TIPC_DUMP_SK_SNDQ) {
3969 i += scnprintf(buf + i, sz - i, "sk_write_queue: ");
3970 i += tipc_list_dump(&sk->sk_write_queue, false, buf + i);
3971 }
3972
3973 if (dqueues & TIPC_DUMP_SK_RCVQ) {
3974 i += scnprintf(buf + i, sz - i, "sk_receive_queue: ");
3975 i += tipc_list_dump(&sk->sk_receive_queue, false, buf + i);
3976 }
3977
3978 if (dqueues & TIPC_DUMP_SK_BKLGQ) {
3979 i += scnprintf(buf + i, sz - i, "sk_backlog:\n head ");
3980 i += tipc_skb_dump(sk->sk_backlog.head, false, buf + i);
3981 if (sk->sk_backlog.tail != sk->sk_backlog.head) {
3982 i += scnprintf(buf + i, sz - i, " tail ");
3983 i += tipc_skb_dump(sk->sk_backlog.tail, false,
3984 buf + i);
3985 }
3986 }
3987
3988 return i;
3989}