blob: 1b92b72e812f942fc9826d8542f29bf1bd7c26c5 [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 Paul Maloy4f4482d2014-05-14 05:39:09 -0400114static int tipc_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 Xue39a02952015-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 Bhuvaraganc7520232016-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 Paul Maloy4f4482d2014-05-14 05:39:09 -0400456 sk->sk_backlog_rcv = tipc_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 Bhuvaraganc7520232016-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 Paul Maloyac0074e2014-06-25 20:41:41 -0500853 * tipc_sk_proto_rcv - receive a connection mng protocol message
854 * @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 Paul Maloyf1d048f2016-06-17 06:35:57 -0400857static void tipc_sk_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 sock *sk = &tsk->sk;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400861 u32 onode = tsk_own_node(tsk);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400862 struct tipc_msg *hdr = buf_msg(skb);
863 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 Paul Maloye2dafe82014-06-25 20:41:37 -0500899/**
Ying Xue247f0f32014-02-18 16:06:46 +0800900 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100901 * @sock: socket structure
902 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500903 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900904 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900906 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100907 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
908 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900909 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910 * Returns the number of bytes sent on success, or errno otherwise
911 */
Ying Xue1b784142015-03-02 15:37:48 +0800912static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500913 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914{
Ying Xue39a02952015-03-02 15:37:47 +0800915 struct sock *sk = sock->sk;
916 int ret;
917
918 lock_sock(sk);
919 ret = __tipc_sendmsg(sock, m, dsz);
920 release_sock(sk);
921
922 return ret;
923}
924
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500925static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a02952015-03-02 15:37:47 +0800926{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700927 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +0800928 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500929 struct tipc_sock *tsk = tipc_sk(sk);
930 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
931 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
932 struct list_head *clinks = &tsk->cong_links;
933 bool syn = !tipc_sk_type_connectionless(sk);
934 struct tipc_msg *hdr = &tsk->phdr;
Erik Hugnef2f80362015-03-19 09:02:19 +0100935 struct tipc_name_seq *seq;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500936 struct sk_buff_head pkts;
937 u32 type, inst, domain;
938 u32 dnode, dport;
939 int mtu, rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100940
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500941 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400942 return -EMSGSIZE;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500943
Erik Hugnef2f80362015-03-19 09:02:19 +0100944 if (unlikely(!dest)) {
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500945 dest = &tsk->peer;
946 if (!syn || dest->family != AF_TIPC)
Erik Hugnef2f80362015-03-19 09:02:19 +0100947 return -EDESTADDRREQ;
Erik Hugnef2f80362015-03-19 09:02:19 +0100948 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500949
950 if (unlikely(m->msg_namelen < sizeof(*dest)))
951 return -EINVAL;
952
953 if (unlikely(dest->family != AF_TIPC))
954 return -EINVAL;
955
956 if (unlikely(syn)) {
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100957 if (sk->sk_state == TIPC_LISTEN)
Ying Xue39a02952015-03-02 15:37:47 +0800958 return -EPIPE;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100959 if (sk->sk_state != TIPC_OPEN)
Ying Xue39a02952015-03-02 15:37:47 +0800960 return -EISCONN;
961 if (tsk->published)
962 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700963 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400964 tsk->conn_type = dest->addr.name.name.type;
965 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700966 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100967 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500968
Erik Hugnef2f80362015-03-19 09:02:19 +0100969 seq = &dest->addr.nameseq;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500970 if (dest->addrtype == TIPC_ADDR_MCAST)
971 return tipc_sendmcast(sock, seq, m, dlen, timeout);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500972
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500973 if (dest->addrtype == TIPC_ADDR_NAME) {
974 type = dest->addr.name.name.type;
975 inst = dest->addr.name.name.instance;
976 domain = dest->addr.name.domain;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500977 dnode = domain;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500978 msg_set_type(hdr, TIPC_NAMED_MSG);
979 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
980 msg_set_nametype(hdr, type);
981 msg_set_nameinst(hdr, inst);
982 msg_set_lookup_scope(hdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800983 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500984 msg_set_destnode(hdr, dnode);
985 msg_set_destport(hdr, dport);
Ying Xue39a02952015-03-02 15:37:47 +0800986 if (unlikely(!dport && !dnode))
987 return -EHOSTUNREACH;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500988
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500989 } else if (dest->addrtype == TIPC_ADDR_ID) {
990 dnode = dest->addr.id.node;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500991 msg_set_type(hdr, TIPC_DIRECT_MSG);
992 msg_set_lookup_scope(hdr, 0);
993 msg_set_destnode(hdr, dnode);
994 msg_set_destport(hdr, dest->addr.id.ref);
995 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500996 }
997
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500998 /* Block or return if destination link is congested */
999 rc = tipc_wait_for_cond(sock, &timeout, !u32_find(clinks, dnode));
1000 if (unlikely(rc))
Ying Xue39a02952015-03-02 15:37:47 +08001001 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001002
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001003 skb_queue_head_init(&pkts);
1004 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
1005 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
1006 if (unlikely(rc != dlen))
1007 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001008
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001009 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1010 if (unlikely(rc == -ELINKCONG)) {
1011 u32_push(clinks, dnode);
1012 tsk->cong_link_cnt++;
1013 rc = 0;
1014 }
1015
1016 if (unlikely(syn && !rc))
1017 tipc_set_sk_state(sk, TIPC_CONNECTING);
1018
1019 return rc ? rc : dlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020}
1021
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001022/**
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001023 * tipc_sendstream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 * @sock: socket structure
1025 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001026 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001027 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001028 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001029 *
1030 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001031 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001032 */
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001033static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001035 struct sock *sk = sock->sk;
Ying Xue39a02952015-03-02 15:37:47 +08001036 int ret;
1037
1038 lock_sock(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001039 ret = __tipc_sendstream(sock, m, dsz);
Ying Xue39a02952015-03-02 15:37:47 +08001040 release_sock(sk);
1041
1042 return ret;
1043}
1044
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001045static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a02952015-03-02 15:37:47 +08001046{
1047 struct sock *sk = sock->sk;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001048 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001049 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1050 struct tipc_sock *tsk = tipc_sk(sk);
1051 struct tipc_msg *hdr = &tsk->phdr;
1052 struct net *net = sock_net(sk);
1053 struct sk_buff_head pkts;
1054 u32 dnode = tsk_peer_node(tsk);
1055 int send, sent = 0;
1056 int rc = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001057
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001058 skb_queue_head_init(&pkts);
1059
1060 if (unlikely(dlen > INT_MAX))
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001061 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001062
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001063 /* Handle implicit connection setup */
1064 if (unlikely(dest)) {
1065 rc = __tipc_sendmsg(sock, m, dlen);
1066 if (dlen && (dlen == rc))
1067 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
1068 return rc;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001069 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001070
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001071 do {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001072 rc = tipc_wait_for_cond(sock, &timeout,
1073 (!tsk->cong_link_cnt &&
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -05001074 !tsk_conn_cong(tsk) &&
1075 tipc_sk_connected(sk)));
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001076 if (unlikely(rc))
1077 break;
Ying Xue39a02952015-03-02 15:37:47 +08001078
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001079 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
1080 rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
1081 if (unlikely(rc != send))
1082 break;
1083
1084 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1085 if (unlikely(rc == -ELINKCONG)) {
1086 tsk->cong_link_cnt = 1;
1087 rc = 0;
1088 }
1089 if (likely(!rc)) {
1090 tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
1091 sent += send;
1092 }
1093 } while (sent < dlen && !rc);
1094
Parthasarathy Bhuvaragan3364d612017-04-24 15:00:42 +02001095 return sent ? sent : rc;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001096}
1097
1098/**
1099 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001100 * @sock: socket structure
1101 * @m: message to send
1102 * @dsz: length of data to be transmitted
1103 *
1104 * Used for SOCK_SEQPACKET messages.
1105 *
1106 * Returns the number of bytes sent on success, or errno otherwise
1107 */
Ying Xue1b784142015-03-02 15:37:48 +08001108static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001109{
1110 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1111 return -EMSGSIZE;
1112
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001113 return tipc_sendstream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114}
1115
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001116/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001118static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001119 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001120{
Ying Xue3721e9c2015-01-13 17:07:48 +08001121 struct sock *sk = &tsk->sk;
1122 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001123 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001124
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001125 msg_set_destnode(msg, peer_node);
1126 msg_set_destport(msg, peer_port);
1127 msg_set_type(msg, TIPC_CONN_MSG);
1128 msg_set_lookup_scope(msg, 0);
1129 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001130
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01001131 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001132 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
Ying Xuef2f98002015-01-09 15:27:05 +08001133 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1134 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001135 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001136 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1137 return;
1138
1139 /* Fall back to message based flow control */
1140 tsk->rcv_win = FLOWCTL_MSG_WIN;
1141 tsk->snd_win = FLOWCTL_MSG_WIN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142}
1143
1144/**
1145 * set_orig_addr - capture sender's address for received message
1146 * @m: descriptor for message info
1147 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001148 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001149 * Note: Address is not captured if not requested by receiver.
1150 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001151static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001152{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001153 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001155 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156 addr->family = AF_TIPC;
1157 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001158 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 addr->addr.id.ref = msg_origport(msg);
1160 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001161 addr->addr.name.domain = 0; /* could leave uninitialized */
1162 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 m->msg_namelen = sizeof(struct sockaddr_tipc);
1164 }
1165}
1166
1167/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001168 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 * @m: descriptor for message info
1170 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001171 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001172 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001174 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001175 * Returns 0 if successful, otherwise errno
1176 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001177static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1178 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179{
1180 u32 anc_data[3];
1181 u32 err;
1182 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001183 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001184 int res;
1185
1186 if (likely(m->msg_controllen == 0))
1187 return 0;
1188
1189 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001190 err = msg ? msg_errcode(msg) : 0;
1191 if (unlikely(err)) {
1192 anc_data[0] = err;
1193 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001194 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1195 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001197 if (anc_data[1]) {
1198 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1199 msg_data(msg));
1200 if (res)
1201 return res;
1202 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001203 }
1204
1205 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1207 switch (dest_type) {
1208 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001209 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001210 anc_data[0] = msg_nametype(msg);
1211 anc_data[1] = msg_namelower(msg);
1212 anc_data[2] = msg_namelower(msg);
1213 break;
1214 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001215 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001216 anc_data[0] = msg_nametype(msg);
1217 anc_data[1] = msg_namelower(msg);
1218 anc_data[2] = msg_nameupper(msg);
1219 break;
1220 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001221 has_name = (tsk->conn_type != 0);
1222 anc_data[0] = tsk->conn_type;
1223 anc_data[1] = tsk->conn_instance;
1224 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001225 break;
1226 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001227 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 }
Allan Stephens2db99832010-12-31 18:59:33 +00001229 if (has_name) {
1230 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1231 if (res)
1232 return res;
1233 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234
1235 return 0;
1236}
1237
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001238static void tipc_sk_send_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001239{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001240 struct sock *sk = &tsk->sk;
1241 struct net *net = sock_net(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001242 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001243 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001244 u32 peer_port = tsk_peer_port(tsk);
1245 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001246
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001247 if (!tipc_sk_connected(sk))
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001248 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001249 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1250 dnode, tsk_own_node(tsk), peer_port,
1251 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001252 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001253 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001254 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001255 msg_set_conn_ack(msg, tsk->rcv_unacked);
1256 tsk->rcv_unacked = 0;
1257
1258 /* Adjust to and advertize the correct window limit */
1259 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1260 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1261 msg_set_adv_win(msg, tsk->rcv_win);
1262 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001263 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001264}
1265
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001266static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001267{
1268 struct sock *sk = sock->sk;
1269 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001270 long timeo = *timeop;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001271 int err = sock_error(sk);
1272
1273 if (err)
1274 return err;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001275
1276 for (;;) {
1277 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001278 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01001279 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001280 err = -ENOTCONN;
1281 break;
1282 }
1283 release_sock(sk);
1284 timeo = schedule_timeout(timeo);
1285 lock_sock(sk);
1286 }
1287 err = 0;
1288 if (!skb_queue_empty(&sk->sk_receive_queue))
1289 break;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001290 err = -EAGAIN;
1291 if (!timeo)
1292 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001293 err = sock_intr_errno(timeo);
1294 if (signal_pending(current))
1295 break;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001296
1297 err = sock_error(sk);
1298 if (err)
1299 break;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001300 }
1301 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001302 *timeop = timeo;
Ying Xue9bbb4ecc2014-01-17 09:50:07 +08001303 return err;
1304}
1305
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001306/**
Ying Xue247f0f32014-02-18 16:06:46 +08001307 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001308 * @m: descriptor for message info
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001309 * @buflen: length of user buffer area
Per Lidenb97bf3f2006-01-02 19:04:38 +01001310 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001311 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001312 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1313 * If the complete message doesn't fit in user area, truncate it.
1314 *
1315 * Returns size of returned message data, errno otherwise
1316 */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001317static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
1318 size_t buflen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001320 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001321 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001322 struct sk_buff *skb;
1323 struct tipc_msg *hdr;
1324 bool connected = !tipc_sk_type_connectionless(sk);
1325 int rc, err, hlen, dlen, copy;
1326 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001327
Allan Stephens0c3141e2008-04-15 00:22:02 -07001328 /* Catch invalid receive requests */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001329 if (unlikely(!buflen))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001330 return -EINVAL;
1331
Allan Stephens0c3141e2008-04-15 00:22:02 -07001332 lock_sock(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001333 if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
1334 rc = -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 goto exit;
1336 }
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001337 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001339 do {
1340 /* Look at first msg in receive queue; wait if necessary */
1341 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1342 if (unlikely(rc))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001343 goto exit;
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001344 skb = skb_peek(&sk->sk_receive_queue);
1345 hdr = buf_msg(skb);
1346 dlen = msg_data_sz(hdr);
1347 hlen = msg_hdr_sz(hdr);
1348 err = msg_errcode(hdr);
1349 if (likely(dlen || err))
1350 break;
1351 tsk_advance_rx_queue(sk);
1352 } while (1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001353
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001354 /* Collect msg meta data, including error code and rejected data */
1355 set_orig_addr(m, hdr);
1356 rc = tipc_sk_anc_data_recv(m, hdr, tsk);
1357 if (unlikely(rc))
1358 goto exit;
1359
1360 /* Capture data if non-error msg, otherwise just set return value */
1361 if (likely(!err)) {
1362 copy = min_t(int, dlen, buflen);
1363 if (unlikely(copy != dlen))
1364 m->msg_flags |= MSG_TRUNC;
1365 rc = skb_copy_datagram_msg(skb, hlen, m, copy);
1366 } else {
1367 copy = 0;
1368 rc = 0;
1369 if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control)
1370 rc = -ECONNRESET;
1371 }
1372 if (unlikely(rc))
1373 goto exit;
1374
1375 /* Caption of data or error code/rejected data was successful */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001376 if (unlikely(flags & MSG_PEEK))
1377 goto exit;
1378
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001379 tsk_advance_rx_queue(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001380 if (likely(!connected))
1381 goto exit;
1382
1383 /* Send connection flow control ack when applicable */
1384 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1385 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
1386 tipc_sk_send_ack(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001388 release_sock(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001389 return rc ? rc : copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390}
1391
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001392/**
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001393 * tipc_recvstream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001394 * @m: descriptor for message info
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001395 * @buflen: total size of user buffer area
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001397 *
1398 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001399 * will optionally wait for more; never truncates data.
1400 *
1401 * Returns size of returned message data, errno otherwise
1402 */
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001403static int tipc_recvstream(struct socket *sock, struct msghdr *m,
1404 size_t buflen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001405{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001406 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001407 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001408 struct sk_buff *skb;
1409 struct tipc_msg *hdr;
1410 struct tipc_skb_cb *skb_cb;
1411 bool peek = flags & MSG_PEEK;
1412 int offset, required, copy, copied = 0;
1413 int hlen, dlen, err, rc;
1414 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001415
1416 /* Catch invalid receive attempts */
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001417 if (unlikely(!buflen))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418 return -EINVAL;
1419
Allan Stephens0c3141e2008-04-15 00:22:02 -07001420 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001422 if (unlikely(sk->sk_state == TIPC_OPEN)) {
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001423 rc = -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424 goto exit;
1425 }
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001426 required = sock_rcvlowat(sk, flags & MSG_WAITALL, buflen);
1427 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001428
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001429 do {
1430 /* Look at first msg in receive queue; wait if necessary */
1431 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1432 if (unlikely(rc))
1433 break;
1434 skb = skb_peek(&sk->sk_receive_queue);
1435 skb_cb = TIPC_SKB_CB(skb);
1436 hdr = buf_msg(skb);
1437 dlen = msg_data_sz(hdr);
1438 hlen = msg_hdr_sz(hdr);
1439 err = msg_errcode(hdr);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001440
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001441 /* Discard any empty non-errored (SYN-) message */
1442 if (unlikely(!dlen && !err)) {
1443 tsk_advance_rx_queue(sk);
1444 continue;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001445 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001446
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001447 /* Collect msg meta data, incl. error code and rejected data */
1448 if (!copied) {
1449 set_orig_addr(m, hdr);
1450 rc = tipc_sk_anc_data_recv(m, hdr, tsk);
1451 if (rc)
1452 break;
1453 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001454
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001455 /* Copy data if msg ok, otherwise return error/partial data */
1456 if (likely(!err)) {
1457 offset = skb_cb->bytes_read;
1458 copy = min_t(int, dlen - offset, buflen - copied);
1459 rc = skb_copy_datagram_msg(skb, hlen + offset, m, copy);
1460 if (unlikely(rc))
1461 break;
1462 copied += copy;
1463 offset += copy;
1464 if (unlikely(offset < dlen)) {
1465 if (!peek)
1466 skb_cb->bytes_read = offset;
1467 break;
1468 }
1469 } else {
1470 rc = 0;
1471 if ((err != TIPC_CONN_SHUTDOWN) && !m->msg_control)
1472 rc = -ECONNRESET;
1473 if (copied || rc)
1474 break;
1475 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001476
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001477 if (unlikely(peek))
1478 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001479
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001480 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001481
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001482 /* Send connection flow control advertisement when applicable */
1483 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1484 if (unlikely(tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE))
1485 tipc_sk_send_ack(tsk);
1486
1487 /* Exit if all requested data or FIN/error received */
1488 if (copied == buflen || err)
1489 break;
1490
1491 } while (!skb_queue_empty(&sk->sk_receive_queue) || copied < required);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001492exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001493 release_sock(sk);
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02001494 return copied ? copied : rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001495}
1496
1497/**
Ying Xuef288bef2012-08-21 11:16:57 +08001498 * tipc_write_space - wake up thread if port congestion is released
1499 * @sk: socket
1500 */
1501static void tipc_write_space(struct sock *sk)
1502{
1503 struct socket_wq *wq;
1504
1505 rcu_read_lock();
1506 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001507 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001508 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1509 POLLWRNORM | POLLWRBAND);
1510 rcu_read_unlock();
1511}
1512
1513/**
1514 * tipc_data_ready - wake up threads to indicate messages have been received
1515 * @sk: socket
1516 * @len: the length of messages
1517 */
David S. Miller676d2362014-04-11 16:15:36 -04001518static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001519{
1520 struct socket_wq *wq;
1521
1522 rcu_read_lock();
1523 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001524 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001525 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1526 POLLRDNORM | POLLRDBAND);
1527 rcu_read_unlock();
1528}
1529
Ying Xuef4195d12015-11-22 15:46:05 +08001530static void tipc_sock_destruct(struct sock *sk)
1531{
1532 __skb_queue_purge(&sk->sk_receive_queue);
1533}
1534
Ying Xuef288bef2012-08-21 11:16:57 +08001535/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001536 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001537 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001538 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001539 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001540 * Returns true if everything ok, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001541 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001542static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001543{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001544 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001545 struct net *net = sock_net(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001546 struct tipc_msg *hdr = buf_msg(skb);
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001547 u32 pport = msg_origport(hdr);
1548 u32 pnode = msg_orignode(hdr);
Ying Xue7e6c1312012-11-29 18:39:14 -05001549
Jon Paul Maloycda36962015-07-22 10:11:20 -04001550 if (unlikely(msg_mcast(hdr)))
1551 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001552
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001553 switch (sk->sk_state) {
1554 case TIPC_CONNECTING:
Ying Xue7e6c1312012-11-29 18:39:14 -05001555 /* Accept only ACK or NACK message */
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001556 if (unlikely(!msg_connected(hdr))) {
1557 if (pport != tsk_peer_port(tsk) ||
1558 pnode != tsk_peer_node(tsk))
1559 return false;
1560
1561 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1562 sk->sk_err = ECONNREFUSED;
1563 sk->sk_state_change(sk);
1564 return true;
1565 }
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001566
Jon Paul Maloycda36962015-07-22 10:11:20 -04001567 if (unlikely(msg_errcode(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001568 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugne2c8d8512013-08-28 09:29:58 +02001569 sk->sk_err = ECONNREFUSED;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001570 sk->sk_state_change(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001571 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001572 }
1573
Jon Paul Maloycda36962015-07-22 10:11:20 -04001574 if (unlikely(!msg_isdata(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001575 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001576 sk->sk_err = EINVAL;
Parthasarathy Bhuvaragan4e0df492017-04-26 10:05:01 +02001577 sk->sk_state_change(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001578 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001579 }
1580
Jon Paul Maloycda36962015-07-22 10:11:20 -04001581 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1582 msg_set_importance(&tsk->phdr, msg_importance(hdr));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001583
Jon Paul Maloycda36962015-07-22 10:11:20 -04001584 /* If 'ACK+' message, add to socket receive queue */
1585 if (msg_data_sz(hdr))
1586 return true;
1587
1588 /* If empty 'ACK-' message, wake up sleeping connect() */
Parthasarathy Bhuvaragan42b531d2017-04-26 10:05:00 +02001589 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001590
1591 /* 'ACK-' message is neither accepted nor rejected: */
1592 msg_set_dest_droppable(hdr, 1);
1593 return false;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001594
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001595 case TIPC_OPEN:
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001596 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001597 break;
1598 case TIPC_LISTEN:
Ying Xue7e6c1312012-11-29 18:39:14 -05001599 /* Accept only SYN message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001600 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1601 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001602 break;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001603 case TIPC_ESTABLISHED:
1604 /* Accept only connection-based messages sent by peer */
1605 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1606 return false;
1607
1608 if (unlikely(msg_errcode(hdr))) {
1609 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1610 /* Let timer expire on it's own */
1611 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1612 tsk->portid);
1613 sk->sk_state_change(sk);
1614 }
1615 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001616 default:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001617 pr_err("Unknown sk_state %u\n", sk->sk_state);
Ying Xue7e6c1312012-11-29 18:39:14 -05001618 }
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001619
Jon Paul Maloycda36962015-07-22 10:11:20 -04001620 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001621}
1622
1623/**
Ying Xueaba79f32013-01-20 23:30:09 +01001624 * rcvbuf_limit - get proper overload limit of socket receive queue
1625 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001626 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01001627 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001628 * For connection oriented messages, irrespective of importance,
1629 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01001630 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001631 * For connectionless messages, queue limits are based on message
1632 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01001633 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001634 * TIPC_LOW_IMPORTANCE (2 MB)
1635 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1636 * TIPC_HIGH_IMPORTANCE (8 MB)
1637 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001638 *
1639 * Returns overload limit according to corresponding message importance
1640 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001641static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01001642{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001643 struct tipc_sock *tsk = tipc_sk(sk);
1644 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01001645
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001646 if (unlikely(!msg_connected(hdr)))
1647 return sk->sk_rcvbuf << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08001648
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001649 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1650 return sk->sk_rcvbuf;
1651
1652 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01001653}
1654
1655/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001656 * filter_rcv - validate incoming message
1657 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04001658 * @skb: pointer to message.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001659 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001660 * Enqueues message on receive queue if acceptable; optionally handles
1661 * disconnect indication for a connected socket.
1662 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001663 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001664 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001665 * Returns true if message was added to socket receive queue, otherwise false
Per Lidenb97bf3f2006-01-02 19:04:38 +01001666 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001667static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1668 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001669{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001670 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001671 struct tipc_msg *hdr = buf_msg(skb);
1672 unsigned int limit = rcvbuf_limit(sk, skb);
1673 int err = TIPC_OK;
1674 int usr = msg_user(hdr);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001675 u32 onode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001676
Jon Paul Maloycda36962015-07-22 10:11:20 -04001677 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001678 tipc_sk_proto_rcv(tsk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001679 return false;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001680 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001681
Jon Paul Maloycda36962015-07-22 10:11:20 -04001682 if (unlikely(usr == SOCK_WAKEUP)) {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001683 onode = msg_orignode(hdr);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001684 kfree_skb(skb);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001685 u32_del(&tsk->cong_links, onode);
1686 tsk->cong_link_cnt--;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001687 sk->sk_write_space(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001688 return false;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001689 }
1690
Jon Paul Maloycda36962015-07-22 10:11:20 -04001691 /* Drop if illegal message type */
1692 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1693 kfree_skb(skb);
1694 return false;
1695 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001696
Jon Paul Maloycda36962015-07-22 10:11:20 -04001697 /* Reject if wrong message type for current socket state */
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001698 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001699 if (msg_connected(hdr)) {
1700 err = TIPC_ERR_NO_PORT;
1701 goto reject;
1702 }
1703 } else if (unlikely(!filter_connect(tsk, skb))) {
1704 err = TIPC_ERR_NO_PORT;
1705 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001706 }
1707
1708 /* Reject message if there isn't room to queue it */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001709 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1710 err = TIPC_ERR_OVERLOAD;
1711 goto reject;
1712 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001713
Ying Xueaba79f32013-01-20 23:30:09 +01001714 /* Enqueue message */
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001715 TIPC_SKB_CB(skb)->bytes_read = 0;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001716 __skb_queue_tail(&sk->sk_receive_queue, skb);
1717 skb_set_owner_r(skb, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001718
David S. Miller676d2362014-04-11 16:15:36 -04001719 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001720 return true;
1721
1722reject:
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001723 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1724 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001725 return false;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001726}
1727
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001728/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001729 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001730 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001731 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001732 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001733 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001734 *
1735 * Returns 0
1736 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001737static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001738{
Jon Paul Maloycda36962015-07-22 10:11:20 -04001739 unsigned int truesize = skb->truesize;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001740 struct sk_buff_head xmitq;
1741 u32 dnode, selector;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001742
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001743 __skb_queue_head_init(&xmitq);
1744
1745 if (likely(filter_rcv(sk, skb, &xmitq))) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001746 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001747 return 0;
1748 }
1749
1750 if (skb_queue_empty(&xmitq))
1751 return 0;
1752
1753 /* Send response/rejected message */
1754 skb = __skb_dequeue(&xmitq);
1755 dnode = msg_destnode(buf_msg(skb));
1756 selector = msg_origport(buf_msg(skb));
1757 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
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 Paul Maloyf1d048f2016-06-17 06:35:57 -04001789 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 */
1836 while ((skb = __skb_dequeue(&xmitq))) {
1837 dnode = msg_destnode(buf_msg(skb));
1838 tipc_node_xmit_skb(net, skb, dnode, dport);
1839 }
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001840 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001841 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001842 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001843
1844 /* No destination socket => dequeue skb if still there */
1845 skb = tipc_skb_dequeue(inputq, dport);
1846 if (!skb)
1847 return;
1848
1849 /* Try secondary lookup if unresolved named message */
1850 err = TIPC_ERR_NO_PORT;
1851 if (tipc_msg_lookup_dest(net, skb, &err))
1852 goto xmit;
1853
1854 /* Prepare for message rejection */
1855 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001856 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001857xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001858 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001859 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001860 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001861}
1862
Ying Xue78eb3a52014-01-17 09:50:03 +08001863static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1864{
WANG Congd9dc8b02016-11-11 10:20:50 -08001865 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Ying Xue78eb3a52014-01-17 09:50:03 +08001866 struct sock *sk = sock->sk;
Ying Xue78eb3a52014-01-17 09:50:03 +08001867 int done;
1868
1869 do {
1870 int err = sock_error(sk);
1871 if (err)
1872 return err;
1873 if (!*timeo_p)
1874 return -ETIMEDOUT;
1875 if (signal_pending(current))
1876 return sock_intr_errno(*timeo_p);
1877
WANG Congd9dc8b02016-11-11 10:20:50 -08001878 add_wait_queue(sk_sleep(sk), &wait);
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001879 done = sk_wait_event(sk, timeo_p,
WANG Congd9dc8b02016-11-11 10:20:50 -08001880 sk->sk_state != TIPC_CONNECTING, &wait);
1881 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue78eb3a52014-01-17 09:50:03 +08001882 } while (!done);
1883 return 0;
1884}
1885
Per Lidenb97bf3f2006-01-02 19:04:38 +01001886/**
Ying Xue247f0f32014-02-18 16:06:46 +08001887 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001888 * @sock: socket structure
1889 * @dest: socket address for destination port
1890 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001891 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001892 *
1893 * Returns 0 on success, errno otherwise
1894 */
Ying Xue247f0f32014-02-18 16:06:46 +08001895static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1896 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001897{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001898 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001899 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001900 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1901 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001902 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001903 int previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001904 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001905
Allan Stephens0c3141e2008-04-15 00:22:02 -07001906 lock_sock(sk);
1907
Erik Hugnef2f80362015-03-19 09:02:19 +01001908 /* DGRAM/RDM connect(), just save the destaddr */
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001909 if (tipc_sk_type_connectionless(sk)) {
Erik Hugnef2f80362015-03-19 09:02:19 +01001910 if (dst->family == AF_UNSPEC) {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001911 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
Sasha Levin610600c2015-03-23 15:30:00 -04001912 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1913 res = -EINVAL;
Erik Hugnef2f80362015-03-19 09:02:19 +01001914 } else {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001915 memcpy(&tsk->peer, dest, destlen);
Erik Hugnef2f80362015-03-19 09:02:19 +01001916 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001917 goto exit;
1918 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001919
Allan Stephensb89741a2008-04-15 00:20:37 -07001920 /*
1921 * Reject connection attempt using multicast address
1922 *
1923 * Note: send_msg() validates the rest of the address fields,
1924 * so there's no need to do it here
1925 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001926 if (dst->addrtype == TIPC_ADDR_MCAST) {
1927 res = -EINVAL;
1928 goto exit;
1929 }
1930
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001931 previous = sk->sk_state;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001932
1933 switch (sk->sk_state) {
1934 case TIPC_OPEN:
Ying Xue584d24b2012-11-29 18:51:19 -05001935 /* Send a 'SYN-' to destination */
1936 m.msg_name = dest;
1937 m.msg_namelen = destlen;
1938
1939 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1940 * indicate send_msg() is never blocked.
1941 */
1942 if (!timeout)
1943 m.msg_flags = MSG_DONTWAIT;
1944
Ying Xue39a02952015-03-02 15:37:47 +08001945 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001946 if ((res < 0) && (res != -EWOULDBLOCK))
1947 goto exit;
1948
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001949 /* Just entered TIPC_CONNECTING state; the only
Ying Xue584d24b2012-11-29 18:51:19 -05001950 * difference is that return value in non-blocking
1951 * case is EINPROGRESS, rather than EALREADY.
1952 */
1953 res = -EINPROGRESS;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001954 /* fall thru' */
1955 case TIPC_CONNECTING:
1956 if (!timeout) {
1957 if (previous == TIPC_CONNECTING)
1958 res = -EALREADY;
Ying Xue78eb3a52014-01-17 09:50:03 +08001959 goto exit;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001960 }
Ying Xue78eb3a52014-01-17 09:50:03 +08001961 timeout = msecs_to_jiffies(timeout);
1962 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1963 res = tipc_wait_for_connect(sock, &timeout);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001964 break;
1965 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001966 res = -EISCONN;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001967 break;
1968 default:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001969 res = -EINVAL;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001970 }
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001971
Allan Stephens0c3141e2008-04-15 00:22:02 -07001972exit:
1973 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001974 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001975}
1976
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001977/**
Ying Xue247f0f32014-02-18 16:06:46 +08001978 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001979 * @sock: socket structure
1980 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001981 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001982 * Returns 0 on success, errno otherwise
1983 */
Ying Xue247f0f32014-02-18 16:06:46 +08001984static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001985{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001986 struct sock *sk = sock->sk;
1987 int res;
1988
1989 lock_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001990 res = tipc_set_sk_state(sk, TIPC_LISTEN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001991 release_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001992
Allan Stephens0c3141e2008-04-15 00:22:02 -07001993 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001994}
1995
Ying Xue6398e232014-01-17 09:50:04 +08001996static int tipc_wait_for_accept(struct socket *sock, long timeo)
1997{
1998 struct sock *sk = sock->sk;
1999 DEFINE_WAIT(wait);
2000 int err;
2001
2002 /* True wake-one mechanism for incoming connections: only
2003 * one process gets woken up, not the 'whole herd'.
2004 * Since we do not 'race & poll' for established sockets
2005 * anymore, the common case will execute the loop only once.
2006 */
2007 for (;;) {
2008 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2009 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01002010 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08002011 release_sock(sk);
2012 timeo = schedule_timeout(timeo);
2013 lock_sock(sk);
2014 }
2015 err = 0;
2016 if (!skb_queue_empty(&sk->sk_receive_queue))
2017 break;
Ying Xue6398e232014-01-17 09:50:04 +08002018 err = -EAGAIN;
2019 if (!timeo)
2020 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002021 err = sock_intr_errno(timeo);
2022 if (signal_pending(current))
2023 break;
Ying Xue6398e232014-01-17 09:50:04 +08002024 }
2025 finish_wait(sk_sleep(sk), &wait);
2026 return err;
2027}
2028
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002029/**
Ying Xue247f0f32014-02-18 16:06:46 +08002030 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002031 * @sock: listening socket
2032 * @newsock: new socket that is to be connected
2033 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002034 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002035 * Returns 0 on success, errno otherwise
2036 */
David Howellscdfbabf2017-03-09 08:09:05 +00002037static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
2038 bool kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002039{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002040 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002041 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002042 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002043 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002044 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002045 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002046
Allan Stephens0c3141e2008-04-15 00:22:02 -07002047 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002048
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002049 if (sk->sk_state != TIPC_LISTEN) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002050 res = -EINVAL;
2051 goto exit;
2052 }
Ying Xue6398e232014-01-17 09:50:04 +08002053 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2054 res = tipc_wait_for_accept(sock, timeo);
2055 if (res)
2056 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002057
2058 buf = skb_peek(&sk->sk_receive_queue);
2059
David Howellscdfbabf2017-03-09 08:09:05 +00002060 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002061 if (res)
2062 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002063 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002064
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002065 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002066 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002067 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002068
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002069 /* we lock on new_sk; but lockdep sees the lock on sk */
2070 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002071
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002072 /*
2073 * Reject any stray messages received by new socket
2074 * before the socket lock was taken (very, very unlikely)
2075 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002076 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002077
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002078 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002079 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002080
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002081 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002082 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002083 new_tsock->conn_type = msg_nametype(msg);
2084 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002085 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002086
2087 /*
2088 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2089 * Respond to 'SYN+' by queuing it on new socket.
2090 */
2091 if (!msg_data_sz(msg)) {
2092 struct msghdr m = {NULL,};
2093
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002094 tsk_advance_rx_queue(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002095 __tipc_sendstream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002096 } else {
2097 __skb_dequeue(&sk->sk_receive_queue);
2098 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002099 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002100 }
2101 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002102exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002103 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002104 return res;
2105}
2106
2107/**
Ying Xue247f0f32014-02-18 16:06:46 +08002108 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002109 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002110 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002111 *
2112 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002113 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002114 * Returns 0 on success, errno otherwise
2115 */
Ying Xue247f0f32014-02-18 16:06:46 +08002116static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002117{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002118 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002119 int res;
2120
Allan Stephense247a8f2008-03-06 15:05:38 -08002121 if (how != SHUT_RDWR)
2122 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002123
Allan Stephens0c3141e2008-04-15 00:22:02 -07002124 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002125
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002126 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2127 sk->sk_shutdown = SEND_SHUTDOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002128
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002129 if (sk->sk_state == TIPC_DISCONNECTING) {
Ying Xue75031152012-10-29 09:38:15 -04002130 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002131 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002132
2133 /* Wake up anyone sleeping in poll */
2134 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002135 res = 0;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002136 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002137 res = -ENOTCONN;
2138 }
2139
Allan Stephens0c3141e2008-04-15 00:22:02 -07002140 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002141 return res;
2142}
2143
Ying Xuef2f2a962015-01-09 15:27:02 +08002144static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002145{
Ying Xuef2f2a962015-01-09 15:27:02 +08002146 struct tipc_sock *tsk = (struct tipc_sock *)data;
2147 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002148 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002149 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002150 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002151
Jon Paul Maloy57289012014-08-22 18:09:09 -04002152 bh_lock_sock(sk);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002153 if (!tipc_sk_connected(sk)) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002154 bh_unlock_sock(sk);
2155 goto exit;
2156 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002157 peer_port = tsk_peer_port(tsk);
2158 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002159
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002160 if (tsk->probe_unacked) {
Erik Hugneb3be5e32015-06-09 17:27:12 +02002161 if (!sock_owned_by_user(sk)) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01002162 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugneb3be5e32015-06-09 17:27:12 +02002163 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2164 tsk_peer_port(tsk));
2165 sk->sk_state_change(sk);
2166 } else {
2167 /* Try again later */
2168 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2169 }
2170
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002171 bh_unlock_sock(sk);
2172 goto exit;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002173 }
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002174
2175 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2176 INT_H_SIZE, 0, peer_node, own_node,
2177 peer_port, tsk->portid, TIPC_OK);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002178 tsk->probe_unacked = true;
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002179 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002180 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002181 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002182 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002183exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002184 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002185}
2186
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002187static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002188 struct tipc_name_seq const *seq)
2189{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002190 struct sock *sk = &tsk->sk;
2191 struct net *net = sock_net(sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002192 struct publication *publ;
2193 u32 key;
2194
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002195 if (tipc_sk_connected(sk))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002196 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002197 key = tsk->portid + tsk->pub_count + 1;
2198 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002199 return -EADDRINUSE;
2200
Ying Xuef2f98002015-01-09 15:27:05 +08002201 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002202 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002203 if (unlikely(!publ))
2204 return -EINVAL;
2205
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002206 list_add(&publ->pport_list, &tsk->publications);
2207 tsk->pub_count++;
2208 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002209 return 0;
2210}
2211
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002212static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002213 struct tipc_name_seq const *seq)
2214{
Ying Xuef2f98002015-01-09 15:27:05 +08002215 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002216 struct publication *publ;
2217 struct publication *safe;
2218 int rc = -EINVAL;
2219
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002220 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002221 if (seq) {
2222 if (publ->scope != scope)
2223 continue;
2224 if (publ->type != seq->type)
2225 continue;
2226 if (publ->lower != seq->lower)
2227 continue;
2228 if (publ->upper != seq->upper)
2229 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002230 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002231 publ->ref, publ->key);
2232 rc = 0;
2233 break;
2234 }
Ying Xuef2f98002015-01-09 15:27:05 +08002235 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002236 publ->ref, publ->key);
2237 rc = 0;
2238 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002239 if (list_empty(&tsk->publications))
2240 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002241 return rc;
2242}
2243
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002244/* tipc_sk_reinit: set non-zero address in all existing sockets
2245 * when we go from standalone to network mode.
2246 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002247void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002248{
Ying Xuee05b31f2015-01-09 15:27:08 +08002249 struct tipc_net *tn = net_generic(net, tipc_net_id);
Herbert Xu40f9f432017-02-11 19:26:46 +08002250 struct rhashtable_iter iter;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002251 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002252 struct tipc_msg *msg;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002253
Herbert Xu40f9f432017-02-11 19:26:46 +08002254 rhashtable_walk_enter(&tn->sk_rht, &iter);
2255
2256 do {
2257 tsk = ERR_PTR(rhashtable_walk_start(&iter));
2258 if (tsk)
2259 continue;
2260
2261 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002262 spin_lock_bh(&tsk->sk.sk_lock.slock);
2263 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002264 msg_set_prevnode(msg, tn->own_addr);
2265 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002266 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2267 }
Herbert Xu40f9f432017-02-11 19:26:46 +08002268
2269 rhashtable_walk_stop(&iter);
2270 } while (tsk == ERR_PTR(-EAGAIN));
Ying Xue07f6c4b2015-01-07 13:41:58 +08002271}
2272
Ying Xuee05b31f2015-01-09 15:27:08 +08002273static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002274{
Ying Xuee05b31f2015-01-09 15:27:08 +08002275 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002276 struct tipc_sock *tsk;
2277
2278 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002279 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002280 if (tsk)
2281 sock_hold(&tsk->sk);
2282 rcu_read_unlock();
2283
2284 return tsk;
2285}
2286
2287static int tipc_sk_insert(struct tipc_sock *tsk)
2288{
Ying Xuee05b31f2015-01-09 15:27:08 +08002289 struct sock *sk = &tsk->sk;
2290 struct net *net = sock_net(sk);
2291 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002292 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2293 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2294
2295 while (remaining--) {
2296 portid++;
2297 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2298 portid = TIPC_MIN_PORT;
2299 tsk->portid = portid;
2300 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002301 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2302 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002303 return 0;
2304 sock_put(&tsk->sk);
2305 }
2306
2307 return -1;
2308}
2309
2310static void tipc_sk_remove(struct tipc_sock *tsk)
2311{
2312 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002313 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002314
Herbert Xu6cca72892015-03-20 21:57:05 +11002315 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002316 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2317 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002318 }
2319}
2320
Herbert Xu6cca72892015-03-20 21:57:05 +11002321static const struct rhashtable_params tsk_rht_params = {
2322 .nelem_hint = 192,
2323 .head_offset = offsetof(struct tipc_sock, node),
2324 .key_offset = offsetof(struct tipc_sock, portid),
2325 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11002326 .max_size = 1048576,
2327 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002328 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11002329};
2330
Ying Xuee05b31f2015-01-09 15:27:08 +08002331int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002332{
Ying Xuee05b31f2015-01-09 15:27:08 +08002333 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002334
Herbert Xu6cca72892015-03-20 21:57:05 +11002335 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002336}
2337
Ying Xuee05b31f2015-01-09 15:27:08 +08002338void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002339{
Ying Xuee05b31f2015-01-09 15:27:08 +08002340 struct tipc_net *tn = net_generic(net, tipc_net_id);
2341
Ying Xue07f6c4b2015-01-07 13:41:58 +08002342 /* Wait for socket readers to complete */
2343 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002344
Ying Xuee05b31f2015-01-09 15:27:08 +08002345 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002346}
2347
2348/**
Ying Xue247f0f32014-02-18 16:06:46 +08002349 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002350 * @sock: socket structure
2351 * @lvl: option level
2352 * @opt: option identifier
2353 * @ov: pointer to new option value
2354 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002355 *
2356 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002357 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002358 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002359 * Returns 0 on success, errno otherwise
2360 */
Ying Xue247f0f32014-02-18 16:06:46 +08002361static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2362 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002363{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002364 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002365 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002366 u32 value = 0;
Dan Carpentera08ef472017-01-24 12:49:35 +03002367 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002368
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002369 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2370 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002371 if (lvl != SOL_TIPC)
2372 return -ENOPROTOOPT;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002373
2374 switch (opt) {
2375 case TIPC_IMPORTANCE:
2376 case TIPC_SRC_DROPPABLE:
2377 case TIPC_DEST_DROPPABLE:
2378 case TIPC_CONN_TIMEOUT:
2379 if (ol < sizeof(value))
2380 return -EINVAL;
2381 res = get_user(value, (u32 __user *)ov);
2382 if (res)
2383 return res;
2384 break;
2385 default:
2386 if (ov || ol)
2387 return -EINVAL;
2388 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002389
Allan Stephens0c3141e2008-04-15 00:22:02 -07002390 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002391
Per Lidenb97bf3f2006-01-02 19:04:38 +01002392 switch (opt) {
2393 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002394 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002395 break;
2396 case TIPC_SRC_DROPPABLE:
2397 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002398 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002399 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002400 res = -ENOPROTOOPT;
2401 break;
2402 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002403 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002404 break;
2405 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002406 tipc_sk(sk)->conn_timeout = value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002407 break;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002408 case TIPC_MCAST_BROADCAST:
2409 tsk->mc_method.rcast = false;
2410 tsk->mc_method.mandatory = true;
2411 break;
2412 case TIPC_MCAST_REPLICAST:
2413 tsk->mc_method.rcast = true;
2414 tsk->mc_method.mandatory = true;
2415 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002416 default:
2417 res = -EINVAL;
2418 }
2419
Allan Stephens0c3141e2008-04-15 00:22:02 -07002420 release_sock(sk);
2421
Per Lidenb97bf3f2006-01-02 19:04:38 +01002422 return res;
2423}
2424
2425/**
Ying Xue247f0f32014-02-18 16:06:46 +08002426 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002427 * @sock: socket structure
2428 * @lvl: option level
2429 * @opt: option identifier
2430 * @ov: receptacle for option value
2431 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002432 *
2433 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002434 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002435 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002436 * Returns 0 on success, errno otherwise
2437 */
Ying Xue247f0f32014-02-18 16:06:46 +08002438static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2439 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002440{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002441 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002442 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002443 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002444 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002445 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002446
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002447 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2448 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002449 if (lvl != SOL_TIPC)
2450 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002451 res = get_user(len, ol);
2452 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002453 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002454
Allan Stephens0c3141e2008-04-15 00:22:02 -07002455 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002456
2457 switch (opt) {
2458 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002459 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002460 break;
2461 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002462 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002463 break;
2464 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002465 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002466 break;
2467 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002468 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002469 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002470 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002471 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002472 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002473 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002474 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002475 value = skb_queue_len(&sk->sk_receive_queue);
2476 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002477 default:
2478 res = -EINVAL;
2479 }
2480
Allan Stephens0c3141e2008-04-15 00:22:02 -07002481 release_sock(sk);
2482
Paul Gortmaker25860c32010-12-31 18:59:31 +00002483 if (res)
2484 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002485
Paul Gortmaker25860c32010-12-31 18:59:31 +00002486 if (len < sizeof(value))
2487 return -EINVAL;
2488
2489 if (copy_to_user(ov, &value, sizeof(value)))
2490 return -EFAULT;
2491
2492 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002493}
2494
Ying Xuef2f98002015-01-09 15:27:05 +08002495static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002496{
Ying Xuef2f98002015-01-09 15:27:05 +08002497 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002498 struct tipc_sioc_ln_req lnr;
2499 void __user *argp = (void __user *)arg;
2500
2501 switch (cmd) {
2502 case SIOCGETLINKNAME:
2503 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2504 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002505 if (!tipc_node_get_linkname(sock_net(sk),
2506 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002507 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2508 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2509 return -EFAULT;
2510 return 0;
2511 }
2512 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002513 default:
2514 return -ENOIOCTLCMD;
2515 }
2516}
2517
Erik Hugne70b03752017-03-29 11:22:16 +02002518static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
2519{
2520 struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
2521 struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
Erik Hugne66bc1e82017-03-29 11:22:17 +02002522 u32 onode = tipc_own_addr(sock_net(sock1->sk));
Erik Hugne70b03752017-03-29 11:22:16 +02002523
Erik Hugne66bc1e82017-03-29 11:22:17 +02002524 tsk1->peer.family = AF_TIPC;
2525 tsk1->peer.addrtype = TIPC_ADDR_ID;
2526 tsk1->peer.scope = TIPC_NODE_SCOPE;
2527 tsk1->peer.addr.id.ref = tsk2->portid;
2528 tsk1->peer.addr.id.node = onode;
2529 tsk2->peer.family = AF_TIPC;
2530 tsk2->peer.addrtype = TIPC_ADDR_ID;
2531 tsk2->peer.scope = TIPC_NODE_SCOPE;
2532 tsk2->peer.addr.id.ref = tsk1->portid;
2533 tsk2->peer.addr.id.node = onode;
2534
2535 tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
2536 tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
Erik Hugne70b03752017-03-29 11:22:16 +02002537 return 0;
2538}
2539
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002540/* Protocol switches for the various types of TIPC sockets */
2541
Florian Westphalbca65ea2008-02-07 18:18:01 -08002542static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002543 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002544 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002545 .release = tipc_release,
2546 .bind = tipc_bind,
2547 .connect = tipc_connect,
Erik Hugne66bc1e82017-03-29 11:22:17 +02002548 .socketpair = tipc_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002549 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002550 .getname = tipc_getname,
2551 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002552 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002553 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002554 .shutdown = tipc_shutdown,
2555 .setsockopt = tipc_setsockopt,
2556 .getsockopt = tipc_getsockopt,
2557 .sendmsg = tipc_sendmsg,
2558 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002559 .mmap = sock_no_mmap,
2560 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002561};
2562
Florian Westphalbca65ea2008-02-07 18:18:01 -08002563static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002564 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002565 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002566 .release = tipc_release,
2567 .bind = tipc_bind,
2568 .connect = tipc_connect,
Erik Hugne70b03752017-03-29 11:22:16 +02002569 .socketpair = tipc_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002570 .accept = tipc_accept,
2571 .getname = tipc_getname,
2572 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002573 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002574 .listen = tipc_listen,
2575 .shutdown = tipc_shutdown,
2576 .setsockopt = tipc_setsockopt,
2577 .getsockopt = tipc_getsockopt,
2578 .sendmsg = tipc_send_packet,
2579 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002580 .mmap = sock_no_mmap,
2581 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002582};
2583
Florian Westphalbca65ea2008-02-07 18:18:01 -08002584static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002585 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002586 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002587 .release = tipc_release,
2588 .bind = tipc_bind,
2589 .connect = tipc_connect,
Erik Hugne70b03752017-03-29 11:22:16 +02002590 .socketpair = tipc_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002591 .accept = tipc_accept,
2592 .getname = tipc_getname,
2593 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002594 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002595 .listen = tipc_listen,
2596 .shutdown = tipc_shutdown,
2597 .setsockopt = tipc_setsockopt,
2598 .getsockopt = tipc_getsockopt,
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002599 .sendmsg = tipc_sendstream,
Jon Paul Maloyec8a09f2017-05-02 18:16:54 +02002600 .recvmsg = tipc_recvstream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002601 .mmap = sock_no_mmap,
2602 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002603};
2604
Florian Westphalbca65ea2008-02-07 18:18:01 -08002605static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002606 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002607 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002608 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002609};
2610
2611static struct proto tipc_proto = {
2612 .name = "TIPC",
2613 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002614 .obj_size = sizeof(struct tipc_sock),
2615 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002616};
2617
2618/**
Per Liden4323add2006-01-18 00:38:21 +01002619 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002620 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002621 * Returns 0 on success, errno otherwise
2622 */
Per Liden4323add2006-01-18 00:38:21 +01002623int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002624{
2625 int res;
2626
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002627 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002628 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002629 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002630 goto out;
2631 }
2632
2633 res = sock_register(&tipc_family_ops);
2634 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002635 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002636 proto_unregister(&tipc_proto);
2637 goto out;
2638 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002639 out:
2640 return res;
2641}
2642
2643/**
Per Liden4323add2006-01-18 00:38:21 +01002644 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002645 */
Per Liden4323add2006-01-18 00:38:21 +01002646void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002647{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002648 sock_unregister(tipc_family_ops.family);
2649 proto_unregister(&tipc_proto);
2650}
Richard Alpe34b78a122014-11-20 10:29:10 +01002651
2652/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002653static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002654{
2655 u32 peer_node;
2656 u32 peer_port;
2657 struct nlattr *nest;
2658
2659 peer_node = tsk_peer_node(tsk);
2660 peer_port = tsk_peer_port(tsk);
2661
2662 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2663
2664 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2665 goto msg_full;
2666 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2667 goto msg_full;
2668
2669 if (tsk->conn_type != 0) {
2670 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2671 goto msg_full;
2672 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2673 goto msg_full;
2674 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2675 goto msg_full;
2676 }
2677 nla_nest_end(skb, nest);
2678
2679 return 0;
2680
2681msg_full:
2682 nla_nest_cancel(skb, nest);
2683
2684 return -EMSGSIZE;
2685}
2686
2687/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002688static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2689 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002690{
2691 int err;
2692 void *hdr;
2693 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002694 struct net *net = sock_net(skb->sk);
2695 struct tipc_net *tn = net_generic(net, tipc_net_id);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002696 struct sock *sk = &tsk->sk;
Richard Alpe34b78a122014-11-20 10:29:10 +01002697
2698 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002699 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002700 if (!hdr)
2701 goto msg_cancel;
2702
2703 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2704 if (!attrs)
2705 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002706 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002707 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002708 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002709 goto attr_msg_cancel;
2710
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002711 if (tipc_sk_connected(sk)) {
Richard Alpe34b78a122014-11-20 10:29:10 +01002712 err = __tipc_nl_add_sk_con(skb, tsk);
2713 if (err)
2714 goto attr_msg_cancel;
2715 } else if (!list_empty(&tsk->publications)) {
2716 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2717 goto attr_msg_cancel;
2718 }
2719 nla_nest_end(skb, attrs);
2720 genlmsg_end(skb, hdr);
2721
2722 return 0;
2723
2724attr_msg_cancel:
2725 nla_nest_cancel(skb, attrs);
2726genlmsg_cancel:
2727 genlmsg_cancel(skb, hdr);
2728msg_cancel:
2729 return -EMSGSIZE;
2730}
2731
2732int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2733{
2734 int err;
2735 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002736 const struct bucket_table *tbl;
2737 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002738 struct net *net = sock_net(skb->sk);
2739 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002740 u32 tbl_id = cb->args[0];
2741 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002742
Ying Xue07f6c4b2015-01-07 13:41:58 +08002743 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002744 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002745 for (; tbl_id < tbl->size; tbl_id++) {
2746 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002747 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002748 if (prev_portid && prev_portid != tsk->portid) {
2749 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2750 continue;
2751 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002752
Richard Alped6e164e2015-01-16 12:30:40 +01002753 err = __tipc_nl_add_sk(skb, cb, tsk);
2754 if (err) {
2755 prev_portid = tsk->portid;
2756 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2757 goto out;
2758 }
2759 prev_portid = 0;
2760 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002761 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002762 }
Richard Alped6e164e2015-01-16 12:30:40 +01002763out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002764 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002765 cb->args[0] = tbl_id;
2766 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002767
2768 return skb->len;
2769}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002770
2771/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002772static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2773 struct netlink_callback *cb,
2774 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002775{
2776 void *hdr;
2777 struct nlattr *attrs;
2778
2779 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002780 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002781 if (!hdr)
2782 goto msg_cancel;
2783
2784 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2785 if (!attrs)
2786 goto genlmsg_cancel;
2787
2788 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2789 goto attr_msg_cancel;
2790 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2791 goto attr_msg_cancel;
2792 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2793 goto attr_msg_cancel;
2794 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2795 goto attr_msg_cancel;
2796
2797 nla_nest_end(skb, attrs);
2798 genlmsg_end(skb, hdr);
2799
2800 return 0;
2801
2802attr_msg_cancel:
2803 nla_nest_cancel(skb, attrs);
2804genlmsg_cancel:
2805 genlmsg_cancel(skb, hdr);
2806msg_cancel:
2807 return -EMSGSIZE;
2808}
2809
2810/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002811static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2812 struct netlink_callback *cb,
2813 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002814{
2815 int err;
2816 struct publication *p;
2817
2818 if (*last_publ) {
2819 list_for_each_entry(p, &tsk->publications, pport_list) {
2820 if (p->key == *last_publ)
2821 break;
2822 }
2823 if (p->key != *last_publ) {
2824 /* We never set seq or call nl_dump_check_consistent()
2825 * this means that setting prev_seq here will cause the
2826 * consistence check to fail in the netlink callback
2827 * handler. Resulting in the last NLMSG_DONE message
2828 * having the NLM_F_DUMP_INTR flag set.
2829 */
2830 cb->prev_seq = 1;
2831 *last_publ = 0;
2832 return -EPIPE;
2833 }
2834 } else {
2835 p = list_first_entry(&tsk->publications, struct publication,
2836 pport_list);
2837 }
2838
2839 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2840 err = __tipc_nl_add_sk_publ(skb, cb, p);
2841 if (err) {
2842 *last_publ = p->key;
2843 return err;
2844 }
2845 }
2846 *last_publ = 0;
2847
2848 return 0;
2849}
2850
2851int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2852{
2853 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002854 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002855 u32 last_publ = cb->args[1];
2856 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002857 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002858 struct tipc_sock *tsk;
2859
Ying Xue07f6c4b2015-01-07 13:41:58 +08002860 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002861 struct nlattr **attrs;
2862 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2863
2864 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2865 if (err)
2866 return err;
2867
Richard Alpe45e093a2016-05-16 11:14:54 +02002868 if (!attrs[TIPC_NLA_SOCK])
2869 return -EINVAL;
2870
Richard Alpe1a1a1432014-11-20 10:29:11 +01002871 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2872 attrs[TIPC_NLA_SOCK],
Johannes Bergfceb6432017-04-12 14:34:07 +02002873 tipc_nl_sock_policy, NULL);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002874 if (err)
2875 return err;
2876
2877 if (!sock[TIPC_NLA_SOCK_REF])
2878 return -EINVAL;
2879
Ying Xue07f6c4b2015-01-07 13:41:58 +08002880 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002881 }
2882
2883 if (done)
2884 return 0;
2885
Ying Xuee05b31f2015-01-09 15:27:08 +08002886 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002887 if (!tsk)
2888 return -EINVAL;
2889
2890 lock_sock(&tsk->sk);
2891 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2892 if (!err)
2893 done = 1;
2894 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002895 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002896
Ying Xue07f6c4b2015-01-07 13:41:58 +08002897 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002898 cb->args[1] = last_publ;
2899 cb->args[2] = done;
2900
2901 return skb->len;
2902}