blob: c7c6749344747e8526e5f6b06d861b2bcd2e1a9d [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 Paul Maloy51b9a312016-11-19 14:47:07 -05004 * Copyright (c) 2001-2007, 2012-2016, 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"
Erik Hugne2cf8aa12012-06-29 00:16:37 -040048
Ying Xue07f6c4b2015-01-07 13:41:58 +080049#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
Ying Xue2f55c432015-01-09 15:27:00 +080050#define CONN_PROBING_INTERVAL msecs_to_jiffies(3600000) /* [ms] => 1 h */
Ying Xue07f6c4b2015-01-07 13:41:58 +080051#define TIPC_FWD_MSG 1
Ying Xue07f6c4b2015-01-07 13:41:58 +080052#define TIPC_MAX_PORT 0xffffffff
53#define TIPC_MIN_PORT 1
Jon Paul Maloye9f8b102017-05-02 18:16:53 +020054#define TIPC_ACK_RATE 4 /* ACK at 1/4 of of rcv window size */
Jon Paul Maloy301bae52014-08-22 18:09:20 -040055
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010056enum {
57 TIPC_LISTEN = TCP_LISTEN,
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +010058 TIPC_ESTABLISHED = TCP_ESTABLISHED,
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +010059 TIPC_OPEN = TCP_CLOSE,
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +010060 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +010061 TIPC_CONNECTING = TCP_SYN_SENT,
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010062};
63
Jon Paul Maloy301bae52014-08-22 18:09:20 -040064/**
65 * struct tipc_sock - TIPC socket structure
66 * @sk: socket - interacts with 'port' and with user via the socket API
Jon Paul Maloy301bae52014-08-22 18:09:20 -040067 * @conn_type: TIPC type used when connection was established
68 * @conn_instance: TIPC instance used when connection was established
69 * @published: non-zero if port has one or more associated names
70 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Ying Xue07f6c4b2015-01-07 13:41:58 +080071 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040072 * @phdr: preformatted message header used when sending messages
Jon Paul Maloy365ad352017-01-03 10:55:11 -050073 * #cong_links: list of congested links
Jon Paul Maloy301bae52014-08-22 18:09:20 -040074 * @publications: list of publications for port
Jon Paul Maloy365ad352017-01-03 10:55:11 -050075 * @blocking_link: address of the congested link we are currently sleeping on
Jon Paul Maloy301bae52014-08-22 18:09:20 -040076 * @pub_count: total # of publications port has made during its lifetime
77 * @probing_state:
Jon Paul Maloy301bae52014-08-22 18:09:20 -040078 * @conn_timeout: the time we can wait for an unresponded setup request
79 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
Jon Paul Maloy365ad352017-01-03 10:55:11 -050080 * @cong_link_cnt: number of congested links
Jon Paul Maloy301bae52014-08-22 18:09:20 -040081 * @sent_unacked: # messages sent by socket, and not yet acked by peer
82 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +010083 * @peer: 'connected' peer for dgram/rdm
Ying Xue07f6c4b2015-01-07 13:41:58 +080084 * @node: hash table node
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -050085 * @mc_method: cookie for use between socket and broadcast layer
Ying Xue07f6c4b2015-01-07 13:41:58 +080086 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040087 */
88struct tipc_sock {
89 struct sock sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040090 u32 conn_type;
91 u32 conn_instance;
92 int published;
93 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080094 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040095 struct tipc_msg phdr;
Jon Paul Maloy365ad352017-01-03 10:55:11 -050096 struct list_head cong_links;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040097 struct list_head publications;
98 u32 pub_count;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040099 uint conn_timeout;
100 atomic_t dupl_rcvcnt;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100101 bool probe_unacked;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500102 u16 cong_link_cnt;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400103 u16 snt_unacked;
104 u16 snd_win;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400105 u16 peer_caps;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400106 u16 rcv_unacked;
107 u16 rcv_win;
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +0100108 struct sockaddr_tipc peer;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800109 struct rhash_head node;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500110 struct tipc_mc_method mc_method;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800111 struct rcu_head rcu;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400112};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113
Jon Maloy64ac5f52017-10-13 11:04:20 +0200114static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400115static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800116static void tipc_write_space(struct sock *sk);
Ying Xuef4195d12015-11-22 15:46:05 +0800117static void tipc_sock_destruct(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800118static int tipc_release(struct socket *sock);
David Howellscdfbabf2017-03-09 08:09:05 +0000119static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
120 bool kern);
Ying Xuef2f2a962015-01-09 15:27:02 +0800121static void tipc_sk_timeout(unsigned long data);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400122static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400123 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400124static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400125 struct tipc_name_seq const *seq);
Ying Xuee05b31f2015-01-09 15:27:08 +0800126static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800127static int tipc_sk_insert(struct tipc_sock *tsk);
128static void tipc_sk_remove(struct tipc_sock *tsk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500129static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
Ying Xue39a0295f2015-03-02 15:37:47 +0800130static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100131
Florian Westphalbca65ea2008-02-07 18:18:01 -0800132static const struct proto_ops packet_ops;
133static const struct proto_ops stream_ops;
134static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135static struct proto tipc_proto;
Herbert Xu6cca72892015-03-20 21:57:05 +1100136static const struct rhashtable_params tsk_rht_params;
137
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500138static u32 tsk_own_node(struct tipc_sock *tsk)
139{
140 return msg_prevnode(&tsk->phdr);
141}
142
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400143static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400144{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400145 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400146}
147
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400148static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400149{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400150 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400151}
152
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400153static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400154{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400155 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400156}
157
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400158static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400159{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400160 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400161}
162
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400163static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400164{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400165 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400166}
167
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400168static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400169{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400170 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400171}
172
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400173static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400174{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400175 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400176}
177
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400178static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400179{
180 if (imp > TIPC_CRITICAL_IMPORTANCE)
181 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400182 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400183 return 0;
184}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400185
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400186static struct tipc_sock *tipc_sk(const struct sock *sk)
187{
188 return container_of(sk, struct tipc_sock, sk);
189}
190
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400191static bool tsk_conn_cong(struct tipc_sock *tsk)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400192{
Jon Paul Maloy6998cc62016-11-24 18:47:07 -0500193 return tsk->snt_unacked > tsk->snd_win;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400194}
195
196/* tsk_blocks(): translate a buffer size in bytes to number of
197 * advertisable blocks, taking into account the ratio truesize(len)/len
198 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
199 */
200static u16 tsk_adv_blocks(int len)
201{
202 return len / FLOWCTL_BLK_SZ / 4;
203}
204
205/* tsk_inc(): increment counter for sent or received data
206 * - If block based flow control is not supported by peer we
207 * fall back to message based ditto, incrementing the counter
208 */
209static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
210{
211 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
212 return ((msglen / FLOWCTL_BLK_SZ) + 1);
213 return 1;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400214}
215
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400217 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700218 *
219 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400221static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100222{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400223 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224}
225
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400226/* tipc_sk_respond() : send response message back to sender
227 */
228static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
229{
230 u32 selector;
231 u32 dnode;
232 u32 onode = tipc_own_addr(sock_net(sk));
233
234 if (!tipc_msg_reverse(onode, &skb, err))
235 return;
236
237 dnode = msg_destnode(buf_msg(skb));
238 selector = msg_origport(buf_msg(skb));
239 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
240}
241
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400243 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700244 *
245 * Caller must hold socket lock
246 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400247static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700248{
Ying Xuea6ca1092014-11-26 11:41:55 +0800249 struct sk_buff *skb;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700250
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400251 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
252 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700253}
254
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100255static bool tipc_sk_connected(struct sock *sk)
256{
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100257 return sk->sk_state == TIPC_ESTABLISHED;
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100258}
259
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +0100260/* tipc_sk_type_connectionless - check if the socket is datagram socket
261 * @sk: socket
262 *
263 * Returns true if connection less, false otherwise
264 */
265static bool tipc_sk_type_connectionless(struct sock *sk)
266{
267 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
268}
269
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400270/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400271 *
272 * Handles cases where the node's network address has changed from
273 * the default of <0.0.0> to its configured setting.
274 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400275static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400276{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100277 struct sock *sk = &tsk->sk;
278 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400279 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400280 u32 orig_node;
281 u32 peer_node;
282
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100283 if (unlikely(!tipc_sk_connected(sk)))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400284 return false;
285
286 if (unlikely(msg_origport(msg) != peer_port))
287 return false;
288
289 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400290 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400291
292 if (likely(orig_node == peer_node))
293 return true;
294
Ying Xue34747532015-01-09 15:27:10 +0800295 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400296 return true;
297
Ying Xue34747532015-01-09 15:27:10 +0800298 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400299 return true;
300
301 return false;
302}
303
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100304/* tipc_set_sk_state - set the sk_state of the socket
305 * @sk: socket
306 *
307 * Caller must hold socket lock
308 *
309 * Returns 0 on success, errno otherwise
310 */
311static int tipc_set_sk_state(struct sock *sk, int state)
312{
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100313 int oldsk_state = sk->sk_state;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100314 int res = -EINVAL;
315
316 switch (state) {
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100317 case TIPC_OPEN:
318 res = 0;
319 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100320 case TIPC_LISTEN:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100321 case TIPC_CONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100322 if (oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100323 res = 0;
324 break;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100325 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100326 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100327 oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100328 res = 0;
329 break;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100330 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100331 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100332 oldsk_state == TIPC_ESTABLISHED)
333 res = 0;
334 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100335 }
336
337 if (!res)
338 sk->sk_state = state;
339
340 return res;
341}
342
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500343static int tipc_sk_sock_err(struct socket *sock, long *timeout)
344{
345 struct sock *sk = sock->sk;
346 int err = sock_error(sk);
347 int typ = sock->type;
348
349 if (err)
350 return err;
351 if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
352 if (sk->sk_state == TIPC_DISCONNECTING)
353 return -EPIPE;
354 else if (!tipc_sk_connected(sk))
355 return -ENOTCONN;
356 }
357 if (!*timeout)
358 return -EAGAIN;
359 if (signal_pending(current))
360 return sock_intr_errno(*timeout);
361
362 return 0;
363}
364
Jon Paul Maloy844cf762017-05-11 20:28:15 +0200365#define tipc_wait_for_cond(sock_, timeo_, condition_) \
366({ \
367 struct sock *sk_; \
368 int rc_; \
369 \
370 while ((rc_ = !(condition_))) { \
371 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
372 sk_ = (sock_)->sk; \
373 rc_ = tipc_sk_sock_err((sock_), timeo_); \
374 if (rc_) \
375 break; \
376 prepare_to_wait(sk_sleep(sk_), &wait_, TASK_INTERRUPTIBLE); \
377 release_sock(sk_); \
378 *(timeo_) = wait_woken(&wait_, TASK_INTERRUPTIBLE, *(timeo_)); \
379 sched_annotate_sleep(); \
380 lock_sock(sk_); \
381 remove_wait_queue(sk_sleep(sk_), &wait_); \
382 } \
383 rc_; \
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500384})
385
Allan Stephens0c3141e2008-04-15 00:22:02 -0700386/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400387 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700388 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389 * @sock: pre-allocated socket structure
390 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800391 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900392 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700393 * This routine creates additional data structures used by the TIPC socket,
394 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 *
396 * Returns 0 on success, errno otherwise
397 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400398static int tipc_sk_create(struct net *net, struct socket *sock,
399 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100400{
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500401 struct tipc_net *tn;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700402 const struct proto_ops *ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400404 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400405 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700406
407 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408 if (unlikely(protocol != 0))
409 return -EPROTONOSUPPORT;
410
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411 switch (sock->type) {
412 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700413 ops = &stream_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414 break;
415 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700416 ops = &packet_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 break;
418 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700420 ops = &msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421 break;
Allan Stephens49978652006-06-25 23:47:18 -0700422 default:
Allan Stephens49978652006-06-25 23:47:18 -0700423 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 }
425
Allan Stephens0c3141e2008-04-15 00:22:02 -0700426 /* Allocate socket's protocol area */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500427 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700428 if (sk == NULL)
429 return -ENOMEM;
430
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400431 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400432 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400433 INIT_LIST_HEAD(&tsk->publications);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500434 INIT_LIST_HEAD(&tsk->cong_links);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400435 msg = &tsk->phdr;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500436 tn = net_generic(sock_net(sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437
Allan Stephens0c3141e2008-04-15 00:22:02 -0700438 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700439 sock->ops = ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100440 sock_init_data(sock, sk);
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100441 tipc_set_sk_state(sk, TIPC_OPEN);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800442 if (tipc_sk_insert(tsk)) {
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900443 pr_warn("Socket create failed; port number exhausted\n");
Ying Xue07f6c4b2015-01-07 13:41:58 +0800444 return -EINVAL;
445 }
Herbert Xu40f9f432017-02-11 19:26:46 +0800446
447 /* Ensure tsk is visible before we read own_addr. */
448 smp_mb();
449
450 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
451 NAMED_H_SIZE, 0);
452
Ying Xue07f6c4b2015-01-07 13:41:58 +0800453 msg_set_origport(msg, tsk->portid);
Ying Xue3721e9c2015-01-13 17:07:48 +0800454 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100455 sk->sk_shutdown = 0;
Jon Maloy64ac5f52017-10-13 11:04:20 +0200456 sk->sk_backlog_rcv = tipc_sk_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400457 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800458 sk->sk_data_ready = tipc_data_ready;
459 sk->sk_write_space = tipc_write_space;
Ying Xuef4195d12015-11-22 15:46:05 +0800460 sk->sk_destruct = tipc_sock_destruct;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400461 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
462 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700463
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400464 /* Start out with safe limits until we receive an advertised window */
465 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
466 tsk->rcv_win = tsk->snd_win;
467
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +0100468 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400469 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700470 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400471 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700472 }
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100473
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474 return 0;
475}
476
Ying Xue07f6c4b2015-01-07 13:41:58 +0800477static void tipc_sk_callback(struct rcu_head *head)
478{
479 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
480
481 sock_put(&tsk->sk);
482}
483
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100484/* Caller should hold socket lock for the socket. */
485static void __tipc_shutdown(struct socket *sock, int error)
486{
487 struct sock *sk = sock->sk;
488 struct tipc_sock *tsk = tipc_sk(sk);
489 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500490 long timeout = CONN_TIMEOUT_DEFAULT;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100491 u32 dnode = tsk_peer_node(tsk);
492 struct sk_buff *skb;
493
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500494 /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
495 tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
496 !tsk_conn_cong(tsk)));
497
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100498 /* Reject all unreceived messages, except on an active connection
499 * (which disconnects locally & sends a 'FIN+' to peer).
500 */
501 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
502 if (TIPC_SKB_CB(skb)->bytes_read) {
503 kfree_skb(skb);
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500504 continue;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100505 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500506 if (!tipc_sk_type_connectionless(sk) &&
507 sk->sk_state != TIPC_DISCONNECTING) {
508 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
509 tipc_node_remove_conn(net, dnode, tsk->portid);
510 }
511 tipc_sk_respond(sk, skb, error);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100512 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500513
514 if (tipc_sk_type_connectionless(sk))
515 return;
516
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100517 if (sk->sk_state != TIPC_DISCONNECTING) {
518 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
519 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
520 tsk_own_node(tsk), tsk_peer_port(tsk),
521 tsk->portid, error);
522 if (skb)
523 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500524 tipc_node_remove_conn(net, dnode, tsk->portid);
525 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100526 }
527}
528
Ying Xuec5fa7b32013-06-17 10:54:39 -0400529/**
Ying Xue247f0f32014-02-18 16:06:46 +0800530 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531 * @sock: socket to destroy
532 *
533 * This routine cleans up any messages that are still queued on the socket.
534 * For DGRAM and RDM socket types, all queued messages are rejected.
535 * For SEQPACKET and STREAM socket types, the first message is rejected
536 * and any others are discarded. (If the first message on a STREAM socket
537 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900538 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539 * NOTE: Rejected messages are not necessarily returned to the sender! They
540 * are returned or discarded according to the "destination droppable" setting
541 * specified for the message by the sender.
542 *
543 * Returns 0 on success, errno otherwise
544 */
Ying Xue247f0f32014-02-18 16:06:46 +0800545static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400548 struct tipc_sock *tsk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549
Allan Stephens0c3141e2008-04-15 00:22:02 -0700550 /*
551 * Exit if socket isn't fully initialized (occurs when a failed accept()
552 * releases a pre-allocated child socket that was never used)
553 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700554 if (sk == NULL)
555 return 0;
556
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400557 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700558 lock_sock(sk);
559
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100560 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
561 sk->sk_shutdown = SHUTDOWN_MASK;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400562 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue1ea23a22015-05-28 13:19:22 +0800563 sk_stop_timer(sk, &sk->sk_timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800564 tipc_sk_remove(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100565
Allan Stephens0c3141e2008-04-15 00:22:02 -0700566 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700567 release_sock(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500568 u32_list_purge(&tsk->cong_links);
569 tsk->cong_link_cnt = 0;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800570 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700571 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200573 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574}
575
576/**
Ying Xue247f0f32014-02-18 16:06:46 +0800577 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 * @sock: socket structure
579 * @uaddr: socket address describing name(s) and desired operation
580 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900581 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100582 * Name and name sequence binding is indicated using a positive scope value;
583 * a negative scope value unbinds the specified name. Specifying no name
584 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900585 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700587 *
588 * NOTE: This routine doesn't need to take the socket lock since it doesn't
589 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590 */
Ying Xue247f0f32014-02-18 16:06:46 +0800591static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
592 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593{
Ying Xue84602762013-12-27 10:18:28 +0800594 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100595 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400596 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800597 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598
Ying Xue84602762013-12-27 10:18:28 +0800599 lock_sock(sk);
600 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400601 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800602 goto exit;
603 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900604
Ying Xue84602762013-12-27 10:18:28 +0800605 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
606 res = -EINVAL;
607 goto exit;
608 }
609 if (addr->family != AF_TIPC) {
610 res = -EAFNOSUPPORT;
611 goto exit;
612 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613
Per Lidenb97bf3f2006-01-02 19:04:38 +0100614 if (addr->addrtype == TIPC_ADDR_NAME)
615 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800616 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
617 res = -EAFNOSUPPORT;
618 goto exit;
619 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900620
Ying Xue13a2e892013-06-17 10:54:40 -0400621 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400622 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800623 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
624 res = -EACCES;
625 goto exit;
626 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400627
Ying Xue84602762013-12-27 10:18:28 +0800628 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400629 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
630 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800631exit:
632 release_sock(sk);
633 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100634}
635
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900636/**
Ying Xue247f0f32014-02-18 16:06:46 +0800637 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100638 * @sock: socket structure
639 * @uaddr: area for returned socket address
640 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700641 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900642 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100643 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700644 *
Allan Stephens2da59912008-07-14 22:43:32 -0700645 * NOTE: This routine doesn't need to take the socket lock since it only
646 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000647 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100648 */
Ying Xue247f0f32014-02-18 16:06:46 +0800649static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
650 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100651{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100652 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100653 struct sock *sk = sock->sk;
654 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue34747532015-01-09 15:27:10 +0800655 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100656
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000657 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700658 if (peer) {
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100659 if ((!tipc_sk_connected(sk)) &&
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100660 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
Allan Stephens2da59912008-07-14 22:43:32 -0700661 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400662 addr->addr.id.ref = tsk_peer_port(tsk);
663 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700664 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800665 addr->addr.id.ref = tsk->portid;
Ying Xue34747532015-01-09 15:27:10 +0800666 addr->addr.id.node = tn->own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700667 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100668
669 *uaddr_len = sizeof(*addr);
670 addr->addrtype = TIPC_ADDR_ID;
671 addr->family = AF_TIPC;
672 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100673 addr->addr.name.domain = 0;
674
Allan Stephens0c3141e2008-04-15 00:22:02 -0700675 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100676}
677
678/**
Ying Xue247f0f32014-02-18 16:06:46 +0800679 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100680 * @file: file structure associated with the socket
681 * @sock: socket for which to calculate the poll bits
682 * @wait: ???
683 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700684 * Returns pollmask value
685 *
686 * COMMENTARY:
687 * It appears that the usual socket locking mechanisms are not useful here
688 * since the pollmask info is potentially out-of-date the moment this routine
689 * exits. TCP and other protocols seem to rely on higher level poll routines
690 * to handle any preventable race conditions, so TIPC will do the same ...
691 *
Allan Stephensf662c072010-08-17 11:00:06 +0000692 * IMPORTANT: The fact that a read or write operation is indicated does NOT
693 * imply that the operation will succeed, merely that it should be performed
694 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100695 */
Ying Xue247f0f32014-02-18 16:06:46 +0800696static unsigned int tipc_poll(struct file *file, struct socket *sock,
697 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698{
Allan Stephens9b674e82008-03-26 16:48:21 -0700699 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400700 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000701 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700702
Ying Xuef288bef2012-08-21 11:16:57 +0800703 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700704
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100705 if (sk->sk_shutdown & RCV_SHUTDOWN)
706 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
707 if (sk->sk_shutdown == SHUTDOWN_MASK)
708 mask |= POLLHUP;
709
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100710 switch (sk->sk_state) {
711 case TIPC_ESTABLISHED:
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500712 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000713 mask |= POLLOUT;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100714 /* fall thru' */
715 case TIPC_LISTEN:
716 case TIPC_CONNECTING:
Allan Stephensf662c072010-08-17 11:00:06 +0000717 if (!skb_queue_empty(&sk->sk_receive_queue))
718 mask |= (POLLIN | POLLRDNORM);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100719 break;
720 case TIPC_OPEN:
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500721 if (!tsk->cong_link_cnt)
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100722 mask |= POLLOUT;
723 if (tipc_sk_type_connectionless(sk) &&
724 (!skb_queue_empty(&sk->sk_receive_queue)))
725 mask |= (POLLIN | POLLRDNORM);
726 break;
727 case TIPC_DISCONNECTING:
728 mask = (POLLIN | POLLRDNORM | POLLHUP);
729 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000730 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700731
732 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100733}
734
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400735/**
736 * tipc_sendmcast - send multicast message
737 * @sock: socket structure
738 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500739 * @msg: message to send
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500740 * @dlen: length of data to send
741 * @timeout: timeout to wait for wakeup
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400742 *
743 * Called from function tipc_sendmsg(), which has done all sanity checks
744 * Returns the number of bytes sent on success, or errno
745 */
746static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500747 struct msghdr *msg, size_t dlen, long timeout)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400748{
749 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500750 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500751 struct tipc_msg *hdr = &tsk->phdr;
Ying Xuef2f98002015-01-09 15:27:05 +0800752 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500753 int mtu = tipc_bcast_get_mtu(net);
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500754 struct tipc_mc_method *method = &tsk->mc_method;
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500755 u32 domain = addr_domain(net, TIPC_CLUSTER_SCOPE);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500756 struct sk_buff_head pkts;
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500757 struct tipc_nlist dsts;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400758 int rc;
759
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500760 /* Block or return if any destination link is congested */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500761 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
762 if (unlikely(rc))
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400763 return rc;
764
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500765 /* Lookup destination nodes */
766 tipc_nlist_init(&dsts, tipc_own_addr(net));
767 tipc_nametbl_lookup_dst_nodes(net, seq->type, seq->lower,
768 seq->upper, domain, &dsts);
769 if (!dsts.local && !dsts.remote)
770 return -EHOSTUNREACH;
771
772 /* Build message header */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500773 msg_set_type(hdr, TIPC_MCAST_MSG);
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500774 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500775 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
776 msg_set_destport(hdr, 0);
777 msg_set_destnode(hdr, 0);
778 msg_set_nametype(hdr, seq->type);
779 msg_set_namelower(hdr, seq->lower);
780 msg_set_nameupper(hdr, seq->upper);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400781
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500782 /* Build message as chain of buffers */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500783 skb_queue_head_init(&pkts);
784 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500785
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500786 /* Send message if build was successful */
787 if (unlikely(rc == dlen))
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500788 rc = tipc_mcast_xmit(net, &pkts, method, &dsts,
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500789 &tsk->cong_link_cnt);
790
791 tipc_nlist_purge(&dsts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500792
793 return rc ? rc : dlen;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400794}
795
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500796/**
797 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
798 * @arrvq: queue with arriving messages, to be cloned after destination lookup
799 * @inputq: queue with cloned messages, delivered to socket after dest lookup
800 *
801 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400802 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500803void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
804 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400805{
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500806 struct tipc_msg *msg;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -0500807 struct list_head dports;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500808 u32 portid;
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400809 u32 scope = TIPC_CLUSTER_SCOPE;
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500810 struct sk_buff_head tmpq;
811 uint hsz;
812 struct sk_buff *skb, *_skb;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500813
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500814 __skb_queue_head_init(&tmpq);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -0500815 INIT_LIST_HEAD(&dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400816
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500817 skb = tipc_skb_peek(arrvq, &inputq->lock);
818 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
819 msg = buf_msg(skb);
820 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400821
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500822 if (in_own_node(net, msg_orignode(msg)))
823 scope = TIPC_NODE_SCOPE;
824
825 /* Create destination port list and message clones: */
826 tipc_nametbl_mc_translate(net,
827 msg_nametype(msg), msg_namelower(msg),
828 msg_nameupper(msg), scope, &dports);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -0500829 portid = u32_pop(&dports);
830 for (; portid; portid = u32_pop(&dports)) {
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500831 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
832 if (_skb) {
833 msg_set_destport(buf_msg(_skb), portid);
834 __skb_queue_tail(&tmpq, _skb);
835 continue;
836 }
837 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400838 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500839 /* Append to inputq if not already done by other thread */
840 spin_lock_bh(&inputq->lock);
841 if (skb_peek(arrvq) == skb) {
842 skb_queue_splice_tail_init(&tmpq, inputq);
843 kfree_skb(__skb_dequeue(arrvq));
844 }
845 spin_unlock_bh(&inputq->lock);
846 __skb_queue_purge(&tmpq);
847 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400848 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500849 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400850}
851
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900852/**
Jon Maloy64ac5f52017-10-13 11:04:20 +0200853 * tipc_sk_conn_proto_rcv - receive a connection mng protocol message
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500854 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400855 * @skb: pointer to message buffer.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500856 */
Jon Maloy64ac5f52017-10-13 11:04:20 +0200857static void tipc_sk_conn_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
858 struct sk_buff_head *xmitq)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500859{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400860 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy64ac5f52017-10-13 11:04:20 +0200861 u32 onode = tsk_own_node(tsk);
862 struct sock *sk = &tsk->sk;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400863 int mtyp = msg_type(hdr);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400864 bool conn_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400865
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500866 /* Ignore if connection cannot be validated: */
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400867 if (!tsk_peer_msg(tsk, hdr))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500868 goto exit;
869
Parthasarathy Bhuvaraganc1be7752017-04-26 10:05:02 +0200870 if (unlikely(msg_errcode(hdr))) {
871 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
872 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
873 tsk_peer_port(tsk));
874 sk->sk_state_change(sk);
875 goto exit;
876 }
877
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100878 tsk->probe_unacked = false;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500879
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400880 if (mtyp == CONN_PROBE) {
881 msg_set_type(hdr, CONN_PROBE_REPLY);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400882 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
883 __skb_queue_tail(xmitq, skb);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400884 return;
885 } else if (mtyp == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400886 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400887 tsk->snt_unacked -= msg_conn_ack(hdr);
888 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
889 tsk->snd_win = msg_adv_win(hdr);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500890 if (conn_cong)
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400891 sk->sk_write_space(sk);
892 } else if (mtyp != CONN_PROBE_REPLY) {
893 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500894 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500895exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400896 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500897}
898
Jon Maloy14c04492017-10-13 11:04:17 +0200899static void tipc_sk_top_evt(struct tipc_sock *tsk, struct tipc_event *evt)
900{
901}
902
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500903/**
Ying Xue247f0f32014-02-18 16:06:46 +0800904 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905 * @sock: socket structure
906 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500907 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900908 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100909 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900910 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100911 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
912 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900913 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914 * Returns the number of bytes sent on success, or errno otherwise
915 */
Ying Xue1b784142015-03-02 15:37:48 +0800916static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500917 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100918{
Ying Xue39a0295f2015-03-02 15:37:47 +0800919 struct sock *sk = sock->sk;
920 int ret;
921
922 lock_sock(sk);
923 ret = __tipc_sendmsg(sock, m, dsz);
924 release_sock(sk);
925
926 return ret;
927}
928
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500929static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a0295f2015-03-02 15:37:47 +0800930{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700931 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +0800932 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500933 struct tipc_sock *tsk = tipc_sk(sk);
934 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
935 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
936 struct list_head *clinks = &tsk->cong_links;
937 bool syn = !tipc_sk_type_connectionless(sk);
938 struct tipc_msg *hdr = &tsk->phdr;
Erik Hugnef2f80362015-03-19 09:02:19 +0100939 struct tipc_name_seq *seq;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500940 struct sk_buff_head pkts;
941 u32 type, inst, domain;
942 u32 dnode, dport;
943 int mtu, rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100944
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500945 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400946 return -EMSGSIZE;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500947
Erik Hugnef2f80362015-03-19 09:02:19 +0100948 if (unlikely(!dest)) {
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500949 dest = &tsk->peer;
950 if (!syn || dest->family != AF_TIPC)
Erik Hugnef2f80362015-03-19 09:02:19 +0100951 return -EDESTADDRREQ;
Erik Hugnef2f80362015-03-19 09:02:19 +0100952 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500953
954 if (unlikely(m->msg_namelen < sizeof(*dest)))
955 return -EINVAL;
956
957 if (unlikely(dest->family != AF_TIPC))
958 return -EINVAL;
959
960 if (unlikely(syn)) {
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100961 if (sk->sk_state == TIPC_LISTEN)
Ying Xue39a0295f2015-03-02 15:37:47 +0800962 return -EPIPE;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100963 if (sk->sk_state != TIPC_OPEN)
Ying Xue39a0295f2015-03-02 15:37:47 +0800964 return -EISCONN;
965 if (tsk->published)
966 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700967 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400968 tsk->conn_type = dest->addr.name.name.type;
969 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700970 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100971 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500972
Erik Hugnef2f80362015-03-19 09:02:19 +0100973 seq = &dest->addr.nameseq;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500974 if (dest->addrtype == TIPC_ADDR_MCAST)
975 return tipc_sendmcast(sock, seq, m, dlen, timeout);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500976
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500977 if (dest->addrtype == TIPC_ADDR_NAME) {
978 type = dest->addr.name.name.type;
979 inst = dest->addr.name.name.instance;
980 domain = dest->addr.name.domain;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500981 dnode = domain;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500982 msg_set_type(hdr, TIPC_NAMED_MSG);
983 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
984 msg_set_nametype(hdr, type);
985 msg_set_nameinst(hdr, inst);
986 msg_set_lookup_scope(hdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800987 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500988 msg_set_destnode(hdr, dnode);
989 msg_set_destport(hdr, dport);
Ying Xue39a0295f2015-03-02 15:37:47 +0800990 if (unlikely(!dport && !dnode))
991 return -EHOSTUNREACH;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500992
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500993 } else if (dest->addrtype == TIPC_ADDR_ID) {
994 dnode = dest->addr.id.node;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500995 msg_set_type(hdr, TIPC_DIRECT_MSG);
996 msg_set_lookup_scope(hdr, 0);
997 msg_set_destnode(hdr, dnode);
998 msg_set_destport(hdr, dest->addr.id.ref);
999 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001000 }
1001
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001002 /* Block or return if destination link is congested */
1003 rc = tipc_wait_for_cond(sock, &timeout, !u32_find(clinks, dnode));
1004 if (unlikely(rc))
Ying Xue39a0295f2015-03-02 15:37:47 +08001005 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001006
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001007 skb_queue_head_init(&pkts);
1008 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
1009 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1010 if (unlikely(rc != dlen))
1011 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001012
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001013 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1014 if (unlikely(rc == -ELINKCONG)) {
1015 u32_push(clinks, dnode);
1016 tsk->cong_link_cnt++;
1017 rc = 0;
1018 }
1019
1020 if (unlikely(syn && !rc))
1021 tipc_set_sk_state(sk, TIPC_CONNECTING);
1022
1023 return rc ? rc : dlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024}
1025
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001026/**
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001027 * tipc_sendstream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 * @sock: socket structure
1029 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001030 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001031 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001033 *
1034 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001035 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001036 */
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001037static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001038{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001039 struct sock *sk = sock->sk;
Ying Xue39a0295f2015-03-02 15:37:47 +08001040 int ret;
1041
1042 lock_sock(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001043 ret = __tipc_sendstream(sock, m, dsz);
Ying Xue39a0295f2015-03-02 15:37:47 +08001044 release_sock(sk);
1045
1046 return ret;
1047}
1048
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001049static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a0295f2015-03-02 15:37:47 +08001050{
1051 struct sock *sk = sock->sk;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001052 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001053 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1054 struct tipc_sock *tsk = tipc_sk(sk);
1055 struct tipc_msg *hdr = &tsk->phdr;
1056 struct net *net = sock_net(sk);
1057 struct sk_buff_head pkts;
1058 u32 dnode = tsk_peer_node(tsk);
1059 int send, sent = 0;
1060 int rc = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001061
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001062 skb_queue_head_init(&pkts);
1063
1064 if (unlikely(dlen > INT_MAX))
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001065 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001066
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001067 /* Handle implicit connection setup */
1068 if (unlikely(dest)) {
1069 rc = __tipc_sendmsg(sock, m, dlen);
1070 if (dlen && (dlen == rc))
1071 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
1072 return rc;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001073 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001074
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001075 do {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001076 rc = tipc_wait_for_cond(sock, &timeout,
1077 (!tsk->cong_link_cnt &&
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -05001078 !tsk_conn_cong(tsk) &&
1079 tipc_sk_connected(sk)));
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001080 if (unlikely(rc))
1081 break;
Ying Xue39a0295f2015-03-02 15:37:47 +08001082
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001083 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
1084 rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
1085 if (unlikely(rc != send))
1086 break;
1087
1088 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1089 if (unlikely(rc == -ELINKCONG)) {
1090 tsk->cong_link_cnt = 1;
1091 rc = 0;
1092 }
1093 if (likely(!rc)) {
1094 tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
1095 sent += send;
1096 }
1097 } while (sent < dlen && !rc);
1098
Parthasarathy Bhuvaragan3364d612017-04-24 15:00:42 +02001099 return sent ? sent : rc;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001100}
1101
1102/**
1103 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001104 * @sock: socket structure
1105 * @m: message to send
1106 * @dsz: length of data to be transmitted
1107 *
1108 * Used for SOCK_SEQPACKET messages.
1109 *
1110 * Returns the number of bytes sent on success, or errno otherwise
1111 */
Ying Xue1b784142015-03-02 15:37:48 +08001112static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001113{
1114 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1115 return -EMSGSIZE;
1116
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001117 return tipc_sendstream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001118}
1119
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001120/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001121 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001122static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001123 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124{
Ying Xue3721e9c2015-01-13 17:07:48 +08001125 struct sock *sk = &tsk->sk;
1126 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001127 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001128
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001129 msg_set_destnode(msg, peer_node);
1130 msg_set_destport(msg, peer_port);
1131 msg_set_type(msg, TIPC_CONN_MSG);
1132 msg_set_lookup_scope(msg, 0);
1133 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001134
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01001135 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001136 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
Ying Xuef2f98002015-01-09 15:27:05 +08001137 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1138 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001139 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001140 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1141 return;
1142
1143 /* Fall back to message based flow control */
1144 tsk->rcv_win = FLOWCTL_MSG_WIN;
1145 tsk->snd_win = FLOWCTL_MSG_WIN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146}
1147
1148/**
1149 * set_orig_addr - capture sender's address for received message
1150 * @m: descriptor for message info
1151 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001152 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153 * Note: Address is not captured if not requested by receiver.
1154 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001155static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001157 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001158
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001159 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160 addr->family = AF_TIPC;
1161 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001162 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 addr->addr.id.ref = msg_origport(msg);
1164 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001165 addr->addr.name.domain = 0; /* could leave uninitialized */
1166 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 m->msg_namelen = sizeof(struct sockaddr_tipc);
1168 }
1169}
1170
1171/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001172 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 * @m: descriptor for message info
1174 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001175 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001176 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001177 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001178 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179 * Returns 0 if successful, otherwise errno
1180 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001181static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1182 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001183{
1184 u32 anc_data[3];
1185 u32 err;
1186 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001187 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001188 int res;
1189
1190 if (likely(m->msg_controllen == 0))
1191 return 0;
1192
1193 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 err = msg ? msg_errcode(msg) : 0;
1195 if (unlikely(err)) {
1196 anc_data[0] = err;
1197 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001198 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1199 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001201 if (anc_data[1]) {
1202 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1203 msg_data(msg));
1204 if (res)
1205 return res;
1206 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001207 }
1208
1209 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001210 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1211 switch (dest_type) {
1212 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001213 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001214 anc_data[0] = msg_nametype(msg);
1215 anc_data[1] = msg_namelower(msg);
1216 anc_data[2] = msg_namelower(msg);
1217 break;
1218 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001219 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 anc_data[0] = msg_nametype(msg);
1221 anc_data[1] = msg_namelower(msg);
1222 anc_data[2] = msg_nameupper(msg);
1223 break;
1224 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001225 has_name = (tsk->conn_type != 0);
1226 anc_data[0] = tsk->conn_type;
1227 anc_data[1] = tsk->conn_instance;
1228 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001229 break;
1230 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001231 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232 }
Allan Stephens2db99832010-12-31 18:59:33 +00001233 if (has_name) {
1234 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1235 if (res)
1236 return res;
1237 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001238
1239 return 0;
1240}
1241
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001242static void tipc_sk_send_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001243{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001244 struct sock *sk = &tsk->sk;
1245 struct net *net = sock_net(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001246 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001247 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001248 u32 peer_port = tsk_peer_port(tsk);
1249 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001250
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001251 if (!tipc_sk_connected(sk))
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001252 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001253 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1254 dnode, tsk_own_node(tsk), peer_port,
1255 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001256 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001257 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001258 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001259 msg_set_conn_ack(msg, tsk->rcv_unacked);
1260 tsk->rcv_unacked = 0;
1261
1262 /* Adjust to and advertize the correct window limit */
1263 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1264 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1265 msg_set_adv_win(msg, tsk->rcv_win);
1266 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001267 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001268}
1269
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001270static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001271{
1272 struct sock *sk = sock->sk;
1273 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001274 long timeo = *timeop;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001275 int err = sock_error(sk);
1276
1277 if (err)
1278 return err;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001279
1280 for (;;) {
1281 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001282 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01001283 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001284 err = -ENOTCONN;
1285 break;
1286 }
1287 release_sock(sk);
1288 timeo = schedule_timeout(timeo);
1289 lock_sock(sk);
1290 }
1291 err = 0;
1292 if (!skb_queue_empty(&sk->sk_receive_queue))
1293 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001294 err = -EAGAIN;
1295 if (!timeo)
1296 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001297 err = sock_intr_errno(timeo);
1298 if (signal_pending(current))
1299 break;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001300
1301 err = sock_error(sk);
1302 if (err)
1303 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001304 }
1305 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001306 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001307 return err;
1308}
1309
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001310/**
Ying Xue247f0f32014-02-18 16:06:46 +08001311 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001312 * @m: descriptor for message info
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001313 * @buflen: length of user buffer area
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001315 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1317 * If the complete message doesn't fit in user area, truncate it.
1318 *
1319 * Returns size of returned message data, errno otherwise
1320 */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001321static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
1322 size_t buflen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001324 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001325 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001326 struct sk_buff *skb;
1327 struct tipc_msg *hdr;
1328 bool connected = !tipc_sk_type_connectionless(sk);
1329 int rc, err, hlen, dlen, copy;
1330 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001331
Allan Stephens0c3141e2008-04-15 00:22:02 -07001332 /* Catch invalid receive requests */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001333 if (unlikely(!buflen))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334 return -EINVAL;
1335
Allan Stephens0c3141e2008-04-15 00:22:02 -07001336 lock_sock(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001337 if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
1338 rc = -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 goto exit;
1340 }
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001341 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001343 do {
1344 /* Look at first msg in receive queue; wait if necessary */
1345 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1346 if (unlikely(rc))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001347 goto exit;
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001348 skb = skb_peek(&sk->sk_receive_queue);
1349 hdr = buf_msg(skb);
1350 dlen = msg_data_sz(hdr);
1351 hlen = msg_hdr_sz(hdr);
1352 err = msg_errcode(hdr);
1353 if (likely(dlen || err))
1354 break;
1355 tsk_advance_rx_queue(sk);
1356 } while (1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001357
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001358 /* Collect msg meta data, including error code and rejected data */
1359 set_orig_addr(m, hdr);
1360 rc = tipc_sk_anc_data_recv(m, hdr, tsk);
1361 if (unlikely(rc))
1362 goto exit;
1363
1364 /* Capture data if non-error msg, otherwise just set return value */
1365 if (likely(!err)) {
1366 copy = min_t(int, dlen, buflen);
1367 if (unlikely(copy != dlen))
1368 m->msg_flags |= MSG_TRUNC;
1369 rc = skb_copy_datagram_msg(skb, hlen, m, copy);
1370 } else {
1371 copy = 0;
1372 rc = 0;
1373 if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control)
1374 rc = -ECONNRESET;
1375 }
1376 if (unlikely(rc))
1377 goto exit;
1378
1379 /* Caption of data or error code/rejected data was successful */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001380 if (unlikely(flags & MSG_PEEK))
1381 goto exit;
1382
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001383 tsk_advance_rx_queue(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001384 if (likely(!connected))
1385 goto exit;
1386
1387 /* Send connection flow control ack when applicable */
1388 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1389 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
1390 tipc_sk_send_ack(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001391exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001392 release_sock(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001393 return rc ? rc : copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394}
1395
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001396/**
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001397 * tipc_recvstream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398 * @m: descriptor for message info
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001399 * @buflen: total size of user buffer area
Per Lidenb97bf3f2006-01-02 19:04:38 +01001400 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001401 *
1402 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 * will optionally wait for more; never truncates data.
1404 *
1405 * Returns size of returned message data, errno otherwise
1406 */
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001407static int tipc_recvstream(struct socket *sock, struct msghdr *m,
1408 size_t buflen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001409{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001410 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001411 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001412 struct sk_buff *skb;
1413 struct tipc_msg *hdr;
1414 struct tipc_skb_cb *skb_cb;
1415 bool peek = flags & MSG_PEEK;
1416 int offset, required, copy, copied = 0;
1417 int hlen, dlen, err, rc;
1418 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001419
1420 /* Catch invalid receive attempts */
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001421 if (unlikely(!buflen))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422 return -EINVAL;
1423
Allan Stephens0c3141e2008-04-15 00:22:02 -07001424 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001425
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001426 if (unlikely(sk->sk_state == TIPC_OPEN)) {
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001427 rc = -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001428 goto exit;
1429 }
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001430 required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
1431 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001432
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001433 do {
1434 /* Look at first msg in receive queue; wait if necessary */
1435 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1436 if (unlikely(rc))
1437 break;
1438 skb = skb_peek(&sk->sk_receive_queue);
1439 skb_cb = TIPC_SKB_CB(skb);
1440 hdr = buf_msg(skb);
1441 dlen = msg_data_sz(hdr);
1442 hlen = msg_hdr_sz(hdr);
1443 err = msg_errcode(hdr);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001444
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001445 /* Discard any empty non-errored (SYN-) message */
1446 if (unlikely(!dlen && !err)) {
1447 tsk_advance_rx_queue(sk);
1448 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001449 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001450
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001451 /* Collect msg meta data, incl. error code and rejected data */
1452 if (!copied) {
1453 set_orig_addr(m, hdr);
1454 rc = tipc_sk_anc_data_recv(m, hdr, tsk);
1455 if (rc)
1456 break;
1457 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001458
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001459 /* Copy data if msg ok, otherwise return error/partial data */
1460 if (likely(!err)) {
1461 offset = skb_cb->bytes_read;
1462 copy = min_t(int, dlen - offset, buflen - copied);
1463 rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
1464 if (unlikely(rc))
1465 break;
1466 copied += copy;
1467 offset += copy;
1468 if (unlikely(offset < dlen)) {
1469 if (!peek)
1470 skb_cb->bytes_read = offset;
1471 break;
1472 }
1473 } else {
1474 rc = 0;
1475 if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control)
1476 rc = -ECONNRESET;
1477 if (copied || rc)
1478 break;
1479 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001480
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001481 if (unlikely(peek))
1482 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001483
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001484 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001485
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001486 /* Send connection flow control advertisement when applicable */
1487 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1488 if (unlikely(tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE))
1489 tipc_sk_send_ack(tsk);
1490
1491 /* Exit if all requested data or FIN/error received */
1492 if (copied == buflen || err)
1493 break;
1494
1495 } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001496exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001497 release_sock(sk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001498 return copied ? copied : rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001499}
1500
1501/**
Ying Xuef288bef2012-08-21 11:16:57 +08001502 * tipc_write_space - wake up thread if port congestion is released
1503 * @sk: socket
1504 */
1505static void tipc_write_space(struct sock *sk)
1506{
1507 struct socket_wq *wq;
1508
1509 rcu_read_lock();
1510 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001511 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001512 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1513 POLLWRNORM | POLLWRBAND);
1514 rcu_read_unlock();
1515}
1516
1517/**
1518 * tipc_data_ready - wake up threads to indicate messages have been received
1519 * @sk: socket
1520 * @len: the length of messages
1521 */
David S. Miller676d2362014-04-11 16:15:36 -04001522static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001523{
1524 struct socket_wq *wq;
1525
1526 rcu_read_lock();
1527 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001528 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001529 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1530 POLLRDNORM | POLLRDBAND);
1531 rcu_read_unlock();
1532}
1533
Ying Xuef4195d12015-11-22 15:46:05 +08001534static void tipc_sock_destruct(struct sock *sk)
1535{
1536 __skb_queue_purge(&sk->sk_receive_queue);
1537}
1538
Jon Maloy64ac5f52017-10-13 11:04:20 +02001539static void tipc_sk_proto_rcv(struct sock *sk,
1540 struct sk_buff_head *inputq,
1541 struct sk_buff_head *xmitq)
1542{
1543 struct sk_buff *skb = __skb_dequeue(inputq);
1544 struct tipc_sock *tsk = tipc_sk(sk);
1545 struct tipc_msg *hdr = buf_msg(skb);
1546
1547 switch (msg_user(hdr)) {
1548 case CONN_MANAGER:
1549 tipc_sk_conn_proto_rcv(tsk, skb, xmitq);
1550 return;
1551 case SOCK_WAKEUP:
1552 u32_del(&tsk->cong_links, msg_orignode(hdr));
1553 tsk->cong_link_cnt--;
1554 sk->sk_write_space(sk);
1555 break;
1556 case TOP_SRV:
1557 tipc_sk_top_evt(tsk, (void *)msg_data(hdr));
1558 break;
1559 default:
1560 break;
1561 }
1562
1563 kfree_skb(skb);
1564}
1565
Ying Xuef288bef2012-08-21 11:16:57 +08001566/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02001567 * tipc_filter_connect - Handle incoming message for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001568 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001569 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001570 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001571 * Returns true if everything ok, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001572 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02001573static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001574{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001575 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001576 struct net *net = sock_net(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001577 struct tipc_msg *hdr = buf_msg(skb);
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001578 u32 pport = msg_origport(hdr);
1579 u32 pnode = msg_orignode(hdr);
Ying Xue7e6c1312012-11-29 18:39:14 -05001580
Jon Paul Maloycda36962015-07-22 10:11:20 -04001581 if (unlikely(msg_mcast(hdr)))
1582 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001583
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001584 switch (sk->sk_state) {
1585 case TIPC_CONNECTING:
Ying Xue7e6c1312012-11-29 18:39:14 -05001586 /* Accept only ACK or NACK message */
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001587 if (unlikely(!msg_connected(hdr))) {
1588 if (pport != tsk_peer_port(tsk) ||
1589 pnode != tsk_peer_node(tsk))
1590 return false;
1591
1592 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1593 sk->sk_err = ECONNREFUSED;
1594 sk->sk_state_change(sk);
1595 return true;
1596 }
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001597
Jon Paul Maloycda36962015-07-22 10:11:20 -04001598 if (unlikely(msg_errcode(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001599 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugne2c8d8512013-08-28 09:29:58 +02001600 sk->sk_err = ECONNREFUSED;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001601 sk->sk_state_change(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001602 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001603 }
1604
Jon Paul Maloycda36962015-07-22 10:11:20 -04001605 if (unlikely(!msg_isdata(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001606 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001607 sk->sk_err = EINVAL;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001608 sk->sk_state_change(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001609 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001610 }
1611
Jon Paul Maloycda36962015-07-22 10:11:20 -04001612 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1613 msg_set_importance(&tsk->phdr, msg_importance(hdr));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001614
Jon Paul Maloycda36962015-07-22 10:11:20 -04001615 /* If 'ACK+' message, add to socket receive queue */
1616 if (msg_data_sz(hdr))
1617 return true;
1618
1619 /* If empty 'ACK-' message, wake up sleeping connect() */
Parthasarathy Bhuvaragan42b531d2017-04-26 10:05:00 +02001620 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001621
1622 /* 'ACK-' message is neither accepted nor rejected: */
1623 msg_set_dest_droppable(hdr, 1);
1624 return false;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001625
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001626 case TIPC_OPEN:
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001627 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001628 break;
1629 case TIPC_LISTEN:
Ying Xue7e6c1312012-11-29 18:39:14 -05001630 /* Accept only SYN message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001631 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1632 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001633 break;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001634 case TIPC_ESTABLISHED:
1635 /* Accept only connection-based messages sent by peer */
1636 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1637 return false;
1638
1639 if (unlikely(msg_errcode(hdr))) {
1640 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1641 /* Let timer expire on it's own */
1642 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1643 tsk->portid);
1644 sk->sk_state_change(sk);
1645 }
1646 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001647 default:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001648 pr_err("Unknown sk_state %u\n", sk->sk_state);
Ying Xue7e6c1312012-11-29 18:39:14 -05001649 }
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001650
Jon Paul Maloycda36962015-07-22 10:11:20 -04001651 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001652}
1653
1654/**
Ying Xueaba79f32013-01-20 23:30:09 +01001655 * rcvbuf_limit - get proper overload limit of socket receive queue
1656 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001657 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01001658 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001659 * For connection oriented messages, irrespective of importance,
1660 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01001661 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001662 * For connectionless messages, queue limits are based on message
1663 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01001664 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001665 * TIPC_LOW_IMPORTANCE (2 MB)
1666 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1667 * TIPC_HIGH_IMPORTANCE (8 MB)
1668 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001669 *
1670 * Returns overload limit according to corresponding message importance
1671 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001672static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01001673{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001674 struct tipc_sock *tsk = tipc_sk(sk);
1675 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01001676
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001677 if (unlikely(!msg_connected(hdr)))
1678 return sk->sk_rcvbuf << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08001679
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001680 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1681 return sk->sk_rcvbuf;
1682
1683 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01001684}
1685
1686/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02001687 * tipc_sk_filter_rcv - validate incoming message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001688 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04001689 * @skb: pointer to message.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001690 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001691 * Enqueues message on receive queue if acceptable; optionally handles
1692 * disconnect indication for a connected socket.
1693 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001694 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001695 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001696 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02001697static void tipc_sk_filter_rcv(struct sock *sk, struct sk_buff *skb,
1698 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001699{
Jon Maloy64ac5f52017-10-13 11:04:20 +02001700 bool sk_conn = !tipc_sk_type_connectionless(sk);
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001701 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001702 struct tipc_msg *hdr = buf_msg(skb);
Jon Maloy64ac5f52017-10-13 11:04:20 +02001703 struct net *net = sock_net(sk);
1704 struct sk_buff_head inputq;
1705 int limit, err = TIPC_OK;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001706
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001707 TIPC_SKB_CB(skb)->bytes_read = 0;
Jon Maloy64ac5f52017-10-13 11:04:20 +02001708 __skb_queue_head_init(&inputq);
1709 __skb_queue_tail(&inputq, skb);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001710
Jon Maloy64ac5f52017-10-13 11:04:20 +02001711 if (unlikely(!msg_isdata(hdr)))
1712 tipc_sk_proto_rcv(sk, &inputq, xmitq);
1713 else if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG))
1714 return kfree_skb(skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001715
Jon Maloy64ac5f52017-10-13 11:04:20 +02001716 /* Validate and add to receive buffer if there is space */
1717 while ((skb = __skb_dequeue(&inputq))) {
1718 hdr = buf_msg(skb);
1719 limit = rcvbuf_limit(sk, skb);
1720 if ((sk_conn && !tipc_sk_filter_connect(tsk, skb)) ||
1721 (!sk_conn && msg_connected(hdr)))
1722 err = TIPC_ERR_NO_PORT;
1723 else if (sk_rmem_alloc_get(sk) + skb->truesize >= limit)
1724 err = TIPC_ERR_OVERLOAD;
1725
1726 if (unlikely(err)) {
1727 tipc_skb_reject(net, err, skb, xmitq);
1728 err = TIPC_OK;
1729 continue;
1730 }
1731 __skb_queue_tail(&sk->sk_receive_queue, skb);
1732 skb_set_owner_r(skb, sk);
1733 sk->sk_data_ready(sk);
1734 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001735}
1736
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001737/**
Jon Maloy64ac5f52017-10-13 11:04:20 +02001738 * tipc_sk_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001739 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001740 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001741 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001742 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001743 */
Jon Maloy64ac5f52017-10-13 11:04:20 +02001744static int tipc_sk_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001745{
Jon Maloy64ac5f52017-10-13 11:04:20 +02001746 unsigned int before = sk_rmem_alloc_get(sk);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001747 struct sk_buff_head xmitq;
Jon Maloy64ac5f52017-10-13 11:04:20 +02001748 unsigned int added;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001749
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001750 __skb_queue_head_init(&xmitq);
1751
Jon Maloy64ac5f52017-10-13 11:04:20 +02001752 tipc_sk_filter_rcv(sk, skb, &xmitq);
1753 added = sk_rmem_alloc_get(sk) - before;
1754 atomic_add(added, &tipc_sk(sk)->dupl_rcvcnt);
1755
1756 /* Send pending response/rejected messages, if any */
Jon Maloyf70d37b2017-10-13 11:04:21 +02001757 tipc_node_distr_xmit(sock_net(sk), &xmitq);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001758 return 0;
1759}
1760
1761/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001762 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1763 * inputq and try adding them to socket or backlog queue
1764 * @inputq: list of incoming buffers with potentially different destinations
1765 * @sk: socket where the buffers should be enqueued
1766 * @dport: port number for the socket
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001767 *
1768 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001769 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001770static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001771 u32 dport, struct sk_buff_head *xmitq)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001772{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001773 unsigned long time_limit = jiffies + 2;
1774 struct sk_buff *skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001775 unsigned int lim;
1776 atomic_t *dcnt;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001777 u32 onode;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001778
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001779 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05001780 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001781 return;
1782
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001783 skb = tipc_skb_dequeue(inputq, dport);
1784 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001785 return;
1786
1787 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001788 if (!sock_owned_by_user(sk)) {
Jon Maloy64ac5f52017-10-13 11:04:20 +02001789 tipc_sk_filter_rcv(sk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001790 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001791 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001792
1793 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001794 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04001795 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001796 atomic_set(dcnt, 0);
1797 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1798 if (likely(!sk_add_backlog(sk, skb, lim)))
1799 continue;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001800
1801 /* Overload => reject message back to sender */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001802 onode = tipc_own_addr(sock_net(sk));
1803 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1804 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001805 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001806 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001807}
1808
1809/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001810 * tipc_sk_rcv - handle a chain of incoming buffers
1811 * @inputq: buffer list containing the buffers
1812 * Consumes all buffers in list until inputq is empty
1813 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001814 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001815void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001816{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001817 struct sk_buff_head xmitq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001818 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04001819 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001820 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001821 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001822 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001823
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001824 __skb_queue_head_init(&xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001825 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001826 dport = tipc_skb_peek_port(inputq, dport);
1827 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001828
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001829 if (likely(tsk)) {
1830 sk = &tsk->sk;
1831 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001832 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001833 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001834 }
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001835 /* Send pending response/rejected messages, if any */
Jon Maloyf70d37b2017-10-13 11:04:21 +02001836 tipc_node_distr_xmit(sock_net(sk), &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001837 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001838 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001839 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001840
1841 /* No destination socket => dequeue skb if still there */
1842 skb = tipc_skb_dequeue(inputq, dport);
1843 if (!skb)
1844 return;
1845
1846 /* Try secondary lookup if unresolved named message */
1847 err = TIPC_ERR_NO_PORT;
1848 if (tipc_msg_lookup_dest(net, skb, &err))
1849 goto xmit;
1850
1851 /* Prepare for message rejection */
1852 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001853 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001854xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001855 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001856 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001857 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001858}
1859
Ying Xue78eb3a52014-01-17 09:50:03 +08001860static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1861{
WANG Congd9dc8b02016-11-11 10:20:50 -08001862 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Ying Xue78eb3a52014-01-17 09:50:03 +08001863 struct sock *sk = sock->sk;
Ying Xue78eb3a52014-01-17 09:50:03 +08001864 int done;
1865
1866 do {
1867 int err = sock_error(sk);
1868 if (err)
1869 return err;
1870 if (!*timeo_p)
1871 return -ETIMEDOUT;
1872 if (signal_pending(current))
1873 return sock_intr_errno(*timeo_p);
1874
WANG Congd9dc8b02016-11-11 10:20:50 -08001875 add_wait_queue(sk_sleep(sk), &wait);
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001876 done = sk_wait_event(sk, timeo_p,
WANG Congd9dc8b02016-11-11 10:20:50 -08001877 sk->sk_state != TIPC_CONNECTING, &wait);
1878 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue78eb3a52014-01-17 09:50:03 +08001879 } while (!done);
1880 return 0;
1881}
1882
Per Lidenb97bf3f2006-01-02 19:04:38 +01001883/**
Ying Xue247f0f32014-02-18 16:06:46 +08001884 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001885 * @sock: socket structure
1886 * @dest: socket address for destination port
1887 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001888 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001889 *
1890 * Returns 0 on success, errno otherwise
1891 */
Ying Xue247f0f32014-02-18 16:06:46 +08001892static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1893 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001894{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001895 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001896 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001897 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1898 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001899 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001900 int previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001901 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001902
Jon Maloy23998832017-10-13 11:04:18 +02001903 if (destlen != sizeof(struct sockaddr_tipc))
1904 return -EINVAL;
1905
Allan Stephens0c3141e2008-04-15 00:22:02 -07001906 lock_sock(sk);
1907
Jon Maloy23998832017-10-13 11:04:18 +02001908 if (dst->family == AF_UNSPEC) {
1909 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
1910 if (!tipc_sk_type_connectionless(sk))
1911 res = -EINVAL;
1912 goto exit;
1913 } else if (dst->family != AF_TIPC) {
1914 res = -EINVAL;
1915 }
1916 if (dst->addrtype != TIPC_ADDR_ID && dst->addrtype != TIPC_ADDR_NAME)
1917 res = -EINVAL;
1918 if (res)
1919 goto exit;
1920
Erik Hugnef2f80362015-03-19 09:02:19 +01001921 /* DGRAM/RDM connect(), just save the destaddr */
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +01001922 if (tipc_sk_type_connectionless(sk)) {
Jon Maloy23998832017-10-13 11:04:18 +02001923 memcpy(&tsk->peer, dest, destlen);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001924 goto exit;
1925 }
1926
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001927 previous = sk->sk_state;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001928
1929 switch (sk->sk_state) {
1930 case TIPC_OPEN:
Ying Xue584d24b2012-11-29 18:51:19 -05001931 /* Send a 'SYN-' to destination */
1932 m.msg_name = dest;
1933 m.msg_namelen = destlen;
1934
1935 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1936 * indicate send_msg() is never blocked.
1937 */
1938 if (!timeout)
1939 m.msg_flags = MSG_DONTWAIT;
1940
Ying Xue39a0295f2015-03-02 15:37:47 +08001941 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001942 if ((res < 0) && (res != -EWOULDBLOCK))
1943 goto exit;
1944
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001945 /* Just entered TIPC_CONNECTING state; the only
Ying Xue584d24b2012-11-29 18:51:19 -05001946 * difference is that return value in non-blocking
1947 * case is EINPROGRESS, rather than EALREADY.
1948 */
1949 res = -EINPROGRESS;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001950 /* fall thru' */
1951 case TIPC_CONNECTING:
1952 if (!timeout) {
1953 if (previous == TIPC_CONNECTING)
1954 res = -EALREADY;
Ying Xue78eb3a52014-01-17 09:50:03 +08001955 goto exit;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001956 }
Ying Xue78eb3a52014-01-17 09:50:03 +08001957 timeout = msecs_to_jiffies(timeout);
1958 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1959 res = tipc_wait_for_connect(sock, &timeout);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001960 break;
1961 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001962 res = -EISCONN;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001963 break;
1964 default:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001965 res = -EINVAL;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001966 }
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001967
Allan Stephens0c3141e2008-04-15 00:22:02 -07001968exit:
1969 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001970 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001971}
1972
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001973/**
Ying Xue247f0f32014-02-18 16:06:46 +08001974 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001975 * @sock: socket structure
1976 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001977 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001978 * Returns 0 on success, errno otherwise
1979 */
Ying Xue247f0f32014-02-18 16:06:46 +08001980static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001981{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001982 struct sock *sk = sock->sk;
1983 int res;
1984
1985 lock_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001986 res = tipc_set_sk_state(sk, TIPC_LISTEN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001987 release_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001988
Allan Stephens0c3141e2008-04-15 00:22:02 -07001989 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001990}
1991
Ying Xue6398e232014-01-17 09:50:04 +08001992static int tipc_wait_for_accept(struct socket *sock, long timeo)
1993{
1994 struct sock *sk = sock->sk;
1995 DEFINE_WAIT(wait);
1996 int err;
1997
1998 /* True wake-one mechanism for incoming connections: only
1999 * one process gets woken up, not the 'whole herd'.
2000 * Since we do not 'race & poll' for established sockets
2001 * anymore, the common case will execute the loop only once.
2002 */
2003 for (;;) {
2004 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2005 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01002006 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08002007 release_sock(sk);
2008 timeo = schedule_timeout(timeo);
2009 lock_sock(sk);
2010 }
2011 err = 0;
2012 if (!skb_queue_empty(&sk->sk_receive_queue))
2013 break;
Ying Xue6398e232014-01-17 09:50:04 +08002014 err = -EAGAIN;
2015 if (!timeo)
2016 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002017 err = sock_intr_errno(timeo);
2018 if (signal_pending(current))
2019 break;
Ying Xue6398e232014-01-17 09:50:04 +08002020 }
2021 finish_wait(sk_sleep(sk), &wait);
2022 return err;
2023}
2024
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002025/**
Ying Xue247f0f32014-02-18 16:06:46 +08002026 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002027 * @sock: listening socket
2028 * @newsock: new socket that is to be connected
2029 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002030 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002031 * Returns 0 on success, errno otherwise
2032 */
David Howellscdfbabf2017-03-09 08:09:05 +00002033static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
2034 bool kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002035{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002036 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002037 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002038 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002039 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002040 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002041 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002042
Allan Stephens0c3141e2008-04-15 00:22:02 -07002043 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002044
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002045 if (sk->sk_state != TIPC_LISTEN) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002046 res = -EINVAL;
2047 goto exit;
2048 }
Ying Xue6398e232014-01-17 09:50:04 +08002049 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2050 res = tipc_wait_for_accept(sock, timeo);
2051 if (res)
2052 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002053
2054 buf = skb_peek(&sk->sk_receive_queue);
2055
David Howellscdfbabf2017-03-09 08:09:05 +00002056 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002057 if (res)
2058 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002059 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002060
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002061 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002062 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002063 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002064
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002065 /* we lock on new_sk; but lockdep sees the lock on sk */
2066 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002067
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002068 /*
2069 * Reject any stray messages received by new socket
2070 * before the socket lock was taken (very, very unlikely)
2071 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002072 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002073
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002074 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002075 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002076
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002077 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002078 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002079 new_tsock->conn_type = msg_nametype(msg);
2080 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002081 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002082
2083 /*
2084 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2085 * Respond to 'SYN+' by queuing it on new socket.
2086 */
2087 if (!msg_data_sz(msg)) {
2088 struct msghdr m = {NULL,};
2089
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002090 tsk_advance_rx_queue(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002091 __tipc_sendstream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002092 } else {
2093 __skb_dequeue(&sk->sk_receive_queue);
2094 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002095 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002096 }
2097 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002098exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002099 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002100 return res;
2101}
2102
2103/**
Ying Xue247f0f32014-02-18 16:06:46 +08002104 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002105 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002106 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002107 *
2108 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002109 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002110 * Returns 0 on success, errno otherwise
2111 */
Ying Xue247f0f32014-02-18 16:06:46 +08002112static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002113{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002114 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002115 int res;
2116
Allan Stephense247a8f2008-03-06 15:05:38 -08002117 if (how != SHUT_RDWR)
2118 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002119
Allan Stephens0c3141e2008-04-15 00:22:02 -07002120 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002121
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002122 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2123 sk->sk_shutdown = SEND_SHUTDOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002124
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002125 if (sk->sk_state == TIPC_DISCONNECTING) {
Ying Xue75031152012-10-29 09:38:15 -04002126 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002127 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002128
2129 /* Wake up anyone sleeping in poll */
2130 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002131 res = 0;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002132 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002133 res = -ENOTCONN;
2134 }
2135
Allan Stephens0c3141e2008-04-15 00:22:02 -07002136 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002137 return res;
2138}
2139
Ying Xuef2f2a962015-01-09 15:27:02 +08002140static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002141{
Ying Xuef2f2a962015-01-09 15:27:02 +08002142 struct tipc_sock *tsk = (struct tipc_sock *)data;
2143 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002144 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002145 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002146 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002147
Jon Paul Maloy57289012014-08-22 18:09:09 -04002148 bh_lock_sock(sk);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002149 if (!tipc_sk_connected(sk)) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002150 bh_unlock_sock(sk);
2151 goto exit;
2152 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002153 peer_port = tsk_peer_port(tsk);
2154 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002155
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002156 if (tsk->probe_unacked) {
Erik Hugneb3be5e32015-06-09 17:27:12 +02002157 if (!sock_owned_by_user(sk)) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01002158 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugneb3be5e32015-06-09 17:27:12 +02002159 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2160 tsk_peer_port(tsk));
2161 sk->sk_state_change(sk);
2162 } else {
2163 /* Try again later */
2164 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2165 }
2166
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002167 bh_unlock_sock(sk);
2168 goto exit;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002169 }
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002170
2171 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2172 INT_H_SIZE, 0, peer_node, own_node,
2173 peer_port, tsk->portid, TIPC_OK);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002174 tsk->probe_unacked = true;
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002175 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002176 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002177 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002178 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002179exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002180 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002181}
2182
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002183static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002184 struct tipc_name_seq const *seq)
2185{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002186 struct sock *sk = &tsk->sk;
2187 struct net *net = sock_net(sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002188 struct publication *publ;
2189 u32 key;
2190
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002191 if (tipc_sk_connected(sk))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002192 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002193 key = tsk->portid + tsk->pub_count + 1;
2194 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002195 return -EADDRINUSE;
2196
Ying Xuef2f98002015-01-09 15:27:05 +08002197 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002198 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002199 if (unlikely(!publ))
2200 return -EINVAL;
2201
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002202 list_add(&publ->pport_list, &tsk->publications);
2203 tsk->pub_count++;
2204 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002205 return 0;
2206}
2207
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002208static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002209 struct tipc_name_seq const *seq)
2210{
Ying Xuef2f98002015-01-09 15:27:05 +08002211 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002212 struct publication *publ;
2213 struct publication *safe;
2214 int rc = -EINVAL;
2215
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002216 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002217 if (seq) {
2218 if (publ->scope != scope)
2219 continue;
2220 if (publ->type != seq->type)
2221 continue;
2222 if (publ->lower != seq->lower)
2223 continue;
2224 if (publ->upper != seq->upper)
2225 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002226 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002227 publ->ref, publ->key);
2228 rc = 0;
2229 break;
2230 }
Ying Xuef2f98002015-01-09 15:27:05 +08002231 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002232 publ->ref, publ->key);
2233 rc = 0;
2234 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002235 if (list_empty(&tsk->publications))
2236 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002237 return rc;
2238}
2239
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002240/* tipc_sk_reinit: set non-zero address in all existing sockets
2241 * when we go from standalone to network mode.
2242 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002243void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002244{
Ying Xuee05b31f2015-01-09 15:27:08 +08002245 struct tipc_net *tn = net_generic(net, tipc_net_id);
Herbert Xu40f9f432017-02-11 19:26:46 +08002246 struct rhashtable_iter iter;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002247 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002248 struct tipc_msg *msg;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002249
Herbert Xu40f9f432017-02-11 19:26:46 +08002250 rhashtable_walk_enter(&tn->sk_rht, &iter);
2251
2252 do {
2253 tsk = ERR_PTR(rhashtable_walk_start(&iter));
Bob Peterson6c7e9832017-08-23 10:43:02 -04002254 if (IS_ERR(tsk))
2255 goto walk_stop;
Herbert Xu40f9f432017-02-11 19:26:46 +08002256
2257 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002258 spin_lock_bh(&tsk->sk.sk_lock.slock);
2259 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002260 msg_set_prevnode(msg, tn->own_addr);
2261 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002262 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2263 }
Bob Peterson6c7e9832017-08-23 10:43:02 -04002264walk_stop:
Herbert Xu40f9f432017-02-11 19:26:46 +08002265 rhashtable_walk_stop(&iter);
2266 } while (tsk == ERR_PTR(-EAGAIN));
Ying Xue07f6c4b2015-01-07 13:41:58 +08002267}
2268
Ying Xuee05b31f2015-01-09 15:27:08 +08002269static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002270{
Ying Xuee05b31f2015-01-09 15:27:08 +08002271 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002272 struct tipc_sock *tsk;
2273
2274 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002275 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002276 if (tsk)
2277 sock_hold(&tsk->sk);
2278 rcu_read_unlock();
2279
2280 return tsk;
2281}
2282
2283static int tipc_sk_insert(struct tipc_sock *tsk)
2284{
Ying Xuee05b31f2015-01-09 15:27:08 +08002285 struct sock *sk = &tsk->sk;
2286 struct net *net = sock_net(sk);
2287 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002288 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2289 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2290
2291 while (remaining--) {
2292 portid++;
2293 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2294 portid = TIPC_MIN_PORT;
2295 tsk->portid = portid;
2296 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002297 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2298 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002299 return 0;
2300 sock_put(&tsk->sk);
2301 }
2302
2303 return -1;
2304}
2305
2306static void tipc_sk_remove(struct tipc_sock *tsk)
2307{
2308 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002309 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002310
Herbert Xu6cca72892015-03-20 21:57:05 +11002311 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Reshetova, Elena41c6d652017-06-30 13:08:01 +03002312 WARN_ON(refcount_read(&sk->sk_refcnt) == 1);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002313 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002314 }
2315}
2316
Herbert Xu6cca72892015-03-20 21:57:05 +11002317static const struct rhashtable_params tsk_rht_params = {
2318 .nelem_hint = 192,
2319 .head_offset = offsetof(struct tipc_sock, node),
2320 .key_offset = offsetof(struct tipc_sock, portid),
2321 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11002322 .max_size = 1048576,
2323 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002324 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11002325};
2326
Ying Xuee05b31f2015-01-09 15:27:08 +08002327int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002328{
Ying Xuee05b31f2015-01-09 15:27:08 +08002329 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002330
Herbert Xu6cca72892015-03-20 21:57:05 +11002331 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002332}
2333
Ying Xuee05b31f2015-01-09 15:27:08 +08002334void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002335{
Ying Xuee05b31f2015-01-09 15:27:08 +08002336 struct tipc_net *tn = net_generic(net, tipc_net_id);
2337
Ying Xue07f6c4b2015-01-07 13:41:58 +08002338 /* Wait for socket readers to complete */
2339 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002340
Ying Xuee05b31f2015-01-09 15:27:08 +08002341 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002342}
2343
2344/**
Ying Xue247f0f32014-02-18 16:06:46 +08002345 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002346 * @sock: socket structure
2347 * @lvl: option level
2348 * @opt: option identifier
2349 * @ov: pointer to new option value
2350 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002351 *
2352 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002353 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002354 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002355 * Returns 0 on success, errno otherwise
2356 */
Ying Xue247f0f32014-02-18 16:06:46 +08002357static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2358 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002359{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002360 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002361 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002362 u32 value = 0;
Dan Carpentera08ef472017-01-24 12:49:35 +03002363 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002364
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002365 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2366 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002367 if (lvl != SOL_TIPC)
2368 return -ENOPROTOOPT;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002369
2370 switch (opt) {
2371 case TIPC_IMPORTANCE:
2372 case TIPC_SRC_DROPPABLE:
2373 case TIPC_DEST_DROPPABLE:
2374 case TIPC_CONN_TIMEOUT:
2375 if (ol < sizeof(value))
2376 return -EINVAL;
2377 res = get_user(value, (u32 __user *)ov);
2378 if (res)
2379 return res;
2380 break;
2381 default:
2382 if (ov || ol)
2383 return -EINVAL;
2384 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002385
Allan Stephens0c3141e2008-04-15 00:22:02 -07002386 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002387
Per Lidenb97bf3f2006-01-02 19:04:38 +01002388 switch (opt) {
2389 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002390 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002391 break;
2392 case TIPC_SRC_DROPPABLE:
2393 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002394 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002395 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002396 res = -ENOPROTOOPT;
2397 break;
2398 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002399 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002400 break;
2401 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002402 tipc_sk(sk)->conn_timeout = value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002403 break;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002404 case TIPC_MCAST_BROADCAST:
2405 tsk->mc_method.rcast = false;
2406 tsk->mc_method.mandatory = true;
2407 break;
2408 case TIPC_MCAST_REPLICAST:
2409 tsk->mc_method.rcast = true;
2410 tsk->mc_method.mandatory = true;
2411 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002412 default:
2413 res = -EINVAL;
2414 }
2415
Allan Stephens0c3141e2008-04-15 00:22:02 -07002416 release_sock(sk);
2417
Per Lidenb97bf3f2006-01-02 19:04:38 +01002418 return res;
2419}
2420
2421/**
Ying Xue247f0f32014-02-18 16:06:46 +08002422 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002423 * @sock: socket structure
2424 * @lvl: option level
2425 * @opt: option identifier
2426 * @ov: receptacle for option value
2427 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002428 *
2429 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002430 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002431 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002432 * Returns 0 on success, errno otherwise
2433 */
Ying Xue247f0f32014-02-18 16:06:46 +08002434static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2435 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002436{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002437 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002438 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002439 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002440 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002441 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002442
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002443 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2444 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002445 if (lvl != SOL_TIPC)
2446 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002447 res = get_user(len, ol);
2448 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002449 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002450
Allan Stephens0c3141e2008-04-15 00:22:02 -07002451 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002452
2453 switch (opt) {
2454 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002455 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002456 break;
2457 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002458 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002459 break;
2460 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002461 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002462 break;
2463 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002464 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002465 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002466 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002467 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002468 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002469 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002470 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002471 value = skb_queue_len(&sk->sk_receive_queue);
2472 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002473 default:
2474 res = -EINVAL;
2475 }
2476
Allan Stephens0c3141e2008-04-15 00:22:02 -07002477 release_sock(sk);
2478
Paul Gortmaker25860c32010-12-31 18:59:31 +00002479 if (res)
2480 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002481
Paul Gortmaker25860c32010-12-31 18:59:31 +00002482 if (len < sizeof(value))
2483 return -EINVAL;
2484
2485 if (copy_to_user(ov, &value, sizeof(value)))
2486 return -EFAULT;
2487
2488 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002489}
2490
Ying Xuef2f98002015-01-09 15:27:05 +08002491static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002492{
Ying Xuef2f98002015-01-09 15:27:05 +08002493 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002494 struct tipc_sioc_ln_req lnr;
2495 void __user *argp = (void __user *)arg;
2496
2497 switch (cmd) {
2498 case SIOCGETLINKNAME:
2499 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2500 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002501 if (!tipc_node_get_linkname(sock_net(sk),
2502 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002503 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2504 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2505 return -EFAULT;
2506 return 0;
2507 }
2508 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002509 default:
2510 return -ENOIOCTLCMD;
2511 }
2512}
2513
Erik Hugne70b03752017-03-29 11:22:16 +02002514static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
2515{
2516 struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
2517 struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
Erik Hugne66bc1e82017-03-29 11:22:17 +02002518 u32 onode = tipc_own_addr(sock_net(sock1->sk));
Erik Hugne70b03752017-03-29 11:22:16 +02002519
Erik Hugne66bc1e82017-03-29 11:22:17 +02002520 tsk1->peer.family = AF_TIPC;
2521 tsk1->peer.addrtype = TIPC_ADDR_ID;
2522 tsk1->peer.scope = TIPC_NODE_SCOPE;
2523 tsk1->peer.addr.id.ref = tsk2->portid;
2524 tsk1->peer.addr.id.node = onode;
2525 tsk2->peer.family = AF_TIPC;
2526 tsk2->peer.addrtype = TIPC_ADDR_ID;
2527 tsk2->peer.scope = TIPC_NODE_SCOPE;
2528 tsk2->peer.addr.id.ref = tsk1->portid;
2529 tsk2->peer.addr.id.node = onode;
2530
2531 tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
2532 tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
Erik Hugne70b03752017-03-29 11:22:16 +02002533 return 0;
2534}
2535
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002536/* Protocol switches for the various types of TIPC sockets */
2537
Florian Westphalbca65ea2008-02-07 18:18:01 -08002538static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002539 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002540 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002541 .release = tipc_release,
2542 .bind = tipc_bind,
2543 .connect = tipc_connect,
Erik Hugne66bc1e82017-03-29 11:22:17 +02002544 .socketpair = tipc_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002545 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002546 .getname = tipc_getname,
2547 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002548 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002549 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002550 .shutdown = tipc_shutdown,
2551 .setsockopt = tipc_setsockopt,
2552 .getsockopt = tipc_getsockopt,
2553 .sendmsg = tipc_sendmsg,
2554 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002555 .mmap = sock_no_mmap,
2556 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002557};
2558
Florian Westphalbca65ea2008-02-07 18:18:01 -08002559static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002560 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002561 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002562 .release = tipc_release,
2563 .bind = tipc_bind,
2564 .connect = tipc_connect,
Erik Hugne70b03752017-03-29 11:22:16 +02002565 .socketpair = tipc_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002566 .accept = tipc_accept,
2567 .getname = tipc_getname,
2568 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002569 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002570 .listen = tipc_listen,
2571 .shutdown = tipc_shutdown,
2572 .setsockopt = tipc_setsockopt,
2573 .getsockopt = tipc_getsockopt,
2574 .sendmsg = tipc_send_packet,
2575 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002576 .mmap = sock_no_mmap,
2577 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002578};
2579
Florian Westphalbca65ea2008-02-07 18:18:01 -08002580static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002581 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002582 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002583 .release = tipc_release,
2584 .bind = tipc_bind,
2585 .connect = tipc_connect,
Erik Hugne70b03752017-03-29 11:22:16 +02002586 .socketpair = tipc_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002587 .accept = tipc_accept,
2588 .getname = tipc_getname,
2589 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002590 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002591 .listen = tipc_listen,
2592 .shutdown = tipc_shutdown,
2593 .setsockopt = tipc_setsockopt,
2594 .getsockopt = tipc_getsockopt,
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002595 .sendmsg = tipc_sendstream,
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002596 .recvmsg = tipc_recvstream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002597 .mmap = sock_no_mmap,
2598 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002599};
2600
Florian Westphalbca65ea2008-02-07 18:18:01 -08002601static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002602 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002603 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002604 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002605};
2606
2607static struct proto tipc_proto = {
2608 .name = "TIPC",
2609 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002610 .obj_size = sizeof(struct tipc_sock),
2611 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002612};
2613
2614/**
Per Liden4323add2006-01-18 00:38:21 +01002615 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002616 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002617 * Returns 0 on success, errno otherwise
2618 */
Per Liden4323add2006-01-18 00:38:21 +01002619int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002620{
2621 int res;
2622
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002623 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002624 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002625 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002626 goto out;
2627 }
2628
2629 res = sock_register(&tipc_family_ops);
2630 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002631 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002632 proto_unregister(&tipc_proto);
2633 goto out;
2634 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002635 out:
2636 return res;
2637}
2638
2639/**
Per Liden4323add2006-01-18 00:38:21 +01002640 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002641 */
Per Liden4323add2006-01-18 00:38:21 +01002642void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002643{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002644 sock_unregister(tipc_family_ops.family);
2645 proto_unregister(&tipc_proto);
2646}
Richard Alpe34b78a122014-11-20 10:29:10 +01002647
2648/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002649static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002650{
2651 u32 peer_node;
2652 u32 peer_port;
2653 struct nlattr *nest;
2654
2655 peer_node = tsk_peer_node(tsk);
2656 peer_port = tsk_peer_port(tsk);
2657
2658 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2659
2660 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2661 goto msg_full;
2662 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2663 goto msg_full;
2664
2665 if (tsk->conn_type != 0) {
2666 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2667 goto msg_full;
2668 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2669 goto msg_full;
2670 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2671 goto msg_full;
2672 }
2673 nla_nest_end(skb, nest);
2674
2675 return 0;
2676
2677msg_full:
2678 nla_nest_cancel(skb, nest);
2679
2680 return -EMSGSIZE;
2681}
2682
2683/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002684static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2685 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002686{
2687 int err;
2688 void *hdr;
2689 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002690 struct net *net = sock_net(skb->sk);
2691 struct tipc_net *tn = net_generic(net, tipc_net_id);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002692 struct sock *sk = &tsk->sk;
Richard Alpe34b78a122014-11-20 10:29:10 +01002693
2694 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002695 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002696 if (!hdr)
2697 goto msg_cancel;
2698
2699 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2700 if (!attrs)
2701 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002702 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002703 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002704 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002705 goto attr_msg_cancel;
2706
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002707 if (tipc_sk_connected(sk)) {
Richard Alpe34b78a122014-11-20 10:29:10 +01002708 err = __tipc_nl_add_sk_con(skb, tsk);
2709 if (err)
2710 goto attr_msg_cancel;
2711 } else if (!list_empty(&tsk->publications)) {
2712 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2713 goto attr_msg_cancel;
2714 }
2715 nla_nest_end(skb, attrs);
2716 genlmsg_end(skb, hdr);
2717
2718 return 0;
2719
2720attr_msg_cancel:
2721 nla_nest_cancel(skb, attrs);
2722genlmsg_cancel:
2723 genlmsg_cancel(skb, hdr);
2724msg_cancel:
2725 return -EMSGSIZE;
2726}
2727
2728int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2729{
2730 int err;
2731 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002732 const struct bucket_table *tbl;
2733 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002734 struct net *net = sock_net(skb->sk);
2735 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002736 u32 tbl_id = cb->args[0];
2737 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002738
Ying Xue07f6c4b2015-01-07 13:41:58 +08002739 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002740 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002741 for (; tbl_id < tbl->size; tbl_id++) {
2742 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002743 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002744 if (prev_portid && prev_portid != tsk->portid) {
2745 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2746 continue;
2747 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002748
Richard Alped6e164e2015-01-16 12:30:40 +01002749 err = __tipc_nl_add_sk(skb, cb, tsk);
2750 if (err) {
2751 prev_portid = tsk->portid;
2752 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2753 goto out;
2754 }
2755 prev_portid = 0;
2756 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002757 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002758 }
Richard Alped6e164e2015-01-16 12:30:40 +01002759out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002760 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002761 cb->args[0] = tbl_id;
2762 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002763
2764 return skb->len;
2765}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002766
2767/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002768static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2769 struct netlink_callback *cb,
2770 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002771{
2772 void *hdr;
2773 struct nlattr *attrs;
2774
2775 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002776 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002777 if (!hdr)
2778 goto msg_cancel;
2779
2780 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2781 if (!attrs)
2782 goto genlmsg_cancel;
2783
2784 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2785 goto attr_msg_cancel;
2786 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2787 goto attr_msg_cancel;
2788 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2789 goto attr_msg_cancel;
2790 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2791 goto attr_msg_cancel;
2792
2793 nla_nest_end(skb, attrs);
2794 genlmsg_end(skb, hdr);
2795
2796 return 0;
2797
2798attr_msg_cancel:
2799 nla_nest_cancel(skb, attrs);
2800genlmsg_cancel:
2801 genlmsg_cancel(skb, hdr);
2802msg_cancel:
2803 return -EMSGSIZE;
2804}
2805
2806/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002807static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2808 struct netlink_callback *cb,
2809 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002810{
2811 int err;
2812 struct publication *p;
2813
2814 if (*last_publ) {
2815 list_for_each_entry(p, &tsk->publications, pport_list) {
2816 if (p->key == *last_publ)
2817 break;
2818 }
2819 if (p->key != *last_publ) {
2820 /* We never set seq or call nl_dump_check_consistent()
2821 * this means that setting prev_seq here will cause the
2822 * consistence check to fail in the netlink callback
2823 * handler. Resulting in the last NLMSG_DONE message
2824 * having the NLM_F_DUMP_INTR flag set.
2825 */
2826 cb->prev_seq = 1;
2827 *last_publ = 0;
2828 return -EPIPE;
2829 }
2830 } else {
2831 p = list_first_entry(&tsk->publications, struct publication,
2832 pport_list);
2833 }
2834
2835 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2836 err = __tipc_nl_add_sk_publ(skb, cb, p);
2837 if (err) {
2838 *last_publ = p->key;
2839 return err;
2840 }
2841 }
2842 *last_publ = 0;
2843
2844 return 0;
2845}
2846
2847int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2848{
2849 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002850 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002851 u32 last_publ = cb->args[1];
2852 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002853 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002854 struct tipc_sock *tsk;
2855
Ying Xue07f6c4b2015-01-07 13:41:58 +08002856 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002857 struct nlattr **attrs;
2858 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2859
2860 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2861 if (err)
2862 return err;
2863
Richard Alpe45e093a2016-05-16 11:14:54 +02002864 if (!attrs[TIPC_NLA_SOCK])
2865 return -EINVAL;
2866
Richard Alpe1a1a1432014-11-20 10:29:11 +01002867 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2868 attrs[TIPC_NLA_SOCK],
Johannes Bergfceb6432017-04-12 14:34:07 +02002869 tipc_nl_sock_policy, NULL);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002870 if (err)
2871 return err;
2872
2873 if (!sock[TIPC_NLA_SOCK_REF])
2874 return -EINVAL;
2875
Ying Xue07f6c4b2015-01-07 13:41:58 +08002876 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002877 }
2878
2879 if (done)
2880 return 0;
2881
Ying Xuee05b31f2015-01-09 15:27:08 +08002882 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002883 if (!tsk)
2884 return -EINVAL;
2885
2886 lock_sock(&tsk->sk);
2887 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2888 if (!err)
2889 done = 1;
2890 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002891 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002892
Ying Xue07f6c4b2015-01-07 13:41:58 +08002893 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002894 cb->args[1] = last_publ;
2895 cb->args[2] = done;
2896
2897 return skb->len;
2898}