blob: 3855bfd1fb1bdd7c9db129b719ff92138196beda [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
365#define tipc_wait_for_cond(sock_, timeout_, condition_) \
366({ \
367 int rc_ = 0; \
368 int done_ = 0; \
369 \
370 while (!(condition_) && !done_) { \
371 struct sock *sk_ = sock->sk; \
372 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
373 \
374 rc_ = tipc_sk_sock_err(sock_, timeout_); \
375 if (rc_) \
376 break; \
377 prepare_to_wait(sk_sleep(sk_), &wait_, \
378 TASK_INTERRUPTIBLE); \
379 done_ = sk_wait_event(sk_, timeout_, \
380 (condition_), &wait_); \
381 remove_wait_queue(sk_sleep(sk_), &wait_); \
382 } \
383 rc_; \
384})
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 Bhuvaragan8ea642e2016-11-01 14:02:44 +0100870 tsk->probe_unacked = false;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500871
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400872 if (mtyp == CONN_PROBE) {
873 msg_set_type(hdr, CONN_PROBE_REPLY);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400874 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
875 __skb_queue_tail(xmitq, skb);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400876 return;
877 } else if (mtyp == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400878 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400879 tsk->snt_unacked -= msg_conn_ack(hdr);
880 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
881 tsk->snd_win = msg_adv_win(hdr);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500882 if (conn_cong)
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400883 sk->sk_write_space(sk);
884 } else if (mtyp != CONN_PROBE_REPLY) {
885 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500886 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500887exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400888 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500889}
890
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500891/**
Ying Xue247f0f32014-02-18 16:06:46 +0800892 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893 * @sock: socket structure
894 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500895 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900896 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900898 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
900 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900901 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100902 * Returns the number of bytes sent on success, or errno otherwise
903 */
Ying Xue1b784142015-03-02 15:37:48 +0800904static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500905 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906{
Ying Xue39a02952015-03-02 15:37:47 +0800907 struct sock *sk = sock->sk;
908 int ret;
909
910 lock_sock(sk);
911 ret = __tipc_sendmsg(sock, m, dsz);
912 release_sock(sk);
913
914 return ret;
915}
916
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500917static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a02952015-03-02 15:37:47 +0800918{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700919 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +0800920 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500921 struct tipc_sock *tsk = tipc_sk(sk);
922 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
923 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
924 struct list_head *clinks = &tsk->cong_links;
925 bool syn = !tipc_sk_type_connectionless(sk);
926 struct tipc_msg *hdr = &tsk->phdr;
Erik Hugnef2f80362015-03-19 09:02:19 +0100927 struct tipc_name_seq *seq;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500928 struct sk_buff_head pkts;
929 u32 type, inst, domain;
930 u32 dnode, dport;
931 int mtu, rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100932
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500933 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400934 return -EMSGSIZE;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500935
Erik Hugnef2f80362015-03-19 09:02:19 +0100936 if (unlikely(!dest)) {
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500937 dest = &tsk->peer;
938 if (!syn || dest->family != AF_TIPC)
Erik Hugnef2f80362015-03-19 09:02:19 +0100939 return -EDESTADDRREQ;
Erik Hugnef2f80362015-03-19 09:02:19 +0100940 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500941
942 if (unlikely(m->msg_namelen < sizeof(*dest)))
943 return -EINVAL;
944
945 if (unlikely(dest->family != AF_TIPC))
946 return -EINVAL;
947
948 if (unlikely(syn)) {
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100949 if (sk->sk_state == TIPC_LISTEN)
Ying Xue39a02952015-03-02 15:37:47 +0800950 return -EPIPE;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100951 if (sk->sk_state != TIPC_OPEN)
Ying Xue39a02952015-03-02 15:37:47 +0800952 return -EISCONN;
953 if (tsk->published)
954 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700955 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400956 tsk->conn_type = dest->addr.name.name.type;
957 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700958 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100959 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500960
Erik Hugnef2f80362015-03-19 09:02:19 +0100961 seq = &dest->addr.nameseq;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500962 if (dest->addrtype == TIPC_ADDR_MCAST)
963 return tipc_sendmcast(sock, seq, m, dlen, timeout);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500964
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500965 if (dest->addrtype == TIPC_ADDR_NAME) {
966 type = dest->addr.name.name.type;
967 inst = dest->addr.name.name.instance;
968 domain = dest->addr.name.domain;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500969 dnode = domain;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500970 msg_set_type(hdr, TIPC_NAMED_MSG);
971 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
972 msg_set_nametype(hdr, type);
973 msg_set_nameinst(hdr, inst);
974 msg_set_lookup_scope(hdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800975 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500976 msg_set_destnode(hdr, dnode);
977 msg_set_destport(hdr, dport);
Ying Xue39a02952015-03-02 15:37:47 +0800978 if (unlikely(!dport && !dnode))
979 return -EHOSTUNREACH;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500980
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500981 } else if (dest->addrtype == TIPC_ADDR_ID) {
982 dnode = dest->addr.id.node;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500983 msg_set_type(hdr, TIPC_DIRECT_MSG);
984 msg_set_lookup_scope(hdr, 0);
985 msg_set_destnode(hdr, dnode);
986 msg_set_destport(hdr, dest->addr.id.ref);
987 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500988 }
989
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500990 /* Block or return if destination link is congested */
991 rc = tipc_wait_for_cond(sock, &timeout, !u32_find(clinks, dnode));
992 if (unlikely(rc))
Ying Xue39a02952015-03-02 15:37:47 +0800993 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500994
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500995 skb_queue_head_init(&pkts);
996 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
997 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
998 if (unlikely(rc != dlen))
999 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -05001000
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001001 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1002 if (unlikely(rc == -ELINKCONG)) {
1003 u32_push(clinks, dnode);
1004 tsk->cong_link_cnt++;
1005 rc = 0;
1006 }
1007
1008 if (unlikely(syn && !rc))
1009 tipc_set_sk_state(sk, TIPC_CONNECTING);
1010
1011 return rc ? rc : dlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001012}
1013
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001014/**
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001015 * tipc_sendstream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016 * @sock: socket structure
1017 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001018 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001019 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001021 *
1022 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001023 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024 */
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001025static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001026{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001027 struct sock *sk = sock->sk;
Ying Xue39a02952015-03-02 15:37:47 +08001028 int ret;
1029
1030 lock_sock(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001031 ret = __tipc_sendstream(sock, m, dsz);
Ying Xue39a02952015-03-02 15:37:47 +08001032 release_sock(sk);
1033
1034 return ret;
1035}
1036
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001037static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a02952015-03-02 15:37:47 +08001038{
1039 struct sock *sk = sock->sk;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001040 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001041 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1042 struct tipc_sock *tsk = tipc_sk(sk);
1043 struct tipc_msg *hdr = &tsk->phdr;
1044 struct net *net = sock_net(sk);
1045 struct sk_buff_head pkts;
1046 u32 dnode = tsk_peer_node(tsk);
1047 int send, sent = 0;
1048 int rc = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001049
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001050 skb_queue_head_init(&pkts);
1051
1052 if (unlikely(dlen > INT_MAX))
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001053 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001054
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001055 /* Handle implicit connection setup */
1056 if (unlikely(dest)) {
1057 rc = __tipc_sendmsg(sock, m, dlen);
1058 if (dlen && (dlen == rc))
1059 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
1060 return rc;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001061 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001063 do {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001064 rc = tipc_wait_for_cond(sock, &timeout,
1065 (!tsk->cong_link_cnt &&
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -05001066 !tsk_conn_cong(tsk) &&
1067 tipc_sk_connected(sk)));
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001068 if (unlikely(rc))
1069 break;
Ying Xue39a02952015-03-02 15:37:47 +08001070
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001071 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
1072 rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
1073 if (unlikely(rc != send))
1074 break;
1075
1076 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1077 if (unlikely(rc == -ELINKCONG)) {
1078 tsk->cong_link_cnt = 1;
1079 rc = 0;
1080 }
1081 if (likely(!rc)) {
1082 tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
1083 sent += send;
1084 }
1085 } while (sent < dlen && !rc);
1086
Parthasarathy Bhuvaragan3364d612017-04-24 15:00:42 +02001087 return sent ? sent : rc;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001088}
1089
1090/**
1091 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001092 * @sock: socket structure
1093 * @m: message to send
1094 * @dsz: length of data to be transmitted
1095 *
1096 * Used for SOCK_SEQPACKET messages.
1097 *
1098 * Returns the number of bytes sent on success, or errno otherwise
1099 */
Ying Xue1b784142015-03-02 15:37:48 +08001100static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001101{
1102 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1103 return -EMSGSIZE;
1104
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001105 return tipc_sendstream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001106}
1107
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001108/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001109 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001110static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001111 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001112{
Ying Xue3721e9c2015-01-13 17:07:48 +08001113 struct sock *sk = &tsk->sk;
1114 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001115 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001116
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001117 msg_set_destnode(msg, peer_node);
1118 msg_set_destport(msg, peer_port);
1119 msg_set_type(msg, TIPC_CONN_MSG);
1120 msg_set_lookup_scope(msg, 0);
1121 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001122
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01001123 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001124 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
Ying Xuef2f98002015-01-09 15:27:05 +08001125 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1126 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001127 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001128 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1129 return;
1130
1131 /* Fall back to message based flow control */
1132 tsk->rcv_win = FLOWCTL_MSG_WIN;
1133 tsk->snd_win = FLOWCTL_MSG_WIN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134}
1135
1136/**
1137 * set_orig_addr - capture sender's address for received message
1138 * @m: descriptor for message info
1139 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001140 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001141 * Note: Address is not captured if not requested by receiver.
1142 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001143static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001145 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001147 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001148 addr->family = AF_TIPC;
1149 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001150 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001151 addr->addr.id.ref = msg_origport(msg);
1152 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001153 addr->addr.name.domain = 0; /* could leave uninitialized */
1154 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001155 m->msg_namelen = sizeof(struct sockaddr_tipc);
1156 }
1157}
1158
1159/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001160 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 * @m: descriptor for message info
1162 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001163 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001164 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001165 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001166 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001167 * Returns 0 if successful, otherwise errno
1168 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001169static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1170 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171{
1172 u32 anc_data[3];
1173 u32 err;
1174 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001175 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176 int res;
1177
1178 if (likely(m->msg_controllen == 0))
1179 return 0;
1180
1181 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001182 err = msg ? msg_errcode(msg) : 0;
1183 if (unlikely(err)) {
1184 anc_data[0] = err;
1185 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001186 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1187 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001188 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001189 if (anc_data[1]) {
1190 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1191 msg_data(msg));
1192 if (res)
1193 return res;
1194 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001195 }
1196
1197 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1199 switch (dest_type) {
1200 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001201 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001202 anc_data[0] = msg_nametype(msg);
1203 anc_data[1] = msg_namelower(msg);
1204 anc_data[2] = msg_namelower(msg);
1205 break;
1206 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001207 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 anc_data[0] = msg_nametype(msg);
1209 anc_data[1] = msg_namelower(msg);
1210 anc_data[2] = msg_nameupper(msg);
1211 break;
1212 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001213 has_name = (tsk->conn_type != 0);
1214 anc_data[0] = tsk->conn_type;
1215 anc_data[1] = tsk->conn_instance;
1216 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001217 break;
1218 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001219 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001220 }
Allan Stephens2db99832010-12-31 18:59:33 +00001221 if (has_name) {
1222 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1223 if (res)
1224 return res;
1225 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001226
1227 return 0;
1228}
1229
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001230static void tipc_sk_send_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001231{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001232 struct sock *sk = &tsk->sk;
1233 struct net *net = sock_net(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001234 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001235 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001236 u32 peer_port = tsk_peer_port(tsk);
1237 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001238
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001239 if (!tipc_sk_connected(sk))
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001240 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001241 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1242 dnode, tsk_own_node(tsk), peer_port,
1243 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001244 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001245 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001246 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001247 msg_set_conn_ack(msg, tsk->rcv_unacked);
1248 tsk->rcv_unacked = 0;
1249
1250 /* Adjust to and advertize the correct window limit */
1251 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1252 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1253 msg_set_adv_win(msg, tsk->rcv_win);
1254 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001255 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001256}
1257
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001258static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001259{
1260 struct sock *sk = sock->sk;
1261 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001262 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001263 int err;
1264
1265 for (;;) {
1266 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001267 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01001268 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001269 err = -ENOTCONN;
1270 break;
1271 }
1272 release_sock(sk);
1273 timeo = schedule_timeout(timeo);
1274 lock_sock(sk);
1275 }
1276 err = 0;
1277 if (!skb_queue_empty(&sk->sk_receive_queue))
1278 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001279 err = -EAGAIN;
1280 if (!timeo)
1281 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001282 err = sock_intr_errno(timeo);
1283 if (signal_pending(current))
1284 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001285 }
1286 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001287 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001288 return err;
1289}
1290
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001291/**
Ying Xue247f0f32014-02-18 16:06:46 +08001292 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293 * @m: descriptor for message info
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001294 * @buflen: length of user buffer area
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001296 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001297 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1298 * If the complete message doesn't fit in user area, truncate it.
1299 *
1300 * Returns size of returned message data, errno otherwise
1301 */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001302static int tipc_recvmsg(struct socket *sock, struct msghdr *m,
1303 size_t buflen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001304{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001305 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001306 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001307 struct sk_buff *skb;
1308 struct tipc_msg *hdr;
1309 bool connected = !tipc_sk_type_connectionless(sk);
1310 int rc, err, hlen, dlen, copy;
1311 long timeout;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001312
Allan Stephens0c3141e2008-04-15 00:22:02 -07001313 /* Catch invalid receive requests */
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001314 if (unlikely(!buflen))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001315 return -EINVAL;
1316
Allan Stephens0c3141e2008-04-15 00:22:02 -07001317 lock_sock(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001318 if (unlikely(connected && sk->sk_state == TIPC_OPEN)) {
1319 rc = -ENOTCONN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 goto exit;
1321 }
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001322 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001324 do {
1325 /* Look at first msg in receive queue; wait if necessary */
1326 rc = tipc_wait_for_rcvmsg(sock, &timeout);
1327 if (unlikely(rc))
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328 goto exit;
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001329 skb = skb_peek(&sk->sk_receive_queue);
1330 hdr = buf_msg(skb);
1331 dlen = msg_data_sz(hdr);
1332 hlen = msg_hdr_sz(hdr);
1333 err = msg_errcode(hdr);
1334 if (likely(dlen || err))
1335 break;
1336 tsk_advance_rx_queue(sk);
1337 } while (1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001339 /* Collect msg meta data, including error code and rejected data */
1340 set_orig_addr(m, hdr);
1341 rc = tipc_sk_anc_data_recv(m, hdr, tsk);
1342 if (unlikely(rc))
1343 goto exit;
1344
1345 /* Capture data if non-error msg, otherwise just set return value */
1346 if (likely(!err)) {
1347 copy = min_t(int, dlen, buflen);
1348 if (unlikely(copy != dlen))
1349 m->msg_flags |= MSG_TRUNC;
1350 rc = skb_copy_datagram_msg(skb, hlen, m, copy);
1351 } else {
1352 copy = 0;
1353 rc = 0;
1354 if (err != TIPC_CONN_SHUTDOWN && connected && !m->msg_control)
1355 rc = -ECONNRESET;
1356 }
1357 if (unlikely(rc))
1358 goto exit;
1359
1360 /* Caption of data or error code/rejected data was successful */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001361 if (unlikely(flags & MSG_PEEK))
1362 goto exit;
1363
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001364 tsk_advance_rx_queue(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001365 if (likely(!connected))
1366 goto exit;
1367
1368 /* Send connection flow control ack when applicable */
1369 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1370 if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
1371 tipc_sk_send_ack(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001372exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001373 release_sock(sk);
Jon Paul Maloye9f8b102017-05-02 18:16:53 +02001374 return rc ? rc : copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001375}
1376
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001377/**
Ying Xue247f0f32014-02-18 16:06:46 +08001378 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001379 * @m: descriptor for message info
1380 * @buf_len: total size of user buffer area
1381 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001382 *
1383 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001384 * will optionally wait for more; never truncates data.
1385 *
1386 * Returns size of returned message data, errno otherwise
1387 */
Ying Xue1b784142015-03-02 15:37:48 +08001388static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1389 size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001391 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001392 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001393 struct sk_buff *buf;
1394 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001395 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001396 unsigned int sz;
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001397 int target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001399 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001400 int res = 0, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001401
1402 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 if (unlikely(!buf_len))
1404 return -EINVAL;
1405
Allan Stephens0c3141e2008-04-15 00:22:02 -07001406 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001407
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001408 if (unlikely(sk->sk_state == TIPC_OPEN)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001409 res = -ENOTCONN;
1410 goto exit;
1411 }
1412
Florian Westphal3720d402010-08-17 11:00:04 +00001413 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001414 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001415
Allan Stephens0c3141e2008-04-15 00:22:02 -07001416restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001417 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001418 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001419 if (res)
1420 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001421
1422 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001423 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001424 msg = buf_msg(buf);
1425 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001426 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001427 err = msg_errcode(msg);
1428
1429 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001430 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001431 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001432 goto restart;
1433 }
1434
1435 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001436 if (sz_copied == 0) {
1437 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001438 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001439 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001440 goto exit;
1441 }
1442
1443 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444 if (!err) {
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001445 u32 offset = TIPC_SKB_CB(buf)->bytes_read;
1446 u32 needed;
1447 int sz_to_copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448
Allan Stephens0232fd02011-02-21 09:45:40 -05001449 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001450 needed = (buf_len - sz_copied);
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001451 sz_to_copy = min(sz, needed);
Allan Stephens0232fd02011-02-21 09:45:40 -05001452
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001453 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001454 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001455 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001456
Per Lidenb97bf3f2006-01-02 19:04:38 +01001457 sz_copied += sz_to_copy;
1458
1459 if (sz_to_copy < sz) {
1460 if (!(flags & MSG_PEEK))
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001461 TIPC_SKB_CB(buf)->bytes_read =
1462 offset + sz_to_copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463 goto exit;
1464 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001465 } else {
1466 if (sz_copied != 0)
1467 goto exit; /* can't add error msg to valid data */
1468
1469 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1470 res = 0;
1471 else
1472 res = -ECONNRESET;
1473 }
1474
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001475 if (unlikely(flags & MSG_PEEK))
1476 goto exit;
1477
Parthasarathy Bhuvaragan05ff8372017-04-24 15:00:43 +02001478 tsk->rcv_unacked += tsk_inc(tsk, hlen + msg_data_sz(msg));
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001479 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1480 tipc_sk_send_ack(tsk);
1481 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001482
1483 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001484 if ((sz_copied < buf_len) && /* didn't get all requested data */
1485 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001486 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001487 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001488 goto restart;
1489
1490exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001491 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001492 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001493}
1494
1495/**
Ying Xuef288bef2012-08-21 11:16:57 +08001496 * tipc_write_space - wake up thread if port congestion is released
1497 * @sk: socket
1498 */
1499static void tipc_write_space(struct sock *sk)
1500{
1501 struct socket_wq *wq;
1502
1503 rcu_read_lock();
1504 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001505 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001506 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1507 POLLWRNORM | POLLWRBAND);
1508 rcu_read_unlock();
1509}
1510
1511/**
1512 * tipc_data_ready - wake up threads to indicate messages have been received
1513 * @sk: socket
1514 * @len: the length of messages
1515 */
David S. Miller676d2362014-04-11 16:15:36 -04001516static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001517{
1518 struct socket_wq *wq;
1519
1520 rcu_read_lock();
1521 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001522 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001523 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1524 POLLRDNORM | POLLRDBAND);
1525 rcu_read_unlock();
1526}
1527
Ying Xuef4195d12015-11-22 15:46:05 +08001528static void tipc_sock_destruct(struct sock *sk)
1529{
1530 __skb_queue_purge(&sk->sk_receive_queue);
1531}
1532
Ying Xuef288bef2012-08-21 11:16:57 +08001533/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001534 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001535 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001536 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001537 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001538 * Returns true if everything ok, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001539 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001540static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001541{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001542 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001543 struct net *net = sock_net(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001544 struct tipc_msg *hdr = buf_msg(skb);
Ying Xue7e6c1312012-11-29 18:39:14 -05001545
Jon Paul Maloycda36962015-07-22 10:11:20 -04001546 if (unlikely(msg_mcast(hdr)))
1547 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001548
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001549 switch (sk->sk_state) {
1550 case TIPC_CONNECTING:
Ying Xue7e6c1312012-11-29 18:39:14 -05001551 /* Accept only ACK or NACK message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001552 if (unlikely(!msg_connected(hdr)))
1553 return false;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001554
Jon Paul Maloycda36962015-07-22 10:11:20 -04001555 if (unlikely(msg_errcode(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001556 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugne2c8d8512013-08-28 09:29:58 +02001557 sk->sk_err = ECONNREFUSED;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001558 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001559 }
1560
Jon Paul Maloycda36962015-07-22 10:11:20 -04001561 if (unlikely(!msg_isdata(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001562 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001563 sk->sk_err = EINVAL;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001564 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001565 }
1566
Jon Paul Maloycda36962015-07-22 10:11:20 -04001567 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1568 msg_set_importance(&tsk->phdr, msg_importance(hdr));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001569
Jon Paul Maloycda36962015-07-22 10:11:20 -04001570 /* If 'ACK+' message, add to socket receive queue */
1571 if (msg_data_sz(hdr))
1572 return true;
1573
1574 /* If empty 'ACK-' message, wake up sleeping connect() */
1575 if (waitqueue_active(sk_sleep(sk)))
1576 wake_up_interruptible(sk_sleep(sk));
1577
1578 /* 'ACK-' message is neither accepted nor rejected: */
1579 msg_set_dest_droppable(hdr, 1);
1580 return false;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001581
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001582 case TIPC_OPEN:
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001583 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001584 break;
1585 case TIPC_LISTEN:
Ying Xue7e6c1312012-11-29 18:39:14 -05001586 /* Accept only SYN message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001587 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1588 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001589 break;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001590 case TIPC_ESTABLISHED:
1591 /* Accept only connection-based messages sent by peer */
1592 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1593 return false;
1594
1595 if (unlikely(msg_errcode(hdr))) {
1596 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1597 /* Let timer expire on it's own */
1598 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1599 tsk->portid);
1600 sk->sk_state_change(sk);
1601 }
1602 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001603 default:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001604 pr_err("Unknown sk_state %u\n", sk->sk_state);
Ying Xue7e6c1312012-11-29 18:39:14 -05001605 }
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001606
Jon Paul Maloycda36962015-07-22 10:11:20 -04001607 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001608}
1609
1610/**
Ying Xueaba79f32013-01-20 23:30:09 +01001611 * rcvbuf_limit - get proper overload limit of socket receive queue
1612 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001613 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01001614 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001615 * For connection oriented messages, irrespective of importance,
1616 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01001617 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001618 * For connectionless messages, queue limits are based on message
1619 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01001620 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001621 * TIPC_LOW_IMPORTANCE (2 MB)
1622 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1623 * TIPC_HIGH_IMPORTANCE (8 MB)
1624 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001625 *
1626 * Returns overload limit according to corresponding message importance
1627 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001628static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01001629{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001630 struct tipc_sock *tsk = tipc_sk(sk);
1631 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01001632
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001633 if (unlikely(!msg_connected(hdr)))
1634 return sk->sk_rcvbuf << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08001635
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001636 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1637 return sk->sk_rcvbuf;
1638
1639 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01001640}
1641
1642/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001643 * filter_rcv - validate incoming message
1644 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04001645 * @skb: pointer to message.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001646 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001647 * Enqueues message on receive queue if acceptable; optionally handles
1648 * disconnect indication for a connected socket.
1649 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001650 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001651 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001652 * Returns true if message was added to socket receive queue, otherwise false
Per Lidenb97bf3f2006-01-02 19:04:38 +01001653 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001654static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1655 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001656{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001657 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001658 struct tipc_msg *hdr = buf_msg(skb);
1659 unsigned int limit = rcvbuf_limit(sk, skb);
1660 int err = TIPC_OK;
1661 int usr = msg_user(hdr);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001662 u32 onode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001663
Jon Paul Maloycda36962015-07-22 10:11:20 -04001664 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001665 tipc_sk_proto_rcv(tsk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001666 return false;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001667 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001668
Jon Paul Maloycda36962015-07-22 10:11:20 -04001669 if (unlikely(usr == SOCK_WAKEUP)) {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001670 onode = msg_orignode(hdr);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001671 kfree_skb(skb);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001672 u32_del(&tsk->cong_links, onode);
1673 tsk->cong_link_cnt--;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001674 sk->sk_write_space(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001675 return false;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001676 }
1677
Jon Paul Maloycda36962015-07-22 10:11:20 -04001678 /* Drop if illegal message type */
1679 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1680 kfree_skb(skb);
1681 return false;
1682 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001683
Jon Paul Maloycda36962015-07-22 10:11:20 -04001684 /* Reject if wrong message type for current socket state */
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001685 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001686 if (msg_connected(hdr)) {
1687 err = TIPC_ERR_NO_PORT;
1688 goto reject;
1689 }
1690 } else if (unlikely(!filter_connect(tsk, skb))) {
1691 err = TIPC_ERR_NO_PORT;
1692 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001693 }
1694
1695 /* Reject message if there isn't room to queue it */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001696 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1697 err = TIPC_ERR_OVERLOAD;
1698 goto reject;
1699 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001700
Ying Xueaba79f32013-01-20 23:30:09 +01001701 /* Enqueue message */
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001702 TIPC_SKB_CB(skb)->bytes_read = 0;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001703 __skb_queue_tail(&sk->sk_receive_queue, skb);
1704 skb_set_owner_r(skb, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001705
David S. Miller676d2362014-04-11 16:15:36 -04001706 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001707 return true;
1708
1709reject:
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001710 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1711 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001712 return false;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001713}
1714
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001715/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001716 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001717 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001718 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001719 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001720 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001721 *
1722 * Returns 0
1723 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001724static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001725{
Jon Paul Maloycda36962015-07-22 10:11:20 -04001726 unsigned int truesize = skb->truesize;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001727 struct sk_buff_head xmitq;
1728 u32 dnode, selector;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001729
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001730 __skb_queue_head_init(&xmitq);
1731
1732 if (likely(filter_rcv(sk, skb, &xmitq))) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001733 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001734 return 0;
1735 }
1736
1737 if (skb_queue_empty(&xmitq))
1738 return 0;
1739
1740 /* Send response/rejected message */
1741 skb = __skb_dequeue(&xmitq);
1742 dnode = msg_destnode(buf_msg(skb));
1743 selector = msg_origport(buf_msg(skb));
1744 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001745 return 0;
1746}
1747
1748/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001749 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1750 * inputq and try adding them to socket or backlog queue
1751 * @inputq: list of incoming buffers with potentially different destinations
1752 * @sk: socket where the buffers should be enqueued
1753 * @dport: port number for the socket
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001754 *
1755 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001756 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001757static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001758 u32 dport, struct sk_buff_head *xmitq)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001759{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001760 unsigned long time_limit = jiffies + 2;
1761 struct sk_buff *skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001762 unsigned int lim;
1763 atomic_t *dcnt;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001764 u32 onode;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001765
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001766 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05001767 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001768 return;
1769
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001770 skb = tipc_skb_dequeue(inputq, dport);
1771 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001772 return;
1773
1774 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001775 if (!sock_owned_by_user(sk)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001776 filter_rcv(sk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001777 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001778 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001779
1780 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001781 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04001782 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001783 atomic_set(dcnt, 0);
1784 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1785 if (likely(!sk_add_backlog(sk, skb, lim)))
1786 continue;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001787
1788 /* Overload => reject message back to sender */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001789 onode = tipc_own_addr(sock_net(sk));
1790 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1791 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001792 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001793 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001794}
1795
1796/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001797 * tipc_sk_rcv - handle a chain of incoming buffers
1798 * @inputq: buffer list containing the buffers
1799 * Consumes all buffers in list until inputq is empty
1800 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001801 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001802void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001803{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001804 struct sk_buff_head xmitq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001805 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04001806 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001807 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001808 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001809 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001810
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001811 __skb_queue_head_init(&xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001812 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001813 dport = tipc_skb_peek_port(inputq, dport);
1814 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001815
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001816 if (likely(tsk)) {
1817 sk = &tsk->sk;
1818 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001819 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001820 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001821 }
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001822 /* Send pending response/rejected messages, if any */
1823 while ((skb = __skb_dequeue(&xmitq))) {
1824 dnode = msg_destnode(buf_msg(skb));
1825 tipc_node_xmit_skb(net, skb, dnode, dport);
1826 }
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001827 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001828 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001829 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001830
1831 /* No destination socket => dequeue skb if still there */
1832 skb = tipc_skb_dequeue(inputq, dport);
1833 if (!skb)
1834 return;
1835
1836 /* Try secondary lookup if unresolved named message */
1837 err = TIPC_ERR_NO_PORT;
1838 if (tipc_msg_lookup_dest(net, skb, &err))
1839 goto xmit;
1840
1841 /* Prepare for message rejection */
1842 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001843 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001844xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001845 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001846 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001847 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001848}
1849
Ying Xue78eb3a52014-01-17 09:50:03 +08001850static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1851{
WANG Congd9dc8b02016-11-11 10:20:50 -08001852 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Ying Xue78eb3a52014-01-17 09:50:03 +08001853 struct sock *sk = sock->sk;
Ying Xue78eb3a52014-01-17 09:50:03 +08001854 int done;
1855
1856 do {
1857 int err = sock_error(sk);
1858 if (err)
1859 return err;
1860 if (!*timeo_p)
1861 return -ETIMEDOUT;
1862 if (signal_pending(current))
1863 return sock_intr_errno(*timeo_p);
1864
WANG Congd9dc8b02016-11-11 10:20:50 -08001865 add_wait_queue(sk_sleep(sk), &wait);
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001866 done = sk_wait_event(sk, timeo_p,
WANG Congd9dc8b02016-11-11 10:20:50 -08001867 sk->sk_state != TIPC_CONNECTING, &wait);
1868 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue78eb3a52014-01-17 09:50:03 +08001869 } while (!done);
1870 return 0;
1871}
1872
Per Lidenb97bf3f2006-01-02 19:04:38 +01001873/**
Ying Xue247f0f32014-02-18 16:06:46 +08001874 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001875 * @sock: socket structure
1876 * @dest: socket address for destination port
1877 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001878 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001879 *
1880 * Returns 0 on success, errno otherwise
1881 */
Ying Xue247f0f32014-02-18 16:06:46 +08001882static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1883 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001884{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001885 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001886 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001887 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1888 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001889 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001890 int previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001891 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001892
Allan Stephens0c3141e2008-04-15 00:22:02 -07001893 lock_sock(sk);
1894
Erik Hugnef2f80362015-03-19 09:02:19 +01001895 /* DGRAM/RDM connect(), just save the destaddr */
Parthasarathy Bhuvaraganc7520232016-11-01 14:02:42 +01001896 if (tipc_sk_type_connectionless(sk)) {
Erik Hugnef2f80362015-03-19 09:02:19 +01001897 if (dst->family == AF_UNSPEC) {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001898 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
Sasha Levin610600c2015-03-23 15:30:00 -04001899 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1900 res = -EINVAL;
Erik Hugnef2f80362015-03-19 09:02:19 +01001901 } else {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001902 memcpy(&tsk->peer, dest, destlen);
Erik Hugnef2f80362015-03-19 09:02:19 +01001903 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001904 goto exit;
1905 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001906
Allan Stephensb89741a2008-04-15 00:20:37 -07001907 /*
1908 * Reject connection attempt using multicast address
1909 *
1910 * Note: send_msg() validates the rest of the address fields,
1911 * so there's no need to do it here
1912 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001913 if (dst->addrtype == TIPC_ADDR_MCAST) {
1914 res = -EINVAL;
1915 goto exit;
1916 }
1917
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001918 previous = sk->sk_state;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001919
1920 switch (sk->sk_state) {
1921 case TIPC_OPEN:
Ying Xue584d24b2012-11-29 18:51:19 -05001922 /* Send a 'SYN-' to destination */
1923 m.msg_name = dest;
1924 m.msg_namelen = destlen;
1925
1926 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1927 * indicate send_msg() is never blocked.
1928 */
1929 if (!timeout)
1930 m.msg_flags = MSG_DONTWAIT;
1931
Ying Xue39a02952015-03-02 15:37:47 +08001932 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001933 if ((res < 0) && (res != -EWOULDBLOCK))
1934 goto exit;
1935
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001936 /* Just entered TIPC_CONNECTING state; the only
Ying Xue584d24b2012-11-29 18:51:19 -05001937 * difference is that return value in non-blocking
1938 * case is EINPROGRESS, rather than EALREADY.
1939 */
1940 res = -EINPROGRESS;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001941 /* fall thru' */
1942 case TIPC_CONNECTING:
1943 if (!timeout) {
1944 if (previous == TIPC_CONNECTING)
1945 res = -EALREADY;
Ying Xue78eb3a52014-01-17 09:50:03 +08001946 goto exit;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001947 }
Ying Xue78eb3a52014-01-17 09:50:03 +08001948 timeout = msecs_to_jiffies(timeout);
1949 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1950 res = tipc_wait_for_connect(sock, &timeout);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001951 break;
1952 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001953 res = -EISCONN;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001954 break;
1955 default:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001956 res = -EINVAL;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001957 }
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001958
Allan Stephens0c3141e2008-04-15 00:22:02 -07001959exit:
1960 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001961 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001962}
1963
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001964/**
Ying Xue247f0f32014-02-18 16:06:46 +08001965 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001966 * @sock: socket structure
1967 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001968 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001969 * Returns 0 on success, errno otherwise
1970 */
Ying Xue247f0f32014-02-18 16:06:46 +08001971static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001972{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001973 struct sock *sk = sock->sk;
1974 int res;
1975
1976 lock_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001977 res = tipc_set_sk_state(sk, TIPC_LISTEN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001978 release_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001979
Allan Stephens0c3141e2008-04-15 00:22:02 -07001980 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001981}
1982
Ying Xue6398e232014-01-17 09:50:04 +08001983static int tipc_wait_for_accept(struct socket *sock, long timeo)
1984{
1985 struct sock *sk = sock->sk;
1986 DEFINE_WAIT(wait);
1987 int err;
1988
1989 /* True wake-one mechanism for incoming connections: only
1990 * one process gets woken up, not the 'whole herd'.
1991 * Since we do not 'race & poll' for established sockets
1992 * anymore, the common case will execute the loop only once.
1993 */
1994 for (;;) {
1995 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
1996 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001997 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08001998 release_sock(sk);
1999 timeo = schedule_timeout(timeo);
2000 lock_sock(sk);
2001 }
2002 err = 0;
2003 if (!skb_queue_empty(&sk->sk_receive_queue))
2004 break;
Ying Xue6398e232014-01-17 09:50:04 +08002005 err = -EAGAIN;
2006 if (!timeo)
2007 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002008 err = sock_intr_errno(timeo);
2009 if (signal_pending(current))
2010 break;
Ying Xue6398e232014-01-17 09:50:04 +08002011 }
2012 finish_wait(sk_sleep(sk), &wait);
2013 return err;
2014}
2015
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002016/**
Ying Xue247f0f32014-02-18 16:06:46 +08002017 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002018 * @sock: listening socket
2019 * @newsock: new socket that is to be connected
2020 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002021 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002022 * Returns 0 on success, errno otherwise
2023 */
David Howellscdfbabf2017-03-09 08:09:05 +00002024static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags,
2025 bool kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002026{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002027 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002028 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002029 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002030 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002031 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002032 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002033
Allan Stephens0c3141e2008-04-15 00:22:02 -07002034 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002035
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002036 if (sk->sk_state != TIPC_LISTEN) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002037 res = -EINVAL;
2038 goto exit;
2039 }
Ying Xue6398e232014-01-17 09:50:04 +08002040 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2041 res = tipc_wait_for_accept(sock, timeo);
2042 if (res)
2043 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002044
2045 buf = skb_peek(&sk->sk_receive_queue);
2046
David Howellscdfbabf2017-03-09 08:09:05 +00002047 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, kern);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002048 if (res)
2049 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002050 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002051
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002052 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002053 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002054 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002055
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002056 /* we lock on new_sk; but lockdep sees the lock on sk */
2057 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002058
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002059 /*
2060 * Reject any stray messages received by new socket
2061 * before the socket lock was taken (very, very unlikely)
2062 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002063 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002064
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002065 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002066 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002067
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002068 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002069 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002070 new_tsock->conn_type = msg_nametype(msg);
2071 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002072 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002073
2074 /*
2075 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2076 * Respond to 'SYN+' by queuing it on new socket.
2077 */
2078 if (!msg_data_sz(msg)) {
2079 struct msghdr m = {NULL,};
2080
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002081 tsk_advance_rx_queue(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002082 __tipc_sendstream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002083 } else {
2084 __skb_dequeue(&sk->sk_receive_queue);
2085 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002086 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002087 }
2088 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002089exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002090 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002091 return res;
2092}
2093
2094/**
Ying Xue247f0f32014-02-18 16:06:46 +08002095 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002096 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002097 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002098 *
2099 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002100 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002101 * Returns 0 on success, errno otherwise
2102 */
Ying Xue247f0f32014-02-18 16:06:46 +08002103static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002104{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002105 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002106 int res;
2107
Allan Stephense247a8f2008-03-06 15:05:38 -08002108 if (how != SHUT_RDWR)
2109 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002110
Allan Stephens0c3141e2008-04-15 00:22:02 -07002111 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002112
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002113 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2114 sk->sk_shutdown = SEND_SHUTDOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002115
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002116 if (sk->sk_state == TIPC_DISCONNECTING) {
Ying Xue75031152012-10-29 09:38:15 -04002117 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002118 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002119
2120 /* Wake up anyone sleeping in poll */
2121 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002122 res = 0;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002123 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002124 res = -ENOTCONN;
2125 }
2126
Allan Stephens0c3141e2008-04-15 00:22:02 -07002127 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002128 return res;
2129}
2130
Ying Xuef2f2a962015-01-09 15:27:02 +08002131static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002132{
Ying Xuef2f2a962015-01-09 15:27:02 +08002133 struct tipc_sock *tsk = (struct tipc_sock *)data;
2134 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002135 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002136 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002137 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002138
Jon Paul Maloy57289012014-08-22 18:09:09 -04002139 bh_lock_sock(sk);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002140 if (!tipc_sk_connected(sk)) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002141 bh_unlock_sock(sk);
2142 goto exit;
2143 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002144 peer_port = tsk_peer_port(tsk);
2145 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002146
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002147 if (tsk->probe_unacked) {
Erik Hugneb3be5e32015-06-09 17:27:12 +02002148 if (!sock_owned_by_user(sk)) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01002149 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugneb3be5e32015-06-09 17:27:12 +02002150 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2151 tsk_peer_port(tsk));
2152 sk->sk_state_change(sk);
2153 } else {
2154 /* Try again later */
2155 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2156 }
2157
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002158 bh_unlock_sock(sk);
2159 goto exit;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002160 }
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002161
2162 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2163 INT_H_SIZE, 0, peer_node, own_node,
2164 peer_port, tsk->portid, TIPC_OK);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002165 tsk->probe_unacked = true;
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002166 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002167 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002168 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002169 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002170exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002171 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002172}
2173
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002174static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002175 struct tipc_name_seq const *seq)
2176{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002177 struct sock *sk = &tsk->sk;
2178 struct net *net = sock_net(sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002179 struct publication *publ;
2180 u32 key;
2181
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002182 if (tipc_sk_connected(sk))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002183 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002184 key = tsk->portid + tsk->pub_count + 1;
2185 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002186 return -EADDRINUSE;
2187
Ying Xuef2f98002015-01-09 15:27:05 +08002188 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002189 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002190 if (unlikely(!publ))
2191 return -EINVAL;
2192
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002193 list_add(&publ->pport_list, &tsk->publications);
2194 tsk->pub_count++;
2195 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002196 return 0;
2197}
2198
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002199static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002200 struct tipc_name_seq const *seq)
2201{
Ying Xuef2f98002015-01-09 15:27:05 +08002202 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002203 struct publication *publ;
2204 struct publication *safe;
2205 int rc = -EINVAL;
2206
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002207 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002208 if (seq) {
2209 if (publ->scope != scope)
2210 continue;
2211 if (publ->type != seq->type)
2212 continue;
2213 if (publ->lower != seq->lower)
2214 continue;
2215 if (publ->upper != seq->upper)
2216 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002217 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002218 publ->ref, publ->key);
2219 rc = 0;
2220 break;
2221 }
Ying Xuef2f98002015-01-09 15:27:05 +08002222 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002223 publ->ref, publ->key);
2224 rc = 0;
2225 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002226 if (list_empty(&tsk->publications))
2227 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002228 return rc;
2229}
2230
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002231/* tipc_sk_reinit: set non-zero address in all existing sockets
2232 * when we go from standalone to network mode.
2233 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002234void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002235{
Ying Xuee05b31f2015-01-09 15:27:08 +08002236 struct tipc_net *tn = net_generic(net, tipc_net_id);
Herbert Xu40f9f432017-02-11 19:26:46 +08002237 struct rhashtable_iter iter;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002238 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002239 struct tipc_msg *msg;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002240
Herbert Xu40f9f432017-02-11 19:26:46 +08002241 rhashtable_walk_enter(&tn->sk_rht, &iter);
2242
2243 do {
2244 tsk = ERR_PTR(rhashtable_walk_start(&iter));
2245 if (tsk)
2246 continue;
2247
2248 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002249 spin_lock_bh(&tsk->sk.sk_lock.slock);
2250 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002251 msg_set_prevnode(msg, tn->own_addr);
2252 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002253 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2254 }
Herbert Xu40f9f432017-02-11 19:26:46 +08002255
2256 rhashtable_walk_stop(&iter);
2257 } while (tsk == ERR_PTR(-EAGAIN));
Ying Xue07f6c4b2015-01-07 13:41:58 +08002258}
2259
Ying Xuee05b31f2015-01-09 15:27:08 +08002260static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002261{
Ying Xuee05b31f2015-01-09 15:27:08 +08002262 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002263 struct tipc_sock *tsk;
2264
2265 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002266 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002267 if (tsk)
2268 sock_hold(&tsk->sk);
2269 rcu_read_unlock();
2270
2271 return tsk;
2272}
2273
2274static int tipc_sk_insert(struct tipc_sock *tsk)
2275{
Ying Xuee05b31f2015-01-09 15:27:08 +08002276 struct sock *sk = &tsk->sk;
2277 struct net *net = sock_net(sk);
2278 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002279 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2280 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2281
2282 while (remaining--) {
2283 portid++;
2284 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2285 portid = TIPC_MIN_PORT;
2286 tsk->portid = portid;
2287 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002288 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2289 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002290 return 0;
2291 sock_put(&tsk->sk);
2292 }
2293
2294 return -1;
2295}
2296
2297static void tipc_sk_remove(struct tipc_sock *tsk)
2298{
2299 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002300 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002301
Herbert Xu6cca72892015-03-20 21:57:05 +11002302 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002303 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2304 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002305 }
2306}
2307
Herbert Xu6cca72892015-03-20 21:57:05 +11002308static const struct rhashtable_params tsk_rht_params = {
2309 .nelem_hint = 192,
2310 .head_offset = offsetof(struct tipc_sock, node),
2311 .key_offset = offsetof(struct tipc_sock, portid),
2312 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11002313 .max_size = 1048576,
2314 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002315 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11002316};
2317
Ying Xuee05b31f2015-01-09 15:27:08 +08002318int tipc_sk_rht_init(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002319{
Ying Xuee05b31f2015-01-09 15:27:08 +08002320 struct tipc_net *tn = net_generic(net, tipc_net_id);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002321
Herbert Xu6cca72892015-03-20 21:57:05 +11002322 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002323}
2324
Ying Xuee05b31f2015-01-09 15:27:08 +08002325void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002326{
Ying Xuee05b31f2015-01-09 15:27:08 +08002327 struct tipc_net *tn = net_generic(net, tipc_net_id);
2328
Ying Xue07f6c4b2015-01-07 13:41:58 +08002329 /* Wait for socket readers to complete */
2330 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002331
Ying Xuee05b31f2015-01-09 15:27:08 +08002332 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002333}
2334
2335/**
Ying Xue247f0f32014-02-18 16:06:46 +08002336 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002337 * @sock: socket structure
2338 * @lvl: option level
2339 * @opt: option identifier
2340 * @ov: pointer to new option value
2341 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002342 *
2343 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002344 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002345 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002346 * Returns 0 on success, errno otherwise
2347 */
Ying Xue247f0f32014-02-18 16:06:46 +08002348static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2349 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002350{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002351 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002352 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002353 u32 value = 0;
Dan Carpentera08ef472017-01-24 12:49:35 +03002354 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002355
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002356 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2357 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002358 if (lvl != SOL_TIPC)
2359 return -ENOPROTOOPT;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002360
2361 switch (opt) {
2362 case TIPC_IMPORTANCE:
2363 case TIPC_SRC_DROPPABLE:
2364 case TIPC_DEST_DROPPABLE:
2365 case TIPC_CONN_TIMEOUT:
2366 if (ol < sizeof(value))
2367 return -EINVAL;
2368 res = get_user(value, (u32 __user *)ov);
2369 if (res)
2370 return res;
2371 break;
2372 default:
2373 if (ov || ol)
2374 return -EINVAL;
2375 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002376
Allan Stephens0c3141e2008-04-15 00:22:02 -07002377 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002378
Per Lidenb97bf3f2006-01-02 19:04:38 +01002379 switch (opt) {
2380 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002381 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002382 break;
2383 case TIPC_SRC_DROPPABLE:
2384 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002385 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002386 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002387 res = -ENOPROTOOPT;
2388 break;
2389 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002390 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002391 break;
2392 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002393 tipc_sk(sk)->conn_timeout = value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002394 break;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002395 case TIPC_MCAST_BROADCAST:
2396 tsk->mc_method.rcast = false;
2397 tsk->mc_method.mandatory = true;
2398 break;
2399 case TIPC_MCAST_REPLICAST:
2400 tsk->mc_method.rcast = true;
2401 tsk->mc_method.mandatory = true;
2402 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002403 default:
2404 res = -EINVAL;
2405 }
2406
Allan Stephens0c3141e2008-04-15 00:22:02 -07002407 release_sock(sk);
2408
Per Lidenb97bf3f2006-01-02 19:04:38 +01002409 return res;
2410}
2411
2412/**
Ying Xue247f0f32014-02-18 16:06:46 +08002413 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002414 * @sock: socket structure
2415 * @lvl: option level
2416 * @opt: option identifier
2417 * @ov: receptacle for option value
2418 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002419 *
2420 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002421 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002422 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002423 * Returns 0 on success, errno otherwise
2424 */
Ying Xue247f0f32014-02-18 16:06:46 +08002425static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2426 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002427{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002428 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002429 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002430 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002431 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002432 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002433
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002434 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2435 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002436 if (lvl != SOL_TIPC)
2437 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002438 res = get_user(len, ol);
2439 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002440 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002441
Allan Stephens0c3141e2008-04-15 00:22:02 -07002442 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002443
2444 switch (opt) {
2445 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002446 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002447 break;
2448 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002449 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002450 break;
2451 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002452 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002453 break;
2454 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002455 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002456 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002457 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002458 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002459 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002460 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002461 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002462 value = skb_queue_len(&sk->sk_receive_queue);
2463 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002464 default:
2465 res = -EINVAL;
2466 }
2467
Allan Stephens0c3141e2008-04-15 00:22:02 -07002468 release_sock(sk);
2469
Paul Gortmaker25860c32010-12-31 18:59:31 +00002470 if (res)
2471 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002472
Paul Gortmaker25860c32010-12-31 18:59:31 +00002473 if (len < sizeof(value))
2474 return -EINVAL;
2475
2476 if (copy_to_user(ov, &value, sizeof(value)))
2477 return -EFAULT;
2478
2479 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002480}
2481
Ying Xuef2f98002015-01-09 15:27:05 +08002482static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002483{
Ying Xuef2f98002015-01-09 15:27:05 +08002484 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002485 struct tipc_sioc_ln_req lnr;
2486 void __user *argp = (void __user *)arg;
2487
2488 switch (cmd) {
2489 case SIOCGETLINKNAME:
2490 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2491 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002492 if (!tipc_node_get_linkname(sock_net(sk),
2493 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002494 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2495 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2496 return -EFAULT;
2497 return 0;
2498 }
2499 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002500 default:
2501 return -ENOIOCTLCMD;
2502 }
2503}
2504
Erik Hugne70b03752017-03-29 11:22:16 +02002505static int tipc_socketpair(struct socket *sock1, struct socket *sock2)
2506{
2507 struct tipc_sock *tsk2 = tipc_sk(sock2->sk);
2508 struct tipc_sock *tsk1 = tipc_sk(sock1->sk);
Erik Hugne66bc1e82017-03-29 11:22:17 +02002509 u32 onode = tipc_own_addr(sock_net(sock1->sk));
Erik Hugne70b03752017-03-29 11:22:16 +02002510
Erik Hugne66bc1e82017-03-29 11:22:17 +02002511 tsk1->peer.family = AF_TIPC;
2512 tsk1->peer.addrtype = TIPC_ADDR_ID;
2513 tsk1->peer.scope = TIPC_NODE_SCOPE;
2514 tsk1->peer.addr.id.ref = tsk2->portid;
2515 tsk1->peer.addr.id.node = onode;
2516 tsk2->peer.family = AF_TIPC;
2517 tsk2->peer.addrtype = TIPC_ADDR_ID;
2518 tsk2->peer.scope = TIPC_NODE_SCOPE;
2519 tsk2->peer.addr.id.ref = tsk1->portid;
2520 tsk2->peer.addr.id.node = onode;
2521
2522 tipc_sk_finish_conn(tsk1, tsk2->portid, onode);
2523 tipc_sk_finish_conn(tsk2, tsk1->portid, onode);
Erik Hugne70b03752017-03-29 11:22:16 +02002524 return 0;
2525}
2526
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002527/* Protocol switches for the various types of TIPC sockets */
2528
Florian Westphalbca65ea2008-02-07 18:18:01 -08002529static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002530 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002531 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002532 .release = tipc_release,
2533 .bind = tipc_bind,
2534 .connect = tipc_connect,
Erik Hugne66bc1e82017-03-29 11:22:17 +02002535 .socketpair = tipc_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002536 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002537 .getname = tipc_getname,
2538 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002539 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002540 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002541 .shutdown = tipc_shutdown,
2542 .setsockopt = tipc_setsockopt,
2543 .getsockopt = tipc_getsockopt,
2544 .sendmsg = tipc_sendmsg,
2545 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002546 .mmap = sock_no_mmap,
2547 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002548};
2549
Florian Westphalbca65ea2008-02-07 18:18:01 -08002550static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002551 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002552 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002553 .release = tipc_release,
2554 .bind = tipc_bind,
2555 .connect = tipc_connect,
Erik Hugne70b03752017-03-29 11:22:16 +02002556 .socketpair = tipc_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002557 .accept = tipc_accept,
2558 .getname = tipc_getname,
2559 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002560 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002561 .listen = tipc_listen,
2562 .shutdown = tipc_shutdown,
2563 .setsockopt = tipc_setsockopt,
2564 .getsockopt = tipc_getsockopt,
2565 .sendmsg = tipc_send_packet,
2566 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002567 .mmap = sock_no_mmap,
2568 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002569};
2570
Florian Westphalbca65ea2008-02-07 18:18:01 -08002571static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002572 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002573 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002574 .release = tipc_release,
2575 .bind = tipc_bind,
2576 .connect = tipc_connect,
Erik Hugne70b03752017-03-29 11:22:16 +02002577 .socketpair = tipc_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002578 .accept = tipc_accept,
2579 .getname = tipc_getname,
2580 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002581 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002582 .listen = tipc_listen,
2583 .shutdown = tipc_shutdown,
2584 .setsockopt = tipc_setsockopt,
2585 .getsockopt = tipc_getsockopt,
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002586 .sendmsg = tipc_sendstream,
Ying Xue247f0f32014-02-18 16:06:46 +08002587 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002588 .mmap = sock_no_mmap,
2589 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002590};
2591
Florian Westphalbca65ea2008-02-07 18:18:01 -08002592static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002593 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002594 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002595 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002596};
2597
2598static struct proto tipc_proto = {
2599 .name = "TIPC",
2600 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002601 .obj_size = sizeof(struct tipc_sock),
2602 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002603};
2604
2605/**
Per Liden4323add2006-01-18 00:38:21 +01002606 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002607 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002608 * Returns 0 on success, errno otherwise
2609 */
Per Liden4323add2006-01-18 00:38:21 +01002610int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002611{
2612 int res;
2613
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002614 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002615 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002616 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002617 goto out;
2618 }
2619
2620 res = sock_register(&tipc_family_ops);
2621 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002622 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002623 proto_unregister(&tipc_proto);
2624 goto out;
2625 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002626 out:
2627 return res;
2628}
2629
2630/**
Per Liden4323add2006-01-18 00:38:21 +01002631 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002632 */
Per Liden4323add2006-01-18 00:38:21 +01002633void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002634{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002635 sock_unregister(tipc_family_ops.family);
2636 proto_unregister(&tipc_proto);
2637}
Richard Alpe34b78a122014-11-20 10:29:10 +01002638
2639/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002640static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002641{
2642 u32 peer_node;
2643 u32 peer_port;
2644 struct nlattr *nest;
2645
2646 peer_node = tsk_peer_node(tsk);
2647 peer_port = tsk_peer_port(tsk);
2648
2649 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2650
2651 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2652 goto msg_full;
2653 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2654 goto msg_full;
2655
2656 if (tsk->conn_type != 0) {
2657 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2658 goto msg_full;
2659 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2660 goto msg_full;
2661 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2662 goto msg_full;
2663 }
2664 nla_nest_end(skb, nest);
2665
2666 return 0;
2667
2668msg_full:
2669 nla_nest_cancel(skb, nest);
2670
2671 return -EMSGSIZE;
2672}
2673
2674/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002675static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2676 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002677{
2678 int err;
2679 void *hdr;
2680 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002681 struct net *net = sock_net(skb->sk);
2682 struct tipc_net *tn = net_generic(net, tipc_net_id);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002683 struct sock *sk = &tsk->sk;
Richard Alpe34b78a122014-11-20 10:29:10 +01002684
2685 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002686 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002687 if (!hdr)
2688 goto msg_cancel;
2689
2690 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2691 if (!attrs)
2692 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002693 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002694 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002695 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002696 goto attr_msg_cancel;
2697
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002698 if (tipc_sk_connected(sk)) {
Richard Alpe34b78a122014-11-20 10:29:10 +01002699 err = __tipc_nl_add_sk_con(skb, tsk);
2700 if (err)
2701 goto attr_msg_cancel;
2702 } else if (!list_empty(&tsk->publications)) {
2703 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2704 goto attr_msg_cancel;
2705 }
2706 nla_nest_end(skb, attrs);
2707 genlmsg_end(skb, hdr);
2708
2709 return 0;
2710
2711attr_msg_cancel:
2712 nla_nest_cancel(skb, attrs);
2713genlmsg_cancel:
2714 genlmsg_cancel(skb, hdr);
2715msg_cancel:
2716 return -EMSGSIZE;
2717}
2718
2719int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2720{
2721 int err;
2722 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002723 const struct bucket_table *tbl;
2724 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002725 struct net *net = sock_net(skb->sk);
2726 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002727 u32 tbl_id = cb->args[0];
2728 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002729
Ying Xue07f6c4b2015-01-07 13:41:58 +08002730 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002731 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002732 for (; tbl_id < tbl->size; tbl_id++) {
2733 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002734 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002735 if (prev_portid && prev_portid != tsk->portid) {
2736 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2737 continue;
2738 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002739
Richard Alped6e164e2015-01-16 12:30:40 +01002740 err = __tipc_nl_add_sk(skb, cb, tsk);
2741 if (err) {
2742 prev_portid = tsk->portid;
2743 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2744 goto out;
2745 }
2746 prev_portid = 0;
2747 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002748 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002749 }
Richard Alped6e164e2015-01-16 12:30:40 +01002750out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002751 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002752 cb->args[0] = tbl_id;
2753 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002754
2755 return skb->len;
2756}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002757
2758/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002759static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2760 struct netlink_callback *cb,
2761 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002762{
2763 void *hdr;
2764 struct nlattr *attrs;
2765
2766 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002767 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002768 if (!hdr)
2769 goto msg_cancel;
2770
2771 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2772 if (!attrs)
2773 goto genlmsg_cancel;
2774
2775 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2776 goto attr_msg_cancel;
2777 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2778 goto attr_msg_cancel;
2779 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2780 goto attr_msg_cancel;
2781 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2782 goto attr_msg_cancel;
2783
2784 nla_nest_end(skb, attrs);
2785 genlmsg_end(skb, hdr);
2786
2787 return 0;
2788
2789attr_msg_cancel:
2790 nla_nest_cancel(skb, attrs);
2791genlmsg_cancel:
2792 genlmsg_cancel(skb, hdr);
2793msg_cancel:
2794 return -EMSGSIZE;
2795}
2796
2797/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002798static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2799 struct netlink_callback *cb,
2800 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002801{
2802 int err;
2803 struct publication *p;
2804
2805 if (*last_publ) {
2806 list_for_each_entry(p, &tsk->publications, pport_list) {
2807 if (p->key == *last_publ)
2808 break;
2809 }
2810 if (p->key != *last_publ) {
2811 /* We never set seq or call nl_dump_check_consistent()
2812 * this means that setting prev_seq here will cause the
2813 * consistence check to fail in the netlink callback
2814 * handler. Resulting in the last NLMSG_DONE message
2815 * having the NLM_F_DUMP_INTR flag set.
2816 */
2817 cb->prev_seq = 1;
2818 *last_publ = 0;
2819 return -EPIPE;
2820 }
2821 } else {
2822 p = list_first_entry(&tsk->publications, struct publication,
2823 pport_list);
2824 }
2825
2826 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2827 err = __tipc_nl_add_sk_publ(skb, cb, p);
2828 if (err) {
2829 *last_publ = p->key;
2830 return err;
2831 }
2832 }
2833 *last_publ = 0;
2834
2835 return 0;
2836}
2837
2838int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2839{
2840 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002841 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002842 u32 last_publ = cb->args[1];
2843 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002844 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002845 struct tipc_sock *tsk;
2846
Ying Xue07f6c4b2015-01-07 13:41:58 +08002847 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002848 struct nlattr **attrs;
2849 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2850
2851 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2852 if (err)
2853 return err;
2854
Richard Alpe45e093a2016-05-16 11:14:54 +02002855 if (!attrs[TIPC_NLA_SOCK])
2856 return -EINVAL;
2857
Richard Alpe1a1a1432014-11-20 10:29:11 +01002858 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2859 attrs[TIPC_NLA_SOCK],
Johannes Bergfceb6432017-04-12 14:34:07 +02002860 tipc_nl_sock_policy, NULL);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002861 if (err)
2862 return err;
2863
2864 if (!sock[TIPC_NLA_SOCK_REF])
2865 return -EINVAL;
2866
Ying Xue07f6c4b2015-01-07 13:41:58 +08002867 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002868 }
2869
2870 if (done)
2871 return 0;
2872
Ying Xuee05b31f2015-01-09 15:27:08 +08002873 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002874 if (!tsk)
2875 return -EINVAL;
2876
2877 lock_sock(&tsk->sk);
2878 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2879 if (!err)
2880 done = 1;
2881 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002882 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002883
Ying Xue07f6c4b2015-01-07 13:41:58 +08002884 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002885 cb->args[1] = last_publ;
2886 cb->args[2] = done;
2887
2888 return skb->len;
2889}