blob: d6b67d07d22ecfeb05d552a736604a94ede03d77 [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 Maloy75da2162017-10-13 11:04:23 +02004 * Copyright (c) 2001-2007, 2012-2017, Ericsson AB
Ying Xuec5fa7b32013-06-17 10:54:39 -04005 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
Ying Xue07f6c4b2015-01-07 13:41:58 +080037#include <linux/rhashtable.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010038#include <linux/sched/signal.h>
39
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "core.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050041#include "name_table.h"
Erik Hugne78acb1f2014-04-24 16:26:47 +020042#include "node.h"
Jon Paul Maloye2dafe82014-06-25 20:41:37 -050043#include "link.h"
Jon Paul Maloyc637c102015-02-05 08:36:41 -050044#include "name_distr.h"
Jon Paul Maloy2e84c602014-08-22 18:09:18 -040045#include "socket.h"
Jon Paul Maloya6bf70f2015-05-14 10:46:13 -040046#include "bcast.h"
Richard Alpe49cc66e2016-03-04 17:04:42 +010047#include "netlink.h"
Jon Maloy75da2162017-10-13 11:04:23 +020048#include "group.h"
Tuong Lienb4b97712018-12-19 09:17:56 +070049#include "trace.h"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040050
Tuong Lien0a3e0602020-05-26 16:38:38 +070051#define NAGLE_START_INIT 4
52#define NAGLE_START_MAX 1024
Tung Nguyen67879272018-09-28 20:23:22 +020053#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Jon Maloy0d5fcebf2017-10-20 11:21:32 +020054#define CONN_PROBING_INTV msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080055#define TIPC_FWD_MSG 1
Ying Xue07f6c4b2015-01-07 13:41:58 +080056#define TIPC_MAX_PORT 0xffffffff
57#define TIPC_MIN_PORT 1
Jon Paul Maloye9f8b102017-05-02 18:16:53 +020058#define TIPC_ACK_RATE 4 /* ACK at 1/4 of of rcv window size */
Jon Paul Maloy301bae52014-08-22 18:09:20 -040059
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010060enum {
61 TIPC_LISTEN = TCP_LISTEN,
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +010062 TIPC_ESTABLISHED = TCP_ESTABLISHED,
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +010063 TIPC_OPEN = TCP_CLOSE,
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +010064 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +010065 TIPC_CONNECTING = TCP_SYN_SENT,
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010066};
67
Jon Maloy31c82a22017-10-13 11:04:24 +020068struct sockaddr_pair {
69 struct sockaddr_tipc sock;
70 struct sockaddr_tipc member;
71};
72
Jon Paul Maloy301bae52014-08-22 18:09:20 -040073/**
74 * struct tipc_sock - TIPC socket structure
75 * @sk: socket - interacts with 'port' and with user via the socket API
Jon Paul Maloy301bae52014-08-22 18:09:20 -040076 * @conn_type: TIPC type used when connection was established
77 * @conn_instance: TIPC instance used when connection was established
78 * @published: non-zero if port has one or more associated names
79 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Jon Maloyc0bceb92019-10-30 14:00:41 +010080 * @maxnagle: maximum size of msg which can be subject to nagle
Ying Xue07f6c4b2015-01-07 13:41:58 +080081 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040082 * @phdr: preformatted message header used when sending messages
Jon Paul Maloy365ad352017-01-03 10:55:11 -050083 * #cong_links: list of congested links
Jon Paul Maloy301bae52014-08-22 18:09:20 -040084 * @publications: list of publications for port
Jon Paul Maloy365ad352017-01-03 10:55:11 -050085 * @blocking_link: address of the congested link we are currently sleeping on
Jon Paul Maloy301bae52014-08-22 18:09:20 -040086 * @pub_count: total # of publications port has made during its lifetime
Jon Paul Maloy301bae52014-08-22 18:09:20 -040087 * @conn_timeout: the time we can wait for an unresponded setup request
88 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
Jon Paul Maloy365ad352017-01-03 10:55:11 -050089 * @cong_link_cnt: number of congested links
Jon Maloy75da2162017-10-13 11:04:23 +020090 * @snt_unacked: # messages sent by socket, and not yet acked by peer
Jon Paul Maloy301bae52014-08-22 18:09:20 -040091 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +010092 * @peer: 'connected' peer for dgram/rdm
Ying Xue07f6c4b2015-01-07 13:41:58 +080093 * @node: hash table node
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -050094 * @mc_method: cookie for use between socket and broadcast layer
Ying Xue07f6c4b2015-01-07 13:41:58 +080095 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040096 */
97struct tipc_sock {
98 struct sock sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040099 u32 conn_type;
100 u32 conn_instance;
101 int published;
102 u32 max_pkt;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100103 u32 maxnagle;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800104 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400105 struct tipc_msg phdr;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500106 struct list_head cong_links;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400107 struct list_head publications;
108 u32 pub_count;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400109 atomic_t dupl_rcvcnt;
Tung Nguyen67879272018-09-28 20:23:22 +0200110 u16 conn_timeout;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100111 bool probe_unacked;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500112 u16 cong_link_cnt;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400113 u16 snt_unacked;
114 u16 snd_win;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400115 u16 peer_caps;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400116 u16 rcv_unacked;
117 u16 rcv_win;
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +0100118 struct sockaddr_tipc peer;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800119 struct rhash_head node;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500120 struct tipc_mc_method mc_method;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800121 struct rcu_head rcu;
Jon Maloy75da2162017-10-13 11:04:23 +0200122 struct tipc_group *group;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100123 u32 oneway;
Tuong Lien0a3e0602020-05-26 16:38:38 +0700124 u32 nagle_start;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100125 u16 snd_backlog;
Tuong Lien0a3e0602020-05-26 16:38:38 +0700126 u16 msg_acc;
127 u16 pkt_cnt;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100128 bool expect_ack;
129 bool nodelay;
Jon Maloy60c25302018-01-17 16:42:46 +0100130 bool group_is_open;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400131};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100132
Jon Maloy64ac5f52017-10-13 11:04:20 +0200133static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400134static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800135static void tipc_write_space(struct sock *sk);
Ying Xuef4195d12015-11-22 15:46:05 +0800136static void tipc_sock_destruct(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800137static int tipc_release(struct socket *sock);
David Howellscdfbabf2017-03-09 08:09:05 +0000138static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
139 bool kern);
Kees Cook31b102b2017-10-30 14:06:45 -0700140static void tipc_sk_timeout(struct timer_list *t);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400141static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400142 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400143static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400144 struct tipc_name_seq const *seq);
Jon Maloy75da2162017-10-13 11:04:23 +0200145static int tipc_sk_leave(struct tipc_sock *tsk);
Ying Xuee05b31f2015-01-09 15:27:08 +0800146static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800147static int tipc_sk_insert(struct tipc_sock *tsk);
148static void tipc_sk_remove(struct tipc_sock *tsk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500149static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
Ying Xue39a0295f2015-03-02 15:37:47 +0800150static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Tuong Lien0a3e0602020-05-26 16:38:38 +0700151static void tipc_sk_push_backlog(struct tipc_sock *tsk, bool nagle_ack);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152
Florian Westphalbca65ea2008-02-07 18:18:01 -0800153static const struct proto_ops packet_ops;
154static const struct proto_ops stream_ops;
155static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156static struct proto tipc_proto;
Herbert Xu6cca72892015-03-20 21:57:05 +1100157static const struct rhashtable_params tsk_rht_params;
158
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500159static u32 tsk_own_node(struct tipc_sock *tsk)
160{
161 return msg_prevnode(&tsk->phdr);
162}
163
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400164static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400165{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400166 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400167}
168
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400169static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400170{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400171 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400172}
173
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400174static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400175{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400176 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400177}
178
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400179static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400180{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400181 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400182}
183
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400184static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400185{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400186 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400187}
188
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400189static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400190{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400191 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400192}
193
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400194static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400195{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400196 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400197}
198
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400199static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400200{
201 if (imp > TIPC_CRITICAL_IMPORTANCE)
202 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400203 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400204 return 0;
205}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400206
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400207static struct tipc_sock *tipc_sk(const struct sock *sk)
208{
209 return container_of(sk, struct tipc_sock, sk);
210}
211
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400212static bool tsk_conn_cong(struct tipc_sock *tsk)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400213{
Jon Paul Maloy6998cc62016-11-24 18:47:07 -0500214 return tsk->snt_unacked > tsk->snd_win;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400215}
216
Jon Maloyb7d42632017-10-13 11:04:26 +0200217static u16 tsk_blocks(int len)
218{
219 return ((len / FLOWCTL_BLK_SZ) + 1);
220}
221
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400222/* tsk_blocks(): translate a buffer size in bytes to number of
223 * advertisable blocks, taking into account the ratio truesize(len)/len
224 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
225 */
226static u16 tsk_adv_blocks(int len)
227{
228 return len / FLOWCTL_BLK_SZ / 4;
229}
230
231/* tsk_inc(): increment counter for sent or received data
232 * - If block based flow control is not supported by peer we
233 * fall back to message based ditto, incrementing the counter
234 */
235static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
236{
237 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
238 return ((msglen / FLOWCTL_BLK_SZ) + 1);
239 return 1;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400240}
241
Jon Maloyc0bceb92019-10-30 14:00:41 +0100242/* tsk_set_nagle - enable/disable nagle property by manipulating maxnagle
243 */
244static void tsk_set_nagle(struct tipc_sock *tsk)
245{
246 struct sock *sk = &tsk->sk;
247
248 tsk->maxnagle = 0;
249 if (sk->sk_type != SOCK_STREAM)
250 return;
251 if (tsk->nodelay)
252 return;
253 if (!(tsk->peer_caps & TIPC_NAGLE))
254 return;
255 /* Limit node local buffer size to avoid receive queue overflow */
256 if (tsk->max_pkt == MAX_MSG_SIZE)
257 tsk->maxnagle = 1500;
258 else
259 tsk->maxnagle = tsk->max_pkt;
260}
261
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400263 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700264 *
265 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100266 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400267static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100268{
Tuong Lien01e661e2018-12-19 09:17:58 +0700269 trace_tipc_sk_advance_rx(sk, NULL, TIPC_DUMP_SK_RCVQ, " ");
Allan Stephens5f6d9122011-11-04 13:24:29 -0400270 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100271}
272
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400273/* tipc_sk_respond() : send response message back to sender
274 */
275static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
276{
277 u32 selector;
278 u32 dnode;
279 u32 onode = tipc_own_addr(sock_net(sk));
280
281 if (!tipc_msg_reverse(onode, &skb, err))
282 return;
283
Tuong Lien01e661e2018-12-19 09:17:58 +0700284 trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE, "@sk_respond!");
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400285 dnode = msg_destnode(buf_msg(skb));
286 selector = msg_origport(buf_msg(skb));
287 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
288}
289
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400291 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700292 *
293 * Caller must hold socket lock
294 */
Tuong Lien49afb802020-01-08 09:18:15 +0700295static void tsk_rej_rx_queue(struct sock *sk, int error)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700296{
Ying Xuea6ca1092014-11-26 11:41:55 +0800297 struct sk_buff *skb;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700298
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400299 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
Tuong Lien49afb802020-01-08 09:18:15 +0700300 tipc_sk_respond(sk, skb, error);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700301}
302
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100303static bool tipc_sk_connected(struct sock *sk)
304{
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100305 return sk->sk_state == TIPC_ESTABLISHED;
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100306}
307
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +0100308/* tipc_sk_type_connectionless - check if the socket is datagram socket
309 * @sk: socket
310 *
311 * Returns true if connection less, false otherwise
312 */
313static bool tipc_sk_type_connectionless(struct sock *sk)
314{
315 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
316}
317
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400318/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400319 *
320 * Handles cases where the node's network address has changed from
321 * the default of <0.0.0> to its configured setting.
322 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400323static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400324{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100325 struct sock *sk = &tsk->sk;
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100326 u32 self = tipc_own_addr(sock_net(sk));
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400327 u32 peer_port = tsk_peer_port(tsk);
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100328 u32 orig_node, peer_node;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400329
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100330 if (unlikely(!tipc_sk_connected(sk)))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400331 return false;
332
333 if (unlikely(msg_origport(msg) != peer_port))
334 return false;
335
336 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400337 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400338
339 if (likely(orig_node == peer_node))
340 return true;
341
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100342 if (!orig_node && peer_node == self)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400343 return true;
344
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100345 if (!peer_node && orig_node == self)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400346 return true;
347
348 return false;
349}
350
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100351/* tipc_set_sk_state - set the sk_state of the socket
352 * @sk: socket
353 *
354 * Caller must hold socket lock
355 *
356 * Returns 0 on success, errno otherwise
357 */
358static int tipc_set_sk_state(struct sock *sk, int state)
359{
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100360 int oldsk_state = sk->sk_state;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100361 int res = -EINVAL;
362
363 switch (state) {
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100364 case TIPC_OPEN:
365 res = 0;
366 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100367 case TIPC_LISTEN:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100368 case TIPC_CONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100369 if (oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100370 res = 0;
371 break;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100372 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100373 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100374 oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100375 res = 0;
376 break;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100377 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100378 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100379 oldsk_state == TIPC_ESTABLISHED)
380 res = 0;
381 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100382 }
383
384 if (!res)
385 sk->sk_state = state;
386
387 return res;
388}
389
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500390static int tipc_sk_sock_err(struct socket *sock, long *timeout)
391{
392 struct sock *sk = sock->sk;
393 int err = sock_error(sk);
394 int typ = sock->type;
395
396 if (err)
397 return err;
398 if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
399 if (sk->sk_state == TIPC_DISCONNECTING)
400 return -EPIPE;
401 else if (!tipc_sk_connected(sk))
402 return -ENOTCONN;
403 }
404 if (!*timeout)
405 return -EAGAIN;
406 if (signal_pending(current))
407 return sock_intr_errno(*timeout);
408
409 return 0;
410}
411
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200412#define tipc_wait_for_cond(sock_, timeo_, condition_) \
413({ \
Tung Nguyenbfd07f32019-02-25 10:57:20 +0700414 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200415 struct sock *sk_; \
416 int rc_; \
417 \
418 while ((rc_ = !(condition_))) { \
Tung Nguyenbfd07f32019-02-25 10:57:20 +0700419 /* coupled with smp_wmb() in tipc_sk_proto_rcv() */ \
420 smp_rmb(); \
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200421 sk_ = (sock_)->sk; \
422 rc_ = tipc_sk_sock_err((sock_), timeo_); \
423 if (rc_) \
424 break; \
Tung Nguyen223b7322019-02-19 11:20:47 +0700425 add_wait_queue(sk_sleep(sk_), &wait_); \
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200426 release_sock(sk_); \
427 *(timeo_) = wait_woken(&wait_, TASK_INTERRUPTIBLE, *(timeo_)); \
428 sched_annotate_sleep(); \
429 lock_sock(sk_); \
430 remove_wait_queue(sk_sleep(sk_), &wait_); \
431 } \
432 rc_; \
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500433})
434
Allan Stephens0c3141e2008-04-15 00:22:02 -0700435/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400436 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700437 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438 * @sock: pre-allocated socket structure
439 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800440 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900441 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700442 * This routine creates additional data structures used by the TIPC socket,
443 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444 *
445 * Returns 0 on success, errno otherwise
446 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400447static int tipc_sk_create(struct net *net, struct socket *sock,
448 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100449{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700450 const struct proto_ops *ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400452 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400453 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700454
455 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456 if (unlikely(protocol != 0))
457 return -EPROTONOSUPPORT;
458
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 switch (sock->type) {
460 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700461 ops = &stream_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462 break;
463 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700464 ops = &packet_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465 break;
466 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700468 ops = &msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469 break;
Allan Stephens49978652006-06-25 23:47:18 -0700470 default:
Allan Stephens49978652006-06-25 23:47:18 -0700471 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472 }
473
Allan Stephens0c3141e2008-04-15 00:22:02 -0700474 /* Allocate socket's protocol area */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500475 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700476 if (sk == NULL)
477 return -ENOMEM;
478
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400479 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400480 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Maloyc0bceb92019-10-30 14:00:41 +0100481 tsk->maxnagle = 0;
Tuong Lien0a3e0602020-05-26 16:38:38 +0700482 tsk->nagle_start = NAGLE_START_INIT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400483 INIT_LIST_HEAD(&tsk->publications);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500484 INIT_LIST_HEAD(&tsk->cong_links);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400485 msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486
Allan Stephens0c3141e2008-04-15 00:22:02 -0700487 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700488 sock->ops = ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100489 sock_init_data(sock, sk);
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100490 tipc_set_sk_state(sk, TIPC_OPEN);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800491 if (tipc_sk_insert(tsk)) {
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900492 pr_warn("Socket create failed; port number exhausted\n");
Ying Xue07f6c4b2015-01-07 13:41:58 +0800493 return -EINVAL;
494 }
Herbert Xu40f9f432017-02-11 19:26:46 +0800495
496 /* Ensure tsk is visible before we read own_addr. */
497 smp_mb();
498
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100499 tipc_msg_init(tipc_own_addr(net), msg, TIPC_LOW_IMPORTANCE,
500 TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
Herbert Xu40f9f432017-02-11 19:26:46 +0800501
Ying Xue07f6c4b2015-01-07 13:41:58 +0800502 msg_set_origport(msg, tsk->portid);
Kees Cook31b102b2017-10-30 14:06:45 -0700503 timer_setup(&sk->sk_timer, tipc_sk_timeout, 0);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100504 sk->sk_shutdown = 0;
Jon Maloy64ac5f52017-10-13 11:04:20 +0200505 sk->sk_backlog_rcv = tipc_sk_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400506 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800507 sk->sk_data_ready = tipc_data_ready;
508 sk->sk_write_space = tipc_write_space;
Ying Xuef4195d12015-11-22 15:46:05 +0800509 sk->sk_destruct = tipc_sock_destruct;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400510 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
Jon Maloy1b22bca2018-02-26 20:14:04 +0100511 tsk->group_is_open = true;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400512 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700513
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400514 /* Start out with safe limits until we receive an advertised window */
515 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
516 tsk->rcv_win = tsk->snd_win;
517
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +0100518 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400519 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700520 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400521 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700522 }
Jon Maloy2948a1f2019-07-30 20:19:10 +0200523 __skb_queue_head_init(&tsk->mc_method.deferredq);
Tuong Lien01e661e2018-12-19 09:17:58 +0700524 trace_tipc_sk_create(sk, NULL, TIPC_DUMP_NONE, " ");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 return 0;
526}
527
Ying Xue07f6c4b2015-01-07 13:41:58 +0800528static void tipc_sk_callback(struct rcu_head *head)
529{
530 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
531
532 sock_put(&tsk->sk);
533}
534
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100535/* Caller should hold socket lock for the socket. */
536static void __tipc_shutdown(struct socket *sock, int error)
537{
538 struct sock *sk = sock->sk;
539 struct tipc_sock *tsk = tipc_sk(sk);
540 struct net *net = sock_net(sk);
Tung Nguyen12db3c82019-11-28 10:10:07 +0700541 long timeout = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100542 u32 dnode = tsk_peer_node(tsk);
543 struct sk_buff *skb;
544
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500545 /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
546 tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
547 !tsk_conn_cong(tsk)));
548
Tung Nguyend34910e2019-11-28 10:10:08 +0700549 /* Push out delayed messages if in Nagle mode */
Tuong Lien0a3e0602020-05-26 16:38:38 +0700550 tipc_sk_push_backlog(tsk, false);
Tung Nguyend34910e2019-11-28 10:10:08 +0700551 /* Remove pending SYN */
552 __skb_queue_purge(&sk->sk_write_queue);
Tung Nguyen67879272018-09-28 20:23:22 +0200553
Tuong Lien49afb802020-01-08 09:18:15 +0700554 /* Remove partially received buffer if any */
555 skb = skb_peek(&sk->sk_receive_queue);
556 if (skb && TIPC_SKB_CB(skb)->bytes_read) {
557 __skb_unlink(skb, &sk->sk_receive_queue);
558 kfree_skb(skb);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100559 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500560
Tuong Lien49afb802020-01-08 09:18:15 +0700561 /* Reject all unreceived messages if connectionless */
562 if (tipc_sk_type_connectionless(sk)) {
563 tsk_rej_rx_queue(sk, error);
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500564 return;
Tuong Lien49afb802020-01-08 09:18:15 +0700565 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500566
Tuong Lien49afb802020-01-08 09:18:15 +0700567 switch (sk->sk_state) {
568 case TIPC_CONNECTING:
569 case TIPC_ESTABLISHED:
570 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
571 tipc_node_remove_conn(net, dnode, tsk->portid);
572 /* Send a FIN+/- to its peer */
573 skb = __skb_dequeue(&sk->sk_receive_queue);
574 if (skb) {
575 __skb_queue_purge(&sk->sk_receive_queue);
576 tipc_sk_respond(sk, skb, error);
577 break;
578 }
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100579 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
580 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
581 tsk_own_node(tsk), tsk_peer_port(tsk),
582 tsk->portid, error);
583 if (skb)
584 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Tuong Lien49afb802020-01-08 09:18:15 +0700585 break;
586 case TIPC_LISTEN:
587 /* Reject all SYN messages */
588 tsk_rej_rx_queue(sk, error);
589 break;
590 default:
591 __skb_queue_purge(&sk->sk_receive_queue);
592 break;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100593 }
594}
595
Ying Xuec5fa7b32013-06-17 10:54:39 -0400596/**
Ying Xue247f0f32014-02-18 16:06:46 +0800597 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 * @sock: socket to destroy
599 *
600 * This routine cleans up any messages that are still queued on the socket.
601 * For DGRAM and RDM socket types, all queued messages are rejected.
602 * For SEQPACKET and STREAM socket types, the first message is rejected
603 * and any others are discarded. (If the first message on a STREAM socket
604 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900605 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 * NOTE: Rejected messages are not necessarily returned to the sender! They
607 * are returned or discarded according to the "destination droppable" setting
608 * specified for the message by the sender.
609 *
610 * Returns 0 on success, errno otherwise
611 */
Ying Xue247f0f32014-02-18 16:06:46 +0800612static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400615 struct tipc_sock *tsk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100616
Allan Stephens0c3141e2008-04-15 00:22:02 -0700617 /*
618 * Exit if socket isn't fully initialized (occurs when a failed accept()
619 * releases a pre-allocated child socket that was never used)
620 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700621 if (sk == NULL)
622 return 0;
623
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400624 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700625 lock_sock(sk);
626
Tuong Lien01e661e2018-12-19 09:17:58 +0700627 trace_tipc_sk_release(sk, NULL, TIPC_DUMP_ALL, " ");
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100628 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
629 sk->sk_shutdown = SHUTDOWN_MASK;
Jon Maloy75da2162017-10-13 11:04:23 +0200630 tipc_sk_leave(tsk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400631 tipc_sk_withdraw(tsk, 0, NULL);
Hoang Lec55c8ed2019-03-19 18:49:50 +0700632 __skb_queue_purge(&tsk->mc_method.deferredq);
Ying Xue1ea23a22015-05-28 13:19:22 +0800633 sk_stop_timer(sk, &sk->sk_timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800634 tipc_sk_remove(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635
Cong Wang0a3b8b22018-09-03 19:12:41 -0700636 sock_orphan(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700637 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700638 release_sock(sk);
Jon Maloya80ae532017-10-13 11:04:22 +0200639 tipc_dest_list_purge(&tsk->cong_links);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500640 tsk->cong_link_cnt = 0;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800641 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700642 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200644 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645}
646
647/**
Ying Xue247f0f32014-02-18 16:06:46 +0800648 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649 * @sock: socket structure
650 * @uaddr: socket address describing name(s) and desired operation
651 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900652 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100653 * Name and name sequence binding is indicated using a positive scope value;
654 * a negative scope value unbinds the specified name. Specifying no name
655 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900656 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700658 *
659 * NOTE: This routine doesn't need to take the socket lock since it doesn't
660 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661 */
Ying Xue247f0f32014-02-18 16:06:46 +0800662static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
663 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664{
Ying Xue84602762013-12-27 10:18:28 +0800665 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400667 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800668 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100669
Ying Xue84602762013-12-27 10:18:28 +0800670 lock_sock(sk);
671 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400672 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800673 goto exit;
674 }
Jon Maloy75da2162017-10-13 11:04:23 +0200675 if (tsk->group) {
676 res = -EACCES;
677 goto exit;
678 }
Ying Xue84602762013-12-27 10:18:28 +0800679 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
680 res = -EINVAL;
681 goto exit;
682 }
683 if (addr->family != AF_TIPC) {
684 res = -EAFNOSUPPORT;
685 goto exit;
686 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100687
Per Lidenb97bf3f2006-01-02 19:04:38 +0100688 if (addr->addrtype == TIPC_ADDR_NAME)
689 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800690 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
691 res = -EAFNOSUPPORT;
692 goto exit;
693 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900694
Ying Xue13a2e892013-06-17 10:54:40 -0400695 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400696 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800697 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
698 res = -EACCES;
699 goto exit;
700 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400701
Jon Maloy928df182018-03-15 16:48:51 +0100702 res = (addr->scope >= 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400703 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
704 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800705exit:
706 release_sock(sk);
707 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100708}
709
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900710/**
Ying Xue247f0f32014-02-18 16:06:46 +0800711 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712 * @sock: socket structure
713 * @uaddr: area for returned socket address
714 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700715 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900716 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100717 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700718 *
Allan Stephens2da59912008-07-14 22:43:32 -0700719 * NOTE: This routine doesn't need to take the socket lock since it only
720 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000721 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100722 */
Ying Xue247f0f32014-02-18 16:06:46 +0800723static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100724 int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100725{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100726 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100727 struct sock *sk = sock->sk;
728 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100729
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000730 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700731 if (peer) {
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100732 if ((!tipc_sk_connected(sk)) &&
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100733 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
Allan Stephens2da59912008-07-14 22:43:32 -0700734 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400735 addr->addr.id.ref = tsk_peer_port(tsk);
736 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700737 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800738 addr->addr.id.ref = tsk->portid;
Jon Maloy23fd3ea2018-03-22 20:42:49 +0100739 addr->addr.id.node = tipc_own_addr(sock_net(sk));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700740 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100741
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742 addr->addrtype = TIPC_ADDR_ID;
743 addr->family = AF_TIPC;
744 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100745 addr->addr.name.domain = 0;
746
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +0100747 return sizeof(*addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100748}
749
750/**
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700751 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752 * @file: file structure associated with the socket
753 * @sock: socket for which to calculate the poll bits
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700754 * @wait: ???
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700756 * Returns pollmask value
757 *
758 * COMMENTARY:
759 * It appears that the usual socket locking mechanisms are not useful here
760 * since the pollmask info is potentially out-of-date the moment this routine
761 * exits. TCP and other protocols seem to rely on higher level poll routines
762 * to handle any preventable race conditions, so TIPC will do the same ...
763 *
Allan Stephensf662c072010-08-17 11:00:06 +0000764 * IMPORTANT: The fact that a read or write operation is indicated does NOT
765 * imply that the operation will succeed, merely that it should be performed
766 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100767 */
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700768static __poll_t tipc_poll(struct file *file, struct socket *sock,
769 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100770{
Allan Stephens9b674e82008-03-26 16:48:21 -0700771 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400772 struct tipc_sock *tsk = tipc_sk(sk);
Al Viroade994f2017-07-03 00:01:49 -0400773 __poll_t revents = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700774
Karsten Graul89ab0662018-10-23 13:40:39 +0200775 sock_poll_wait(file, sock, wait);
Tuong Lien01e661e2018-12-19 09:17:58 +0700776 trace_tipc_sk_poll(sk, NULL, TIPC_DUMP_ALL, " ");
Linus Torvaldsa11e1d42018-06-28 09:43:44 -0700777
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100778 if (sk->sk_shutdown & RCV_SHUTDOWN)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800779 revents |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100780 if (sk->sk_shutdown == SHUTDOWN_MASK)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800781 revents |= EPOLLHUP;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100782
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100783 switch (sk->sk_state) {
784 case TIPC_ESTABLISHED:
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500785 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800786 revents |= EPOLLOUT;
Gustavo A. R. Silvaf79e3362019-01-23 01:09:31 -0600787 /* fall through */
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100788 case TIPC_LISTEN:
Parthasarathy Bhuvaraganff946832019-05-09 07:13:42 +0200789 case TIPC_CONNECTING:
Eric Dumazet3ef7cf52019-10-23 22:44:50 -0700790 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800791 revents |= EPOLLIN | EPOLLRDNORM;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100792 break;
793 case TIPC_OPEN:
Jon Maloy60c25302018-01-17 16:42:46 +0100794 if (tsk->group_is_open && !tsk->cong_link_cnt)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800795 revents |= EPOLLOUT;
Jon Maloyae236fb2017-10-13 11:04:25 +0200796 if (!tipc_sk_type_connectionless(sk))
797 break;
Eric Dumazet3ef7cf52019-10-23 22:44:50 -0700798 if (skb_queue_empty_lockless(&sk->sk_receive_queue))
Jon Maloyae236fb2017-10-13 11:04:25 +0200799 break;
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800800 revents |= EPOLLIN | EPOLLRDNORM;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100801 break;
802 case TIPC_DISCONNECTING:
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800803 revents = EPOLLIN | EPOLLRDNORM | EPOLLHUP;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100804 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000805 }
Jon Maloyae236fb2017-10-13 11:04:25 +0200806 return revents;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100807}
808
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400809/**
810 * tipc_sendmcast - send multicast message
811 * @sock: socket structure
812 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500813 * @msg: message to send
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500814 * @dlen: length of data to send
815 * @timeout: timeout to wait for wakeup
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400816 *
817 * Called from function tipc_sendmsg(), which has done all sanity checks
818 * Returns the number of bytes sent on success, or errno
819 */
820static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500821 struct msghdr *msg, size_t dlen, long timeout)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400822{
823 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500824 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500825 struct tipc_msg *hdr = &tsk->phdr;
Ying Xuef2f98002015-01-09 15:27:05 +0800826 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500827 int mtu = tipc_bcast_get_mtu(net);
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500828 struct tipc_mc_method *method = &tsk->mc_method;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500829 struct sk_buff_head pkts;
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500830 struct tipc_nlist dsts;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400831 int rc;
832
Jon Maloy75da2162017-10-13 11:04:23 +0200833 if (tsk->group)
834 return -EACCES;
835
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500836 /* Block or return if any destination link is congested */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500837 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
838 if (unlikely(rc))
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400839 return rc;
840
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500841 /* Lookup destination nodes */
842 tipc_nlist_init(&dsts, tipc_own_addr(net));
843 tipc_nametbl_lookup_dst_nodes(net, seq->type, seq->lower,
Jon Maloye9a03442018-01-12 20:56:50 +0100844 seq->upper, &dsts);
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500845 if (!dsts.local && !dsts.remote)
846 return -EHOSTUNREACH;
847
848 /* Build message header */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500849 msg_set_type(hdr, TIPC_MCAST_MSG);
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500850 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500851 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
852 msg_set_destport(hdr, 0);
853 msg_set_destnode(hdr, 0);
854 msg_set_nametype(hdr, seq->type);
855 msg_set_namelower(hdr, seq->lower);
856 msg_set_nameupper(hdr, seq->upper);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400857
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500858 /* Build message as chain of buffers */
Jon Maloye654f9f2019-08-15 16:42:50 +0200859 __skb_queue_head_init(&pkts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500860 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500861
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500862 /* Send message if build was successful */
Tuong Lien01e661e2018-12-19 09:17:58 +0700863 if (unlikely(rc == dlen)) {
864 trace_tipc_sk_sendmcast(sk, skb_peek(&pkts),
865 TIPC_DUMP_SK_SNDQ, " ");
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500866 rc = tipc_mcast_xmit(net, &pkts, method, &dsts,
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500867 &tsk->cong_link_cnt);
Tuong Lien01e661e2018-12-19 09:17:58 +0700868 }
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500869
870 tipc_nlist_purge(&dsts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500871
872 return rc ? rc : dlen;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400873}
874
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500875/**
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200876 * tipc_send_group_msg - send a message to a member in the group
877 * @net: network namespace
878 * @m: message to send
879 * @mb: group member
880 * @dnode: destination node
881 * @dport: destination port
882 * @dlen: total length of message data
883 */
884static int tipc_send_group_msg(struct net *net, struct tipc_sock *tsk,
885 struct msghdr *m, struct tipc_member *mb,
886 u32 dnode, u32 dport, int dlen)
887{
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200888 u16 bc_snd_nxt = tipc_group_bc_snd_nxt(tsk->group);
Jon Maloy2f487712017-10-13 11:04:31 +0200889 struct tipc_mc_method *method = &tsk->mc_method;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200890 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
891 struct tipc_msg *hdr = &tsk->phdr;
892 struct sk_buff_head pkts;
893 int mtu, rc;
894
895 /* Complete message header */
896 msg_set_type(hdr, TIPC_GRP_UCAST_MSG);
897 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
898 msg_set_destport(hdr, dport);
899 msg_set_destnode(hdr, dnode);
Jon Maloyb87a5ea2017-10-13 11:04:30 +0200900 msg_set_grp_bc_seqno(hdr, bc_snd_nxt);
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200901
902 /* Build message as chain of buffers */
Jon Maloye654f9f2019-08-15 16:42:50 +0200903 __skb_queue_head_init(&pkts);
Hoang Lef73b1282019-10-29 07:51:21 +0700904 mtu = tipc_node_get_mtu(net, dnode, tsk->portid, false);
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200905 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
906 if (unlikely(rc != dlen))
907 return rc;
908
909 /* Send message */
910 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
911 if (unlikely(rc == -ELINKCONG)) {
912 tipc_dest_push(&tsk->cong_links, dnode, 0);
913 tsk->cong_link_cnt++;
914 }
915
Jon Maloy2f487712017-10-13 11:04:31 +0200916 /* Update send window */
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200917 tipc_group_update_member(mb, blks);
918
Jon Maloy2f487712017-10-13 11:04:31 +0200919 /* A broadcast sent within next EXPIRE period must follow same path */
920 method->rcast = true;
921 method->mandatory = true;
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200922 return dlen;
923}
924
925/**
926 * tipc_send_group_unicast - send message to a member in the group
927 * @sock: socket structure
928 * @m: message to send
929 * @dlen: total length of message data
930 * @timeout: timeout to wait for wakeup
931 *
932 * Called from function tipc_sendmsg(), which has done all sanity checks
933 * Returns the number of bytes sent on success, or errno
934 */
935static int tipc_send_group_unicast(struct socket *sock, struct msghdr *m,
936 int dlen, long timeout)
937{
938 struct sock *sk = sock->sk;
939 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
940 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
941 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200942 struct net *net = sock_net(sk);
943 struct tipc_member *mb = NULL;
944 u32 node, port;
945 int rc;
946
947 node = dest->addr.id.node;
948 port = dest->addr.id.ref;
949 if (!port && !node)
950 return -EHOSTUNREACH;
951
952 /* Block or return if destination link or member is congested */
953 rc = tipc_wait_for_cond(sock, &timeout,
954 !tipc_dest_find(&tsk->cong_links, node, 0) &&
Cong Wang143ece62018-12-11 21:43:51 -0800955 tsk->group &&
956 !tipc_group_cong(tsk->group, node, port, blks,
957 &mb));
Jon Maloy27bd9ec2017-10-13 11:04:27 +0200958 if (unlikely(rc))
959 return rc;
960
961 if (unlikely(!mb))
962 return -EHOSTUNREACH;
963
964 rc = tipc_send_group_msg(net, tsk, m, mb, node, port, dlen);
965
966 return rc ? rc : dlen;
967}
968
969/**
Jon Maloyee106d72017-10-13 11:04:28 +0200970 * tipc_send_group_anycast - send message to any member with given identity
971 * @sock: socket structure
972 * @m: message to send
973 * @dlen: total length of message data
974 * @timeout: timeout to wait for wakeup
975 *
976 * Called from function tipc_sendmsg(), which has done all sanity checks
977 * Returns the number of bytes sent on success, or errno
978 */
979static int tipc_send_group_anycast(struct socket *sock, struct msghdr *m,
980 int dlen, long timeout)
981{
982 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
983 struct sock *sk = sock->sk;
984 struct tipc_sock *tsk = tipc_sk(sk);
985 struct list_head *cong_links = &tsk->cong_links;
986 int blks = tsk_blocks(GROUP_H_SIZE + dlen);
Jon Maloy232d07b2018-01-08 21:03:30 +0100987 struct tipc_msg *hdr = &tsk->phdr;
Jon Maloyee106d72017-10-13 11:04:28 +0200988 struct tipc_member *first = NULL;
989 struct tipc_member *mbr = NULL;
990 struct net *net = sock_net(sk);
991 u32 node, port, exclude;
Jon Maloyee106d72017-10-13 11:04:28 +0200992 struct list_head dsts;
Jon Maloy232d07b2018-01-08 21:03:30 +0100993 u32 type, inst, scope;
Jon Maloyee106d72017-10-13 11:04:28 +0200994 int lookups = 0;
995 int dstcnt, rc;
996 bool cong;
997
998 INIT_LIST_HEAD(&dsts);
999
Jon Maloy232d07b2018-01-08 21:03:30 +01001000 type = msg_nametype(hdr);
Jon Maloyee106d72017-10-13 11:04:28 +02001001 inst = dest->addr.name.name.instance;
Jon Maloy232d07b2018-01-08 21:03:30 +01001002 scope = msg_lookup_scope(hdr);
Jon Maloyee106d72017-10-13 11:04:28 +02001003
1004 while (++lookups < 4) {
Cong Wang143ece62018-12-11 21:43:51 -08001005 exclude = tipc_group_exclude(tsk->group);
1006
Jon Maloyee106d72017-10-13 11:04:28 +02001007 first = NULL;
1008
1009 /* Look for a non-congested destination member, if any */
1010 while (1) {
Jon Maloy232d07b2018-01-08 21:03:30 +01001011 if (!tipc_nametbl_lookup(net, type, inst, scope, &dsts,
Jon Maloyee106d72017-10-13 11:04:28 +02001012 &dstcnt, exclude, false))
1013 return -EHOSTUNREACH;
1014 tipc_dest_pop(&dsts, &node, &port);
Cong Wang143ece62018-12-11 21:43:51 -08001015 cong = tipc_group_cong(tsk->group, node, port, blks,
1016 &mbr);
Jon Maloyee106d72017-10-13 11:04:28 +02001017 if (!cong)
1018 break;
1019 if (mbr == first)
1020 break;
1021 if (!first)
1022 first = mbr;
1023 }
1024
1025 /* Start over if destination was not in member list */
1026 if (unlikely(!mbr))
1027 continue;
1028
1029 if (likely(!cong && !tipc_dest_find(cong_links, node, 0)))
1030 break;
1031
1032 /* Block or return if destination link or member is congested */
1033 rc = tipc_wait_for_cond(sock, &timeout,
1034 !tipc_dest_find(cong_links, node, 0) &&
Cong Wang143ece62018-12-11 21:43:51 -08001035 tsk->group &&
1036 !tipc_group_cong(tsk->group, node, port,
Jon Maloyee106d72017-10-13 11:04:28 +02001037 blks, &mbr));
1038 if (unlikely(rc))
1039 return rc;
1040
1041 /* Send, unless destination disappeared while waiting */
1042 if (likely(mbr))
1043 break;
1044 }
1045
1046 if (unlikely(lookups >= 4))
1047 return -EHOSTUNREACH;
1048
1049 rc = tipc_send_group_msg(net, tsk, m, mbr, node, port, dlen);
1050
1051 return rc ? rc : dlen;
1052}
1053
1054/**
Jon Maloy75da2162017-10-13 11:04:23 +02001055 * tipc_send_group_bcast - send message to all members in communication group
1056 * @sk: socket structure
1057 * @m: message to send
1058 * @dlen: total length of message data
1059 * @timeout: timeout to wait for wakeup
1060 *
1061 * Called from function tipc_sendmsg(), which has done all sanity checks
1062 * Returns the number of bytes sent on success, or errno
1063 */
1064static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m,
1065 int dlen, long timeout)
1066{
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001067 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Jon Maloy75da2162017-10-13 11:04:23 +02001068 struct sock *sk = sock->sk;
1069 struct net *net = sock_net(sk);
1070 struct tipc_sock *tsk = tipc_sk(sk);
Cong Wang3c6306d2018-12-16 23:25:12 -08001071 struct tipc_nlist *dsts;
Jon Maloy75da2162017-10-13 11:04:23 +02001072 struct tipc_mc_method *method = &tsk->mc_method;
Jon Maloy2f487712017-10-13 11:04:31 +02001073 bool ack = method->mandatory && method->rcast;
Jon Maloyb7d42632017-10-13 11:04:26 +02001074 int blks = tsk_blocks(MCAST_H_SIZE + dlen);
Jon Maloy75da2162017-10-13 11:04:23 +02001075 struct tipc_msg *hdr = &tsk->phdr;
1076 int mtu = tipc_bcast_get_mtu(net);
1077 struct sk_buff_head pkts;
1078 int rc = -EHOSTUNREACH;
1079
Jon Maloyb7d42632017-10-13 11:04:26 +02001080 /* Block or return if any destination link or member is congested */
Cong Wang143ece62018-12-11 21:43:51 -08001081 rc = tipc_wait_for_cond(sock, &timeout,
1082 !tsk->cong_link_cnt && tsk->group &&
1083 !tipc_group_bc_cong(tsk->group, blks));
Jon Maloy75da2162017-10-13 11:04:23 +02001084 if (unlikely(rc))
1085 return rc;
1086
Cong Wang3c6306d2018-12-16 23:25:12 -08001087 dsts = tipc_group_dests(tsk->group);
1088 if (!dsts->local && !dsts->remote)
1089 return -EHOSTUNREACH;
1090
Jon Maloy75da2162017-10-13 11:04:23 +02001091 /* Complete message header */
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001092 if (dest) {
1093 msg_set_type(hdr, TIPC_GRP_MCAST_MSG);
1094 msg_set_nameinst(hdr, dest->addr.name.name.instance);
1095 } else {
1096 msg_set_type(hdr, TIPC_GRP_BCAST_MSG);
1097 msg_set_nameinst(hdr, 0);
1098 }
Jon Maloyb7d42632017-10-13 11:04:26 +02001099 msg_set_hdr_sz(hdr, GROUP_H_SIZE);
Jon Maloy75da2162017-10-13 11:04:23 +02001100 msg_set_destport(hdr, 0);
1101 msg_set_destnode(hdr, 0);
Cong Wang143ece62018-12-11 21:43:51 -08001102 msg_set_grp_bc_seqno(hdr, tipc_group_bc_snd_nxt(tsk->group));
Jon Maloy75da2162017-10-13 11:04:23 +02001103
Jon Maloy2f487712017-10-13 11:04:31 +02001104 /* Avoid getting stuck with repeated forced replicasts */
1105 msg_set_grp_bc_ack_req(hdr, ack);
1106
Jon Maloy75da2162017-10-13 11:04:23 +02001107 /* Build message as chain of buffers */
Jon Maloye654f9f2019-08-15 16:42:50 +02001108 __skb_queue_head_init(&pkts);
Jon Maloy75da2162017-10-13 11:04:23 +02001109 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1110 if (unlikely(rc != dlen))
1111 return rc;
1112
1113 /* Send message */
Jon Maloy2f487712017-10-13 11:04:31 +02001114 rc = tipc_mcast_xmit(net, &pkts, method, dsts, &tsk->cong_link_cnt);
Jon Maloy75da2162017-10-13 11:04:23 +02001115 if (unlikely(rc))
1116 return rc;
1117
Jon Maloyb7d42632017-10-13 11:04:26 +02001118 /* Update broadcast sequence number and send windows */
Jon Maloy2f487712017-10-13 11:04:31 +02001119 tipc_group_update_bc_members(tsk->group, blks, ack);
1120
1121 /* Broadcast link is now free to choose method for next broadcast */
1122 method->mandatory = false;
1123 method->expires = jiffies;
1124
Jon Maloy75da2162017-10-13 11:04:23 +02001125 return dlen;
1126}
1127
1128/**
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001129 * tipc_send_group_mcast - send message to all members with given identity
1130 * @sock: socket structure
1131 * @m: message to send
1132 * @dlen: total length of message data
1133 * @timeout: timeout to wait for wakeup
1134 *
1135 * Called from function tipc_sendmsg(), which has done all sanity checks
1136 * Returns the number of bytes sent on success, or errno
1137 */
1138static int tipc_send_group_mcast(struct socket *sock, struct msghdr *m,
1139 int dlen, long timeout)
1140{
1141 struct sock *sk = sock->sk;
1142 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001143 struct tipc_sock *tsk = tipc_sk(sk);
1144 struct tipc_group *grp = tsk->group;
Jon Maloy232d07b2018-01-08 21:03:30 +01001145 struct tipc_msg *hdr = &tsk->phdr;
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001146 struct net *net = sock_net(sk);
Jon Maloy232d07b2018-01-08 21:03:30 +01001147 u32 type, inst, scope, exclude;
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001148 struct list_head dsts;
Jon Maloy232d07b2018-01-08 21:03:30 +01001149 u32 dstcnt;
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001150
1151 INIT_LIST_HEAD(&dsts);
1152
Jon Maloy232d07b2018-01-08 21:03:30 +01001153 type = msg_nametype(hdr);
1154 inst = dest->addr.name.name.instance;
1155 scope = msg_lookup_scope(hdr);
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001156 exclude = tipc_group_exclude(grp);
Jon Maloy232d07b2018-01-08 21:03:30 +01001157
1158 if (!tipc_nametbl_lookup(net, type, inst, scope, &dsts,
1159 &dstcnt, exclude, true))
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001160 return -EHOSTUNREACH;
1161
1162 if (dstcnt == 1) {
1163 tipc_dest_pop(&dsts, &dest->addr.id.node, &dest->addr.id.ref);
1164 return tipc_send_group_unicast(sock, m, dlen, timeout);
1165 }
1166
1167 tipc_dest_list_purge(&dsts);
1168 return tipc_send_group_bcast(sock, m, dlen, timeout);
1169}
1170
1171/**
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001172 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
1173 * @arrvq: queue with arriving messages, to be cloned after destination lookup
1174 * @inputq: queue with cloned messages, delivered to socket after dest lookup
1175 *
1176 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001177 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001178void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
1179 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001180{
Jon Maloy75da2162017-10-13 11:04:23 +02001181 u32 self = tipc_own_addr(net);
Jon Maloy232d07b2018-01-08 21:03:30 +01001182 u32 type, lower, upper, scope;
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001183 struct sk_buff *skb, *_skb;
Colin Ian Kingb053fcc2018-07-31 17:01:37 +01001184 u32 portid, onode;
Jon Maloy232d07b2018-01-08 21:03:30 +01001185 struct sk_buff_head tmpq;
Jon Maloy75da2162017-10-13 11:04:23 +02001186 struct list_head dports;
Jon Maloy232d07b2018-01-08 21:03:30 +01001187 struct tipc_msg *hdr;
1188 int user, mtyp, hlen;
1189 bool exact;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -05001190
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001191 __skb_queue_head_init(&tmpq);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -05001192 INIT_LIST_HEAD(&dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001193
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001194 skb = tipc_skb_peek(arrvq, &inputq->lock);
1195 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
Jon Maloy232d07b2018-01-08 21:03:30 +01001196 hdr = buf_msg(skb);
1197 user = msg_user(hdr);
1198 mtyp = msg_type(hdr);
1199 hlen = skb_headroom(skb) + msg_hdr_sz(hdr);
Jon Maloy232d07b2018-01-08 21:03:30 +01001200 onode = msg_orignode(hdr);
1201 type = msg_nametype(hdr);
1202
Jon Maloy2f487712017-10-13 11:04:31 +02001203 if (mtyp == TIPC_GRP_UCAST_MSG || user == GROUP_PROTOCOL) {
1204 spin_lock_bh(&inputq->lock);
1205 if (skb_peek(arrvq) == skb) {
1206 __skb_dequeue(arrvq);
1207 __skb_queue_tail(inputq, skb);
1208 }
Jon Maloyc545a942017-12-11 19:11:55 +01001209 kfree_skb(skb);
Jon Maloy2f487712017-10-13 11:04:31 +02001210 spin_unlock_bh(&inputq->lock);
1211 continue;
1212 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001213
Jon Maloy232d07b2018-01-08 21:03:30 +01001214 /* Group messages require exact scope match */
1215 if (msg_in_group(hdr)) {
1216 lower = 0;
1217 upper = ~0;
1218 scope = msg_lookup_scope(hdr);
1219 exact = true;
1220 } else {
1221 /* TIPC_NODE_SCOPE means "any scope" in this context */
1222 if (onode == self)
1223 scope = TIPC_NODE_SCOPE;
1224 else
1225 scope = TIPC_CLUSTER_SCOPE;
1226 exact = false;
1227 lower = msg_namelower(hdr);
1228 upper = msg_nameupper(hdr);
Jon Maloy75da2162017-10-13 11:04:23 +02001229 }
Jon Maloy232d07b2018-01-08 21:03:30 +01001230
1231 /* Create destination port list: */
1232 tipc_nametbl_mc_lookup(net, type, lower, upper,
1233 scope, exact, &dports);
1234
1235 /* Clone message per destination */
Jon Maloya80ae532017-10-13 11:04:22 +02001236 while (tipc_dest_pop(&dports, NULL, &portid)) {
Jon Maloy232d07b2018-01-08 21:03:30 +01001237 _skb = __pskb_copy(skb, hlen, GFP_ATOMIC);
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001238 if (_skb) {
1239 msg_set_destport(buf_msg(_skb), portid);
1240 __skb_queue_tail(&tmpq, _skb);
1241 continue;
1242 }
1243 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001244 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001245 /* Append to inputq if not already done by other thread */
1246 spin_lock_bh(&inputq->lock);
1247 if (skb_peek(arrvq) == skb) {
1248 skb_queue_splice_tail_init(&tmpq, inputq);
1249 kfree_skb(__skb_dequeue(arrvq));
1250 }
1251 spin_unlock_bh(&inputq->lock);
1252 __skb_queue_purge(&tmpq);
1253 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001254 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -05001255 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -04001256}
1257
Jon Maloyc0bceb92019-10-30 14:00:41 +01001258/* tipc_sk_push_backlog(): send accumulated buffers in socket write queue
1259 * when socket is in Nagle mode
1260 */
Tuong Lien0a3e0602020-05-26 16:38:38 +07001261static void tipc_sk_push_backlog(struct tipc_sock *tsk, bool nagle_ack)
Jon Maloyc0bceb92019-10-30 14:00:41 +01001262{
1263 struct sk_buff_head *txq = &tsk->sk.sk_write_queue;
Tuong Lien0a3e0602020-05-26 16:38:38 +07001264 struct sk_buff *skb = skb_peek_tail(txq);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001265 struct net *net = sock_net(&tsk->sk);
1266 u32 dnode = tsk_peer_node(tsk);
1267 int rc;
1268
Tuong Lien0a3e0602020-05-26 16:38:38 +07001269 if (nagle_ack) {
1270 tsk->pkt_cnt += skb_queue_len(txq);
1271 if (!tsk->pkt_cnt || tsk->msg_acc / tsk->pkt_cnt < 2) {
1272 tsk->oneway = 0;
1273 if (tsk->nagle_start < NAGLE_START_MAX)
1274 tsk->nagle_start *= 2;
1275 tsk->expect_ack = false;
1276 pr_debug("tsk %10u: bad nagle %u -> %u, next start %u!\n",
1277 tsk->portid, tsk->msg_acc, tsk->pkt_cnt,
1278 tsk->nagle_start);
1279 } else {
1280 tsk->nagle_start = NAGLE_START_INIT;
1281 if (skb) {
1282 msg_set_ack_required(buf_msg(skb));
1283 tsk->expect_ack = true;
1284 } else {
1285 tsk->expect_ack = false;
1286 }
1287 }
1288 tsk->msg_acc = 0;
1289 tsk->pkt_cnt = 0;
1290 }
1291
Tung Nguyend34910e2019-11-28 10:10:08 +07001292 if (!skb || tsk->cong_link_cnt)
1293 return;
1294
1295 /* Do not send SYN again after congestion */
1296 if (msg_is_syn(buf_msg(skb)))
Jon Maloyc0bceb92019-10-30 14:00:41 +01001297 return;
1298
Tuong Lien0a3e0602020-05-26 16:38:38 +07001299 if (tsk->msg_acc)
1300 tsk->pkt_cnt += skb_queue_len(txq);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001301 tsk->snt_unacked += tsk->snd_backlog;
1302 tsk->snd_backlog = 0;
Jon Maloyc0bceb92019-10-30 14:00:41 +01001303 rc = tipc_node_xmit(net, txq, dnode, tsk->portid);
1304 if (rc == -ELINKCONG)
1305 tsk->cong_link_cnt = 1;
1306}
1307
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001308/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02001309 * tipc_sk_conn_proto_rcv - receive a connection mng protocol message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001310 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001311 * @skb: pointer to message buffer.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001312 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02001313static void tipc_sk_conn_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
Parthasarathy Bhuvaragane7eb0582018-10-10 17:50:23 +02001314 struct sk_buff_head *inputq,
Jon Maloy64ac5f52017-10-13 11:04:20 +02001315 struct sk_buff_head *xmitq)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001316{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001317 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy64ac5f52017-10-13 11:04:20 +02001318 u32 onode = tsk_own_node(tsk);
1319 struct sock *sk = &tsk->sk;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001320 int mtyp = msg_type(hdr);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001321 bool was_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001322
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001323 /* Ignore if connection cannot be validated: */
Tuong Lien01e661e2018-12-19 09:17:58 +07001324 if (!tsk_peer_msg(tsk, hdr)) {
1325 trace_tipc_sk_drop_msg(sk, skb, TIPC_DUMP_NONE, "@proto_rcv!");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001326 goto exit;
Tuong Lien01e661e2018-12-19 09:17:58 +07001327 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001328
Parthasarathy Bhuvaraganc1be7752017-04-26 10:05:02 +02001329 if (unlikely(msg_errcode(hdr))) {
1330 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1331 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
1332 tsk_peer_port(tsk));
1333 sk->sk_state_change(sk);
Parthasarathy Bhuvaragane7eb0582018-10-10 17:50:23 +02001334
1335 /* State change is ignored if socket already awake,
1336 * - convert msg to abort msg and add to inqueue
1337 */
1338 msg_set_user(hdr, TIPC_CRITICAL_IMPORTANCE);
1339 msg_set_type(hdr, TIPC_CONN_MSG);
1340 msg_set_size(hdr, BASIC_H_SIZE);
1341 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
1342 __skb_queue_tail(inputq, skb);
1343 return;
Parthasarathy Bhuvaraganc1be7752017-04-26 10:05:02 +02001344 }
1345
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001346 tsk->probe_unacked = false;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001347
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001348 if (mtyp == CONN_PROBE) {
1349 msg_set_type(hdr, CONN_PROBE_REPLY);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001350 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
1351 __skb_queue_tail(xmitq, skb);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001352 return;
1353 } else if (mtyp == CONN_ACK) {
Jon Maloyc0bceb92019-10-30 14:00:41 +01001354 was_cong = tsk_conn_cong(tsk);
Tuong Lien0a3e0602020-05-26 16:38:38 +07001355 tipc_sk_push_backlog(tsk, msg_nagle_ack(hdr));
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001356 tsk->snt_unacked -= msg_conn_ack(hdr);
1357 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1358 tsk->snd_win = msg_adv_win(hdr);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001359 if (was_cong && !tsk_conn_cong(tsk))
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001360 sk->sk_write_space(sk);
1361 } else if (mtyp != CONN_PROBE_REPLY) {
1362 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001363 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001364exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -04001365 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -05001366}
1367
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001368/**
Ying Xue247f0f32014-02-18 16:06:46 +08001369 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370 * @sock: socket structure
1371 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001372 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001373 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001375 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
1377 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001378 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001379 * Returns the number of bytes sent on success, or errno otherwise
1380 */
Ying Xue1b784142015-03-02 15:37:48 +08001381static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001382 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001383{
Ying Xue39a0295f2015-03-02 15:37:47 +08001384 struct sock *sk = sock->sk;
1385 int ret;
1386
1387 lock_sock(sk);
1388 ret = __tipc_sendmsg(sock, m, dsz);
1389 release_sock(sk);
1390
1391 return ret;
1392}
1393
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001394static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a0295f2015-03-02 15:37:47 +08001395{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001396 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001397 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001398 struct tipc_sock *tsk = tipc_sk(sk);
1399 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1400 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1401 struct list_head *clinks = &tsk->cong_links;
1402 bool syn = !tipc_sk_type_connectionless(sk);
Jon Maloy75da2162017-10-13 11:04:23 +02001403 struct tipc_group *grp = tsk->group;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001404 struct tipc_msg *hdr = &tsk->phdr;
Erik Hugnef2f80362015-03-19 09:02:19 +01001405 struct tipc_name_seq *seq;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001406 struct sk_buff_head pkts;
Tuong Lienabc9b4e2019-12-10 15:21:04 +07001407 u32 dport = 0, dnode = 0;
1408 u32 type = 0, inst = 0;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001409 int mtu, rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001410
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001411 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
Allan Stephensc29c3f72010-04-20 17:58:24 -04001412 return -EMSGSIZE;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001413
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001414 if (likely(dest)) {
1415 if (unlikely(m->msg_namelen < sizeof(*dest)))
1416 return -EINVAL;
1417 if (unlikely(dest->family != AF_TIPC))
1418 return -EINVAL;
1419 }
1420
1421 if (grp) {
1422 if (!dest)
1423 return tipc_send_group_bcast(sock, m, dlen, timeout);
Jon Maloyee106d72017-10-13 11:04:28 +02001424 if (dest->addrtype == TIPC_ADDR_NAME)
1425 return tipc_send_group_anycast(sock, m, dlen, timeout);
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001426 if (dest->addrtype == TIPC_ADDR_ID)
1427 return tipc_send_group_unicast(sock, m, dlen, timeout);
Jon Maloy5b8dddb2017-10-13 11:04:29 +02001428 if (dest->addrtype == TIPC_ADDR_MCAST)
1429 return tipc_send_group_mcast(sock, m, dlen, timeout);
Jon Maloy27bd9ec2017-10-13 11:04:27 +02001430 return -EINVAL;
1431 }
Jon Maloy75da2162017-10-13 11:04:23 +02001432
Erik Hugnef2f80362015-03-19 09:02:19 +01001433 if (unlikely(!dest)) {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001434 dest = &tsk->peer;
Erik Hugne0e632082019-03-04 23:26:10 +01001435 if (!syn && dest->family != AF_TIPC)
Erik Hugnef2f80362015-03-19 09:02:19 +01001436 return -EDESTADDRREQ;
Erik Hugnef2f80362015-03-19 09:02:19 +01001437 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001438
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001439 if (unlikely(syn)) {
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001440 if (sk->sk_state == TIPC_LISTEN)
Ying Xue39a0295f2015-03-02 15:37:47 +08001441 return -EPIPE;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001442 if (sk->sk_state != TIPC_OPEN)
Ying Xue39a0295f2015-03-02 15:37:47 +08001443 return -EISCONN;
1444 if (tsk->published)
1445 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001446 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001447 tsk->conn_type = dest->addr.name.name.type;
1448 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001449 }
Jon Maloy25b92212018-09-28 20:23:21 +02001450 msg_set_syn(hdr, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001451 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001452
Erik Hugnef2f80362015-03-19 09:02:19 +01001453 seq = &dest->addr.nameseq;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001454 if (dest->addrtype == TIPC_ADDR_MCAST)
1455 return tipc_sendmcast(sock, seq, m, dlen, timeout);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001456
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001457 if (dest->addrtype == TIPC_ADDR_NAME) {
1458 type = dest->addr.name.name.type;
1459 inst = dest->addr.name.name.instance;
Jon Maloy928df182018-03-15 16:48:51 +01001460 dnode = dest->addr.name.domain;
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001461 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Ying Xue39a0295f2015-03-02 15:37:47 +08001462 if (unlikely(!dport && !dnode))
1463 return -EHOSTUNREACH;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001464 } else if (dest->addrtype == TIPC_ADDR_ID) {
1465 dnode = dest->addr.id.node;
Jon Maloy335b9292018-04-12 01:15:48 +02001466 } else {
1467 return -EINVAL;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001468 }
1469
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001470 /* Block or return if destination link is congested */
Jon Maloya80ae532017-10-13 11:04:22 +02001471 rc = tipc_wait_for_cond(sock, &timeout,
1472 !tipc_dest_find(clinks, dnode, 0));
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001473 if (unlikely(rc))
Ying Xue39a0295f2015-03-02 15:37:47 +08001474 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001475
Tuong Lienabc9b4e2019-12-10 15:21:04 +07001476 if (dest->addrtype == TIPC_ADDR_NAME) {
1477 msg_set_type(hdr, TIPC_NAMED_MSG);
1478 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
1479 msg_set_nametype(hdr, type);
1480 msg_set_nameinst(hdr, inst);
1481 msg_set_lookup_scope(hdr, tipc_node2scope(dnode));
1482 msg_set_destnode(hdr, dnode);
1483 msg_set_destport(hdr, dport);
1484 } else { /* TIPC_ADDR_ID */
1485 msg_set_type(hdr, TIPC_DIRECT_MSG);
1486 msg_set_lookup_scope(hdr, 0);
1487 msg_set_destnode(hdr, dnode);
1488 msg_set_destport(hdr, dest->addr.id.ref);
1489 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
1490 }
1491
Jon Maloye654f9f2019-08-15 16:42:50 +02001492 __skb_queue_head_init(&pkts);
Hoang Le8b1e5b0a2020-03-26 09:50:29 +07001493 mtu = tipc_node_get_mtu(net, dnode, tsk->portid, true);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001494 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1495 if (unlikely(rc != dlen))
1496 return rc;
Tung Nguyen2fe97a52019-11-28 10:10:05 +07001497 if (unlikely(syn && !tipc_msg_skb_clone(&pkts, &sk->sk_write_queue))) {
1498 __skb_queue_purge(&pkts);
Tung Nguyen67879272018-09-28 20:23:22 +02001499 return -ENOMEM;
Tung Nguyen2fe97a52019-11-28 10:10:05 +07001500 }
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001501
Tuong Lien01e661e2018-12-19 09:17:58 +07001502 trace_tipc_sk_sendmsg(sk, skb_peek(&pkts), TIPC_DUMP_SK_SNDQ, " ");
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001503 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1504 if (unlikely(rc == -ELINKCONG)) {
Jon Maloya80ae532017-10-13 11:04:22 +02001505 tipc_dest_push(clinks, dnode, 0);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001506 tsk->cong_link_cnt++;
1507 rc = 0;
1508 }
1509
1510 if (unlikely(syn && !rc))
1511 tipc_set_sk_state(sk, TIPC_CONNECTING);
1512
1513 return rc ? rc : dlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001514}
1515
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001516/**
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001517 * tipc_sendstream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001518 * @sock: socket structure
1519 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001520 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001521 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001522 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001523 *
1524 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001525 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001526 */
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001527static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001528{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001529 struct sock *sk = sock->sk;
Ying Xue39a0295f2015-03-02 15:37:47 +08001530 int ret;
1531
1532 lock_sock(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001533 ret = __tipc_sendstream(sock, m, dsz);
Ying Xue39a0295f2015-03-02 15:37:47 +08001534 release_sock(sk);
1535
1536 return ret;
1537}
1538
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001539static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a0295f2015-03-02 15:37:47 +08001540{
1541 struct sock *sk = sock->sk;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001542 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001543 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001544 struct sk_buff_head *txq = &sk->sk_write_queue;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001545 struct tipc_sock *tsk = tipc_sk(sk);
1546 struct tipc_msg *hdr = &tsk->phdr;
1547 struct net *net = sock_net(sk);
Tuong Lien0a3e0602020-05-26 16:38:38 +07001548 struct sk_buff *skb;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001549 u32 dnode = tsk_peer_node(tsk);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001550 int maxnagle = tsk->maxnagle;
1551 int maxpkt = tsk->max_pkt;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001552 int send, sent = 0;
Jon Maloyc0bceb92019-10-30 14:00:41 +01001553 int blocks, rc = 0;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001554
1555 if (unlikely(dlen > INT_MAX))
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001556 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001557
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001558 /* Handle implicit connection setup */
1559 if (unlikely(dest)) {
1560 rc = __tipc_sendmsg(sock, m, dlen);
Parthasarathy Bhuvaragan92ef12b2018-09-25 18:21:58 +02001561 if (dlen && dlen == rc) {
1562 tsk->peer_caps = tipc_node_get_capabilities(net, dnode);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001563 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
Parthasarathy Bhuvaragan92ef12b2018-09-25 18:21:58 +02001564 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001565 return rc;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001566 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001567
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001568 do {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001569 rc = tipc_wait_for_cond(sock, &timeout,
1570 (!tsk->cong_link_cnt &&
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -05001571 !tsk_conn_cong(tsk) &&
1572 tipc_sk_connected(sk)));
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001573 if (unlikely(rc))
1574 break;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001575 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001576 blocks = tsk->snd_backlog;
Tuong Lien0a3e0602020-05-26 16:38:38 +07001577 if (tsk->oneway++ >= tsk->nagle_start && send <= maxnagle) {
Jon Maloyc0bceb92019-10-30 14:00:41 +01001578 rc = tipc_msg_append(hdr, m, send, maxnagle, txq);
1579 if (unlikely(rc < 0))
1580 break;
1581 blocks += rc;
Tuong Lien0a3e0602020-05-26 16:38:38 +07001582 tsk->msg_acc++;
Jon Maloyc0bceb92019-10-30 14:00:41 +01001583 if (blocks <= 64 && tsk->expect_ack) {
1584 tsk->snd_backlog = blocks;
1585 sent += send;
1586 break;
Tuong Lien0a3e0602020-05-26 16:38:38 +07001587 } else if (blocks > 64) {
1588 tsk->pkt_cnt += skb_queue_len(txq);
1589 } else {
1590 skb = skb_peek_tail(txq);
1591 msg_set_ack_required(buf_msg(skb));
1592 tsk->expect_ack = true;
1593 tsk->msg_acc = 0;
1594 tsk->pkt_cnt = 0;
Jon Maloyc0bceb92019-10-30 14:00:41 +01001595 }
Jon Maloyc0bceb92019-10-30 14:00:41 +01001596 } else {
1597 rc = tipc_msg_build(hdr, m, sent, send, maxpkt, txq);
1598 if (unlikely(rc != send))
1599 break;
1600 blocks += tsk_inc(tsk, send + MIN_H_SIZE);
1601 }
1602 trace_tipc_sk_sendstream(sk, skb_peek(txq),
Tuong Lien01e661e2018-12-19 09:17:58 +07001603 TIPC_DUMP_SK_SNDQ, " ");
Jon Maloyc0bceb92019-10-30 14:00:41 +01001604 rc = tipc_node_xmit(net, txq, dnode, tsk->portid);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001605 if (unlikely(rc == -ELINKCONG)) {
1606 tsk->cong_link_cnt = 1;
1607 rc = 0;
1608 }
1609 if (likely(!rc)) {
Jon Maloyc0bceb92019-10-30 14:00:41 +01001610 tsk->snt_unacked += blocks;
1611 tsk->snd_backlog = 0;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001612 sent += send;
1613 }
1614 } while (sent < dlen && !rc);
1615
Parthasarathy Bhuvaragan3364d612017-04-24 15:00:42 +02001616 return sent ? sent : rc;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001617}
1618
1619/**
1620 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001621 * @sock: socket structure
1622 * @m: message to send
1623 * @dsz: length of data to be transmitted
1624 *
1625 * Used for SOCK_SEQPACKET messages.
1626 *
1627 * Returns the number of bytes sent on success, or errno otherwise
1628 */
Ying Xue1b784142015-03-02 15:37:48 +08001629static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001630{
1631 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1632 return -EMSGSIZE;
1633
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001634 return tipc_sendstream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001635}
1636
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001637/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001638 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001639static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001640 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001641{
Ying Xue3721e9c2015-01-13 17:07:48 +08001642 struct sock *sk = &tsk->sk;
1643 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001644 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001645
Jon Maloy25b92212018-09-28 20:23:21 +02001646 msg_set_syn(msg, 0);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001647 msg_set_destnode(msg, peer_node);
1648 msg_set_destport(msg, peer_port);
1649 msg_set_type(msg, TIPC_CONN_MSG);
1650 msg_set_lookup_scope(msg, 0);
1651 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001652
Jon Maloy0d5fcebf2017-10-20 11:21:32 +02001653 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001654 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
Ying Xuef2f98002015-01-09 15:27:05 +08001655 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
Hoang Lef73b1282019-10-29 07:51:21 +07001656 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid, true);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001657 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Maloyc0bceb92019-10-30 14:00:41 +01001658 tsk_set_nagle(tsk);
Tung Nguyen67879272018-09-28 20:23:22 +02001659 __skb_queue_purge(&sk->sk_write_queue);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001660 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1661 return;
1662
1663 /* Fall back to message based flow control */
1664 tsk->rcv_win = FLOWCTL_MSG_WIN;
1665 tsk->snd_win = FLOWCTL_MSG_WIN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666}
1667
1668/**
Jon Maloy31c82a22017-10-13 11:04:24 +02001669 * tipc_sk_set_orig_addr - capture sender's address for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001670 * @m: descriptor for message info
Jon Maloy31c82a22017-10-13 11:04:24 +02001671 * @hdr: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001672 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001673 * Note: Address is not captured if not requested by receiver.
1674 */
Jon Maloy31c82a22017-10-13 11:04:24 +02001675static void tipc_sk_set_orig_addr(struct msghdr *m, struct sk_buff *skb)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001676{
Jon Maloy31c82a22017-10-13 11:04:24 +02001677 DECLARE_SOCKADDR(struct sockaddr_pair *, srcaddr, m->msg_name);
1678 struct tipc_msg *hdr = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679
Jon Maloy31c82a22017-10-13 11:04:24 +02001680 if (!srcaddr)
1681 return;
1682
1683 srcaddr->sock.family = AF_TIPC;
1684 srcaddr->sock.addrtype = TIPC_ADDR_ID;
Eric Dumazet09c8b972018-05-09 09:50:22 -07001685 srcaddr->sock.scope = 0;
Jon Maloy31c82a22017-10-13 11:04:24 +02001686 srcaddr->sock.addr.id.ref = msg_origport(hdr);
1687 srcaddr->sock.addr.id.node = msg_orignode(hdr);
1688 srcaddr->sock.addr.name.domain = 0;
Jon Maloy31c82a22017-10-13 11:04:24 +02001689 m->msg_namelen = sizeof(struct sockaddr_tipc);
1690
1691 if (!msg_in_group(hdr))
1692 return;
1693
1694 /* Group message users may also want to know sending member's id */
1695 srcaddr->member.family = AF_TIPC;
1696 srcaddr->member.addrtype = TIPC_ADDR_NAME;
Eric Dumazet09c8b972018-05-09 09:50:22 -07001697 srcaddr->member.scope = 0;
Jon Maloy31c82a22017-10-13 11:04:24 +02001698 srcaddr->member.addr.name.name.type = msg_nametype(hdr);
1699 srcaddr->member.addr.name.name.instance = TIPC_SKB_CB(skb)->orig_member;
1700 srcaddr->member.addr.name.domain = 0;
1701 m->msg_namelen = sizeof(*srcaddr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001702}
1703
1704/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001705 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001706 * @m: descriptor for message info
Jon Maloy1c1274a2018-11-17 12:17:06 -05001707 * @skb: received message buffer
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001708 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001709 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001710 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001711 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001712 * Returns 0 if successful, otherwise errno
1713 */
Jon Maloy1c1274a2018-11-17 12:17:06 -05001714static int tipc_sk_anc_data_recv(struct msghdr *m, struct sk_buff *skb,
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001715 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001716{
Jon Maloy1c1274a2018-11-17 12:17:06 -05001717 struct tipc_msg *msg;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001718 u32 anc_data[3];
1719 u32 err;
1720 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001721 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001722 int res;
1723
1724 if (likely(m->msg_controllen == 0))
1725 return 0;
Jon Maloy1c1274a2018-11-17 12:17:06 -05001726 msg = buf_msg(skb);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001727
1728 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001729 err = msg ? msg_errcode(msg) : 0;
1730 if (unlikely(err)) {
1731 anc_data[0] = err;
1732 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001733 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1734 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001735 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001736 if (anc_data[1]) {
Jon Maloy1c1274a2018-11-17 12:17:06 -05001737 if (skb_linearize(skb))
1738 return -ENOMEM;
1739 msg = buf_msg(skb);
Allan Stephens2db99832010-12-31 18:59:33 +00001740 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1741 msg_data(msg));
1742 if (res)
1743 return res;
1744 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001745 }
1746
1747 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001748 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1749 switch (dest_type) {
1750 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001751 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001752 anc_data[0] = msg_nametype(msg);
1753 anc_data[1] = msg_namelower(msg);
1754 anc_data[2] = msg_namelower(msg);
1755 break;
1756 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001757 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001758 anc_data[0] = msg_nametype(msg);
1759 anc_data[1] = msg_namelower(msg);
1760 anc_data[2] = msg_nameupper(msg);
1761 break;
1762 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001763 has_name = (tsk->conn_type != 0);
1764 anc_data[0] = tsk->conn_type;
1765 anc_data[1] = tsk->conn_instance;
1766 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001767 break;
1768 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001769 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001770 }
Allan Stephens2db99832010-12-31 18:59:33 +00001771 if (has_name) {
1772 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1773 if (res)
1774 return res;
1775 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001776
1777 return 0;
1778}
1779
Tuong Lienc7268582020-05-13 19:33:16 +07001780static struct sk_buff *tipc_sk_build_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001781{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001782 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08001783 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001784 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001785 u32 peer_port = tsk_peer_port(tsk);
1786 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001787
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001788 if (!tipc_sk_connected(sk))
Tuong Lienc7268582020-05-13 19:33:16 +07001789 return NULL;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001790 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1791 dnode, tsk_own_node(tsk), peer_port,
1792 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001793 if (!skb)
Tuong Lienc7268582020-05-13 19:33:16 +07001794 return NULL;
Ying Xuea6ca1092014-11-26 11:41:55 +08001795 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001796 msg_set_conn_ack(msg, tsk->rcv_unacked);
1797 tsk->rcv_unacked = 0;
1798
1799 /* Adjust to and advertize the correct window limit */
1800 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1801 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1802 msg_set_adv_win(msg, tsk->rcv_win);
1803 }
Tuong Lienc7268582020-05-13 19:33:16 +07001804 return skb;
1805}
1806
1807static void tipc_sk_send_ack(struct tipc_sock *tsk)
1808{
1809 struct sk_buff *skb;
1810
1811 skb = tipc_sk_build_ack(tsk);
1812 if (!skb)
1813 return;
1814
1815 tipc_node_xmit_skb(sock_net(&tsk->sk), skb, tsk_peer_node(tsk),
1816 msg_link_selector(buf_msg(skb)));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001817}
1818
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001819static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001820{
1821 struct sock *sk = sock->sk;
Tung Nguyen48766a52019-02-19 11:20:48 +07001822 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001823 long timeo = *timeop;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001824 int err = sock_error(sk);
1825
1826 if (err)
1827 return err;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001828
1829 for (;;) {
Ying Xuefe8e4642014-03-06 14:40:18 +01001830 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01001831 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001832 err = -ENOTCONN;
1833 break;
1834 }
Tung Nguyen48766a52019-02-19 11:20:48 +07001835 add_wait_queue(sk_sleep(sk), &wait);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001836 release_sock(sk);
Tung Nguyen48766a52019-02-19 11:20:48 +07001837 timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
1838 sched_annotate_sleep();
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001839 lock_sock(sk);
Tung Nguyen48766a52019-02-19 11:20:48 +07001840 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001841 }
1842 err = 0;
1843 if (!skb_queue_empty(&sk->sk_receive_queue))
1844 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001845 err = -EAGAIN;
1846 if (!timeo)
1847 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001848 err = sock_intr_errno(timeo);
1849 if (signal_pending(current))
1850 break;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001851
1852 err = sock_error(sk);
1853 if (err)
1854 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001855 }
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001856 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001857 return err;
1858}
1859
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001860/**
Ying Xue247f0f32014-02-18 16:06:46 +08001861 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001862 * @m: descriptor for message info
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001863 * @buflen: length of user buffer area
Per Lidenb97bf3f2006-01-02 19:04:38 +01001864 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001865 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001866 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1867 * If the complete message doesn't fit in user area, truncate it.
1868 *
1869 * Returns size of returned message data, errno otherwise
1870 */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001871static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
1872 size_t buflen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001873{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001874 struct sock *sk = sock->sk;
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001875 bool connected = !tipc_sk_type_connectionless(sk);
Jon Maloyae236fb2017-10-13 11:04:25 +02001876 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001877 int rc, err, hlen, dlen, copy;
Jon Maloyb7d42632017-10-13 11:04:26 +02001878 struct sk_buff_head xmitq;
Jon Maloyae236fb2017-10-13 11:04:25 +02001879 struct tipc_msg *hdr;
1880 struct sk_buff *skb;
1881 bool grp_evt;
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001882 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001883
Allan Stephens0c3141e2008-04-15 00:22:02 -07001884 /* Catch invalid receive requests */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001885 if (unlikely(!buflen))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886 return -EINVAL;
1887
Allan Stephens0c3141e2008-04-15 00:22:02 -07001888 lock_sock(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001889 if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
1890 rc = -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001891 goto exit;
1892 }
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001893 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001894
Jon Maloyb7d42632017-10-13 11:04:26 +02001895 /* Step rcv queue to first msg with data or error; wait if necessary */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001896 do {
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001897 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1898 if (unlikely(rc))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001899 goto exit;
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001900 skb = skb_peek(&sk->sk_receive_queue);
1901 hdr = buf_msg(skb);
1902 dlen = msg_data_sz(hdr);
1903 hlen = msg_hdr_sz(hdr);
1904 err = msg_errcode(hdr);
Jon Maloyae236fb2017-10-13 11:04:25 +02001905 grp_evt = msg_is_grp_evt(hdr);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001906 if (likely(dlen || err))
1907 break;
1908 tsk_advance_rx_queue(sk);
1909 } while (1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001910
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001911 /* Collect msg meta data, including error code and rejected data */
Jon Maloy31c82a22017-10-13 11:04:24 +02001912 tipc_sk_set_orig_addr(m, skb);
Jon Maloy1c1274a2018-11-17 12:17:06 -05001913 rc = tipc_sk_anc_data_recv(m, skb, tsk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001914 if (unlikely(rc))
1915 goto exit;
Jon Maloy1c1274a2018-11-17 12:17:06 -05001916 hdr = buf_msg(skb);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001917
1918 /* Capture data if non-error msg, otherwise just set return value */
1919 if (likely(!err)) {
1920 copy = min_t(int, dlen, buflen);
1921 if (unlikely(copy != dlen))
1922 m->msg_flags |= MSG_TRUNC;
1923 rc = skb_copy_datagram_msg(skb, hlen, m, copy);
1924 } else {
1925 copy = 0;
1926 rc = 0;
1927 if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control)
1928 rc = -ECONNRESET;
1929 }
1930 if (unlikely(rc))
1931 goto exit;
1932
Jon Maloyae236fb2017-10-13 11:04:25 +02001933 /* Mark message as group event if applicable */
1934 if (unlikely(grp_evt)) {
1935 if (msg_grp_evt(hdr) == TIPC_WITHDRAWN)
1936 m->msg_flags |= MSG_EOR;
1937 m->msg_flags |= MSG_OOB;
1938 copy = 0;
1939 }
1940
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001941 /* Caption of data or error code/rejected data was successful */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001942 if (unlikely(flags & MSG_PEEK))
1943 goto exit;
1944
Jon Maloyb7d42632017-10-13 11:04:26 +02001945 /* Send group flow control advertisement when applicable */
1946 if (tsk->group && msg_in_group(hdr) && !grp_evt) {
Jon Maloye654f9f2019-08-15 16:42:50 +02001947 __skb_queue_head_init(&xmitq);
Jon Maloyb7d42632017-10-13 11:04:26 +02001948 tipc_group_update_rcv_win(tsk->group, tsk_blocks(hlen + dlen),
1949 msg_orignode(hdr), msg_origport(hdr),
1950 &xmitq);
1951 tipc_node_distr_xmit(sock_net(sk), &xmitq);
1952 }
1953
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001954 tsk_advance_rx_queue(sk);
Jon Maloyae236fb2017-10-13 11:04:25 +02001955
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001956 if (likely(!connected))
1957 goto exit;
1958
Jon Maloyb7d42632017-10-13 11:04:26 +02001959 /* Send connection flow control advertisement when applicable */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001960 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1961 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
1962 tipc_sk_send_ack(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001963exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001964 release_sock(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001965 return rc ? rc : copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001966}
1967
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001968/**
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001969 * tipc_recvstream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001970 * @m: descriptor for message info
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001971 * @buflen: total size of user buffer area
Per Lidenb97bf3f2006-01-02 19:04:38 +01001972 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001973 *
1974 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001975 * will optionally wait for more; never truncates data.
1976 *
1977 * Returns size of returned message data, errno otherwise
1978 */
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001979static int tipc_recvstream(struct socket *sock, struct msghdr *m,
1980 size_t buflen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001981{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001982 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001983 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001984 struct sk_buff *skb;
1985 struct tipc_msg *hdr;
1986 struct tipc_skb_cb *skb_cb;
1987 bool peek = flags & MSG_PEEK;
1988 int offset, required, copy, copied = 0;
1989 int hlen, dlen, err, rc;
1990 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001991
1992 /* Catch invalid receive attempts */
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001993 if (unlikely(!buflen))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001994 return -EINVAL;
1995
Allan Stephens0c3141e2008-04-15 00:22:02 -07001996 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001997
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001998 if (unlikely(sk->sk_state == TIPC_OPEN)) {
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001999 rc = -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002000 goto exit;
2001 }
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002002 required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
2003 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002004
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002005 do {
2006 /* Look at first msg in receive queue; wait if necessary */
2007 rc = tipc_wait_for_rcvmsg(sock, &timeout);
2008 if (unlikely(rc))
2009 break;
2010 skb = skb_peek(&sk->sk_receive_queue);
2011 skb_cb = TIPC_SKB_CB(skb);
2012 hdr = buf_msg(skb);
2013 dlen = msg_data_sz(hdr);
2014 hlen = msg_hdr_sz(hdr);
2015 err = msg_errcode(hdr);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04002016
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002017 /* Discard any empty non-errored (SYN-) message */
2018 if (unlikely(!dlen && !err)) {
2019 tsk_advance_rx_queue(sk);
2020 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002021 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002022
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002023 /* Collect msg meta data, incl. error code and rejected data */
2024 if (!copied) {
Jon Maloy31c82a22017-10-13 11:04:24 +02002025 tipc_sk_set_orig_addr(m, skb);
Jon Maloy1c1274a2018-11-17 12:17:06 -05002026 rc = tipc_sk_anc_data_recv(m, skb, tsk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002027 if (rc)
2028 break;
Jon Maloy1c1274a2018-11-17 12:17:06 -05002029 hdr = buf_msg(skb);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002030 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002031
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002032 /* Copy data if msg ok, otherwise return error/partial data */
2033 if (likely(!err)) {
2034 offset = skb_cb->bytes_read;
2035 copy = min_t(int, dlen - offset, buflen - copied);
2036 rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
2037 if (unlikely(rc))
2038 break;
2039 copied += copy;
2040 offset += copy;
2041 if (unlikely(offset < dlen)) {
2042 if (!peek)
2043 skb_cb->bytes_read = offset;
2044 break;
2045 }
2046 } else {
2047 rc = 0;
2048 if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control)
2049 rc = -ECONNRESET;
2050 if (copied || rc)
2051 break;
2052 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002053
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002054 if (unlikely(peek))
2055 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002056
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002057 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002058
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002059 /* Send connection flow control advertisement when applicable */
2060 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
Tuong Lienc7268582020-05-13 19:33:16 +07002061 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002062 tipc_sk_send_ack(tsk);
2063
2064 /* Exit if all requested data or FIN/error received */
2065 if (copied == buflen || err)
2066 break;
2067
2068 } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002069exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002070 release_sock(sk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002071 return copied ? copied : rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002072}
2073
2074/**
Ying Xuef288bef2012-08-21 11:16:57 +08002075 * tipc_write_space - wake up thread if port congestion is released
2076 * @sk: socket
2077 */
2078static void tipc_write_space(struct sock *sk)
2079{
2080 struct socket_wq *wq;
2081
2082 rcu_read_lock();
2083 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08002084 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002085 wake_up_interruptible_sync_poll(&wq->wait, EPOLLOUT |
2086 EPOLLWRNORM | EPOLLWRBAND);
Ying Xuef288bef2012-08-21 11:16:57 +08002087 rcu_read_unlock();
2088}
2089
2090/**
2091 * tipc_data_ready - wake up threads to indicate messages have been received
2092 * @sk: socket
2093 * @len: the length of messages
2094 */
David S. Miller676d2362014-04-11 16:15:36 -04002095static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08002096{
2097 struct socket_wq *wq;
2098
2099 rcu_read_lock();
2100 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08002101 if (skwq_has_sleeper(wq))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002102 wake_up_interruptible_sync_poll(&wq->wait, EPOLLIN |
2103 EPOLLRDNORM | EPOLLRDBAND);
Ying Xuef288bef2012-08-21 11:16:57 +08002104 rcu_read_unlock();
2105}
2106
Ying Xuef4195d12015-11-22 15:46:05 +08002107static void tipc_sock_destruct(struct sock *sk)
2108{
2109 __skb_queue_purge(&sk->sk_receive_queue);
2110}
2111
Jon Maloy64ac5f52017-10-13 11:04:20 +02002112static void tipc_sk_proto_rcv(struct sock *sk,
2113 struct sk_buff_head *inputq,
2114 struct sk_buff_head *xmitq)
2115{
2116 struct sk_buff *skb = __skb_dequeue(inputq);
2117 struct tipc_sock *tsk = tipc_sk(sk);
2118 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy75da2162017-10-13 11:04:23 +02002119 struct tipc_group *grp = tsk->group;
Jon Maloyb7d42632017-10-13 11:04:26 +02002120 bool wakeup = false;
Jon Maloy64ac5f52017-10-13 11:04:20 +02002121
2122 switch (msg_user(hdr)) {
2123 case CONN_MANAGER:
Parthasarathy Bhuvaragane7eb0582018-10-10 17:50:23 +02002124 tipc_sk_conn_proto_rcv(tsk, skb, inputq, xmitq);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002125 return;
2126 case SOCK_WAKEUP:
Jon Maloya80ae532017-10-13 11:04:22 +02002127 tipc_dest_del(&tsk->cong_links, msg_orignode(hdr), 0);
Tung Nguyenbfd07f32019-02-25 10:57:20 +07002128 /* coupled with smp_rmb() in tipc_wait_for_cond() */
2129 smp_wmb();
Jon Maloy64ac5f52017-10-13 11:04:20 +02002130 tsk->cong_link_cnt--;
Jon Maloyb7d42632017-10-13 11:04:26 +02002131 wakeup = true;
Tuong Lien0a3e0602020-05-26 16:38:38 +07002132 tipc_sk_push_backlog(tsk, false);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002133 break;
Jon Maloy75da2162017-10-13 11:04:23 +02002134 case GROUP_PROTOCOL:
Jon Maloyb7d42632017-10-13 11:04:26 +02002135 tipc_group_proto_rcv(grp, &wakeup, hdr, inputq, xmitq);
Jon Maloy75da2162017-10-13 11:04:23 +02002136 break;
Jon Maloy64ac5f52017-10-13 11:04:20 +02002137 case TOP_SRV:
Jon Maloyb7d42632017-10-13 11:04:26 +02002138 tipc_group_member_evt(tsk->group, &wakeup, &sk->sk_rcvbuf,
Jon Maloy7ad32bc2018-01-08 21:03:26 +01002139 hdr, inputq, xmitq);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002140 break;
2141 default:
2142 break;
2143 }
2144
Jon Maloyb7d42632017-10-13 11:04:26 +02002145 if (wakeup)
2146 sk->sk_write_space(sk);
2147
Jon Maloy64ac5f52017-10-13 11:04:20 +02002148 kfree_skb(skb);
2149}
2150
Ying Xuef288bef2012-08-21 11:16:57 +08002151/**
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002152 * tipc_sk_filter_connect - check incoming message for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002153 * @tsk: TIPC socket
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002154 * @skb: pointer to message buffer.
Tuong Lienc7268582020-05-13 19:33:16 +07002155 * @xmitq: for Nagle ACK if any
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002156 * Returns true if message should be added to receive queue, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05002157 */
Tuong Lienc7268582020-05-13 19:33:16 +07002158static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb,
2159 struct sk_buff_head *xmitq)
Ying Xue7e6c1312012-11-29 18:39:14 -05002160{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002161 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08002162 struct net *net = sock_net(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04002163 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002164 bool con_msg = msg_connected(hdr);
2165 u32 pport = tsk_peer_port(tsk);
2166 u32 pnode = tsk_peer_node(tsk);
2167 u32 oport = msg_origport(hdr);
2168 u32 onode = msg_orignode(hdr);
2169 int err = msg_errcode(hdr);
Tung Nguyen67879272018-09-28 20:23:22 +02002170 unsigned long delay;
Ying Xue7e6c1312012-11-29 18:39:14 -05002171
Jon Paul Maloycda36962015-07-22 10:11:20 -04002172 if (unlikely(msg_mcast(hdr)))
2173 return false;
Jon Maloyc0bceb92019-10-30 14:00:41 +01002174 tsk->oneway = 0;
Ying Xue7e6c1312012-11-29 18:39:14 -05002175
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002176 switch (sk->sk_state) {
2177 case TIPC_CONNECTING:
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002178 /* Setup ACK */
2179 if (likely(con_msg)) {
2180 if (err)
2181 break;
2182 tipc_sk_finish_conn(tsk, oport, onode);
2183 msg_set_importance(&tsk->phdr, msg_importance(hdr));
2184 /* ACK+ message with data is added to receive queue */
2185 if (msg_data_sz(hdr))
2186 return true;
2187 /* Empty ACK-, - wake up sleeping connect() and drop */
Parthasarathy Bhuvaraganff946832019-05-09 07:13:42 +02002188 sk->sk_state_change(sk);
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002189 msg_set_dest_droppable(hdr, 1);
2190 return false;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02002191 }
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002192 /* Ignore connectionless message if not from listening socket */
2193 if (oport != pport || onode != pnode)
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002194 return false;
2195
Tung Nguyen67879272018-09-28 20:23:22 +02002196 /* Rejected SYN */
2197 if (err != TIPC_ERR_OVERLOAD)
2198 break;
2199
2200 /* Prepare for new setup attempt if we have a SYN clone */
2201 if (skb_queue_empty(&sk->sk_write_queue))
2202 break;
2203 get_random_bytes(&delay, 2);
2204 delay %= (tsk->conn_timeout / 4);
2205 delay = msecs_to_jiffies(delay + 100);
2206 sk_reset_timer(sk, &sk->sk_timer, jiffies + delay);
2207 return false;
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002208 case TIPC_OPEN:
2209 case TIPC_DISCONNECTING:
2210 return false;
2211 case TIPC_LISTEN:
2212 /* Accept only SYN message */
Jon Maloy25b92212018-09-28 20:23:21 +02002213 if (!msg_is_syn(hdr) &&
2214 tipc_node_get_capabilities(net, onode) & TIPC_SYN_BIT)
2215 return false;
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002216 if (!con_msg && !err)
2217 return true;
2218 return false;
2219 case TIPC_ESTABLISHED:
Jon Maloyc0bceb92019-10-30 14:00:41 +01002220 if (!skb_queue_empty(&sk->sk_write_queue))
Tuong Lien0a3e0602020-05-26 16:38:38 +07002221 tipc_sk_push_backlog(tsk, false);
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002222 /* Accept only connection-based messages sent by peer */
Tuong Lienc7268582020-05-13 19:33:16 +07002223 if (likely(con_msg && !err && pport == oport &&
2224 pnode == onode)) {
2225 if (msg_ack_required(hdr)) {
2226 struct sk_buff *skb;
2227
2228 skb = tipc_sk_build_ack(tsk);
Tuong Lien0a3e0602020-05-26 16:38:38 +07002229 if (skb) {
2230 msg_set_nagle_ack(buf_msg(skb));
Tuong Lienc7268582020-05-13 19:33:16 +07002231 __skb_queue_tail(xmitq, skb);
Tuong Lien0a3e0602020-05-26 16:38:38 +07002232 }
Tuong Lienc7268582020-05-13 19:33:16 +07002233 }
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002234 return true;
Tuong Lienc7268582020-05-13 19:33:16 +07002235 }
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002236 if (!tsk_peer_msg(tsk, hdr))
2237 return false;
2238 if (!err)
2239 return true;
2240 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2241 tipc_node_remove_conn(net, pnode, tsk->portid);
2242 sk->sk_state_change(sk);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002243 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05002244 default:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01002245 pr_err("Unknown sk_state %u\n", sk->sk_state);
Ying Xue7e6c1312012-11-29 18:39:14 -05002246 }
Jon Maloy39fdc9c2018-09-28 20:23:20 +02002247 /* Abort connection setup attempt */
2248 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2249 sk->sk_err = ECONNREFUSED;
2250 sk->sk_state_change(sk);
2251 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05002252}
2253
2254/**
Ying Xueaba79f32013-01-20 23:30:09 +01002255 * rcvbuf_limit - get proper overload limit of socket receive queue
2256 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002257 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01002258 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002259 * For connection oriented messages, irrespective of importance,
2260 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01002261 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002262 * For connectionless messages, queue limits are based on message
2263 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01002264 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002265 * TIPC_LOW_IMPORTANCE (2 MB)
2266 * TIPC_MEDIUM_IMPORTANCE (4 MB)
2267 * TIPC_HIGH_IMPORTANCE (8 MB)
2268 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01002269 *
2270 * Returns overload limit according to corresponding message importance
2271 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002272static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01002273{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002274 struct tipc_sock *tsk = tipc_sk(sk);
2275 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01002276
Jon Maloyb7d42632017-10-13 11:04:26 +02002277 if (unlikely(msg_in_group(hdr)))
Eric Dumazet82657922019-10-09 15:21:13 -07002278 return READ_ONCE(sk->sk_rcvbuf);
Jon Maloyb7d42632017-10-13 11:04:26 +02002279
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002280 if (unlikely(!msg_connected(hdr)))
Eric Dumazet82657922019-10-09 15:21:13 -07002281 return READ_ONCE(sk->sk_rcvbuf) << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08002282
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002283 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
Eric Dumazet82657922019-10-09 15:21:13 -07002284 return READ_ONCE(sk->sk_rcvbuf);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04002285
2286 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01002287}
2288
2289/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02002290 * tipc_sk_filter_rcv - validate incoming message
Allan Stephens0c3141e2008-04-15 00:22:02 -07002291 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04002292 * @skb: pointer to message.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002293 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07002294 * Enqueues message on receive queue if acceptable; optionally handles
2295 * disconnect indication for a connected socket.
2296 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05002297 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002298 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002299 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02002300static void tipc_sk_filter_rcv(struct sock *sk, struct sk_buff *skb,
2301 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002302{
Jon Maloy64ac5f52017-10-13 11:04:20 +02002303 bool sk_conn = !tipc_sk_type_connectionless(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002304 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy75da2162017-10-13 11:04:23 +02002305 struct tipc_group *grp = tsk->group;
Jon Paul Maloycda36962015-07-22 10:11:20 -04002306 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002307 struct net *net = sock_net(sk);
2308 struct sk_buff_head inputq;
Hoang Le77d5ad42019-03-21 17:25:17 +07002309 int mtyp = msg_type(hdr);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002310 int limit, err = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002311
Tuong Lien01e661e2018-12-19 09:17:58 +07002312 trace_tipc_sk_filter_rcv(sk, skb, TIPC_DUMP_ALL, " ");
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01002313 TIPC_SKB_CB(skb)->bytes_read = 0;
Jon Maloy64ac5f52017-10-13 11:04:20 +02002314 __skb_queue_head_init(&inputq);
2315 __skb_queue_tail(&inputq, skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002316
Jon Maloy64ac5f52017-10-13 11:04:20 +02002317 if (unlikely(!msg_isdata(hdr)))
2318 tipc_sk_proto_rcv(sk, &inputq, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04002319
Jon Maloy75da2162017-10-13 11:04:23 +02002320 if (unlikely(grp))
2321 tipc_group_filter_msg(grp, &inputq, xmitq);
2322
Hoang Le77d5ad42019-03-21 17:25:17 +07002323 if (unlikely(!grp) && mtyp == TIPC_MCAST_MSG)
Hoang Le08e046c2019-03-21 17:25:18 +07002324 tipc_mcast_filter_msg(net, &tsk->mc_method.deferredq, &inputq);
Hoang Lec55c8ed2019-03-19 18:49:50 +07002325
Jon Maloy64ac5f52017-10-13 11:04:20 +02002326 /* Validate and add to receive buffer if there is space */
2327 while ((skb = __skb_dequeue(&inputq))) {
2328 hdr = buf_msg(skb);
2329 limit = rcvbuf_limit(sk, skb);
Tuong Lienc7268582020-05-13 19:33:16 +07002330 if ((sk_conn && !tipc_sk_filter_connect(tsk, skb, xmitq)) ||
Jon Maloy75da2162017-10-13 11:04:23 +02002331 (!sk_conn && msg_connected(hdr)) ||
2332 (!grp && msg_in_group(hdr)))
Jon Maloy64ac5f52017-10-13 11:04:20 +02002333 err = TIPC_ERR_NO_PORT;
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01002334 else if (sk_rmem_alloc_get(sk) + skb->truesize >= limit) {
Tuong Lien01e661e2018-12-19 09:17:58 +07002335 trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL,
2336 "err_overload2!");
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01002337 atomic_inc(&sk->sk_drops);
Jon Maloy64ac5f52017-10-13 11:04:20 +02002338 err = TIPC_ERR_OVERLOAD;
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01002339 }
Jon Maloy64ac5f52017-10-13 11:04:20 +02002340
2341 if (unlikely(err)) {
Tuong Lien01e661e2018-12-19 09:17:58 +07002342 if (tipc_msg_reverse(tipc_own_addr(net), &skb, err)) {
2343 trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE,
2344 "@filter_rcv!");
2345 __skb_queue_tail(xmitq, skb);
2346 }
Jon Maloy64ac5f52017-10-13 11:04:20 +02002347 err = TIPC_OK;
2348 continue;
2349 }
2350 __skb_queue_tail(&sk->sk_receive_queue, skb);
2351 skb_set_owner_r(skb, sk);
Tuong Lien01e661e2018-12-19 09:17:58 +07002352 trace_tipc_sk_overlimit2(sk, skb, TIPC_DUMP_ALL,
2353 "rcvq >90% allocated!");
Jon Maloy64ac5f52017-10-13 11:04:20 +02002354 sk->sk_data_ready(sk);
2355 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002356}
2357
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002358/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02002359 * tipc_sk_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07002360 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08002361 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07002362 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05002363 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07002364 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02002365static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07002366{
Jon Maloy64ac5f52017-10-13 11:04:20 +02002367 unsigned int before = sk_rmem_alloc_get(sk);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002368 struct sk_buff_head xmitq;
Jon Maloy64ac5f52017-10-13 11:04:20 +02002369 unsigned int added;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002370
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002371 __skb_queue_head_init(&xmitq);
2372
Jon Maloy64ac5f52017-10-13 11:04:20 +02002373 tipc_sk_filter_rcv(sk, skb, &xmitq);
2374 added = sk_rmem_alloc_get(sk) - before;
2375 atomic_add(added, &tipc_sk(sk)->dupl_rcvcnt);
2376
2377 /* Send pending response/rejected messages, if any */
Jon Maloyf70d37b2017-10-13 11:04:21 +02002378 tipc_node_distr_xmit(sock_net(sk), &xmitq);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002379 return 0;
2380}
2381
2382/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002383 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
2384 * inputq and try adding them to socket or backlog queue
2385 * @inputq: list of incoming buffers with potentially different destinations
2386 * @sk: socket where the buffers should be enqueued
2387 * @dport: port number for the socket
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002388 *
2389 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002390 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04002391static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002392 u32 dport, struct sk_buff_head *xmitq)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002393{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002394 unsigned long time_limit = jiffies + 2;
2395 struct sk_buff *skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002396 unsigned int lim;
2397 atomic_t *dcnt;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002398 u32 onode;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002399
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002400 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05002401 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04002402 return;
2403
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002404 skb = tipc_skb_dequeue(inputq, dport);
2405 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04002406 return;
2407
2408 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002409 if (!sock_owned_by_user(sk)) {
Jon Maloy64ac5f52017-10-13 11:04:20 +02002410 tipc_sk_filter_rcv(sk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04002411 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002412 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04002413
2414 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002415 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04002416 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002417 atomic_set(dcnt, 0);
2418 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
Tuong Lien01e661e2018-12-19 09:17:58 +07002419 if (likely(!sk_add_backlog(sk, skb, lim))) {
2420 trace_tipc_sk_overlimit1(sk, skb, TIPC_DUMP_ALL,
2421 "bklg & rcvq >90% allocated!");
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002422 continue;
Tuong Lien01e661e2018-12-19 09:17:58 +07002423 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04002424
Tuong Lien01e661e2018-12-19 09:17:58 +07002425 trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL, "err_overload!");
Jon Paul Maloycda36962015-07-22 10:11:20 -04002426 /* Overload => reject message back to sender */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002427 onode = tipc_own_addr(sock_net(sk));
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01002428 atomic_inc(&sk->sk_drops);
Tuong Lien01e661e2018-12-19 09:17:58 +07002429 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD)) {
2430 trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_ALL,
2431 "@sk_enqueue!");
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002432 __skb_queue_tail(xmitq, skb);
Tuong Lien01e661e2018-12-19 09:17:58 +07002433 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04002434 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002435 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05002436}
2437
2438/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002439 * tipc_sk_rcv - handle a chain of incoming buffers
2440 * @inputq: buffer list containing the buffers
2441 * Consumes all buffers in list until inputq is empty
2442 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07002443 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04002444void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07002445{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002446 struct sk_buff_head xmitq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002447 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04002448 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04002449 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04002450 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04002451 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04002452
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002453 __skb_queue_head_init(&xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002454 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002455 dport = tipc_skb_peek_port(inputq, dport);
2456 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04002457
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002458 if (likely(tsk)) {
2459 sk = &tsk->sk;
2460 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002461 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002462 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002463 }
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04002464 /* Send pending response/rejected messages, if any */
Jon Maloyf70d37b2017-10-13 11:04:21 +02002465 tipc_node_distr_xmit(sock_net(sk), &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002466 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002467 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002468 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04002469 /* No destination socket => dequeue skb if still there */
2470 skb = tipc_skb_dequeue(inputq, dport);
2471 if (!skb)
2472 return;
2473
2474 /* Try secondary lookup if unresolved named message */
2475 err = TIPC_ERR_NO_PORT;
2476 if (tipc_msg_lookup_dest(net, skb, &err))
2477 goto xmit;
2478
2479 /* Prepare for message rejection */
2480 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002481 continue;
Tuong Lien01e661e2018-12-19 09:17:58 +07002482
2483 trace_tipc_sk_rej_msg(NULL, skb, TIPC_DUMP_NONE, "@sk_rcv!");
Jon Paul Maloye3a77562015-02-05 08:36:39 -05002484xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04002485 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002486 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05002487 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07002488}
2489
Ying Xue78eb3a52014-01-17 09:50:03 +08002490static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
2491{
WANG Congd9dc8b02016-11-11 10:20:50 -08002492 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Ying Xue78eb3a52014-01-17 09:50:03 +08002493 struct sock *sk = sock->sk;
Ying Xue78eb3a52014-01-17 09:50:03 +08002494 int done;
2495
2496 do {
2497 int err = sock_error(sk);
2498 if (err)
2499 return err;
2500 if (!*timeo_p)
2501 return -ETIMEDOUT;
2502 if (signal_pending(current))
2503 return sock_intr_errno(*timeo_p);
Tuong Lien5391a872020-02-10 15:35:44 +07002504 if (sk->sk_state == TIPC_DISCONNECTING)
2505 break;
Ying Xue78eb3a52014-01-17 09:50:03 +08002506
WANG Congd9dc8b02016-11-11 10:20:50 -08002507 add_wait_queue(sk_sleep(sk), &wait);
Tuong Lien9546a0b2020-01-08 09:19:00 +07002508 done = sk_wait_event(sk, timeo_p, tipc_sk_connected(sk),
2509 &wait);
WANG Congd9dc8b02016-11-11 10:20:50 -08002510 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue78eb3a52014-01-17 09:50:03 +08002511 } while (!done);
2512 return 0;
2513}
2514
Erik Hugneea239312019-03-17 18:46:42 +01002515static bool tipc_sockaddr_is_sane(struct sockaddr_tipc *addr)
2516{
2517 if (addr->family != AF_TIPC)
2518 return false;
2519 if (addr->addrtype == TIPC_SERVICE_RANGE)
2520 return (addr->addr.nameseq.lower <= addr->addr.nameseq.upper);
2521 return (addr->addrtype == TIPC_SERVICE_ADDR ||
2522 addr->addrtype == TIPC_SOCKET_ADDR);
2523}
2524
Per Lidenb97bf3f2006-01-02 19:04:38 +01002525/**
Ying Xue247f0f32014-02-18 16:06:46 +08002526 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01002527 * @sock: socket structure
2528 * @dest: socket address for destination port
2529 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07002530 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01002531 *
2532 * Returns 0 on success, errno otherwise
2533 */
Ying Xue247f0f32014-02-18 16:06:46 +08002534static int tipc_connect(struct socket *sock, struct sockaddr *dest,
2535 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002536{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002537 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01002538 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07002539 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
2540 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01002541 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002542 int previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01002543 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002544
Jon Maloy23998832017-10-13 11:04:18 +02002545 if (destlen != sizeof(struct sockaddr_tipc))
2546 return -EINVAL;
2547
Allan Stephens0c3141e2008-04-15 00:22:02 -07002548 lock_sock(sk);
2549
Jon Maloy75da2162017-10-13 11:04:23 +02002550 if (tsk->group) {
2551 res = -EINVAL;
2552 goto exit;
2553 }
2554
Jon Maloy23998832017-10-13 11:04:18 +02002555 if (dst->family == AF_UNSPEC) {
2556 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
2557 if (!tipc_sk_type_connectionless(sk))
2558 res = -EINVAL;
2559 goto exit;
Jon Maloy23998832017-10-13 11:04:18 +02002560 }
Erik Hugneea239312019-03-17 18:46:42 +01002561 if (!tipc_sockaddr_is_sane(dst)) {
Jon Maloy23998832017-10-13 11:04:18 +02002562 res = -EINVAL;
Jon Maloy23998832017-10-13 11:04:18 +02002563 goto exit;
Erik Hugneea239312019-03-17 18:46:42 +01002564 }
Erik Hugnef2f80362015-03-19 09:02:19 +01002565 /* DGRAM/RDM connect(), just save the destaddr */
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +01002566 if (tipc_sk_type_connectionless(sk)) {
Jon Maloy23998832017-10-13 11:04:18 +02002567 memcpy(&tsk->peer, dest, destlen);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002568 goto exit;
Erik Hugneea239312019-03-17 18:46:42 +01002569 } else if (dst->addrtype == TIPC_SERVICE_RANGE) {
2570 res = -EINVAL;
2571 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002572 }
2573
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002574 previous = sk->sk_state;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01002575
2576 switch (sk->sk_state) {
2577 case TIPC_OPEN:
Ying Xue584d24b2012-11-29 18:51:19 -05002578 /* Send a 'SYN-' to destination */
2579 m.msg_name = dest;
2580 m.msg_namelen = destlen;
2581
2582 /* If connect is in non-blocking case, set MSG_DONTWAIT to
2583 * indicate send_msg() is never blocked.
2584 */
2585 if (!timeout)
2586 m.msg_flags = MSG_DONTWAIT;
2587
Ying Xue39a0295f2015-03-02 15:37:47 +08002588 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05002589 if ((res < 0) && (res != -EWOULDBLOCK))
2590 goto exit;
2591
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002592 /* Just entered TIPC_CONNECTING state; the only
Ying Xue584d24b2012-11-29 18:51:19 -05002593 * difference is that return value in non-blocking
2594 * case is EINPROGRESS, rather than EALREADY.
2595 */
2596 res = -EINPROGRESS;
Gustavo A. R. Silvaf79e3362019-01-23 01:09:31 -06002597 /* fall through */
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002598 case TIPC_CONNECTING:
2599 if (!timeout) {
2600 if (previous == TIPC_CONNECTING)
2601 res = -EALREADY;
Ying Xue78eb3a52014-01-17 09:50:03 +08002602 goto exit;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002603 }
Ying Xue78eb3a52014-01-17 09:50:03 +08002604 timeout = msecs_to_jiffies(timeout);
2605 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
2606 res = tipc_wait_for_connect(sock, &timeout);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002607 break;
2608 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002609 res = -EISCONN;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002610 break;
2611 default:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002612 res = -EINVAL;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002613 }
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002614
Allan Stephens0c3141e2008-04-15 00:22:02 -07002615exit:
2616 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07002617 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002618}
2619
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002620/**
Ying Xue247f0f32014-02-18 16:06:46 +08002621 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01002622 * @sock: socket structure
2623 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002624 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002625 * Returns 0 on success, errno otherwise
2626 */
Ying Xue247f0f32014-02-18 16:06:46 +08002627static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002628{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002629 struct sock *sk = sock->sk;
2630 int res;
2631
2632 lock_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002633 res = tipc_set_sk_state(sk, TIPC_LISTEN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002634 release_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002635
Allan Stephens0c3141e2008-04-15 00:22:02 -07002636 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002637}
2638
Ying Xue6398e232014-01-17 09:50:04 +08002639static int tipc_wait_for_accept(struct socket *sock, long timeo)
2640{
2641 struct sock *sk = sock->sk;
2642 DEFINE_WAIT(wait);
2643 int err;
2644
2645 /* True wake-one mechanism for incoming connections: only
2646 * one process gets woken up, not the 'whole herd'.
2647 * Since we do not 'race & poll' for established sockets
2648 * anymore, the common case will execute the loop only once.
2649 */
2650 for (;;) {
2651 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2652 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01002653 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08002654 release_sock(sk);
2655 timeo = schedule_timeout(timeo);
2656 lock_sock(sk);
2657 }
2658 err = 0;
2659 if (!skb_queue_empty(&sk->sk_receive_queue))
2660 break;
Ying Xue6398e232014-01-17 09:50:04 +08002661 err = -EAGAIN;
2662 if (!timeo)
2663 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002664 err = sock_intr_errno(timeo);
2665 if (signal_pending(current))
2666 break;
Ying Xue6398e232014-01-17 09:50:04 +08002667 }
2668 finish_wait(sk_sleep(sk), &wait);
2669 return err;
2670}
2671
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002672/**
Ying Xue247f0f32014-02-18 16:06:46 +08002673 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002674 * @sock: listening socket
2675 * @newsock: new socket that is to be connected
2676 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002677 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002678 * Returns 0 on success, errno otherwise
2679 */
David Howellscdfbabf2017-03-09 08:09:05 +00002680static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
2681 bool kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002682{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002683 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002684 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002685 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002686 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002687 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002688 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002689
Allan Stephens0c3141e2008-04-15 00:22:02 -07002690 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002691
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002692 if (sk->sk_state != TIPC_LISTEN) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002693 res = -EINVAL;
2694 goto exit;
2695 }
Ying Xue6398e232014-01-17 09:50:04 +08002696 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2697 res = tipc_wait_for_accept(sock, timeo);
2698 if (res)
2699 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002700
2701 buf = skb_peek(&sk->sk_receive_queue);
2702
David Howellscdfbabf2017-03-09 08:09:05 +00002703 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002704 if (res)
2705 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002706 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002707
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002708 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002709 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002710 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002711
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002712 /* we lock on new_sk; but lockdep sees the lock on sk */
2713 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002714
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002715 /*
2716 * Reject any stray messages received by new socket
2717 * before the socket lock was taken (very, very unlikely)
2718 */
Tuong Lien49afb802020-01-08 09:18:15 +07002719 tsk_rej_rx_queue(new_sk, TIPC_ERR_NO_PORT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002720
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002721 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002722 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002723
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002724 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002725 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002726 new_tsock->conn_type = msg_nametype(msg);
2727 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002728 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002729
2730 /*
2731 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2732 * Respond to 'SYN+' by queuing it on new socket.
2733 */
2734 if (!msg_data_sz(msg)) {
2735 struct msghdr m = {NULL,};
2736
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002737 tsk_advance_rx_queue(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002738 __tipc_sendstream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002739 } else {
2740 __skb_dequeue(&sk->sk_receive_queue);
2741 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002742 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002743 }
2744 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002745exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002746 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002747 return res;
2748}
2749
2750/**
Ying Xue247f0f32014-02-18 16:06:46 +08002751 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002752 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002753 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002754 *
2755 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002756 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002757 * Returns 0 on success, errno otherwise
2758 */
Ying Xue247f0f32014-02-18 16:06:46 +08002759static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002760{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002761 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002762 int res;
2763
Allan Stephense247a8f2008-03-06 15:05:38 -08002764 if (how != SHUT_RDWR)
2765 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002766
Allan Stephens0c3141e2008-04-15 00:22:02 -07002767 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002768
Tuong Lien01e661e2018-12-19 09:17:58 +07002769 trace_tipc_sk_shutdown(sk, NULL, TIPC_DUMP_ALL, " ");
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002770 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2771 sk->sk_shutdown = SEND_SHUTDOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002772
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002773 if (sk->sk_state == TIPC_DISCONNECTING) {
Ying Xue75031152012-10-29 09:38:15 -04002774 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002775 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002776
2777 /* Wake up anyone sleeping in poll */
2778 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002779 res = 0;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002780 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002781 res = -ENOTCONN;
2782 }
2783
Allan Stephens0c3141e2008-04-15 00:22:02 -07002784 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002785 return res;
2786}
2787
Jon Maloyafe87922018-09-28 20:23:19 +02002788static void tipc_sk_check_probing_state(struct sock *sk,
2789 struct sk_buff_head *list)
2790{
2791 struct tipc_sock *tsk = tipc_sk(sk);
2792 u32 pnode = tsk_peer_node(tsk);
2793 u32 pport = tsk_peer_port(tsk);
2794 u32 self = tsk_own_node(tsk);
2795 u32 oport = tsk->portid;
2796 struct sk_buff *skb;
2797
2798 if (tsk->probe_unacked) {
2799 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2800 sk->sk_err = ECONNABORTED;
2801 tipc_node_remove_conn(sock_net(sk), pnode, pport);
2802 sk->sk_state_change(sk);
2803 return;
2804 }
2805 /* Prepare new probe */
2806 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, 0,
2807 pnode, self, pport, oport, TIPC_OK);
2808 if (skb)
2809 __skb_queue_tail(list, skb);
2810 tsk->probe_unacked = true;
2811 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
2812}
2813
Tung Nguyen67879272018-09-28 20:23:22 +02002814static void tipc_sk_retry_connect(struct sock *sk, struct sk_buff_head *list)
2815{
2816 struct tipc_sock *tsk = tipc_sk(sk);
2817
2818 /* Try again later if dest link is congested */
2819 if (tsk->cong_link_cnt) {
2820 sk_reset_timer(sk, &sk->sk_timer, msecs_to_jiffies(100));
2821 return;
2822 }
2823 /* Prepare SYN for retransmit */
2824 tipc_msg_skb_clone(&sk->sk_write_queue, list);
2825}
2826
Kees Cook31b102b2017-10-30 14:06:45 -07002827static void tipc_sk_timeout(struct timer_list *t)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002828{
Kees Cook31b102b2017-10-30 14:06:45 -07002829 struct sock *sk = from_timer(sk, t, sk_timer);
2830 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloyafe87922018-09-28 20:23:19 +02002831 u32 pnode = tsk_peer_node(tsk);
2832 struct sk_buff_head list;
Tung Nguyen67879272018-09-28 20:23:22 +02002833 int rc = 0;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002834
Jon Maloye654f9f2019-08-15 16:42:50 +02002835 __skb_queue_head_init(&list);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002836 bh_lock_sock(sk);
Jon Maloy0d5fcebf2017-10-20 11:21:32 +02002837
2838 /* Try again later if socket is busy */
2839 if (sock_owned_by_user(sk)) {
2840 sk_reset_timer(sk, &sk->sk_timer, jiffies + HZ / 20);
Jon Maloyafe87922018-09-28 20:23:19 +02002841 bh_unlock_sock(sk);
Tung Nguyen91a4a3e2019-11-28 10:10:06 +07002842 sock_put(sk);
Jon Maloyafe87922018-09-28 20:23:19 +02002843 return;
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002844 }
Jon Paul Maloy57289012014-08-22 18:09:09 -04002845
Jon Maloyafe87922018-09-28 20:23:19 +02002846 if (sk->sk_state == TIPC_ESTABLISHED)
2847 tipc_sk_check_probing_state(sk, &list);
Tung Nguyen67879272018-09-28 20:23:22 +02002848 else if (sk->sk_state == TIPC_CONNECTING)
2849 tipc_sk_retry_connect(sk, &list);
Jon Maloyafe87922018-09-28 20:23:19 +02002850
Jon Paul Maloy57289012014-08-22 18:09:09 -04002851 bh_unlock_sock(sk);
Jon Maloyafe87922018-09-28 20:23:19 +02002852
2853 if (!skb_queue_empty(&list))
Tung Nguyen67879272018-09-28 20:23:22 +02002854 rc = tipc_node_xmit(sock_net(sk), &list, pnode, tsk->portid);
Jon Maloyafe87922018-09-28 20:23:19 +02002855
Tung Nguyen67879272018-09-28 20:23:22 +02002856 /* SYN messages may cause link congestion */
2857 if (rc == -ELINKCONG) {
2858 tipc_dest_push(&tsk->cong_links, pnode, 0);
2859 tsk->cong_link_cnt = 1;
2860 }
Ying Xue07f6c4b2015-01-07 13:41:58 +08002861 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002862}
2863
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002864static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002865 struct tipc_name_seq const *seq)
2866{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002867 struct sock *sk = &tsk->sk;
2868 struct net *net = sock_net(sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002869 struct publication *publ;
2870 u32 key;
2871
Jon Maloy928df182018-03-15 16:48:51 +01002872 if (scope != TIPC_NODE_SCOPE)
2873 scope = TIPC_CLUSTER_SCOPE;
2874
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002875 if (tipc_sk_connected(sk))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002876 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002877 key = tsk->portid + tsk->pub_count + 1;
2878 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002879 return -EADDRINUSE;
2880
Ying Xuef2f98002015-01-09 15:27:05 +08002881 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002882 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002883 if (unlikely(!publ))
2884 return -EINVAL;
2885
Jon Maloye50e73e2018-03-15 16:48:55 +01002886 list_add(&publ->binding_sock, &tsk->publications);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002887 tsk->pub_count++;
2888 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002889 return 0;
2890}
2891
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002892static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002893 struct tipc_name_seq const *seq)
2894{
Ying Xuef2f98002015-01-09 15:27:05 +08002895 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002896 struct publication *publ;
2897 struct publication *safe;
2898 int rc = -EINVAL;
2899
Jon Maloy928df182018-03-15 16:48:51 +01002900 if (scope != TIPC_NODE_SCOPE)
2901 scope = TIPC_CLUSTER_SCOPE;
2902
Jon Maloye50e73e2018-03-15 16:48:55 +01002903 list_for_each_entry_safe(publ, safe, &tsk->publications, binding_sock) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002904 if (seq) {
2905 if (publ->scope != scope)
2906 continue;
2907 if (publ->type != seq->type)
2908 continue;
2909 if (publ->lower != seq->lower)
2910 continue;
2911 if (publ->upper != seq->upper)
2912 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002913 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Maloy37922ea2018-03-29 23:20:43 +02002914 publ->upper, publ->key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002915 rc = 0;
2916 break;
2917 }
Ying Xuef2f98002015-01-09 15:27:05 +08002918 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Maloy37922ea2018-03-29 23:20:43 +02002919 publ->upper, publ->key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002920 rc = 0;
2921 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002922 if (list_empty(&tsk->publications))
2923 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002924 return rc;
2925}
2926
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002927/* tipc_sk_reinit: set non-zero address in all existing sockets
2928 * when we go from standalone to network mode.
2929 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002930void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002931{
Ying Xuee05b31f2015-01-09 15:27:08 +08002932 struct tipc_net *tn = net_generic(net, tipc_net_id);
Herbert Xu40f9f432017-02-11 19:26:46 +08002933 struct rhashtable_iter iter;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002934 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002935 struct tipc_msg *msg;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002936
Herbert Xu40f9f432017-02-11 19:26:46 +08002937 rhashtable_walk_enter(&tn->sk_rht, &iter);
2938
2939 do {
Tom Herbert97a6ec42017-12-04 10:31:41 -08002940 rhashtable_walk_start(&iter);
Herbert Xu40f9f432017-02-11 19:26:46 +08002941
2942 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
Cong Wang15ef70e2018-12-10 11:49:55 -08002943 sock_hold(&tsk->sk);
2944 rhashtable_walk_stop(&iter);
2945 lock_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002946 msg = &tsk->phdr;
Jon Maloy23fd3ea2018-03-22 20:42:49 +01002947 msg_set_prevnode(msg, tipc_own_addr(net));
2948 msg_set_orignode(msg, tipc_own_addr(net));
Cong Wang15ef70e2018-12-10 11:49:55 -08002949 release_sock(&tsk->sk);
2950 rhashtable_walk_start(&iter);
2951 sock_put(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002952 }
Tom Herbert97a6ec42017-12-04 10:31:41 -08002953
Herbert Xu40f9f432017-02-11 19:26:46 +08002954 rhashtable_walk_stop(&iter);
2955 } while (tsk == ERR_PTR(-EAGAIN));
Cong Wangbd583fe2018-08-23 16:19:44 -07002956
2957 rhashtable_walk_exit(&iter);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002958}
2959
Ying Xuee05b31f2015-01-09 15:27:08 +08002960static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002961{
Ying Xuee05b31f2015-01-09 15:27:08 +08002962 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002963 struct tipc_sock *tsk;
2964
2965 rcu_read_lock();
Taehee Yooab818362019-11-22 08:15:19 +00002966 tsk = rhashtable_lookup(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002967 if (tsk)
2968 sock_hold(&tsk->sk);
2969 rcu_read_unlock();
2970
2971 return tsk;
2972}
2973
2974static int tipc_sk_insert(struct tipc_sock *tsk)
2975{
Ying Xuee05b31f2015-01-09 15:27:08 +08002976 struct sock *sk = &tsk->sk;
2977 struct net *net = sock_net(sk);
2978 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002979 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2980 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2981
2982 while (remaining--) {
2983 portid++;
2984 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2985 portid = TIPC_MIN_PORT;
2986 tsk->portid = portid;
2987 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002988 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2989 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002990 return 0;
2991 sock_put(&tsk->sk);
2992 }
2993
2994 return -1;
2995}
2996
2997static void tipc_sk_remove(struct tipc_sock *tsk)
2998{
2999 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08003000 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08003001
Herbert Xu6cca72892015-03-20 21:57:05 +11003002 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Reshetova, Elena41c6d652017-06-30 13:08:01 +03003003 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
Ying Xue07f6c4b2015-01-07 13:41:58 +08003004 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04003005 }
3006}
3007
Herbert Xu6cca72892015-03-20 21:57:05 +11003008static const struct rhashtable_params tsk_rht_params = {
3009 .nelem_hint = 192,
3010 .head_offset = offsetof(struct tipc_sock, node),
3011 .key_offset = offsetof(struct tipc_sock, portid),
3012 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11003013 .max_size = 1048576,
3014 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00003015 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11003016};
3017
Ying Xuee05b31f2015-01-09 15:27:08 +08003018int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003019{
Ying Xuee05b31f2015-01-09 15:27:08 +08003020 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003021
Herbert Xu6cca72892015-03-20 21:57:05 +11003022 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003023}
3024
Ying Xuee05b31f2015-01-09 15:27:08 +08003025void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003026{
Ying Xuee05b31f2015-01-09 15:27:08 +08003027 struct tipc_net *tn = net_generic(net, tipc_net_id);
3028
Ying Xue07f6c4b2015-01-07 13:41:58 +08003029 /* Wait for socket readers to complete */
3030 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003031
Ying Xuee05b31f2015-01-09 15:27:08 +08003032 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003033}
3034
Jon Maloy75da2162017-10-13 11:04:23 +02003035static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq)
3036{
3037 struct net *net = sock_net(&tsk->sk);
Jon Maloy75da2162017-10-13 11:04:23 +02003038 struct tipc_group *grp = tsk->group;
3039 struct tipc_msg *hdr = &tsk->phdr;
3040 struct tipc_name_seq seq;
3041 int rc;
3042
3043 if (mreq->type < TIPC_RESERVED_TYPES)
3044 return -EACCES;
Jon Maloy232d07b2018-01-08 21:03:30 +01003045 if (mreq->scope > TIPC_NODE_SCOPE)
3046 return -EINVAL;
Jon Maloy75da2162017-10-13 11:04:23 +02003047 if (grp)
3048 return -EACCES;
Jon Maloy60c25302018-01-17 16:42:46 +01003049 grp = tipc_group_create(net, tsk->portid, mreq, &tsk->group_is_open);
Jon Maloy75da2162017-10-13 11:04:23 +02003050 if (!grp)
3051 return -ENOMEM;
3052 tsk->group = grp;
3053 msg_set_lookup_scope(hdr, mreq->scope);
3054 msg_set_nametype(hdr, mreq->type);
3055 msg_set_dest_droppable(hdr, true);
3056 seq.type = mreq->type;
3057 seq.lower = mreq->instance;
3058 seq.upper = seq.lower;
Jon Maloy232d07b2018-01-08 21:03:30 +01003059 tipc_nametbl_build_group(net, grp, mreq->type, mreq->scope);
Jon Maloy75da2162017-10-13 11:04:23 +02003060 rc = tipc_sk_publish(tsk, mreq->scope, &seq);
Cong Wange233df02017-10-24 15:44:49 -07003061 if (rc) {
Jon Maloy75da2162017-10-13 11:04:23 +02003062 tipc_group_delete(net, grp);
Cong Wange233df02017-10-24 15:44:49 -07003063 tsk->group = NULL;
Jon Maloyfebafc82018-01-10 21:08:50 +01003064 return rc;
Cong Wange233df02017-10-24 15:44:49 -07003065 }
Jon Maloyd12d2e12018-01-08 21:03:28 +01003066 /* Eliminate any risk that a broadcast overtakes sent JOINs */
Jon Maloy399574d2017-10-13 11:04:32 +02003067 tsk->mc_method.rcast = true;
3068 tsk->mc_method.mandatory = true;
Jon Maloyd12d2e12018-01-08 21:03:28 +01003069 tipc_group_join(net, grp, &tsk->sk.sk_rcvbuf);
Jon Maloy75da2162017-10-13 11:04:23 +02003070 return rc;
3071}
3072
3073static int tipc_sk_leave(struct tipc_sock *tsk)
3074{
3075 struct net *net = sock_net(&tsk->sk);
3076 struct tipc_group *grp = tsk->group;
3077 struct tipc_name_seq seq;
3078 int scope;
3079
3080 if (!grp)
3081 return -EINVAL;
3082 tipc_group_self(grp, &seq, &scope);
3083 tipc_group_delete(net, grp);
3084 tsk->group = NULL;
3085 tipc_sk_withdraw(tsk, scope, &seq);
3086 return 0;
3087}
3088
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04003089/**
Ying Xue247f0f32014-02-18 16:06:46 +08003090 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01003091 * @sock: socket structure
3092 * @lvl: option level
3093 * @opt: option identifier
3094 * @ov: pointer to new option value
3095 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003096 *
3097 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01003098 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003099 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01003100 * Returns 0 on success, errno otherwise
3101 */
Ying Xue247f0f32014-02-18 16:06:46 +08003102static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
3103 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003104{
Allan Stephens0c3141e2008-04-15 00:22:02 -07003105 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04003106 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy75da2162017-10-13 11:04:23 +02003107 struct tipc_group_req mreq;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003108 u32 value = 0;
Dan Carpentera08ef472017-01-24 12:49:35 +03003109 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003110
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003111 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
3112 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003113 if (lvl != SOL_TIPC)
3114 return -ENOPROTOOPT;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003115
3116 switch (opt) {
3117 case TIPC_IMPORTANCE:
3118 case TIPC_SRC_DROPPABLE:
3119 case TIPC_DEST_DROPPABLE:
3120 case TIPC_CONN_TIMEOUT:
Jon Maloyc0bceb92019-10-30 14:00:41 +01003121 case TIPC_NODELAY:
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003122 if (ol < sizeof(value))
3123 return -EINVAL;
Jon Maloy75da2162017-10-13 11:04:23 +02003124 if (get_user(value, (u32 __user *)ov))
3125 return -EFAULT;
3126 break;
3127 case TIPC_GROUP_JOIN:
3128 if (ol < sizeof(mreq))
3129 return -EINVAL;
3130 if (copy_from_user(&mreq, ov, sizeof(mreq)))
3131 return -EFAULT;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003132 break;
3133 default:
3134 if (ov || ol)
3135 return -EINVAL;
3136 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01003137
Allan Stephens0c3141e2008-04-15 00:22:02 -07003138 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003139
Per Lidenb97bf3f2006-01-02 19:04:38 +01003140 switch (opt) {
3141 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003142 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003143 break;
3144 case TIPC_SRC_DROPPABLE:
3145 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003146 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003147 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01003148 res = -ENOPROTOOPT;
3149 break;
3150 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003151 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003152 break;
3153 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04003154 tipc_sk(sk)->conn_timeout = value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003155 break;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05003156 case TIPC_MCAST_BROADCAST:
3157 tsk->mc_method.rcast = false;
3158 tsk->mc_method.mandatory = true;
3159 break;
3160 case TIPC_MCAST_REPLICAST:
3161 tsk->mc_method.rcast = true;
3162 tsk->mc_method.mandatory = true;
3163 break;
Jon Maloy75da2162017-10-13 11:04:23 +02003164 case TIPC_GROUP_JOIN:
3165 res = tipc_sk_join(tsk, &mreq);
3166 break;
3167 case TIPC_GROUP_LEAVE:
3168 res = tipc_sk_leave(tsk);
3169 break;
Jon Maloyc0bceb92019-10-30 14:00:41 +01003170 case TIPC_NODELAY:
3171 tsk->nodelay = !!value;
3172 tsk_set_nagle(tsk);
3173 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003174 default:
3175 res = -EINVAL;
3176 }
3177
Allan Stephens0c3141e2008-04-15 00:22:02 -07003178 release_sock(sk);
3179
Per Lidenb97bf3f2006-01-02 19:04:38 +01003180 return res;
3181}
3182
3183/**
Ying Xue247f0f32014-02-18 16:06:46 +08003184 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01003185 * @sock: socket structure
3186 * @lvl: option level
3187 * @opt: option identifier
3188 * @ov: receptacle for option value
3189 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003190 *
3191 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01003192 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003193 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01003194 * Returns 0 on success, errno otherwise
3195 */
Ying Xue247f0f32014-02-18 16:06:46 +08003196static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
3197 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003198{
Allan Stephens0c3141e2008-04-15 00:22:02 -07003199 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04003200 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy75da2162017-10-13 11:04:23 +02003201 struct tipc_name_seq seq;
3202 int len, scope;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003203 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003204 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003205
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003206 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
3207 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003208 if (lvl != SOL_TIPC)
3209 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00003210 res = get_user(len, ol);
3211 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003212 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003213
Allan Stephens0c3141e2008-04-15 00:22:02 -07003214 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003215
3216 switch (opt) {
3217 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003218 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003219 break;
3220 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003221 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003222 break;
3223 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003224 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003225 break;
3226 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04003227 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07003228 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01003229 break;
Allan Stephens0e659672010-12-31 18:59:32 +00003230 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05003231 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00003232 break;
Allan Stephens0e659672010-12-31 18:59:32 +00003233 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00003234 value = skb_queue_len(&sk->sk_receive_queue);
3235 break;
Tung Nguyen42e54252019-04-18 21:02:19 +07003236 case TIPC_SOCK_RECVQ_USED:
3237 value = sk_rmem_alloc_get(sk);
3238 break;
Jon Maloy75da2162017-10-13 11:04:23 +02003239 case TIPC_GROUP_JOIN:
3240 seq.type = 0;
3241 if (tsk->group)
3242 tipc_group_self(tsk->group, &seq, &scope);
3243 value = seq.type;
3244 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01003245 default:
3246 res = -EINVAL;
3247 }
3248
Allan Stephens0c3141e2008-04-15 00:22:02 -07003249 release_sock(sk);
3250
Paul Gortmaker25860c32010-12-31 18:59:31 +00003251 if (res)
3252 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01003253
Paul Gortmaker25860c32010-12-31 18:59:31 +00003254 if (len < sizeof(value))
3255 return -EINVAL;
3256
3257 if (copy_to_user(ov, &value, sizeof(value)))
3258 return -EFAULT;
3259
3260 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003261}
3262
Ying Xuef2f98002015-01-09 15:27:05 +08003263static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02003264{
Jon Maloy3e5cf362018-04-25 19:29:36 +02003265 struct net *net = sock_net(sock->sk);
3266 struct tipc_sioc_nodeid_req nr = {0};
Erik Hugne78acb1f2014-04-24 16:26:47 +02003267 struct tipc_sioc_ln_req lnr;
3268 void __user *argp = (void __user *)arg;
3269
3270 switch (cmd) {
3271 case SIOCGETLINKNAME:
3272 if (copy_from_user(&lnr, argp, sizeof(lnr)))
3273 return -EFAULT;
Jon Maloy3e5cf362018-04-25 19:29:36 +02003274 if (!tipc_node_get_linkname(net,
Ying Xuef2f98002015-01-09 15:27:05 +08003275 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02003276 lnr.linkname, TIPC_MAX_LINK_NAME)) {
3277 if (copy_to_user(argp, &lnr, sizeof(lnr)))
3278 return -EFAULT;
3279 return 0;
3280 }
3281 return -EADDRNOTAVAIL;
Jon Maloy3e5cf362018-04-25 19:29:36 +02003282 case SIOCGETNODEID:
3283 if (copy_from_user(&nr, argp, sizeof(nr)))
3284 return -EFAULT;
3285 if (!tipc_node_get_id(net, nr.peer, nr.node_id))
3286 return -EADDRNOTAVAIL;
3287 if (copy_to_user(argp, &nr, sizeof(nr)))
3288 return -EFAULT;
3289 return 0;
Erik Hugne78acb1f2014-04-24 16:26:47 +02003290 default:
3291 return -ENOIOCTLCMD;
3292 }
3293}
3294
Erik Hugne70b03752017-03-29 11:22:16 +02003295static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
3296{
3297 struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
3298 struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
Erik Hugne66bc1e82017-03-29 11:22:17 +02003299 u32 onode = tipc_own_addr(sock_net(sock1->sk));
Erik Hugne70b03752017-03-29 11:22:16 +02003300
Erik Hugne66bc1e82017-03-29 11:22:17 +02003301 tsk1->peer.family = AF_TIPC;
3302 tsk1->peer.addrtype = TIPC_ADDR_ID;
3303 tsk1->peer.scope = TIPC_NODE_SCOPE;
3304 tsk1->peer.addr.id.ref = tsk2->portid;
3305 tsk1->peer.addr.id.node = onode;
3306 tsk2->peer.family = AF_TIPC;
3307 tsk2->peer.addrtype = TIPC_ADDR_ID;
3308 tsk2->peer.scope = TIPC_NODE_SCOPE;
3309 tsk2->peer.addr.id.ref = tsk1->portid;
3310 tsk2->peer.addr.id.node = onode;
3311
3312 tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
3313 tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
Erik Hugne70b03752017-03-29 11:22:16 +02003314 return 0;
3315}
3316
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00003317/* Protocol switches for the various types of TIPC sockets */
3318
Florian Westphalbca65ea2008-02-07 18:18:01 -08003319static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00003320 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003321 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08003322 .release = tipc_release,
3323 .bind = tipc_bind,
3324 .connect = tipc_connect,
Erik Hugne66bc1e82017-03-29 11:22:17 +02003325 .socketpair = tipc_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04003326 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08003327 .getname = tipc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003328 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02003329 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04003330 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08003331 .shutdown = tipc_shutdown,
3332 .setsockopt = tipc_setsockopt,
3333 .getsockopt = tipc_getsockopt,
3334 .sendmsg = tipc_sendmsg,
3335 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09003336 .mmap = sock_no_mmap,
3337 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01003338};
3339
Florian Westphalbca65ea2008-02-07 18:18:01 -08003340static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00003341 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003342 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08003343 .release = tipc_release,
3344 .bind = tipc_bind,
3345 .connect = tipc_connect,
Erik Hugne70b03752017-03-29 11:22:16 +02003346 .socketpair = tipc_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08003347 .accept = tipc_accept,
3348 .getname = tipc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003349 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02003350 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08003351 .listen = tipc_listen,
3352 .shutdown = tipc_shutdown,
3353 .setsockopt = tipc_setsockopt,
3354 .getsockopt = tipc_getsockopt,
3355 .sendmsg = tipc_send_packet,
3356 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09003357 .mmap = sock_no_mmap,
3358 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01003359};
3360
Florian Westphalbca65ea2008-02-07 18:18:01 -08003361static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00003362 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003363 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08003364 .release = tipc_release,
3365 .bind = tipc_bind,
3366 .connect = tipc_connect,
Erik Hugne70b03752017-03-29 11:22:16 +02003367 .socketpair = tipc_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08003368 .accept = tipc_accept,
3369 .getname = tipc_getname,
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07003370 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02003371 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08003372 .listen = tipc_listen,
3373 .shutdown = tipc_shutdown,
3374 .setsockopt = tipc_setsockopt,
3375 .getsockopt = tipc_getsockopt,
Jon Paul Maloy365ad352017-01-03 10:55:11 -05003376 .sendmsg = tipc_sendstream,
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02003377 .recvmsg = tipc_recvstream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09003378 .mmap = sock_no_mmap,
3379 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01003380};
3381
Florian Westphalbca65ea2008-02-07 18:18:01 -08003382static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00003383 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01003384 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04003385 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01003386};
3387
3388static struct proto tipc_proto = {
3389 .name = "TIPC",
3390 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04003391 .obj_size = sizeof(struct tipc_sock),
3392 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01003393};
3394
3395/**
Per Liden4323add2006-01-18 00:38:21 +01003396 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003397 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01003398 * Returns 0 on success, errno otherwise
3399 */
Per Liden4323add2006-01-18 00:38:21 +01003400int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003401{
3402 int res;
3403
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003404 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01003405 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04003406 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01003407 goto out;
3408 }
3409
3410 res = sock_register(&tipc_family_ops);
3411 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04003412 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01003413 proto_unregister(&tipc_proto);
3414 goto out;
3415 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01003416 out:
3417 return res;
3418}
3419
3420/**
Per Liden4323add2006-01-18 00:38:21 +01003421 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01003422 */
Per Liden4323add2006-01-18 00:38:21 +01003423void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01003424{
Per Lidenb97bf3f2006-01-02 19:04:38 +01003425 sock_unregister(tipc_family_ops.family);
3426 proto_unregister(&tipc_proto);
3427}
Richard Alpe34b78a122014-11-20 10:29:10 +01003428
3429/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01003430static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01003431{
3432 u32 peer_node;
3433 u32 peer_port;
3434 struct nlattr *nest;
3435
3436 peer_node = tsk_peer_node(tsk);
3437 peer_port = tsk_peer_port(tsk);
3438
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003439 nest = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_CON);
Kangjie Lu517ccc22019-03-16 16:46:05 -05003440 if (!nest)
3441 return -EMSGSIZE;
Richard Alpe34b78a122014-11-20 10:29:10 +01003442
3443 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
3444 goto msg_full;
3445 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
3446 goto msg_full;
3447
3448 if (tsk->conn_type != 0) {
3449 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
3450 goto msg_full;
3451 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
3452 goto msg_full;
3453 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
3454 goto msg_full;
3455 }
3456 nla_nest_end(skb, nest);
3457
3458 return 0;
3459
3460msg_full:
3461 nla_nest_cancel(skb, nest);
3462
3463 return -EMSGSIZE;
3464}
3465
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003466static int __tipc_nl_add_sk_info(struct sk_buff *skb, struct tipc_sock
3467 *tsk)
3468{
3469 struct net *net = sock_net(skb->sk);
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003470 struct sock *sk = &tsk->sk;
3471
3472 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid) ||
Jon Maloy23fd3ea2018-03-22 20:42:49 +01003473 nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tipc_own_addr(net)))
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003474 return -EMSGSIZE;
3475
3476 if (tipc_sk_connected(sk)) {
3477 if (__tipc_nl_add_sk_con(skb, tsk))
3478 return -EMSGSIZE;
3479 } else if (!list_empty(&tsk->publications)) {
3480 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
3481 return -EMSGSIZE;
3482 }
3483 return 0;
3484}
3485
Richard Alpe34b78a122014-11-20 10:29:10 +01003486/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01003487static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
3488 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01003489{
Richard Alpe34b78a122014-11-20 10:29:10 +01003490 struct nlattr *attrs;
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003491 void *hdr;
Richard Alpe34b78a122014-11-20 10:29:10 +01003492
3493 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01003494 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01003495 if (!hdr)
3496 goto msg_cancel;
3497
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003498 attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK);
Richard Alpe34b78a122014-11-20 10:29:10 +01003499 if (!attrs)
3500 goto genlmsg_cancel;
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003501
3502 if (__tipc_nl_add_sk_info(skb, tsk))
Richard Alpe34b78a122014-11-20 10:29:10 +01003503 goto attr_msg_cancel;
3504
Richard Alpe34b78a122014-11-20 10:29:10 +01003505 nla_nest_end(skb, attrs);
3506 genlmsg_end(skb, hdr);
3507
3508 return 0;
3509
3510attr_msg_cancel:
3511 nla_nest_cancel(skb, attrs);
3512genlmsg_cancel:
3513 genlmsg_cancel(skb, hdr);
3514msg_cancel:
3515 return -EMSGSIZE;
3516}
3517
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003518int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,
3519 int (*skb_handler)(struct sk_buff *skb,
3520 struct netlink_callback *cb,
3521 struct tipc_sock *tsk))
Richard Alpe34b78a122014-11-20 10:29:10 +01003522{
Cong Wang8f5c5fc2018-09-04 14:54:55 -07003523 struct rhashtable_iter *iter = (void *)cb->args[4];
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003524 struct tipc_sock *tsk;
3525 int err;
Richard Alpe34b78a122014-11-20 10:29:10 +01003526
Cong Wang9a07efa2018-08-24 12:28:06 -07003527 rhashtable_walk_start(iter);
3528 while ((tsk = rhashtable_walk_next(iter)) != NULL) {
3529 if (IS_ERR(tsk)) {
3530 err = PTR_ERR(tsk);
3531 if (err == -EAGAIN) {
3532 err = 0;
Richard Alped6e164e2015-01-16 12:30:40 +01003533 continue;
3534 }
Cong Wang9a07efa2018-08-24 12:28:06 -07003535 break;
Ying Xue07f6c4b2015-01-07 13:41:58 +08003536 }
Richard Alpe34b78a122014-11-20 10:29:10 +01003537
Cong Wang9a07efa2018-08-24 12:28:06 -07003538 sock_hold(&tsk->sk);
3539 rhashtable_walk_stop(iter);
3540 lock_sock(&tsk->sk);
3541 err = skb_handler(skb, cb, tsk);
3542 if (err) {
3543 release_sock(&tsk->sk);
3544 sock_put(&tsk->sk);
3545 goto out;
3546 }
3547 release_sock(&tsk->sk);
3548 rhashtable_walk_start(iter);
3549 sock_put(&tsk->sk);
3550 }
3551 rhashtable_walk_stop(iter);
3552out:
Richard Alpe34b78a122014-11-20 10:29:10 +01003553 return skb->len;
3554}
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003555EXPORT_SYMBOL(tipc_nl_sk_walk);
3556
Cong Wang9a07efa2018-08-24 12:28:06 -07003557int tipc_dump_start(struct netlink_callback *cb)
3558{
Cong Wang8f5c5fc2018-09-04 14:54:55 -07003559 return __tipc_dump_start(cb, sock_net(cb->skb->sk));
3560}
3561EXPORT_SYMBOL(tipc_dump_start);
3562
3563int __tipc_dump_start(struct netlink_callback *cb, struct net *net)
3564{
3565 /* tipc_nl_name_table_dump() uses cb->args[0...3]. */
3566 struct rhashtable_iter *iter = (void *)cb->args[4];
Cong Wang9a07efa2018-08-24 12:28:06 -07003567 struct tipc_net *tn = tipc_net(net);
3568
3569 if (!iter) {
3570 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3571 if (!iter)
3572 return -ENOMEM;
3573
Cong Wang8f5c5fc2018-09-04 14:54:55 -07003574 cb->args[4] = (long)iter;
Cong Wang9a07efa2018-08-24 12:28:06 -07003575 }
3576
3577 rhashtable_walk_enter(&tn->sk_rht, iter);
3578 return 0;
3579}
Cong Wang9a07efa2018-08-24 12:28:06 -07003580
3581int tipc_dump_done(struct netlink_callback *cb)
3582{
Cong Wang8f5c5fc2018-09-04 14:54:55 -07003583 struct rhashtable_iter *hti = (void *)cb->args[4];
Cong Wang9a07efa2018-08-24 12:28:06 -07003584
3585 rhashtable_walk_exit(hti);
3586 kfree(hti);
3587 return 0;
3588}
3589EXPORT_SYMBOL(tipc_dump_done);
3590
Cong Wange41f0542018-04-06 18:54:52 -07003591int tipc_sk_fill_sock_diag(struct sk_buff *skb, struct netlink_callback *cb,
3592 struct tipc_sock *tsk, u32 sk_filter_state,
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003593 u64 (*tipc_diag_gen_cookie)(struct sock *sk))
3594{
3595 struct sock *sk = &tsk->sk;
3596 struct nlattr *attrs;
3597 struct nlattr *stat;
3598
3599 /*filter response w.r.t sk_state*/
3600 if (!(sk_filter_state & (1 << sk->sk_state)))
3601 return 0;
3602
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003603 attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK);
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003604 if (!attrs)
3605 goto msg_cancel;
3606
3607 if (__tipc_nl_add_sk_info(skb, tsk))
3608 goto attr_msg_cancel;
3609
3610 if (nla_put_u32(skb, TIPC_NLA_SOCK_TYPE, (u32)sk->sk_type) ||
3611 nla_put_u32(skb, TIPC_NLA_SOCK_TIPC_STATE, (u32)sk->sk_state) ||
3612 nla_put_u32(skb, TIPC_NLA_SOCK_INO, sock_i_ino(sk)) ||
3613 nla_put_u32(skb, TIPC_NLA_SOCK_UID,
Cong Wange41f0542018-04-06 18:54:52 -07003614 from_kuid_munged(sk_user_ns(NETLINK_CB(cb->skb).sk),
GhantaKrishnamurthy MohanKrishna4b2e6872018-04-04 14:49:47 +02003615 sock_i_uid(sk))) ||
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003616 nla_put_u64_64bit(skb, TIPC_NLA_SOCK_COOKIE,
3617 tipc_diag_gen_cookie(sk),
3618 TIPC_NLA_SOCK_PAD))
3619 goto attr_msg_cancel;
3620
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003621 stat = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_STAT);
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003622 if (!stat)
3623 goto attr_msg_cancel;
3624
3625 if (nla_put_u32(skb, TIPC_NLA_SOCK_STAT_RCVQ,
3626 skb_queue_len(&sk->sk_receive_queue)) ||
3627 nla_put_u32(skb, TIPC_NLA_SOCK_STAT_SENDQ,
GhantaKrishnamurthy MohanKrishna872619d2018-03-21 14:37:45 +01003628 skb_queue_len(&sk->sk_write_queue)) ||
3629 nla_put_u32(skb, TIPC_NLA_SOCK_STAT_DROP,
3630 atomic_read(&sk->sk_drops)))
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003631 goto stat_msg_cancel;
3632
3633 if (tsk->cong_link_cnt &&
3634 nla_put_flag(skb, TIPC_NLA_SOCK_STAT_LINK_CONG))
3635 goto stat_msg_cancel;
3636
3637 if (tsk_conn_cong(tsk) &&
3638 nla_put_flag(skb, TIPC_NLA_SOCK_STAT_CONN_CONG))
3639 goto stat_msg_cancel;
3640
3641 nla_nest_end(skb, stat);
GhantaKrishnamurthy MohanKrishnaa1be5a22018-06-29 13:26:18 +02003642
3643 if (tsk->group)
3644 if (tipc_group_fill_sock_diag(tsk->group, skb))
3645 goto stat_msg_cancel;
3646
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003647 nla_nest_end(skb, attrs);
3648
3649 return 0;
3650
3651stat_msg_cancel:
3652 nla_nest_cancel(skb, stat);
3653attr_msg_cancel:
3654 nla_nest_cancel(skb, attrs);
3655msg_cancel:
3656 return -EMSGSIZE;
3657}
3658EXPORT_SYMBOL(tipc_sk_fill_sock_diag);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003659
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003660int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
3661{
GhantaKrishnamurthy MohanKrishnac30b70d2018-03-21 14:37:44 +01003662 return tipc_nl_sk_walk(skb, cb, __tipc_nl_add_sk);
GhantaKrishnamurthy MohanKrishnadfde3312018-03-21 14:37:43 +01003663}
3664
Richard Alpe1a1a1432014-11-20 10:29:11 +01003665/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01003666static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
3667 struct netlink_callback *cb,
3668 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01003669{
3670 void *hdr;
3671 struct nlattr *attrs;
3672
3673 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01003674 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003675 if (!hdr)
3676 goto msg_cancel;
3677
Michal Kubecekae0be8d2019-04-26 11:13:06 +02003678 attrs = nla_nest_start_noflag(skb, TIPC_NLA_PUBL);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003679 if (!attrs)
3680 goto genlmsg_cancel;
3681
3682 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
3683 goto attr_msg_cancel;
3684 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
3685 goto attr_msg_cancel;
3686 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
3687 goto attr_msg_cancel;
3688 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
3689 goto attr_msg_cancel;
3690
3691 nla_nest_end(skb, attrs);
3692 genlmsg_end(skb, hdr);
3693
3694 return 0;
3695
3696attr_msg_cancel:
3697 nla_nest_cancel(skb, attrs);
3698genlmsg_cancel:
3699 genlmsg_cancel(skb, hdr);
3700msg_cancel:
3701 return -EMSGSIZE;
3702}
3703
3704/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01003705static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
3706 struct netlink_callback *cb,
3707 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01003708{
3709 int err;
3710 struct publication *p;
3711
3712 if (*last_publ) {
Jon Maloye50e73e2018-03-15 16:48:55 +01003713 list_for_each_entry(p, &tsk->publications, binding_sock) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01003714 if (p->key == *last_publ)
3715 break;
3716 }
3717 if (p->key != *last_publ) {
3718 /* We never set seq or call nl_dump_check_consistent()
3719 * this means that setting prev_seq here will cause the
3720 * consistence check to fail in the netlink callback
3721 * handler. Resulting in the last NLMSG_DONE message
3722 * having the NLM_F_DUMP_INTR flag set.
3723 */
3724 cb->prev_seq = 1;
3725 *last_publ = 0;
3726 return -EPIPE;
3727 }
3728 } else {
3729 p = list_first_entry(&tsk->publications, struct publication,
Jon Maloye50e73e2018-03-15 16:48:55 +01003730 binding_sock);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003731 }
3732
Jon Maloye50e73e2018-03-15 16:48:55 +01003733 list_for_each_entry_from(p, &tsk->publications, binding_sock) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01003734 err = __tipc_nl_add_sk_publ(skb, cb, p);
3735 if (err) {
3736 *last_publ = p->key;
3737 return err;
3738 }
3739 }
3740 *last_publ = 0;
3741
3742 return 0;
3743}
3744
3745int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
3746{
3747 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08003748 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01003749 u32 last_publ = cb->args[1];
3750 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08003751 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003752 struct tipc_sock *tsk;
3753
Ying Xue07f6c4b2015-01-07 13:41:58 +08003754 if (!tsk_portid) {
Jiri Pirko057af702019-10-05 20:04:39 +02003755 struct nlattr **attrs = genl_dumpit_info(cb)->attrs;
Richard Alpe1a1a1432014-11-20 10:29:11 +01003756 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
3757
Richard Alpe45e093a2016-05-16 11:14:54 +02003758 if (!attrs[TIPC_NLA_SOCK])
3759 return -EINVAL;
3760
Johannes Berg8cb08172019-04-26 14:07:28 +02003761 err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
3762 attrs[TIPC_NLA_SOCK],
3763 tipc_nl_sock_policy, NULL);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003764 if (err)
3765 return err;
3766
3767 if (!sock[TIPC_NLA_SOCK_REF])
3768 return -EINVAL;
3769
Ying Xue07f6c4b2015-01-07 13:41:58 +08003770 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003771 }
3772
3773 if (done)
3774 return 0;
3775
Ying Xuee05b31f2015-01-09 15:27:08 +08003776 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003777 if (!tsk)
3778 return -EINVAL;
3779
3780 lock_sock(&tsk->sk);
3781 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
3782 if (!err)
3783 done = 1;
3784 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08003785 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003786
Ying Xue07f6c4b2015-01-07 13:41:58 +08003787 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01003788 cb->args[1] = last_publ;
3789 cb->args[2] = done;
3790
3791 return skb->len;
3792}
Tuong Lienb4b97712018-12-19 09:17:56 +07003793
Tuong Lien01e661e2018-12-19 09:17:58 +07003794/**
3795 * tipc_sk_filtering - check if a socket should be traced
3796 * @sk: the socket to be examined
3797 * @sysctl_tipc_sk_filter[]: the socket tuple for filtering,
3798 * (portid, sock type, name type, name lower, name upper)
3799 *
3800 * Returns true if the socket meets the socket tuple data
3801 * (value 0 = 'any') or when there is no tuple set (all = 0),
3802 * otherwise false
3803 */
3804bool tipc_sk_filtering(struct sock *sk)
3805{
3806 struct tipc_sock *tsk;
3807 struct publication *p;
3808 u32 _port, _sktype, _type, _lower, _upper;
3809 u32 type = 0, lower = 0, upper = 0;
3810
3811 if (!sk)
3812 return true;
3813
3814 tsk = tipc_sk(sk);
3815
3816 _port = sysctl_tipc_sk_filter[0];
3817 _sktype = sysctl_tipc_sk_filter[1];
3818 _type = sysctl_tipc_sk_filter[2];
3819 _lower = sysctl_tipc_sk_filter[3];
3820 _upper = sysctl_tipc_sk_filter[4];
3821
3822 if (!_port && !_sktype && !_type && !_lower && !_upper)
3823 return true;
3824
3825 if (_port)
3826 return (_port == tsk->portid);
3827
3828 if (_sktype && _sktype != sk->sk_type)
3829 return false;
3830
3831 if (tsk->published) {
3832 p = list_first_entry_or_null(&tsk->publications,
3833 struct publication, binding_sock);
3834 if (p) {
3835 type = p->type;
3836 lower = p->lower;
3837 upper = p->upper;
3838 }
3839 }
3840
3841 if (!tipc_sk_type_connectionless(sk)) {
3842 type = tsk->conn_type;
3843 lower = tsk->conn_instance;
3844 upper = tsk->conn_instance;
3845 }
3846
3847 if ((_type && _type != type) || (_lower && _lower != lower) ||
3848 (_upper && _upper != upper))
3849 return false;
3850
3851 return true;
3852}
3853
Tuong Lienb4b97712018-12-19 09:17:56 +07003854u32 tipc_sock_get_portid(struct sock *sk)
3855{
3856 return (sk) ? (tipc_sk(sk))->portid : 0;
3857}
3858
3859/**
Tuong Lien01e661e2018-12-19 09:17:58 +07003860 * tipc_sk_overlimit1 - check if socket rx queue is about to be overloaded,
3861 * both the rcv and backlog queues are considered
3862 * @sk: tipc sk to be checked
3863 * @skb: tipc msg to be checked
3864 *
3865 * Returns true if the socket rx queue allocation is > 90%, otherwise false
3866 */
3867
3868bool tipc_sk_overlimit1(struct sock *sk, struct sk_buff *skb)
3869{
3870 atomic_t *dcnt = &tipc_sk(sk)->dupl_rcvcnt;
3871 unsigned int lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
3872 unsigned int qsize = sk->sk_backlog.len + sk_rmem_alloc_get(sk);
3873
3874 return (qsize > lim * 90 / 100);
3875}
3876
3877/**
3878 * tipc_sk_overlimit2 - check if socket rx queue is about to be overloaded,
3879 * only the rcv queue is considered
3880 * @sk: tipc sk to be checked
3881 * @skb: tipc msg to be checked
3882 *
3883 * Returns true if the socket rx queue allocation is > 90%, otherwise false
3884 */
3885
3886bool tipc_sk_overlimit2(struct sock *sk, struct sk_buff *skb)
3887{
3888 unsigned int lim = rcvbuf_limit(sk, skb);
3889 unsigned int qsize = sk_rmem_alloc_get(sk);
3890
3891 return (qsize > lim * 90 / 100);
3892}
3893
3894/**
Tuong Lienb4b97712018-12-19 09:17:56 +07003895 * tipc_sk_dump - dump TIPC socket
3896 * @sk: tipc sk to be dumped
3897 * @dqueues: bitmask to decide if any socket queue to be dumped?
3898 * - TIPC_DUMP_NONE: don't dump socket queues
3899 * - TIPC_DUMP_SK_SNDQ: dump socket send queue
3900 * - TIPC_DUMP_SK_RCVQ: dump socket rcv queue
3901 * - TIPC_DUMP_SK_BKLGQ: dump socket backlog queue
3902 * - TIPC_DUMP_ALL: dump all the socket queues above
3903 * @buf: returned buffer of dump data in format
3904 */
3905int tipc_sk_dump(struct sock *sk, u16 dqueues, char *buf)
3906{
3907 int i = 0;
3908 size_t sz = (dqueues) ? SK_LMAX : SK_LMIN;
3909 struct tipc_sock *tsk;
3910 struct publication *p;
3911 bool tsk_connected;
3912
3913 if (!sk) {
3914 i += scnprintf(buf, sz, "sk data: (null)\n");
3915 return i;
3916 }
3917
3918 tsk = tipc_sk(sk);
3919 tsk_connected = !tipc_sk_type_connectionless(sk);
3920
3921 i += scnprintf(buf, sz, "sk data: %u", sk->sk_type);
3922 i += scnprintf(buf + i, sz - i, " %d", sk->sk_state);
3923 i += scnprintf(buf + i, sz - i, " %x", tsk_own_node(tsk));
3924 i += scnprintf(buf + i, sz - i, " %u", tsk->portid);
3925 i += scnprintf(buf + i, sz - i, " | %u", tsk_connected);
3926 if (tsk_connected) {
3927 i += scnprintf(buf + i, sz - i, " %x", tsk_peer_node(tsk));
3928 i += scnprintf(buf + i, sz - i, " %u", tsk_peer_port(tsk));
3929 i += scnprintf(buf + i, sz - i, " %u", tsk->conn_type);
3930 i += scnprintf(buf + i, sz - i, " %u", tsk->conn_instance);
3931 }
3932 i += scnprintf(buf + i, sz - i, " | %u", tsk->published);
3933 if (tsk->published) {
3934 p = list_first_entry_or_null(&tsk->publications,
3935 struct publication, binding_sock);
3936 i += scnprintf(buf + i, sz - i, " %u", (p) ? p->type : 0);
3937 i += scnprintf(buf + i, sz - i, " %u", (p) ? p->lower : 0);
3938 i += scnprintf(buf + i, sz - i, " %u", (p) ? p->upper : 0);
3939 }
3940 i += scnprintf(buf + i, sz - i, " | %u", tsk->snd_win);
3941 i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_win);
3942 i += scnprintf(buf + i, sz - i, " %u", tsk->max_pkt);
3943 i += scnprintf(buf + i, sz - i, " %x", tsk->peer_caps);
3944 i += scnprintf(buf + i, sz - i, " %u", tsk->cong_link_cnt);
3945 i += scnprintf(buf + i, sz - i, " %u", tsk->snt_unacked);
3946 i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_unacked);
3947 i += scnprintf(buf + i, sz - i, " %u", atomic_read(&tsk->dupl_rcvcnt));
3948 i += scnprintf(buf + i, sz - i, " %u", sk->sk_shutdown);
3949 i += scnprintf(buf + i, sz - i, " | %d", sk_wmem_alloc_get(sk));
3950 i += scnprintf(buf + i, sz - i, " %d", sk->sk_sndbuf);
3951 i += scnprintf(buf + i, sz - i, " | %d", sk_rmem_alloc_get(sk));
3952 i += scnprintf(buf + i, sz - i, " %d", sk->sk_rcvbuf);
Eric Dumazet70c26552019-10-09 15:41:03 -07003953 i += scnprintf(buf + i, sz - i, " | %d\n", READ_ONCE(sk->sk_backlog.len));
Tuong Lienb4b97712018-12-19 09:17:56 +07003954
3955 if (dqueues & TIPC_DUMP_SK_SNDQ) {
3956 i += scnprintf(buf + i, sz - i, "sk_write_queue: ");
3957 i += tipc_list_dump(&sk->sk_write_queue, false, buf + i);
3958 }
3959
3960 if (dqueues & TIPC_DUMP_SK_RCVQ) {
3961 i += scnprintf(buf + i, sz - i, "sk_receive_queue: ");
3962 i += tipc_list_dump(&sk->sk_receive_queue, false, buf + i);
3963 }
3964
3965 if (dqueues & TIPC_DUMP_SK_BKLGQ) {
3966 i += scnprintf(buf + i, sz - i, "sk_backlog:\n head ");
3967 i += tipc_skb_dump(sk->sk_backlog.head, false, buf + i);
3968 if (sk->sk_backlog.tail != sk->sk_backlog.head) {
3969 i += scnprintf(buf + i, sz - i, " tail ");
3970 i += tipc_skb_dump(sk->sk_backlog.tail, false,
3971 buf + i);
3972 }
3973 }
3974
3975 return i;
3976}