blob: 43e4045e72bc00cfbc9db6c1bf987a46e272969b [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 Maloy301bae52014-08-22 18:09:20 -040054
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010055enum {
56 TIPC_LISTEN = TCP_LISTEN,
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +010057 TIPC_ESTABLISHED = TCP_ESTABLISHED,
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +010058 TIPC_OPEN = TCP_CLOSE,
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +010059 TIPC_DISCONNECTING = TCP_CLOSE_WAIT,
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +010060 TIPC_CONNECTING = TCP_SYN_SENT,
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +010061};
62
Jon Paul Maloy301bae52014-08-22 18:09:20 -040063/**
64 * struct tipc_sock - TIPC socket structure
65 * @sk: socket - interacts with 'port' and with user via the socket API
Jon Paul Maloy301bae52014-08-22 18:09:20 -040066 * @conn_type: TIPC type used when connection was established
67 * @conn_instance: TIPC instance used when connection was established
68 * @published: non-zero if port has one or more associated names
69 * @max_pkt: maximum packet size "hint" used when building messages sent by port
Ying Xue07f6c4b2015-01-07 13:41:58 +080070 * @portid: unique port identity in TIPC socket hash table
Jon Paul Maloy301bae52014-08-22 18:09:20 -040071 * @phdr: preformatted message header used when sending messages
Jon Paul Maloy365ad352017-01-03 10:55:11 -050072 * #cong_links: list of congested links
Jon Paul Maloy301bae52014-08-22 18:09:20 -040073 * @publications: list of publications for port
Jon Paul Maloy365ad352017-01-03 10:55:11 -050074 * @blocking_link: address of the congested link we are currently sleeping on
Jon Paul Maloy301bae52014-08-22 18:09:20 -040075 * @pub_count: total # of publications port has made during its lifetime
76 * @probing_state:
Jon Paul Maloy301bae52014-08-22 18:09:20 -040077 * @conn_timeout: the time we can wait for an unresponded setup request
78 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
Jon Paul Maloy365ad352017-01-03 10:55:11 -050079 * @cong_link_cnt: number of congested links
Jon Paul Maloy301bae52014-08-22 18:09:20 -040080 * @sent_unacked: # messages sent by socket, and not yet acked by peer
81 * @rcv_unacked: # messages read by user, but not yet acked back to peer
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +010082 * @peer: 'connected' peer for dgram/rdm
Ying Xue07f6c4b2015-01-07 13:41:58 +080083 * @node: hash table node
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -050084 * @mc_method: cookie for use between socket and broadcast layer
Ying Xue07f6c4b2015-01-07 13:41:58 +080085 * @rcu: rcu struct for tipc_sock
Jon Paul Maloy301bae52014-08-22 18:09:20 -040086 */
87struct tipc_sock {
88 struct sock sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040089 u32 conn_type;
90 u32 conn_instance;
91 int published;
92 u32 max_pkt;
Ying Xue07f6c4b2015-01-07 13:41:58 +080093 u32 portid;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040094 struct tipc_msg phdr;
Jon Paul Maloy365ad352017-01-03 10:55:11 -050095 struct list_head cong_links;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040096 struct list_head publications;
97 u32 pub_count;
Jon Paul Maloy301bae52014-08-22 18:09:20 -040098 uint conn_timeout;
99 atomic_t dupl_rcvcnt;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100100 bool probe_unacked;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500101 u16 cong_link_cnt;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400102 u16 snt_unacked;
103 u16 snd_win;
Jon Paul Maloy60020e12016-05-02 11:58:46 -0400104 u16 peer_caps;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400105 u16 rcv_unacked;
106 u16 rcv_win;
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +0100107 struct sockaddr_tipc peer;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800108 struct rhash_head node;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500109 struct tipc_mc_method mc_method;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800110 struct rcu_head rcu;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400111};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400113static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb);
David S. Miller676d2362014-04-11 16:15:36 -0400114static void tipc_data_ready(struct sock *sk);
Ying Xuef288bef2012-08-21 11:16:57 +0800115static void tipc_write_space(struct sock *sk);
Ying Xuef4195d12015-11-22 15:46:05 +0800116static void tipc_sock_destruct(struct sock *sk);
Ying Xue247f0f32014-02-18 16:06:46 +0800117static int tipc_release(struct socket *sock);
118static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags);
Ying Xuef2f2a962015-01-09 15:27:02 +0800119static void tipc_sk_timeout(unsigned long data);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400120static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400121 struct tipc_name_seq const *seq);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400122static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400123 struct tipc_name_seq const *seq);
Ying Xuee05b31f2015-01-09 15:27:08 +0800124static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800125static int tipc_sk_insert(struct tipc_sock *tsk);
126static void tipc_sk_remove(struct tipc_sock *tsk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500127static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
Ying Xue39a0295f2015-03-02 15:37:47 +0800128static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129
Florian Westphalbca65ea2008-02-07 18:18:01 -0800130static const struct proto_ops packet_ops;
131static const struct proto_ops stream_ops;
132static const struct proto_ops msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100133static struct proto tipc_proto;
Herbert Xu6cca72892015-03-20 21:57:05 +1100134static const struct rhashtable_params tsk_rht_params;
135
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500136static u32 tsk_own_node(struct tipc_sock *tsk)
137{
138 return msg_prevnode(&tsk->phdr);
139}
140
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400141static u32 tsk_peer_node(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400142{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400143 return msg_destnode(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400144}
145
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400146static u32 tsk_peer_port(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400147{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400148 return msg_destport(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400149}
150
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400151static bool tsk_unreliable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400152{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400153 return msg_src_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400154}
155
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400156static void tsk_set_unreliable(struct tipc_sock *tsk, bool unreliable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400157{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400158 msg_set_src_droppable(&tsk->phdr, unreliable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400159}
160
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400161static bool tsk_unreturnable(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400162{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400163 return msg_dest_droppable(&tsk->phdr) != 0;
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400164}
165
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400166static void tsk_set_unreturnable(struct tipc_sock *tsk, bool unreturnable)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400167{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400168 msg_set_dest_droppable(&tsk->phdr, unreturnable ? 1 : 0);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400169}
170
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400171static int tsk_importance(struct tipc_sock *tsk)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400172{
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400173 return msg_importance(&tsk->phdr);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400174}
175
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400176static int tsk_set_importance(struct tipc_sock *tsk, int imp)
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400177{
178 if (imp > TIPC_CRITICAL_IMPORTANCE)
179 return -EINVAL;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400180 msg_set_importance(&tsk->phdr, (u32)imp);
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400181 return 0;
182}
Jon Paul Maloy8826cde2014-03-12 11:31:09 -0400183
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400184static struct tipc_sock *tipc_sk(const struct sock *sk)
185{
186 return container_of(sk, struct tipc_sock, sk);
187}
188
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400189static bool tsk_conn_cong(struct tipc_sock *tsk)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400190{
Jon Paul Maloy6998cc62016-11-24 18:47:07 -0500191 return tsk->snt_unacked > tsk->snd_win;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400192}
193
194/* tsk_blocks(): translate a buffer size in bytes to number of
195 * advertisable blocks, taking into account the ratio truesize(len)/len
196 * We can trust that this ratio is always < 4 for len >= FLOWCTL_BLK_SZ
197 */
198static u16 tsk_adv_blocks(int len)
199{
200 return len / FLOWCTL_BLK_SZ / 4;
201}
202
203/* tsk_inc(): increment counter for sent or received data
204 * - If block based flow control is not supported by peer we
205 * fall back to message based ditto, incrementing the counter
206 */
207static u16 tsk_inc(struct tipc_sock *tsk, int msglen)
208{
209 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
210 return ((msglen / FLOWCTL_BLK_SZ) + 1);
211 return 1;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400212}
213
Per Lidenb97bf3f2006-01-02 19:04:38 +0100214/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400215 * tsk_advance_rx_queue - discard first buffer in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700216 *
217 * Caller must hold socket lock
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400219static void tsk_advance_rx_queue(struct sock *sk)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220{
Allan Stephens5f6d9122011-11-04 13:24:29 -0400221 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100222}
223
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400224/* tipc_sk_respond() : send response message back to sender
225 */
226static void tipc_sk_respond(struct sock *sk, struct sk_buff *skb, int err)
227{
228 u32 selector;
229 u32 dnode;
230 u32 onode = tipc_own_addr(sock_net(sk));
231
232 if (!tipc_msg_reverse(onode, &skb, err))
233 return;
234
235 dnode = msg_destnode(buf_msg(skb));
236 selector = msg_origport(buf_msg(skb));
237 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
238}
239
Per Lidenb97bf3f2006-01-02 19:04:38 +0100240/**
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400241 * tsk_rej_rx_queue - reject all buffers in socket receive queue
Allan Stephens0c3141e2008-04-15 00:22:02 -0700242 *
243 * Caller must hold socket lock
244 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400245static void tsk_rej_rx_queue(struct sock *sk)
Allan Stephens0c3141e2008-04-15 00:22:02 -0700246{
Ying Xuea6ca1092014-11-26 11:41:55 +0800247 struct sk_buff *skb;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700248
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400249 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
250 tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700251}
252
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100253static bool tipc_sk_connected(struct sock *sk)
254{
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100255 return sk->sk_state == TIPC_ESTABLISHED;
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100256}
257
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +0100258/* tipc_sk_type_connectionless - check if the socket is datagram socket
259 * @sk: socket
260 *
261 * Returns true if connection less, false otherwise
262 */
263static bool tipc_sk_type_connectionless(struct sock *sk)
264{
265 return sk->sk_type == SOCK_RDM || sk->sk_type == SOCK_DGRAM;
266}
267
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400268/* tsk_peer_msg - verify if message was sent by connected port's peer
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400269 *
270 * Handles cases where the node's network address has changed from
271 * the default of <0.0.0> to its configured setting.
272 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -0400273static bool tsk_peer_msg(struct tipc_sock *tsk, struct tipc_msg *msg)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400274{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100275 struct sock *sk = &tsk->sk;
276 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400277 u32 peer_port = tsk_peer_port(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400278 u32 orig_node;
279 u32 peer_node;
280
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +0100281 if (unlikely(!tipc_sk_connected(sk)))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400282 return false;
283
284 if (unlikely(msg_origport(msg) != peer_port))
285 return false;
286
287 orig_node = msg_orignode(msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400288 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400289
290 if (likely(orig_node == peer_node))
291 return true;
292
Ying Xue34747532015-01-09 15:27:10 +0800293 if (!orig_node && (peer_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400294 return true;
295
Ying Xue34747532015-01-09 15:27:10 +0800296 if (!peer_node && (orig_node == tn->own_addr))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -0400297 return true;
298
299 return false;
300}
301
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100302/* tipc_set_sk_state - set the sk_state of the socket
303 * @sk: socket
304 *
305 * Caller must hold socket lock
306 *
307 * Returns 0 on success, errno otherwise
308 */
309static int tipc_set_sk_state(struct sock *sk, int state)
310{
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100311 int oldsk_state = sk->sk_state;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100312 int res = -EINVAL;
313
314 switch (state) {
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100315 case TIPC_OPEN:
316 res = 0;
317 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100318 case TIPC_LISTEN:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100319 case TIPC_CONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100320 if (oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100321 res = 0;
322 break;
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100323 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100324 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100325 oldsk_state == TIPC_OPEN)
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100326 res = 0;
327 break;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100328 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +0100329 if (oldsk_state == TIPC_CONNECTING ||
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100330 oldsk_state == TIPC_ESTABLISHED)
331 res = 0;
332 break;
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100333 }
334
335 if (!res)
336 sk->sk_state = state;
337
338 return res;
339}
340
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -0500341static int tipc_sk_sock_err(struct socket *sock, long *timeout)
342{
343 struct sock *sk = sock->sk;
344 int err = sock_error(sk);
345 int typ = sock->type;
346
347 if (err)
348 return err;
349 if (typ == SOCK_STREAM || typ == SOCK_SEQPACKET) {
350 if (sk->sk_state == TIPC_DISCONNECTING)
351 return -EPIPE;
352 else if (!tipc_sk_connected(sk))
353 return -ENOTCONN;
354 }
355 if (!*timeout)
356 return -EAGAIN;
357 if (signal_pending(current))
358 return sock_intr_errno(*timeout);
359
360 return 0;
361}
362
363#define tipc_wait_for_cond(sock_, timeout_, condition_) \
364({ \
365 int rc_ = 0; \
366 int done_ = 0; \
367 \
368 while (!(condition_) && !done_) { \
369 struct sock *sk_ = sock->sk; \
370 DEFINE_WAIT_FUNC(wait_, woken_wake_function); \
371 \
372 rc_ = tipc_sk_sock_err(sock_, timeout_); \
373 if (rc_) \
374 break; \
375 prepare_to_wait(sk_sleep(sk_), &wait_, \
376 TASK_INTERRUPTIBLE); \
377 done_ = sk_wait_event(sk_, timeout_, \
378 (condition_), &wait_); \
379 remove_wait_queue(sk_sleep(sk_), &wait_); \
380 } \
381 rc_; \
382})
383
Allan Stephens0c3141e2008-04-15 00:22:02 -0700384/**
Ying Xuec5fa7b32013-06-17 10:54:39 -0400385 * tipc_sk_create - create a TIPC socket
Allan Stephens0c3141e2008-04-15 00:22:02 -0700386 * @net: network namespace (must be default network)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387 * @sock: pre-allocated socket structure
388 * @protocol: protocol indicator (must be 0)
Eric Paris3f378b62009-11-05 22:18:14 -0800389 * @kern: caused by kernel or by userspace?
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900390 *
Allan Stephens0c3141e2008-04-15 00:22:02 -0700391 * This routine creates additional data structures used by the TIPC socket,
392 * initializes them, and links them together.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 *
394 * Returns 0 on success, errno otherwise
395 */
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400396static int tipc_sk_create(struct net *net, struct socket *sock,
397 int protocol, int kern)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398{
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500399 struct tipc_net *tn;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700400 const struct proto_ops *ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 struct sock *sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400402 struct tipc_sock *tsk;
Jon Paul Maloy5b8fa7c2014-08-22 18:09:13 -0400403 struct tipc_msg *msg;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700404
405 /* Validate arguments */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406 if (unlikely(protocol != 0))
407 return -EPROTONOSUPPORT;
408
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409 switch (sock->type) {
410 case SOCK_STREAM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700411 ops = &stream_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100412 break;
413 case SOCK_SEQPACKET:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700414 ops = &packet_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415 break;
416 case SOCK_DGRAM:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 case SOCK_RDM:
Allan Stephens0c3141e2008-04-15 00:22:02 -0700418 ops = &msg_ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100419 break;
Allan Stephens49978652006-06-25 23:47:18 -0700420 default:
Allan Stephens49978652006-06-25 23:47:18 -0700421 return -EPROTOTYPE;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100422 }
423
Allan Stephens0c3141e2008-04-15 00:22:02 -0700424 /* Allocate socket's protocol area */
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500425 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto, kern);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700426 if (sk == NULL)
427 return -ENOMEM;
428
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400429 tsk = tipc_sk(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400430 tsk->max_pkt = MAX_PKT_DEFAULT;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400431 INIT_LIST_HEAD(&tsk->publications);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500432 INIT_LIST_HEAD(&tsk->cong_links);
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400433 msg = &tsk->phdr;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500434 tn = net_generic(sock_net(sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435
Allan Stephens0c3141e2008-04-15 00:22:02 -0700436 /* Finish initializing socket data structures */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700437 sock->ops = ops;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100438 sock_init_data(sock, sk);
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100439 tipc_set_sk_state(sk, TIPC_OPEN);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800440 if (tipc_sk_insert(tsk)) {
Masanari Iidac19ca6c2016-02-08 20:53:12 +0900441 pr_warn("Socket create failed; port number exhausted\n");
Ying Xue07f6c4b2015-01-07 13:41:58 +0800442 return -EINVAL;
443 }
Herbert Xu40f9f432017-02-11 19:26:46 +0800444
445 /* Ensure tsk is visible before we read own_addr. */
446 smp_mb();
447
448 tipc_msg_init(tn->own_addr, msg, TIPC_LOW_IMPORTANCE, TIPC_NAMED_MSG,
449 NAMED_H_SIZE, 0);
450
Ying Xue07f6c4b2015-01-07 13:41:58 +0800451 msg_set_origport(msg, tsk->portid);
Ying Xue3721e9c2015-01-13 17:07:48 +0800452 setup_timer(&sk->sk_timer, tipc_sk_timeout, (unsigned long)tsk);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100453 sk->sk_shutdown = 0;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400454 sk->sk_backlog_rcv = tipc_backlog_rcv;
Ying Xuecc79dd12013-06-17 10:54:37 -0400455 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
Ying Xuef288bef2012-08-21 11:16:57 +0800456 sk->sk_data_ready = tipc_data_ready;
457 sk->sk_write_space = tipc_write_space;
Ying Xuef4195d12015-11-22 15:46:05 +0800458 sk->sk_destruct = tipc_sock_destruct;
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -0400459 tsk->conn_timeout = CONN_TIMEOUT_DEFAULT;
460 atomic_set(&tsk->dupl_rcvcnt, 0);
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700461
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400462 /* Start out with safe limits until we receive an advertised window */
463 tsk->snd_win = tsk_adv_blocks(RCVBUF_MIN);
464 tsk->rcv_win = tsk->snd_win;
465
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +0100466 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400467 tsk_set_unreturnable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700468 if (sock->type == SOCK_DGRAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400469 tsk_set_unreliable(tsk, true);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700470 }
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100471
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472 return 0;
473}
474
Ying Xue07f6c4b2015-01-07 13:41:58 +0800475static void tipc_sk_callback(struct rcu_head *head)
476{
477 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
478
479 sock_put(&tsk->sk);
480}
481
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100482/* Caller should hold socket lock for the socket. */
483static void __tipc_shutdown(struct socket *sock, int error)
484{
485 struct sock *sk = sock->sk;
486 struct tipc_sock *tsk = tipc_sk(sk);
487 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500488 long timeout = CONN_TIMEOUT_DEFAULT;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100489 u32 dnode = tsk_peer_node(tsk);
490 struct sk_buff *skb;
491
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500492 /* Avoid that hi-prio shutdown msgs bypass msgs in link wakeup queue */
493 tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
494 !tsk_conn_cong(tsk)));
495
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100496 /* Reject all unreceived messages, except on an active connection
497 * (which disconnects locally & sends a 'FIN+' to peer).
498 */
499 while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
500 if (TIPC_SKB_CB(skb)->bytes_read) {
501 kfree_skb(skb);
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500502 continue;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100503 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500504 if (!tipc_sk_type_connectionless(sk) &&
505 sk->sk_state != TIPC_DISCONNECTING) {
506 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
507 tipc_node_remove_conn(net, dnode, tsk->portid);
508 }
509 tipc_sk_respond(sk, skb, error);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100510 }
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500511
512 if (tipc_sk_type_connectionless(sk))
513 return;
514
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100515 if (sk->sk_state != TIPC_DISCONNECTING) {
516 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
517 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
518 tsk_own_node(tsk), tsk_peer_port(tsk),
519 tsk->portid, error);
520 if (skb)
521 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
Jon Paul Maloy693c5642016-12-22 07:22:29 -0500522 tipc_node_remove_conn(net, dnode, tsk->portid);
523 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100524 }
525}
526
Ying Xuec5fa7b32013-06-17 10:54:39 -0400527/**
Ying Xue247f0f32014-02-18 16:06:46 +0800528 * tipc_release - destroy a TIPC socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529 * @sock: socket to destroy
530 *
531 * This routine cleans up any messages that are still queued on the socket.
532 * For DGRAM and RDM socket types, all queued messages are rejected.
533 * For SEQPACKET and STREAM socket types, the first message is rejected
534 * and any others are discarded. (If the first message on a STREAM socket
535 * is partially-read, it is discarded and the next one is rejected instead.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900536 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 * NOTE: Rejected messages are not necessarily returned to the sender! They
538 * are returned or discarded according to the "destination droppable" setting
539 * specified for the message by the sender.
540 *
541 * Returns 0 on success, errno otherwise
542 */
Ying Xue247f0f32014-02-18 16:06:46 +0800543static int tipc_release(struct socket *sock)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100545 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400546 struct tipc_sock *tsk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547
Allan Stephens0c3141e2008-04-15 00:22:02 -0700548 /*
549 * Exit if socket isn't fully initialized (occurs when a failed accept()
550 * releases a pre-allocated child socket that was never used)
551 */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700552 if (sk == NULL)
553 return 0;
554
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400555 tsk = tipc_sk(sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700556 lock_sock(sk);
557
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100558 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
559 sk->sk_shutdown = SHUTDOWN_MASK;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400560 tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue1ea23a22015-05-28 13:19:22 +0800561 sk_stop_timer(sk, &sk->sk_timer);
Ying Xue07f6c4b2015-01-07 13:41:58 +0800562 tipc_sk_remove(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563
Allan Stephens0c3141e2008-04-15 00:22:02 -0700564 /* Reject any messages that accumulated in backlog queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -0700565 release_sock(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500566 u32_list_purge(&tsk->cong_links);
567 tsk->cong_link_cnt = 0;
Ying Xue07f6c4b2015-01-07 13:41:58 +0800568 call_rcu(&tsk->rcu, tipc_sk_callback);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700569 sock->sk = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570
Geert Uytterhoeven065d7e32014-04-06 15:56:14 +0200571 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100572}
573
574/**
Ying Xue247f0f32014-02-18 16:06:46 +0800575 * tipc_bind - associate or disassocate TIPC name(s) with a socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 * @sock: socket structure
577 * @uaddr: socket address describing name(s) and desired operation
578 * @uaddr_len: size of socket address data structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900579 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100580 * Name and name sequence binding is indicated using a positive scope value;
581 * a negative scope value unbinds the specified name. Specifying no name
582 * (i.e. a socket address length of 0) unbinds all names from the socket.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900583 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100584 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700585 *
586 * NOTE: This routine doesn't need to take the socket lock since it doesn't
587 * access any non-constant socket information.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588 */
Ying Xue247f0f32014-02-18 16:06:46 +0800589static int tipc_bind(struct socket *sock, struct sockaddr *uaddr,
590 int uaddr_len)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591{
Ying Xue84602762013-12-27 10:18:28 +0800592 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400594 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue84602762013-12-27 10:18:28 +0800595 int res = -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596
Ying Xue84602762013-12-27 10:18:28 +0800597 lock_sock(sk);
598 if (unlikely(!uaddr_len)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400599 res = tipc_sk_withdraw(tsk, 0, NULL);
Ying Xue84602762013-12-27 10:18:28 +0800600 goto exit;
601 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900602
Ying Xue84602762013-12-27 10:18:28 +0800603 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
604 res = -EINVAL;
605 goto exit;
606 }
607 if (addr->family != AF_TIPC) {
608 res = -EAFNOSUPPORT;
609 goto exit;
610 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100611
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612 if (addr->addrtype == TIPC_ADDR_NAME)
613 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
Ying Xue84602762013-12-27 10:18:28 +0800614 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
615 res = -EAFNOSUPPORT;
616 goto exit;
617 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900618
Ying Xue13a2e892013-06-17 10:54:40 -0400619 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
Ying Xue7d0ab172013-06-17 10:54:41 -0400620 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
Ying Xue84602762013-12-27 10:18:28 +0800621 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
622 res = -EACCES;
623 goto exit;
624 }
Allan Stephensc422f1b2011-11-02 15:49:40 -0400625
Ying Xue84602762013-12-27 10:18:28 +0800626 res = (addr->scope > 0) ?
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400627 tipc_sk_publish(tsk, addr->scope, &addr->addr.nameseq) :
628 tipc_sk_withdraw(tsk, -addr->scope, &addr->addr.nameseq);
Ying Xue84602762013-12-27 10:18:28 +0800629exit:
630 release_sock(sk);
631 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100632}
633
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900634/**
Ying Xue247f0f32014-02-18 16:06:46 +0800635 * tipc_getname - get port ID of socket or peer socket
Per Lidenb97bf3f2006-01-02 19:04:38 +0100636 * @sock: socket structure
637 * @uaddr: area for returned socket address
638 * @uaddr_len: area for returned length of socket address
Allan Stephens2da59912008-07-14 22:43:32 -0700639 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900640 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100641 * Returns 0 on success, errno otherwise
Allan Stephens0c3141e2008-04-15 00:22:02 -0700642 *
Allan Stephens2da59912008-07-14 22:43:32 -0700643 * NOTE: This routine doesn't need to take the socket lock since it only
644 * accesses socket information that is unchanging (or which changes in
Allan Stephens0e659672010-12-31 18:59:32 +0000645 * a completely predictable manner).
Per Lidenb97bf3f2006-01-02 19:04:38 +0100646 */
Ying Xue247f0f32014-02-18 16:06:46 +0800647static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
648 int *uaddr_len, int peer)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100650 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100651 struct sock *sk = sock->sk;
652 struct tipc_sock *tsk = tipc_sk(sk);
Ying Xue34747532015-01-09 15:27:10 +0800653 struct tipc_net *tn = net_generic(sock_net(sock->sk), tipc_net_id);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100654
Kulikov Vasiliy88f8a5e2010-10-31 07:10:32 +0000655 memset(addr, 0, sizeof(*addr));
Allan Stephens0c3141e2008-04-15 00:22:02 -0700656 if (peer) {
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100657 if ((!tipc_sk_connected(sk)) &&
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +0100658 ((peer != 2) || (sk->sk_state != TIPC_DISCONNECTING)))
Allan Stephens2da59912008-07-14 22:43:32 -0700659 return -ENOTCONN;
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400660 addr->addr.id.ref = tsk_peer_port(tsk);
661 addr->addr.id.node = tsk_peer_node(tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -0700662 } else {
Ying Xue07f6c4b2015-01-07 13:41:58 +0800663 addr->addr.id.ref = tsk->portid;
Ying Xue34747532015-01-09 15:27:10 +0800664 addr->addr.id.node = tn->own_addr;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700665 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666
667 *uaddr_len = sizeof(*addr);
668 addr->addrtype = TIPC_ADDR_ID;
669 addr->family = AF_TIPC;
670 addr->scope = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100671 addr->addr.name.domain = 0;
672
Allan Stephens0c3141e2008-04-15 00:22:02 -0700673 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100674}
675
676/**
Ying Xue247f0f32014-02-18 16:06:46 +0800677 * tipc_poll - read and possibly block on pollmask
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678 * @file: file structure associated with the socket
679 * @sock: socket for which to calculate the poll bits
680 * @wait: ???
681 *
Allan Stephens9b674e82008-03-26 16:48:21 -0700682 * Returns pollmask value
683 *
684 * COMMENTARY:
685 * It appears that the usual socket locking mechanisms are not useful here
686 * since the pollmask info is potentially out-of-date the moment this routine
687 * exits. TCP and other protocols seem to rely on higher level poll routines
688 * to handle any preventable race conditions, so TIPC will do the same ...
689 *
Allan Stephensf662c072010-08-17 11:00:06 +0000690 * IMPORTANT: The fact that a read or write operation is indicated does NOT
691 * imply that the operation will succeed, merely that it should be performed
692 * and will not block.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693 */
Ying Xue247f0f32014-02-18 16:06:46 +0800694static unsigned int tipc_poll(struct file *file, struct socket *sock,
695 poll_table *wait)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100696{
Allan Stephens9b674e82008-03-26 16:48:21 -0700697 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -0400698 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensf662c072010-08-17 11:00:06 +0000699 u32 mask = 0;
Allan Stephens9b674e82008-03-26 16:48:21 -0700700
Ying Xuef288bef2012-08-21 11:16:57 +0800701 sock_poll_wait(file, sk_sleep(sk), wait);
Allan Stephens9b674e82008-03-26 16:48:21 -0700702
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +0100703 if (sk->sk_shutdown & RCV_SHUTDOWN)
704 mask |= POLLRDHUP | POLLIN | POLLRDNORM;
705 if (sk->sk_shutdown == SHUTDOWN_MASK)
706 mask |= POLLHUP;
707
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100708 switch (sk->sk_state) {
709 case TIPC_ESTABLISHED:
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500710 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
Allan Stephensf662c072010-08-17 11:00:06 +0000711 mask |= POLLOUT;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100712 /* fall thru' */
713 case TIPC_LISTEN:
714 case TIPC_CONNECTING:
Allan Stephensf662c072010-08-17 11:00:06 +0000715 if (!skb_queue_empty(&sk->sk_receive_queue))
716 mask |= (POLLIN | POLLRDNORM);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100717 break;
718 case TIPC_OPEN:
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500719 if (!tsk->cong_link_cnt)
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +0100720 mask |= POLLOUT;
721 if (tipc_sk_type_connectionless(sk) &&
722 (!skb_queue_empty(&sk->sk_receive_queue)))
723 mask |= (POLLIN | POLLRDNORM);
724 break;
725 case TIPC_DISCONNECTING:
726 mask = (POLLIN | POLLRDNORM | POLLHUP);
727 break;
Allan Stephensf662c072010-08-17 11:00:06 +0000728 }
Allan Stephens9b674e82008-03-26 16:48:21 -0700729
730 return mask;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100731}
732
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400733/**
734 * tipc_sendmcast - send multicast message
735 * @sock: socket structure
736 * @seq: destination address
Al Viro562640f2014-11-15 01:13:43 -0500737 * @msg: message to send
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500738 * @dlen: length of data to send
739 * @timeout: timeout to wait for wakeup
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400740 *
741 * Called from function tipc_sendmsg(), which has done all sanity checks
742 * Returns the number of bytes sent on success, or errno
743 */
744static int tipc_sendmcast(struct socket *sock, struct tipc_name_seq *seq,
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500745 struct msghdr *msg, size_t dlen, long timeout)
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400746{
747 struct sock *sk = sock->sk;
Jon Paul Maloyc5898632015-02-05 08:36:36 -0500748 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500749 struct tipc_msg *hdr = &tsk->phdr;
Ying Xuef2f98002015-01-09 15:27:05 +0800750 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500751 int mtu = tipc_bcast_get_mtu(net);
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500752 struct tipc_mc_method *method = &tsk->mc_method;
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500753 u32 domain = addr_domain(net, TIPC_CLUSTER_SCOPE);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500754 struct sk_buff_head pkts;
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500755 struct tipc_nlist dsts;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400756 int rc;
757
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500758 /* Block or return if any destination link is congested */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500759 rc = tipc_wait_for_cond(sock, &timeout, !tsk->cong_link_cnt);
760 if (unlikely(rc))
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400761 return rc;
762
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500763 /* Lookup destination nodes */
764 tipc_nlist_init(&dsts, tipc_own_addr(net));
765 tipc_nametbl_lookup_dst_nodes(net, seq->type, seq->lower,
766 seq->upper, domain, &dsts);
767 if (!dsts.local && !dsts.remote)
768 return -EHOSTUNREACH;
769
770 /* Build message header */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500771 msg_set_type(hdr, TIPC_MCAST_MSG);
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500772 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500773 msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
774 msg_set_destport(hdr, 0);
775 msg_set_destnode(hdr, 0);
776 msg_set_nametype(hdr, seq->type);
777 msg_set_namelower(hdr, seq->lower);
778 msg_set_nameupper(hdr, seq->upper);
Jon Paul Maloy22d85c72015-07-16 16:54:23 -0400779
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500780 /* Build message as chain of buffers */
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500781 skb_queue_head_init(&pkts);
782 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500783
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500784 /* Send message if build was successful */
785 if (unlikely(rc == dlen))
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -0500786 rc = tipc_mcast_xmit(net, &pkts, method, &dsts,
Jon Paul Maloya853e4c2017-01-18 13:50:52 -0500787 &tsk->cong_link_cnt);
788
789 tipc_nlist_purge(&dsts);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500790
791 return rc ? rc : dlen;
Jon Paul Maloy0abd8ff2014-07-16 20:41:01 -0400792}
793
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500794/**
795 * tipc_sk_mcast_rcv - Deliver multicast messages to all destination sockets
796 * @arrvq: queue with arriving messages, to be cloned after destination lookup
797 * @inputq: queue with cloned messages, delivered to socket after dest lookup
798 *
799 * Multi-threaded: parallel calls with reference to same queues may occur
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400800 */
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500801void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
802 struct sk_buff_head *inputq)
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400803{
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500804 struct tipc_msg *msg;
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -0500805 struct list_head dports;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500806 u32 portid;
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400807 u32 scope = TIPC_CLUSTER_SCOPE;
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500808 struct sk_buff_head tmpq;
809 uint hsz;
810 struct sk_buff *skb, *_skb;
Jon Paul Maloy3c724ac2015-02-05 08:36:43 -0500811
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500812 __skb_queue_head_init(&tmpq);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -0500813 INIT_LIST_HEAD(&dports);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400814
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500815 skb = tipc_skb_peek(arrvq, &inputq->lock);
816 for (; skb; skb = tipc_skb_peek(arrvq, &inputq->lock)) {
817 msg = buf_msg(skb);
818 hsz = skb_headroom(skb) + msg_hdr_sz(msg);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400819
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500820 if (in_own_node(net, msg_orignode(msg)))
821 scope = TIPC_NODE_SCOPE;
822
823 /* Create destination port list and message clones: */
824 tipc_nametbl_mc_translate(net,
825 msg_nametype(msg), msg_namelower(msg),
826 msg_nameupper(msg), scope, &dports);
Jon Paul Maloy4d8642d2017-01-03 10:55:10 -0500827 portid = u32_pop(&dports);
828 for (; portid; portid = u32_pop(&dports)) {
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500829 _skb = __pskb_copy(skb, hsz, GFP_ATOMIC);
830 if (_skb) {
831 msg_set_destport(buf_msg(_skb), portid);
832 __skb_queue_tail(&tmpq, _skb);
833 continue;
834 }
835 pr_warn("Failed to clone mcast rcv buffer\n");
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400836 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500837 /* Append to inputq if not already done by other thread */
838 spin_lock_bh(&inputq->lock);
839 if (skb_peek(arrvq) == skb) {
840 skb_queue_splice_tail_init(&tmpq, inputq);
841 kfree_skb(__skb_dequeue(arrvq));
842 }
843 spin_unlock_bh(&inputq->lock);
844 __skb_queue_purge(&tmpq);
845 kfree_skb(skb);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400846 }
Jon Paul Maloycb1b7282015-02-05 08:36:44 -0500847 tipc_sk_rcv(net, inputq);
Jon Paul Maloy078bec82014-07-16 20:41:00 -0400848}
849
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900850/**
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500851 * tipc_sk_proto_rcv - receive a connection mng protocol message
852 * @tsk: receiving socket
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400853 * @skb: pointer to message buffer.
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500854 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400855static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff *skb,
856 struct sk_buff_head *xmitq)
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500857{
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400858 struct sock *sk = &tsk->sk;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400859 u32 onode = tsk_own_node(tsk);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400860 struct tipc_msg *hdr = buf_msg(skb);
861 int mtyp = msg_type(hdr);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400862 bool conn_cong;
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400863
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500864 /* Ignore if connection cannot be validated: */
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400865 if (!tsk_peer_msg(tsk, hdr))
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500866 goto exit;
867
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +0100868 tsk->probe_unacked = false;
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500869
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400870 if (mtyp == CONN_PROBE) {
871 msg_set_type(hdr, CONN_PROBE_REPLY);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -0400872 if (tipc_msg_reverse(onode, &skb, TIPC_OK))
873 __skb_queue_tail(xmitq, skb);
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400874 return;
875 } else if (mtyp == CONN_ACK) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400876 conn_cong = tsk_conn_cong(tsk);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -0400877 tsk->snt_unacked -= msg_conn_ack(hdr);
878 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
879 tsk->snd_win = msg_adv_win(hdr);
Jon Paul Maloy60120522014-06-25 20:41:42 -0500880 if (conn_cong)
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400881 sk->sk_write_space(sk);
882 } else if (mtyp != CONN_PROBE_REPLY) {
883 pr_warn("Received unknown CONN_PROTO msg\n");
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500884 }
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500885exit:
Jon Paul Maloybcd3ffd2015-07-22 10:11:19 -0400886 kfree_skb(skb);
Jon Paul Maloyac0074e2014-06-25 20:41:41 -0500887}
888
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500889/**
Ying Xue247f0f32014-02-18 16:06:46 +0800890 * tipc_sendmsg - send message in connectionless manner
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 * @sock: socket structure
892 * @m: message to send
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500893 * @dsz: amount of user data to be sent
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900894 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100895 * Message must have an destination specified explicitly.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900896 * Used for SOCK_RDM and SOCK_DGRAM messages,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100897 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
898 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900899 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100900 * Returns the number of bytes sent on success, or errno otherwise
901 */
Ying Xue1b784142015-03-02 15:37:48 +0800902static int tipc_sendmsg(struct socket *sock,
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500903 struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100904{
Ying Xue39a0295f2015-03-02 15:37:47 +0800905 struct sock *sk = sock->sk;
906 int ret;
907
908 lock_sock(sk);
909 ret = __tipc_sendmsg(sock, m, dsz);
910 release_sock(sk);
911
912 return ret;
913}
914
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500915static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a0295f2015-03-02 15:37:47 +0800916{
Allan Stephens0c3141e2008-04-15 00:22:02 -0700917 struct sock *sk = sock->sk;
Ying Xuef2f98002015-01-09 15:27:05 +0800918 struct net *net = sock_net(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500919 struct tipc_sock *tsk = tipc_sk(sk);
920 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
921 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
922 struct list_head *clinks = &tsk->cong_links;
923 bool syn = !tipc_sk_type_connectionless(sk);
924 struct tipc_msg *hdr = &tsk->phdr;
Erik Hugnef2f80362015-03-19 09:02:19 +0100925 struct tipc_name_seq *seq;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500926 struct sk_buff_head pkts;
927 u32 type, inst, domain;
928 u32 dnode, dport;
929 int mtu, rc;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100930
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500931 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
Allan Stephensc29c3f72010-04-20 17:58:24 -0400932 return -EMSGSIZE;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500933
Erik Hugnef2f80362015-03-19 09:02:19 +0100934 if (unlikely(!dest)) {
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500935 dest = &tsk->peer;
936 if (!syn || dest->family != AF_TIPC)
Erik Hugnef2f80362015-03-19 09:02:19 +0100937 return -EDESTADDRREQ;
Erik Hugnef2f80362015-03-19 09:02:19 +0100938 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500939
940 if (unlikely(m->msg_namelen < sizeof(*dest)))
941 return -EINVAL;
942
943 if (unlikely(dest->family != AF_TIPC))
944 return -EINVAL;
945
946 if (unlikely(syn)) {
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +0100947 if (sk->sk_state == TIPC_LISTEN)
Ying Xue39a0295f2015-03-02 15:37:47 +0800948 return -EPIPE;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +0100949 if (sk->sk_state != TIPC_OPEN)
Ying Xue39a0295f2015-03-02 15:37:47 +0800950 return -EISCONN;
951 if (tsk->published)
952 return -EOPNOTSUPP;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700953 if (dest->addrtype == TIPC_ADDR_NAME) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -0400954 tsk->conn_type = dest->addr.name.name.type;
955 tsk->conn_instance = dest->addr.name.name.instance;
Allan Stephens0c3141e2008-04-15 00:22:02 -0700956 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100957 }
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500958
Erik Hugnef2f80362015-03-19 09:02:19 +0100959 seq = &dest->addr.nameseq;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500960 if (dest->addrtype == TIPC_ADDR_MCAST)
961 return tipc_sendmcast(sock, seq, m, dlen, timeout);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500962
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500963 if (dest->addrtype == TIPC_ADDR_NAME) {
964 type = dest->addr.name.name.type;
965 inst = dest->addr.name.name.instance;
966 domain = dest->addr.name.domain;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500967 dnode = domain;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500968 msg_set_type(hdr, TIPC_NAMED_MSG);
969 msg_set_hdr_sz(hdr, NAMED_H_SIZE);
970 msg_set_nametype(hdr, type);
971 msg_set_nameinst(hdr, inst);
972 msg_set_lookup_scope(hdr, tipc_addr_scope(domain));
Ying Xue4ac1c8d2015-01-09 15:27:09 +0800973 dport = tipc_nametbl_translate(net, type, inst, &dnode);
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500974 msg_set_destnode(hdr, dnode);
975 msg_set_destport(hdr, dport);
Ying Xue39a0295f2015-03-02 15:37:47 +0800976 if (unlikely(!dport && !dnode))
977 return -EHOSTUNREACH;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500978
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500979 } else if (dest->addrtype == TIPC_ADDR_ID) {
980 dnode = dest->addr.id.node;
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500981 msg_set_type(hdr, TIPC_DIRECT_MSG);
982 msg_set_lookup_scope(hdr, 0);
983 msg_set_destnode(hdr, dnode);
984 msg_set_destport(hdr, dest->addr.id.ref);
985 msg_set_hdr_sz(hdr, BASIC_H_SIZE);
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500986 }
987
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500988 /* Block or return if destination link is congested */
989 rc = tipc_wait_for_cond(sock, &timeout, !u32_find(clinks, dnode));
990 if (unlikely(rc))
Ying Xue39a0295f2015-03-02 15:37:47 +0800991 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500992
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500993 skb_queue_head_init(&pkts);
994 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
995 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
996 if (unlikely(rc != dlen))
997 return rc;
Jon Paul Maloye2dafe82014-06-25 20:41:37 -0500998
Jon Paul Maloy365ad352017-01-03 10:55:11 -0500999 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1000 if (unlikely(rc == -ELINKCONG)) {
1001 u32_push(clinks, dnode);
1002 tsk->cong_link_cnt++;
1003 rc = 0;
1004 }
1005
1006 if (unlikely(syn && !rc))
1007 tipc_set_sk_state(sk, TIPC_CONNECTING);
1008
1009 return rc ? rc : dlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010}
1011
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001012/**
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001013 * tipc_sendstream - send stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014 * @sock: socket structure
1015 * @m: data to send
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001016 * @dsz: total length of data to be transmitted
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001017 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 * Used for SOCK_STREAM data.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001019 *
1020 * Returns the number of bytes sent on success (or partial success),
Allan Stephens1303e8f2006-06-25 23:46:50 -07001021 * or errno if no data sent
Per Lidenb97bf3f2006-01-02 19:04:38 +01001022 */
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001023static int tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001024{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001025 struct sock *sk = sock->sk;
Ying Xue39a0295f2015-03-02 15:37:47 +08001026 int ret;
1027
1028 lock_sock(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001029 ret = __tipc_sendstream(sock, m, dsz);
Ying Xue39a0295f2015-03-02 15:37:47 +08001030 release_sock(sk);
1031
1032 return ret;
1033}
1034
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001035static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dlen)
Ying Xue39a0295f2015-03-02 15:37:47 +08001036{
1037 struct sock *sk = sock->sk;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001038 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001039 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1040 struct tipc_sock *tsk = tipc_sk(sk);
1041 struct tipc_msg *hdr = &tsk->phdr;
1042 struct net *net = sock_net(sk);
1043 struct sk_buff_head pkts;
1044 u32 dnode = tsk_peer_node(tsk);
1045 int send, sent = 0;
1046 int rc = 0;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001047
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001048 skb_queue_head_init(&pkts);
1049
1050 if (unlikely(dlen > INT_MAX))
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001051 return -EMSGSIZE;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001052
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001053 /* Handle implicit connection setup */
1054 if (unlikely(dest)) {
1055 rc = __tipc_sendmsg(sock, m, dlen);
1056 if (dlen && (dlen == rc))
1057 tsk->snt_unacked = tsk_inc(tsk, dlen + msg_hdr_sz(hdr));
1058 return rc;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001059 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001061 do {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001062 rc = tipc_wait_for_cond(sock, &timeout,
1063 (!tsk->cong_link_cnt &&
Jon Paul Maloy8c44e1a2017-01-03 10:55:09 -05001064 !tsk_conn_cong(tsk) &&
1065 tipc_sk_connected(sk)));
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001066 if (unlikely(rc))
1067 break;
Ying Xue39a0295f2015-03-02 15:37:47 +08001068
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001069 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
1070 rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
1071 if (unlikely(rc != send))
1072 break;
1073
1074 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1075 if (unlikely(rc == -ELINKCONG)) {
1076 tsk->cong_link_cnt = 1;
1077 rc = 0;
1078 }
1079 if (likely(!rc)) {
1080 tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
1081 sent += send;
1082 }
1083 } while (sent < dlen && !rc);
1084
1085 return rc ? rc : sent;
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001086}
1087
1088/**
1089 * tipc_send_packet - send a connection-oriented message
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001090 * @sock: socket structure
1091 * @m: message to send
1092 * @dsz: length of data to be transmitted
1093 *
1094 * Used for SOCK_SEQPACKET messages.
1095 *
1096 * Returns the number of bytes sent on success, or errno otherwise
1097 */
Ying Xue1b784142015-03-02 15:37:48 +08001098static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
Jon Paul Maloy4ccfe5e2014-06-25 20:41:38 -05001099{
1100 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1101 return -EMSGSIZE;
1102
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001103 return tipc_sendstream(sock, m, dsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001104}
1105
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001106/* tipc_sk_finish_conn - complete the setup of a connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01001107 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001108static void tipc_sk_finish_conn(struct tipc_sock *tsk, u32 peer_port,
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001109 u32 peer_node)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001110{
Ying Xue3721e9c2015-01-13 17:07:48 +08001111 struct sock *sk = &tsk->sk;
1112 struct net *net = sock_net(sk);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001113 struct tipc_msg *msg = &tsk->phdr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001114
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001115 msg_set_destnode(msg, peer_node);
1116 msg_set_destport(msg, peer_port);
1117 msg_set_type(msg, TIPC_CONN_MSG);
1118 msg_set_lookup_scope(msg, 0);
1119 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Jon Paul Maloyf9fef182014-03-12 11:31:08 -04001120
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01001121 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01001122 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
Ying Xuef2f98002015-01-09 15:27:05 +08001123 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1124 tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
Jon Paul Maloy60020e12016-05-02 11:58:46 -04001125 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001126 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
1127 return;
1128
1129 /* Fall back to message based flow control */
1130 tsk->rcv_win = FLOWCTL_MSG_WIN;
1131 tsk->snd_win = FLOWCTL_MSG_WIN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001132}
1133
1134/**
1135 * set_orig_addr - capture sender's address for received message
1136 * @m: descriptor for message info
1137 * @msg: received message header
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001138 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001139 * Note: Address is not captured if not requested by receiver.
1140 */
Sam Ravnborg05790c62006-03-20 22:37:04 -08001141static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001142{
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001143 DECLARE_SOCKADDR(struct sockaddr_tipc *, addr, m->msg_name);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001144
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001145 if (addr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001146 addr->family = AF_TIPC;
1147 addr->addrtype = TIPC_ADDR_ID;
Mathias Krause60085c32013-04-07 01:52:00 +00001148 memset(&addr->addr, 0, sizeof(addr->addr));
Per Lidenb97bf3f2006-01-02 19:04:38 +01001149 addr->addr.id.ref = msg_origport(msg);
1150 addr->addr.id.node = msg_orignode(msg);
Allan Stephens0e659672010-12-31 18:59:32 +00001151 addr->addr.name.domain = 0; /* could leave uninitialized */
1152 addr->scope = 0; /* could leave uninitialized */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153 m->msg_namelen = sizeof(struct sockaddr_tipc);
1154 }
1155}
1156
1157/**
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001158 * tipc_sk_anc_data_recv - optionally capture ancillary data for received message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001159 * @m: descriptor for message info
1160 * @msg: received message header
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001161 * @tsk: TIPC port associated with message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001162 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001163 * Note: Ancillary data is not captured if not requested by receiver.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001164 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001165 * Returns 0 if successful, otherwise errno
1166 */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001167static int tipc_sk_anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
1168 struct tipc_sock *tsk)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169{
1170 u32 anc_data[3];
1171 u32 err;
1172 u32 dest_type;
Allan Stephens3546c752006-06-25 23:45:24 -07001173 int has_name;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 int res;
1175
1176 if (likely(m->msg_controllen == 0))
1177 return 0;
1178
1179 /* Optionally capture errored message object(s) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 err = msg ? msg_errcode(msg) : 0;
1181 if (unlikely(err)) {
1182 anc_data[0] = err;
1183 anc_data[1] = msg_data_sz(msg);
Allan Stephens2db99832010-12-31 18:59:33 +00001184 res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data);
1185 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 return res;
Allan Stephens2db99832010-12-31 18:59:33 +00001187 if (anc_data[1]) {
1188 res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
1189 msg_data(msg));
1190 if (res)
1191 return res;
1192 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001193 }
1194
1195 /* Optionally capture message destination object */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
1197 switch (dest_type) {
1198 case TIPC_NAMED_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001199 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001200 anc_data[0] = msg_nametype(msg);
1201 anc_data[1] = msg_namelower(msg);
1202 anc_data[2] = msg_namelower(msg);
1203 break;
1204 case TIPC_MCAST_MSG:
Allan Stephens3546c752006-06-25 23:45:24 -07001205 has_name = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206 anc_data[0] = msg_nametype(msg);
1207 anc_data[1] = msg_namelower(msg);
1208 anc_data[2] = msg_nameupper(msg);
1209 break;
1210 case TIPC_CONN_MSG:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001211 has_name = (tsk->conn_type != 0);
1212 anc_data[0] = tsk->conn_type;
1213 anc_data[1] = tsk->conn_instance;
1214 anc_data[2] = tsk->conn_instance;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001215 break;
1216 default:
Allan Stephens3546c752006-06-25 23:45:24 -07001217 has_name = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001218 }
Allan Stephens2db99832010-12-31 18:59:33 +00001219 if (has_name) {
1220 res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data);
1221 if (res)
1222 return res;
1223 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001224
1225 return 0;
1226}
1227
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001228static void tipc_sk_send_ack(struct tipc_sock *tsk)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001229{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001230 struct sock *sk = &tsk->sk;
1231 struct net *net = sock_net(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08001232 struct sk_buff *skb = NULL;
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001233 struct tipc_msg *msg;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001234 u32 peer_port = tsk_peer_port(tsk);
1235 u32 dnode = tsk_peer_node(tsk);
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001236
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01001237 if (!tipc_sk_connected(sk))
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001238 return;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05001239 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
1240 dnode, tsk_own_node(tsk), peer_port,
1241 tsk->portid, TIPC_OK);
Ying Xuea6ca1092014-11-26 11:41:55 +08001242 if (!skb)
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001243 return;
Ying Xuea6ca1092014-11-26 11:41:55 +08001244 msg = buf_msg(skb);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001245 msg_set_conn_ack(msg, tsk->rcv_unacked);
1246 tsk->rcv_unacked = 0;
1247
1248 /* Adjust to and advertize the correct window limit */
1249 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL) {
1250 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
1251 msg_set_adv_win(msg, tsk->rcv_win);
1252 }
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001253 tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
Jon Paul Maloy739f5e42014-08-22 18:09:12 -04001254}
1255
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001256static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001257{
1258 struct sock *sk = sock->sk;
1259 DEFINE_WAIT(wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001260 long timeo = *timeop;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001261 int err;
1262
1263 for (;;) {
1264 prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01001265 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01001266 if (sk->sk_shutdown & RCV_SHUTDOWN) {
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001267 err = -ENOTCONN;
1268 break;
1269 }
1270 release_sock(sk);
1271 timeo = schedule_timeout(timeo);
1272 lock_sock(sk);
1273 }
1274 err = 0;
1275 if (!skb_queue_empty(&sk->sk_receive_queue))
1276 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001277 err = -EAGAIN;
1278 if (!timeo)
1279 break;
Erik Hugne143fe222015-03-09 10:43:42 +01001280 err = sock_intr_errno(timeo);
1281 if (signal_pending(current))
1282 break;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001283 }
1284 finish_wait(sk_sleep(sk), &wait);
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001285 *timeop = timeo;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001286 return err;
1287}
1288
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001289/**
Ying Xue247f0f32014-02-18 16:06:46 +08001290 * tipc_recvmsg - receive packet-oriented message
Per Lidenb97bf3f2006-01-02 19:04:38 +01001291 * @m: descriptor for message info
1292 * @buf_len: total size of user buffer area
1293 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001294 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1296 * If the complete message doesn't fit in user area, truncate it.
1297 *
1298 * Returns size of returned message data, errno otherwise
1299 */
Ying Xue1b784142015-03-02 15:37:48 +08001300static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1301 int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001302{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001303 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001304 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001305 struct sk_buff *buf;
1306 struct tipc_msg *msg;
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +01001307 bool is_connectionless = tipc_sk_type_connectionless(sk);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001308 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001309 unsigned int sz;
1310 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001311 int res, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001312
Allan Stephens0c3141e2008-04-15 00:22:02 -07001313 /* Catch invalid receive requests */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 if (unlikely(!buf_len))
1315 return -EINVAL;
1316
Allan Stephens0c3141e2008-04-15 00:22:02 -07001317 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001318
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001319 if (!is_connectionless && unlikely(sk->sk_state == TIPC_OPEN)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001320 res = -ENOTCONN;
1321 goto exit;
1322 }
1323
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001324 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001325restart:
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326
Allan Stephens0c3141e2008-04-15 00:22:02 -07001327 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001328 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001329 if (res)
1330 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001331
1332 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001333 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334 msg = buf_msg(buf);
1335 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001336 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001337 err = msg_errcode(msg);
1338
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001340 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001341 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342 goto restart;
1343 }
1344
1345 /* Capture sender's address (optional) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001346 set_orig_addr(m, msg);
1347
1348 /* Capture ancillary data (optional) */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001349 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001350 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351 goto exit;
1352
1353 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001354 if (!err) {
1355 if (unlikely(buf_len < sz)) {
1356 sz = buf_len;
1357 m->msg_flags |= MSG_TRUNC;
1358 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001359 res = skb_copy_datagram_msg(buf, hlen, m, sz);
Allan Stephens0232fd02011-02-21 09:45:40 -05001360 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001361 goto exit;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362 res = sz;
1363 } else {
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +01001364 if (is_connectionless || err == TIPC_CONN_SHUTDOWN ||
1365 m->msg_control)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001366 res = 0;
1367 else
1368 res = -ECONNRESET;
1369 }
1370
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001371 if (unlikely(flags & MSG_PEEK))
1372 goto exit;
1373
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +01001374 if (likely(!is_connectionless)) {
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001375 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1376 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1377 tipc_sk_send_ack(tsk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001378 }
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001379 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001380exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001381 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001382 return res;
1383}
1384
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001385/**
Ying Xue247f0f32014-02-18 16:06:46 +08001386 * tipc_recv_stream - receive stream-oriented data
Per Lidenb97bf3f2006-01-02 19:04:38 +01001387 * @m: descriptor for message info
1388 * @buf_len: total size of user buffer area
1389 * @flags: receive flags
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001390 *
1391 * Used for SOCK_STREAM messages only. If not enough data is available
Per Lidenb97bf3f2006-01-02 19:04:38 +01001392 * will optionally wait for more; never truncates data.
1393 *
1394 * Returns size of returned message data, errno otherwise
1395 */
Ying Xue1b784142015-03-02 15:37:48 +08001396static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1397 size_t buf_len, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001398{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001399 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001400 struct tipc_sock *tsk = tipc_sk(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001401 struct sk_buff *buf;
1402 struct tipc_msg *msg;
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001403 long timeo;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001404 unsigned int sz;
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001405 int target;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001406 int sz_copied = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001407 u32 err;
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001408 int res = 0, hlen;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001409
1410 /* Catch invalid receive attempts */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001411 if (unlikely(!buf_len))
1412 return -EINVAL;
1413
Allan Stephens0c3141e2008-04-15 00:22:02 -07001414 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001415
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001416 if (unlikely(sk->sk_state == TIPC_OPEN)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001417 res = -ENOTCONN;
1418 goto exit;
1419 }
1420
Florian Westphal3720d402010-08-17 11:00:04 +00001421 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001422 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
Paul Gortmaker617d3c72012-04-30 15:29:02 -04001423
Allan Stephens0c3141e2008-04-15 00:22:02 -07001424restart:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001425 /* Look for a message in receive queue; wait if necessary */
Arnaldo Carvalho de Melo85d3fc92014-05-23 15:55:12 -04001426 res = tipc_wait_for_rcvmsg(sock, &timeo);
Ying Xue9bbb4ec2014-01-17 09:50:07 +08001427 if (res)
1428 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001429
1430 /* Look at first message in receive queue */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001431 buf = skb_peek(&sk->sk_receive_queue);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001432 msg = buf_msg(buf);
1433 sz = msg_data_sz(msg);
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001434 hlen = msg_hdr_sz(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001435 err = msg_errcode(msg);
1436
1437 /* Discard an empty non-errored message & try again */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001438 if ((!sz) && (!err)) {
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04001439 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001440 goto restart;
1441 }
1442
1443 /* Optionally capture sender's address & ancillary data of first msg */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444 if (sz_copied == 0) {
1445 set_orig_addr(m, msg);
Jon Paul Maloy301bae52014-08-22 18:09:20 -04001446 res = tipc_sk_anc_data_recv(m, msg, tsk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001447 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448 goto exit;
1449 }
1450
1451 /* Capture message data (if valid) & compute return value (always) */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001452 if (!err) {
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001453 u32 offset = TIPC_SKB_CB(buf)->bytes_read;
1454 u32 needed;
1455 int sz_to_copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001456
Allan Stephens0232fd02011-02-21 09:45:40 -05001457 sz -= offset;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001458 needed = (buf_len - sz_copied);
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001459 sz_to_copy = min(sz, needed);
Allan Stephens0232fd02011-02-21 09:45:40 -05001460
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001461 res = skb_copy_datagram_msg(buf, hlen + offset, m, sz_to_copy);
Allan Stephens0232fd02011-02-21 09:45:40 -05001462 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001463 goto exit;
Allan Stephens0232fd02011-02-21 09:45:40 -05001464
Per Lidenb97bf3f2006-01-02 19:04:38 +01001465 sz_copied += sz_to_copy;
1466
1467 if (sz_to_copy < sz) {
1468 if (!(flags & MSG_PEEK))
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001469 TIPC_SKB_CB(buf)->bytes_read =
1470 offset + sz_to_copy;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001471 goto exit;
1472 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001473 } else {
1474 if (sz_copied != 0)
1475 goto exit; /* can't add error msg to valid data */
1476
1477 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1478 res = 0;
1479 else
1480 res = -ECONNRESET;
1481 }
1482
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001483 if (unlikely(flags & MSG_PEEK))
1484 goto exit;
1485
1486 tsk->rcv_unacked += tsk_inc(tsk, hlen + sz);
1487 if (unlikely(tsk->rcv_unacked >= (tsk->rcv_win / 4)))
1488 tipc_sk_send_ack(tsk);
1489 tsk_advance_rx_queue(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001490
1491 /* Loop around if more data is required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001492 if ((sz_copied < buf_len) && /* didn't get all requested data */
1493 (!skb_queue_empty(&sk->sk_receive_queue) ||
Florian Westphal3720d402010-08-17 11:00:04 +00001494 (sz_copied < target)) && /* and more is ready or required */
Joe Perchesf64f9e72009-11-29 16:55:45 -08001495 (!err)) /* and haven't reached a FIN */
Per Lidenb97bf3f2006-01-02 19:04:38 +01001496 goto restart;
1497
1498exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07001499 release_sock(sk);
Allan Stephensa3b0a5a2006-06-25 23:48:22 -07001500 return sz_copied ? sz_copied : res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001501}
1502
1503/**
Ying Xuef288bef2012-08-21 11:16:57 +08001504 * tipc_write_space - wake up thread if port congestion is released
1505 * @sk: socket
1506 */
1507static void tipc_write_space(struct sock *sk)
1508{
1509 struct socket_wq *wq;
1510
1511 rcu_read_lock();
1512 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001513 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001514 wake_up_interruptible_sync_poll(&wq->wait, POLLOUT |
1515 POLLWRNORM | POLLWRBAND);
1516 rcu_read_unlock();
1517}
1518
1519/**
1520 * tipc_data_ready - wake up threads to indicate messages have been received
1521 * @sk: socket
1522 * @len: the length of messages
1523 */
David S. Miller676d2362014-04-11 16:15:36 -04001524static void tipc_data_ready(struct sock *sk)
Ying Xuef288bef2012-08-21 11:16:57 +08001525{
1526 struct socket_wq *wq;
1527
1528 rcu_read_lock();
1529 wq = rcu_dereference(sk->sk_wq);
Herbert Xu1ce0bf52015-11-26 13:55:39 +08001530 if (skwq_has_sleeper(wq))
Ying Xuef288bef2012-08-21 11:16:57 +08001531 wake_up_interruptible_sync_poll(&wq->wait, POLLIN |
1532 POLLRDNORM | POLLRDBAND);
1533 rcu_read_unlock();
1534}
1535
Ying Xuef4195d12015-11-22 15:46:05 +08001536static void tipc_sock_destruct(struct sock *sk)
1537{
1538 __skb_queue_purge(&sk->sk_receive_queue);
1539}
1540
Ying Xuef288bef2012-08-21 11:16:57 +08001541/**
Ying Xue7e6c1312012-11-29 18:39:14 -05001542 * filter_connect - Handle all incoming messages for a connection-based socket
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001543 * @tsk: TIPC socket
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001544 * @skb: pointer to message buffer. Set to NULL if buffer is consumed
Ying Xue7e6c1312012-11-29 18:39:14 -05001545 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001546 * Returns true if everything ok, false otherwise
Ying Xue7e6c1312012-11-29 18:39:14 -05001547 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001548static bool filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
Ying Xue7e6c1312012-11-29 18:39:14 -05001549{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001550 struct sock *sk = &tsk->sk;
Ying Xuef2f98002015-01-09 15:27:05 +08001551 struct net *net = sock_net(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001552 struct tipc_msg *hdr = buf_msg(skb);
Ying Xue7e6c1312012-11-29 18:39:14 -05001553
Jon Paul Maloycda36962015-07-22 10:11:20 -04001554 if (unlikely(msg_mcast(hdr)))
1555 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001556
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001557 switch (sk->sk_state) {
1558 case TIPC_CONNECTING:
Ying Xue7e6c1312012-11-29 18:39:14 -05001559 /* Accept only ACK or NACK message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001560 if (unlikely(!msg_connected(hdr)))
1561 return false;
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001562
Jon Paul Maloycda36962015-07-22 10:11:20 -04001563 if (unlikely(msg_errcode(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001564 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugne2c8d8512013-08-28 09:29:58 +02001565 sk->sk_err = ECONNREFUSED;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001566 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001567 }
1568
Jon Paul Maloycda36962015-07-22 10:11:20 -04001569 if (unlikely(!msg_isdata(hdr))) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001570 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001571 sk->sk_err = EINVAL;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001572 return true;
Ying Xue584d24b2012-11-29 18:51:19 -05001573 }
1574
Jon Paul Maloycda36962015-07-22 10:11:20 -04001575 tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
1576 msg_set_importance(&tsk->phdr, msg_importance(hdr));
Jon Paul Maloydadebc02014-08-22 18:09:11 -04001577
Jon Paul Maloycda36962015-07-22 10:11:20 -04001578 /* If 'ACK+' message, add to socket receive queue */
1579 if (msg_data_sz(hdr))
1580 return true;
1581
1582 /* If empty 'ACK-' message, wake up sleeping connect() */
1583 if (waitqueue_active(sk_sleep(sk)))
1584 wake_up_interruptible(sk_sleep(sk));
1585
1586 /* 'ACK-' message is neither accepted nor rejected: */
1587 msg_set_dest_droppable(hdr, 1);
1588 return false;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001589
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001590 case TIPC_OPEN:
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01001591 case TIPC_DISCONNECTING:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001592 break;
1593 case TIPC_LISTEN:
Ying Xue7e6c1312012-11-29 18:39:14 -05001594 /* Accept only SYN message */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001595 if (!msg_connected(hdr) && !(msg_errcode(hdr)))
1596 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001597 break;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001598 case TIPC_ESTABLISHED:
1599 /* Accept only connection-based messages sent by peer */
1600 if (unlikely(!tsk_peer_msg(tsk, hdr)))
1601 return false;
1602
1603 if (unlikely(msg_errcode(hdr))) {
1604 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
1605 /* Let timer expire on it's own */
1606 tipc_node_remove_conn(net, tsk_peer_node(tsk),
1607 tsk->portid);
1608 sk->sk_state_change(sk);
1609 }
1610 return true;
Ying Xue7e6c1312012-11-29 18:39:14 -05001611 default:
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001612 pr_err("Unknown sk_state %u\n", sk->sk_state);
Ying Xue7e6c1312012-11-29 18:39:14 -05001613 }
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001614
Jon Paul Maloycda36962015-07-22 10:11:20 -04001615 return false;
Ying Xue7e6c1312012-11-29 18:39:14 -05001616}
1617
1618/**
Ying Xueaba79f32013-01-20 23:30:09 +01001619 * rcvbuf_limit - get proper overload limit of socket receive queue
1620 * @sk: socket
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001621 * @skb: message
Ying Xueaba79f32013-01-20 23:30:09 +01001622 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001623 * For connection oriented messages, irrespective of importance,
1624 * default queue limit is 2 MB.
Ying Xueaba79f32013-01-20 23:30:09 +01001625 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001626 * For connectionless messages, queue limits are based on message
1627 * importance as follows:
Ying Xueaba79f32013-01-20 23:30:09 +01001628 *
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001629 * TIPC_LOW_IMPORTANCE (2 MB)
1630 * TIPC_MEDIUM_IMPORTANCE (4 MB)
1631 * TIPC_HIGH_IMPORTANCE (8 MB)
1632 * TIPC_CRITICAL_IMPORTANCE (16 MB)
Ying Xueaba79f32013-01-20 23:30:09 +01001633 *
1634 * Returns overload limit according to corresponding message importance
1635 */
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001636static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *skb)
Ying Xueaba79f32013-01-20 23:30:09 +01001637{
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001638 struct tipc_sock *tsk = tipc_sk(sk);
1639 struct tipc_msg *hdr = buf_msg(skb);
Ying Xueaba79f32013-01-20 23:30:09 +01001640
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001641 if (unlikely(!msg_connected(hdr)))
1642 return sk->sk_rcvbuf << msg_importance(hdr);
wangweidong0cee6bb2013-12-12 09:36:39 +08001643
Jon Paul Maloy10724cc2016-05-02 11:58:47 -04001644 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
1645 return sk->sk_rcvbuf;
1646
1647 return FLOWCTL_MSG_LIM;
Ying Xueaba79f32013-01-20 23:30:09 +01001648}
1649
1650/**
Allan Stephens0c3141e2008-04-15 00:22:02 -07001651 * filter_rcv - validate incoming message
1652 * @sk: socket
Jon Paul Maloycda36962015-07-22 10:11:20 -04001653 * @skb: pointer to message.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001654 *
Allan Stephens0c3141e2008-04-15 00:22:02 -07001655 * Enqueues message on receive queue if acceptable; optionally handles
1656 * disconnect indication for a connected socket.
1657 *
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001658 * Called with socket lock already taken
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001659 *
Jon Paul Maloycda36962015-07-22 10:11:20 -04001660 * Returns true if message was added to socket receive queue, otherwise false
Per Lidenb97bf3f2006-01-02 19:04:38 +01001661 */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001662static bool filter_rcv(struct sock *sk, struct sk_buff *skb,
1663 struct sk_buff_head *xmitq)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001664{
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04001665 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001666 struct tipc_msg *hdr = buf_msg(skb);
1667 unsigned int limit = rcvbuf_limit(sk, skb);
1668 int err = TIPC_OK;
1669 int usr = msg_user(hdr);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001670 u32 onode;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001671
Jon Paul Maloycda36962015-07-22 10:11:20 -04001672 if (unlikely(msg_user(hdr) == CONN_MANAGER)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001673 tipc_sk_proto_rcv(tsk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001674 return false;
Jon Paul Maloy1186adf2015-02-05 08:36:37 -05001675 }
Jon Paul Maloyec8a2e52014-06-25 20:41:40 -05001676
Jon Paul Maloycda36962015-07-22 10:11:20 -04001677 if (unlikely(usr == SOCK_WAKEUP)) {
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001678 onode = msg_orignode(hdr);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001679 kfree_skb(skb);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05001680 u32_del(&tsk->cong_links, onode);
1681 tsk->cong_link_cnt--;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001682 sk->sk_write_space(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001683 return false;
Jon Paul Maloy50100a52014-08-22 18:09:07 -04001684 }
1685
Jon Paul Maloycda36962015-07-22 10:11:20 -04001686 /* Drop if illegal message type */
1687 if (unlikely(msg_type(hdr) > TIPC_DIRECT_MSG)) {
1688 kfree_skb(skb);
1689 return false;
1690 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001691
Jon Paul Maloycda36962015-07-22 10:11:20 -04001692 /* Reject if wrong message type for current socket state */
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +01001693 if (tipc_sk_type_connectionless(sk)) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001694 if (msg_connected(hdr)) {
1695 err = TIPC_ERR_NO_PORT;
1696 goto reject;
1697 }
1698 } else if (unlikely(!filter_connect(tsk, skb))) {
1699 err = TIPC_ERR_NO_PORT;
1700 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701 }
1702
1703 /* Reject message if there isn't room to queue it */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001704 if (unlikely(sk_rmem_alloc_get(sk) + skb->truesize >= limit)) {
1705 err = TIPC_ERR_OVERLOAD;
1706 goto reject;
1707 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001708
Ying Xueaba79f32013-01-20 23:30:09 +01001709 /* Enqueue message */
Parthasarathy Bhuvaraganba8aebe2016-11-01 14:02:37 +01001710 TIPC_SKB_CB(skb)->bytes_read = 0;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001711 __skb_queue_tail(&sk->sk_receive_queue, skb);
1712 skb_set_owner_r(skb, sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001713
David S. Miller676d2362014-04-11 16:15:36 -04001714 sk->sk_data_ready(sk);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001715 return true;
1716
1717reject:
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001718 if (tipc_msg_reverse(tsk_own_node(tsk), &skb, err))
1719 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001720 return false;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001721}
1722
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001723/**
Jon Paul Maloy4f4482d2014-05-14 05:39:09 -04001724 * tipc_backlog_rcv - handle incoming message from backlog queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001725 * @sk: socket
Ying Xuea6ca1092014-11-26 11:41:55 +08001726 * @skb: message
Allan Stephens0c3141e2008-04-15 00:22:02 -07001727 *
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001728 * Caller must hold socket lock
Allan Stephens0c3141e2008-04-15 00:22:02 -07001729 *
1730 * Returns 0
1731 */
Ying Xuea6ca1092014-11-26 11:41:55 +08001732static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001733{
Jon Paul Maloycda36962015-07-22 10:11:20 -04001734 unsigned int truesize = skb->truesize;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001735 struct sk_buff_head xmitq;
1736 u32 dnode, selector;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001737
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001738 __skb_queue_head_init(&xmitq);
1739
1740 if (likely(filter_rcv(sk, skb, &xmitq))) {
Jon Paul Maloycda36962015-07-22 10:11:20 -04001741 atomic_add(truesize, &tipc_sk(sk)->dupl_rcvcnt);
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001742 return 0;
1743 }
1744
1745 if (skb_queue_empty(&xmitq))
1746 return 0;
1747
1748 /* Send response/rejected message */
1749 skb = __skb_dequeue(&xmitq);
1750 dnode = msg_destnode(buf_msg(skb));
1751 selector = msg_origport(buf_msg(skb));
1752 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001753 return 0;
1754}
1755
1756/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001757 * tipc_sk_enqueue - extract all buffers with destination 'dport' from
1758 * inputq and try adding them to socket or backlog queue
1759 * @inputq: list of incoming buffers with potentially different destinations
1760 * @sk: socket where the buffers should be enqueued
1761 * @dport: port number for the socket
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001762 *
1763 * Caller must hold socket lock
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001764 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001765static void tipc_sk_enqueue(struct sk_buff_head *inputq, struct sock *sk,
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001766 u32 dport, struct sk_buff_head *xmitq)
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001767{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001768 unsigned long time_limit = jiffies + 2;
1769 struct sk_buff *skb;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001770 unsigned int lim;
1771 atomic_t *dcnt;
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001772 u32 onode;
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001773
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001774 while (skb_queue_len(inputq)) {
Jon Paul Maloy51a00da2015-02-08 11:10:50 -05001775 if (unlikely(time_after_eq(jiffies, time_limit)))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001776 return;
1777
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001778 skb = tipc_skb_dequeue(inputq, dport);
1779 if (unlikely(!skb))
Jon Paul Maloycda36962015-07-22 10:11:20 -04001780 return;
1781
1782 /* Add message directly to receive queue if possible */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001783 if (!sock_owned_by_user(sk)) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001784 filter_rcv(sk, skb, xmitq);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001785 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001786 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001787
1788 /* Try backlog, compensating for double-counted bytes */
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001789 dcnt = &tipc_sk(sk)->dupl_rcvcnt;
Jon Paul Maloy7c8bcfb2016-05-02 11:58:45 -04001790 if (!sk->sk_backlog.len)
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001791 atomic_set(dcnt, 0);
1792 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
1793 if (likely(!sk_add_backlog(sk, skb, lim)))
1794 continue;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001795
1796 /* Overload => reject message back to sender */
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001797 onode = tipc_own_addr(sock_net(sk));
1798 if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
1799 __skb_queue_tail(xmitq, skb);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001800 break;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001801 }
Jon Paul Maloyd570d862015-02-05 08:36:38 -05001802}
1803
1804/**
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001805 * tipc_sk_rcv - handle a chain of incoming buffers
1806 * @inputq: buffer list containing the buffers
1807 * Consumes all buffers in list until inputq is empty
1808 * Note: may be called in multiple threads referring to the same queue
Allan Stephens0c3141e2008-04-15 00:22:02 -07001809 */
Jon Paul Maloycda36962015-07-22 10:11:20 -04001810void tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq)
Allan Stephens0c3141e2008-04-15 00:22:02 -07001811{
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001812 struct sk_buff_head xmitq;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001813 u32 dnode, dport = 0;
Erik Hugne9871b272015-04-23 09:37:39 -04001814 int err;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001815 struct tipc_sock *tsk;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001816 struct sock *sk;
Jon Paul Maloycda36962015-07-22 10:11:20 -04001817 struct sk_buff *skb;
Jon Paul Maloy9816f062014-05-14 05:39:15 -04001818
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001819 __skb_queue_head_init(&xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001820 while (skb_queue_len(inputq)) {
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001821 dport = tipc_skb_peek_port(inputq, dport);
1822 tsk = tipc_sk_lookup(net, dport);
Jon Paul Maloycda36962015-07-22 10:11:20 -04001823
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001824 if (likely(tsk)) {
1825 sk = &tsk->sk;
1826 if (likely(spin_trylock_bh(&sk->sk_lock.slock))) {
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001827 tipc_sk_enqueue(inputq, sk, dport, &xmitq);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001828 spin_unlock_bh(&sk->sk_lock.slock);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001829 }
Jon Paul Maloyf1d048f2016-06-17 06:35:57 -04001830 /* Send pending response/rejected messages, if any */
1831 while ((skb = __skb_dequeue(&xmitq))) {
1832 dnode = msg_destnode(buf_msg(skb));
1833 tipc_node_xmit_skb(net, skb, dnode, dport);
1834 }
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001835 sock_put(sk);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001836 continue;
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001837 }
Jon Paul Maloycda36962015-07-22 10:11:20 -04001838
1839 /* No destination socket => dequeue skb if still there */
1840 skb = tipc_skb_dequeue(inputq, dport);
1841 if (!skb)
1842 return;
1843
1844 /* Try secondary lookup if unresolved named message */
1845 err = TIPC_ERR_NO_PORT;
1846 if (tipc_msg_lookup_dest(net, skb, &err))
1847 goto xmit;
1848
1849 /* Prepare for message rejection */
1850 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001851 continue;
Jon Paul Maloye3a77562015-02-05 08:36:39 -05001852xmit:
Jon Paul Maloycda36962015-07-22 10:11:20 -04001853 dnode = msg_destnode(buf_msg(skb));
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04001854 tipc_node_xmit_skb(net, skb, dnode, dport);
Jon Paul Maloyc637c102015-02-05 08:36:41 -05001855 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001856}
1857
Ying Xue78eb3a52014-01-17 09:50:03 +08001858static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)
1859{
WANG Congd9dc8b02016-11-11 10:20:50 -08001860 DEFINE_WAIT_FUNC(wait, woken_wake_function);
Ying Xue78eb3a52014-01-17 09:50:03 +08001861 struct sock *sk = sock->sk;
Ying Xue78eb3a52014-01-17 09:50:03 +08001862 int done;
1863
1864 do {
1865 int err = sock_error(sk);
1866 if (err)
1867 return err;
1868 if (!*timeo_p)
1869 return -ETIMEDOUT;
1870 if (signal_pending(current))
1871 return sock_intr_errno(*timeo_p);
1872
WANG Congd9dc8b02016-11-11 10:20:50 -08001873 add_wait_queue(sk_sleep(sk), &wait);
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001874 done = sk_wait_event(sk, timeo_p,
WANG Congd9dc8b02016-11-11 10:20:50 -08001875 sk->sk_state != TIPC_CONNECTING, &wait);
1876 remove_wait_queue(sk_sleep(sk), &wait);
Ying Xue78eb3a52014-01-17 09:50:03 +08001877 } while (!done);
1878 return 0;
1879}
1880
Per Lidenb97bf3f2006-01-02 19:04:38 +01001881/**
Ying Xue247f0f32014-02-18 16:06:46 +08001882 * tipc_connect - establish a connection to another TIPC port
Per Lidenb97bf3f2006-01-02 19:04:38 +01001883 * @sock: socket structure
1884 * @dest: socket address for destination port
1885 * @destlen: size of socket address data structure
Allan Stephens0c3141e2008-04-15 00:22:02 -07001886 * @flags: file-related flags associated with socket
Per Lidenb97bf3f2006-01-02 19:04:38 +01001887 *
1888 * Returns 0 on success, errno otherwise
1889 */
Ying Xue247f0f32014-02-18 16:06:46 +08001890static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1891 int destlen, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001892{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001893 struct sock *sk = sock->sk;
Erik Hugnef2f80362015-03-19 09:02:19 +01001894 struct tipc_sock *tsk = tipc_sk(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001895 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1896 struct msghdr m = {NULL,};
Erik Hugnef2f80362015-03-19 09:02:19 +01001897 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001898 int previous;
Erik Hugnef2f80362015-03-19 09:02:19 +01001899 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001900
Allan Stephens0c3141e2008-04-15 00:22:02 -07001901 lock_sock(sk);
1902
Erik Hugnef2f80362015-03-19 09:02:19 +01001903 /* DGRAM/RDM connect(), just save the destaddr */
Parthasarathy Bhuvaraganc752023a2016-11-01 14:02:42 +01001904 if (tipc_sk_type_connectionless(sk)) {
Erik Hugnef2f80362015-03-19 09:02:19 +01001905 if (dst->family == AF_UNSPEC) {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001906 memset(&tsk->peer, 0, sizeof(struct sockaddr_tipc));
Sasha Levin610600c2015-03-23 15:30:00 -04001907 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1908 res = -EINVAL;
Erik Hugnef2f80362015-03-19 09:02:19 +01001909 } else {
Parthasarathy Bhuvaraganaeda16b2016-11-01 14:02:38 +01001910 memcpy(&tsk->peer, dest, destlen);
Erik Hugnef2f80362015-03-19 09:02:19 +01001911 }
Allan Stephens0c3141e2008-04-15 00:22:02 -07001912 goto exit;
1913 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01001914
Allan Stephensb89741a2008-04-15 00:20:37 -07001915 /*
1916 * Reject connection attempt using multicast address
1917 *
1918 * Note: send_msg() validates the rest of the address fields,
1919 * so there's no need to do it here
1920 */
Allan Stephens0c3141e2008-04-15 00:22:02 -07001921 if (dst->addrtype == TIPC_ADDR_MCAST) {
1922 res = -EINVAL;
1923 goto exit;
1924 }
1925
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001926 previous = sk->sk_state;
Parthasarathy Bhuvaragan438adca2016-11-01 14:02:45 +01001927
1928 switch (sk->sk_state) {
1929 case TIPC_OPEN:
Ying Xue584d24b2012-11-29 18:51:19 -05001930 /* Send a 'SYN-' to destination */
1931 m.msg_name = dest;
1932 m.msg_namelen = destlen;
1933
1934 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1935 * indicate send_msg() is never blocked.
1936 */
1937 if (!timeout)
1938 m.msg_flags = MSG_DONTWAIT;
1939
Ying Xue39a0295f2015-03-02 15:37:47 +08001940 res = __tipc_sendmsg(sock, &m, 0);
Ying Xue584d24b2012-11-29 18:51:19 -05001941 if ((res < 0) && (res != -EWOULDBLOCK))
1942 goto exit;
1943
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001944 /* Just entered TIPC_CONNECTING state; the only
Ying Xue584d24b2012-11-29 18:51:19 -05001945 * difference is that return value in non-blocking
1946 * case is EINPROGRESS, rather than EALREADY.
1947 */
1948 res = -EINPROGRESS;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001949 /* fall thru' */
1950 case TIPC_CONNECTING:
1951 if (!timeout) {
1952 if (previous == TIPC_CONNECTING)
1953 res = -EALREADY;
Ying Xue78eb3a52014-01-17 09:50:03 +08001954 goto exit;
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001955 }
Ying Xue78eb3a52014-01-17 09:50:03 +08001956 timeout = msecs_to_jiffies(timeout);
1957 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1958 res = tipc_wait_for_connect(sock, &timeout);
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001959 break;
1960 case TIPC_ESTABLISHED:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001961 res = -EISCONN;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001962 break;
1963 default:
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001964 res = -EINVAL;
Parthasarathy Bhuvaraganf40acba2016-11-01 14:02:49 +01001965 }
Parthasarathy Bhuvaragan99a20882016-11-01 14:02:48 +01001966
Allan Stephens0c3141e2008-04-15 00:22:02 -07001967exit:
1968 release_sock(sk);
Allan Stephensb89741a2008-04-15 00:20:37 -07001969 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001970}
1971
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001972/**
Ying Xue247f0f32014-02-18 16:06:46 +08001973 * tipc_listen - allow socket to listen for incoming connections
Per Lidenb97bf3f2006-01-02 19:04:38 +01001974 * @sock: socket structure
1975 * @len: (unused)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001976 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01001977 * Returns 0 on success, errno otherwise
1978 */
Ying Xue247f0f32014-02-18 16:06:46 +08001979static int tipc_listen(struct socket *sock, int len)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001980{
Allan Stephens0c3141e2008-04-15 00:22:02 -07001981 struct sock *sk = sock->sk;
1982 int res;
1983
1984 lock_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001985 res = tipc_set_sk_state(sk, TIPC_LISTEN);
Allan Stephens0c3141e2008-04-15 00:22:02 -07001986 release_sock(sk);
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01001987
Allan Stephens0c3141e2008-04-15 00:22:02 -07001988 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001989}
1990
Ying Xue6398e232014-01-17 09:50:04 +08001991static int tipc_wait_for_accept(struct socket *sock, long timeo)
1992{
1993 struct sock *sk = sock->sk;
1994 DEFINE_WAIT(wait);
1995 int err;
1996
1997 /* True wake-one mechanism for incoming connections: only
1998 * one process gets woken up, not the 'whole herd'.
1999 * Since we do not 'race & poll' for established sockets
2000 * anymore, the common case will execute the loop only once.
2001 */
2002 for (;;) {
2003 prepare_to_wait_exclusive(sk_sleep(sk), &wait,
2004 TASK_INTERRUPTIBLE);
Ying Xuefe8e4642014-03-06 14:40:18 +01002005 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
Ying Xue6398e232014-01-17 09:50:04 +08002006 release_sock(sk);
2007 timeo = schedule_timeout(timeo);
2008 lock_sock(sk);
2009 }
2010 err = 0;
2011 if (!skb_queue_empty(&sk->sk_receive_queue))
2012 break;
Ying Xue6398e232014-01-17 09:50:04 +08002013 err = -EAGAIN;
2014 if (!timeo)
2015 break;
Erik Hugne143fe222015-03-09 10:43:42 +01002016 err = sock_intr_errno(timeo);
2017 if (signal_pending(current))
2018 break;
Ying Xue6398e232014-01-17 09:50:04 +08002019 }
2020 finish_wait(sk_sleep(sk), &wait);
2021 return err;
2022}
2023
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002024/**
Ying Xue247f0f32014-02-18 16:06:46 +08002025 * tipc_accept - wait for connection request
Per Lidenb97bf3f2006-01-02 19:04:38 +01002026 * @sock: listening socket
2027 * @newsock: new socket that is to be connected
2028 * @flags: file-related flags associated with socket
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002029 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002030 * Returns 0 on success, errno otherwise
2031 */
Ying Xue247f0f32014-02-18 16:06:46 +08002032static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002033{
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002034 struct sock *new_sk, *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002035 struct sk_buff *buf;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002036 struct tipc_sock *new_tsock;
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002037 struct tipc_msg *msg;
Ying Xue6398e232014-01-17 09:50:04 +08002038 long timeo;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002039 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002040
Allan Stephens0c3141e2008-04-15 00:22:02 -07002041 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002042
Parthasarathy Bhuvaragan0c288c82016-11-01 14:02:43 +01002043 if (sk->sk_state != TIPC_LISTEN) {
Allan Stephens0c3141e2008-04-15 00:22:02 -07002044 res = -EINVAL;
2045 goto exit;
2046 }
Ying Xue6398e232014-01-17 09:50:04 +08002047 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
2048 res = tipc_wait_for_accept(sock, timeo);
2049 if (res)
2050 goto exit;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002051
2052 buf = skb_peek(&sk->sk_receive_queue);
2053
Parthasarathy Bhuvaragancb5da8472016-11-01 14:02:36 +01002054 res = tipc_sk_create(sock_net(sock->sk), new_sock, 0, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002055 if (res)
2056 goto exit;
Stephen Smalleyfdd75ea2015-07-07 09:43:45 -04002057 security_sk_clone(sock->sk, new_sock->sk);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002058
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002059 new_sk = new_sock->sk;
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002060 new_tsock = tipc_sk(new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002061 msg = buf_msg(buf);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002062
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002063 /* we lock on new_sk; but lockdep sees the lock on sk */
2064 lock_sock_nested(new_sk, SINGLE_DEPTH_NESTING);
Allan Stephens0c3141e2008-04-15 00:22:02 -07002065
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002066 /*
2067 * Reject any stray messages received by new socket
2068 * before the socket lock was taken (very, very unlikely)
2069 */
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002070 tsk_rej_rx_queue(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002071
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002072 /* Connect new socket to it's peer */
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002073 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +01002074
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002075 tsk_set_importance(new_tsock, msg_importance(msg));
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002076 if (msg_named(msg)) {
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002077 new_tsock->conn_type = msg_nametype(msg);
2078 new_tsock->conn_instance = msg_nameinst(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002079 }
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002080
2081 /*
2082 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2083 * Respond to 'SYN+' by queuing it on new socket.
2084 */
2085 if (!msg_data_sz(msg)) {
2086 struct msghdr m = {NULL,};
2087
Jon Paul Maloy2e84c602014-08-22 18:09:18 -04002088 tsk_advance_rx_queue(sk);
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002089 __tipc_sendstream(new_sock, &m, 0);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002090 } else {
2091 __skb_dequeue(&sk->sk_receive_queue);
2092 __skb_queue_head(&new_sk->sk_receive_queue, buf);
Ying Xueaba79f32013-01-20 23:30:09 +01002093 skb_set_owner_r(buf, new_sk);
Paul Gortmaker0fef8f22012-12-04 11:01:55 -05002094 }
2095 release_sock(new_sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002096exit:
Allan Stephens0c3141e2008-04-15 00:22:02 -07002097 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002098 return res;
2099}
2100
2101/**
Ying Xue247f0f32014-02-18 16:06:46 +08002102 * tipc_shutdown - shutdown socket connection
Per Lidenb97bf3f2006-01-02 19:04:38 +01002103 * @sock: socket structure
Allan Stephense247a8f2008-03-06 15:05:38 -08002104 * @how: direction to close (must be SHUT_RDWR)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002105 *
2106 * Terminates connection (if necessary), then purges socket's receive queue.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002107 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002108 * Returns 0 on success, errno otherwise
2109 */
Ying Xue247f0f32014-02-18 16:06:46 +08002110static int tipc_shutdown(struct socket *sock, int how)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002111{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002112 struct sock *sk = sock->sk;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002113 int res;
2114
Allan Stephense247a8f2008-03-06 15:05:38 -08002115 if (how != SHUT_RDWR)
2116 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002117
Allan Stephens0c3141e2008-04-15 00:22:02 -07002118 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002119
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002120 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
2121 sk->sk_shutdown = SEND_SHUTDOWN;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002122
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002123 if (sk->sk_state == TIPC_DISCONNECTING) {
Ying Xue75031152012-10-29 09:38:15 -04002124 /* Discard any unreceived messages */
Ying Xue57467e52013-01-20 23:30:08 +01002125 __skb_queue_purge(&sk->sk_receive_queue);
Ying Xue75031152012-10-29 09:38:15 -04002126
2127 /* Wake up anyone sleeping in poll */
2128 sk->sk_state_change(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002129 res = 0;
Parthasarathy Bhuvaragan6f000892016-11-01 14:02:47 +01002130 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +01002131 res = -ENOTCONN;
2132 }
2133
Allan Stephens0c3141e2008-04-15 00:22:02 -07002134 release_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002135 return res;
2136}
2137
Ying Xuef2f2a962015-01-09 15:27:02 +08002138static void tipc_sk_timeout(unsigned long data)
Jon Paul Maloy57289012014-08-22 18:09:09 -04002139{
Ying Xuef2f2a962015-01-09 15:27:02 +08002140 struct tipc_sock *tsk = (struct tipc_sock *)data;
2141 struct sock *sk = &tsk->sk;
Ying Xuea6ca1092014-11-26 11:41:55 +08002142 struct sk_buff *skb = NULL;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002143 u32 peer_port, peer_node;
Jon Paul Maloyc5898632015-02-05 08:36:36 -05002144 u32 own_node = tsk_own_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002145
Jon Paul Maloy57289012014-08-22 18:09:09 -04002146 bh_lock_sock(sk);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002147 if (!tipc_sk_connected(sk)) {
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002148 bh_unlock_sock(sk);
2149 goto exit;
2150 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002151 peer_port = tsk_peer_port(tsk);
2152 peer_node = tsk_peer_node(tsk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002153
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002154 if (tsk->probe_unacked) {
Erik Hugneb3be5e32015-06-09 17:27:12 +02002155 if (!sock_owned_by_user(sk)) {
Parthasarathy Bhuvaragan9fd4b072016-11-01 14:02:46 +01002156 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
Erik Hugneb3be5e32015-06-09 17:27:12 +02002157 tipc_node_remove_conn(sock_net(sk), tsk_peer_node(tsk),
2158 tsk_peer_port(tsk));
2159 sk->sk_state_change(sk);
2160 } else {
2161 /* Try again later */
2162 sk_reset_timer(sk, &sk->sk_timer, (HZ / 20));
2163 }
2164
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002165 bh_unlock_sock(sk);
2166 goto exit;
Jon Paul Maloy57289012014-08-22 18:09:09 -04002167 }
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002168
2169 skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE,
2170 INT_H_SIZE, 0, peer_node, own_node,
2171 peer_port, tsk->portid, TIPC_OK);
Parthasarathy Bhuvaragan8ea642e2016-11-01 14:02:44 +01002172 tsk->probe_unacked = true;
Parthasarathy Bhuvaragan360aab62016-11-01 14:02:41 +01002173 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTERVAL);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002174 bh_unlock_sock(sk);
Ying Xuea6ca1092014-11-26 11:41:55 +08002175 if (skb)
Jon Paul Maloyaf9b0282015-07-16 16:54:24 -04002176 tipc_node_xmit_skb(sock_net(sk), skb, peer_node, tsk->portid);
Jon Paul Maloy6c9808c2014-08-22 18:09:16 -04002177exit:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002178 sock_put(sk);
Jon Paul Maloy57289012014-08-22 18:09:09 -04002179}
2180
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002181static int tipc_sk_publish(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002182 struct tipc_name_seq const *seq)
2183{
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002184 struct sock *sk = &tsk->sk;
2185 struct net *net = sock_net(sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002186 struct publication *publ;
2187 u32 key;
2188
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002189 if (tipc_sk_connected(sk))
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002190 return -EINVAL;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002191 key = tsk->portid + tsk->pub_count + 1;
2192 if (key == tsk->portid)
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002193 return -EADDRINUSE;
2194
Ying Xuef2f98002015-01-09 15:27:05 +08002195 publ = tipc_nametbl_publish(net, seq->type, seq->lower, seq->upper,
Ying Xue07f6c4b2015-01-07 13:41:58 +08002196 scope, tsk->portid, key);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002197 if (unlikely(!publ))
2198 return -EINVAL;
2199
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002200 list_add(&publ->pport_list, &tsk->publications);
2201 tsk->pub_count++;
2202 tsk->published = 1;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002203 return 0;
2204}
2205
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002206static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002207 struct tipc_name_seq const *seq)
2208{
Ying Xuef2f98002015-01-09 15:27:05 +08002209 struct net *net = sock_net(&tsk->sk);
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002210 struct publication *publ;
2211 struct publication *safe;
2212 int rc = -EINVAL;
2213
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002214 list_for_each_entry_safe(publ, safe, &tsk->publications, pport_list) {
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002215 if (seq) {
2216 if (publ->scope != scope)
2217 continue;
2218 if (publ->type != seq->type)
2219 continue;
2220 if (publ->lower != seq->lower)
2221 continue;
2222 if (publ->upper != seq->upper)
2223 break;
Ying Xuef2f98002015-01-09 15:27:05 +08002224 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002225 publ->ref, publ->key);
2226 rc = 0;
2227 break;
2228 }
Ying Xuef2f98002015-01-09 15:27:05 +08002229 tipc_nametbl_withdraw(net, publ->type, publ->lower,
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002230 publ->ref, publ->key);
2231 rc = 0;
2232 }
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002233 if (list_empty(&tsk->publications))
2234 tsk->published = 0;
Jon Paul Maloy0fc87aa2014-08-22 18:09:17 -04002235 return rc;
2236}
2237
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002238/* tipc_sk_reinit: set non-zero address in all existing sockets
2239 * when we go from standalone to network mode.
2240 */
Ying Xuee05b31f2015-01-09 15:27:08 +08002241void tipc_sk_reinit(struct net *net)
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002242{
Ying Xuee05b31f2015-01-09 15:27:08 +08002243 struct tipc_net *tn = net_generic(net, tipc_net_id);
Herbert Xu40f9f432017-02-11 19:26:46 +08002244 struct rhashtable_iter iter;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002245 struct tipc_sock *tsk;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002246 struct tipc_msg *msg;
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002247
Herbert Xu40f9f432017-02-11 19:26:46 +08002248 rhashtable_walk_enter(&tn->sk_rht, &iter);
2249
2250 do {
2251 tsk = ERR_PTR(rhashtable_walk_start(&iter));
2252 if (tsk)
2253 continue;
2254
2255 while ((tsk = rhashtable_walk_next(&iter)) && !IS_ERR(tsk)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002256 spin_lock_bh(&tsk->sk.sk_lock.slock);
2257 msg = &tsk->phdr;
Ying Xue34747532015-01-09 15:27:10 +08002258 msg_set_prevnode(msg, tn->own_addr);
2259 msg_set_orignode(msg, tn->own_addr);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002260 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2261 }
Herbert Xu40f9f432017-02-11 19:26:46 +08002262
2263 rhashtable_walk_stop(&iter);
2264 } while (tsk == ERR_PTR(-EAGAIN));
Ying Xue07f6c4b2015-01-07 13:41:58 +08002265}
2266
Ying Xuee05b31f2015-01-09 15:27:08 +08002267static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
Ying Xue07f6c4b2015-01-07 13:41:58 +08002268{
Ying Xuee05b31f2015-01-09 15:27:08 +08002269 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002270 struct tipc_sock *tsk;
2271
2272 rcu_read_lock();
Herbert Xu6cca72892015-03-20 21:57:05 +11002273 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002274 if (tsk)
2275 sock_hold(&tsk->sk);
2276 rcu_read_unlock();
2277
2278 return tsk;
2279}
2280
2281static int tipc_sk_insert(struct tipc_sock *tsk)
2282{
Ying Xuee05b31f2015-01-09 15:27:08 +08002283 struct sock *sk = &tsk->sk;
2284 struct net *net = sock_net(sk);
2285 struct tipc_net *tn = net_generic(net, tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002286 u32 remaining = (TIPC_MAX_PORT - TIPC_MIN_PORT) + 1;
2287 u32 portid = prandom_u32() % remaining + TIPC_MIN_PORT;
2288
2289 while (remaining--) {
2290 portid++;
2291 if ((portid < TIPC_MIN_PORT) || (portid > TIPC_MAX_PORT))
2292 portid = TIPC_MIN_PORT;
2293 tsk->portid = portid;
2294 sock_hold(&tsk->sk);
Herbert Xu6cca72892015-03-20 21:57:05 +11002295 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2296 tsk_rht_params))
Ying Xue07f6c4b2015-01-07 13:41:58 +08002297 return 0;
2298 sock_put(&tsk->sk);
2299 }
2300
2301 return -1;
2302}
2303
2304static void tipc_sk_remove(struct tipc_sock *tsk)
2305{
2306 struct sock *sk = &tsk->sk;
Ying Xuee05b31f2015-01-09 15:27:08 +08002307 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002308
Herbert Xu6cca72892015-03-20 21:57:05 +11002309 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002310 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2311 __sock_put(sk);
Jon Paul Maloy5a9ee0be2014-08-22 18:09:14 -04002312 }
2313}
2314
Herbert Xu6cca72892015-03-20 21:57:05 +11002315static const struct rhashtable_params tsk_rht_params = {
2316 .nelem_hint = 192,
2317 .head_offset = offsetof(struct tipc_sock, node),
2318 .key_offset = offsetof(struct tipc_sock, portid),
2319 .key_len = sizeof(u32), /* portid */
Herbert Xu6cca72892015-03-20 21:57:05 +11002320 .max_size = 1048576,
2321 .min_size = 256,
Thomas Grafb5e2c152015-03-24 20:42:19 +00002322 .automatic_shrinking = true,
Herbert Xu6cca72892015-03-20 21:57:05 +11002323};
2324
Ying Xuee05b31f2015-01-09 15:27:08 +08002325int tipc_sk_rht_init(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);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002328
Herbert Xu6cca72892015-03-20 21:57:05 +11002329 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002330}
2331
Ying Xuee05b31f2015-01-09 15:27:08 +08002332void tipc_sk_rht_destroy(struct net *net)
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002333{
Ying Xuee05b31f2015-01-09 15:27:08 +08002334 struct tipc_net *tn = net_generic(net, tipc_net_id);
2335
Ying Xue07f6c4b2015-01-07 13:41:58 +08002336 /* Wait for socket readers to complete */
2337 synchronize_net();
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002338
Ying Xuee05b31f2015-01-09 15:27:08 +08002339 rhashtable_destroy(&tn->sk_rht);
Jon Paul Maloy808d90f2014-08-22 18:09:19 -04002340}
2341
2342/**
Ying Xue247f0f32014-02-18 16:06:46 +08002343 * tipc_setsockopt - set socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002344 * @sock: socket structure
2345 * @lvl: option level
2346 * @opt: option identifier
2347 * @ov: pointer to new option value
2348 * @ol: length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002349 *
2350 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002351 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002352 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002353 * Returns 0 on success, errno otherwise
2354 */
Ying Xue247f0f32014-02-18 16:06:46 +08002355static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2356 char __user *ov, unsigned int ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002357{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002358 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002359 struct tipc_sock *tsk = tipc_sk(sk);
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002360 u32 value = 0;
Dan Carpentera08ef472017-01-24 12:49:35 +03002361 int res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002362
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002363 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2364 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002365 if (lvl != SOL_TIPC)
2366 return -ENOPROTOOPT;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002367
2368 switch (opt) {
2369 case TIPC_IMPORTANCE:
2370 case TIPC_SRC_DROPPABLE:
2371 case TIPC_DEST_DROPPABLE:
2372 case TIPC_CONN_TIMEOUT:
2373 if (ol < sizeof(value))
2374 return -EINVAL;
2375 res = get_user(value, (u32 __user *)ov);
2376 if (res)
2377 return res;
2378 break;
2379 default:
2380 if (ov || ol)
2381 return -EINVAL;
2382 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002383
Allan Stephens0c3141e2008-04-15 00:22:02 -07002384 lock_sock(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002385
Per Lidenb97bf3f2006-01-02 19:04:38 +01002386 switch (opt) {
2387 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002388 res = tsk_set_importance(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002389 break;
2390 case TIPC_SRC_DROPPABLE:
2391 if (sock->type != SOCK_STREAM)
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002392 tsk_set_unreliable(tsk, value);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002393 else
Per Lidenb97bf3f2006-01-02 19:04:38 +01002394 res = -ENOPROTOOPT;
2395 break;
2396 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002397 tsk_set_unreturnable(tsk, value);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002398 break;
2399 case TIPC_CONN_TIMEOUT:
Allan Stephensa0f40f02011-05-26 13:44:34 -04002400 tipc_sk(sk)->conn_timeout = value;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002401 break;
Jon Paul Maloy01fd12b2017-01-18 13:50:53 -05002402 case TIPC_MCAST_BROADCAST:
2403 tsk->mc_method.rcast = false;
2404 tsk->mc_method.mandatory = true;
2405 break;
2406 case TIPC_MCAST_REPLICAST:
2407 tsk->mc_method.rcast = true;
2408 tsk->mc_method.mandatory = true;
2409 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002410 default:
2411 res = -EINVAL;
2412 }
2413
Allan Stephens0c3141e2008-04-15 00:22:02 -07002414 release_sock(sk);
2415
Per Lidenb97bf3f2006-01-02 19:04:38 +01002416 return res;
2417}
2418
2419/**
Ying Xue247f0f32014-02-18 16:06:46 +08002420 * tipc_getsockopt - get socket option
Per Lidenb97bf3f2006-01-02 19:04:38 +01002421 * @sock: socket structure
2422 * @lvl: option level
2423 * @opt: option identifier
2424 * @ov: receptacle for option value
2425 * @ol: receptacle for length of option value
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002426 *
2427 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
Per Lidenb97bf3f2006-01-02 19:04:38 +01002428 * (to ease compatibility).
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002429 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002430 * Returns 0 on success, errno otherwise
2431 */
Ying Xue247f0f32014-02-18 16:06:46 +08002432static int tipc_getsockopt(struct socket *sock, int lvl, int opt,
2433 char __user *ov, int __user *ol)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002434{
Allan Stephens0c3141e2008-04-15 00:22:02 -07002435 struct sock *sk = sock->sk;
Jon Paul Maloy58ed9442014-03-12 11:31:12 -04002436 struct tipc_sock *tsk = tipc_sk(sk);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002437 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002438 u32 value;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002439 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002440
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002441 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
2442 return put_user(0, ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002443 if (lvl != SOL_TIPC)
2444 return -ENOPROTOOPT;
Allan Stephens2db99832010-12-31 18:59:33 +00002445 res = get_user(len, ol);
2446 if (res)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002447 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002448
Allan Stephens0c3141e2008-04-15 00:22:02 -07002449 lock_sock(sk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002450
2451 switch (opt) {
2452 case TIPC_IMPORTANCE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002453 value = tsk_importance(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002454 break;
2455 case TIPC_SRC_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002456 value = tsk_unreliable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002457 break;
2458 case TIPC_DEST_DROPPABLE:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002459 value = tsk_unreturnable(tsk);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002460 break;
2461 case TIPC_CONN_TIMEOUT:
Jon Paul Maloy301bae52014-08-22 18:09:20 -04002462 value = tsk->conn_timeout;
Allan Stephens0c3141e2008-04-15 00:22:02 -07002463 /* no need to set "res", since already 0 at this point */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002464 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002465 case TIPC_NODE_RECVQ_DEPTH:
Ying Xue9da3d472012-11-27 06:15:27 -05002466 value = 0; /* was tipc_queue_size, now obsolete */
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002467 break;
Allan Stephens0e659672010-12-31 18:59:32 +00002468 case TIPC_SOCK_RECVQ_DEPTH:
oscar.medina@motorola.com66506132009-06-30 03:25:39 +00002469 value = skb_queue_len(&sk->sk_receive_queue);
2470 break;
Per Lidenb97bf3f2006-01-02 19:04:38 +01002471 default:
2472 res = -EINVAL;
2473 }
2474
Allan Stephens0c3141e2008-04-15 00:22:02 -07002475 release_sock(sk);
2476
Paul Gortmaker25860c32010-12-31 18:59:31 +00002477 if (res)
2478 return res; /* "get" failed */
Per Lidenb97bf3f2006-01-02 19:04:38 +01002479
Paul Gortmaker25860c32010-12-31 18:59:31 +00002480 if (len < sizeof(value))
2481 return -EINVAL;
2482
2483 if (copy_to_user(ov, &value, sizeof(value)))
2484 return -EFAULT;
2485
2486 return put_user(sizeof(value), ol);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002487}
2488
Ying Xuef2f98002015-01-09 15:27:05 +08002489static int tipc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
Erik Hugne78acb1f2014-04-24 16:26:47 +02002490{
Ying Xuef2f98002015-01-09 15:27:05 +08002491 struct sock *sk = sock->sk;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002492 struct tipc_sioc_ln_req lnr;
2493 void __user *argp = (void __user *)arg;
2494
2495 switch (cmd) {
2496 case SIOCGETLINKNAME:
2497 if (copy_from_user(&lnr, argp, sizeof(lnr)))
2498 return -EFAULT;
Ying Xuef2f98002015-01-09 15:27:05 +08002499 if (!tipc_node_get_linkname(sock_net(sk),
2500 lnr.bearer_id & 0xffff, lnr.peer,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002501 lnr.linkname, TIPC_MAX_LINK_NAME)) {
2502 if (copy_to_user(argp, &lnr, sizeof(lnr)))
2503 return -EFAULT;
2504 return 0;
2505 }
2506 return -EADDRNOTAVAIL;
Erik Hugne78acb1f2014-04-24 16:26:47 +02002507 default:
2508 return -ENOIOCTLCMD;
2509 }
2510}
2511
Ben Hutchingsae86b9e2012-07-10 10:55:35 +00002512/* Protocol switches for the various types of TIPC sockets */
2513
Florian Westphalbca65ea2008-02-07 18:18:01 -08002514static const struct proto_ops msg_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002515 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002516 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002517 .release = tipc_release,
2518 .bind = tipc_bind,
2519 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002520 .socketpair = sock_no_socketpair,
Ying Xue245f3d32011-07-06 06:01:13 -04002521 .accept = sock_no_accept,
Ying Xue247f0f32014-02-18 16:06:46 +08002522 .getname = tipc_getname,
2523 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002524 .ioctl = tipc_ioctl,
Ying Xue245f3d32011-07-06 06:01:13 -04002525 .listen = sock_no_listen,
Ying Xue247f0f32014-02-18 16:06:46 +08002526 .shutdown = tipc_shutdown,
2527 .setsockopt = tipc_setsockopt,
2528 .getsockopt = tipc_getsockopt,
2529 .sendmsg = tipc_sendmsg,
2530 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002531 .mmap = sock_no_mmap,
2532 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002533};
2534
Florian Westphalbca65ea2008-02-07 18:18:01 -08002535static const struct proto_ops packet_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002536 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002537 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002538 .release = tipc_release,
2539 .bind = tipc_bind,
2540 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002541 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002542 .accept = tipc_accept,
2543 .getname = tipc_getname,
2544 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002545 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002546 .listen = tipc_listen,
2547 .shutdown = tipc_shutdown,
2548 .setsockopt = tipc_setsockopt,
2549 .getsockopt = tipc_getsockopt,
2550 .sendmsg = tipc_send_packet,
2551 .recvmsg = tipc_recvmsg,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002552 .mmap = sock_no_mmap,
2553 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002554};
2555
Florian Westphalbca65ea2008-02-07 18:18:01 -08002556static const struct proto_ops stream_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002557 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002558 .family = AF_TIPC,
Ying Xue247f0f32014-02-18 16:06:46 +08002559 .release = tipc_release,
2560 .bind = tipc_bind,
2561 .connect = tipc_connect,
Allan Stephens5eee6a62007-06-10 17:24:55 -07002562 .socketpair = sock_no_socketpair,
Ying Xue247f0f32014-02-18 16:06:46 +08002563 .accept = tipc_accept,
2564 .getname = tipc_getname,
2565 .poll = tipc_poll,
Erik Hugne78acb1f2014-04-24 16:26:47 +02002566 .ioctl = tipc_ioctl,
Ying Xue247f0f32014-02-18 16:06:46 +08002567 .listen = tipc_listen,
2568 .shutdown = tipc_shutdown,
2569 .setsockopt = tipc_setsockopt,
2570 .getsockopt = tipc_getsockopt,
Jon Paul Maloy365ad352017-01-03 10:55:11 -05002571 .sendmsg = tipc_sendstream,
Ying Xue247f0f32014-02-18 16:06:46 +08002572 .recvmsg = tipc_recv_stream,
YOSHIFUJI Hideaki82387452007-07-19 10:44:56 +09002573 .mmap = sock_no_mmap,
2574 .sendpage = sock_no_sendpage
Per Lidenb97bf3f2006-01-02 19:04:38 +01002575};
2576
Florian Westphalbca65ea2008-02-07 18:18:01 -08002577static const struct net_proto_family tipc_family_ops = {
Allan Stephens0e659672010-12-31 18:59:32 +00002578 .owner = THIS_MODULE,
Per Lidenb97bf3f2006-01-02 19:04:38 +01002579 .family = AF_TIPC,
Ying Xuec5fa7b32013-06-17 10:54:39 -04002580 .create = tipc_sk_create
Per Lidenb97bf3f2006-01-02 19:04:38 +01002581};
2582
2583static struct proto tipc_proto = {
2584 .name = "TIPC",
2585 .owner = THIS_MODULE,
Ying Xuecc79dd12013-06-17 10:54:37 -04002586 .obj_size = sizeof(struct tipc_sock),
2587 .sysctl_rmem = sysctl_tipc_rmem
Per Lidenb97bf3f2006-01-02 19:04:38 +01002588};
2589
2590/**
Per Liden4323add2006-01-18 00:38:21 +01002591 * tipc_socket_init - initialize TIPC socket interface
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002592 *
Per Lidenb97bf3f2006-01-02 19:04:38 +01002593 * Returns 0 on success, errno otherwise
2594 */
Per Liden4323add2006-01-18 00:38:21 +01002595int tipc_socket_init(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002596{
2597 int res;
2598
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09002599 res = proto_register(&tipc_proto, 1);
Per Lidenb97bf3f2006-01-02 19:04:38 +01002600 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002601 pr_err("Failed to register TIPC protocol type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002602 goto out;
2603 }
2604
2605 res = sock_register(&tipc_family_ops);
2606 if (res) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -04002607 pr_err("Failed to register TIPC socket type\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01002608 proto_unregister(&tipc_proto);
2609 goto out;
2610 }
Per Lidenb97bf3f2006-01-02 19:04:38 +01002611 out:
2612 return res;
2613}
2614
2615/**
Per Liden4323add2006-01-18 00:38:21 +01002616 * tipc_socket_stop - stop TIPC socket interface
Per Lidenb97bf3f2006-01-02 19:04:38 +01002617 */
Per Liden4323add2006-01-18 00:38:21 +01002618void tipc_socket_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +01002619{
Per Lidenb97bf3f2006-01-02 19:04:38 +01002620 sock_unregister(tipc_family_ops.family);
2621 proto_unregister(&tipc_proto);
2622}
Richard Alpe34b78a122014-11-20 10:29:10 +01002623
2624/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002625static int __tipc_nl_add_sk_con(struct sk_buff *skb, struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002626{
2627 u32 peer_node;
2628 u32 peer_port;
2629 struct nlattr *nest;
2630
2631 peer_node = tsk_peer_node(tsk);
2632 peer_port = tsk_peer_port(tsk);
2633
2634 nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
2635
2636 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
2637 goto msg_full;
2638 if (nla_put_u32(skb, TIPC_NLA_CON_SOCK, peer_port))
2639 goto msg_full;
2640
2641 if (tsk->conn_type != 0) {
2642 if (nla_put_flag(skb, TIPC_NLA_CON_FLAG))
2643 goto msg_full;
2644 if (nla_put_u32(skb, TIPC_NLA_CON_TYPE, tsk->conn_type))
2645 goto msg_full;
2646 if (nla_put_u32(skb, TIPC_NLA_CON_INST, tsk->conn_instance))
2647 goto msg_full;
2648 }
2649 nla_nest_end(skb, nest);
2650
2651 return 0;
2652
2653msg_full:
2654 nla_nest_cancel(skb, nest);
2655
2656 return -EMSGSIZE;
2657}
2658
2659/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002660static int __tipc_nl_add_sk(struct sk_buff *skb, struct netlink_callback *cb,
2661 struct tipc_sock *tsk)
Richard Alpe34b78a122014-11-20 10:29:10 +01002662{
2663 int err;
2664 void *hdr;
2665 struct nlattr *attrs;
Ying Xue34747532015-01-09 15:27:10 +08002666 struct net *net = sock_net(skb->sk);
2667 struct tipc_net *tn = net_generic(net, tipc_net_id);
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002668 struct sock *sk = &tsk->sk;
Richard Alpe34b78a122014-11-20 10:29:10 +01002669
2670 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002671 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_SOCK_GET);
Richard Alpe34b78a122014-11-20 10:29:10 +01002672 if (!hdr)
2673 goto msg_cancel;
2674
2675 attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
2676 if (!attrs)
2677 goto genlmsg_cancel;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002678 if (nla_put_u32(skb, TIPC_NLA_SOCK_REF, tsk->portid))
Richard Alpe34b78a122014-11-20 10:29:10 +01002679 goto attr_msg_cancel;
Ying Xue34747532015-01-09 15:27:10 +08002680 if (nla_put_u32(skb, TIPC_NLA_SOCK_ADDR, tn->own_addr))
Richard Alpe34b78a122014-11-20 10:29:10 +01002681 goto attr_msg_cancel;
2682
Parthasarathy Bhuvaragand6fb7e92016-11-01 14:02:40 +01002683 if (tipc_sk_connected(sk)) {
Richard Alpe34b78a122014-11-20 10:29:10 +01002684 err = __tipc_nl_add_sk_con(skb, tsk);
2685 if (err)
2686 goto attr_msg_cancel;
2687 } else if (!list_empty(&tsk->publications)) {
2688 if (nla_put_flag(skb, TIPC_NLA_SOCK_HAS_PUBL))
2689 goto attr_msg_cancel;
2690 }
2691 nla_nest_end(skb, attrs);
2692 genlmsg_end(skb, hdr);
2693
2694 return 0;
2695
2696attr_msg_cancel:
2697 nla_nest_cancel(skb, attrs);
2698genlmsg_cancel:
2699 genlmsg_cancel(skb, hdr);
2700msg_cancel:
2701 return -EMSGSIZE;
2702}
2703
2704int tipc_nl_sk_dump(struct sk_buff *skb, struct netlink_callback *cb)
2705{
2706 int err;
2707 struct tipc_sock *tsk;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002708 const struct bucket_table *tbl;
2709 struct rhash_head *pos;
Ying Xuee05b31f2015-01-09 15:27:08 +08002710 struct net *net = sock_net(skb->sk);
2711 struct tipc_net *tn = net_generic(net, tipc_net_id);
Richard Alped6e164e2015-01-16 12:30:40 +01002712 u32 tbl_id = cb->args[0];
2713 u32 prev_portid = cb->args[1];
Richard Alpe34b78a122014-11-20 10:29:10 +01002714
Ying Xue07f6c4b2015-01-07 13:41:58 +08002715 rcu_read_lock();
Ying Xuee05b31f2015-01-09 15:27:08 +08002716 tbl = rht_dereference_rcu((&tn->sk_rht)->tbl, &tn->sk_rht);
Richard Alped6e164e2015-01-16 12:30:40 +01002717 for (; tbl_id < tbl->size; tbl_id++) {
2718 rht_for_each_entry_rcu(tsk, pos, tbl, tbl_id, node) {
Ying Xue07f6c4b2015-01-07 13:41:58 +08002719 spin_lock_bh(&tsk->sk.sk_lock.slock);
Richard Alped6e164e2015-01-16 12:30:40 +01002720 if (prev_portid && prev_portid != tsk->portid) {
2721 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2722 continue;
2723 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002724
Richard Alped6e164e2015-01-16 12:30:40 +01002725 err = __tipc_nl_add_sk(skb, cb, tsk);
2726 if (err) {
2727 prev_portid = tsk->portid;
2728 spin_unlock_bh(&tsk->sk.sk_lock.slock);
2729 goto out;
2730 }
2731 prev_portid = 0;
2732 spin_unlock_bh(&tsk->sk.sk_lock.slock);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002733 }
Richard Alpe34b78a122014-11-20 10:29:10 +01002734 }
Richard Alped6e164e2015-01-16 12:30:40 +01002735out:
Ying Xue07f6c4b2015-01-07 13:41:58 +08002736 rcu_read_unlock();
Richard Alped6e164e2015-01-16 12:30:40 +01002737 cb->args[0] = tbl_id;
2738 cb->args[1] = prev_portid;
Richard Alpe34b78a122014-11-20 10:29:10 +01002739
2740 return skb->len;
2741}
Richard Alpe1a1a1432014-11-20 10:29:11 +01002742
2743/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002744static int __tipc_nl_add_sk_publ(struct sk_buff *skb,
2745 struct netlink_callback *cb,
2746 struct publication *publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002747{
2748 void *hdr;
2749 struct nlattr *attrs;
2750
2751 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
Richard Alpebfb3e5d2015-02-09 09:50:03 +01002752 &tipc_genl_family, NLM_F_MULTI, TIPC_NL_PUBL_GET);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002753 if (!hdr)
2754 goto msg_cancel;
2755
2756 attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
2757 if (!attrs)
2758 goto genlmsg_cancel;
2759
2760 if (nla_put_u32(skb, TIPC_NLA_PUBL_KEY, publ->key))
2761 goto attr_msg_cancel;
2762 if (nla_put_u32(skb, TIPC_NLA_PUBL_TYPE, publ->type))
2763 goto attr_msg_cancel;
2764 if (nla_put_u32(skb, TIPC_NLA_PUBL_LOWER, publ->lower))
2765 goto attr_msg_cancel;
2766 if (nla_put_u32(skb, TIPC_NLA_PUBL_UPPER, publ->upper))
2767 goto attr_msg_cancel;
2768
2769 nla_nest_end(skb, attrs);
2770 genlmsg_end(skb, hdr);
2771
2772 return 0;
2773
2774attr_msg_cancel:
2775 nla_nest_cancel(skb, attrs);
2776genlmsg_cancel:
2777 genlmsg_cancel(skb, hdr);
2778msg_cancel:
2779 return -EMSGSIZE;
2780}
2781
2782/* Caller should hold socket lock for the passed tipc socket. */
Richard Alped8182802014-11-24 11:10:29 +01002783static int __tipc_nl_list_sk_publ(struct sk_buff *skb,
2784 struct netlink_callback *cb,
2785 struct tipc_sock *tsk, u32 *last_publ)
Richard Alpe1a1a1432014-11-20 10:29:11 +01002786{
2787 int err;
2788 struct publication *p;
2789
2790 if (*last_publ) {
2791 list_for_each_entry(p, &tsk->publications, pport_list) {
2792 if (p->key == *last_publ)
2793 break;
2794 }
2795 if (p->key != *last_publ) {
2796 /* We never set seq or call nl_dump_check_consistent()
2797 * this means that setting prev_seq here will cause the
2798 * consistence check to fail in the netlink callback
2799 * handler. Resulting in the last NLMSG_DONE message
2800 * having the NLM_F_DUMP_INTR flag set.
2801 */
2802 cb->prev_seq = 1;
2803 *last_publ = 0;
2804 return -EPIPE;
2805 }
2806 } else {
2807 p = list_first_entry(&tsk->publications, struct publication,
2808 pport_list);
2809 }
2810
2811 list_for_each_entry_from(p, &tsk->publications, pport_list) {
2812 err = __tipc_nl_add_sk_publ(skb, cb, p);
2813 if (err) {
2814 *last_publ = p->key;
2815 return err;
2816 }
2817 }
2818 *last_publ = 0;
2819
2820 return 0;
2821}
2822
2823int tipc_nl_publ_dump(struct sk_buff *skb, struct netlink_callback *cb)
2824{
2825 int err;
Ying Xue07f6c4b2015-01-07 13:41:58 +08002826 u32 tsk_portid = cb->args[0];
Richard Alpe1a1a1432014-11-20 10:29:11 +01002827 u32 last_publ = cb->args[1];
2828 u32 done = cb->args[2];
Ying Xuee05b31f2015-01-09 15:27:08 +08002829 struct net *net = sock_net(skb->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002830 struct tipc_sock *tsk;
2831
Ying Xue07f6c4b2015-01-07 13:41:58 +08002832 if (!tsk_portid) {
Richard Alpe1a1a1432014-11-20 10:29:11 +01002833 struct nlattr **attrs;
2834 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
2835
2836 err = tipc_nlmsg_parse(cb->nlh, &attrs);
2837 if (err)
2838 return err;
2839
Richard Alpe45e093a2016-05-16 11:14:54 +02002840 if (!attrs[TIPC_NLA_SOCK])
2841 return -EINVAL;
2842
Richard Alpe1a1a1432014-11-20 10:29:11 +01002843 err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
2844 attrs[TIPC_NLA_SOCK],
2845 tipc_nl_sock_policy);
2846 if (err)
2847 return err;
2848
2849 if (!sock[TIPC_NLA_SOCK_REF])
2850 return -EINVAL;
2851
Ying Xue07f6c4b2015-01-07 13:41:58 +08002852 tsk_portid = nla_get_u32(sock[TIPC_NLA_SOCK_REF]);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002853 }
2854
2855 if (done)
2856 return 0;
2857
Ying Xuee05b31f2015-01-09 15:27:08 +08002858 tsk = tipc_sk_lookup(net, tsk_portid);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002859 if (!tsk)
2860 return -EINVAL;
2861
2862 lock_sock(&tsk->sk);
2863 err = __tipc_nl_list_sk_publ(skb, cb, tsk, &last_publ);
2864 if (!err)
2865 done = 1;
2866 release_sock(&tsk->sk);
Ying Xue07f6c4b2015-01-07 13:41:58 +08002867 sock_put(&tsk->sk);
Richard Alpe1a1a1432014-11-20 10:29:11 +01002868
Ying Xue07f6c4b2015-01-07 13:41:58 +08002869 cb->args[0] = tsk_portid;
Richard Alpe1a1a1432014-11-20 10:29:11 +01002870 cb->args[1] = last_publ;
2871 cb->args[2] = done;
2872
2873 return skb->len;
2874}