blob: 64bbf9d036290f890c6b816b5c6e1576ad0df3b2 [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"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040049
Ying Xue07f6c4b2015-01-07 13:41:58 +080050#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Ying Xue2f55c432015-01-09 15:27:00 +080051#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080052#define TIPC_FWD_MSG 1
Ying Xue07f6c4b2015-01-07 13:41:58 +080053#define TIPC_MAX_PORT 0xffffffff
54#define TIPC_MIN_PORT 1
Jon Paul Maloye9f8b102017-05-02 18:16:53 +020055#define TIPC_ACK_RATE 4 /* ACK at 1/4 of of rcv window size */
Jon Paul Maloy301bae52014-08-22 18:09:20 -040056
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010057enum {
58 TIPC_LISTEN = TCP_LISTEN,
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +010059 TIPC_ESTABLISHED = TCP_ESTABLISHED,
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +010060 TIPC_OPEN = TCP_CLOSE,
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +010061 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +010062 TIPC_CONNECTING = TCP_SYN_SENT,
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010063};
64
Jon Paul Maloy301bae52014-08-22 18:09:20 -040065/**
66 * struct tipc_sock - TIPC socket structure
67 * @sk: socket - interacts with 'port' and with user via the socket API
Jon Paul Maloy301bae52014-08-22 18:09:20 -040068 * @conn_type: TIPC type used when connection was established
69 * @conn_instance: TIPC instance used when connection was established
70 * @published: non-zero if port has one or more associated names
71 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Ying Xue07f6c4b2015-01-07 13:41:58 +080072 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040073 * @phdr: preformatted message header used when sending messages
Jon Paul Maloy365ad352017-01-03 10:55:11 -050074 * #cong_links: list of congested links
Jon Paul Maloy301bae52014-08-22 18:09:20 -040075 * @publications: list of publications for port
Jon Paul Maloy365ad352017-01-03 10:55:11 -050076 * @blocking_link: address of the congested link we are currently sleeping on
Jon Paul Maloy301bae52014-08-22 18:09:20 -040077 * @pub_count: total # of publications port has made during its lifetime
78 * @probing_state:
Jon Paul Maloy301bae52014-08-22 18:09:20 -040079 * @conn_timeout: the time we can wait for an unresponded setup request
80 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
Jon Paul Maloy365ad352017-01-03 10:55:11 -050081 * @cong_link_cnt: number of congested links
Jon Maloy75da2162017-10-13 11:04:23 +020082 * @snt_unacked: # messages sent by socket, and not yet acked by peer
Jon Paul Maloy301bae52014-08-22 18:09:20 -040083 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +010084 * @peer: 'connected' peer for dgram/rdm
Ying Xue07f6c4b2015-01-07 13:41:58 +080085 * @node: hash table node
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -050086 * @mc_method: cookie for use between socket and broadcast layer
Ying Xue07f6c4b2015-01-07 13:41:58 +080087 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040088 */
89struct tipc_sock {
90 struct sock sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040091 u32 conn_type;
92 u32 conn_instance;
93 int published;
94 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080095 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040096 struct tipc_msg phdr;
Jon Paul Maloy365ad352017-01-03 10:55:11 -050097 struct list_head cong_links;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040098 struct list_head publications;
99 u32 pub_count;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400100 uint conn_timeout;
101 atomic_t dupl_rcvcnt;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100102 bool probe_unacked;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500103 u16 cong_link_cnt;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400104 u16 snt_unacked;
105 u16 snd_win;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400106 u16 peer_caps;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400107 u16 rcv_unacked;
108 u16 rcv_win;
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +0100109 struct sockaddr_tipc peer;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800110 struct rhash_head node;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500111 struct tipc_mc_method mc_method;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800112 struct rcu_head rcu;
Jon Maloy75da2162017-10-13 11:04:23 +0200113 struct tipc_group *group;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400114};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100115
Jon Maloy64ac5f52017-10-13 11:04:20 +0200116static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400117static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800118static void tipc_write_space(struct sock *sk);
Ying Xuef4195d12015-11-22 15:46:05 +0800119static void tipc_sock_destruct(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800120static int tipc_release(struct socket *sock);
David Howellscdfbabf2017-03-09 08:09:05 +0000121static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
122 bool kern);
Ying Xuef2f2a962015-01-09 15:27:02 +0800123static void tipc_sk_timeout(unsigned long data);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400124static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400125 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400126static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400127 struct tipc_name_seq const *seq);
Jon Maloy75da2162017-10-13 11:04:23 +0200128static int tipc_sk_leave(struct tipc_sock *tsk);
Ying Xuee05b31f2015-01-09 15:27:08 +0800129static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800130static int tipc_sk_insert(struct tipc_sock *tsk);
131static void tipc_sk_remove(struct tipc_sock *tsk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500132static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
Ying Xue39a02952015-03-02 15:37:47 +0800133static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100134
Florian Westphalbca65ea2008-02-07 18:18:01 -0800135static const struct proto_ops packet_ops;
136static const struct proto_ops stream_ops;
137static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100138static struct proto tipc_proto;
Herbert Xu6cca72892015-03-20 21:57:05 +1100139static const struct rhashtable_params tsk_rht_params;
140
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500141static u32 tsk_own_node(struct tipc_sock *tsk)
142{
143 return msg_prevnode(&tsk->phdr);
144}
145
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400146static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400147{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400148 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400149}
150
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400151static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400152{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400153 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400154}
155
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400156static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400157{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400158 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400159}
160
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400161static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400162{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400163 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400164}
165
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400166static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400167{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400168 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400169}
170
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400171static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400172{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400173 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400174}
175
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400176static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400177{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400178 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400179}
180
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400181static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400182{
183 if (imp > TIPC_CRITICAL_IMPORTANCE)
184 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400185 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400186 return 0;
187}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400188
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400189static struct tipc_sock *tipc_sk(const struct sock *sk)
190{
191 return container_of(sk, struct tipc_sock, sk);
192}
193
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400194static bool tsk_conn_cong(struct tipc_sock *tsk)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400195{
Jon Paul Maloy6998cc62016-11-24 18:47:07 -0500196 return tsk->snt_unacked > tsk->snd_win;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400197}
198
199/* tsk_blocks(): translate a buffer size in bytes to number of
200 * advertisable blocks, taking into account the ratio truesize(len)/len
201 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
202 */
203static u16 tsk_adv_blocks(int len)
204{
205 return len / FLOWCTL_BLK_SZ / 4;
206}
207
208/* tsk_inc(): increment counter for sent or received data
209 * - If block based flow control is not supported by peer we
210 * fall back to message based ditto, incrementing the counter
211 */
212static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
213{
214 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
215 return ((msglen / FLOWCTL_BLK_SZ) + 1);
216 return 1;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400217}
218
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400220 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700221 *
222 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100223 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400224static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100225{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400226 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227}
228
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400229/* tipc_sk_respond() : send response message back to sender
230 */
231static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
232{
233 u32 selector;
234 u32 dnode;
235 u32 onode = tipc_own_addr(sock_net(sk));
236
237 if (!tipc_msg_reverse(onode, &skb, err))
238 return;
239
240 dnode = msg_destnode(buf_msg(skb));
241 selector = msg_origport(buf_msg(skb));
242 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
243}
244
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400246 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700247 *
248 * Caller must hold socket lock
249 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400250static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700251{
Ying Xuea6ca1092014-11-26 11:41:55 +0800252 struct sk_buff *skb;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700253
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400254 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
255 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700256}
257
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100258static bool tipc_sk_connected(struct sock *sk)
259{
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100260 return sk->sk_state == TIPC_ESTABLISHED;
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100261}
262
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100263/* tipc_sk_type_connectionless - check if the socket is datagram socket
264 * @sk: socket
265 *
266 * Returns true if connection less, false otherwise
267 */
268static bool tipc_sk_type_connectionless(struct sock *sk)
269{
270 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
271}
272
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400273/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400274 *
275 * Handles cases where the node's network address has changed from
276 * the default of <0.0.0> to its configured setting.
277 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400278static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400279{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100280 struct sock *sk = &tsk->sk;
281 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400282 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400283 u32 orig_node;
284 u32 peer_node;
285
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100286 if (unlikely(!tipc_sk_connected(sk)))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400287 return false;
288
289 if (unlikely(msg_origport(msg) != peer_port))
290 return false;
291
292 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400293 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400294
295 if (likely(orig_node == peer_node))
296 return true;
297
Ying Xue34747532015-01-09 15:27:10 +0800298 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400299 return true;
300
Ying Xue34747532015-01-09 15:27:10 +0800301 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400302 return true;
303
304 return false;
305}
306
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100307/* tipc_set_sk_state - set the sk_state of the socket
308 * @sk: socket
309 *
310 * Caller must hold socket lock
311 *
312 * Returns 0 on success, errno otherwise
313 */
314static int tipc_set_sk_state(struct sock *sk, int state)
315{
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100316 int oldsk_state = sk->sk_state;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100317 int res = -EINVAL;
318
319 switch (state) {
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100320 case TIPC_OPEN:
321 res = 0;
322 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100323 case TIPC_LISTEN:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100324 case TIPC_CONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100325 if (oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100326 res = 0;
327 break;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100328 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100329 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100330 oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100331 res = 0;
332 break;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100333 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100334 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100335 oldsk_state == TIPC_ESTABLISHED)
336 res = 0;
337 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100338 }
339
340 if (!res)
341 sk->sk_state = state;
342
343 return res;
344}
345
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500346static int tipc_sk_sock_err(struct socket *sock, long *timeout)
347{
348 struct sock *sk = sock->sk;
349 int err = sock_error(sk);
350 int typ = sock->type;
351
352 if (err)
353 return err;
354 if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
355 if (sk->sk_state == TIPC_DISCONNECTING)
356 return -EPIPE;
357 else if (!tipc_sk_connected(sk))
358 return -ENOTCONN;
359 }
360 if (!*timeout)
361 return -EAGAIN;
362 if (signal_pending(current))
363 return sock_intr_errno(*timeout);
364
365 return 0;
366}
367
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200368#define tipc_wait_for_cond(sock_, timeo_, condition_) \
369({ \
370 struct sock *sk_; \
371 int rc_; \
372 \
373 while ((rc_ = !(condition_))) { \
374 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
375 sk_ = (sock_)->sk; \
376 rc_ = tipc_sk_sock_err((sock_), timeo_); \
377 if (rc_) \
378 break; \
379 prepare_to_wait(sk_sleep(sk_), &wait_, TASK_INTERRUPTIBLE); \
380 release_sock(sk_); \
381 *(timeo_) = wait_woken(&wait_, TASK_INTERRUPTIBLE, *(timeo_)); \
382 sched_annotate_sleep(); \
383 lock_sock(sk_); \
384 remove_wait_queue(sk_sleep(sk_), &wait_); \
385 } \
386 rc_; \
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500387})
388
Allan Stephens0c3141e2008-04-15 00:22:02 -0700389/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400390 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700391 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392 * @sock: pre-allocated socket structure
393 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800394 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900395 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700396 * This routine creates additional data structures used by the TIPC socket,
397 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398 *
399 * Returns 0 on success, errno otherwise
400 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400401static int tipc_sk_create(struct net *net, struct socket *sock,
402 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403{
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500404 struct tipc_net *tn;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700405 const struct proto_ops *ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400407 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400408 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700409
410 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411 if (unlikely(protocol != 0))
412 return -EPROTONOSUPPORT;
413
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414 switch (sock->type) {
415 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700416 ops = &stream_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 break;
418 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700419 ops = &packet_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420 break;
421 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700423 ops = &msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 break;
Allan Stephens49978652006-06-25 23:47:18 -0700425 default:
Allan Stephens49978652006-06-25 23:47:18 -0700426 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100427 }
428
Allan Stephens0c3141e2008-04-15 00:22:02 -0700429 /* Allocate socket's protocol area */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500430 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700431 if (sk == NULL)
432 return -ENOMEM;
433
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400434 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400435 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400436 INIT_LIST_HEAD(&tsk->publications);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500437 INIT_LIST_HEAD(&tsk->cong_links);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400438 msg = &tsk->phdr;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500439 tn = net_generic(sock_net(sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440
Allan Stephens0c3141e2008-04-15 00:22:02 -0700441 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700442 sock->ops = ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443 sock_init_data(sock, sk);
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100444 tipc_set_sk_state(sk, TIPC_OPEN);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800445 if (tipc_sk_insert(tsk)) {
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900446 pr_warn("Socket create failed; port number exhausted\n");
Ying Xue07f6c4b2015-01-07 13:41:58 +0800447 return -EINVAL;
448 }
Herbert Xu40f9f432017-02-11 19:26:46 +0800449
450 /* Ensure tsk is visible before we read own_addr. */
451 smp_mb();
452
453 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
454 NAMED_H_SIZE, 0);
455
Ying Xue07f6c4b2015-01-07 13:41:58 +0800456 msg_set_origport(msg, tsk->portid);
Ying Xue3721e9c2015-01-13 17:07:48 +0800457 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100458 sk->sk_shutdown = 0;
Jon Maloy64ac5f52017-10-13 11:04:20 +0200459 sk->sk_backlog_rcv = tipc_sk_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400460 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800461 sk->sk_data_ready = tipc_data_ready;
462 sk->sk_write_space = tipc_write_space;
Ying Xuef4195d12015-11-22 15:46:05 +0800463 sk->sk_destruct = tipc_sock_destruct;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400464 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
465 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700466
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400467 /* Start out with safe limits until we receive an advertised window */
468 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
469 tsk->rcv_win = tsk->snd_win;
470
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +0100471 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400472 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700473 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400474 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700475 }
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100476
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477 return 0;
478}
479
Ying Xue07f6c4b2015-01-07 13:41:58 +0800480static void tipc_sk_callback(struct rcu_head *head)
481{
482 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
483
484 sock_put(&tsk->sk);
485}
486
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100487/* Caller should hold socket lock for the socket. */
488static void __tipc_shutdown(struct socket *sock, int error)
489{
490 struct sock *sk = sock->sk;
491 struct tipc_sock *tsk = tipc_sk(sk);
492 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500493 long timeout = CONN_TIMEOUT_DEFAULT;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100494 u32 dnode = tsk_peer_node(tsk);
495 struct sk_buff *skb;
496
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500497 /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
498 tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
499 !tsk_conn_cong(tsk)));
500
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100501 /* Reject all unreceived messages, except on an active connection
502 * (which disconnects locally & sends a 'FIN+' to peer).
503 */
504 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
505 if (TIPC_SKB_CB(skb)->bytes_read) {
506 kfree_skb(skb);
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500507 continue;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100508 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500509 if (!tipc_sk_type_connectionless(sk) &&
510 sk->sk_state != TIPC_DISCONNECTING) {
511 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
512 tipc_node_remove_conn(net, dnode, tsk->portid);
513 }
514 tipc_sk_respond(sk, skb, error);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100515 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500516
517 if (tipc_sk_type_connectionless(sk))
518 return;
519
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100520 if (sk->sk_state != TIPC_DISCONNECTING) {
521 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
522 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
523 tsk_own_node(tsk), tsk_peer_port(tsk),
524 tsk->portid, error);
525 if (skb)
526 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500527 tipc_node_remove_conn(net, dnode, tsk->portid);
528 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100529 }
530}
531
Ying Xuec5fa7b32013-06-17 10:54:39 -0400532/**
Ying Xue247f0f32014-02-18 16:06:46 +0800533 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534 * @sock: socket to destroy
535 *
536 * This routine cleans up any messages that are still queued on the socket.
537 * For DGRAM and RDM socket types, all queued messages are rejected.
538 * For SEQPACKET and STREAM socket types, the first message is rejected
539 * and any others are discarded. (If the first message on a STREAM socket
540 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900541 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542 * NOTE: Rejected messages are not necessarily returned to the sender! They
543 * are returned or discarded according to the "destination droppable" setting
544 * specified for the message by the sender.
545 *
546 * Returns 0 on success, errno otherwise
547 */
Ying Xue247f0f32014-02-18 16:06:46 +0800548static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100550 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400551 struct tipc_sock *tsk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552
Allan Stephens0c3141e2008-04-15 00:22:02 -0700553 /*
554 * Exit if socket isn't fully initialized (occurs when a failed accept()
555 * releases a pre-allocated child socket that was never used)
556 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700557 if (sk == NULL)
558 return 0;
559
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400560 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700561 lock_sock(sk);
562
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100563 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
564 sk->sk_shutdown = SHUTDOWN_MASK;
Jon Maloy75da2162017-10-13 11:04:23 +0200565 tipc_sk_leave(tsk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400566 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue1ea23a22015-05-28 13:19:22 +0800567 sk_stop_timer(sk, &sk->sk_timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800568 tipc_sk_remove(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100569
Allan Stephens0c3141e2008-04-15 00:22:02 -0700570 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700571 release_sock(sk);
Jon Maloya80ae532017-10-13 11:04:22 +0200572 tipc_dest_list_purge(&tsk->cong_links);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500573 tsk->cong_link_cnt = 0;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800574 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700575 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200577 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578}
579
580/**
Ying Xue247f0f32014-02-18 16:06:46 +0800581 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 * @sock: socket structure
583 * @uaddr: socket address describing name(s) and desired operation
584 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900585 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586 * Name and name sequence binding is indicated using a positive scope value;
587 * a negative scope value unbinds the specified name. Specifying no name
588 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900589 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700591 *
592 * NOTE: This routine doesn't need to take the socket lock since it doesn't
593 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100594 */
Ying Xue247f0f32014-02-18 16:06:46 +0800595static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
596 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100597{
Ying Xue84602762013-12-27 10:18:28 +0800598 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100599 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400600 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800601 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602
Ying Xue84602762013-12-27 10:18:28 +0800603 lock_sock(sk);
604 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400605 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800606 goto exit;
607 }
Jon Maloy75da2162017-10-13 11:04:23 +0200608 if (tsk->group) {
609 res = -EACCES;
610 goto exit;
611 }
Ying Xue84602762013-12-27 10:18:28 +0800612 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
613 res = -EINVAL;
614 goto exit;
615 }
616 if (addr->family != AF_TIPC) {
617 res = -EAFNOSUPPORT;
618 goto exit;
619 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100620
Per Lidenb97bf3f2006-01-02 19:04:38 +0100621 if (addr->addrtype == TIPC_ADDR_NAME)
622 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800623 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
624 res = -EAFNOSUPPORT;
625 goto exit;
626 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900627
Ying Xue13a2e892013-06-17 10:54:40 -0400628 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400629 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800630 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
631 res = -EACCES;
632 goto exit;
633 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400634
Ying Xue84602762013-12-27 10:18:28 +0800635 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400636 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
637 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800638exit:
639 release_sock(sk);
640 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641}
642
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900643/**
Ying Xue247f0f32014-02-18 16:06:46 +0800644 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100645 * @sock: socket structure
646 * @uaddr: area for returned socket address
647 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700648 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900649 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700651 *
Allan Stephens2da59912008-07-14 22:43:32 -0700652 * NOTE: This routine doesn't need to take the socket lock since it only
653 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000654 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100655 */
Ying Xue247f0f32014-02-18 16:06:46 +0800656static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
657 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100658{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100660 struct sock *sk = sock->sk;
661 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue34747532015-01-09 15:27:10 +0800662 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000664 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700665 if (peer) {
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100666 if ((!tipc_sk_connected(sk)) &&
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100667 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
Allan Stephens2da59912008-07-14 22:43:32 -0700668 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400669 addr->addr.id.ref = tsk_peer_port(tsk);
670 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700671 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800672 addr->addr.id.ref = tsk->portid;
Ying Xue34747532015-01-09 15:27:10 +0800673 addr->addr.id.node = tn->own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700674 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100675
676 *uaddr_len = sizeof(*addr);
677 addr->addrtype = TIPC_ADDR_ID;
678 addr->family = AF_TIPC;
679 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680 addr->addr.name.domain = 0;
681
Allan Stephens0c3141e2008-04-15 00:22:02 -0700682 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683}
684
685/**
Ying Xue247f0f32014-02-18 16:06:46 +0800686 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100687 * @file: file structure associated with the socket
688 * @sock: socket for which to calculate the poll bits
689 * @wait: ???
690 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700691 * Returns pollmask value
692 *
693 * COMMENTARY:
694 * It appears that the usual socket locking mechanisms are not useful here
695 * since the pollmask info is potentially out-of-date the moment this routine
696 * exits. TCP and other protocols seem to rely on higher level poll routines
697 * to handle any preventable race conditions, so TIPC will do the same ...
698 *
Allan Stephensf662c072010-08-17 11:00:06 +0000699 * IMPORTANT: The fact that a read or write operation is indicated does NOT
700 * imply that the operation will succeed, merely that it should be performed
701 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100702 */
Ying Xue247f0f32014-02-18 16:06:46 +0800703static unsigned int tipc_poll(struct file *file, struct socket *sock,
704 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100705{
Allan Stephens9b674e82008-03-26 16:48:21 -0700706 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400707 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy75da2162017-10-13 11:04:23 +0200708 struct tipc_group *grp = tsk->group;
Allan Stephensf662c072010-08-17 11:00:06 +0000709 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700710
Ying Xuef288bef2012-08-21 11:16:57 +0800711 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700712
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100713 if (sk->sk_shutdown & RCV_SHUTDOWN)
714 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
715 if (sk->sk_shutdown == SHUTDOWN_MASK)
716 mask |= POLLHUP;
717
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100718 switch (sk->sk_state) {
719 case TIPC_ESTABLISHED:
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500720 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000721 mask |= POLLOUT;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100722 /* fall thru' */
723 case TIPC_LISTEN:
724 case TIPC_CONNECTING:
Allan Stephensf662c072010-08-17 11:00:06 +0000725 if (!skb_queue_empty(&sk->sk_receive_queue))
726 mask |= (POLLIN | POLLRDNORM);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100727 break;
728 case TIPC_OPEN:
Jon Maloy75da2162017-10-13 11:04:23 +0200729 if (!grp || tipc_group_size(grp))
730 if (!tsk->cong_link_cnt)
731 mask |= POLLOUT;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100732 if (tipc_sk_type_connectionless(sk) &&
733 (!skb_queue_empty(&sk->sk_receive_queue)))
734 mask |= (POLLIN | POLLRDNORM);
735 break;
736 case TIPC_DISCONNECTING:
737 mask = (POLLIN | POLLRDNORM | POLLHUP);
738 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000739 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700740
741 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742}
743
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400744/**
745 * tipc_sendmcast - send multicast message
746 * @sock: socket structure
747 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500748 * @msg: message to send
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500749 * @dlen: length of data to send
750 * @timeout: timeout to wait for wakeup
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400751 *
752 * Called from function tipc_sendmsg(), which has done all sanity checks
753 * Returns the number of bytes sent on success, or errno
754 */
755static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500756 struct msghdr *msg, size_t dlen, long timeout)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400757{
758 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500759 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500760 struct tipc_msg *hdr = &tsk->phdr;
Ying Xuef2f98002015-01-09 15:27:05 +0800761 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500762 int mtu = tipc_bcast_get_mtu(net);
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500763 struct tipc_mc_method *method = &tsk->mc_method;
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500764 u32 domain = addr_domain(net, TIPC_CLUSTER_SCOPE);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500765 struct sk_buff_head pkts;
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500766 struct tipc_nlist dsts;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400767 int rc;
768
Jon Maloy75da2162017-10-13 11:04:23 +0200769 if (tsk->group)
770 return -EACCES;
771
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500772 /* Block or return if any destination link is congested */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500773 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
774 if (unlikely(rc))
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400775 return rc;
776
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500777 /* Lookup destination nodes */
778 tipc_nlist_init(&dsts, tipc_own_addr(net));
779 tipc_nametbl_lookup_dst_nodes(net, seq->type, seq->lower,
780 seq->upper, domain, &dsts);
781 if (!dsts.local && !dsts.remote)
782 return -EHOSTUNREACH;
783
784 /* Build message header */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500785 msg_set_type(hdr, TIPC_MCAST_MSG);
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500786 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500787 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
788 msg_set_destport(hdr, 0);
789 msg_set_destnode(hdr, 0);
790 msg_set_nametype(hdr, seq->type);
791 msg_set_namelower(hdr, seq->lower);
792 msg_set_nameupper(hdr, seq->upper);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400793
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500794 /* Build message as chain of buffers */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500795 skb_queue_head_init(&pkts);
796 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500797
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500798 /* Send message if build was successful */
799 if (unlikely(rc == dlen))
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500800 rc = tipc_mcast_xmit(net, &pkts, method, &dsts,
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500801 &tsk->cong_link_cnt);
802
803 tipc_nlist_purge(&dsts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500804
805 return rc ? rc : dlen;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400806}
807
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500808/**
Jon Maloy75da2162017-10-13 11:04:23 +0200809 * tipc_send_group_bcast - send message to all members in communication group
810 * @sk: socket structure
811 * @m: message to send
812 * @dlen: total length of message data
813 * @timeout: timeout to wait for wakeup
814 *
815 * Called from function tipc_sendmsg(), which has done all sanity checks
816 * Returns the number of bytes sent on success, or errno
817 */
818static int tipc_send_group_bcast(struct socket *sock, struct msghdr *m,
819 int dlen, long timeout)
820{
821 struct sock *sk = sock->sk;
822 struct net *net = sock_net(sk);
823 struct tipc_sock *tsk = tipc_sk(sk);
824 struct tipc_group *grp = tsk->group;
825 struct tipc_nlist *dsts = tipc_group_dests(grp);
826 struct tipc_mc_method *method = &tsk->mc_method;
827 struct tipc_msg *hdr = &tsk->phdr;
828 int mtu = tipc_bcast_get_mtu(net);
829 struct sk_buff_head pkts;
830 int rc = -EHOSTUNREACH;
831
832 if (!dsts->local && !dsts->remote)
833 return -EHOSTUNREACH;
834
835 /* Block or return if any destination link is congested */
836 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
837 if (unlikely(rc))
838 return rc;
839
840 /* Complete message header */
841 msg_set_type(hdr, TIPC_GRP_BCAST_MSG);
842 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
843 msg_set_destport(hdr, 0);
844 msg_set_destnode(hdr, 0);
845 msg_set_nameinst(hdr, 0);
846 msg_set_grp_bc_seqno(hdr, tipc_group_bc_snd_nxt(grp));
847
848 /* Build message as chain of buffers */
849 skb_queue_head_init(&pkts);
850 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
851 if (unlikely(rc != dlen))
852 return rc;
853
854 /* Send message */
855 rc = tipc_mcast_xmit(net, &pkts, method, dsts,
856 &tsk->cong_link_cnt);
857 if (unlikely(rc))
858 return rc;
859
860 /* Update broadcast sequence number */
861 tipc_group_update_bc_members(tsk->group);
862
863 return dlen;
864}
865
866/**
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500867 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
868 * @arrvq: queue with arriving messages, to be cloned after destination lookup
869 * @inputq: queue with cloned messages, delivered to socket after dest lookup
870 *
871 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400872 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500873void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
874 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400875{
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400876 u32 scope = TIPC_CLUSTER_SCOPE;
Jon Maloy75da2162017-10-13 11:04:23 +0200877 u32 self = tipc_own_addr(net);
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500878 struct sk_buff *skb, *_skb;
Jon Maloy75da2162017-10-13 11:04:23 +0200879 u32 lower = 0, upper = ~0;
880 struct sk_buff_head tmpq;
881 u32 portid, oport, onode;
882 struct list_head dports;
883 struct tipc_msg *msg;
884 int hsz;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500885
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500886 __skb_queue_head_init(&tmpq);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -0500887 INIT_LIST_HEAD(&dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400888
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500889 skb = tipc_skb_peek(arrvq, &inputq->lock);
890 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
891 msg = buf_msg(skb);
892 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
Jon Maloy75da2162017-10-13 11:04:23 +0200893 oport = msg_origport(msg);
894 onode = msg_orignode(msg);
895 if (onode == self)
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500896 scope = TIPC_NODE_SCOPE;
897
898 /* Create destination port list and message clones: */
Jon Maloy75da2162017-10-13 11:04:23 +0200899 if (!msg_in_group(msg)) {
900 lower = msg_namelower(msg);
901 upper = msg_nameupper(msg);
902 }
903 tipc_nametbl_mc_translate(net, msg_nametype(msg), lower, upper,
904 scope, &dports);
Jon Maloya80ae532017-10-13 11:04:22 +0200905 while (tipc_dest_pop(&dports, NULL, &portid)) {
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500906 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
907 if (_skb) {
908 msg_set_destport(buf_msg(_skb), portid);
909 __skb_queue_tail(&tmpq, _skb);
910 continue;
911 }
912 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400913 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500914 /* Append to inputq if not already done by other thread */
915 spin_lock_bh(&inputq->lock);
916 if (skb_peek(arrvq) == skb) {
917 skb_queue_splice_tail_init(&tmpq, inputq);
918 kfree_skb(__skb_dequeue(arrvq));
919 }
920 spin_unlock_bh(&inputq->lock);
921 __skb_queue_purge(&tmpq);
922 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400923 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500924 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400925}
926
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900927/**
Jon Maloy64ac5f52017-10-13 11:04:20 +0200928 * tipc_sk_conn_proto_rcv - receive a connection mng protocol message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500929 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400930 * @skb: pointer to message buffer.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500931 */
Jon Maloy64ac5f52017-10-13 11:04:20 +0200932static void tipc_sk_conn_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
933 struct sk_buff_head *xmitq)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500934{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400935 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy64ac5f52017-10-13 11:04:20 +0200936 u32 onode = tsk_own_node(tsk);
937 struct sock *sk = &tsk->sk;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400938 int mtyp = msg_type(hdr);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400939 bool conn_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400940
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500941 /* Ignore if connection cannot be validated: */
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400942 if (!tsk_peer_msg(tsk, hdr))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500943 goto exit;
944
Parthasarathy Bhuvaraganc1be7752017-04-26 10:05:02 +0200945 if (unlikely(msg_errcode(hdr))) {
946 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
947 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
948 tsk_peer_port(tsk));
949 sk->sk_state_change(sk);
950 goto exit;
951 }
952
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100953 tsk->probe_unacked = false;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500954
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400955 if (mtyp == CONN_PROBE) {
956 msg_set_type(hdr, CONN_PROBE_REPLY);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400957 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
958 __skb_queue_tail(xmitq, skb);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400959 return;
960 } else if (mtyp == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400961 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400962 tsk->snt_unacked -= msg_conn_ack(hdr);
963 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
964 tsk->snd_win = msg_adv_win(hdr);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500965 if (conn_cong)
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400966 sk->sk_write_space(sk);
967 } else if (mtyp != CONN_PROBE_REPLY) {
968 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500969 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500970exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400971 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500972}
973
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500974/**
Ying Xue247f0f32014-02-18 16:06:46 +0800975 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976 * @sock: socket structure
977 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500978 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900979 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900981 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100982 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
983 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900984 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100985 * Returns the number of bytes sent on success, or errno otherwise
986 */
Ying Xue1b784142015-03-02 15:37:48 +0800987static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500988 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989{
Ying Xue39a02952015-03-02 15:37:47 +0800990 struct sock *sk = sock->sk;
991 int ret;
992
993 lock_sock(sk);
994 ret = __tipc_sendmsg(sock, m, dsz);
995 release_sock(sk);
996
997 return ret;
998}
999
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001000static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a02952015-03-02 15:37:47 +08001001{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001002 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001003 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001004 struct tipc_sock *tsk = tipc_sk(sk);
1005 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
1006 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1007 struct list_head *clinks = &tsk->cong_links;
1008 bool syn = !tipc_sk_type_connectionless(sk);
Jon Maloy75da2162017-10-13 11:04:23 +02001009 struct tipc_group *grp = tsk->group;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001010 struct tipc_msg *hdr = &tsk->phdr;
Erik Hugnef2f80362015-03-19 09:02:19 +01001011 struct tipc_name_seq *seq;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001012 struct sk_buff_head pkts;
1013 u32 type, inst, domain;
1014 u32 dnode, dport;
1015 int mtu, rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001017 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
Allan Stephensc29c3f72010-04-20 17:58:24 -04001018 return -EMSGSIZE;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001019
Jon Maloy75da2162017-10-13 11:04:23 +02001020 if (unlikely(grp))
1021 return tipc_send_group_bcast(sock, m, dlen, timeout);
1022
Erik Hugnef2f80362015-03-19 09:02:19 +01001023 if (unlikely(!dest)) {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001024 dest = &tsk->peer;
1025 if (!syn || dest->family != AF_TIPC)
Erik Hugnef2f80362015-03-19 09:02:19 +01001026 return -EDESTADDRREQ;
Erik Hugnef2f80362015-03-19 09:02:19 +01001027 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001028
1029 if (unlikely(m->msg_namelen < sizeof(*dest)))
1030 return -EINVAL;
1031
1032 if (unlikely(dest->family != AF_TIPC))
1033 return -EINVAL;
1034
1035 if (unlikely(syn)) {
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001036 if (sk->sk_state == TIPC_LISTEN)
Ying Xue39a02952015-03-02 15:37:47 +08001037 return -EPIPE;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001038 if (sk->sk_state != TIPC_OPEN)
Ying Xue39a02952015-03-02 15:37:47 +08001039 return -EISCONN;
1040 if (tsk->published)
1041 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001042 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001043 tsk->conn_type = dest->addr.name.name.type;
1044 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001045 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001046 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001047
Erik Hugnef2f80362015-03-19 09:02:19 +01001048 seq = &dest->addr.nameseq;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001049 if (dest->addrtype == TIPC_ADDR_MCAST)
1050 return tipc_sendmcast(sock, seq, m, dlen, timeout);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001051
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001052 if (dest->addrtype == TIPC_ADDR_NAME) {
1053 type = dest->addr.name.name.type;
1054 inst = dest->addr.name.name.instance;
1055 domain = dest->addr.name.domain;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001056 dnode = domain;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001057 msg_set_type(hdr, TIPC_NAMED_MSG);
1058 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
1059 msg_set_nametype(hdr, type);
1060 msg_set_nameinst(hdr, inst);
1061 msg_set_lookup_scope(hdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +08001062 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001063 msg_set_destnode(hdr, dnode);
1064 msg_set_destport(hdr, dport);
Ying Xue39a02952015-03-02 15:37:47 +08001065 if (unlikely(!dport && !dnode))
1066 return -EHOSTUNREACH;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001067
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001068 } else if (dest->addrtype == TIPC_ADDR_ID) {
1069 dnode = dest->addr.id.node;
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001070 msg_set_type(hdr, TIPC_DIRECT_MSG);
1071 msg_set_lookup_scope(hdr, 0);
1072 msg_set_destnode(hdr, dnode);
1073 msg_set_destport(hdr, dest->addr.id.ref);
1074 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001075 }
1076
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001077 /* Block or return if destination link is congested */
Jon Maloya80ae532017-10-13 11:04:22 +02001078 rc = tipc_wait_for_cond(sock, &timeout,
1079 !tipc_dest_find(clinks, dnode, 0));
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001080 if (unlikely(rc))
Ying Xue39a02952015-03-02 15:37:47 +08001081 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001082
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001083 skb_queue_head_init(&pkts);
1084 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
1085 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1086 if (unlikely(rc != dlen))
1087 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001088
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001089 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1090 if (unlikely(rc == -ELINKCONG)) {
Jon Maloya80ae532017-10-13 11:04:22 +02001091 tipc_dest_push(clinks, dnode, 0);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001092 tsk->cong_link_cnt++;
1093 rc = 0;
1094 }
1095
1096 if (unlikely(syn && !rc))
1097 tipc_set_sk_state(sk, TIPC_CONNECTING);
1098
1099 return rc ? rc : dlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001100}
1101
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001102/**
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001103 * tipc_sendstream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001104 * @sock: socket structure
1105 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001106 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001107 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001108 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001109 *
1110 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001111 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001112 */
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001113static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001115 struct sock *sk = sock->sk;
Ying Xue39a02952015-03-02 15:37:47 +08001116 int ret;
1117
1118 lock_sock(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001119 ret = __tipc_sendstream(sock, m, dsz);
Ying Xue39a02952015-03-02 15:37:47 +08001120 release_sock(sk);
1121
1122 return ret;
1123}
1124
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001125static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a02952015-03-02 15:37:47 +08001126{
1127 struct sock *sk = sock->sk;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001128 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001129 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1130 struct tipc_sock *tsk = tipc_sk(sk);
1131 struct tipc_msg *hdr = &tsk->phdr;
1132 struct net *net = sock_net(sk);
1133 struct sk_buff_head pkts;
1134 u32 dnode = tsk_peer_node(tsk);
1135 int send, sent = 0;
1136 int rc = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001137
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001138 skb_queue_head_init(&pkts);
1139
1140 if (unlikely(dlen > INT_MAX))
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001141 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001142
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001143 /* Handle implicit connection setup */
1144 if (unlikely(dest)) {
1145 rc = __tipc_sendmsg(sock, m, dlen);
1146 if (dlen && (dlen == rc))
1147 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
1148 return rc;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001149 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001151 do {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001152 rc = tipc_wait_for_cond(sock, &timeout,
1153 (!tsk->cong_link_cnt &&
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -05001154 !tsk_conn_cong(tsk) &&
1155 tipc_sk_connected(sk)));
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001156 if (unlikely(rc))
1157 break;
Ying Xue39a02952015-03-02 15:37:47 +08001158
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001159 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
1160 rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
1161 if (unlikely(rc != send))
1162 break;
1163
1164 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1165 if (unlikely(rc == -ELINKCONG)) {
1166 tsk->cong_link_cnt = 1;
1167 rc = 0;
1168 }
1169 if (likely(!rc)) {
1170 tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
1171 sent += send;
1172 }
1173 } while (sent < dlen && !rc);
1174
Parthasarathy Bhuvaragan3364d612017-04-24 15:00:42 +02001175 return sent ? sent : rc;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001176}
1177
1178/**
1179 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001180 * @sock: socket structure
1181 * @m: message to send
1182 * @dsz: length of data to be transmitted
1183 *
1184 * Used for SOCK_SEQPACKET messages.
1185 *
1186 * Returns the number of bytes sent on success, or errno otherwise
1187 */
Ying Xue1b784142015-03-02 15:37:48 +08001188static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001189{
1190 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1191 return -EMSGSIZE;
1192
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001193 return tipc_sendstream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194}
1195
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001196/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001197 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001198static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001199 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200{
Ying Xue3721e9c2015-01-13 17:07:48 +08001201 struct sock *sk = &tsk->sk;
1202 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001203 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001204
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001205 msg_set_destnode(msg, peer_node);
1206 msg_set_destport(msg, peer_port);
1207 msg_set_type(msg, TIPC_CONN_MSG);
1208 msg_set_lookup_scope(msg, 0);
1209 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001210
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01001211 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001212 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
Ying Xuef2f98002015-01-09 15:27:05 +08001213 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1214 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001215 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001216 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1217 return;
1218
1219 /* Fall back to message based flow control */
1220 tsk->rcv_win = FLOWCTL_MSG_WIN;
1221 tsk->snd_win = FLOWCTL_MSG_WIN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001222}
1223
1224/**
1225 * set_orig_addr - capture sender's address for received message
1226 * @m: descriptor for message info
1227 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001228 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001229 * Note: Address is not captured if not requested by receiver.
1230 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001231static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001233 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001235 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001236 addr->family = AF_TIPC;
1237 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001238 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001239 addr->addr.id.ref = msg_origport(msg);
1240 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001241 addr->addr.name.domain = 0; /* could leave uninitialized */
1242 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001243 m->msg_namelen = sizeof(struct sockaddr_tipc);
1244 }
1245}
1246
1247/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001248 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001249 * @m: descriptor for message info
1250 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001251 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001252 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001253 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001254 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001255 * Returns 0 if successful, otherwise errno
1256 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001257static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1258 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259{
1260 u32 anc_data[3];
1261 u32 err;
1262 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001263 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264 int res;
1265
1266 if (likely(m->msg_controllen == 0))
1267 return 0;
1268
1269 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001270 err = msg ? msg_errcode(msg) : 0;
1271 if (unlikely(err)) {
1272 anc_data[0] = err;
1273 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001274 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1275 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001276 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001277 if (anc_data[1]) {
1278 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1279 msg_data(msg));
1280 if (res)
1281 return res;
1282 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001283 }
1284
1285 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001286 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1287 switch (dest_type) {
1288 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001289 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001290 anc_data[0] = msg_nametype(msg);
1291 anc_data[1] = msg_namelower(msg);
1292 anc_data[2] = msg_namelower(msg);
1293 break;
1294 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001295 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001296 anc_data[0] = msg_nametype(msg);
1297 anc_data[1] = msg_namelower(msg);
1298 anc_data[2] = msg_nameupper(msg);
1299 break;
1300 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001301 has_name = (tsk->conn_type != 0);
1302 anc_data[0] = tsk->conn_type;
1303 anc_data[1] = tsk->conn_instance;
1304 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001305 break;
1306 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001307 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001308 }
Allan Stephens2db99832010-12-31 18:59:33 +00001309 if (has_name) {
1310 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1311 if (res)
1312 return res;
1313 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314
1315 return 0;
1316}
1317
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001318static void tipc_sk_send_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001319{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001320 struct sock *sk = &tsk->sk;
1321 struct net *net = sock_net(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001322 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001323 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001324 u32 peer_port = tsk_peer_port(tsk);
1325 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001326
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001327 if (!tipc_sk_connected(sk))
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001328 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001329 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1330 dnode, tsk_own_node(tsk), peer_port,
1331 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001332 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001333 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001334 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001335 msg_set_conn_ack(msg, tsk->rcv_unacked);
1336 tsk->rcv_unacked = 0;
1337
1338 /* Adjust to and advertize the correct window limit */
1339 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1340 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1341 msg_set_adv_win(msg, tsk->rcv_win);
1342 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001343 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001344}
1345
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001346static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001347{
1348 struct sock *sk = sock->sk;
1349 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001350 long timeo = *timeop;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001351 int err = sock_error(sk);
1352
1353 if (err)
1354 return err;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001355
1356 for (;;) {
1357 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001358 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01001359 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001360 err = -ENOTCONN;
1361 break;
1362 }
1363 release_sock(sk);
1364 timeo = schedule_timeout(timeo);
1365 lock_sock(sk);
1366 }
1367 err = 0;
1368 if (!skb_queue_empty(&sk->sk_receive_queue))
1369 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001370 err = -EAGAIN;
1371 if (!timeo)
1372 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001373 err = sock_intr_errno(timeo);
1374 if (signal_pending(current))
1375 break;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001376
1377 err = sock_error(sk);
1378 if (err)
1379 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001380 }
1381 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001382 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001383 return err;
1384}
1385
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001386/**
Ying Xue247f0f32014-02-18 16:06:46 +08001387 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001388 * @m: descriptor for message info
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001389 * @buflen: length of user buffer area
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001391 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1393 * If the complete message doesn't fit in user area, truncate it.
1394 *
1395 * Returns size of returned message data, errno otherwise
1396 */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001397static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
1398 size_t buflen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001399{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001400 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001401 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001402 struct sk_buff *skb;
1403 struct tipc_msg *hdr;
1404 bool connected = !tipc_sk_type_connectionless(sk);
1405 int rc, err, hlen, dlen, copy;
1406 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001407
Allan Stephens0c3141e2008-04-15 00:22:02 -07001408 /* Catch invalid receive requests */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001409 if (unlikely(!buflen))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001410 return -EINVAL;
1411
Allan Stephens0c3141e2008-04-15 00:22:02 -07001412 lock_sock(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001413 if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
1414 rc = -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001415 goto exit;
1416 }
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001417 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001419 do {
1420 /* Look at first msg in receive queue; wait if necessary */
1421 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1422 if (unlikely(rc))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001423 goto exit;
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001424 skb = skb_peek(&sk->sk_receive_queue);
1425 hdr = buf_msg(skb);
1426 dlen = msg_data_sz(hdr);
1427 hlen = msg_hdr_sz(hdr);
1428 err = msg_errcode(hdr);
1429 if (likely(dlen || err))
1430 break;
1431 tsk_advance_rx_queue(sk);
1432 } while (1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001433
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001434 /* Collect msg meta data, including error code and rejected data */
1435 set_orig_addr(m, hdr);
1436 rc = tipc_sk_anc_data_recv(m, hdr, tsk);
1437 if (unlikely(rc))
1438 goto exit;
1439
1440 /* Capture data if non-error msg, otherwise just set return value */
1441 if (likely(!err)) {
1442 copy = min_t(int, dlen, buflen);
1443 if (unlikely(copy != dlen))
1444 m->msg_flags |= MSG_TRUNC;
1445 rc = skb_copy_datagram_msg(skb, hlen, m, copy);
1446 } else {
1447 copy = 0;
1448 rc = 0;
1449 if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control)
1450 rc = -ECONNRESET;
1451 }
1452 if (unlikely(rc))
1453 goto exit;
1454
1455 /* Caption of data or error code/rejected data was successful */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001456 if (unlikely(flags & MSG_PEEK))
1457 goto exit;
1458
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001459 tsk_advance_rx_queue(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001460 if (likely(!connected))
1461 goto exit;
1462
1463 /* Send connection flow control ack when applicable */
1464 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1465 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
1466 tipc_sk_send_ack(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001467exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001468 release_sock(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001469 return rc ? rc : copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001470}
1471
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001472/**
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001473 * tipc_recvstream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001474 * @m: descriptor for message info
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001475 * @buflen: total size of user buffer area
Per Lidenb97bf3f2006-01-02 19:04:38 +01001476 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001477 *
1478 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479 * will optionally wait for more; never truncates data.
1480 *
1481 * Returns size of returned message data, errno otherwise
1482 */
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001483static int tipc_recvstream(struct socket *sock, struct msghdr *m,
1484 size_t buflen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001485{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001486 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001487 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001488 struct sk_buff *skb;
1489 struct tipc_msg *hdr;
1490 struct tipc_skb_cb *skb_cb;
1491 bool peek = flags & MSG_PEEK;
1492 int offset, required, copy, copied = 0;
1493 int hlen, dlen, err, rc;
1494 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001495
1496 /* Catch invalid receive attempts */
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001497 if (unlikely(!buflen))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001498 return -EINVAL;
1499
Allan Stephens0c3141e2008-04-15 00:22:02 -07001500 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001501
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001502 if (unlikely(sk->sk_state == TIPC_OPEN)) {
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001503 rc = -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001504 goto exit;
1505 }
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001506 required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
1507 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001508
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001509 do {
1510 /* Look at first msg in receive queue; wait if necessary */
1511 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1512 if (unlikely(rc))
1513 break;
1514 skb = skb_peek(&sk->sk_receive_queue);
1515 skb_cb = TIPC_SKB_CB(skb);
1516 hdr = buf_msg(skb);
1517 dlen = msg_data_sz(hdr);
1518 hlen = msg_hdr_sz(hdr);
1519 err = msg_errcode(hdr);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001520
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001521 /* Discard any empty non-errored (SYN-) message */
1522 if (unlikely(!dlen && !err)) {
1523 tsk_advance_rx_queue(sk);
1524 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001525 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001526
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001527 /* Collect msg meta data, incl. error code and rejected data */
1528 if (!copied) {
1529 set_orig_addr(m, hdr);
1530 rc = tipc_sk_anc_data_recv(m, hdr, tsk);
1531 if (rc)
1532 break;
1533 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001534
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001535 /* Copy data if msg ok, otherwise return error/partial data */
1536 if (likely(!err)) {
1537 offset = skb_cb->bytes_read;
1538 copy = min_t(int, dlen - offset, buflen - copied);
1539 rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
1540 if (unlikely(rc))
1541 break;
1542 copied += copy;
1543 offset += copy;
1544 if (unlikely(offset < dlen)) {
1545 if (!peek)
1546 skb_cb->bytes_read = offset;
1547 break;
1548 }
1549 } else {
1550 rc = 0;
1551 if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control)
1552 rc = -ECONNRESET;
1553 if (copied || rc)
1554 break;
1555 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001556
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001557 if (unlikely(peek))
1558 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001559
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001560 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001561
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001562 /* Send connection flow control advertisement when applicable */
1563 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1564 if (unlikely(tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE))
1565 tipc_sk_send_ack(tsk);
1566
1567 /* Exit if all requested data or FIN/error received */
1568 if (copied == buflen || err)
1569 break;
1570
1571 } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001572exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001573 release_sock(sk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001574 return copied ? copied : rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001575}
1576
1577/**
Ying Xuef288bef2012-08-21 11:16:57 +08001578 * tipc_write_space - wake up thread if port congestion is released
1579 * @sk: socket
1580 */
1581static void tipc_write_space(struct sock *sk)
1582{
1583 struct socket_wq *wq;
1584
1585 rcu_read_lock();
1586 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001587 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001588 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1589 POLLWRNORM | POLLWRBAND);
1590 rcu_read_unlock();
1591}
1592
1593/**
1594 * tipc_data_ready - wake up threads to indicate messages have been received
1595 * @sk: socket
1596 * @len: the length of messages
1597 */
David S. Miller676d2362014-04-11 16:15:36 -04001598static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001599{
1600 struct socket_wq *wq;
1601
1602 rcu_read_lock();
1603 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001604 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001605 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1606 POLLRDNORM | POLLRDBAND);
1607 rcu_read_unlock();
1608}
1609
Ying Xuef4195d12015-11-22 15:46:05 +08001610static void tipc_sock_destruct(struct sock *sk)
1611{
1612 __skb_queue_purge(&sk->sk_receive_queue);
1613}
1614
Jon Maloy64ac5f52017-10-13 11:04:20 +02001615static void tipc_sk_proto_rcv(struct sock *sk,
1616 struct sk_buff_head *inputq,
1617 struct sk_buff_head *xmitq)
1618{
1619 struct sk_buff *skb = __skb_dequeue(inputq);
1620 struct tipc_sock *tsk = tipc_sk(sk);
1621 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy75da2162017-10-13 11:04:23 +02001622 struct tipc_group *grp = tsk->group;
Jon Maloy64ac5f52017-10-13 11:04:20 +02001623
1624 switch (msg_user(hdr)) {
1625 case CONN_MANAGER:
1626 tipc_sk_conn_proto_rcv(tsk, skb, xmitq);
1627 return;
1628 case SOCK_WAKEUP:
Jon Maloya80ae532017-10-13 11:04:22 +02001629 tipc_dest_del(&tsk->cong_links, msg_orignode(hdr), 0);
Jon Maloy64ac5f52017-10-13 11:04:20 +02001630 tsk->cong_link_cnt--;
1631 sk->sk_write_space(sk);
1632 break;
Jon Maloy75da2162017-10-13 11:04:23 +02001633 case GROUP_PROTOCOL:
1634 tipc_group_proto_rcv(grp, hdr, xmitq);
1635 break;
Jon Maloy64ac5f52017-10-13 11:04:20 +02001636 case TOP_SRV:
Jon Maloy75da2162017-10-13 11:04:23 +02001637 tipc_group_member_evt(tsk->group, skb, xmitq);
1638 skb = NULL;
Jon Maloy64ac5f52017-10-13 11:04:20 +02001639 break;
1640 default:
1641 break;
1642 }
1643
1644 kfree_skb(skb);
1645}
1646
Ying Xuef288bef2012-08-21 11:16:57 +08001647/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02001648 * tipc_filter_connect - Handle incoming message for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001649 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001650 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001651 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001652 * Returns true if everything ok, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001653 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02001654static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001655{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001656 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001657 struct net *net = sock_net(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001658 struct tipc_msg *hdr = buf_msg(skb);
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001659 u32 pport = msg_origport(hdr);
1660 u32 pnode = msg_orignode(hdr);
Ying Xue7e6c1312012-11-29 18:39:14 -05001661
Jon Paul Maloycda36962015-07-22 10:11:20 -04001662 if (unlikely(msg_mcast(hdr)))
1663 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001664
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001665 switch (sk->sk_state) {
1666 case TIPC_CONNECTING:
Ying Xue7e6c1312012-11-29 18:39:14 -05001667 /* Accept only ACK or NACK message */
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001668 if (unlikely(!msg_connected(hdr))) {
1669 if (pport != tsk_peer_port(tsk) ||
1670 pnode != tsk_peer_node(tsk))
1671 return false;
1672
1673 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1674 sk->sk_err = ECONNREFUSED;
1675 sk->sk_state_change(sk);
1676 return true;
1677 }
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001678
Jon Paul Maloycda36962015-07-22 10:11:20 -04001679 if (unlikely(msg_errcode(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001680 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugne2c8d8512013-08-28 09:29:58 +02001681 sk->sk_err = ECONNREFUSED;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001682 sk->sk_state_change(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001683 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001684 }
1685
Jon Paul Maloycda36962015-07-22 10:11:20 -04001686 if (unlikely(!msg_isdata(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001687 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001688 sk->sk_err = EINVAL;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001689 sk->sk_state_change(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001690 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001691 }
1692
Jon Paul Maloycda36962015-07-22 10:11:20 -04001693 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1694 msg_set_importance(&tsk->phdr, msg_importance(hdr));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001695
Jon Paul Maloycda36962015-07-22 10:11:20 -04001696 /* If 'ACK+' message, add to socket receive queue */
1697 if (msg_data_sz(hdr))
1698 return true;
1699
1700 /* If empty 'ACK-' message, wake up sleeping connect() */
Parthasarathy Bhuvaragan42b531d2017-04-26 10:05:00 +02001701 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001702
1703 /* 'ACK-' message is neither accepted nor rejected: */
1704 msg_set_dest_droppable(hdr, 1);
1705 return false;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001706
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001707 case TIPC_OPEN:
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001708 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001709 break;
1710 case TIPC_LISTEN:
Ying Xue7e6c1312012-11-29 18:39:14 -05001711 /* Accept only SYN message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001712 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1713 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001714 break;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001715 case TIPC_ESTABLISHED:
1716 /* Accept only connection-based messages sent by peer */
1717 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1718 return false;
1719
1720 if (unlikely(msg_errcode(hdr))) {
1721 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1722 /* Let timer expire on it's own */
1723 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1724 tsk->portid);
1725 sk->sk_state_change(sk);
1726 }
1727 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001728 default:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001729 pr_err("Unknown sk_state %u\n", sk->sk_state);
Ying Xue7e6c1312012-11-29 18:39:14 -05001730 }
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001731
Jon Paul Maloycda36962015-07-22 10:11:20 -04001732 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001733}
1734
1735/**
Ying Xueaba79f32013-01-20 23:30:09 +01001736 * rcvbuf_limit - get proper overload limit of socket receive queue
1737 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001738 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01001739 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001740 * For connection oriented messages, irrespective of importance,
1741 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01001742 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001743 * For connectionless messages, queue limits are based on message
1744 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01001745 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001746 * TIPC_LOW_IMPORTANCE (2 MB)
1747 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1748 * TIPC_HIGH_IMPORTANCE (8 MB)
1749 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001750 *
1751 * Returns overload limit according to corresponding message importance
1752 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001753static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01001754{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001755 struct tipc_sock *tsk = tipc_sk(sk);
1756 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01001757
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001758 if (unlikely(!msg_connected(hdr)))
1759 return sk->sk_rcvbuf << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08001760
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001761 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1762 return sk->sk_rcvbuf;
1763
1764 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01001765}
1766
1767/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02001768 * tipc_sk_filter_rcv - validate incoming message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001769 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04001770 * @skb: pointer to message.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001771 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001772 * Enqueues message on receive queue if acceptable; optionally handles
1773 * disconnect indication for a connected socket.
1774 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001775 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001776 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001777 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02001778static void tipc_sk_filter_rcv(struct sock *sk, struct sk_buff *skb,
1779 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001780{
Jon Maloy64ac5f52017-10-13 11:04:20 +02001781 bool sk_conn = !tipc_sk_type_connectionless(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001782 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy75da2162017-10-13 11:04:23 +02001783 struct tipc_group *grp = tsk->group;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001784 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy64ac5f52017-10-13 11:04:20 +02001785 struct net *net = sock_net(sk);
1786 struct sk_buff_head inputq;
1787 int limit, err = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001788
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001789 TIPC_SKB_CB(skb)->bytes_read = 0;
Jon Maloy64ac5f52017-10-13 11:04:20 +02001790 __skb_queue_head_init(&inputq);
1791 __skb_queue_tail(&inputq, skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001792
Jon Maloy64ac5f52017-10-13 11:04:20 +02001793 if (unlikely(!msg_isdata(hdr)))
1794 tipc_sk_proto_rcv(sk, &inputq, xmitq);
Jon Maloy75da2162017-10-13 11:04:23 +02001795 else if (unlikely(msg_type(hdr) > TIPC_GRP_BCAST_MSG))
Jon Maloy64ac5f52017-10-13 11:04:20 +02001796 return kfree_skb(skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001797
Jon Maloy75da2162017-10-13 11:04:23 +02001798 if (unlikely(grp))
1799 tipc_group_filter_msg(grp, &inputq, xmitq);
1800
Jon Maloy64ac5f52017-10-13 11:04:20 +02001801 /* Validate and add to receive buffer if there is space */
1802 while ((skb = __skb_dequeue(&inputq))) {
1803 hdr = buf_msg(skb);
1804 limit = rcvbuf_limit(sk, skb);
1805 if ((sk_conn && !tipc_sk_filter_connect(tsk, skb)) ||
Jon Maloy75da2162017-10-13 11:04:23 +02001806 (!sk_conn && msg_connected(hdr)) ||
1807 (!grp && msg_in_group(hdr)))
Jon Maloy64ac5f52017-10-13 11:04:20 +02001808 err = TIPC_ERR_NO_PORT;
1809 else if (sk_rmem_alloc_get(sk) + skb->truesize >= limit)
1810 err = TIPC_ERR_OVERLOAD;
1811
1812 if (unlikely(err)) {
1813 tipc_skb_reject(net, err, skb, xmitq);
1814 err = TIPC_OK;
1815 continue;
1816 }
1817 __skb_queue_tail(&sk->sk_receive_queue, skb);
1818 skb_set_owner_r(skb, sk);
1819 sk->sk_data_ready(sk);
1820 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001821}
1822
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001823/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02001824 * tipc_sk_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001825 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001826 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001827 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001828 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001829 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02001830static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001831{
Jon Maloy64ac5f52017-10-13 11:04:20 +02001832 unsigned int before = sk_rmem_alloc_get(sk);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001833 struct sk_buff_head xmitq;
Jon Maloy64ac5f52017-10-13 11:04:20 +02001834 unsigned int added;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001835
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001836 __skb_queue_head_init(&xmitq);
1837
Jon Maloy64ac5f52017-10-13 11:04:20 +02001838 tipc_sk_filter_rcv(sk, skb, &xmitq);
1839 added = sk_rmem_alloc_get(sk) - before;
1840 atomic_add(added, &tipc_sk(sk)->dupl_rcvcnt);
1841
1842 /* Send pending response/rejected messages, if any */
Jon Maloyf70d37b2017-10-13 11:04:21 +02001843 tipc_node_distr_xmit(sock_net(sk), &xmitq);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001844 return 0;
1845}
1846
1847/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001848 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1849 * inputq and try adding them to socket or backlog queue
1850 * @inputq: list of incoming buffers with potentially different destinations
1851 * @sk: socket where the buffers should be enqueued
1852 * @dport: port number for the socket
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001853 *
1854 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001855 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001856static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001857 u32 dport, struct sk_buff_head *xmitq)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001858{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001859 unsigned long time_limit = jiffies + 2;
1860 struct sk_buff *skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001861 unsigned int lim;
1862 atomic_t *dcnt;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001863 u32 onode;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001864
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001865 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05001866 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001867 return;
1868
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001869 skb = tipc_skb_dequeue(inputq, dport);
1870 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001871 return;
1872
1873 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001874 if (!sock_owned_by_user(sk)) {
Jon Maloy64ac5f52017-10-13 11:04:20 +02001875 tipc_sk_filter_rcv(sk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001876 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001877 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001878
1879 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001880 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04001881 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001882 atomic_set(dcnt, 0);
1883 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1884 if (likely(!sk_add_backlog(sk, skb, lim)))
1885 continue;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001886
1887 /* Overload => reject message back to sender */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001888 onode = tipc_own_addr(sock_net(sk));
1889 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1890 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001891 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001892 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001893}
1894
1895/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001896 * tipc_sk_rcv - handle a chain of incoming buffers
1897 * @inputq: buffer list containing the buffers
1898 * Consumes all buffers in list until inputq is empty
1899 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001900 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001901void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001902{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001903 struct sk_buff_head xmitq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001904 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04001905 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001906 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001907 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001908 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001909
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001910 __skb_queue_head_init(&xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001911 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001912 dport = tipc_skb_peek_port(inputq, dport);
1913 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001914
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001915 if (likely(tsk)) {
1916 sk = &tsk->sk;
1917 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001918 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001919 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001920 }
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001921 /* Send pending response/rejected messages, if any */
Jon Maloyf70d37b2017-10-13 11:04:21 +02001922 tipc_node_distr_xmit(sock_net(sk), &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001923 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001924 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001925 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001926 /* No destination socket => dequeue skb if still there */
1927 skb = tipc_skb_dequeue(inputq, dport);
1928 if (!skb)
1929 return;
1930
1931 /* Try secondary lookup if unresolved named message */
1932 err = TIPC_ERR_NO_PORT;
1933 if (tipc_msg_lookup_dest(net, skb, &err))
1934 goto xmit;
1935
1936 /* Prepare for message rejection */
1937 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001938 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001939xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001940 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001941 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001942 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001943}
1944
Ying Xue78eb3a52014-01-17 09:50:03 +08001945static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1946{
WANG Congd9dc8b02016-11-11 10:20:50 -08001947 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Ying Xue78eb3a52014-01-17 09:50:03 +08001948 struct sock *sk = sock->sk;
Ying Xue78eb3a52014-01-17 09:50:03 +08001949 int done;
1950
1951 do {
1952 int err = sock_error(sk);
1953 if (err)
1954 return err;
1955 if (!*timeo_p)
1956 return -ETIMEDOUT;
1957 if (signal_pending(current))
1958 return sock_intr_errno(*timeo_p);
1959
WANG Congd9dc8b02016-11-11 10:20:50 -08001960 add_wait_queue(sk_sleep(sk), &wait);
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001961 done = sk_wait_event(sk, timeo_p,
WANG Congd9dc8b02016-11-11 10:20:50 -08001962 sk->sk_state != TIPC_CONNECTING, &wait);
1963 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue78eb3a52014-01-17 09:50:03 +08001964 } while (!done);
1965 return 0;
1966}
1967
Per Lidenb97bf3f2006-01-02 19:04:38 +01001968/**
Ying Xue247f0f32014-02-18 16:06:46 +08001969 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001970 * @sock: socket structure
1971 * @dest: socket address for destination port
1972 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001973 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001974 *
1975 * Returns 0 on success, errno otherwise
1976 */
Ying Xue247f0f32014-02-18 16:06:46 +08001977static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1978 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001979{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001980 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001981 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001982 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1983 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001984 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001985 int previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001986 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001987
Jon Maloy23998832017-10-13 11:04:18 +02001988 if (destlen != sizeof(struct sockaddr_tipc))
1989 return -EINVAL;
1990
Allan Stephens0c3141e2008-04-15 00:22:02 -07001991 lock_sock(sk);
1992
Jon Maloy75da2162017-10-13 11:04:23 +02001993 if (tsk->group) {
1994 res = -EINVAL;
1995 goto exit;
1996 }
1997
Jon Maloy23998832017-10-13 11:04:18 +02001998 if (dst->family == AF_UNSPEC) {
1999 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
2000 if (!tipc_sk_type_connectionless(sk))
2001 res = -EINVAL;
2002 goto exit;
2003 } else if (dst->family != AF_TIPC) {
2004 res = -EINVAL;
2005 }
2006 if (dst->addrtype != TIPC_ADDR_ID && dst->addrtype != TIPC_ADDR_NAME)
2007 res = -EINVAL;
2008 if (res)
2009 goto exit;
2010
Erik Hugnef2f80362015-03-19 09:02:19 +01002011 /* DGRAM/RDM connect(), just save the destaddr */
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01002012 if (tipc_sk_type_connectionless(sk)) {
Jon Maloy23998832017-10-13 11:04:18 +02002013 memcpy(&tsk->peer, dest, destlen);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002014 goto exit;
2015 }
2016
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002017 previous = sk->sk_state;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01002018
2019 switch (sk->sk_state) {
2020 case TIPC_OPEN:
Ying Xue584d24b2012-11-29 18:51:19 -05002021 /* Send a 'SYN-' to destination */
2022 m.msg_name = dest;
2023 m.msg_namelen = destlen;
2024
2025 /* If connect is in non-blocking case, set MSG_DONTWAIT to
2026 * indicate send_msg() is never blocked.
2027 */
2028 if (!timeout)
2029 m.msg_flags = MSG_DONTWAIT;
2030
Ying Xue39a02952015-03-02 15:37:47 +08002031 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05002032 if ((res < 0) && (res != -EWOULDBLOCK))
2033 goto exit;
2034
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002035 /* Just entered TIPC_CONNECTING state; the only
Ying Xue584d24b2012-11-29 18:51:19 -05002036 * difference is that return value in non-blocking
2037 * case is EINPROGRESS, rather than EALREADY.
2038 */
2039 res = -EINPROGRESS;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002040 /* fall thru' */
2041 case TIPC_CONNECTING:
2042 if (!timeout) {
2043 if (previous == TIPC_CONNECTING)
2044 res = -EALREADY;
Ying Xue78eb3a52014-01-17 09:50:03 +08002045 goto exit;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002046 }
Ying Xue78eb3a52014-01-17 09:50:03 +08002047 timeout = msecs_to_jiffies(timeout);
2048 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
2049 res = tipc_wait_for_connect(sock, &timeout);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002050 break;
2051 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002052 res = -EISCONN;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002053 break;
2054 default:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002055 res = -EINVAL;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01002056 }
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01002057
Allan Stephens0c3141e2008-04-15 00:22:02 -07002058exit:
2059 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07002060 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002061}
2062
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002063/**
Ying Xue247f0f32014-02-18 16:06:46 +08002064 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01002065 * @sock: socket structure
2066 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002067 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002068 * Returns 0 on success, errno otherwise
2069 */
Ying Xue247f0f32014-02-18 16:06:46 +08002070static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002071{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002072 struct sock *sk = sock->sk;
2073 int res;
2074
2075 lock_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002076 res = tipc_set_sk_state(sk, TIPC_LISTEN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002077 release_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002078
Allan Stephens0c3141e2008-04-15 00:22:02 -07002079 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002080}
2081
Ying Xue6398e232014-01-17 09:50:04 +08002082static int tipc_wait_for_accept(struct socket *sock, long timeo)
2083{
2084 struct sock *sk = sock->sk;
2085 DEFINE_WAIT(wait);
2086 int err;
2087
2088 /* True wake-one mechanism for incoming connections: only
2089 * one process gets woken up, not the 'whole herd'.
2090 * Since we do not 'race & poll' for established sockets
2091 * anymore, the common case will execute the loop only once.
2092 */
2093 for (;;) {
2094 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2095 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01002096 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08002097 release_sock(sk);
2098 timeo = schedule_timeout(timeo);
2099 lock_sock(sk);
2100 }
2101 err = 0;
2102 if (!skb_queue_empty(&sk->sk_receive_queue))
2103 break;
Ying Xue6398e232014-01-17 09:50:04 +08002104 err = -EAGAIN;
2105 if (!timeo)
2106 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002107 err = sock_intr_errno(timeo);
2108 if (signal_pending(current))
2109 break;
Ying Xue6398e232014-01-17 09:50:04 +08002110 }
2111 finish_wait(sk_sleep(sk), &wait);
2112 return err;
2113}
2114
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002115/**
Ying Xue247f0f32014-02-18 16:06:46 +08002116 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002117 * @sock: listening socket
2118 * @newsock: new socket that is to be connected
2119 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002120 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002121 * Returns 0 on success, errno otherwise
2122 */
David Howellscdfbabf2017-03-09 08:09:05 +00002123static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
2124 bool kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002125{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002126 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002127 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002128 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002129 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002130 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002131 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002132
Allan Stephens0c3141e2008-04-15 00:22:02 -07002133 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002134
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002135 if (sk->sk_state != TIPC_LISTEN) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002136 res = -EINVAL;
2137 goto exit;
2138 }
Ying Xue6398e232014-01-17 09:50:04 +08002139 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2140 res = tipc_wait_for_accept(sock, timeo);
2141 if (res)
2142 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002143
2144 buf = skb_peek(&sk->sk_receive_queue);
2145
David Howellscdfbabf2017-03-09 08:09:05 +00002146 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002147 if (res)
2148 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002149 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002150
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002151 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002152 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002153 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002154
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002155 /* we lock on new_sk; but lockdep sees the lock on sk */
2156 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002157
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002158 /*
2159 * Reject any stray messages received by new socket
2160 * before the socket lock was taken (very, very unlikely)
2161 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002162 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002163
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002164 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002165 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002166
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002167 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002168 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002169 new_tsock->conn_type = msg_nametype(msg);
2170 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002171 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002172
2173 /*
2174 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2175 * Respond to 'SYN+' by queuing it on new socket.
2176 */
2177 if (!msg_data_sz(msg)) {
2178 struct msghdr m = {NULL,};
2179
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002180 tsk_advance_rx_queue(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002181 __tipc_sendstream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002182 } else {
2183 __skb_dequeue(&sk->sk_receive_queue);
2184 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002185 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002186 }
2187 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002188exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002189 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002190 return res;
2191}
2192
2193/**
Ying Xue247f0f32014-02-18 16:06:46 +08002194 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002195 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002196 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002197 *
2198 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002199 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002200 * Returns 0 on success, errno otherwise
2201 */
Ying Xue247f0f32014-02-18 16:06:46 +08002202static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002203{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002204 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002205 int res;
2206
Allan Stephense247a8f2008-03-06 15:05:38 -08002207 if (how != SHUT_RDWR)
2208 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002209
Allan Stephens0c3141e2008-04-15 00:22:02 -07002210 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002211
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002212 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2213 sk->sk_shutdown = SEND_SHUTDOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002214
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002215 if (sk->sk_state == TIPC_DISCONNECTING) {
Ying Xue75031152012-10-29 09:38:15 -04002216 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002217 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002218
2219 /* Wake up anyone sleeping in poll */
2220 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002221 res = 0;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002222 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002223 res = -ENOTCONN;
2224 }
2225
Allan Stephens0c3141e2008-04-15 00:22:02 -07002226 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002227 return res;
2228}
2229
Ying Xuef2f2a962015-01-09 15:27:02 +08002230static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002231{
Ying Xuef2f2a962015-01-09 15:27:02 +08002232 struct tipc_sock *tsk = (struct tipc_sock *)data;
2233 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002234 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002235 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002236 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002237
Jon Paul Maloy57289012014-08-22 18:09:09 -04002238 bh_lock_sock(sk);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002239 if (!tipc_sk_connected(sk)) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002240 bh_unlock_sock(sk);
2241 goto exit;
2242 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002243 peer_port = tsk_peer_port(tsk);
2244 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002245
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002246 if (tsk->probe_unacked) {
Erik Hugneb3be5e32015-06-09 17:27:12 +02002247 if (!sock_owned_by_user(sk)) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01002248 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugneb3be5e32015-06-09 17:27:12 +02002249 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2250 tsk_peer_port(tsk));
2251 sk->sk_state_change(sk);
2252 } else {
2253 /* Try again later */
2254 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2255 }
2256
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002257 bh_unlock_sock(sk);
2258 goto exit;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002259 }
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002260
2261 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2262 INT_H_SIZE, 0, peer_node, own_node,
2263 peer_port, tsk->portid, TIPC_OK);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002264 tsk->probe_unacked = true;
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002265 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002266 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002267 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002268 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002269exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002270 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002271}
2272
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002273static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002274 struct tipc_name_seq const *seq)
2275{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002276 struct sock *sk = &tsk->sk;
2277 struct net *net = sock_net(sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002278 struct publication *publ;
2279 u32 key;
2280
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002281 if (tipc_sk_connected(sk))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002282 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002283 key = tsk->portid + tsk->pub_count + 1;
2284 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002285 return -EADDRINUSE;
2286
Ying Xuef2f98002015-01-09 15:27:05 +08002287 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002288 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002289 if (unlikely(!publ))
2290 return -EINVAL;
2291
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002292 list_add(&publ->pport_list, &tsk->publications);
2293 tsk->pub_count++;
2294 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002295 return 0;
2296}
2297
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002298static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002299 struct tipc_name_seq const *seq)
2300{
Ying Xuef2f98002015-01-09 15:27:05 +08002301 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002302 struct publication *publ;
2303 struct publication *safe;
2304 int rc = -EINVAL;
2305
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002306 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002307 if (seq) {
2308 if (publ->scope != scope)
2309 continue;
2310 if (publ->type != seq->type)
2311 continue;
2312 if (publ->lower != seq->lower)
2313 continue;
2314 if (publ->upper != seq->upper)
2315 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002316 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002317 publ->ref, publ->key);
2318 rc = 0;
2319 break;
2320 }
Ying Xuef2f98002015-01-09 15:27:05 +08002321 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002322 publ->ref, publ->key);
2323 rc = 0;
2324 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002325 if (list_empty(&tsk->publications))
2326 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002327 return rc;
2328}
2329
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002330/* tipc_sk_reinit: set non-zero address in all existing sockets
2331 * when we go from standalone to network mode.
2332 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002333void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002334{
Ying Xuee05b31f2015-01-09 15:27:08 +08002335 struct tipc_net *tn = net_generic(net, tipc_net_id);
Herbert Xu40f9f432017-02-11 19:26:46 +08002336 struct rhashtable_iter iter;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002337 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002338 struct tipc_msg *msg;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002339
Herbert Xu40f9f432017-02-11 19:26:46 +08002340 rhashtable_walk_enter(&tn->sk_rht, &iter);
2341
2342 do {
2343 tsk = ERR_PTR(rhashtable_walk_start(&iter));
Bob Peterson6c7e9832017-08-23 10:43:02 -04002344 if (IS_ERR(tsk))
2345 goto walk_stop;
Herbert Xu40f9f432017-02-11 19:26:46 +08002346
2347 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002348 spin_lock_bh(&tsk->sk.sk_lock.slock);
2349 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002350 msg_set_prevnode(msg, tn->own_addr);
2351 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002352 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2353 }
Bob Peterson6c7e9832017-08-23 10:43:02 -04002354walk_stop:
Herbert Xu40f9f432017-02-11 19:26:46 +08002355 rhashtable_walk_stop(&iter);
2356 } while (tsk == ERR_PTR(-EAGAIN));
Ying Xue07f6c4b2015-01-07 13:41:58 +08002357}
2358
Ying Xuee05b31f2015-01-09 15:27:08 +08002359static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002360{
Ying Xuee05b31f2015-01-09 15:27:08 +08002361 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002362 struct tipc_sock *tsk;
2363
2364 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002365 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002366 if (tsk)
2367 sock_hold(&tsk->sk);
2368 rcu_read_unlock();
2369
2370 return tsk;
2371}
2372
2373static int tipc_sk_insert(struct tipc_sock *tsk)
2374{
Ying Xuee05b31f2015-01-09 15:27:08 +08002375 struct sock *sk = &tsk->sk;
2376 struct net *net = sock_net(sk);
2377 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002378 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2379 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2380
2381 while (remaining--) {
2382 portid++;
2383 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2384 portid = TIPC_MIN_PORT;
2385 tsk->portid = portid;
2386 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002387 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2388 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002389 return 0;
2390 sock_put(&tsk->sk);
2391 }
2392
2393 return -1;
2394}
2395
2396static void tipc_sk_remove(struct tipc_sock *tsk)
2397{
2398 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002399 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002400
Herbert Xu6cca72892015-03-20 21:57:05 +11002401 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Reshetova, Elena41c6d652017-06-30 13:08:01 +03002402 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002403 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002404 }
2405}
2406
Herbert Xu6cca72892015-03-20 21:57:05 +11002407static const struct rhashtable_params tsk_rht_params = {
2408 .nelem_hint = 192,
2409 .head_offset = offsetof(struct tipc_sock, node),
2410 .key_offset = offsetof(struct tipc_sock, portid),
2411 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11002412 .max_size = 1048576,
2413 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002414 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11002415};
2416
Ying Xuee05b31f2015-01-09 15:27:08 +08002417int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002418{
Ying Xuee05b31f2015-01-09 15:27:08 +08002419 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002420
Herbert Xu6cca72892015-03-20 21:57:05 +11002421 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002422}
2423
Ying Xuee05b31f2015-01-09 15:27:08 +08002424void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002425{
Ying Xuee05b31f2015-01-09 15:27:08 +08002426 struct tipc_net *tn = net_generic(net, tipc_net_id);
2427
Ying Xue07f6c4b2015-01-07 13:41:58 +08002428 /* Wait for socket readers to complete */
2429 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002430
Ying Xuee05b31f2015-01-09 15:27:08 +08002431 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002432}
2433
Jon Maloy75da2162017-10-13 11:04:23 +02002434static int tipc_sk_join(struct tipc_sock *tsk, struct tipc_group_req *mreq)
2435{
2436 struct net *net = sock_net(&tsk->sk);
2437 u32 domain = addr_domain(net, mreq->scope);
2438 struct tipc_group *grp = tsk->group;
2439 struct tipc_msg *hdr = &tsk->phdr;
2440 struct tipc_name_seq seq;
2441 int rc;
2442
2443 if (mreq->type < TIPC_RESERVED_TYPES)
2444 return -EACCES;
2445 if (grp)
2446 return -EACCES;
2447 grp = tipc_group_create(net, tsk->portid, mreq);
2448 if (!grp)
2449 return -ENOMEM;
2450 tsk->group = grp;
2451 msg_set_lookup_scope(hdr, mreq->scope);
2452 msg_set_nametype(hdr, mreq->type);
2453 msg_set_dest_droppable(hdr, true);
2454 seq.type = mreq->type;
2455 seq.lower = mreq->instance;
2456 seq.upper = seq.lower;
2457 tipc_nametbl_build_group(net, grp, mreq->type, domain);
2458 rc = tipc_sk_publish(tsk, mreq->scope, &seq);
2459 if (rc)
2460 tipc_group_delete(net, grp);
2461 return rc;
2462}
2463
2464static int tipc_sk_leave(struct tipc_sock *tsk)
2465{
2466 struct net *net = sock_net(&tsk->sk);
2467 struct tipc_group *grp = tsk->group;
2468 struct tipc_name_seq seq;
2469 int scope;
2470
2471 if (!grp)
2472 return -EINVAL;
2473 tipc_group_self(grp, &seq, &scope);
2474 tipc_group_delete(net, grp);
2475 tsk->group = NULL;
2476 tipc_sk_withdraw(tsk, scope, &seq);
2477 return 0;
2478}
2479
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002480/**
Ying Xue247f0f32014-02-18 16:06:46 +08002481 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002482 * @sock: socket structure
2483 * @lvl: option level
2484 * @opt: option identifier
2485 * @ov: pointer to new option value
2486 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002487 *
2488 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002489 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002490 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002491 * Returns 0 on success, errno otherwise
2492 */
Ying Xue247f0f32014-02-18 16:06:46 +08002493static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2494 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002495{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002496 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002497 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy75da2162017-10-13 11:04:23 +02002498 struct tipc_group_req mreq;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002499 u32 value = 0;
Dan Carpentera08ef472017-01-24 12:49:35 +03002500 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002501
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002502 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2503 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002504 if (lvl != SOL_TIPC)
2505 return -ENOPROTOOPT;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002506
2507 switch (opt) {
2508 case TIPC_IMPORTANCE:
2509 case TIPC_SRC_DROPPABLE:
2510 case TIPC_DEST_DROPPABLE:
2511 case TIPC_CONN_TIMEOUT:
2512 if (ol < sizeof(value))
2513 return -EINVAL;
Jon Maloy75da2162017-10-13 11:04:23 +02002514 if (get_user(value, (u32 __user *)ov))
2515 return -EFAULT;
2516 break;
2517 case TIPC_GROUP_JOIN:
2518 if (ol < sizeof(mreq))
2519 return -EINVAL;
2520 if (copy_from_user(&mreq, ov, sizeof(mreq)))
2521 return -EFAULT;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002522 break;
2523 default:
2524 if (ov || ol)
2525 return -EINVAL;
2526 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002527
Allan Stephens0c3141e2008-04-15 00:22:02 -07002528 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002529
Per Lidenb97bf3f2006-01-02 19:04:38 +01002530 switch (opt) {
2531 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002532 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002533 break;
2534 case TIPC_SRC_DROPPABLE:
2535 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002536 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002537 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002538 res = -ENOPROTOOPT;
2539 break;
2540 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002541 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002542 break;
2543 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002544 tipc_sk(sk)->conn_timeout = value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002545 break;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002546 case TIPC_MCAST_BROADCAST:
2547 tsk->mc_method.rcast = false;
2548 tsk->mc_method.mandatory = true;
2549 break;
2550 case TIPC_MCAST_REPLICAST:
2551 tsk->mc_method.rcast = true;
2552 tsk->mc_method.mandatory = true;
2553 break;
Jon Maloy75da2162017-10-13 11:04:23 +02002554 case TIPC_GROUP_JOIN:
2555 res = tipc_sk_join(tsk, &mreq);
2556 break;
2557 case TIPC_GROUP_LEAVE:
2558 res = tipc_sk_leave(tsk);
2559 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002560 default:
2561 res = -EINVAL;
2562 }
2563
Allan Stephens0c3141e2008-04-15 00:22:02 -07002564 release_sock(sk);
2565
Per Lidenb97bf3f2006-01-02 19:04:38 +01002566 return res;
2567}
2568
2569/**
Ying Xue247f0f32014-02-18 16:06:46 +08002570 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002571 * @sock: socket structure
2572 * @lvl: option level
2573 * @opt: option identifier
2574 * @ov: receptacle for option value
2575 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002576 *
2577 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002578 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002579 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002580 * Returns 0 on success, errno otherwise
2581 */
Ying Xue247f0f32014-02-18 16:06:46 +08002582static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2583 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002584{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002585 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002586 struct tipc_sock *tsk = tipc_sk(sk);
Jon Maloy75da2162017-10-13 11:04:23 +02002587 struct tipc_name_seq seq;
2588 int len, scope;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002589 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002590 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002591
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002592 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2593 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002594 if (lvl != SOL_TIPC)
2595 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002596 res = get_user(len, ol);
2597 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002598 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002599
Allan Stephens0c3141e2008-04-15 00:22:02 -07002600 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002601
2602 switch (opt) {
2603 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002604 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002605 break;
2606 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002607 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002608 break;
2609 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002610 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002611 break;
2612 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002613 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002614 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002615 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002616 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002617 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002618 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002619 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002620 value = skb_queue_len(&sk->sk_receive_queue);
2621 break;
Jon Maloy75da2162017-10-13 11:04:23 +02002622 case TIPC_GROUP_JOIN:
2623 seq.type = 0;
2624 if (tsk->group)
2625 tipc_group_self(tsk->group, &seq, &scope);
2626 value = seq.type;
2627 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002628 default:
2629 res = -EINVAL;
2630 }
2631
Allan Stephens0c3141e2008-04-15 00:22:02 -07002632 release_sock(sk);
2633
Paul Gortmaker25860c32010-12-31 18:59:31 +00002634 if (res)
2635 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002636
Paul Gortmaker25860c32010-12-31 18:59:31 +00002637 if (len < sizeof(value))
2638 return -EINVAL;
2639
2640 if (copy_to_user(ov, &value, sizeof(value)))
2641 return -EFAULT;
2642
2643 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002644}
2645
Ying Xuef2f98002015-01-09 15:27:05 +08002646static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002647{
Ying Xuef2f98002015-01-09 15:27:05 +08002648 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002649 struct tipc_sioc_ln_req lnr;
2650 void __user *argp = (void __user *)arg;
2651
2652 switch (cmd) {
2653 case SIOCGETLINKNAME:
2654 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2655 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002656 if (!tipc_node_get_linkname(sock_net(sk),
2657 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002658 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2659 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2660 return -EFAULT;
2661 return 0;
2662 }
2663 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002664 default:
2665 return -ENOIOCTLCMD;
2666 }
2667}
2668
Erik Hugne70b03752017-03-29 11:22:16 +02002669static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
2670{
2671 struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
2672 struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
Erik Hugne66bc1e82017-03-29 11:22:17 +02002673 u32 onode = tipc_own_addr(sock_net(sock1->sk));
Erik Hugne70b03752017-03-29 11:22:16 +02002674
Erik Hugne66bc1e82017-03-29 11:22:17 +02002675 tsk1->peer.family = AF_TIPC;
2676 tsk1->peer.addrtype = TIPC_ADDR_ID;
2677 tsk1->peer.scope = TIPC_NODE_SCOPE;
2678 tsk1->peer.addr.id.ref = tsk2->portid;
2679 tsk1->peer.addr.id.node = onode;
2680 tsk2->peer.family = AF_TIPC;
2681 tsk2->peer.addrtype = TIPC_ADDR_ID;
2682 tsk2->peer.scope = TIPC_NODE_SCOPE;
2683 tsk2->peer.addr.id.ref = tsk1->portid;
2684 tsk2->peer.addr.id.node = onode;
2685
2686 tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
2687 tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
Erik Hugne70b03752017-03-29 11:22:16 +02002688 return 0;
2689}
2690
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002691/* Protocol switches for the various types of TIPC sockets */
2692
Florian Westphalbca65ea2008-02-07 18:18:01 -08002693static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002694 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002695 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002696 .release = tipc_release,
2697 .bind = tipc_bind,
2698 .connect = tipc_connect,
Erik Hugne66bc1e82017-03-29 11:22:17 +02002699 .socketpair = tipc_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002700 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002701 .getname = tipc_getname,
2702 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002703 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002704 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002705 .shutdown = tipc_shutdown,
2706 .setsockopt = tipc_setsockopt,
2707 .getsockopt = tipc_getsockopt,
2708 .sendmsg = tipc_sendmsg,
2709 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002710 .mmap = sock_no_mmap,
2711 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002712};
2713
Florian Westphalbca65ea2008-02-07 18:18:01 -08002714static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002715 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002716 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002717 .release = tipc_release,
2718 .bind = tipc_bind,
2719 .connect = tipc_connect,
Erik Hugne70b03752017-03-29 11:22:16 +02002720 .socketpair = tipc_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002721 .accept = tipc_accept,
2722 .getname = tipc_getname,
2723 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002724 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002725 .listen = tipc_listen,
2726 .shutdown = tipc_shutdown,
2727 .setsockopt = tipc_setsockopt,
2728 .getsockopt = tipc_getsockopt,
2729 .sendmsg = tipc_send_packet,
2730 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002731 .mmap = sock_no_mmap,
2732 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002733};
2734
Florian Westphalbca65ea2008-02-07 18:18:01 -08002735static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002736 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002737 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002738 .release = tipc_release,
2739 .bind = tipc_bind,
2740 .connect = tipc_connect,
Erik Hugne70b03752017-03-29 11:22:16 +02002741 .socketpair = tipc_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002742 .accept = tipc_accept,
2743 .getname = tipc_getname,
2744 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002745 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002746 .listen = tipc_listen,
2747 .shutdown = tipc_shutdown,
2748 .setsockopt = tipc_setsockopt,
2749 .getsockopt = tipc_getsockopt,
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002750 .sendmsg = tipc_sendstream,
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002751 .recvmsg = tipc_recvstream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002752 .mmap = sock_no_mmap,
2753 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002754};
2755
Florian Westphalbca65ea2008-02-07 18:18:01 -08002756static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002757 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002758 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002759 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002760};
2761
2762static struct proto tipc_proto = {
2763 .name = "TIPC",
2764 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002765 .obj_size = sizeof(struct tipc_sock),
2766 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002767};
2768
2769/**
Per Liden4323add2006-01-18 00:38:21 +01002770 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002771 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002772 * Returns 0 on success, errno otherwise
2773 */
Per Liden4323add2006-01-18 00:38:21 +01002774int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002775{
2776 int res;
2777
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002778 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002779 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002780 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002781 goto out;
2782 }
2783
2784 res = sock_register(&tipc_family_ops);
2785 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002786 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002787 proto_unregister(&tipc_proto);
2788 goto out;
2789 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002790 out:
2791 return res;
2792}
2793
2794/**
Per Liden4323add2006-01-18 00:38:21 +01002795 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002796 */
Per Liden4323add2006-01-18 00:38:21 +01002797void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002798{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002799 sock_unregister(tipc_family_ops.family);
2800 proto_unregister(&tipc_proto);
2801}
Richard Alpe34b78a122014-11-20 10:29:10 +01002802
2803/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002804static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002805{
2806 u32 peer_node;
2807 u32 peer_port;
2808 struct nlattr *nest;
2809
2810 peer_node = tsk_peer_node(tsk);
2811 peer_port = tsk_peer_port(tsk);
2812
2813 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2814
2815 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2816 goto msg_full;
2817 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2818 goto msg_full;
2819
2820 if (tsk->conn_type != 0) {
2821 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2822 goto msg_full;
2823 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2824 goto msg_full;
2825 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2826 goto msg_full;
2827 }
2828 nla_nest_end(skb, nest);
2829
2830 return 0;
2831
2832msg_full:
2833 nla_nest_cancel(skb, nest);
2834
2835 return -EMSGSIZE;
2836}
2837
2838/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002839static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2840 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002841{
2842 int err;
2843 void *hdr;
2844 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002845 struct net *net = sock_net(skb->sk);
2846 struct tipc_net *tn = net_generic(net, tipc_net_id);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002847 struct sock *sk = &tsk->sk;
Richard Alpe34b78a122014-11-20 10:29:10 +01002848
2849 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002850 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002851 if (!hdr)
2852 goto msg_cancel;
2853
2854 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2855 if (!attrs)
2856 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002857 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002858 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002859 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002860 goto attr_msg_cancel;
2861
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002862 if (tipc_sk_connected(sk)) {
Richard Alpe34b78a122014-11-20 10:29:10 +01002863 err = __tipc_nl_add_sk_con(skb, tsk);
2864 if (err)
2865 goto attr_msg_cancel;
2866 } else if (!list_empty(&tsk->publications)) {
2867 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2868 goto attr_msg_cancel;
2869 }
2870 nla_nest_end(skb, attrs);
2871 genlmsg_end(skb, hdr);
2872
2873 return 0;
2874
2875attr_msg_cancel:
2876 nla_nest_cancel(skb, attrs);
2877genlmsg_cancel:
2878 genlmsg_cancel(skb, hdr);
2879msg_cancel:
2880 return -EMSGSIZE;
2881}
2882
2883int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2884{
2885 int err;
2886 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002887 const struct bucket_table *tbl;
2888 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002889 struct net *net = sock_net(skb->sk);
2890 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002891 u32 tbl_id = cb->args[0];
2892 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002893
Ying Xue07f6c4b2015-01-07 13:41:58 +08002894 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002895 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002896 for (; tbl_id < tbl->size; tbl_id++) {
2897 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002898 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002899 if (prev_portid && prev_portid != tsk->portid) {
2900 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2901 continue;
2902 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002903
Richard Alped6e164e2015-01-16 12:30:40 +01002904 err = __tipc_nl_add_sk(skb, cb, tsk);
2905 if (err) {
2906 prev_portid = tsk->portid;
2907 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2908 goto out;
2909 }
2910 prev_portid = 0;
2911 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002912 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002913 }
Richard Alped6e164e2015-01-16 12:30:40 +01002914out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002915 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002916 cb->args[0] = tbl_id;
2917 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002918
2919 return skb->len;
2920}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002921
2922/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002923static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2924 struct netlink_callback *cb,
2925 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002926{
2927 void *hdr;
2928 struct nlattr *attrs;
2929
2930 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002931 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002932 if (!hdr)
2933 goto msg_cancel;
2934
2935 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2936 if (!attrs)
2937 goto genlmsg_cancel;
2938
2939 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2940 goto attr_msg_cancel;
2941 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2942 goto attr_msg_cancel;
2943 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2944 goto attr_msg_cancel;
2945 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2946 goto attr_msg_cancel;
2947
2948 nla_nest_end(skb, attrs);
2949 genlmsg_end(skb, hdr);
2950
2951 return 0;
2952
2953attr_msg_cancel:
2954 nla_nest_cancel(skb, attrs);
2955genlmsg_cancel:
2956 genlmsg_cancel(skb, hdr);
2957msg_cancel:
2958 return -EMSGSIZE;
2959}
2960
2961/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002962static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2963 struct netlink_callback *cb,
2964 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002965{
2966 int err;
2967 struct publication *p;
2968
2969 if (*last_publ) {
2970 list_for_each_entry(p, &tsk->publications, pport_list) {
2971 if (p->key == *last_publ)
2972 break;
2973 }
2974 if (p->key != *last_publ) {
2975 /* We never set seq or call nl_dump_check_consistent()
2976 * this means that setting prev_seq here will cause the
2977 * consistence check to fail in the netlink callback
2978 * handler. Resulting in the last NLMSG_DONE message
2979 * having the NLM_F_DUMP_INTR flag set.
2980 */
2981 cb->prev_seq = 1;
2982 *last_publ = 0;
2983 return -EPIPE;
2984 }
2985 } else {
2986 p = list_first_entry(&tsk->publications, struct publication,
2987 pport_list);
2988 }
2989
2990 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2991 err = __tipc_nl_add_sk_publ(skb, cb, p);
2992 if (err) {
2993 *last_publ = p->key;
2994 return err;
2995 }
2996 }
2997 *last_publ = 0;
2998
2999 return 0;
3000}
3001
3002int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
3003{
3004 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08003005 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01003006 u32 last_publ = cb->args[1];
3007 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08003008 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003009 struct tipc_sock *tsk;
3010
Ying Xue07f6c4b2015-01-07 13:41:58 +08003011 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01003012 struct nlattr **attrs;
3013 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
3014
3015 err = tipc_nlmsg_parse(cb->nlh, &attrs);
3016 if (err)
3017 return err;
3018
Richard Alpe45e093a2016-05-16 11:14:54 +02003019 if (!attrs[TIPC_NLA_SOCK])
3020 return -EINVAL;
3021
Richard Alpe1a1a1432014-11-20 10:29:11 +01003022 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
3023 attrs[TIPC_NLA_SOCK],
Johannes Bergfceb6432017-04-12 14:34:07 +02003024 tipc_nl_sock_policy, NULL);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003025 if (err)
3026 return err;
3027
3028 if (!sock[TIPC_NLA_SOCK_REF])
3029 return -EINVAL;
3030
Ying Xue07f6c4b2015-01-07 13:41:58 +08003031 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003032 }
3033
3034 if (done)
3035 return 0;
3036
Ying Xuee05b31f2015-01-09 15:27:08 +08003037 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003038 if (!tsk)
3039 return -EINVAL;
3040
3041 lock_sock(&tsk->sk);
3042 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
3043 if (!err)
3044 done = 1;
3045 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08003046 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01003047
Ying Xue07f6c4b2015-01-07 13:41:58 +08003048 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01003049 cb->args[1] = last_publ;
3050 cb->args[2] = done;
3051
3052 return skb->len;
3053}